2 # Copyright 2019 Maximilian Friedersdorff
4 # This file is part of 'disk_creator.sh'
6 # 'disk_creator.sh' is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
11 # Foobar is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with Foobar. If not, see <https://www.gnu.org/licenses/>.
21 echo "disk_creator.sh OS_IMAGE USB_DRIVE"
24 echo "disk_creator.sh ~/Downloads/xubuntu-18.04.2-desktop-amd64.iso /dev/sdd"
26 echo "Create a live usb drive on the device '/dev/sdd' with the os found in"
27 echo "~/Downloads/xubuntu-18.04.2-desktop-amd64.iso"
31 echo "ERROR: expected exactly 2 arguments"
37 echo "ERROR: $2 is not a block device"
43 echo "ERROR: $1 is not a regular file"
48 # The name of the USB drive you want to create
51 # The location of the linux distribution you want to use
54 # Removing leading directory/folder name
55 image_name="${os_image##*/}"
56 # Removing the filename extension
57 os_name="$(echo ${image_name%.*} | tr '-' ' ' | tr '_' ' ')"
59 # Querying the block size, the logical size of entities on the USB drive
60 block_size=$(cat /sys/block/${usb_drive#/dev/}/queue/hw_sector_size)
62 # Calculate the size of the partition to hold the iso and boot loader. Allow
63 # 50MiB for the bootloader (this is way overkill).
64 live_part_size="$(ls -l ${os_image} |
65 awk -v block_size=${block_size} \
66 '{print (($5/block_size) + (50*1024*1024/block_size))}')"
68 # Calculate the start of the 'usbdata' partition
69 let usbdata_part_start="${live_part_size} + 2048 + 3*1024*1024*1024/${block_size}"
72 # Find out the location of the linux kernel and the initramfs (initial ramdisk
73 # file system) in the os image file. We need these locations later when
76 loop_dev="$(losetup --show -f ${os_image})"
77 l_mount="$(mktemp -d)"
78 mount -o ro "${loop_dev}" "${l_mount}"
79 kernel="$(find ${l_mount} -iname *vmlinuz* -printf '%P\n')"
80 initramfs="$(find ${l_mount} -iname *initr* -printf '%P\n')"
83 losetup -d "${loop_dev}"
85 if [ $(echo "${kernel}" | wc -l) -gt 1 ]; then
86 echo "Too many possible linux kernels found:"
90 if [ $(echo "${initramfs}" | wc -l) -gt 1 ]; then
91 echo "Too many possible initramfs found:"
95 # Partition the usb drive. The partition listed first can be read by linux
96 # and windows operating systems. It is located at the 'end' of the drive
97 # The partition listed second contains the operating system and boot loader,
98 # it is located at the 'beginning' of the drive. The partition listed last fills
99 # the space between the other two, and is used to record changes made to the
101 sfdisk "${usb_drive}" <<- EOF
105 start=$usbdata_part_start, type=11
106 start=2048, size=$live_part_size, type=1
110 # Format each partition with the relevant filesystem.
111 mkfs.ntfs --fast -L usbdata "${usb_drive}1"
112 mkfs.fat -F 32 "${usb_drive}2"
113 mkfs.ext4 -q -F -L casper-rw "${usb_drive}3"
115 # Create a temporary directory
118 # Mount the drive to the temporary directory, create some folders/directories,
119 # copy the os image, and install the boot loader.
120 mount "${usb_drive}2" "${m_point}"
121 mkdir "${m_point}"/{boot,iso_boot}
122 cp "${os_image}" "${m_point}/iso_boot" && sync
123 grub-install "${usb_drive}" \
124 --target=x86_64-efi \
125 --efi-directory="${m_point}" \
126 --boot-directory="${m_point}/boot" \
129 # Install the configuration file for the bootloader
130 cat <<-EOF> "${m_point}/boot/grub/grub.cfg"
136 menuentry "Run ${os_name} Persistent, in RAM" {
137 loopback loop /iso_boot/${image_name}
139 linux (loop)/${kernel} boot=casper iso-scan/filename=/iso_boot/${image_name} quiet splash persistent toram ---
140 initrd (loop)/${initramfs}
143 menuentry "Run ${os_name} - Persistent" {
144 loopback loop /iso_boot/${image_name}
146 linux (loop)/${kernel} boot=casper iso-scan/filename=/iso_boot/${image_name} quiet splash persistent ---
147 initrd (loop)/${initramfs}
150 menuentry "Run ${os_name}" {
151 loopback loop /iso_boot/${image_name}
153 linux (loop)/${kernel} boot=casper iso-scan/filename=/iso_boot/${image_name} quiet splash ---
154 initrd (loop)/${initramfs}
163 main 2> >(sed $'s,.*,\e[31m&\e[m, '>&2)