forked from sailfishos/initrd-helpers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfactory-reset-lvm
executable file
·178 lines (148 loc) · 4.59 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
#!/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"
NAME=$0
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
flash_script > $1/tmp/flash-firmwares
chmod 755 $1/tmp/flash-firmwares
chroot $1 /tmp/flash-firmwares
umount $1/tmp $1/dev $1/proc
}
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 "sailfish")
if test $? != "0"; then
echo "$NAME: Error: could not find sailfish partition" > /dev/kmsg
exit 1
fi
FIMAGE_DEV_PATH=$(find-mmc-bypartlabel "fimage")
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
MOUNT_POINT=$(mktemp -d)
# Clean up old LVM if it happens to exist
lvm vgchange -a n
lvm vgremove -y sailfish
lvm pvremove -y $PHYSDEV
# 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 -L "$HOME_SIZE"K --name home sailfish
# Create filesystems
mkfs.ext4 $ROOTDEV
mkfs.ext4 $HOMEDEV
# Start restoring Sailfish OS from the factory images
mount $ROOTDEV $MOUNT_POINT
mkdir -p $MOUNT_POINT/home
mount $HOMEDEV $MOUNT_POINT/home
TEMPMOUNT=$(mktemp -d)
# Find the biggest versioned Sailfish folder and use that
SAILFISH_FIMAGE=$(ls -d $FIMAGE_MOUNT/Sailfish* | tail -1)
if test -z $SAILFISH_FIMAGE; then
echo "$NAME: Error: Could not find a recovery image folder!" > /dev/kmsg
exit 1
fi
if ! test -f $SAILFISH_FIMAGE/sailfish_root.squashfs.img; then
echo "$NAME: Error: cannot find sailfish root recovery image!" > /dev/kmsg
exit 1
fi
if ! test -f $SAILFISH_FIMAGE/sailfish_home.squashfs.img; then
echo "$NAME: Error: cannot find sailfish home recovery image!" > /dev/kmsg
exit 1
fi
mount -t squashfs -o loop $SAILFISH_FIMAGE/sailfish_root.squashfs.img $TEMPMOUNT
cp -a $TEMPMOUNT/* $MOUNT_POINT
sync
umount $TEMPMOUNT
mount -t squashfs -o loop $SAILFISH_FIMAGE/sailfish_home.squashfs.img $TEMPMOUNT
cp -a $TEMPMOUNT/* $MOUNT_POINT/home
sync
umount $TEMPMOUNT
rmdir $TEMPMOUNT
# Flash firmwares from the resetted root
flash_firmwares $MOUNT_POINT
# Clean up
umount $MOUNT_POINT/home
umount $MOUNT_POINT
rmdir $MOUNT_POINT
umount $FIMAGE_MOUNT
rmdir $FIMAGE_MOUNT