Circlean/circlean_fs/root_partition/opt/groomer/groomer.sh

85 lines
2.6 KiB
Bash
Raw Normal View History

2015-11-06 17:40:33 +01:00
#!/bin/bash
2017-01-31 21:44:13 +01:00
clean(){
2017-08-10 17:51:05 +02:00
if [ "${DEBUG}" = true ]; then
2017-01-31 21:44:13 +01:00
sleep 20
fi
2017-01-12 20:00:51 +01:00
# Write anything in memory to disk
${SYNC}
2015-11-06 17:40:33 +01:00
# Remove temporary files from destination key
2017-08-10 17:51:05 +02:00
rm -rf "${TEMP}"
2015-11-06 17:40:33 +01:00
}
2017-08-10 17:51:05 +02:00
check_not_root() {
if ! [ "${ID}" -ge "1000" ]; then
echo "GROOMER: groomer.sh cannot run as root."
exit
fi
}
2015-11-06 17:40:33 +01:00
check_has_partitions () {
2017-08-10 17:51:05 +02:00
local partitions=$1
if [ -z "${partitions}" ]; then
echo "GROOMER: ${SRC_DEV} does not have any partitions."
2017-08-10 17:51:05 +02:00
exit
fi
}
2017-01-12 21:42:41 +01:00
2017-08-10 17:51:05 +02:00
run_groomer() {
local dev_partitions
# Find the partition names on the device
dev_partitions=$(ls "${SRC_DEV}"* | grep "${SRC_DEV}[1-9][0-6]*" || true)
2017-08-10 17:51:05 +02:00
check_has_partitions dev_partitions
local partcount=1
local partition
for partition in ${dev_partitions}
do
echo "GROOMER: Processing partition ${partition}"
# Mount the current partition in write mode
SRC_MNT=`${MOUNT} -o rw -b ${partition}| sed -ne 's/Mounted \(.*\) at \(\/media\/kitten\/.*\).$/\2/p'`
if [ -z "$SRC_MNT" ]; then
echo "Unable to mount source partition (${partition})."
continue
fi
2017-08-10 17:51:05 +02:00
# Mark any autorun.inf files as dangerous on the source device to be extra careful
ls "${SRC_MNT}" | grep -i autorun.inf | xargs -I {} mv "${SRC_MNT}"/{} "${SRC_MNT}"/DANGEROUS_{}_DANGEROUS || true
2017-08-10 17:51:05 +02:00
# Unmount and remount the current partition in read-only mode
${UMOUNT} -b "${partition}"
2017-08-10 17:51:05 +02:00
if ${MOUNT} -o ro -b "${partition}"; then
echo "GROOMER: ${partition} mounted at ${SRC_MNT}"
# Create a directory on ${DST_MNT} named PARTION_$PARTCOUNT
local target_dir="${DST_MNT}/FROM_PARTITION_${partcount}"
2017-08-10 17:51:05 +02:00
mkdir -p "${target_dir}"
2017-08-10 17:51:05 +02:00
# Run the current partition through filecheck.py
filecheck.py --source "${SRC_MNT}" --destination "${target_dir}" || true
2017-08-10 17:51:05 +02:00
# List destination files (recursively) for debugging
2017-08-10 23:44:11 +02:00
if [ "${DEBUG}" = true ]; then
ls -lR "${target_dir}"
fi
${UMOUNT} -b "${partition}"
2017-08-10 17:51:05 +02:00
else
# Previous command (mounting current partition) failed
echo "GROOMER: Unable to mount ${partition} on ${SRC_MNT}"
2017-08-10 17:51:05 +02:00
fi
let partcount=$((partcount + 1))
done
}
2017-08-10 17:51:05 +02:00
main() {
set -eu # exit when a line returns non-0 status, treat unset variables as errors
trap clean EXIT TERM INT # run clean when the script ends or is interrupted
source ./config.sh # get config values
if [ "${DEBUG}" = true ]; then
set -x
fi
2017-08-10 17:51:05 +02:00
run_groomer
}
2015-11-06 17:40:33 +01:00
2017-08-10 17:51:05 +02:00
main