agilesetllc

CustomCD.sh

Jul 6th, 2017
1,999
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2.  
  3. # 2017-July-04 - having difficulties building a custom Live CD that boots from a USB stick
  4. #
  5. # Tried https://nathanpfry.com/how-to-customize-an-ubuntu-installation-disc/, but no joy.
  6. #
  7. # So, reverting to 'base' documentation, I attempted a
  8. # Simple extraction/decompression/re-manifest/compress/make iso image
  9. # based on https://help.ubuntu.com/community/LiveCDCustomization
  10. #
  11. # Could NOT get this to work using genisoimage, even with adaptations from
  12. # https://askubuntu.com/questions/457528/how-do-i-create-an-efi-bootable-iso-of-a-customized-version-of-ubuntu which
  13. # pointed me toward "-eltorito-alt-boot and -e boot/grub/efi.img -no-emul-boot"
  14. #
  15. # 2017-July-05 - success, but only with xorriso
  16. #
  17. # Eventually, Google pointed me to yet another tool to install, xorriso, thanks to
  18. # https://linuxconfig.org/legacy-bios-uefi-and-secureboot-ready-ubuntu-live-image-customization
  19. #
  20. # It works. If anyone cares to propose updates to the script that do not require the 'dd' of isohdpfx.bin
  21. # and still result in a USB-stick-bootable .iso using genisoimage, I would be pleased to drop xorriso.
  22.  
  23. # Assumptions:
  24. # 0. I need this to work for ubuntu 16.04. I don't care about earlier releases, or necessarily later ones (TODO)
  25. # 1. Host is a fully-patched Ubuntu 16.04.2 server. (I tried a VM, but no loop device)
  26. # 2. I have downloaded "ubuntu-mate-16.04.2-desktop-amd64.iso" using wget
  27. #    This script sits at ~/.
  28. #    e.g. wget http://cdimage.ubuntu.com/ubuntu-mate/releases/16.04.2/release/ubuntu-mate-16.04.2-desktop-amd64.iso
  29. #         Always good to check http://releases.ubuntu.com/
  30.  
  31. CDIMAGENAME='ubuntu-mate-16.04.2-desktop-amd64.iso'
  32. IMAGE_NAME='Custom1604'
  33.  
  34. echo "Copying $CDIMAGENAME to working directory..."
  35.  
  36. cd ~/.
  37. mkdir custom-img
  38. cp $CDIMAGENAME custom-img
  39. cd custom-img
  40.  
  41. # Extract the CD .iso contents
  42.  
  43. #Mount the .iso to a local mount point. 'loop' is a read-only device, so mount will
  44. # warn that it is mounting it read-only. You can use "-o loop,ro" to avoid that warning, if you like.
  45. mkdir mnt
  46. echo "Mounting the .iso as 'mnt' in the local directory. Password-up, please."
  47. sudo mount -o loop $CDIMAGENAME mnt
  48.  
  49. #Extract the .iso contents into dir 'extract-cd'
  50. mkdir extract-cd
  51. sudo rsync --exclude=/casper/filesystem.squashfs -a mnt/ extract-cd
  52.  
  53. #Extract the isohybrid MBR 'isohdpfx.bin' from the source ISO image using dd
  54. sudo dd if=$CDIMAGENAME bs=512 count=1 of=extract-cd/isolinux/isohdpfx.bin
  55.  
  56. # Extract the Desktop system
  57. #Extract the SquashFS filesystem
  58. sudo unsquashfs mnt/casper/filesystem.squashfs
  59. sudo mv squashfs-root edit
  60.  
  61. #We are finished with the source .iso image. Unmount it.
  62. sudo umount mnt
  63.  
  64. #Delete the source .iso copy.
  65. rm $CDIMAGENAME
  66.  
  67. # Prepare and chroot
  68. sudo cp /etc/resolv.conf edit/etc/
  69. sudo mount --bind /dev/ edit/dev
  70.  
  71. # Learned this inline scripting from https://askubuntu.com/questions/551195/scripting-chroot-how-to
  72. cat << EOF | sudo chroot edit
  73. mount -t proc none /proc
  74. mount -t sysfs none /sys
  75. mount -t devpts none /dev/pts
  76.  
  77. # "To avoid locale issues and in order to import GPG keys..."
  78. export HOME=/root
  79. export LC_ALL=C
  80. dbus-uuidgen > /var/lib/dbus/machine-id
  81. dpkg-divert --local --rename --add /sbin/initctl
  82. ln -s /bin/true /sbin/initctl
  83.  
  84. #Customizations
  85.  
  86. # Add Google Chrome's stable repository to apt (hey, I like Chrome)
  87. echo "deb http://dl.google.com/linux/chrome/deb/ stable main" | tee /etc/apt/sources.list.d/google-chrome.list
  88. wget -q -O - https://dl.google.com/linux/linux_signing_key.pub | apt-key add -
  89.  
  90. #Update and Upgrade (distributions)
  91. apt-get update
  92. apt-get purge thunderbird -y
  93. apt-get dist-upgrade -y
  94.  
  95. apt-get install google-chrome-stable yubikey-luks -y
  96. apt-get autoremove -y
  97. apt-get autoclean -y
  98.  
  99. #Clean up
  100. rm -rf /tmp/* ~/.bash_history
  101. rm /var/lib/dbus/machine-id
  102. rm /sbin/initctl
  103. dpkg-divert --rename --remove /sbin/initctl
  104.  
  105. # "now umount (unmount) special filesystems and exit chroot"
  106. umount /proc || umount -lf /proc
  107. umount /sys
  108. umount /dev/pts
  109. EOF
  110.  
  111. sudo umount edit/dev
  112.  
  113. echo "Regenerate the manifest"
  114.  
  115. #Regenerate the manifest
  116. sudo chmod +w extract-cd/casper/filesystem.manifest
  117. sudo chroot edit dpkg-query -W --showformat='${Package} ${Version}\n' | sudo tee extract-cd/casper/filesystem.manifest
  118. sudo cp extract-cd/casper/filesystem.manifest extract-cd/casper/filesystem.manifest-desktop
  119. sudo sed -i '/ubiquity/d' extract-cd/casper/filesystem.manifest-desktop
  120. sudo sed -i '/casper/d' extract-cd/casper/filesystem.manifest-desktop
  121.  
  122. #Compress the filesystem
  123. # Delete any existing squashfs - normally nothing to delete/rm.
  124. sudo rm extract-cd/casper/filesystem.squashfs
  125. sudo mksquashfs edit extract-cd/casper/filesystem.squashfs -b 1048576
  126.  
  127. #"Update the filesystem.size file, which is needed by the installer"
  128. printf $(sudo du -sx --block-size=1 edit | cut -f1) | sudo tee extract-cd/casper/filesystem.size
  129.  
  130. #"Remove old md5sum.txt and calculate new md5 sums"
  131. cd extract-cd
  132. sudo rm md5sum.txt
  133. find -type f -print0 | sudo xargs -0 md5sum | grep -v isolinux/boot.cat | sudo tee md5sum.txt
  134.  
  135. #"Create the ISO image"
  136. #manpage for genisoimage http://manpages.ubuntu.com/manpages/trusty/man1/genisoimage.1.html
  137. #original
  138. #sudo genisoimage -D -r -V "$IMAGE_NAME" -cache-inodes -J -l -b isolinux/isolinux.bin -c isolinux/boot.cat -no-emul-boot -boot-load-size 4 -boot-info-table -o ../$IMAGE_NAME.iso .
  139.  
  140. #from EFI Q&A: https://askubuntu.com/questions/457528/how-do-i-create-an-efi-bootable-iso-of-a-customized-version-of-ubuntu
  141. #sudo mkisofs -U -A "Custom1604" -V "Custom1604" -volset "Custom1604" -J -joliet-long -r -v -T -o ../Custom1604.iso -b isolinux/isolinux.bin -c isolinux/boot.cat -no-emul-boot -boot-load-size 4 -boot-info-table -eltorito-alt-boot -e boot/grub/efi.img -no-emul-boot .
  142.  
  143. # From https://linuxconfig.org/legacy-bios-uefi-and-secureboot-ready-ubuntu-live-image-customization
  144. # THIS WORKS for creating a .iso that can boot a PC from USB after dd to the USB drive, and as a file referenced as the boot image for a VM (e.g. VirtualBox)
  145. sudo xorriso -as mkisofs -isohybrid-mbr isolinux/isohdpfx.bin -c isolinux/boot.cat -b isolinux/isolinux.bin -no-emul-boot -boot-load-size 4 -boot-info-table -eltorito-alt-boot -e boot/grub/efi.img -no-emul-boot -isohybrid-gpt-basdat -o ../$IMAGE_NAME.iso .
  146.  
  147. # Not necessary, but you can check that a bootable partition is visible to fdisk.
  148. # If no bootable partiction is visible to fdisk, my experience is that the ISO will not boot from USB.
  149. # If so, we should be good to go.
  150. sudo fdisk -lu ../$IMAGE_NAME.iso
RAW Paste Data