initramfs-init 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. # mount the bare necesities
  39. mount -n -t proc proc /proc
  40. mount -n -t sysfs sysfs /sys
  41. mount -n -t devtmpfs devtmpfs /dev
  42. # get the root device, so we can find the boot partiton
  43. UNPARSED=$(cmdline root)
  44. ROOT_PARTUUID=$(rootpartuuid $UNPARSED)
  45. echo ${ROOT_PARTUUID} > /dev/tty1
  46. BLKID=$(/bin/blkid | grep $ROOT_PARTUUID )
  47. echo ${BLKID} > /dev/tty1
  48. #If its an mmcblk device, the kernel partiton will p1. If it is a usb device, the partiton will just be 1
  49. #Just want everything before the 1
  50. ROOT_DEV="${BLKID%1:*}"
  51. echo ${ROOT_DEV} > /dev/tty1
  52. # we can use this to change what cmdline options get passed into
  53. # the next boot stage
  54. CMDLINE='cat /proc/cmdline'
  55. # label any partition on the system with RESCUESHELL to enter the initramfs rescue shell before mount and root_switch.
  56. # you can do this with "cgpt add -i 1 -l RESCUESHELL /dev/sda" for example to label the first partiton of a usb drive.
  57. if [ -n "$(blkid | grep RESCUESHELL)" ]
  58. then
  59. rescue_shell debug
  60. fi
  61. if [ -n "$(blkid ${ROOT_DEV}2 | grep crypto_LUKS)" ]
  62. then
  63. #decrypt and mount the root filesystem
  64. echo "Opening encrypted root partition, this will take 30s..."
  65. cryptsetup --tries 5 luksOpen ${ROOT_DEV}2 luksroot || rescue_shell
  66. mount /dev/mapper/luksroot /newroot
  67. else
  68. # mount the unencrypted root filesystem
  69. [ -d "/newroot" ] || mkdir -p /newroot
  70. mount ${ROOT_DEV}2 /newroot
  71. fi
  72. umount /sys
  73. umount /proc
  74. #switch to the new rootfs
  75. exec switch_root /newroot /sbin/init ${CMDLINE}