-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathsdpart_imagefile
executable file
·149 lines (139 loc) · 4.94 KB
/
sdpart_imagefile
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
#!/bin/bash
###################################################################
# All Emoncms code is released under the GNU Affero General Public License.
# See COPYRIGHT.txt and LICENSE.txt.
# ---------------------------------------------------------------------
# Emoncms - open source energy visualisation
# Part of the OpenEnergyMonitor project: http://openenergymonitor.org
###################################################################
# This script's purpose is to prepare a SD Card after a emoncms imagefile
# has been written to a SD card.
# It will calculate and expand the /data partition to the preferred size,
# and then also expand the partition filesystem.
# The whole process takes about 20 minutes to complete, depending upon the
# size of the SD card, and its important that the script is not interupted.
# Upon completion, your Raspberry Pi will poweroff.
#
# Author: Paul Reed
###################################################################
if [ $(id -u) -ne 0 ]; then
printf "Script must be run as root. Try 'sudo ./sdpart'\n"
exit 1
fi
data_dir="/var/opt/emoncms"
if [ -d "/home/pi" ]; then
if [ -d "/home/pi/data" ]; then
data_dir="/home/pi/data"
elif [ -d "/var/opt/emoncms" ]; then
data_dir="/var/opt/emoncms"
else
echo This does not appear to be an emoncms imagefile installation
echo the script is aborting.
exit 1
fi
fi
echo "data_dir: $data_dir"
###################################################################
if [ -f /usr/bin/rpi-rw ]; then
rpi-rw
fi
echo
PART_END="$(($(blockdev --getsz /dev/mmcblk0)))"
DATA_START="$(parted /dev/mmcblk0 -ms unit s p | grep "^3" | cut -f2 -d: | sed 's/[^0-9]*//g')"
[ "$DATA_START" ] || exit 1
# Create smaller data partition for 4Gb cards
if [ $PART_END -lt 10485760 ]
then
# Creates a 300Mb data partition and 10Mb buffer
DATA_END="$(((PART_END)-20480))"
else
# Creates a 1Gb data partition and 50Mb buffer
DATA_END="$(((PART_END)-102400))"
fi
###################################################################
# Display current SD card data
echo ======================================================
echo
echo Current Disk Info:
fdisk -l /dev/mmcblk0
echo
echo ======================================================
echo
echo Proposed changes to be made:
echo " SD card total disk size = "$(python -c "print $PART_END * 512.0 / 1073741824")"Gb"
echo " Data Partition size = "$(python -c "print ($DATA_END - $DATA_START + 1) * 512.0 / 1073741824")"Gb"
echo
read -r -p "Are you sure you want to proceed? [Y/n] " response
if [[ $response =~ ^([nN][oO]|[nN])$ ]]
then
exit 1
else
if [ -f /usr/bin/rpi-rw ]; then
rpi-rw
fi
###################################################################
# Create a systemd unit file to extend filesystem after a reboot
cat <<\EOF > /lib/systemd/system/resize2fs_once.service &&
[Unit]
Description=Resize2fs_once Service
[Service]
Type=idle
ExecStart=/home/pi/resize2fs_once.sh
StandardOutput=null
TimeoutStartSec=infinity
[Install]
WantedBy=multi-user.target
Alias=resize2fs_once.service
EOF
###################################################################
# Enable the unit file so it runs after a reboot
systemctl enable resize2fs_once.service
#
# Fdisk will now make the changes..
printf "d\n3\nn\np\n3\n$DATA_START\n$DATA_END\np\nw\n" | fdisk /dev/mmcblk0
echo This error message can however be disregarded, because your system
echo is about to be rebooted, and the new partition table will then be
echo read by your operating system.
echo
###################################################################
echo Writing the resize2fs_once script
cat <<\EOF > /home/pi/resize2fs_once.sh &&
#!/bin/sh
log_daemon_msg "Starting resize2fs_once script"
umount /dev/mmcblk0p3
e2fsck -yf /dev/mmcblk0p3
resize2fs -f /dev/mmcblk0p3
if [ -f /usr/bin/rpi-rw ]; then
rpi-rw
fi
rm /lib/systemd/system/resize2fs_once.service
systemctl disable resize2fs_once.service
rm /home/pi/resize2fs_once.sh
sed -i "s~^#\(.*$data_dir\)~\1~" /etc/fstab
/bin/sync
/sbin/shutdown -h now
EOF
###################################################################
# Make the resize2fs_once script executable
chmod +x /home/pi/resize2fs_once.sh
# Ensure that the /data partition is unmounted after a reboot
sed -i "s~^[^#]*$data_dir~#&~" /etc/fstab
echo
echo
echo ======================================================================
echo
echo So far, so good... in 20s your system will reboot
echo and resize the $data_dir filesystem to fill the resized partition.
echo **THIS OPERATION WILL TAKE UP TO 20 MINUTES**
echo When completed the Pi will poweroff and close down.
echo PLEASE DO NOT UNPLUG THE POWER FOR AT LEAST 30min
echo because it will likely result in an unusable filesystem.
echo
echo ======================================================================
sleep 20
/bin/sync
/sbin/reboot
fi
###################################################################
# END OF SCRIPT
###################################################################