-
Notifications
You must be signed in to change notification settings - Fork 32
/
test-growpart
executable file
·98 lines (75 loc) · 1.83 KB
/
test-growpart
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
#!/bin/bash
# NEED_ROOT
set -e
[ "$(id -u)" = "0" ] ||
{ echo "sorry, must be root"; exit 1; }
PT_TYPE="${PT_TYPE:-dos}" # dos or gpt
size=${DISK_SIZE_NEW:-1000M}
osize=${DISK_SIZE_ORIG:-500M}
MP=""
LODEV=""
TEMP_D=""
cleanup() {
if [ -n "$MP" ]; then
echo "unmount $MP";
umount "$MP";
fi
if [ -n "$LODEV" ]; then
echo "losetup -d $LODEV";
losetup -d "$LODEV";
fi
[ ! -d "${TEMP_D}" ] || rm -Rf "${TEMP_D}"
}
rq() {
local out="${TEMP_D}/out"
"$@" > "$out" 2>&1 || { echo "FAILED:" "$@"; cat "$out"; return 1; }
}
has_cmd() {
command -v "${1}" >/dev/null 2>&1
}
TEMP_D=$(mktemp -d ${TMPDIR:-/tmp}/${0##*/}.XXXXXX)
trap cleanup EXIT
img="${TEMP_D}/disk.img"
mp="${TEMP_D}/mp"
echo "Partitioning $PT_TYPE orig_size=$osize grow_size=$size."
echo "growpart is $(which growpart)"
rm -f $img
[ ! -e $mp ] || rmdir $mp || { echo "failed rmdir $mp"; exit 1; }
mkdir $mp
truncate -s $osize "$img"
label_flag="--label=${PT_TYPE}"
echo "2048," | rq sfdisk $label_flag --force --unit=S "$img"
truncate -s "$size" "$img"
lodev=$(losetup -f)
losetup -P "$lodev" "$img"
LODEV=$lodev
! has_cmd udevadm || udevadm settle
echo "set up $lodev"
lodevpart="${lodev}p1"
rq mkfs.ext4 "${lodevpart}"
echo "mounting to $mp"
mount "${lodevpart}" "$mp"
MP="$mp"
echo "==== before ===="
grep "${lodev##*/}" /proc/partitions
sfdisk --list --unit=S "$lodev"
errfile="${TEMP_D}/growpart.err"
growpart -v -v "$lodev" 1 2>"$errfile" || {
rc=$?
echo "failed [$rc]: growpart -v -v $lodev 1"
cat "$errfile" 1>&2
exit $rc
}
out=$(grep "FLOCK:.*releasing exclusive lock" "$errfile") || :
if [ -z "$out" ]; then
echo "ERROR: growpart stderr did not mention releasing lock"
exit 1
fi
echo === growpart stderr ===
cat "$errfile"
echo "==== after ===="
grep "${lodev##*/}" /proc/partitions
resize2fs "${lodevpart}"
echo "== df =="
df -h "$mp"
# vi: ts=4 noexpandtab