-
Notifications
You must be signed in to change notification settings - Fork 7
/
mkosi-resize
executable file
·62 lines (48 loc) · 1.51 KB
/
mkosi-resize
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
#!/bin/bash
# run this script on the target disk after dd'ing the image there
# it only works when the root partition is the last one
set -e
disk=$1
# Make sure we print an error message, loud and clear
trap '
ret=$?
set +e
if [ $ret -ne 0 ]; then
echo "Failed to expand root" > /dev/stderr
fi
' EXIT
trap 'exit 1;' SIGINT
# Recreate partition table by removing the "last-lba" setting and the root
# partition size argument. This allows us to later extend the filesystem
# using the free space and also allows us to put the alternate GPT table to
# end of the device, where it belongs
function recreate_partition_table() {
ptable=$(mktemp --tmpdir expand-part.XXXXXX)
# save current GPT description
sfdisk --dump $disk | \
sed -e "/$1/s/[[:space:]]*size=[[:space:]]*[^,]*,//" \
-e "/^last-lba/d" > $ptable
# Erase any partition table, including MBR that might contain old partitions
wipefs --all $disk
# write back partition table
sfdisk $disk < $ptable
cat $ptable
rm -f $ptable
}
function expand_last() {
# now figure out what is the root parition and extend it
read part xxxxx <<<$(sfdisk --dump $disk | tail -n 1)
e2fsck -f $part
resize2fs $part
}
last_part_uuid=$(sfdisk --dump ~/ubuntu-19.10.raw | tail -n 1 | sed 's/.*uuid=\([^ ,]*\).*/\1/')
recreate_partition_table $last_part_uuid
F=""
if [ -f $disk ]; then
F=$disk
disk=$(losetup -f --show -P $F)
fi
expand_last $disk
if [ -n "$F" ]; then
sudo losetup -d $disk
fi