initramfs-init 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #!/bin/busybox sh
  2. # This is the init script built into the PrawnOS initramfs
  3. # This file is part of PrawnOS (http://www.prawnos.com)
  4. # Copyright (c) 2018 Hal Emmerich <hal@halemmerich.com>
  5. # PrawnOS is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License version 2
  7. # as published by the Free Software Foundation.
  8. # PrawnOS is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. # You should have received a copy of the GNU General Public License
  13. # along with PrawnOS. If not, see <https://www.gnu.org/licenses/>.
  14. echo In PrawnOS Init
  15. #add this to start shell at desired point
  16. rescue_shell() {
  17. [ "{$1}" != "debug" ] && echo "Something went wrong. Dropping to a shell." > /dev/tty1
  18. [ "{$1}" == "debug" ] && echo "Debug flag detected, entering debug shell" > /dev/tty1
  19. echo "Something went wrong. Dropping to a shell." > /dev/tty1
  20. exec setsid /bin/sh -c 'exec /bin/sh </dev/tty1 >/dev/tty1 2>&1'
  21. }
  22. #used to parse the kernel cmdline
  23. cmdline() {
  24. local value
  25. value=" $(cat /proc/cmdline) "
  26. value="${value##* ${1}=}"
  27. value="${value%% *}"
  28. [ "${value}" != "" ] && echo "${value}"
  29. }
  30. #used to get the uuid of the root partiton since findfs isn't in debian busybox-static
  31. rootpartuuid() {
  32. local value
  33. value=$1
  34. value="${value%/*}"
  35. value="${value#*=}"
  36. [ "${value}" != "" ] && echo "${value}"
  37. }
  38. # a clever portable shell script to detect occurances of a substring in a string
  39. # occur <string> <substring> (optional count)
  40. # if optional count is not provided:
  41. # returns 1 if substring is not in string
  42. # returns 0 otherwise
  43. # if optional count is provided:
  44. # returns 1 if substring occurs in string < optional count
  45. # returns 0 otherwise
  46. occur() while case "$1" in (*"$2"*) set -- \
  47. "${1#*"$2"}" "$2" "${3:-0}" "$((${4:-0}+1))";;
  48. (*) return "$((${4:-0}<${3:-1}))";;esac
  49. do : "${_occur:+$((_occur=$4))}";done
  50. # mount the bare necesities
  51. mount -n -t proc proc /proc
  52. mount -n -t sysfs sysfs /sys
  53. mount -n -t devtmpfs devtmpfs /dev
  54. # get the root device, so we can find the boot partiton
  55. UNPARSED=$(cmdline root)
  56. ROOT_PARTUUID=$(rootpartuuid $UNPARSED)
  57. echo ${ROOT_PARTUUID} > /dev/tty1
  58. BLKID=$(/bin/blkid | grep $ROOT_PARTUUID )
  59. echo ${BLKID} > /dev/tty1
  60. #If its an mmcblk device, the kernel partiton will p1. If it is a usb device, the partiton will just be 1
  61. #Just want everything before the 1
  62. ROOT_DEV="${BLKID%1:*}"
  63. echo ${ROOT_DEV} > /dev/tty1
  64. # label any partition on the system with RESCUESHELL to enter the initramfs rescue shell before mount and root_switch.
  65. # you can do this with "cgpt add -i 1 -l RESCUESHELL /dev/sda" for example to label the first partiton of a usb drive.
  66. if [ -n "$(blkid | grep RESCUESHELL)" ]
  67. then
  68. rescue_shell debug
  69. fi
  70. if [ -n "$(blkid ${ROOT_DEV}2 | grep crypto_LUKS)" ]
  71. then
  72. #decrypt and mount the root filesystem, disable kernel log messages to avoid clashing with the prompt
  73. dmesg -n 2
  74. echo "Opening encrypted root partition, this will take 30s..."
  75. cryptsetup --tries 5 luksOpen ${ROOT_DEV}2 luksroot || rescue_shell
  76. dmesg -n 7
  77. mount /dev/mapper/luksroot /newroot
  78. else
  79. # mount the unencrypted root filesystem
  80. [ -d "/newroot" ] || mkdir -p /newroot
  81. mount ${ROOT_DEV}2 /newroot
  82. fi
  83. umount /sys
  84. umount /proc
  85. #switch to the new rootfs
  86. exec switch_root /newroot /sbin/init