]> git.friedersdorff.com Git - max/disk_creator.git/blob - disk_creator.sh
Add pdf version of readme (rst2pdf)
[max/disk_creator.git] / disk_creator.sh
1 #!/bin/bash
2 # Copyright 2019 Maximilian Friedersdorff
3 #
4 # This file is part of 'disk_creator.sh'
5 #
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.
10 #
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.
15 #
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/>.
18
19 usage() {
20         echo "Usage:"
21         echo "disk_creator.sh OS_IMAGE USB_DRIVE"
22         echo
23         echo "Example:"
24         echo "disk_creator.sh ~/Downloads/xubuntu-18.04.2-desktop-amd64.iso /dev/sdd"
25         echo
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"
28 }
29
30 if [ $# -ne 2 ]; then
31         echo "ERROR: expected exactly 2 arguments"
32         usage
33         exit 1
34 fi
35
36 if [ ! -b $2 ]; then
37         echo "ERROR: $2 is not a block device"
38         usage
39         exit 1
40 fi
41
42 if [ ! -f $1 ]; then
43         echo "ERROR: $1 is not a regular file"
44         usage
45         exit 1
46 fi
47
48 # The name of the USB drive you want to create
49 usb_drive="$2"
50
51 # The location of the linux distribution you want to use
52 os_image="$1"
53
54 # Removing leading directory/folder name
55 image_name="${os_image##*/}"
56 # Removing the filename extension
57 os_name="$(echo ${image_name%.*} | tr '-' ' ' | tr '_' ' ')"
58
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)
61
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))}')"
67
68 # Calculate the start of the 'usbdata' partition
69 let usbdata_part_start="${live_part_size} + 2048 + 3*1024*1024*1024/${block_size}"
70
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 
73 # configuring grub
74 modprobe loop
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')"
80 umount "${l_mount}"
81 rmdir "${l_mount}"
82 losetup -d "${loop_dev}"
83
84 if [ $(echo "${kernel}" | wc -l) -gt 1 ]; then
85         echo "Too many possible linux kernels found:"
86         echo $kernel
87 fi
88
89 if [ $(echo "${initramfs}" | wc -l) -gt 1 ]; then
90         echo "Too many possible initramfs found:"
91         echo $initramfs
92 fi
93
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 
99 # operating system
100 sfdisk "${usb_drive}" << EOF
101
102 label: gpt
103
104 start=$usbdata_part_start, type=11
105 start=2048, size=$live_part_size, type=1
106 type=20
107 EOF
108
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"
113
114 # Create a temporary directory
115 m_point=$(mktemp -d)
116
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" \
126         --removable
127
128 # Install the configuration file for the bootloader
129 cat <<EOF> "${m_point}/boot/grub/grub.cfg"
130 set timeout=10
131 set default=0
132 insmod loopback
133 insmod all_video
134
135 menuentry "Run ${os_name} Persistent, in RAM" {
136         loopback loop /iso_boot/${image_name}
137         set gfxpayload=keep
138         linux (loop)/${kernel} boot=casper iso-scan/filename=/iso_boot/${image_name} quiet splash persistent toram ---
139         initrd (loop)/${initramfs}
140 }
141
142 menuentry "Run ${os_name} - Persistent" {
143         loopback loop /iso_boot/${image_name}
144         set gfxpayload=keep
145         linux (loop)/${kernel} boot=casper iso-scan/filename=/iso_boot/${image_name} quiet splash persistent ---
146         initrd (loop)/${initramfs}
147 }
148
149 menuentry "Run ${os_name}" {
150         loopback loop /iso_boot/${image_name}
151         set gfxpayload=keep
152         linux (loop)/${kernel} boot=casper iso-scan/filename=/iso_boot/${image_name} quiet splash ---
153         initrd (loop)/${initramfs}
154 }
155 EOF
156
157 # Clean up
158 umount "${m_point}"
159 rmdir "${m_point}"