]> git.ipfire.org Git - thirdparty/dracut.git/blame - modules.d/99base/init
ignore testimages
[thirdparty/dracut.git] / modules.d / 99base / init
CommitLineData
7f64a3fe 1#!/bin/sh
a5e56335
JK
2#
3# Licensed under the GPLv2
4#
5# Copyright 2008, Red Hat, Inc.
6# Jeremy Katz <katzj@redhat.com>
ec9315e5
JK
7
8emergency_shell()
9{
ca9f6259 10 echo ; echo
ec9315e5
JK
11 echo "Bug in initramfs /init detected. Dropping to a shell. Good luck!"
12 echo
6b0daf2e 13 sh -i
ec9315e5 14}
ec9315e5 15
04789195 16getarg() {
955f8b09 17 local o line
6b0daf2e 18 for o in $CMDLINE; do
d05f3c43
VL
19 [ "$o" = "$1" ] && return 0
20 [ "${o%%=*}" = "${1%=}" ] && { echo ${o#*=}; return 0; }
04789195 21 done
cd20f1f6
VL
22 return 1
23}
24
25source_all() {
26 local f
7f64a3fe 27 [ "$1" ] && [ -d "/$1" ] || return
3f9c86ab 28 for f in "/$1"/*.sh; do [ -f "$f" ] && . "$f"; done
04789195
VL
29}
30
ec9315e5
JK
31echo "Starting initrd..."
32export PATH=/sbin:/bin:/usr/sbin:/usr/bin
33export TERM=linux
7f64a3fe 34trap "emergency_shell" 0
a890fe29
HH
35
36mknod /dev/null c 1 3
37
ec9315e5 38# mount some important things
a890fe29
HH
39mount -t proc /proc /proc >/dev/null 2>&1
40mount -t sysfs /sys /sys >/dev/null 2>&1
41mount -t tmpfs -omode=0755 udev /dev >/dev/null 2>&1
42
6b0daf2e
VL
43read CMDLINE </proc/cmdline;
44
972673f6 45# Make some basic devices first, let udev handle the rest
0c70846e 46mknod /dev/ptmx c 5 2
972673f6 47mknod /dev/console c 5 1
0c70846e 48mkdir /dev/pts
a890fe29 49mount -t devpts -o gid=5,mode=620 /dev/pts /dev/pts >/dev/null 2>&1
0c70846e 50
a08c1f45 51# pre-udev scripts run before udev starts, and are run only once.
8177ca3e 52getarg 'break=pre-udev' && emergency_shell
ec1ad334
VL
53source_all pre-udev
54
ec9315e5 55# start up udev and trigger cold plugs
7bff75da 56udevd --daemon
bc6b0dec 57udevadm trigger >/dev/null 2>&1
8177ca3e 58udevadm settle --timeout=30 >/dev/null 2>&1
ec9315e5 59
9cead591 60NEWROOT="/sysroot"
a08c1f45
VL
61# pre-mount happens before we try to mount the root filesystem,
62# and happens once.
8177ca3e 63getarg 'break=pre-mount' && emergency_shell
ec1ad334 64source_all pre-mount
8177ca3e 65getarg 'break=mount' && emergency_shell
a08c1f45
VL
66# mount scripts actually try to mount the root filesystem, and may
67# be sourced any number of times. As soon as one suceeds, no more are sourced.
87930555 68i=0
a08c1f45
VL
69while :; do
70 for f in /mount/*.sh; do
71 [ -x "$f" ] && . "$f";
72 [ "$ROOTFS_MOUNTED" ] && break;
73 done
74 [ "$ROOTFS_MOUNTED" ] && break;
75 sleep 1
7cd15fe7 76 i=$(($i+1))
87930555 77 [ $i -gt 10 ] && emergency_shell
a08c1f45
VL
78done
79
80# by the time we get here, the root filesystem should be mounted.
d05f3c43
VL
81# Try to find init.
82for i in "$(getarg init=)" /sbin/init /etc/init /init /bin/sh; do
83 [ -f "$NEWROOT$i" -a -x "$NEWROOT$i" ] && { INIT="$i"; break; }
84done
6b0daf2e 85[ "$INIT" ] || {
d05f3c43
VL
86 echo "Cannot find init! Please check to make sure you passed"
87 echo "a valid root filesystem! Dropping to a shell."
88 emergency_shell
6b0daf2e 89}
a08c1f45
VL
90
91# pre pivot scripts are sourced just before we switch over to the new root.
8177ca3e 92getarg 'break=pre-pivot' && emergency_shell
ec1ad334 93source_all pre-pivot
8177ca3e 94getarg break && emergency_shell
87930555 95kill $(pidof udevd)
9cead591 96echo "Switching to real root filesystem $root"
faf88239
VL
97initargs=""
98for x in $CMDLINE; do
99 [ "${x%%=*}" = "console" -o "${x%%=*}" = "BOOT_IMAGE" ] && continue
100 initargs="$initargs $x"
101done
102exec switch_root "$NEWROOT" "$INIT" $initargs || {
9cead591
VL
103 # davej doesn't like initrd bugs
104 echo "Something went very badly wrong in the initrd. Please "
105 echo "file a bug against mkinitrd."
106 emergency_shell
107}