2 # -*- mode: shell-script; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
3 # ex: ts=8 sw=4 sts=4 et filetype=sh
5 # Licensed under the GPLv2
7 # Copyright 2008-2010, Red Hat, Inc.
8 # Harald Hoyer <harald@redhat.com>
9 # Jeremy Katz <katzj@redhat.com>
11 export -p > /tmp/export.orig
14 [ -d $NEWROOT ] || mkdir -p -m 0755 $NEWROOT
17 PATH=/usr/sbin:/usr/bin:/sbin:/bin
20 # mount some important things
21 [ ! -d /proc/self ] && \
22 mount -t proc -o nosuid,noexec,nodev proc /proc >/dev/null
24 if [ "$?" != "0" ]; then
25 echo "Cannot mount proc on /proc! Compile the kernel with CONFIG_PROC_FS!"
29 [ ! -d /sys/kernel ] && \
30 mount -t sysfs -o nosuid,noexec,nodev sysfs /sys >/dev/null
32 if [ "$?" != "0" ]; then
33 echo "Cannot mount sysfs on /sys! Compile the kernel with CONFIG_SYSFS!"
42 if ! ismounted /dev; then
43 mount -t devtmpfs -o mode=0755,nosuid,strictatime devtmpfs /dev >/dev/null
46 if ! ismounted /dev; then
47 echo "Cannot mount devtmpfs on /dev! Compile the kernel with CONFIG_DEVTMPFS!"
52 if [ -f /etc/adjtime ]; then
53 if strstr "$(cat /etc/adjtime)" LOCAL; then
54 hwclock --hctosys --localtime
56 hwclock --hctosys --utc
60 # prepare the /dev directory
61 [ ! -h /dev/fd ] && ln -s /proc/self/fd /dev/fd >/dev/null 2>&1
62 [ ! -h /dev/stdin ] && ln -s /proc/self/fd/0 /dev/stdin >/dev/null 2>&1
63 [ ! -h /dev/stdout ] && ln -s /proc/self/fd/1 /dev/stdout >/dev/null 2>&1
64 [ ! -h /dev/stderr ] && ln -s /proc/self/fd/2 /dev/stderr >/dev/null 2>&1
66 if ! ismounted /dev/pts; then
67 mkdir -m 0755 /dev/pts
68 mount -t devpts -o gid=5,mode=620,noexec,nosuid devpts /dev/pts >/dev/null
71 if ! ismounted /dev/shm; then
72 mkdir -m 0755 /dev/shm
73 mount -t tmpfs -o mode=1777,nosuid,nodev,strictatime tmpfs /dev/shm >/dev/null
76 if ! ismounted /run; then
78 mount -t tmpfs -o mode=0755,nosuid,nodev,strictatime tmpfs /newrun >/dev/null
79 cp -a /run/* /newrun >/dev/null 2>&1
80 mount --move /newrun /run
84 if command -v kmod >/dev/null 2>/dev/null; then
85 kmod static-nodes --format=tmpfiles 2>/dev/null | \
86 while read type file mode a a a majmin; do
89 mkdir -m $mode -p $file
92 mknod -m $mode $file $type ${majmin%:*} ${majmin#*:}
98 trap "action_on_fail Signal caught!" 0
100 [ -d /run/initramfs ] || mkdir -p -m 0755 /run/initramfs
101 [ -d /run/log ] || mkdir -p -m 0755 /run/log
103 export UDEVVERSION=$(udevadm --version)
104 if [ $UDEVVERSION -gt 166 ]; then
105 # newer versions of udev use /run/udev/rules.d
106 export UDEVRULESD=/run/udev/rules.d
107 [ -d /run/udev ] || mkdir -p -m 0755 /run/udev
108 [ -d $UDEVRULESD ] || mkdir -p -m 0755 $UDEVRULESD
110 mkdir -m 0755 /dev/.udev /dev/.udev/rules.d
111 export UDEVRULESD=/dev/.udev/rules.d
114 if [ "$RD_DEBUG" = "yes" ]; then
115 mkfifo /run/initramfs/loginit.pipe
116 loginit $DRACUT_QUIET </run/initramfs/loginit.pipe >/dev/console 2>&1 &
117 exec >/run/initramfs/loginit.pipe 2>&1
119 exec 0<>/dev/console 1<>/dev/console 2<>/dev/console
122 [ -f /etc/initrd-release ] && . /etc/initrd-release
123 [ -n "$VERSION_ID" ] && info "$NAME-$VERSION_ID"
125 source_conf /etc/conf.d
127 # run scriptlets to parse the command line
128 make_trace_mem "hook cmdline" '1+:mem' '1+:iomem' '3+:slab'
129 getarg 'rd.break=cmdline' -d 'rdbreak=cmdline' && emergency_shell -n cmdline "Break before cmdline"
132 [ -z "$root" ] && die "No or empty root= argument"
133 [ -z "$rootok" ] && die "Don't know how to handle 'root=$root'"
135 export root rflags fstype netroot NEWROOT
137 # pre-udev scripts run before udev starts, and are run only once.
138 make_trace_mem "hook pre-udev" '1:shortmem' '2+:mem' '3+:slab'
139 getarg 'rd.break=pre-udev' -d 'rdbreak=pre-udev' && emergency_shell -n pre-udev "Break before pre-udev"
142 # start up udev and trigger cold plugs
143 $systemdutildir/systemd-udevd --daemon --resolve-names=never
145 UDEV_LOG_PRIO_ARG=--log-priority
146 UDEV_QUEUE_EMPTY="udevadm settle --timeout=0"
148 if [ $UDEVVERSION -lt 140 ]; then
149 UDEV_LOG_PRIO_ARG=--log_priority
150 UDEV_QUEUE_EMPTY="udevadm settle --timeout=1"
153 getargbool 0 rd.udev.info -d -y rdudevinfo && udevadm control "$UDEV_LOG_PRIO_ARG=info"
154 getargbool 0 rd.udev.debug -d -y rdudevdebug && udevadm control "$UDEV_LOG_PRIO_ARG=debug"
155 udevproperty "hookdir=$hookdir"
157 make_trace_mem "hook pre-trigger" '1:shortmem' '2+:mem' '3+:slab'
158 getarg 'rd.break=pre-trigger' -d 'rdbreak=pre-trigger' && emergency_shell -n pre-trigger "Break before pre-trigger"
159 source_hook pre-trigger
161 udevadm control --reload >/dev/null 2>&1 || :
163 udevadm trigger --type=subsystems --action=add >/dev/null 2>&1
164 udevadm trigger --type=devices --action=add >/dev/null 2>&1
166 make_trace_mem "hook initqueue" '1:shortmem' '2+:mem' '3+:slab'
167 getarg 'rd.break=initqueue' -d 'rdbreak=initqueue' && emergency_shell -n initqueue "Break before initqueue"
169 RDRETRY=$(getarg rd.retry -d 'rd_retry=')
170 RDRETRY=${RDRETRY:-30}
171 RDRETRY=$(($RDRETRY*2))
177 check_finished && break
181 check_finished && break
183 if [ -f $hookdir/initqueue/work ]; then
184 rm -f -- $hookdir/initqueue/work
187 for job in $hookdir/initqueue/*.sh; do
188 [ -e "$job" ] || break
190 check_finished && break 2
193 $UDEV_QUEUE_EMPTY >/dev/null 2>&1 || continue
195 for job in $hookdir/initqueue/settled/*.sh; do
196 [ -e "$job" ] || break
198 check_finished && break 2
201 $UDEV_QUEUE_EMPTY >/dev/null 2>&1 || continue
203 # no more udev jobs and queues empty.
207 if [ $main_loop -gt $((2*$RDRETRY/3)) ]; then
208 for job in $hookdir/initqueue/timeout/*.sh; do
209 [ -e "$job" ] || break
211 udevadm settle --timeout=0 >/dev/null 2>&1 || main_loop=0
212 [ -f $hookdir/initqueue/work ] && main_loop=0
216 main_loop=$(($main_loop+1))
217 [ $main_loop -gt $RDRETRY ] \
218 && { flock -s 9 ; action_on_fail "Could not boot." && break; } 9>/.console_lock
225 # pre-mount happens before we try to mount the root filesystem,
227 make_trace_mem "hook pre-mount" '1:shortmem' '2+:mem' '3+:slab'
228 getarg 'rd.break=pre-mount' -d 'rdbreak=pre-mount' && emergency_shell -n pre-mount "Break pre-mount"
229 source_hook pre-mount
232 getarg 'rd.break=mount' -d 'rdbreak=mount' && emergency_shell -n mount "Break mount"
233 # mount scripts actually try to mount the root filesystem, and may
234 # be sourced any number of times. As soon as one suceeds, no more are sourced.
237 if ismounted "$NEWROOT"; then
238 usable_root "$NEWROOT" && break;
241 for f in $hookdir/mount/*.sh; do
242 [ -f "$f" ] && . "$f"
243 if ismounted "$NEWROOT"; then
244 usable_root "$NEWROOT" && break;
245 warn "$NEWROOT has no proper rootfs layout, ignoring and removing offending mount hook"
253 && { flock -s 9 ; action_on_fail "Can't mount root filesystem" && break; } 9>/.console_lock
257 echo -n "Mounted root filesystem "
258 while read dev mp rest; do [ "$mp" = "$NEWROOT" ] && echo $dev; done < /proc/mounts
261 # pre pivot scripts are sourced just before we doing cleanup and switch over
263 make_trace_mem "hook pre-pivot" '1:shortmem' '2+:mem' '3+:slab'
264 getarg 'rd.break=pre-pivot' -d 'rdbreak=pre-pivot' && emergency_shell -n pre-pivot "Break pre-pivot"
265 source_hook pre-pivot
267 make_trace_mem "hook cleanup" '1:shortmem' '2+:mem' '3+:slab'
268 # pre pivot cleanup scripts are sourced just before we switch over to the new root.
269 getarg 'rd.break=cleanup' -d 'rdbreak=cleanup' && emergency_shell -n cleanup "Break cleanup"
272 # By the time we get here, the root filesystem should be mounted.
274 for i in "$(getarg real_init=)" "$(getarg init=)" $(getargs rd.distroinit=) /sbin/init; do
275 [ -n "$i" ] || continue
277 __p=$(readlink -f "${NEWROOT}/${i}")
278 if [ -x "$__p" -o -x "${NEWROOT}/${__p}" ]; then
285 echo "Cannot find init!"
286 echo "Please check to make sure you passed a valid root filesystem!"
290 if [ $UDEVVERSION -lt 168 ]; then
291 # stop udev queue before killing it
292 udevadm control --stop-exec-queue
295 while pidof udevd >/dev/null 2>&1; do
296 for pid in $(pidof udevd); do
297 kill $HARD $pid >/dev/null 2>&1
302 udevadm control --exit
303 udevadm info --cleanup-db
306 debug_off # Turn off debugging for this section
309 export_n root rflags fstype netroot NEWROOT
312 # Clean up the environment
313 for i in $(export -p); do
316 strstr "$i" "=" || continue
318 [ -z "$i" ] && continue
320 root|PATH|HOME|TERM|PS4|RD_*)
326 . /tmp/export.orig 2>/dev/null || :
327 rm -f -- /tmp/export.orig
330 read CLINE </proc/cmdline
331 if getarg init= >/dev/null ; then
332 ignoreargs="console BOOT_IMAGE"
333 # only pass arguments after init= to the init
334 CLINE=${CLINE#*init=}
336 shift # clear out the rest of the "init=" arg
338 for s in $ignoreargs; do
339 [ "${x%%=*}" = $s ] && continue 2
341 initargs="$initargs $x"
345 debug_off # Turn off debugging for this section
349 [0-9]|s|S|single|emergency|auto ) \
350 initargs="$initargs $x"
357 if ! [ -d "$NEWROOT"/run ]; then
358 NEWRUN=/dev/.initramfs
359 mkdir -m 0755 "$NEWRUN"
360 mount --rbind /run/initramfs "$NEWRUN"
365 # remove helper symlink
366 [ -h /dev/root ] && rm -f -- /dev/root
368 getarg rd.break -d rdbreak && emergency_shell -n switch_root "Break before switch_root"
369 info "Switching root"
374 CAPSH=$(command -v capsh)
375 SWITCH_ROOT=$(command -v switch_root)
379 if [ -f /etc/capsdrop ]; then
381 info "Calling $INIT with capabilities $CAPS_INIT_DROP dropped."
383 exec $CAPSH --drop="$CAPS_INIT_DROP" -- \
384 -c "exec switch_root \"$NEWROOT\" \"$INIT\" $initargs" || \
387 warn capsh --drop=$CAPS_INIT_DROP -- -c exec switch_root "$NEWROOT" "$INIT" $initargs
393 exec $SWITCH_ROOT "$NEWROOT" "$INIT" $initargs || {
394 warn "Something went very badly wrong in the initramfs. Please "
395 warn "file a bug against dracut."