-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisk_creator.sh
executable file
·166 lines (140 loc) · 5 KB
/
disk_creator.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#!/bin/bash
# Copyright 2019 Maximilian Friedersdorff
#
# This file is part of 'disk_creator.sh'
#
# 'disk_creator.sh' is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# disk_creator.sh is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with disk_creator.sh. If not, see <https://www.gnu.org/licenses/>.
usage() {
echo "Usage:"
echo "disk_creator.sh OS_IMAGE USB_DRIVE"
echo
echo "Example:"
echo "disk_creator.sh ~/Downloads/xubuntu-18.04.2-desktop-amd64.iso /dev/sdd"
echo
echo "Create a live usb drive on the device '/dev/sdd' with the os found in"
echo "~/Downloads/xubuntu-18.04.2-desktop-amd64.iso"
}
if [ $# -ne 2 ]; then
echo "ERROR: expected exactly 2 arguments"
usage
exit 1
fi
if [ ! -b $2 ]; then
echo "ERROR: $2 is not a block device"
usage
exit 1
fi
if [ ! -f $1 ]; then
echo "ERROR: $1 is not a regular file"
usage
exit 1
fi
# The name of the USB drive you want to create
usb_drive="$2"
# The location of the linux distribution you want to use
os_image="$1"
# Removing leading directory/folder name
image_name="${os_image##*/}"
# Removing the filename extension
os_name="$(echo ${image_name%.*} | tr '-' ' ' | tr '_' ' ')"
# Querying the block size, the logical size of entities on the USB drive
block_size=$(cat /sys/block/${usb_drive#/dev/}/queue/hw_sector_size)
# Calculate the size of the partition to hold the iso and boot loader. Allow
# 50MiB for the bootloader (this is way overkill).
live_part_size="$(ls -l ${os_image} |
awk -v block_size=${block_size} \
'{print (($5/block_size) + (50*1024*1024/block_size))}')"
# Calculate the start of the 'usbdata' partition
let usbdata_part_start="${live_part_size} + 2048 + 3*1024*1024*1024/${block_size}"
main() {
# Find out the location of the linux kernel and the initramfs (initial ramdisk
# file system) in the os image file. We need these locations later when
# configuring grub
modprobe loop
loop_dev="$(losetup --show -f ${os_image})"
l_mount="$(mktemp -d)"
mount -o ro "${loop_dev}" "${l_mount}"
kernel="$(find ${l_mount} -iname *vmlinuz* -printf '%P\n')"
initramfs="$(find ${l_mount} -iname *initr* -printf '%P\n')"
umount "${l_mount}"
rmdir "${l_mount}"
losetup -d "${loop_dev}"
if [ $(echo "${kernel}" | wc -l) -gt 1 ]; then
echo "Too many possible linux kernels found:"
echo $kernel
fi
if [ $(echo "${initramfs}" | wc -l) -gt 1 ]; then
echo "Too many possible initramfs found:"
echo $initramfs
fi
# Partition the usb drive. The partition listed first can be read by linux
# and windows operating systems. It is located at the 'end' of the drive
# The partition listed second contains the operating system and boot loader,
# it is located at the 'beginning' of the drive. The partition listed last fills
# the space between the other two, and is used to record changes made to the
# operating system
sfdisk "${usb_drive}" <<- EOF
label: gpt
start=$usbdata_part_start, type=11
start=2048, size=$live_part_size, type=1
type=20
EOF
# waiting to ensure disk has been re recognised after partitioning
sleep 10
# Format each partition with the relevant filesystem.
mkfs.ntfs --fast -L usbdata "${usb_drive}1"
mkfs.fat -F 32 "${usb_drive}2"
mkfs.ext4 -q -F -L casper-rw "${usb_drive}3"
# Create a temporary directory
m_point=$(mktemp -d)
# Mount the drive to the temporary directory, create some folders/directories,
# copy the os image, and install the boot loader.
mount "${usb_drive}2" "${m_point}"
mkdir "${m_point}"/{boot,iso_boot}
cp "${os_image}" "${m_point}/iso_boot" && sync
grub-install "${usb_drive}" \
--target=x86_64-efi \
--efi-directory="${m_point}" \
--boot-directory="${m_point}/boot" \
--removable
# Install the configuration file for the bootloader
cat <<-EOF> "${m_point}/boot/grub/grub.cfg"
set timeout=10
set default=0
insmod loopback
insmod all_video
menuentry "Run ${os_name} Persistent, in RAM" {
loopback loop /iso_boot/${image_name}
set gfxpayload=keep
linux (loop)/${kernel} boot=casper iso-scan/filename=/iso_boot/${image_name} quiet splash persistent toram ---
initrd (loop)/${initramfs}
}
menuentry "Run ${os_name} - Persistent" {
loopback loop /iso_boot/${image_name}
set gfxpayload=keep
linux (loop)/${kernel} boot=casper iso-scan/filename=/iso_boot/${image_name} quiet splash persistent ---
initrd (loop)/${initramfs}
}
menuentry "Run ${os_name}" {
loopback loop /iso_boot/${image_name}
set gfxpayload=keep
linux (loop)/${kernel} boot=casper iso-scan/filename=/iso_boot/${image_name} quiet splash ---
initrd (loop)/${initramfs}
}
EOF
# Clean up
umount "${m_point}"
rmdir "${m_point}"
}
main 2> >(sed $'s,.*,\e[31m&\e[m, '>&2)