3 # The name of the USB drive you want to create
6 # The location of the linux distribution you want to use
7 os_image="/mnt/os_images/xubuntu-18.04.2-desktop-amd64.iso"
9 # Removing leading directory/folder name
10 image_name="${os_image##*/}"
11 # Removing the filename extension
12 os_name="$(echo ${image_name%.*} | tr '-' ' ' | tr '_' ' ')"
14 # Querying the block size, the logical size of entities on the USB drive
15 block_size=$(cat /sys/block/${usb_drive#/dev/}/queue/hw_sector_size)
17 # Calculate the size of the partition to hold the iso and boot loader. Allow
18 # 50MiB for the bootloader (this is way overkill).
19 live_part_size="$(ls -l ${os_image} |
20 awk -v block_size=${block_size} \
21 '{print (($5/block_size) + (50*1024*1024/block_size))}')"
23 # Calculate the start of the 'usbdata' partition
24 let usbdata_part_start="${live_part_size} + 2048 + 3*1024*1024*1024/${block_size}"
26 # Find out the location of the linux kernel and the initramfs (initial ramdisk
27 # file system) in the os image file. We need these locations later when
30 loop_dev="$(losetup --show -f ${os_image})"
31 l_mount="$(mktemp -d)"
32 mount -o ro "${loop_dev}" "${l_mount}"
33 kernel="$(find ${l_mount} -iname *vmlinuz* -printf '%P\n')"
34 initramfs="$(find ${l_mount} -iname *initr* -printf '%P\n')"
37 losetup -d "${loop_dev}"
39 if [ $(echo "${kernel}" | wc -l) -gt 1 ]; then
40 echo "Too many possible linux kernels found:"
44 if [ $(echo "${initramfs}" | wc -l) -gt 1 ]; then
45 echo "Too many possible initramfs found:"
49 # Partition the usb drive. The partition listed first can be read by linux
50 # and windows operating systems. It is located at the 'end' of the drive
51 # The partition listed second contains the operating system and boot loader,
52 # it is located at the 'beginning' of the drive. The partition listed last fills
53 # the space between the other two, and is used to record changes made to the
55 sfdisk "${usb_drive}" << EOF
59 start=$usbdata_part_start, type=11
60 start=2048, size=$live_part_size, type=1
64 # Format each partition with the relevant filesystem.
65 mkfs.ntfs --fast -L usbdata "${usb_drive}1"
66 mkfs.fat -F 32 "${usb_drive}2"
67 mkfs.ext4 -q -F -L casper-rw "${usb_drive}3"
69 # Create a temporary directory
72 # Mount the drive to the temporary directory, create some folders/directories,
73 # copy the os image, and install the boot loader.
74 mount "${usb_drive}2" "${m_point}"
75 mkdir "${m_point}"/{boot,iso_boot}
76 cp "${os_image}" "${m_point}/iso_boot" && sync
77 grub-install "${usb_drive}" \
79 --efi-directory="${m_point}" \
80 --boot-directory="${m_point}/boot" \
83 # Install the configuration file for the bootloader
84 cat <<EOF> "${m_point}/boot/grub/grub.cfg"
90 menuentry "Run ${os_name} Persistent, in RAM" {
91 loopback loop /iso_boot/${image_name}
93 linux (loop)/${kernel} boot=casper iso-scan/filename=/iso_boot/${image_name} quiet splash persistent toram ---
94 initrd (loop)/${initramfs}
97 menuentry "Run ${os_name} - Persistent" {
98 loopback loop /iso_boot/${image_name}
100 linux (loop)/${kernel} boot=casper iso-scan/filename=/iso_boot/${image_name} quiet splash persistent ---
101 initrd (loop)/${initramfs}
104 menuentry "Run ${os_name}" {
105 loopback loop /iso_boot/${image_name}
107 linux (loop)/${kernel} boot=casper iso-scan/filename=/iso_boot/${image_name} quiet splash ---
108 initrd (loop)/${initramfs}