blob: 5714fa3a81fc7ed7d5392293cb5b994845df65f6 (
plain)
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
|
#!/bin/bash
# Command to erase user data file system
MNT_USER="/mnt/user"
OVERLAY="$MNT_USER/overlay"
WORKDIR="$MNT_USER/work"
UPPERDIR="$MNT_USER/upper"
LOWERDIR="/"
USER_ORIG="$UPPERDIR/orig"
USER_PARTITION="User data"
TABLE=$'\n'"$(cat /proc/mtd)"
# Erase the file system
DO_ERASE_PERSISTENT=$(u-boot printenv erase_persistent_f | cut -d '"' -f2 | grep -Eo '[0-9]+$' || true)
# Remove files not hidden
DO_ERASE_ALL_EXCEPT_PERSISTENT=$(u-boot printenv default_reset_f | cut -d '"' -f2 | grep -Eo '[0-9]+$' || true)
pid=$$
prefix="<3>overlayrootfs-generator[${pid}]: "
loginfo() { echo "${prefix}$@" >/dev/kmsg ; }
logpipe() {
OIFS="${IFS}"
IFS=$'\n'
while read ln ; do
echo -n "${prefix}${ln}" >/dev/kmsg
done
IFS="${OIFS}"
}
if [[ $TABLE =~ [[:space:]]mtd([[:digit:]]+)[^\"]*\"$USER_PARTITION\" ]] ; then
USER_BDEV=/dev/mtdblock${BASH_REMATCH[1]}
USER_DEV=/dev/mtd${BASH_REMATCH[1]}
else
loginfo "Cannot find a User data partition"
exit 0
fi
fs="/dev /proc /sys /run /var/volatile /dev/pts /sys/kernel/debug /sys/kernel/config"
mnt_user() {
mkdir -p $MNT_USER
mount -t jffs2 $USER_BDEV $MNT_USER
}
my_switch_root() {
NEW_ROOT=$1
loginfo mountinfo
mount --make-rprivate /
loginfo "my_switch_root: Mount file systems"
for f in $fs ; do
if [[ -d $NEW_ROOT/$f ]] ; then
loginfo "my_switch_root: mount -o noatime,move --bind $f $NEW_ROOT/$f"
mount -o noatime,move --bind "$f" "$NEW_ROOT/$f"
else
# loginfo "my_switch_root: Skipping $NEW_ROOT/$f"
:
fi
done
/sbin/pivot_root "$NEW_ROOT" "$NEW_ROOT"/orig
}
do_erase_all_except_persistent() {
# Delete all files and folders except for the "${MNT_USER}/.persistent" directory.
loginfo "Clearing user data. Persistent data will be left "
#UPPERDIR has to be cleared except 'orig' folder
#erasing folders in ${UPPERDIR}
find ${UPPERDIR} -maxdepth 1 -type d | grep "${UPPERDIR}/" | grep -v orig | xargs rm -rf
#erasing rest (files, if they are there) in ${UPPERDIR}
rm ${UPPERDIR}/* >/dev/null 2>&1
}
# Most efficient way to clear UBIFS file system
do_erase_persistent() {
loginfo "Erasing user data"
# Sys must be mounted to use ubifs utilities
mount -t sysfs sysfs /sys
umount -l $MNT_USER
loginfo "Erase user-data partition $USER_DEV"
flash_erase -j ${USER_DEV} 0 0 2>&1 | logpipe
mnt_user
}
do_rw_mount() {
loginfo "Starting RW overlayfs"
mount -t tmpfs inittemp /mnt
mnt_user # user_data is now accessible
if [[ "$DO_ERASE_PERSISTENT" -eq 1 ]] ; then
do_erase_persistent
u-boot setenv default_reset_f
u-boot setenv erase_persistent_f 0
elif [[ "$DO_ERASE_ALL_EXCEPT_PERSISTENT" -eq 1 ]] ; then
do_erase_all_except_persistent
u-boot setenv default_reset_f
fi
mkdir -p $UPPERDIR $WORKDIR $OVERLAY $USER_ORIG ${MNT_USER}/.persistent
loginfo "Mounting..."
mount -o index=off,metacopy=off,noatime,lowerdir=$LOWERDIR,upperdir=$UPPERDIR,workdir=$WORKDIR,rw -t overlay overlay $OVERLAY
loginfo "/proc/mounts:"
cat /proc/mounts | grep overlay | logpipe
loginfo "Switch root to $OVERLAY"
my_switch_root $OVERLAY
mount --bind /orig/mnt/user/.persistent/ /var/persistent
cat /proc/mounts | grep overlay | logpipe
mount -o remount,rw /
cat /proc/mounts | grep overlay | logpipe
cat /proc/mounts | grep tmp | logpipe
umount -l /tmp >/dev/null 2>&1
umount -l /var/volatile >/dev/null 2>&1
cgroupmnts=$(sed -r -e 's/[^[:space:]]*[[:space:]]//' -e 's/[[:space:]]+.*//g' /proc/mounts | grep '^/orig')
for mp in ${cgroupmnts} ; do
if [[ $mp != /orig ]] && [[ -d $mp ]] ; then
umount -l "$mp" >/dev/null 2>&1
fi
done
}
do_start() {
do_rw_mount
}
case $1 in
start)
do_start
;;
*)
echo "Usage: $0 {start}"
;;
esac
|