]> git.ipfire.org Git - thirdparty/dracut.git/blame - modules.d/99base/init
add dracut.xml documentation
[thirdparty/dracut.git] / modules.d / 99base / init
CommitLineData
7f64a3fe 1#!/bin/sh
cc02093d
HH
2# -*- mode: shell-script; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
3# ex: ts=8 sw=4 sts=4 et filetype=sh
a5e56335
JK
4#
5# Licensed under the GPLv2
6#
cc02093d 7# Copyright 2008-2010, Red Hat, Inc.
ac36d5db 8# Harald Hoyer <harald@redhat.com>
a5e56335 9# Jeremy Katz <katzj@redhat.com>
ec9315e5 10
ac36d5db
HH
11wait_for_loginit()
12{
13 if getarg rdinitdebug; then
cc02093d
HH
14 set +x
15 exec 0<>/dev/console 1<>/dev/console 2<>/dev/console
16 # wait for loginit
17 i=0
18 while [ $i -lt 10 ]; do
19 j=$(jobs)
20 [ -z "$j" ] && break
21 [ -z "${j##*Running*}" ] || break
22 sleep 0.1
23 i=$(($i+1))
24 done
25 [ $i -eq 10 ] && kill %1 >/dev/null 2>&1
ac36d5db
HH
26
27 while pidof -x /sbin/loginit >/dev/null 2>&1; do
28 for pid in $(pidof -x /sbin/loginit); do
29 kill $HARD $pid >/dev/null 2>&1
30 done
31 HARD="-9"
32 done
cc02093d 33 set -x
ac36d5db
HH
34 fi
35}
36
ec9315e5
JK
37emergency_shell()
38{
5db587d7 39 if [ "$1" = "-n" ]; then
b5664d5d
LB
40 _rdshell_name=$2
41 shift 2
42 else
43 _rdshell_name=dracut
44 fi
ac36d5db 45 wait_for_loginit
a3a3448d
HH
46 echo ; echo
47 echo $@
0fd1ab9d 48 source_all emergency
81f83dab 49 echo
dacba778 50 if getarg rdshell || getarg rdbreak; then
81f83dab 51 echo "Dropping to debug shell."
a3a3448d 52 echo
b5664d5d
LB
53 export PS1="$_rdshell_name:\${PWD}# "
54 [ -e /.profile ] || echo "exec 0<>/dev/console 1<>/dev/console 2<>/dev/console" > /.profile
55 sh -i -l
a3a3448d 56 else
c3cd4c76 57 echo "Boot has failed, sleeping forever."
81f83dab 58 while :; do sleep 365d;done
a3a3448d 59 fi
ec9315e5 60}
ec9315e5 61
ec9315e5
JK
62export PATH=/sbin:/bin:/usr/sbin:/usr/bin
63export TERM=linux
bb040f93
HH
64NEWROOT="/sysroot"
65
a75c29be 66trap "emergency_shell Signal caught!" 0
e0013593 67# set umask
db3641a2 68OLD_UMASK=$(umask)
e0013593 69umask 0007
a890fe29 70
52aeb3f3 71RDDEBUG=""
b8f39ef1 72. /lib/dracut-lib.sh
df44688f 73
e0013593 74mknod -m 0666 /dev/null c 1 3
a890fe29 75
ec9315e5 76# mount some important things
a890fe29
HH
77mount -t proc /proc /proc >/dev/null 2>&1
78mount -t sysfs /sys /sys >/dev/null 2>&1
a890fe29 79
3473f5d9 80if [ ! -c /dev/ptmx ]; then
e0364f95
HH
81 # try to mount devtmpfs
82 if ! mount -t devtmpfs -omode=0755 udev /dev >/dev/null 2>&1; then
83 # if it failed fall back to normal tmpfs
cc02093d
HH
84 mount -t tmpfs -omode=0755 udev /dev >/dev/null 2>&1
85 # Make some basic devices first, let udev handle the rest
86 mknod -m 0666 /dev/null c 1 3
87 mknod -m 0666 /dev/ptmx c 5 2
88 mknod -m 0600 /dev/console c 5 1
89 mknod -m 0660 /dev/kmsg c 1 11
3473f5d9
HH
90 fi
91fi
d0e2110a 92
f2a716d4
HH
93ln -s /proc/self/fd /dev/fd >/dev/null 2>&1
94
4592b698
HH
95if getarg rdinitdebug; then
96 getarg quiet && DRACUT_QUIET="yes"
97 mkfifo /dev/initlog.pipe
98 /sbin/loginit $DRACUT_QUIET </dev/initlog.pipe >/dev/console 2>&1 &
99 exec >/dev/initlog.pipe 2>&1
ac36d5db
HH
100else
101 exec 0<>/dev/console 1<>/dev/console 2<>/dev/console
4592b698 102fi
ac36d5db 103
52aeb3f3 104setdebug
83701d66 105
d0e2110a 106mkdir /dev/shm
0c70846e 107mkdir /dev/pts
f2a716d4
HH
108mount -t devpts -o gid=5,mode=620 devpts /dev/pts >/dev/null 2>&1
109mount -t tmpfs tmpfs /dev/shm >/dev/null 2>&1
c492fe12
HH
110mkdir -m 0755 /dev/.udev
111mkdir -m 0755 /dev/.udev/rules.d
0c70846e 112
eab677a2
HH
113UDEVVERSION=$(udevadm --version)
114
0375106c
HH
115source_conf /etc/conf.d
116
e4502814 117# run scriptlets to parse the command line
b5664d5d 118getarg 'rdbreak=cmdline' && emergency_shell -n cmdline "Break before cmdline"
e4502814
DD
119source_all cmdline
120
0fd1ab9d
PS
121[ -z "$root" ] && die "No or empty root= argument"
122[ -z "$rootok" ] && die "Don't know how to handle 'root=$root'"
d15c7a1a 123
7d7efa4a
DD
124# Network root scripts may need updated root= options,
125# so deposit them where they can see them (udev purges the env)
126{
127 echo "root='$root'"
128 echo "rflags='$rflags'"
129 echo "fstype='$fstype'"
2734a875 130 echo "netroot='$netroot'"
7d7efa4a
DD
131 echo "NEWROOT='$NEWROOT'"
132} > /tmp/root.info
133
a08c1f45 134# pre-udev scripts run before udev starts, and are run only once.
b5664d5d 135getarg 'rdbreak=pre-udev' && emergency_shell -n pre-udev "Break before pre-udev"
ec1ad334
VL
136source_all pre-udev
137
ec9315e5 138# start up udev and trigger cold plugs
0e0031dc 139udevd --daemon --resolve-names=never
2a652c0b
HH
140
141UDEV_LOG_PRIO_ARG=--log-priority
142UDEV_QUEUE_EMPTY="udevadm settle --timeout=0"
143
144if [ $UDEVVERSION -lt 140 ]; then
cc02093d
HH
145 UDEV_LOG_PRIO_ARG=--log_priority
146 UDEV_QUEUE_EMPTY="udevadm settle --timeout=1"
2a652c0b
HH
147fi
148
149getarg rdudevinfo && udevadm control $UDEV_LOG_PRIO_ARG=info
150getarg rdudevdebug && udevadm control $UDEV_LOG_PRIO_ARG=debug
1eeddd31 151
b5664d5d 152getarg 'rdbreak=pre-trigger' && emergency_shell -n pre-trigger "Break before pre-trigger"
1eeddd31
HH
153source_all pre-trigger
154
155# then the rest
cd83e4c5 156udevadm trigger --action=add $udevtriggeropts >/dev/null 2>&1
eab677a2 157
b5664d5d 158getarg 'rdbreak=initqueue' && emergency_shell -n initqueue "Break before initqueue"
53e1c5b9 159
85fd75f9
HH
160RDRETRY=$(getarg 'rd_retry=')
161RDRETRY=${RDRETRY:-20}
162RDRETRY=$(($RDRETRY*2))
163
58dbb43e
HH
164i=0
165while :; do
58dbb43e 166
8238850c 167 check_finished && break
cc02093d 168
5c6a593f 169 udevsettle
89056045 170
8238850c
HH
171 check_finished && break
172
58dbb43e
HH
173 if [ -f /initqueue/work ]; then
174 rm /initqueue/work
58dbb43e 175 fi
cc02093d 176
58dbb43e 177 for job in /initqueue/*.sh; do
cc02093d 178 [ -e "$job" ] || break
58dbb43e 179 job=$job . $job
8238850c 180 check_finished && break 2
58dbb43e
HH
181 done
182
8238850c 183 $UDEV_QUEUE_EMPTY >/dev/null 2>&1 || continue
71094bee 184
8238850c 185 modprobe scsi_wait_scan && rmmod scsi_wait_scan
71094bee 186
8238850c 187 $UDEV_QUEUE_EMPTY >/dev/null 2>&1 || continue
71094bee 188
8238850c
HH
189 for job in /initqueue-settled/*.sh; do
190 [ -e "$job" ] || break
191 job=$job . $job
192 check_finished && break 2
193 done
273b197d 194
8238850c 195 $UDEV_QUEUE_EMPTY >/dev/null 2>&1 || continue
273b197d 196
8238850c
HH
197 # no more udev jobs and queues empty.
198 sleep 0.5
0e0bf830
HH
199
200 # dirty hack for some cdrom drives,
201 # which report no medium for quiet
202 # some time.
203 for cdrom in /sys/block/sr*; do
cc02093d
HH
204 [ -e "$cdrom" ] || continue
205 # skip, if cdrom medium was already found
0e0bf830 206 strstr "$(udevadm info --query=env --path=${cdrom##/sys})" \
cc02093d 207 ID_CDROM_MEDIA && continue
2937028c 208 echo change > "$cdrom/uevent"
0e0bf830
HH
209 done
210
8238850c 211 i=$(($i+1))
85fd75f9 212 [ $i -gt $RDRETRY ] \
8238850c 213 && { flock -s 9 ; emergency_shell "No root device found"; } 9>/.console_lock
58dbb43e
HH
214done
215unset job
216unset queuetriggered
ec9315e5 217
a08c1f45
VL
218# pre-mount happens before we try to mount the root filesystem,
219# and happens once.
b5664d5d 220getarg 'rdbreak=pre-mount' && emergency_shell -n pre-mount "Break pre-mount"
ec1ad334 221source_all pre-mount
bb040f93 222
a3a3448d 223
b5664d5d 224getarg 'rdbreak=mount' && emergency_shell -n mount "Break mount"
a08c1f45
VL
225# mount scripts actually try to mount the root filesystem, and may
226# be sourced any number of times. As soon as one suceeds, no more are sourced.
87930555 227i=0
a08c1f45 228while :; do
37dcb400 229 [ -d "$NEWROOT/proc" ] && break;
a08c1f45 230 for f in /mount/*.sh; do
cc02093d
HH
231 [ -f "$f" ] && . "$f"
232 [ -d "$NEWROOT/proc" ] && break;
a08c1f45 233 done
bb040f93 234
7cd15fe7 235 i=$(($i+1))
a3a3448d
HH
236 [ $i -gt 20 ] \
237 && { flock -s 9 ; emergency_shell "Can't mount root filesystem"; } 9>/.console_lock
a08c1f45
VL
238done
239
0b2de1f8
HH
240{
241 echo -n "Mounted root filesystem "
242 while read dev mp rest; do [ "$mp" = "$NEWROOT" ] && echo $dev; done < /proc/mounts
243} | vinfo
244
b5c7c7e8 245# pre pivot scripts are sourced just before we switch over to the new root.
b5664d5d 246getarg 'rdbreak=pre-pivot' && emergency_shell -n pre-pivot "Break pre-pivot"
88ffd2df
VL
247source_all pre-pivot
248
a08c1f45 249# by the time we get here, the root filesystem should be mounted.
d05f3c43
VL
250# Try to find init.
251for i in "$(getarg init=)" /sbin/init /etc/init /init /bin/sh; do
252 [ -f "$NEWROOT$i" -a -x "$NEWROOT$i" ] && { INIT="$i"; break; }
253done
0b2de1f8 254
6b0daf2e 255[ "$INIT" ] || {
a3a3448d
HH
256 echo "Cannot find init!"
257 echo "Please check to make sure you passed a valid root filesystem!"
d05f3c43 258 emergency_shell
6b0daf2e 259}
a08c1f45 260
b5664d5d 261getarg rdbreak && emergency_shell -n switch_root "Break before switch_root"
7e87a0bf 262
de439e24
HH
263# stop udev queue before killing it
264udevadm control --stop-exec-queue
265
7e87a0bf
HH
266HARD=""
267while pidof udevd >/dev/null 2>&1; do
268 for pid in $(pidof udevd); do
cc02093d 269 kill $HARD $pid >/dev/null 2>&1
7e87a0bf
HH
270 done
271 HARD="-9"
272done
5fa49940
AT
273
274# Clean up the environment
275for i in $(export -p); do
276 i=${i#declare -x}
cc02093d
HH
277 i=${i#export}
278 i=${i%%=*}
279 [ "$i" = "root" -o "$i" = "PATH" -o "$i" = "HOME" -o "$i" = "TERM" ] || unset $i
5fa49940
AT
280done
281
faf88239 282initargs=""
52c3715f 283
b63fd4d9 284read CLINE </proc/cmdline
247c9f70
HH
285if getarg init= >/dev/null ; then
286 ignoreargs="console BOOT_IMAGE"
287 # only pass arguments after init= to the init
247c9f70 288 CLINE=${CLINE#*init=}
cc02093d
HH
289 set $CLINE
290 shift
291 for x in "$@"; do
292 for s in $ignoreargs; do
293 [ "${x%%=*}" = $s ] && continue 2
294 done
295 initargs="$initargs $x"
247c9f70 296 done
cc02093d 297 unset CLINE
b63fd4d9
AB
298else
299 set $CLINE
300 shift
301 for x in "$@"; do
302 case "$x" in
cc02093d 303 [0-9]|s|S|single|emergency|auto ) \
b63fd4d9 304 initargs="$initargs $x"
cc02093d 305 ;;
b63fd4d9
AB
306 esac
307 done
247c9f70 308fi
e1ee9353 309
a89fb6da 310# Debug: Copy state
e1ee9353 311if getarg rdcopystate; then
a71f1b49 312 mkdir /dev/.initramfs/
e1ee9353 313 cp /tmp/* /dev/.initramfs/ >/dev/null 2>&1
e1ee9353
WT
314fi
315
0b2de1f8
HH
316info "Switching root"
317
ac36d5db 318wait_for_loginit
4592b698 319
db3641a2 320umask $OLD_UMASK
3636d59e
HH
321unset HOME
322unset TERM
db3641a2 323
faf88239 324exec switch_root "$NEWROOT" "$INIT" $initargs || {
247c9f70
HH
325 echo "Something went very badly wrong in the initramfs. Please "
326 echo "file a bug against dracut."
9cead591
VL
327 emergency_shell
328}