-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathfactory-reset-external
executable file
·151 lines (127 loc) · 3.53 KB
/
factory-reset-external
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
#!/bin/sh
#
# External storage factory reset for Sailfish OS.
#
# Copyright (C) 2017 Jolla Ltd.
#
# 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:
# $@ -> Device paths of partitions to reformat.
format_script()
{
cat << 'EOF'
#!/bin/sh
function log
{
echo "$0: $1" > /dev/kmsg
}
for device in $@; do
unset TYPE
unset LABEL
unset UUID
if [ ! -e "$device" ]; then
continue
fi
partitionname=$( basename $device )
partitionblock=$( readlink -f /sys/class/block/$partitionname )
if [ ! -e "$partitionblock" ]; then
continue
fi
deviceblock=$( dirname "$partitionblock" )
if ! grep -Fxq SD $deviceblock/device/type; then
log "$device is not an SD-card! Skipping"
continue
fi
eval "$(/sbin/blkid -c /dev/null -o export $device)"
case "$TYPE" in
vfat)
format_command=/sbin/mkfs.vfat
format_arguments=()
if [ ! -z "$UUID" ]; then
format_arguments=( "${format_arguments[@]}" -i "${UUID//-}" )
fi
if [ ! -z "$LABEL" ]; then
format_arguments=( "${format_arguments[@]}" -n "${LABEL}" )
fi
;;
ext4)
format_command=/sbin/mkfs.ext4
format_arguments=( -F -E root_owner=100000:100000 )
if [ ! -z "$UUID" ]; then
format_arguments=( "${format_arguments[@]}" -U "${UUID}" )
fi
if [ ! -z "$LABEL" ]; then
format_arguments=( "${format_arguments[@]}" -L "${LABEL}")
fi
;;
*)
if [ -z "$TYPE" ]; then
continue
fi
format_command=
format_arguments=
;;
esac
if [ -f $format_command ]; then
log "Formatting $device as $TYPE"
if test "$SAILFISHOS_WIPE_PARTITIONS" = "1"; then
dd if=/dev/zero of=$device bs=1M
fi
echo $format_command "${format_arguments[@]}" $device
$format_command "${format_arguments[@]}" $device || log "Failed to format $device"
else
TEMPMOUNT=$(mktemp -d)
if mount $device $TEMPMOUNT; then
log "Erasing data from $device"
find $TEMPMOUNT -mindepth 1 -delete
if test "$SAILFISHOS_WIPE_PARTITIONS" = "1"; then
dd if=/dev/zero of=$TEMPMOUNT/large
rm -f $TEMPMOUNT/large
fi
umount $TEMPMOUNT
else
log "Failed to mount $device to erase data"
if test "$SAILFISHOS_WIPE_PARTITIONS" = "1"; then
log "Erasing partition"
dd if=/dev/zero of=$device
fi
fi
rmdir $TEMPMOUNT
fi
done
EOF
}
# Format devices from the rootfs where there is a full shell and more file-system tools.
TEMPMOUNT=$(mktemp -d)
if mount /dev/sailfish/root $TEMPMOUNT; then
mount -t tmpfs tmpfs $TEMPMOUNT/tmp
mount -t devtmpfs devtmpfs $TEMPMOUNT/dev
mount -t proc proc $TEMPMOUNT/proc
mount -t sysfs sys $TEMPMOUNT/sys
format_script > $TEMPMOUNT/tmp/format-devices
chmod 755 $TEMPMOUNT/tmp/format-devices
chroot $TEMPMOUNT /tmp/format-devices $@
umount $TEMPMOUNT/tmp $TEMPMOUNT/dev $TEMPMOUNT/proc $TEMPMOUNT/sys
# Clean up
umount $TEMPMOUNT
else
echo "$0: Failed to mount rootfs" > /dev/kmsg
if test "$SAILFISH_WIPE_PARTITIONS" = "$1"; then
for device in $@; do
dd if=/dev/zero of=$device
done
fi
fi
rmdir $TEMPMOUNT