forked from seamusdemora/RonR-RPi-image-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
image-set-partuuid
211 lines (203 loc) · 5.51 KB
/
image-set-partuuid
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
#!/bin/bash
trap '{ stty sane; echo ""; errexit "Aborted"; }' SIGINT SIGTERM
MNTPATH="/tmp/img-set-partuuid-mnt"
mkloop()
{
LOOP="$(losetup -f --show -P "${IMGFILE}")"
if [ $? -ne 0 ]; then
errexit "Unable to create loop device"
fi
}
rmloop()
{
if [ "${LOOP}" != "" ]; then
losetup -d "${LOOP}"
LOOP=""
fi
}
mntimg()
{
MNTED=TRUE
mkloop
if [ ! -d "${MNTPATH}/" ]; then
mkdir "${MNTPATH}/"
if [ $? -ne 0 ]; then
errexit "Unable to make ROOT partition mount point"
fi
fi
mount "${LOOP}p2" "${MNTPATH}/"
if [ $? -ne 0 ]; then
errexit "Unable to mount image ROOT partition"
fi
if [ ! -d "${MNTPATH}/boot/" ]; then
mkdir -p "${MNTPATH}/boot/"
if [ $? -ne 0 ]; then
errexit "Unable to make BOOT partition mount point"
fi
fi
mount "${LOOP}p1" "${MNTPATH}/boot/"
if [ $? -ne 0 ]; then
errexit "Unable to mount image BOOT partition"
fi
}
umntimg()
{
umount "${MNTPATH}/boot/"
if [ $? -ne 0 ]; then
errexit "Unable to unmount image BOOT partition"
fi
umount "${MNTPATH}/"
if [ $? -ne 0 ]; then
errexit "Unable to unmount image ROOT partition"
fi
rmloop
rm -r "${MNTPATH}/"
MNTED=FALSE
}
errexit()
{
echo ""
echo "$1"
echo ""
if [ "${MNTED}" = "TRUE" ]; then
umount "${MNTPATH}/boot/" &> /dev/null
umount "${MNTPATH}/" &> /dev/null
rm -rf "${MNTPATH}/" &> /dev/null
fi
rmloop
exit 1
}
LOOP=""
MNTED=FALSE
if [ $(id -u) -ne 0 ]; then
errexit "$0 must be run as root user"
fi
PGMNAME="$(basename $0)"
for PID in $(pidof -x -o %PPID "${PGMNAME}"); do
if [ ${PID} -ne $$ ]; then
errexit "${PGMNAME} is already running"
fi
done
gdisk -l "${DEVICE}" &> /dev/null
if [ $? -eq 127 ]; then
echo ""
echo "gdisk not installed"
echo ""
echo -n "Ok to install gdisk (y/n)? "
while read -r -n 1 -s answer; do
if [[ "${answer}" = [yYnN] ]]; then
echo "${answer}"
if [[ "${answer}" = [yY] ]]; then
break
else
errexit "Aborted"
fi
fi
done
echo ""
echo "Installing gdisk"
echo ""
apt-get update
apt-get install gdisk
fi
IMGFILE="$1"
PARTUUID="$2"
if [ "${IMGFILE}" = "" ]; then
errexit "Usage: $0 imagefile [ hhhhhhhh-02 | hhhhhhhh-hhhh-hhhh-hhhh-hhhhhhhhhhhh | random ]"
fi
if [ ! -f "${IMGFILE}" ]; then
errexit "${IMGFILE} not found"
fi
INFO="$(sfdisk -d "${IMGFILE}")"
PTTYPE="$(sed -n "s|^label: \(\S\+\).*$|\1|p" <<< "${INFO}")"
if [[ "${PTTYPE}" != "dos" && "${PTTYPE}" != "gpt" ]]; then
errexit "Unsupported partition table type: ${PTTYPE}"
fi
if [ "${PTTYPE}" = "dos" ]; then
PTUUID="$(sed -n "s|^label-id: 0x\(\S\+\).*$|\1|p" <<< "${INFO}")"
ORIGUUID="${PTUUID}-02"
ORIGUUID_1="${PTUUID}-01"
ORIGUUID_2="${PTUUID}-02"
else
ORIGUUID="$(sed -n "s|^${IMGFILE}2.*uuid=\(\S\+\).*$|\1|p" <<< "${INFO}")"
ORIGUUID_1="$(sed -n "s|^${IMGFILE}1.*uuid=\(\S\+\).*$|\1|p" <<< "${INFO}")"
ORIGUUID_2="$(sed -n "s|^${IMGFILE}2.*uuid=\(\S\+\).*$|\1|p" <<< "${INFO}")"
fi
ORIGUUID="$(tr [A-Z] [a-z] <<< "${ORIGUUID}")"
ORIGUUID_1="$(tr [A-Z] [a-z] <<< "${ORIGUUID_1}")"
ORIGUUID_2="$(tr [A-Z] [a-z] <<< "${ORIGUUID_2}")"
if [ "${PARTUUID}" = "" ]; then
PARTUUID="${ORIGUUID}"
else
PARTUUID="$(tr [A-Z] [a-z] <<< "${PARTUUID}")"
if [[ "${PARTUUID}" != "random" && (("${PTTYPE}" = "dos" && ! "${PARTUUID}" =~ ^[[:xdigit:]]{8}-02$) || \
("${PTTYPE}" = "gpt" && ! "${PARTUUID}" =~ ^[[:xdigit:]]{8}-[[:xdigit:]]{4}-[[:xdigit:]]{4}-[[:xdigit:]]{4}-[[:xdigit:]]{12}$)) ]]; then
errexit "Invalid PARTUUID: ${PARTUUID}"
fi
echo ""
echo -n "Set PARTUUID on ${IMGFILE} to ${PARTUUID} (y/n)? "
while read -r -n 1 -s answer; do
if [[ "${answer}" = [yYnN] ]]; then
echo "${answer}"
if [[ "${answer}" = [yY] ]]; then
break
else
errexit "Aborted"
fi
fi
done
echo ""
mkloop
if [ "$(blkid "${LOOP}p2" | sed -n 's|^.*TYPE="\(\S\+\)".*|\1|p')" = "f2fs" ]; then
fsck.f2fs "${LOOP}p2"
else
fsck.ext4 -f "${LOOP}p2"
fi
if [ $? -ne 0 ]; then
errexit "Image ROOT filesystem appears corrupted"
fi
rmloop
if [ "${PTTYPE}" = "dos" ]; then
if [ "${PARTUUID}" = "random" ]; then
PTUUID="$(hexdump -n 4 -e '"%08X"' /dev/random | tr [A-Z] [a-z])"
else
PTUUID="${PARTUUID:0:(${#PARTUUID} - 3)}"
fi
fdisk "${IMGFILE}" <<EOF &> /dev/null
x
i
0x${PTUUID}
r
w
EOF
else
if [ "${PARTUUID}" = "random" ]; then
sgdisk -u 2:'R' "${IMGFILE}" > /dev/null
else
sgdisk -u 2:${PARTUUID} "${IMGFILE}" > /dev/null
fi
fi
partprobe
INFO="$(sfdisk -d "${IMGFILE}")"
if [ "${PTTYPE}" = "dos" ]; then
PTUUID="$(sed -n "s|^label-id: 0x\(\S\+\).*$|\1|p" <<< "${INFO}")"
PARTUUID="${PTUUID}-02"
PARTUUID_1="${PTUUID}-01"
PARTUUID_2="${PTUUID}-02"
else
PARTUUID="$(sed -n "s|^${IMGFILE}2.*uuid=\(\S\+\).*$|\1|p" <<< "${INFO}")"
PARTUUID_1="$(sed -n "s|^${IMGFILE}1.*uuid=\(\S\+\).*$|\1|p" <<< "${INFO}")"
PARTUUID_2="$(sed -n "s|^${IMGFILE}2.*uuid=\(\S\+\).*$|\1|p" <<< "${INFO}")"
fi
PARTUUID="$(tr [A-Z] [a-z] <<< "${PARTUUID}")"
PARTUUID_1="$(tr [A-Z] [a-z] <<< "${PARTUUID_1}")"
PARTUUID_2="$(tr [A-Z] [a-z] <<< "${PARTUUID_2}")"
mntimg
sed -i "/^[[:space:]]*#/!s|^\(PARTUUID=\)${ORIGUUID_1}\(\s\+/boot\s\+.*\)$|\1${PARTUUID_1}\2|" "${MNTPATH}/etc/fstab"
sed -i "/^[[:space:]]*#/!s|^\(PARTUUID=\)${ORIGUUID_2}\(\s\+/\s\+.*\)$|\1${PARTUUID_2}\2|" "${MNTPATH}/etc/fstab"
sed -i "/^[[:space:]]*#/!s|^\(.*root=PARTUUID=\)${ORIGUUID_2}\(\s\+.*\)$|\1${PARTUUID_2}\2|" "${MNTPATH}/boot/cmdline.txt"
umntimg
fi
echo ""
echo "PARTUUID on ${IMGFILE} is set to ${PARTUUID}"
echo ""