-
Notifications
You must be signed in to change notification settings - Fork 43
/
shrink_image
executable file
·74 lines (55 loc) · 1.69 KB
/
shrink_image
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
#!/bin/sh
# Part of spindle http://asbradbury.org/projects/spindle
#
# See LICENSE file for copyright and license details
# TODO: this script is very rough and ready. Currently shrinks ext4 image to
# minimum size. Should really be refactored and merged with grow_image
set -x
if [ $(id -u) -ne 0 ]; then
printf "Script must be run as root\n"
exit 1
fi
if [ ! "$1" ]; then
printf "Usage: ./shrink_image IMAGEFILE\n"
exit 1
fi
IMAGEFILE="$1"
# Partition resizing code same as in http://github.com/asb/raspi-config
PART_START=$(parted "$IMAGEFILE" -ms unit s p | grep "^2" | cut -f 2 -d:)
if [ ! "$PART_START" ]; then
printf "Failed to extract root partition offset\n"
fi
# Just use losetup to create a loopback device. fdisk will operate on files,
# but it complains about the number of cylinders not being set
LOOP_DEV=$(losetup -f)
# We know the offset anyway so let's just use losetup directly rather than
# bothering with kpartx
PART_START_BYTES=$((${PART_START%s}*512))
losetup --offset "$PART_START_BYTES" -v $LOOP_DEV "$IMAGEFILE"
# Sanity check
file -s $LOOP_DEV
# Run resize2fs
e2fsck -f $LOOP_DEV
resize2fs -M $LOOP_DEV
EXT4_BLCKCNT=$(sudo tune2fs -l /dev/loop0 | grep "^Block count" | cut -d ":" -f 2 | tr -d ' ')
EXT4_BLKSIZE=$(sudo tune2fs -l /dev/loop0 | grep "^Block size" | cut -d ":" -f 2 | tr -d ' ')
EXT4_FS_BYTES=$(($EXT4_BLCKCNT * $EXT4_BLKSIZE))
# Unmount it all
losetup -d $LOOP_DEV
NEW_IMG_BYTES=$(($EXT4_FS_BYTES + $PART_START_BYTES + 4194304))
# Shrink the image file
dd if=/dev/null of="$IMAGEFILE" bs=$NEW_IMG_BYTES seek=1
losetup -v $LOOP_DEV "$IMAGEFILE"
fdisk -cu $LOOP_DEV <<EOF
p
d
2
n
p
2
$PART_START
p
w
EOF
losetup -d $LOOP_DEV
printf "Success! (hopefully)\n"