summaryrefslogtreecommitdiff
path: root/src/initramfs-init.sh
diff options
context:
space:
mode:
authordavidovski <david@davidovski.xyz>2022-03-12 22:33:23 +0000
committerdavidovski <david@davidovski.xyz>2022-03-12 22:33:23 +0000
commitad7e5029cf1a5577a620c250782cac8bbea4af5e (patch)
tree101921a0e538c95c361aa2aa741a0c53aba46d37 /src/initramfs-init.sh
parent9a82bbdf22a8fbab40fcc6d4516b81daac628322 (diff)
added mkinitramfsv1.2
Diffstat (limited to 'src/initramfs-init.sh')
-rw-r--r--src/initramfs-init.sh108
1 files changed, 108 insertions, 0 deletions
diff --git a/src/initramfs-init.sh b/src/initramfs-init.sh
new file mode 100644
index 0000000..2466e0d
--- /dev/null
+++ b/src/initramfs-init.sh
@@ -0,0 +1,108 @@
+#!/bin/sh
+
+PATH=/bin:/usr/bin:/sbin:/usr/sbin
+export PATH
+
+problem()
+{
+ printf "Encountered a problem!\n\nDropping you to a shell.\n\n"
+ sh
+}
+
+no_device()
+{
+ printf "The device %s, which is supposed to contain the\n" $1
+ printf "root file system, does not exist.\n"
+ printf "Please fix this problem and exit this shell.\n\n"
+}
+
+no_mount()
+{
+ printf "Could not mount device %s\n" $1
+ printf "Sleeping forever. Please reboot and fix the kernel command line.\n\n"
+ printf "Maybe the device is formatted with an unsupported file system?\n\n"
+ printf "Or maybe filesystem type autodetection went wrong, in which case\n"
+ printf "you should add the rootfstype=... parameter to the kernel command line.\n\n"
+ printf "Available partitions:\n"
+}
+
+do_mount_root()
+{
+ mkdir /.root
+ [ -n "$rootflags" ] && rootflags="$rootflags,"
+ rootflags="$rootflags$ro"
+
+ case "$root" in
+ /dev/* ) device=$root ;;
+ UUID=* ) eval $root; device="/dev/disk/by-uuid/$UUID" ;;
+ LABEL=*) eval $root; device="/dev/disk/by-label/$LABEL" ;;
+ "" ) echo "No root device specified." ; problem ;;
+ esac
+
+ while [ ! -b "$device" ] ; do
+ no_device $device
+ problem
+ done
+
+ if ! mount -n -t "$rootfstype" -o "$rootflags" "$device" /.root ; then
+ no_mount $device
+ cat /proc/partitions
+ while true ; do sleep 10000 ; done
+ else
+ echo "Successfully mounted device $root"
+ fi
+}
+
+init=/sbin/init
+root=
+rootdelay=
+rootfstype=auto
+ro="ro"
+rootflags=
+device=
+
+mount -n -t devtmpfs devtmpfs /dev
+mount -n -t proc proc /proc
+mount -n -t sysfs sysfs /sys
+mount -n -t tmpfs tmpfs /run
+
+read -r cmdline < /proc/cmdline
+
+for param in $cmdline ; do
+ case $param in
+ init=* ) init=${param#init=} ;;
+ root=* ) root=${param#root=} ;;
+ rootdelay=* ) rootdelay=${param#rootdelay=} ;;
+ rootfstype=*) rootfstype=${param#rootfstype=} ;;
+ rootflags=* ) rootflags=${param#rootflags=} ;;
+ ro ) ro="ro" ;;
+ rw ) ro="rw" ;;
+ esac
+done
+
+# udevd location depends on version
+if [ -x /sbin/udevd ]; then
+ UDEVD=/sbin/udevd
+elif [ -x /lib/udev/udevd ]; then
+ UDEVD=/lib/udev/udevd
+elif [ -x /lib/systemd/systemd-udevd ]; then
+ UDEVD=/lib/systemd/systemd-udevd
+else
+ echo "Cannot find udevd nor systemd-udevd"
+ problem
+fi
+
+${UDEVD} --daemon --resolve-names=never
+udevadm trigger
+udevadm settle
+
+if [ -f /etc/mdadm.conf ] ; then mdadm -As ; fi
+if [ -x /sbin/vgchange ] ; then /sbin/vgchange -a y > /dev/null ; fi
+if [ -n "$rootdelay" ] ; then sleep "$rootdelay" ; fi
+
+do_mount_root
+
+killall -w ${UDEVD##*/}
+
+exec switch_root /.root "$init" "$@"
+