Advertisement
Guest User

init script for teres with encrypted root

a guest
May 16th, 2018
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.36 KB | None | 0 0
  1. #!/bin/busybox sh
  2.  
  3. # Install busybox
  4. /bin/busybox --install -s
  5.  
  6. # Mount the /proc and /sys filesystems.
  7. mount -t proc none /proc
  8. mount -t sysfs none /sys
  9. mount -t devtmpfs none /dev
  10.  
  11. # turn off noise so we can see password prompt
  12. echo "1 1 1 1" > /proc/sys/kernel/printk
  13. # load display driver
  14. modprobe disp
  15.  
  16. cmdline() {
  17.         local value
  18.         value=" $(cat /proc/cmdline) "
  19.         value="${value##* $1=}"
  20.         value="${value%% *}"
  21.         [ "$value" != "" ] && echo "$value"
  22. }
  23.  
  24. m_echo() {
  25.     echo $1
  26.     echo $1 > /dev/tty1
  27. }
  28.  
  29. run_on_tty1() {
  30.         setsid sh -c "exec $1 <>/dev/tty1 >&0 2>&1"
  31. }
  32.  
  33. realboot() {
  34.         m_echo "Unlocking rootfs: $1"
  35.        
  36.         run_on_tty1 "cryptsetup open --allow-discards $1 cryptroot"
  37.  
  38.         if [ ! -e /dev/mapper/cryptroot ]; then
  39.                 m_echo "Something went wrong: /dev/mapper/cryptroot does not exist"
  40.         else
  41.                 m_echo "Encrypted root unlocked, mounting and booting..."
  42.  
  43.                 mkdir -p /mnt/root
  44.                 mount -o rw /dev/mapper/cryptroot /mnt/root
  45.  
  46.                 if [ -x /mnt/root/sbin/init -o -h /mnt/root/sbin/init ]; then
  47.                         # Cleanup.
  48.                         umount /proc
  49.                         umount /sys
  50.                         umount /dev
  51.  
  52.                         # Boot the real system.
  53.                         exec switch_root /mnt/root /sbin/init
  54.                 else
  55.                         umount /mnt/root
  56.                 fi
  57.         fi
  58. }
  59.  
  60. runshell() {
  61.         m_echo "Dropping to a shell."
  62.         m_echo
  63.         setsid cttyhack /bin/sh
  64. }
  65.  
  66. find_parition_by_value() {
  67.         echo $(blkid | tr -d '"' | grep "$1" | cut -d ':' -f 1 | head -n 1)
  68. }
  69.  
  70. boot() {
  71.         m_echo "Kernel params: `cat /proc/cmdline`"
  72.         local i=5
  73.         local kernel_root_param=$(cmdline root)
  74.  
  75.         while [ "$i" -ge 1 ]; do
  76.                 m_echo "Waiting for root system $kernel_root_param, countdown : $i"
  77.                
  78.                 local root=$(find_parition_by_value $kernel_root_param)
  79.                 if [ -e "$root" ]; then
  80.                         realboot $root;
  81.                 fi;
  82.  
  83.                 i=$(( $i - 1 ));
  84.                 sleep 5;
  85.         done;
  86.  
  87.         # Default rootfs - sd partition 2
  88.         realboot /dev/mmcblk0p2;
  89.         run_on_tty1 "sh"
  90.         runshell
  91. }
  92. boot;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement