blob: 04084e536e68e095d4d6e688d9bd8e45a21913a2 [file]
#!/bin/bash
shopt -s nullglob
ret=0
rwfs=/tmp/rwfs
cow="$rwfs/cow"
purge_file="$cow"/var/google/do-rwfs-purge
job_id_tmp=/tmp/job-id
done_job_id="$cow"/var/google/rwfs-purged
mount_rwfs() {
echo 'Mounting RWFS in isolation' >&2
mount -t jffs2 -o defaults,noatime mtd:rwfs "$rwfs"
}
umount_rwfs() {
echo 'Unmounting RWFS' >&2
umount "$@" "$rwfs"
}
mkdir -p "$rwfs" || exit
mount_rwfs || exit
# Keep track of the last version to use this rwfs
version_from_osrelease() {
grep '^VERSION_ID=' "$1" | sed 's,.*gbmc-release-,,'
}
if ver=$(version_from_osrelease /etc/os-release); then
echo "Current Release: $ver" >&2
fi
if ver=$(version_from_osrelease "$rwfs"/os-release); then
echo "Previous Release: $ver" >&2
fi
if [ -e "$purge_file" ]; then
cp -v "$purge_file" "$job_id_tmp"
umount_rwfs -f || true
echo 'Purging RWFS' >&2
flash_eraseall /dev/$(grep '"rwfs"$' /proc/mtd | sed 's,:.*,,') || ret=$?
mount_rwfs || ret=$?
mkdir -p "$(dirname "$done_job_id")"
cp -v "$job_id_tmp" "$done_job_id"
elif [ '@ALWAYS_CLEAR@' = '1' ] || ! cmp "$rwfs"/os-release /etc/os-release 2>/dev/null; then
declare -A allowed=()
while read -r line; do
allowed["$line/*"]=1
while [ -n "$line" ]; do
allowed["$line"]=1
line="${line%/*}"
done
done </usr/share/gbmc-rwfs-allow.list
# remove everything not in the whitelist.
# e.g. the whitelist contains a single entry /a/b, the
# follow command will be generated:
# find /run/initramfs/rw/cow -mindepth 1 ! -path /run/initramfs/rw/cow/a/b/* ! -path /run/initramfs/rw/cow/a/b ! -path /run/initramfs/rw/cow/a -exec rm -rf {} +
args=("$cow" -mindepth 1)
for path in "${!allowed[@]}"; do
args+=(! -path "$cow$path")
done
args+=(-exec rm -rf '{}' '+')
echo 'Cleaning RWFS' >&2
if [ -e "$cow" ]; then
find "${args[@]}" || ret=$?
fi
fi
if ! cmp /etc/os-release "$rwfs"/os-release 2>/dev/null; then
cp /etc/os-release "$rwfs"/os-release || ret=$?
fi
umount /tmp/rwfs || ret=$?
exit $ret