]> git.friedersdorff.com Git - max/disk_creator.git/blob - disk_creator.sh
4dd8217f3db1dc34377e6ff0235d7d6bd1fd266d
[max/disk_creator.git] / disk_creator.sh
1 #!/bin/bash
2
3 # The name of the USB drive you want to create
4 usb_drive="/dev/sdd"
5
6 # The location of the linux distribution you want to use
7 os_image="/mnt/os_images/xubuntu-18.04.2-desktop-amd64.iso"
8
9 # Removing leading directory/folder name
10 image_name="${os_image##*/}"
11 # Removing the filename extension
12 os_name="$(echo ${image_name%.*} | tr '-' ' ' | tr '_' ' ')"
13
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)
16
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))}')"
22
23 # Calculate the start of the 'usbdata' partition
24 let usbdata_part_start="${live_part_size} + 2048 + 3*1024*1024*1024/${block_size}"
25
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 
28 # configuring grub
29 modprobe loop
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')"
35 umount "${l_mount}"
36 rmdir "${l_mount}"
37 losetup -d "${loop_dev}"
38
39 if [ $(echo "${kernel}" | wc -l) -gt 1 ]; then
40         echo "Too many possible linux kernels found:"
41         echo $kernel
42 fi
43
44 if [ $(echo "${initramfs}" | wc -l) -gt 1 ]; then
45         echo "Too many possible initramfs found:"
46         echo $initramfs
47 fi
48
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 
54 # operating system
55 sfdisk "${usb_drive}" << EOF
56
57 label: gpt
58
59 start=$usbdata_part_start, type=11
60 start=2048, size=$live_part_size, type=1
61 type=20
62 EOF
63
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"
68
69 # Create a temporary directory
70 m_point=$(mktemp -d)
71
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}" \
78         --target=x86_64-efi \
79         --efi-directory="${m_point}" \
80         --boot-directory="${m_point}/boot" \
81         --removable
82
83 # Install the configuration file for the bootloader
84 cat <<EOF> "${m_point}/boot/grub/grub.cfg"
85 set timeout=10
86 set default=0
87 insmod loopback
88 insmod all_video
89
90 menuentry "Run ${os_name} Persistent, in RAM" {
91         loopback loop /iso_boot/${image_name}
92         set gfxpayload=keep
93         linux (loop)/${kernel} boot=casper iso-scan/filename=/iso_boot/${image_name} quiet splash persistent toram ---
94         initrd (loop)/${initramfs}
95 }
96
97 menuentry "Run ${os_name} - Persistent" {
98         loopback loop /iso_boot/${image_name}
99         set gfxpayload=keep
100         linux (loop)/${kernel} boot=casper iso-scan/filename=/iso_boot/${image_name} quiet splash persistent ---
101         initrd (loop)/${initramfs}
102 }
103
104 menuentry "Run ${os_name}" {
105         loopback loop /iso_boot/${image_name}
106         set gfxpayload=keep
107         linux (loop)/${kernel} boot=casper iso-scan/filename=/iso_boot/${image_name} quiet splash ---
108         initrd (loop)/${initramfs}
109 }
110 EOF
111
112 # Clean up
113 umount "${m_point}"
114 rmdir "${m_point}"