-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathinit_resize.sh
243 lines (192 loc) · 5.66 KB
/
init_resize.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
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
#!/bin/sh
cat << 'EOF'
## _ _
## _ __ ___ ___(_)___(_)_ __ __ _
## | '__/ _ \/ __| |_ / | '_ \ / _` |
## | | | __/\__ \ |/ /| | | | | (_| |
## |_| \___||___/_/___|_|_| |_|\__, |
## |___/
EOF
sleep 1
reboot_pi () {
umount /boot
mount / -o remount,ro
sync
if [ "$NOOBS" = "1" ]; then
if [ "$NEW_KERNEL" = "1" ]; then
reboot -f "$BOOT_PART_NUM"
else
echo "$BOOT_PART_NUM" > "/sys/module/${BCM_MODULE}/parameters/reboot_part"
fi
fi
echo b > /proc/sysrq-trigger
sleep 5
exit 0
}
check_commands () {
if ! command -v whiptail > /dev/null; then
echo "whiptail not found"
sleep 5
return 1
fi
for COMMAND in grep cut sed parted fdisk findmnt; do
if ! command -v $COMMAND > /dev/null; then
FAIL_REASON="$COMMAND not found"
return 1
fi
done
return 0
}
check_noobs () {
if [ "$BOOT_PART_NUM" = "1" ]; then
NOOBS=0
else
NOOBS=1
fi
}
get_variables () {
ROOT_PART_DEV=$(findmnt / -o source -n)
ROOT_PART_NAME=$(echo "$ROOT_PART_DEV" | cut -d "/" -f 3)
ROOT_DEV_NAME=$(echo /sys/block/*/"${ROOT_PART_NAME}" | cut -d "/" -f 4)
ROOT_DEV="/dev/${ROOT_DEV_NAME}"
ROOT_PART_NUM=$(cat "/sys/block/${ROOT_DEV_NAME}/${ROOT_PART_NAME}/partition")
BOOT_PART_DEV=$(findmnt /boot -o source -n)
BOOT_PART_NAME=$(echo "$BOOT_PART_DEV" | cut -d "/" -f 3)
BOOT_DEV_NAME=$(echo /sys/block/*/"${BOOT_PART_NAME}" | cut -d "/" -f 4)
BOOT_PART_NUM=$(cat "/sys/block/${BOOT_DEV_NAME}/${BOOT_PART_NAME}/partition")
OLD_DISKID=$(fdisk -l "$ROOT_DEV" | sed -n 's/Disk identifier: 0x\([^ ]*\)/\1/p')
check_noobs
ROOT_DEV_SIZE=$(cat "/sys/block/${ROOT_DEV_NAME}/size")
TARGET_END=$((ROOT_DEV_SIZE - 1))
PARTITION_TABLE=$(parted -m "$ROOT_DEV" unit s print | tr -d 's')
LAST_PART_NUM=$(echo "$PARTITION_TABLE" | tail -n 1 | cut -d ":" -f 1)
ROOT_PART_LINE=$(echo "$PARTITION_TABLE" | grep -e "^${ROOT_PART_NUM}:")
ROOT_PART_START=$(echo "$ROOT_PART_LINE" | cut -d ":" -f 2)
ROOT_PART_END=$(echo "$ROOT_PART_LINE" | cut -d ":" -f 3)
if [ "$NOOBS" = "1" ]; then
EXT_PART_LINE=$(echo "$PARTITION_TABLE" | grep ":::;" | head -n 1)
EXT_PART_NUM=$(echo "$EXT_PART_LINE" | cut -d ":" -f 1)
EXT_PART_START=$(echo "$EXT_PART_LINE" | cut -d ":" -f 2)
EXT_PART_END=$(echo "$EXT_PART_LINE" | cut -d ":" -f 3)
fi
}
fix_partuuid() {
mount -o remount,rw "$ROOT_PART_DEV"
mount -o remount,rw "$BOOT_PART_DEV"
DISKID="$(tr -dc 'a-f0-9' < /dev/hwrng | dd bs=1 count=8 2>/dev/null)"
fdisk "$ROOT_DEV" > /dev/null <<EOF
x
i
0x$DISKID
r
w
EOF
if [ "$?" -eq 0 ]; then
sed -i "s/${OLD_DISKID}/${DISKID}/g" /etc/fstab
sed -i "s/${OLD_DISKID}/${DISKID}/" /boot/cmdline.txt
sync
fi
mount -o remount,ro "$ROOT_PART_DEV"
mount -o remount,ro "$BOOT_PART_DEV"
}
check_variables () {
if [ "$NOOBS" = "1" ]; then
if [ "$EXT_PART_NUM" -gt 4 ] || \
[ "$EXT_PART_START" -gt "$ROOT_PART_START" ] || \
[ "$EXT_PART_END" -lt "$ROOT_PART_END" ]; then
FAIL_REASON="Unsupported extended partition"
return 1
fi
fi
if [ "$BOOT_DEV_NAME" != "$ROOT_DEV_NAME" ]; then
FAIL_REASON="Boot and root partitions are on different devices\nBOOT_DEV_NAME=$BOOT_DEV_NAME\nROOT_DEV_NAME=${ROOT_DEV_NAME}"
return 1
fi
if [ "$ROOT_PART_NUM" -ne "$LAST_PART_NUM" ]; then
FAIL_REASON="Root partition should be last partition"
return 1
fi
if [ "$ROOT_PART_END" -gt "$TARGET_END" ]; then
FAIL_REASON="Root partition runs past the end of device"
return 1
fi
if [ ! -b "$ROOT_DEV" ] || [ ! -b "$ROOT_PART_DEV" ] || [ ! -b "$BOOT_PART_DEV" ] ; then
FAIL_REASON="Could not determine partitions"
return 1
fi
}
check_kernel () {
MAJOR="$(uname -r | cut -f1 -d.)"
MINOR="$(uname -r | cut -f2 -d.)"
if [ "$MAJOR" -eq "4" ] && [ "$MINOR" -lt "9" ]; then
return 0
fi
if [ "$MAJOR" -lt "4" ]; then
return 0
fi
NEW_KERNEL=1
}
main () {
get_variables
if ! check_variables; then
return 1
fi
check_kernel
if [ "$NOOBS" = "1" ] && [ "$NEW_KERNEL" != "1" ]; then
BCM_MODULE=$(grep -e "^Hardware" /proc/cpuinfo | cut -d ":" -f 2 | tr -d " " | tr '[:upper:]' '[:lower:]')
if ! modprobe "$BCM_MODULE"; then
FAIL_REASON="Couldn't load BCM module $BCM_MODULE"
return 1
fi
fi
if [ "$ROOT_PART_END" -eq "$TARGET_END" ]; then
echo "ROOT_PART_END=${ROOT_PART_END} TARGET_END=${TARGET_END} "
sleep 20
reboot_pi
fi
if [ "$NOOBS" = "1" ]; then
if ! parted -m "$ROOT_DEV" u s resizepart "$EXT_PART_NUM" yes "$TARGET_END"; then
FAIL_REASON="Extended partition resize failed"
return 1
fi
fi
if ! parted -m "$ROOT_DEV" u s resizepart "$ROOT_PART_NUM" "$TARGET_END"; then
FAIL_REASON="Root partition resize failed"
return 1
fi
fix_partuuid
return 0
}
mount -t proc proc /proc
mount -t sysfs sys /sys
mount -t tmpfs tmp /run
mkdir -p /run/systemd
mount /boot
mount / -o remount,ro
sed -i 's| init=/usr/lib/raspi-config/init_resize\.sh||' /boot/cmdline.txt
sed -i 's| sdhci\.debug_quirks2=4||' /boot/cmdline.txt
if ! grep -q splash /boot/cmdline.txt; then
sed -i "s/ quiet//g" /boot/cmdline.txt
fi
mount /boot -o remount,ro
sync
echo 1 > /proc/sys/kernel/sysrq
if ! check_commands; then
echo "check commands failed"
sleep 10
reboot_pi
fi
if main; then
whiptail --infobox "Resized root filesystem. Rebooting in 5 seconds..." 20 60
sleep 5
else
sleep 5
whiptail --msgbox "Could not expand filesystem, please try raspi-config or rc_gui.\n${FAIL_REASON}" 20 60
sleep 10
fi
# flash screen before rebooting
printf '\e[?5h' # Turn on reverse video
sleep 1
printf '\e[?5l' # Turn on normal video
sleep 10
reboot_pi