Guest User

Create Debian live USB

a guest
May 19th, 2019
1,356
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.95 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. if [[ ${EUID} -ne 0 ]]; then
  4.   echo "this script must be executed with elevated privileges"
  5.   exit 1
  6. fi
  7.  
  8. iso=$1
  9. dev=$2
  10.  
  11. if [[ -z "${iso}" || ! -f ${iso} || ! -s ${iso} ]]; then
  12.   echo "parameter 1 must be source ISO image"
  13.   exit 1
  14. fi
  15.  
  16. if [[ -z "${dev}" || ! -b ${dev} ]]; then
  17.   echo "param 2 must be target block device"
  18.   exit 1
  19. fi
  20.  
  21. if [[ "${dev}" =~ ^.*[0-9]$ ]]; then
  22.   echo "target block device should be device, not a partition (must not end with digit)"
  23.   exit 1
  24. fi
  25.  
  26. echo "unmounting old partitions"
  27. umount ${dev}*
  28.  
  29. lsblk ${dev}
  30. echo "this will destroy data on ${dev}. abort now if unsure!"
  31. read
  32.  
  33. echo "creating partitions"
  34. parted ${dev} --script mktable gpt
  35. parted ${dev} --script mkpart EFI fat16 1MiB 10MiB
  36. parted ${dev} --script mkpart live fat16 10MiB 3GiB
  37. parted ${dev} --script mkpart persistence ext4 3GiB 100%
  38. parted ${dev} --script set 1 msftdata on
  39. parted ${dev} --script set 2 legacy_boot on
  40. parted ${dev} --script set 2 msftdata on
  41.  
  42. echo "syncing and probing new paritions"
  43. sync
  44. partprobe
  45.  
  46. echo "creating file systems"
  47. mkfs.vfat -n EFI ${dev}1
  48. mkfs.vfat -n LIVE ${dev}2
  49. mkfs.ext4 -F -L persistence ${dev}3
  50.  
  51. echo "creating temporary mount locations"
  52. tmp=$(mktemp --tmpdir --directory debianlive.XXXXX)
  53. tmpefi=${tmp}/efi
  54. tmplive=${tmp}/live
  55. tmppersistence=${tmp}/persistence
  56. tmpiso=${tmp}/iso
  57. tmpall="${tmpefi} ${tmplive} ${tmppersistence} ${tmpiso}"
  58.  
  59. echo "mounting resources"
  60. mkdir ${tmpall}
  61. mount ${dev}1 ${tmpefi}
  62. mount ${dev}2 ${tmplive}
  63. mount ${dev}3 ${tmppersistence}
  64. mount -oro ${iso} ${tmpiso}
  65.  
  66. echo "copying iso image filesystem contents"
  67. cp -ar ${tmpiso}/* ${tmplive}
  68. sync
  69.  
  70. echo "creating persistence.conf"
  71. echo "/ union" > ${tmppersistence}/persistence.conf
  72.  
  73. echo "installing grub"
  74. grub-install --no-uefi-secure-boot --removable --target=x86_64-efi --boot-directory=${tmplive}/boot/ --efi-directory=${tmpefi} ${dev}
  75.  
  76. echo "installing syslinux"
  77. dd bs=440 count=1 conv=notrunc if=/usr/lib/syslinux/mbr/gptmbr.bin of=${dev}
  78. syslinux --install ${dev}2
  79.  
  80. echo "fixing isolinux folder/files"
  81. mv ${tmplive}/isolinux ${tmplive}/syslinux
  82. mv ${tmplive}/syslinux/isolinux.bin ${tmplive}/syslinux/syslinux.bin
  83. mv ${tmplive}/syslinux/isolinux.cfg ${tmplive}/syslinux/syslinux.cfg
  84.  
  85. echo "fixing grub splash screen"
  86. sed --in-place 's#isolinux/splash#syslinux/splash#' ${tmplive}/boot/grub/grub.cfg
  87.  
  88. echo "configuring persistence kernel parameter"
  89. sed --in-place '0,/boot=live/{s/\(boot=live .*\)$/\1 persistence/}' ${tmplive}/boot/grub/grub.cfg ${tmplive}/syslinux/menu.cfg
  90.  
  91. echo "configuring default keyboard layout (german) and locales (primary en_US, secondary de_DE)"
  92. sed --in-place '0,/boot=live/{s/\(boot=live .*\)$/\1 keyboard-layouts=de locales=en_US.UTF-8,de_DE.UTF-8/}' ${tmplive}/boot/grub/grub.cfg ${tmplive}/syslinux/menu.cfg
  93.  
  94. echo "cleaning up"
  95. umount ${tmpall}
  96. rmdir ${tmpall} ${tmp}
  97.  
  98. echo "sync'ing so that cached writes to USB stick are written"
  99. sync
  100.  
  101. echo "USB stick is ready"
Add Comment
Please, Sign In to add comment