alexpanoiu

Fix BusyBox in the initramfs environment on Ubuntu 17.10

Oct 13th, 2017 (edited)
601
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.35 KB | None | 0 0
  1. #! /bin/sh
  2.  
  3. # ------------------------------------------------------------------------------
  4. #
  5. #                               fix-busybox-hook
  6. #   update-initramfs hook to provided functional BusyBox tools in initramfs
  7. #                          [email protected]
  8. #                                  2017-10-13
  9. #
  10. #             This file must be made executable and symlinked from
  11. #                          /etc/initramfs-tools/hooks
  12. #
  13. # update-initramfs tools to replace the crippled BusyBox provided by default in
  14. # the initramfs environment by Ubuntu 17.10 with a variant compatible with the
  15. # dropbear and cryptsetup boot scripts.
  16. #
  17. # Adapted from the hook script written by Hamy; see "Remote unlocking of
  18. # LUKS-encrypted root in Ubuntu/Debian", https://hamy.io/post/0005/.
  19. #
  20. # ------------------------------------------------------------------------------
  21.  
  22. if test "/$1/" = '/prereqs/'; then
  23.   echo ''
  24.   exit 0
  25. fi
  26.  
  27. [ "$BUSYBOX" = 'n' ] && exit 0
  28.  
  29. [ -r /usr/share/initramfs-tools/hook-functions ] || exit 0
  30. . /usr/share/initramfs-tools/hook-functions
  31.  
  32. # Testing the presence of busybox-initramfs hook
  33. [ -x /usr/share/initramfs-tools/hooks/zz-busybox-initramfs ] || exit 0
  34.  
  35. # The original busybox binary added by busybox-initramfs
  36. BUSYBOX_ORIGINAL=$DESTDIR/bin/busybox
  37. [ -x $BUSYBOX_ORIGINAL ] || exit 0
  38.  
  39. # The one we want to replace it with
  40. [ -x /bin/busybox ] || exit 0
  41. BUSYBOX_NEW=/bin/busybox
  42.  
  43. # Ensure the original busybox lacks extended options
  44. # and the soon-to-be-replaced-by one does not
  45. if $BUSYBOX_ORIGINAL ps -eo pid,args >/dev/null 2>&1; then
  46.   exit 0
  47. elif ! $BUSYBOX_NEW ps -eo pid,args >/dev/null 2>&1; then
  48.   exit 0
  49. fi
  50.  
  51. # Get the inode number of busybox-initramfs binary
  52. BUSYBOX_ORIGINAL_INODE=$(stat --format=%i $BUSYBOX_ORIGINAL)
  53.  
  54. # Replace the binary
  55. rm -f $BUSYBOX_ORIGINAL
  56. copy_exec $BUSYBOX_NEW /bin/busybox
  57.  
  58. echo "Fixing busybox-initramfs for:"
  59.  
  60. for alias in $(
  61.   $BUSYBOX_NEW --list-long | sed -e 's@/\?\(usr/\)\?\(bin/\)\?@bin/@'
  62. ); do
  63.  
  64.   # Remove (and then re-add) all the hardlinks added by busybox-initramfs
  65.   if [ -e "$DESTDIR/$alias" ] &&
  66.      [ $(stat --format=%i "$DESTDIR/$alias") -eq $BUSYBOX_ORIGINAL_INODE ]
  67.   then
  68.     echo -n " ${alias#bin/}"
  69.     rm -f "$DESTDIR/$alias"
  70.     ln "$DESTDIR/bin/busybox" "$DESTDIR/$alias"
  71.   fi
  72.  
  73. done | fmt -s
  74.  
  75. # vim: set ai bs=2 com=\:# et ft=sh sw=2 tw=80:
Advertisement
Add Comment
Please, Sign In to add comment