@@ -5,6 +5,7 @@ set -euo pipefail
55# Clean up and shrink image
66# ###
77image=" $1 "
8+ shrink=" $2 "
89
910if [[ -e " ${rootdir} /etc/resolv.conf.bak" ]]; then
1011 mv " ${rootdir} /etc/resolv.conf.bak" " ${rootdir} /etc/resolv.conf"
@@ -15,37 +16,39 @@ echo "Zero filling empty space"
1516
1617umount --recursive " ${rootdir} "
1718
18- echo " Resizing root filesystem to minimal size."
19- e2fsck -v -f -p -E discard " ${rootdev} "
20- resize2fs -M " ${rootdev} "
21- rootfs_blocksize=$( tune2fs -l ${rootdev} | grep " ^Block size" | awk ' {print $NF}' )
22- rootfs_blockcount=$( tune2fs -l ${rootdev} | grep " ^Block count" | awk ' {print $NF}' )
23-
24- echo " Resizing rootfs partition."
25- rootfs_partstart=$( parted -m --script " ${loopdev} " unit B print | grep " ^${rootpartition} :" | awk -F " :" ' {print $2}' | tr -d ' B' )
26- rootfs_partsize=$(( ${rootfs_blockcount} * ${rootfs_blocksize} ))
27- rootfs_partend=$(( ${rootfs_partstart} + ${rootfs_partsize} - 1 ))
28- rootfs_partoldend=$( parted -m --script " ${loopdev} " unit B print | grep " ^${rootpartition} :" | awk -F " :" ' {print $3}' | tr -d ' B' )
29- if [ " $rootfs_partoldend " -gt " $rootfs_partend " ]; then
30- echo y | parted ---pretend-input-tty " ${loopdev} " unit B resizepart " ${rootpartition} " " ${rootfs_partend} "
31- else
32- echo " Rootfs partition not resized as it was not shrunk"
33- fi
19+ if [[ ${shrink,,} = y* ]]; then
20+ echo " Resizing root filesystem to minimal size."
21+ e2fsck -v -f -p -E discard " ${rootdev} "
22+ resize2fs -M " ${rootdev} "
23+ rootfs_blocksize=$( tune2fs -l ${rootdev} | grep " ^Block size" | awk ' {print $NF}' )
24+ rootfs_blockcount=$( tune2fs -l ${rootdev} | grep " ^Block count" | awk ' {print $NF}' )
25+
26+ echo " Resizing rootfs partition."
27+ rootfs_partstart=$( parted -m --script " ${loopdev} " unit B print | grep " ^${rootpartition} :" | awk -F " :" ' {print $2}' | tr -d ' B' )
28+ rootfs_partsize=$(( ${rootfs_blockcount} * ${rootfs_blocksize} ))
29+ rootfs_partend=$(( ${rootfs_partstart} + ${rootfs_partsize} - 1 ))
30+ rootfs_partoldend=$( parted -m --script " ${loopdev} " unit B print | grep " ^${rootpartition} :" | awk -F " :" ' {print $3}' | tr -d ' B' )
31+ if [ " $rootfs_partoldend " -gt " $rootfs_partend " ]; then
32+ echo y | parted ---pretend-input-tty " ${loopdev} " unit B resizepart " ${rootpartition} " " ${rootfs_partend} "
33+ else
34+ echo " Rootfs partition not resized as it was not shrunk"
35+ fi
3436
35- free_space=$( parted -m --script " ${loopdev} " unit B print free | tail -1)
36- if [[ " ${free_space} " =~ " free" ]]; then
37- initial_image_size=$( stat -L --printf=" %s" " ${image} " )
38- image_size=$( echo " ${free_space} " | awk -F " :" ' {print $2}' | tr -d ' B' )
39- if [[ " ${part_type} " == " gpt" ]]; then
40- # for GPT partition table, leave space at the end for the secondary GPT
41- # it requires 33 sectors, which is 16896 bytes
42- image_size=$(( image_size + 16896 ))
43- fi
44- echo " Shrinking image from ${initial_image_size} to ${image_size} bytes."
45- truncate -s " ${image_size} " " ${image} "
46- if [[ " ${part_type} " == " gpt" ]]; then
47- # use sgdisk to fix the secondary GPT after truncation
48- sgdisk -e " ${image} "
37+ free_space=$( parted -m --script " ${loopdev} " unit B print free | tail -1)
38+ if [[ " ${free_space} " =~ " free" ]]; then
39+ initial_image_size=$( stat -L --printf=" %s" " ${image} " )
40+ image_size=$( echo " ${free_space} " | awk -F " :" ' {print $2}' | tr -d ' B' )
41+ if [[ " ${part_type} " == " gpt" ]]; then
42+ # for GPT partition table, leave space at the end for the secondary GPT
43+ # it requires 33 sectors, which is 16896 bytes
44+ image_size=$(( image_size + 16896 ))
45+ fi
46+ echo " Shrinking image from ${initial_image_size} to ${image_size} bytes."
47+ truncate -s " ${image_size} " " ${image} "
48+ if [[ " ${part_type} " == " gpt" ]]; then
49+ # use sgdisk to fix the secondary GPT after truncation
50+ sgdisk -e " ${image} "
51+ fi
4952 fi
5053fi
5154
0 commit comments