/
grow.sh
executable file
·80 lines (51 loc) · 1.14 KB
/
grow.sh
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
#!/bin/bash
set -e
IMG=$1
# Default to 100MB
MEGA=${2:-100}
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
NC='\033[0m' # No Color
function debug {
echo -e "${GREEN}GROW${NC}" "$@"
:
}
debug "Growing image ${IMG} by ${MEGA}MB"
debug "Creating dummy partition"
# Create a dummy partition to help defaults later
# The empty lines are significant
fdisk ${IMG} <<- EOF > /dev/null
n
w
EOF
debug "Growing the real file"
# Add zeros to the end of the img file
truncate -s +${MEGA}M ${IMG}
debug "Extending partition"
# Grow by deleting existing partition and creating a new one in the same place, but bigger.
# The empty lines are significant
fdisk ${IMG} <<- EOF > /dev/null 2> /dev/null
d
2
n
p
d
w
EOF
# Setup loop device for image
LOOP=$(losetup -Pf ${IMG} --show)
trap cleanup EXIT
function cleanup {
debug "Cleaning up..."
losetup -d ${LOOP}
}
debug "Checking filesystem"
# Check fs because we can
e2fsck -fy ${LOOP}p2 > /dev/null
debug "Growing filesystem"
# Grow the ext partition
resize2fs ${LOOP}p2 > /dev/null
debug "Checking filesystem (again)"
# Check fs again because we can
e2fsck -fy ${LOOP}p2 > /dev/null