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}"
71 # Find out the location of the linux kernel and the initramfs (initial ramdisk
72 # file system) in the os image file. We need these locations later when
75 loop_dev="$(losetup --show -f ${os_image})"
76 l_mount="$(mktemp -d)"
77 mount -o ro "${loop_dev}" "${l_mount}"
78 kernel="$(find ${l_mount} -iname *vmlinuz* -printf '%P\n')"
79 initramfs="$(find ${l_mount} -iname *initr* -printf '%P\n')"
82 losetup -d "${loop_dev}"
84 if [ $(echo "${kernel}" | wc -l) -gt 1 ]; then
85 echo "Too many possible linux kernels found:"
89 if [ $(echo "${initramfs}" | wc -l) -gt 1 ]; then
90 echo "Too many possible initramfs found:"
94 # Partition the usb drive. The partition listed first can be read by linux
95 # and windows operating systems. It is located at the 'end' of the drive
96 # The partition listed second contains the operating system and boot loader,
97 # it is located at the 'beginning' of the drive. The partition listed last fills
98 # the space between the other two, and is used to record changes made to the
100 sfdisk "${usb_drive}" << EOF
104 start=$usbdata_part_start, type=11
105 start=2048, size=$live_part_size, type=1
109 # Format each partition with the relevant filesystem.
110 mkfs.ntfs --fast -L usbdata "${usb_drive}1"
111 mkfs.fat -F 32 "${usb_drive}2"
112 mkfs.ext4 -q -F -L casper-rw "${usb_drive}3"
114 # Create a temporary directory
117 # Mount the drive to the temporary directory, create some folders/directories,
118 # copy the os image, and install the boot loader.
119 mount "${usb_drive}2" "${m_point}"
120 mkdir "${m_point}"/{boot,iso_boot}
121 cp "${os_image}" "${m_point}/iso_boot" && sync
122 grub-install "${usb_drive}" \
123 --target=x86_64-efi \
124 --efi-directory="${m_point}" \
125 --boot-directory="${m_point}/boot" \
128 # Install the configuration file for the bootloader
129 cat <<EOF> "${m_point}/boot/grub/grub.cfg"
135 menuentry "Run ${os_name} Persistent, in RAM" {
136 loopback loop /iso_boot/${image_name}
138 linux (loop)/${kernel} boot=casper iso-scan/filename=/iso_boot/${image_name} quiet splash persistent toram ---
139 initrd (loop)/${initramfs}
142 menuentry "Run ${os_name} - Persistent" {
143 loopback loop /iso_boot/${image_name}
145 linux (loop)/${kernel} boot=casper iso-scan/filename=/iso_boot/${image_name} quiet splash persistent ---
146 initrd (loop)/${initramfs}
149 menuentry "Run ${os_name}" {
150 loopback loop /iso_boot/${image_name}
152 linux (loop)/${kernel} boot=casper iso-scan/filename=/iso_boot/${image_name} quiet splash ---
153 initrd (loop)/${initramfs}