logo

make-initrd

Unnamed repository; edit this file 'description' to name the repository. git clone https://anongit.hacktivis.me/git/make-initrd.git/

init (2206B)


  1. #!/bin/sh
  2. # Copyright 2017-2022 Haelwenn (lanodan) Monnier <contact@hacktivis.me>
  3. # SPDX-License-Identifier: ISC
  4. export PATH=/bin:/sbin:/usr/bin:/usr/sbin
  5. export init=/sbin/init newroot=/newroot root=ZFS=zroot/ROOT/gentoo sh=/bin/sh level=3\ -a dev_hotplug=mdev
  6. rescueshell() {
  7. export PS1='rsh:$(tty | cut -c6-):$PWD # '
  8. if command -v setsid >/dev/null; then
  9. # shellcheck disable=SC2094
  10. setsid "$sh" -i -0<"$console" 1>"$console" 2>&1
  11. else
  12. # shellcheck disable=SC2094
  13. "$sh" -i 0<"$console" 1>"$console" 2>&1
  14. fi
  15. }
  16. die() {
  17. echo "Dropping into a rescueshell..."
  18. rescueshell || exec $sh -i
  19. }
  20. getdev() {
  21. case "$1" in
  22. UUID=*|LABEL=*)
  23. findfs "$1"
  24. ;;
  25. *-*-*-*)
  26. findfs "UUID=$1"
  27. ;;
  28. *)
  29. echo "$1"
  30. ;;
  31. esac
  32. }
  33. set -v
  34. /bin/busybox --install /usr/bin
  35. umask 0077
  36. mount -t proc proc /proc
  37. mount -t sysfs sysfs /sys
  38. if grep -q devtmpfs /proc/filesystems; then
  39. mount -t devtmpfs devtmpfs /dev
  40. else
  41. mount -t tmpfs tmpfs /dev
  42. fi
  43. # shellcheck disable=SC2013
  44. for arg in $(cat /proc/cmdline); do
  45. case $arg in
  46. rescue*)
  47. export rescue=1
  48. ;;
  49. single)
  50. export level=2
  51. ;;
  52. level*|init*|root*|crypt_root*|sh*|dev_hotplug*)
  53. # shellcheck disable=SC2163
  54. export "$arg"
  55. ;;
  56. esac
  57. done
  58. root="${root/#ZFS=}"
  59. "$dev_hotplug" -s || die
  60. command -v "$dev_hotplug" > /proc/sys/kernel/hotplug
  61. [ -h /dev/fd ] || ln -fs /proc/self/fd /dev/fd
  62. [ -h /dev/stderr ] || ln -fs /proc/self/stderr /dev/stderr
  63. [ -h /dev/stdin ] || ln -fs /proc/self/stdin /dev/stdin
  64. [ -h /dev/stdout ] || ln -fs /proc/self/stdout /dev/stdout
  65. : "${console:=/dev/console}"
  66. # shellcheck disable=SC2094
  67. exec 0<"$console" 1>"$console" 2>&1
  68. test -n "$crypt_root" && {
  69. rootdev=$(getdev "$crypt_root")
  70. if test -f /keyfile; then
  71. cryptsetup --key-file /keyfile open "$rootdev" luks-"${rootdev##*/}" || \
  72. cryptsetup open "$rootdev" luks-"${rootdev##*/}" || die
  73. else
  74. cryptsetup open "$rootdev" luks-"${rootdev##*/}" || die
  75. fi
  76. }
  77. /load_modules.sh || die
  78. pool="${root%%/*}"
  79. zpool import -d /dev/mapper -d /dev -N "$pool" || die
  80. mount -t zfs -o rw,zfsutil "$root" "$newroot" || die
  81. for d in /proc /sys /dev; do umount -l "$d"; done
  82. exec switch_root "$newroot" "${init:-/sbin/init}" "$level" || die