rd

tiny cpt ramdisk image generator meant for system rescue
git clone git://git.ckyln.com/rd
Log | Files | Refs | README | LICENSE

init (2054B)


      1 #!/bin/sh
      2 # shellcheck disable=1090
      3 
      4 PATH=/usr/bin TERM=linux SHELL=/bin/sh PS1='-> ' HOME=/root LANG=C LC_ALL=C
      5 export PATH TERM SHELL PS1 HOME LANG LC_ALL
      6 
      7 # Init script for the initramfs image
      8 
      9 out() {
     10     [ "$debug" = 1 ] && printf '-> %s\n' "$@"
     11 }
     12 
     13 shell() {
     14     printf '\033[1m%s\033[m\n' "${@:-An unknown error occured}" "Dropping to shell..."
     15     sh
     16 }
     17 
     18 run_hook() {
     19     out "Running '$1' hooks..."
     20     for hook in /etc/rd/hooks/*."$1"; do
     21         [ -f "$hook" ] || continue
     22         out "Running '$hook'..."
     23         . "$hook"
     24     done
     25 }
     26 
     27 mnt() {
     28     mountpoint -q "$1" && return 0
     29     mnt=$1; shift
     30     mount "$@" "$mnt" || shell "Could not mount $mnt"
     31 }
     32 
     33 # Mount pseude-filesystems.
     34 mnt /proc -t proc     -o nosuid,noexec,nodev     proc
     35 mnt /sys  -t sysfs    -o nosuid,noexec,nodev     sys
     36 mnt /run  -t tmpfs    -o nosuid,nodev,mode=0755  run
     37 mnt /dev  -t devtmpfs -o nosuid,mode=0755        dev
     38 
     39 # Link IO devices.
     40 ln -sf /proc/self/fd /dev/fd
     41 ln -sf fd/0          /dev/stdin
     42 ln -sf fd/1          /dev/stdout
     43 ln -sf fd/2          /dev/stderr
     44 
     45 # Parse command line options.
     46 while read -r cmdline; do
     47     for arg in $cmdline; do
     48         # Exporting arg is intentional here.
     49         # shellcheck disable=2163
     50         case "$arg" in
     51             *=*) export "$arg"   ;;
     52               *) export "$arg=1" ;;
     53         esac
     54     done
     55 done < /proc/cmdline
     56 
     57 # Run 'early-rd' hooks after mounting pseudo filesystems
     58 run_hook early-rd
     59 
     60 {
     61     mdev -s
     62     mdev -df & mdev=$!
     63 } || shell "Could not start device daemon"
     64 
     65 # Create /mnt/root directory just in case.
     66 mkdir -p /mnt/root
     67 
     68 # These will be parsed from the commandline and can be safely ignored
     69 # shellcheck disable=2154
     70 mnt /mnt/root -o "ro${rootopts+,}${rootopts}" "$(findfs "$root")" || shell "Could not mount root"
     71 
     72 kill "$mdev"
     73 
     74 run_hook rd
     75 [ "$shell" = 1 ] && shell "User requested"
     76 
     77 for dir in dev run sys proc; do
     78     mount -o move "$dir" "/mnt/root/$dir" ||
     79     mount  --move "$dir" "/mnt/root/$dir" || shell "Could not mount '$dir' to /mnt/root/$dir"
     80 done
     81 
     82 exec switch_root /mnt/root /sbin/init