-
Notifications
You must be signed in to change notification settings - Fork 5
/
grow-rootfs
executable file
·85 lines (77 loc) · 2.32 KB
/
grow-rootfs
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
#!/bin/bash -e
set -e
# Grow the last partition to the last of the disk.
ROOTDEV=
ROOTPART=
ROOTTYPE=
PARTNUM=
echo "[+] Preparing to grow..."
if [ "$(mount | grep 'on / type' | grep nfs)" ]; then
echo "[-] No joking around, please. You are using a NFS."
exit 1
fi
if [ "$1" == "yes-please" ]; then
echo "[+] You said yes, so we just go on."
elif [ -t 0 -a -t 1 ]; then
read -p "Are you sure (y/N)? " ANS
if ! echo "$ANS" | grep -q '[yY]' ; then
echo "[+] Aborting."
exit 0
fi
echo "[+] Okay, you choose to take the risk."
else
echo "[+] You are running this with scripts. You have taken the risk."
fi
if [ -e "/.resize-partition" ]; then
# Resize partition
source "/.resize-partition"
case "$ROOTTYPE" in
"ext4")
resize2fs /dev/$ROOTPART
;;
"btrfs")
btrfs filesystem resize max /
;;
*)
;;
esac
rm /.resize-partition
exit 0
else
ROOTPART=$(lsblk -lno NAME,MOUNTPOINT | sed -E 's/\s+/,/g' | grep ',/$' | cut -d',' -f1)
ROOTDEV="/dev/$(lsblk -lno PKNAME /dev/$ROOTPART)"
ROOTTYPE=$(lsblk -lno FSTYPE /dev/$ROOTPART)
# Get current partition number
# PARTNUM=$(lsblk -lno "MAJ:MIN" /dev/$ROOTPART | cut -d':' -f2) - It is not reliable
# Use the old school method
PARTNUMS=($(echo "$ROOTPART" | grep -oE '[0-9]+'))
PARTNUM=${PARTNUMS[-1]}
echo "[+] Root partition: $ROOTPART"
echo "[+] Device the root partition is in: $ROOTDEV"
echo "[+] Root filesystem: $ROOTTYPE"
echo "[+] Number of the root partition: $PARTNUM"
if [ "$DEBUG" ]; then
echo "[D] That's all we need to know."
exit 0
fi
if [ "$ROOTDEV" -a "$ROOTPART" -a "$ROOTTYPE" -a "$PARTNUM" ]; then
# Get partitions
PARTS=($(lsblk -o NAME -ln $ROOTDEV))
echo "[+] Last partition of this disk: ${PARTS[-1]}"
if [ "$ROOTPART" != "${PARTS[-1]}" ]; then
echo "[-] Jesus, don't even think about it; Your partition is not the last partition!"
exit 1
fi
echo "[+] Okay, your root partition is the last partition. Proceeding."
# Resize partition
# Call sfdisk to grow this partition, gently
# It will not touch anything else.
echo ', +' | sfdisk --force -N $PARTNUM $ROOTDEV
# Okay, we are ready to grow
touch "/.resize-partition"
echo "ROOTDEV=$ROOTDEV" >> /.resize-partition
echo "ROOTPART=$ROOTPART" >> /.resize-partition
echo "ROOTTYPE=$ROOTTYPE" >> /.resize-partition
echo "PARTNUM=$PARTNUM" >> /.resize-partition
fi
fi