-
Notifications
You must be signed in to change notification settings - Fork 0
/
rpi-img2GPT
executable file
·377 lines (316 loc) · 8.24 KB
/
rpi-img2GPT
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
#!/bin/bash
## Copyright (C) 2021..2023 Robin ALEXANDER
##
## This program 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.
##
## This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
DisplayUsage() {
cat <<- EOF
${COMMAND} flashes a raspberrypi disk image to a GPT-partitioned device
and performs a few adjustments to allow the raspberry-pi to boot directly
from the device.
When specifying partition sizes:
- you can use the suffix K,M,G,T
- the special value MAX to indicate the remaining disk space
Usage:
${COMMAND} [arguments]
Required arguments:
-d device, --device=device
Specify the name of the device to be partitioned
-i disk-image, --image=disk-image
Specify the name the source disk image
Optional arguments:
--boot-size=size-of-boot-partition
Specif the size of the boot partition
By default 64M
--root-size=size-of-root-partition
Specify the size of the root partition
By default MAX
--home-size=size-of-home-partition
Specify the size of the home partition
By default, there is no home partition
--var-size=size-of-var-partition
Specify the size of the var partition
By default, there is no var partition
--enable-ssh
Activate ssh
--quiet
Do not print status messages
EOF
}
ShowMsg() {
if [ "${beQuiet:=0}" = "0" ]; then
echo "${1}"
fi
}
GetArguments() {
while [ "$#" -gt 0 ] ; do
case "${1}" in
-h|--help)
DisplayUsage;
exit 0
;;
-d*|--device*)
if [ "${1}" = "${1#*=}" ]; then shift; fi
deviceName="${1#*=}"
;;
-i*|--image*)
if [ "${1}" = "${1#*=}" ]; then shift; fi
diskImage="${1#*=}"
;;
--boot-size*)
if [ "${1}" = "${1#*=}" ]; then shift; fi
size="${1#*=}"
partSize[1]="${size@U}"
;;
--root-size*)
if [ "${1}" = "${1#*=}" ]; then shift; fi
size="${1#*=}"
partSize[2]="${size@U}"
;;
--home-size*)
if [ "${1}" = "${1#*=}" ]; then shift; fi
size="${1#*=}"
partSize+=("${size@U}")
partName+=("home")
partType+=("8300")
partDevL+=("p2")
partExcl+=("")
partRoot+=("/home/")
partExcl[2]+=" --exclude=/home/"
;;
--var-size*)
if [ "${1}" = "${1#*=}" ]; then shift; fi
size="${1#*=}"
partSize+=("${size@U}")
partName+=("var")
partType+=("8300")
partDevL+=("p2")
partExcl+=("")
partRoot+=("/var/")
partExcl[2]+=" --exclude=/var/"
;;
--enable-ssh)
enableSSH="1"
;;
--quiet)
beQuiet="1"
;;
*)
DisplayUsage
exit 1
;;
esac
shift
done
}
CheckConditions() {
# Check that the program is run by root
if [ $(id -u) -ne 0 ]; then
echo "${COMMAND} must be run as root" >&2
exit 2
fi
# Check the device
if [ -z "${deviceName}" ]; then
echo "Device name not specified" >&2
exit 3
fi
if ! [ -b "${deviceName}" ]; then
echo "Device ${deviceName} not a block device" >&2
exit 3
fi
# Check the disk image
if [ -z "${diskImage}" ]; then
echo "Disk image not specified" >&2
exit 4
fi
if ! [ -f ${diskImage} ]; then
echo "Disk image ${diskImage} not found" >&2
exit 4
fi
# Check that there are no more than 1 partition with value 0
partitions0=$(echo -n "${partSize[@]}" | \
sed -e "s/MAX/@/g" -e "s/[^@]//g" | wc --chars)
if [ ${partitions0} -gt 1 ]; then
echo "Only 1 partition can take the value MAX" >&2
exit 5
fi
}
CheckDependencies() {
while [ "$#" -gt 0 ] ; do
if ! [ $(which "${1}") ]; then
echo "Command ${1} not found" >&2
exit 10
fi
shift
done
}
SetPartition() {
ShowMsg " > Creating and formatting ${4} on ${deviceName}${5}..."
sgdisk \
--new=${5}:${1}:${2} \
--typecode=0:${3} \
--change-name=0:${4} \
${deviceName} 1>/dev/null 2>&1
if [ "${3}" = "0700" ]; then
mkfs.fat -F 32 -n ${4} ${deviceName}${5} 1>/dev/null 2>&1
else
mkfs.ext4 -F -L ${4} ${deviceName}${5} 1>/dev/null 2>&1
fi
}
CopyPartition() {
mount --read-only ${1} ${tempDir}/from
mount ${2} ${tempDir}/to
ShowMsg " > Copying files to ${2}..."
rsync --archive ${3} ${tempDir}/from${4} ${tempDir}/to
sync
umount ${tempDir}/from
umount ${tempDir}/to
}
AddHybridMBR() {
ShowMsg " > Adding hybrid MBR table..."
cat <<- EOF | gdisk ${deviceName} 1>/dev/null
r
h
1
N
0c
N
Y
EE
w
Y
EOF
}
Cleanup() {
ShowMsg " > Cleaning up..."
sync
# Unmount partitions and delete the temporary directory
if [ -n "${tempDir}" ] && [ -d ${tempDir} ]; then
while (mount | grep --silent ${tempDir}/from); do
umount ${tempDir}/from
done
while (mount | grep --silent ${tempDir}/to); do
umount ${tempDir}/to
done
rm -rf ${tempDir}
fi
# Unmap disk image
if [ -n "${loopDevice}" ]; then
losetup --detach ${loopDevice}
fi
}
AdjustBootFiles() {
ShowMsg " > Adjusting file /boot/cmdline.txt..."
mount "${deviceName}1" "${tempDir}/to"
sed \
-e 's: init=[[:graph:]]*::' \
-e 's: fsck.repair=yes::' \
-e 's: quiet::' \
-e 's:root=PARTUUID=[0-9,a-f,\-]*:root=PARTLABEL=root:' \
-i ${tempDir}/to/cmdline.txt
if [ "${enableSSH}" = "1" ]; then
ShowMsg " > Enabling ssh..."
touch ${tempDir}/to/ssh
fi
umount "${tempDir}/to"
}
AdjustRootFiles() {
ShowMsg " > Adjusting file /etc/fstab..."
mount "${deviceName}2" "${tempDir}/to"
sed \
-e 's:^PARTUUID=.*/boot:PARTLABEL=boot \t/boot:' \
-e 's:^PARTUUID=.*/[[:blank:]]:PARTLABEL=root \t/:' \
-i ${tempDir}/to/etc/fstab
if [ "$(echo -n "${partName[@]}" | grep home)" ]; then
echo \
"PARTLABEL=home /home ext4 defaults,noatime 0 1" \
>> ${tempDir}/to/etc/fstab
fi
if [ "$(echo -n "${partName[@]}" | grep var)" ]; then
echo \
"PARTLABEL=var /var ext4 defaults,noatime 0 1" \
>> ${tempDir}/to/etc/fstab
fi
umount "${tempDir}/to"
}
# MAIN
## Set program execution flags
set -Eeuo pipefail
## Intercept interruptions
trap Cleanup ERR HUP INT QUIT ABRT TERM
## Initialize constants and variables
readonly COMMAND=$(basename ${0})
deviceName=""
diskImage=""
tempDir=""
loopDevice=""
enableSSH="0"
partName=([1]="boot" [2]="root")
partType=([1]="0700" [2]="8300")
partSize=([1]="64M" [2]="MAX")
partDevL=([1]="p1" [2]="p2")
partExcl=([1]="" [2]="")
partRoot=([1]="/" [2]="/")
## Get arguments
GetArguments $@
## Check conditions
CheckConditions
## Check dependencies
CheckDependencies rsync sfdisk sgdisk mkfs.fat mkfs.ext4 losetup
## unmount the partitions of the specified device, if any
mountpoints=$(lsblk \
--noheadings --list --output MOUNTPOINT ${deviceName} 2>/dev/null)
for mp in ${mountpoints}; do
umount ${mp}
done
## Create a temporary directory structure
tempDir=$(mktemp --directory)
mkdir ${tempDir}/from
mkdir ${tempDir}/to
## Map the disk image to a loop device
loopDevice=$(losetup --find --partscan --show ${diskImage})
## Clear the destination device
ShowMsg " > Erasing device ${deviceName}..."
sgdisk --zap-all ${deviceName} 1>/dev/null
## Perform smart copy
i=1
while [ $i -le ${#partSize[@]} ] && [ "${partSize[$i]}" != "MAX" ]; do
SetPartition "0" "+${partSize[$i]}" "${partType[$i]}" "${partName[$i]}" "$i"
CopyPartition \
"${loopDevice}${partDevL[$i]}" "${deviceName}$i" \
"${partExcl[$i]}" "${partRoot[$i]}"
((i++))
done
j=${#partSize[@]}
while [ $j -gt $i ] && [ "${partSize[$j]}" != "MAX" ]; do
SetPartition "-${partSize[$j]}" "0" "${partType[$j]}" "${partName[$j]}" "$j"
CopyPartition \
"${loopDevice}${partDevL[$j]}" "${deviceName}$j" \
"${partExcl[$j]}" "${partRoot[$j]}"
((j--))
done
if [ $i -le ${#partSize[@]} ]; then
SetPartition "0" "0" "${partType[$i]}" "${partName[$i]}" "$i"
CopyPartition \
"${loopDevice}${partDevL[$i]}" "${deviceName}$i" \
"${partExcl[$i]}" "${partRoot[$i]}"
fi
## Adjust files
AdjustBootFiles
AdjustRootFiles
## Create an hybrid MBR with partition type "0c" for the RPI bootloader
AddHybridMBR
# Clean up and exit
Cleanup
ShowMsg "Operation completed"
exit 0