-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathimage-shrink
177 lines (172 loc) · 4.05 KB
/
image-shrink
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
#!/bin/bash
mkloop()
{
LOOP="$(losetup -f --show -P "${IMGFILE}")"
if [ $? -ne 0 ]; then
errexit "Unable to create loop device"
fi
}
rmloop()
{
losetup -d "${LOOP}"
}
fsckerr()
{
rmloop
errexit "Filesystem appears corrupted "$1" resize2fs"
}
errexit()
{
echo ""
echo "$1"
echo ""
exit 1
}
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"
if [ "${IMGFILE}" = "" ]; then
errexit "Usage: $0 imagefile [Additional MB]"
fi
if [ ! -f "${IMGFILE}" ] || [ ! -s "${IMGFILE}" ]; then
errexit "${IMGFILE} is missing or empty"
fi
mkloop
FS_TYPE=$(blkid "${LOOP}p2" | sed -n 's|^.*TYPE="\(\S\+\)".*|\1|p')
rmloop
if [ "${FS_TYPE}" = "f2fs" ]; then
errexit "Cannot shrink F2FS filesystem"
fi
answer="$2"
if [[ ! "${answer}" =~ ^[0-9]+$ ]]; then
echo ""
while :
do
read -r -e -i "${answer}" -p "Additional MB? " answer
if [[ "${answer}" =~ ^[0-9]*$ ]]; then
break
fi
done
fi
while [ "${answer:0:1}" = "0" ]; do
answer="${answer:1}"
done
if [ ${#answer} -eq 0 ]; then
answer=0
fi
ADDMB=${answer}
echo ""
echo -n "Shrink ${IMGFILE} [Additional MB = ${ADDMB}] (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 ""
INFO="$(sfdisk -d "${IMGFILE}")"
BOOTBEG=$(sed -n "s|^${IMGFILE}1\s\+:.*start=\s*\([0-9]\+\).*$|\1|p" <<< "${INFO}")
BOOTEND=$((${BOOTBEG} + $(sed -n "s|^${IMGFILE}1\s\+:.*size=\s*\([0-9]\+\).*$|\1|p" <<< "${INFO}") - 1))
ROOTBEG=$(sed -n "s|^${IMGFILE}2\s\+:.*start=\s*\([0-9]\+\).*$|\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}")"
PTUUID="$(sed -n "s|^label-id: \(\S\+\).*$|\1|p" <<< "${INFO}")"
PTTYPE="$(sed -n "s|^label: \(\S\+\).*$|\1|p" <<< "${INFO}")"
if [[ "${PTTYPE}" != "dos" && "${PTTYPE}" != "gpt" ]]; then
errexit "Unsupported partition table type: ${PTTYPE}"
fi
mkloop
e2fsck -f -n "${LOOP}p2"
if [ $? -ne 0 ]; then
fsckerr "before"
fi
echo ""
resize2fs -f -M "${LOOP}p2"
resize2fs -f -M "${LOOP}p2"
resize2fs -f -M "${LOOP}p2"
e2fsck -f -n "${LOOP}p2"
if [ $? -ne 0 ]; then
fsckerr "after"
fi
INFO="$(tune2fs -l "${LOOP}p2" 2>/dev/null)"
rmloop
NEWSIZE=$(sed -n 's|^Block count:\s*\(.*\)|\1|p' <<< "${INFO}")
BLKSIZE=$(sed -n 's|^Block size:\s*\(.*\)|\1|p' <<< "${INFO}")
NEWEND=$((${ROOTBEG} + (${NEWSIZE} * (${BLKSIZE} / 512)) + ((${ADDMB} * 1024 * 1024) / 512) - 1))
if [ "${PTTYPE}" = "gpt" ]; then
((NEWEND += 33))
fi
truncate -s $(((${NEWEND} + 1) * 512)) "${IMGFILE}"
if [ "${PTTYPE}" = "dos" ]; then
sfdisk --delete "${IMGFILE}" 2 > /dev/null
echo "${ROOTBEG},+" | sfdisk -N2 "${IMGFILE}" &> /dev/null
else
sgdisk -Z "${IMGFILE}" &> /dev/null
sgdisk -n 1:${BOOTBEG}:${BOOTEND} "${IMGFILE}" &> /dev/null
sgdisk -t 1:0700 "${IMGFILE}" > /dev/null
sgdisk -n 2:${ROOTBEG}:0 "${IMGFILE}" &> /dev/null
sgdisk -t 2:8300 "${IMGFILE}" > /dev/null
sgdisk -u 1:"${PARTUUID_1}" "${IMGFILE}" > /dev/null
sgdisk -u 2:"${PARTUUID_2}" "${IMGFILE}" > /dev/null
sgdisk -U "${PTUUID}" "${IMGFILE}" > /dev/null
gdisk "${IMGFILE}" <<EOF > /dev/null
r
h
1
n
0c
n
n
w
y
EOF
fi
if [ ${ADDMB} -ne 0 ]; then
echo ""
mkloop
e2fsck -f -n "${LOOP}p2"
if [ $? -ne 0 ]; then
fsckerr "before"
fi
echo ""
resize2fs -f "${LOOP}p2"
e2fsck -f -n "${LOOP}p2"
if [ $? -ne 0 ]; then
fsckerr "after"
fi
rmloop
fi
echo ""