-
Notifications
You must be signed in to change notification settings - Fork 1
/
expand_filesystem.sh
executable file
·94 lines (81 loc) · 2.46 KB
/
expand_filesystem.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/bin/sh
#
# Raspberry Pi Setup System - Expand FileSystem Code
# This code was borrowed from the raspi-config tool published under the MIT license
# Big thanks to the team. It was modified to remove whiptail which I am not using.
#
if ! [ -h /dev/root ]; then
echo "/dev/root does not exist or is not a symlink. Don't know how to expand"
return 0
fi
ROOT_PART=$(sudo readlink /dev/root)
PART_NUM=${ROOT_PART#mmcblk0p}
if [ "$PART_NUM" = "$ROOT_PART" ]; then
echo "/dev/root is not an SD card. Don't know how to expand"
echo "PART NUM = $PART_NUM"
echo "ROOT PART = $ROOT_PART"
return 0
fi
# NOTE: the NOOBS partition layout confuses parted. For now, let's only
# agree to work with a sufficiently simple partition layout
if [ "$PART_NUM" -ne 2 ]; then
echo "Your partition layout is not currently supported by this tool. You are probably using NOOBS, in which case your root filesystem is already expanded anyway."
return 0
fi
LAST_PART_NUM=$(sudo parted /dev/mmcblk0 -ms unit s p | tail -n 1 | cut -f 1 -d:)
if [ "$LAST_PART_NUM" != "$PART_NUM" ]; then
echo "/dev/root is not the last partition. Don't know how to expand"
echo "Last Part Num = $LAST_PART_NUM"
echo "Part Num = $PART_NUM"
return 0
fi
# Get the starting offset of the root partition
PART_START=$(sudo parted /dev/mmcblk0 -ms unit s p | grep "^${PART_NUM}" | cut -f 2 -d:)
[ "$PART_START" ] || return 1
# Return value will likely be error for fdisk as it fails to reload the
# partition table because the root fs is mounted
sudo fdisk /dev/mmcblk0 <<EOF
p
d
$PART_NUM
n
p
$PART_NUM
$PART_START
p
w
EOF
# ASK_TO_REBOOT=1 # Removing this line because the parent script will reboot anyway
# now set up an init.d script
sudo cat <<\EOF > ./resize2fs_once &&
#!/bin/sh
### BEGIN INIT INFO
# Provides: resize2fs_once
# Required-Start:
# Required-Stop:
# Default-Start: 2 3 4 5 S
# Default-Stop:
# Short-Description: Resize the root filesystem to fill partition
# Description:
### END INIT INFO
. /lib/lsb/init-functions
case "$1" in
start)
log_daemon_msg "Starting resize2fs_once" &&
resize2fs /dev/root &&
rm /etc/init.d/resize2fs_once &&
update-rc.d resize2fs_once remove &&
log_end_msg $?
;;
*)
echo "Usage: $0 start" >&2
exit 3
;;
esac
EOF
sudo mv ./resize2fs_once /etc/init.d
sudo chmod +x /etc/init.d/resize2fs_once &&
sudo update-rc.d resize2fs_once defaults &&
if [ "$INTERACTIVE" = True ]; then
echo "Root partition has been resized.\nThe filesystem will be enlarged upon the next reboot"
fi