-
Notifications
You must be signed in to change notification settings - Fork 43
/
grow_image
executable file
·66 lines (50 loc) · 1.3 KB
/
grow_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
#!/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 grows an image to ~4GiB
set -x
if [ $(id -u) -ne 0 ]; then
printf "Script must be run as root\n"
exit 1
fi
if [ ! "$1" ]; then
printf "Usage: ./grow_image IMAGEFILE\n"
exit 1
fi
IMAGEFILE="$1"
# Grow the image file
dd of="$IMAGEFILE" bs=1 seek=3700M count=0
# 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)
losetup -v $LOOP_DEV "$IMAGEFILE"
# 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
fdisk -cu $LOOP_DEV <<EOF
p
d
2
n
p
2
$PART_START
p
w
EOF
losetup -d $LOOP_DEV
# 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 -p $LOOP_DEV
# Unmount it all
losetup -d $LOOP_DEV
printf "Success! (hopefully)\n"