-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathfactory-reset-lvm
executable file
·207 lines (173 loc) · 5.37 KB
/
factory-reset-lvm
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#!/bin/sh
#
# Factory reset tool for LVM based Sailfish OS filesystems.
#
# Copyright (C) 2015 Jolla Ltd.
# Contact: Kalle Jokiniemi <[email protected]>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; version 2
# of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
# Parameters:
# $1 -> Root size in MB
# $2 -> Reserve space for later use in MB
ROOTDEV="/dev/sailfish/root"
HOMEDEV="/dev/sailfish/home"
ROOTIMG="root.img"
HOMEIMG="home.img"
NAME=$0
PHYSDEV_PART_LABEL="sailfish"
FIMAGE_PART_LABEL="fimage"
# From https://github.com/mer-hybris/hybris-initrd
. /etc/sysconfig/partitions
flash_script()
{
cat << 'EOF'
#!/bin/sh
for SCRIPT in /var/lib/platform-updates/* ; do
if [ -x $SCRIPT ]; then
echo "$SCRIPT ... "
$SCRIPT && echo "OK" || echo "FAILED"
fi
done
EOF
}
flash_firmwares()
{
if test -z $1; then
echo "$NAME: No mount point given" > /dev/kmsg
exit 1
fi
mount -t tmpfs tmpfs $1/tmp
mount -t devtmpfs devtmpfs $1/dev
mount -t proc proc $1/proc
mount -t sysfs sys $1/sys
flash_script > $1/tmp/flash-firmwares
chmod 755 $1/tmp/flash-firmwares
chroot $1 /tmp/flash-firmwares
umount $1/tmp $1/dev $1/proc $1/sys
}
if test -z $1 || ! test $1 -ge 0; then
echo "$NAME: Please pass root size in MB as parameter!" > /dev/kmsg
exit 1
fi
if test -z $2 || ! test $2 -ge 0; then
echo "$NAME: Please pass reserve size in MB as parameter!" > /dev/kmsg
exit 1
fi
ROOT_SIZE=$1
RESERVE_KB=$(expr $2 \* 1024)
echo "$NAME: Starting factory reset.." > /dev/kmsg
PHYSDEV=$(find-mmc-bypartlabel "$PHYSDEV_PART_LABEL")
if test $? != "0"; then
echo "$NAME: Error: could not find sailfish partition" > /dev/kmsg
exit 1
fi
FIMAGE_DEV_PATH=$(find-mmc-bypartlabel "$FIMAGE_PART_LABEL")
if test $? != "0"; then
echo "$NAME: Error: could not find fimage partition" > /dev/kmsg
exit 1
fi
echo "$NAME: fimage partition in $FIMAGE_DEV_PATH" > /dev/kmsg
FIMAGE_MOUNT=$(mktemp -d)
if ! mount "$FIMAGE_DEV_PATH" "$FIMAGE_MOUNT"; then
echo "$NAME: Error, could not mount fimage for factory reset!" > /dev/kmsg
rmdir $FIMAGE_MOUNT
exit 1
fi
# Find the highest versioned image directory and use that
# Image directories must have version number separated by dashes
SAILFISH_FIMAGE=$(ls -d1 $FIMAGE_MOUNT/*/ | grep -E '^'"$FIMAGE_MOUNT"'/.+-[0-9]+\.[0-9].*-.+/$' | tail -1)
WORKDIR=$(pwd)
if test -z $SAILFISH_FIMAGE; then
echo "$NAME: Error: Could not find a recovery image folder!" > /dev/kmsg
exit 1
fi
# Check that the factory images are ok to use and detect compression method.
cd $SAILFISH_FIMAGE
if test -f $ROOTIMG.lzo && test -f $HOMEIMG.lzo; then
ROOTIMG="$ROOTIMG.lzo"
HOMEIMG="$HOMEIMG.lzo"
DECOMPRESS_CMD="lzopcat"
elif test -f $ROOTIMG.gz && test -f $HOMEIMG.gz; then
ROOTIMG="$ROOTIMG.gz"
HOMEIMG="$HOMEIMG.gz"
DECOMPRESS_CMD="pigz -d -c"
elif test -f $ROOTIMG.bz2 && test -f $HOMEIMG.bz2; then
ROOTIMG="$ROOTIMG.bz2"
HOMEIMG="$HOMEIMG.bz2"
DECOMPRESS_CMD="bzip2 -d -c"
elif test -f $ROOTIMG.xz && test -f $HOMEIMG.xz; then
ROOTIMG="$ROOTIMG.xz"
HOMEIMG="$HOMEIMG.xz"
DECOMPRESS_CMD="xz -d -c"
else
echo "$NAME: Error: cannot find sailfish recovery image!" > /dev/kmsg
exit 1
fi
if ! md5sum -c $ROOTIMG.md5 > /dev/kmsg; then
echo "$NAME: Error: root recovery image corrupted!" > /dev/kmsg
exit 1
fi
if ! md5sum -c $HOMEIMG.md5 > /dev/kmsg; then
echo "$NAME: Error: home recovery image corrupted!" > /dev/kmsg
exit 1
fi
cd $WORKDIR
# Clean up old LVM if it happens to exist
lvm vgchange -a n
lvm vgremove -y sailfish
lvm pvremove -y $PHYSDEV
if test "$SAILFISHOS_WIPE_PARTITIONS" = "1"; then
dd if=/dev/zero of=$PHYSDEV bs=1M
fi
# Create the LVM setup
if ! lvm pvcreate $PHYSDEV; then
echo "$NAME: Error, could create LVM physical device for $PHYSDEV" > /dev/kmsg
exit 1
fi
# If the PV exists, creating VG should never fail
lvm vgcreate sailfish $PHYSDEV
# Checking for errors to maybe catch wrong root size parameter
if ! lvm lvcreate -L "$ROOT_SIZE"M --name root sailfish; then
echo "$NAME: Error, could create root LV" > /dev/kmsg
exit 1
fi
# Calculate home size
FREE_EXTENTS=$(lvm vgdisplay sailfish -c | cut -d ":" -f 16)
EXTENT_SIZE=$(lvm vgdisplay sailfish -c | cut -d ":" -f 13)
FREE_KB=$(expr $FREE_EXTENTS \* $EXTENT_SIZE)
HOME_SIZE=$(expr $FREE_KB - $RESERVE_KB)
# Check for too big reserve (not enough room left for home) case (1024kB * 64 = 64MB)
if test $HOME_SIZE -le 65536; then
echo "$NAME: Error: too big reserve, not enough space for home" > /dev/kmsg
exit 1
fi
# Create home LV
lvm lvcreate -y -L "$HOME_SIZE"K --name home sailfish
# Start restoring Sailfish OS from the factory images
$DECOMPRESS_CMD $SAILFISH_FIMAGE/$ROOTIMG > $ROOTDEV
$DECOMPRESS_CMD $SAILFISH_FIMAGE/$HOMEIMG > $HOMEDEV
sync
resize2fs -f $ROOTDEV
resize2fs -f $HOMEDEV
sync
# Flash firmwares from the resetted root
TEMPMOUNT=$(mktemp -d)
mount $ROOTDEV $TEMPMOUNT
flash_firmwares $TEMPMOUNT
# Clean up
umount $TEMPMOUNT
rmdir $TEMPMOUNT
umount $FIMAGE_MOUNT
rmdir $FIMAGE_MOUNT