-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathfs_resize
executable file
·175 lines (151 loc) · 4.77 KB
/
fs_resize
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#!/bin/bash
# ******************************************
# Resize Linux ext4 partition to fill sdcard
# ******************************************
if [ "$(id -u)" != "0" ]; then
echo "Script must be run as root !"
exit 0
fi
_REL=`lsb_release -sc`
_rootpart=`mount | grep "on / " | awk '{print $1}'`
if [ "${_rootpart}" = "/dev/mmcblk0p2" ]; then
rootdrv="mmcblk0p2"
sdcard="/dev/mmcblk0"
elif [ "${_rootpart}" = "/dev/mmcblk1p2" ]; then
rootdrv="mmcblk1p2"
sdcard="/dev/mmcblk1"
else
echo "Root fs mount partition not found!"
exit 1
fi
echo ""
fdisk -l $sdcard | grep $sdcard
echo ""
_btrfs=`mount | grep -o btrfs`
sdcard_part=`fdisk -l $sdcard | grep $rootdrv | awk '{print $1}'`
sdcard_sect=`fdisk -l $sdcard | grep "Disk $sdcard" | awk '{print $7}'`
if [ "${sdcard_sect}" = "" ]; then
sdcard_sect=`fdisk -l $sdcard | grep total | awk '{print $8}'`
fi
sdcard_end=$(expr $sdcard_sect - 1024)
part_start=`fdisk -l $sdcard | grep $rootdrv | awk '{print $2}'`
part_end=`fdisk -l $sdcard | grep $rootdrv | awk '{print $3}'`
echo " Max block: $sdcard_end"
echo " Part end: $part_end"
echo " Part start: $part_start"
if [ ! "${_btrfs}" = "" ]; then
echo " btrfs part: yes"
_resize="btrfs filesystem resize max /"
else
_resize="resize2fs ${sdcard_part}"
fi
echo ""
if [ $part_end -ge $sdcard_end ]; then
echo "Partition allready maximum size !"
rm /usr/local/bin/fs_resize_warning > /dev/null 2>&1
exit 0
fi
echo -n "WARNING: Do you want to resize \"$sdcard_part\" (y/N)? "
read -n 1 ANSWER
if [ ! "${ANSWER}" = "y" ] ; then
echo ""
echo "Canceled.."
exit 0
fi
echo ""
# RESIZE PARTITION
echo -e "p\nd\n2\nn\np\n2\n$part_start\n$sdcard_end\nw" | fdisk ${sdcard} > /dev/null 2>&1
#if [ $? -ne 0 ]; then
# echo "ERROR resizing partition!"
# exit 1
#fi
echo "PARTITION RESIZED."
cat > /tmp/rc.local << _EOF_
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
echo 0 > /proc/sys/kernel/hung_task_timeout_secs
dmesg -n 1
_EOF_
cat > /usr/local/bin/resize_fs << _EOF_
#!/bin/bash
$_resize
if [ \$? -eq 0 ]; then
rm /usr/local/bin/fs_resize_warning
rm /usr/local/bin/resize_fs
sleep 2
if [ -d /etc/local.d ]; then
rm /etc/local.d/opi.start
mv /etc/local.d/opi.start.orig /etc/local.d/opi.start
elif [ -d /etc/rc.d ]; then
if [ -f /etc/rc.d/after.local ]; then
rm /etc/rc.d/after.local
mv /etc/rc.d/after.local.orig /etc/rc.d/after.local
else
rm /etc/rc.d/rc.local
mv /etc/rc.d/rc.local.orig /etc/rc.d/rc.local
fi
else
rm /etc/rc.local
mv /etc/rc.local.orig /etc/rc.local
fi
fi
_EOF_
chmod +x /usr/local/bin/resize_fs > /dev/null 2>&1
if [ -d /etc/local.d ]; then
if [ ! -f /etc/local.d/opi.start ]; then
cp /tmp/rc.local /etc/local.d/opi.start.orig
echo "exit 0" >> /etc/local.d/opi.start.orig
chmod +x /etc/local.d/opi.start.orig > /dev/null 2>&1
fi
echo "/usr/local/bin/resize_fs &&" >> /tmp/rc.local
echo "exit 0" >> /tmp/rc.local
mv /tmp/rc.local /etc/local.d/opi.start
chmod +x /etc/local.d/opi.start > /dev/null 2>&1
elif [ -d /etc/rc.d ]; then
if [ -f /etc/rc.d/after.local ]; then
mv /etc/rc.d/after.local /etc/rc.d/after.local.orig
echo "/usr/local/bin/resize_fs &&" >> /tmp/rc.local
echo "exit 0" >> /tmp/rc.local
mv /tmp/rc.local /etc/rc.d/after.local
chmod +x /etc/rc.d/after.local > /dev/null 2>&1
else
if [ -f /etc/rc.d/rc.local ]; then
mv /etc/rc.d/rc.local /etc/rc.d/rc.local.orig
else
cp /tmp/rc.local /etc/rc.d/rc.local.orig
echo "exit 0" >> /etc/rc.d/rc.local.orig
chmod +x /etc/rc.d/rc.local.orig > /dev/null 2>&1
fi
echo "/usr/local/bin/resize_fs &&" >> /tmp/rc.local
echo "exit 0" >> /tmp/rc.local
mv /tmp/rc.local /etc/rc.d/rc.local
chmod +x /etc/rc.d/rc.local > /dev/null 2>&1
fi
else
if [ -f /etc/rc.local ]; then
mv /etc/rc.local /etc/rc.local.orig
else
cp /tmp/rc.local /etc/rc.local.orig
echo "exit 0" >> /etc/rc.local.orig
chmod +x /etc/rc.local.orig > /dev/null 2>&1
fi
echo "/usr/local/bin/resize_fs &&" >> /tmp/rc.local
echo "exit 0" >> /tmp/rc.local
mv /tmp/rc.local /etc/rc.local
chmod +x /etc/rc.local > /dev/null 2>&1
fi
REBOOT=1
echo "*********************************************"
echo "Rootfs Extended. Please REBOOT to take effect"
echo "*********************************************"
echo ""