]> TLD Linux GIT Repositories - packages/udev.git/blob - start_udev
- updated to 3.2.14
[packages/udev.git] / start_udev
1 #!/bin/sh
2 #
3 # start_udev
4 #
5 # script to initialize /dev by using udev.
6 #
7 # Copyright (C) 2004 Greg Kroah-Hartman <greg@kroah.com>
8 #
9 # Released under the GPL v2 only.
10 #
11 # This needs to be run at the earliest possible point in the boot
12 # process.
13 #
14 # Based on the udev init.d script
15 #
16 # Thanks go out to the Gentoo developers for proving
17 # that this is possible to do.
18 #
19 # Yes, it's very verbose, feel free to turn off all of the echo calls,
20 # they were there to make me feel better that everything was working
21 # properly during development...
22
23 # default value, if no config present.
24 udev_root="/dev"
25 sysfs_dir="/sys"
26 udevd_timeout=8
27
28 # don't use udev if sysfs is not mounted.
29 [ -d $sysfs_dir/class ] || exit 1
30 [ -r /proc/mounts ] || exit 1
31 [ -f /etc/udev/udev.conf ] && . /etc/udev/udev.conf
32
33 . /etc/rc.d/init.d/functions
34
35 prog=udev
36 bin=/sbin/udev
37 udevd=/lib/udev/udevd
38 # trim traling slash, code expects it not to be there
39 udev_root=${udev_root%/}
40
41 create_static_nodes() {
42         /sbin/kmod static-nodes --format=tmpfiles | \
43         while read type file mode uid gid age dev ; do
44                 case $type in
45                         d|D)
46                                 if ! test -d $file; then
47                                         mkdir -p --mode=$mode $file
48                                 fi
49                                 ;;
50                         *)
51                                 oldIFS=$IFS
52                                 IFS=":"
53                                 set -- $dev
54                                 maj=$1
55                                 min=$2
56                                 IFS=$oldIFS
57                                 if ! test -e $file; then
58                                         mknod --mode=$mode $file $type $maj $min
59                                 fi
60                                 ;;
61                 esac
62                 [ $uid = "-" ] || chown $uid $file
63                 [ $gid = "-" ] || chgrp $gid $file
64         done
65 }
66
67 make_extra_nodes() {
68         grep '^[^#]' /etc/udev/links.conf | \
69         while read type name arg1; do
70                 [ "$type" -a "$name" -a ! -e "$udev_root/$name" -a ! -L "/dev/$name" ] ||continue
71                 case "$type" in
72                         L)
73                                 if ! test -h $udev_root/$name; then
74                                         ln -s $arg1 $udev_root/$name
75                                 fi
76                                 ;;
77                         D)
78                                 if ! test -d $udev_root/$name; then
79                                         mkdir -p $udev_root/$name
80                                 fi
81                                 ;;
82                         M)
83                                 if ! test -e /dev/$name; then
84                                         mknod -m 600 /dev/$name $arg1
85                                 fi
86                                 ;;
87                         *)
88                                 echo "links.conf: unparseable line ($type $name $arg1)"
89                                 ;;
90                 esac
91         done
92         [ -d /lib/udev/devices ] && cp -a /lib/udev/devices/* /dev/ >/dev/null 2>&1 || :
93         [ -d /lib64/udev/devices ] && cp -a /lib64/udev/devices/* /dev/ >/dev/null 2>&1 || :
94 }
95
96 kill_udevd() {
97         if [ -x /sbin/pidof ]; then
98                 pid=$(/sbin/pidof -x udevd)
99                 [ -n "$pid" ] && kill $pid
100         fi
101 }
102
103 set_hotplug_handler() {
104         echo "" > /proc/sys/kernel/hotplug
105 }
106
107 # find subdirs mounted under $udev_root
108 get_dev_mounts() {
109         awk -vudev_root="$udev_root/" '
110         BEGIN {
111           len = length(udev_root);
112         }
113
114         substr($2, 1, len) == udev_root {
115           print substr($2, len + 1)
116         }' /proc/mounts
117 }
118
119 show "Starting udev"; busy
120
121 export ACTION=add
122 prog=udev
123 ret=0
124
125 # mount the devtmpfs on $udev_root, if not already done
126 awk "\$2 == \"$udev_root\" && \$3 == \"devtmpfs\" { exit 1 }" /proc/mounts && {
127         submounts=$(get_dev_mounts)
128
129         if [ "$submounts" ]; then
130                 # mount to temporary location to be able to move submounts
131                 # this needs writable TMPDIR:-/tmp, so it won't work in early boot
132                 # but fix is simple: use initramfs instead of romfs
133                 devdir=$(mktemp -d ${TMPDIR:-/tmp}/tmpXXXXXX)
134         else
135                 devdir=$udev_root
136         fi
137         mount -n -o mode=0755 -t devtmpfs devtmpfs "$devdir"
138         ret=$(( $ret + $? ))
139
140         # relocate submounts
141         for dir in $submounts; do
142                 mount -n --move $udev_root/$dir $devdir/$dir
143                 ret=$(( $ret + $? ))
144         done
145
146         if [ "$submounts" ]; then
147                 mount -n --move $devdir $udev_root
148                 rmdir $devdir
149         fi
150 }
151
152 kill_udevd > "$udev_root/null" 2>&1
153
154 # Create required static device nodes for the current kernel
155 create_static_nodes
156
157 # Start udevd daemon
158 $udevd --daemon; rc=$?
159 test $rc -eq 0 && ok || fail
160 ret=$(( $ret + $rc ))
161
162 # Making extra nodes
163 show "Setup extra nodes"; busy
164 make_extra_nodes; rc=$?
165 test $rc -eq 0 && ok || fail
166 ret=$(( $ret + $rc ))
167
168 if [ -f /sys/class/tty/console/uevent ]; then
169         # Setting default hotplug handler
170         set_hotplug_handler
171         ret=$(( $ret + $? ))
172
173         # retrigger all events
174         show "Retrigger subsystems events"; busy
175         /sbin/udevadm trigger --type=subsystems --action=add; rc=$?
176         test $rc -eq 0 && ok || fail
177         ret=$(( $ret + $rc ))
178
179         show "Retrigger devices events"; busy
180         /sbin/udevadm trigger --type=devices --action=add; rc=$?
181         test $rc -eq 0 && ok || fail
182         ret=$(( $ret + $rc ))
183
184         # wait for the events to finish
185         show "udevadm settle"; busy
186         /sbin/udevadm settle; rc=$?
187         test $rc -eq 0 && ok || fail
188         ret=$(( $ret + $rc ))
189 else
190         echo "Kernel too old for this udev version"
191 fi
192
193 exit $ret