From a8a670c08ea0c7f304b940781401d5d5fd5bd991 Mon Sep 17 00:00:00 2001 From: Dan Puttick Date: Fri, 13 Jan 2017 17:05:37 -0500 Subject: [PATCH] Split part of groomer.sh into mount_dest.sh --- fs_filecheck/etc/rc.local | 1 - fs_filecheck/opt/groomer/config.sh | 33 +++--- fs_filecheck/opt/groomer/groomer.sh | 138 +++++++------------------ fs_filecheck/opt/groomer/init.sh | 8 +- fs_filecheck/opt/groomer/mount_dest.sh | 77 ++++++++++++++ shell_utils/copy_groomer_to_image.sh | 6 +- 6 files changed, 140 insertions(+), 123 deletions(-) mode change 100644 => 100755 fs_filecheck/opt/groomer/config.sh create mode 100755 fs_filecheck/opt/groomer/mount_dest.sh diff --git a/fs_filecheck/etc/rc.local b/fs_filecheck/etc/rc.local index fba908f..d12d10f 100755 --- a/fs_filecheck/etc/rc.local +++ b/fs_filecheck/etc/rc.local @@ -16,7 +16,6 @@ clean(){ echo "GROOMER: rc.local done, shutting down." - sleep 30 /sbin/shutdown -P -h now } diff --git a/fs_filecheck/opt/groomer/config.sh b/fs_filecheck/opt/groomer/config.sh old mode 100644 new mode 100755 index bde4fa8..e31a4b5 --- a/fs_filecheck/opt/groomer/config.sh +++ b/fs_filecheck/opt/groomer/config.sh @@ -1,27 +1,28 @@ -DEV_SRC='/dev/sda' -DEV_SRC_ONE='/dev/sda1' -DEV_DST='/dev/sdb' -DEV_DST_ONE='/dev/sdb1' - -# User allowed to do the following commands without password -USERNAME='kitten' -MUSIC="/opt/midi/" - +USERNAME="kitten" ID=`/usr/bin/id -u` + # Paths used in multiple scripts SRC="src" -DST="dst" +DEV_SRC="/dev/sda" +DEV_SRC_ONE="/dev/sda1" SRC_MNT="/media/src" + +DST="dst" +DEV_DST="/dev/sdb" +DEV_DST_ONE="/dev/sdb1" DST_MNT="/media/dst" + TEMP="${DST_MNT}/temp" ZIPTEMP="${DST_MNT}/ziptemp" LOGS="${DST_MNT}/logs" GROOM_LOG="/tmp/groom_log.txt" +MUSIC="/opt/midi/" -# commands -SYNC='/bin/sync' -TIMIDITY='/usr/bin/timidity' -MOUNT='/bin/mount' -PMOUNT='/usr/bin/pmount -A -s' -PUMOUNT='/usr/bin/pumount' + +# Commands +SYNC="/bin/sync" +TIMIDITY="/usr/bin/timidity" +MOUNT="/bin/mount" +PMOUNT="/usr/bin/pmount -A -s" +PUMOUNT="/usr/bin/pumount" diff --git a/fs_filecheck/opt/groomer/groomer.sh b/fs_filecheck/opt/groomer/groomer.sh index 10d57a8..7b8dc02 100755 --- a/fs_filecheck/opt/groomer/groomer.sh +++ b/fs_filecheck/opt/groomer/groomer.sh @@ -4,87 +4,25 @@ set -e set -x +# Import constants from config file source ./config.sh if ! [ "${ID}" -ge "1000" ]; then - echo "GROOMER: This script cannot run as root." + echo "GROOMER: groomer.sh cannot run as root." exit fi -clean(){ - echo "GROOMER: Cleaning up after groomer.sh." - - cp ${GROOM_LOG} "${DST_MNT}/groomer_log_dst.txt" - cp ${GROOM_LOG} "${SRC_MNT}/groomer_log_src.txt" - +clean(){ # Write anything in memory to disk ${SYNC} - # Unmount source - pumount ${SRC} - - # Clean up and unmount destination + # Remove temporary files from destination key rm -rf ${TEMP} rm -rf ${ZIPTEMP} - pumount ${DST} - - exit } trap clean EXIT TERM INT -# De we have a source device? -if [ ! -b ${DEV_SRC} ]; then - echo "GROOMER: Source device (${DEV_SRC}) does not exist." - exit -fi - -# Do we have a destination device -if [ ! -b ${DEV_DST} ]; then - echo "GROOMER: Destination device (${DEV_DST}) does not exist." - exit -fi - -# Make sure destination device isn't already mounted -if ${MOUNT}|grep ${DST}; then - ${PUMOUNT} ${DST} || true -fi - -# uid= only works on a vfat FS. What should wedo if we get an ext* FS ? -# What does this ^ comment mean? - -# Mount the first partition of DST (/dev/sdb1) -# pmount automatically mount on /media/, so at /media/dst in this case -${PMOUNT} -w ${DEV_DST_ONE} ${DST} -if [ ${?} -ne 0 ]; then - echo "GROOMER: Unable to mount ${DEV_DST_ONE} on ${DST_MNT}" - exit -else - echo "GROOMER: Destination USB device (${DEV_DST_ONE}) mounted at ${DST_MNT}" - - # Remove any existing "FROM_PARTITION_" directories - rm -rf "/media/${DST}/FROM_PARTITION_"* - - # prepare temp dirs and make sure they're empty if they already exist - mkdir -p "${TEMP}" - mkdir -p "${ZIPTEMP}" - mkdir -p "${LOGS}" - rm -rf "${TEMP}/"* - rm -rf "${ZIPTEMP}/"* - rm -rf "${LOGS}/"* -fi - -sleep 30 - -${PMOUNT} -w ${DEV_SRC_ONE} ${SRC} - -sleep 10 - -# Groom da kitteh! - -# List all block devices (uncomment for diagnostics) -# lsblk -n -o name,fstype,mountpoint,label,uuid -r - # Find the partition names on the source device DEV_PARTITIONS=`ls "${DEV_SRC}"* | grep "${DEV_SRC}[1-9][0-6]*" || true` if [ -z "${DEV_PARTITIONS}" ]; then @@ -92,42 +30,46 @@ if [ -z "${DEV_PARTITIONS}" ]; then exit fi -sleep 10 -# PARTCOUNT=1 -# for partition in ${DEV_PARTITIONS} -# do -# # Processing a partition -# echo "GROOMER: Processing partition ${partition}" -# if [ `${MOUNT} | grep -c ${SRC}` -ne 0 ]; then -# ${PUMOUNT} ${SRC} -# fi +PARTCOUNT=1 +for partition in ${DEV_PARTITIONS} +do + echo "GROOMER: Processing partition ${partition}" + # Unmount anything that is mounted on /media/src + if [ `${MOUNT} | grep -c ${SRC}` -ne 0 ]; then + ${PUMOUNT} ${SRC} + fi -# ${PMOUNT} -w ${partition} ${SRC} -# ls "/media/${SRC}" | grep -i autorun.inf | xargs -I {} mv "/media/${SRC}"/{} "/media/${SRC}"/DANGEROUS_{}_DANGEROUS || true -# ${PUMOUNT} ${SRC} -# ${PMOUNT} -r ${partition} ${SRC} -# if [ ${?} -ne 0 ]; then -# echo "GROOMER: Unable to mount ${partition} on /media/${SRC}" -# else -# echo "GROOMER: ${partition} mounted at /media/${SRC}" + # Mount the current partition in write mode + ${PMOUNT} -w ${partition} ${SRC} + # Mark any autorun.inf files as dangerous on the source device + ls ${SRC_MNT} | grep -i autorun.inf | xargs -I {} mv "${SRC_MNT}"/{} "{SRC_MNT}"/DANGEROUS_{}_DANGEROUS || true + # Unmount and remount the current partition in read-only mode + ${PUMOUNT} ${SRC} + ${PMOUNT} -r ${partition} ${SRC} + if [ ${?} -ne 0 ]; then + # Previous command (mounting this partition) failed + echo "GROOMER: Unable to mount ${partition} on /media/${SRC}" + else + echo "GROOMER: ${partition} mounted at /media/${SRC}" -# # Print the filenames on the current partition in a logfile -# find "/media/${SRC}" -fls "${LOGS}/Content_partition_${PARTCOUNT}.txt" + # Put the filenames from the current partition in a logfile + find "/media/${SRC}" -fls "${LOGS}/contents_partition_${PARTCOUNT}.txt" -# # create a directory on ${DST} named PARTION_$PARTCOUNT -# target_dir="/media/${DST}/FROM_PARTITION_${PARTCOUNT}" -# echo "GROOMER: Copying to ${target_dir}" -# mkdir -p "${target_dir}" -# LOGFILE="${LOGS}/processing.txt" + # Create a directory on ${DST} named PARTION_$PARTCOUNT + target_dir="/media/${DST}/FROM_PARTITION_${PARTCOUNT}" + mkdir -p "${target_dir}" + LOGFILE="${LOGS}/processing.txt" -# echo "GROOMER: ==== Starting processing of /media/${SRC} to ${target_dir}. ====" >> ${LOGFILE} -# filecheck.py --source /media/${SRC} --destination ${target_dir} || true -# echo "GROOMER: ==== Done with /media/${SRC} to ${target_dir}. ====" >> ${LOGFILE} + # Run the current partition through filecheck.py + echo "GROOMER: ==== Starting processing of /media/${SRC} to ${target_dir}. ====" >> ${LOGFILE} + filecheck.py --source /media/${SRC} --destination ${target_dir} || true + echo "GROOMER: ==== Done with /media/${SRC} to ${target_dir}. ====" >> ${LOGFILE} -# ls -lR "${target_dir}" -# fi -# let PARTCOUNT=`expr $PARTCOUNT + 1` -# done + # List destination files (recursively) for debugging + ls -lR "${target_dir}" + fi + let PARTCOUNT=`expr $PARTCOUNT + 1` +done # The cleanup is automatically done in the function clean called when -# the program quits +# the program exits diff --git a/fs_filecheck/opt/groomer/init.sh b/fs_filecheck/opt/groomer/init.sh index 357d446..120c085 100755 --- a/fs_filecheck/opt/groomer/init.sh +++ b/fs_filecheck/opt/groomer/init.sh @@ -4,6 +4,7 @@ set -e set -x +# Import constants from config file source ./config.sh if [ ${ID} -ne 0 ]; then @@ -18,8 +19,7 @@ clean(){ trap clean EXIT TERM INT -lsblk -n -o name,fstype,mountpoint,label,uuid -r |& tee ${GROOM_LOG} +# List block storage devices (for debugging) +lsblk |& tee ${GROOM_LOG} -sleep 30 - -su ${USERNAME} -c ./groomer.sh |& tee -a ${GROOM_LOG} +su ${USERNAME} -c ./mount_dest.sh |& tee -a ${GROOM_LOG} diff --git a/fs_filecheck/opt/groomer/mount_dest.sh b/fs_filecheck/opt/groomer/mount_dest.sh new file mode 100755 index 0000000..0e3d955 --- /dev/null +++ b/fs_filecheck/opt/groomer/mount_dest.sh @@ -0,0 +1,77 @@ +#!/bin/bash + +# set -e (exit when a line returns non-0 status) and -x (xtrace) flags +set -e +set -x + +# Import constants from config file +source ./config.sh + +if ! [ "${ID}" -ge "1000" ]; then + echo "GROOMER: mount_keys.sh cannot run as root." + exit +fi + +clean(){ + echo "GROOMER: Cleaning up in mount_keys.sh." + + # Copy the temporary logfile to the destination key + cp ${GROOM_LOG} "${DST_MNT}/groomer_log_dst.txt" + + # Write anything in memory to disk + ${SYNC} + + # Unmount source and destination + pumount ${SRC} + + # Clean up and unmount destination + pumount ${DST} + + exit +} + +trap clean EXIT TERM INT + +# Check that a device is available on /dev/sda +if [ ! -b ${DEV_SRC} ]; then + echo "GROOMER: Source device (${DEV_SRC}) does not exist." + exit +fi + +# Check that a device is available on /dev/sdb +if [ ! -b ${DEV_DST} ]; then + echo "GROOMER: Destination device (${DEV_DST}) does not exist." + exit +fi + +# If there is already a device mounted on /media/dst, unmount it +if ${MOUNT}|grep ${DST}; then + ${PUMOUNT} ${DST} || true +fi + +# uid= only works on a vfat FS. What should wedo if we get an ext* FS ? +# What does this ^ comment mean? + +# Mount the first partition of DST (/dev/sdb1) +# pmount automatically mounts on /media/ (at /media/dst in this case). +${PMOUNT} -w ${DEV_DST_ONE} ${DST} +if [ ${?} -ne 0 ]; then + echo "GROOMER: Unable to mount ${DEV_DST_ONE} on ${DST_MNT}" + exit +else + echo "GROOMER: Destination USB device (${DEV_DST_ONE}) mounted at ${DST_MNT}" + + # Remove any existing "FROM_PARTITION_" directories + rm -rf "/media/${DST}/FROM_PARTITION_"* + + # Prepare temp dirs and make sure they're empty if they already exist + mkdir -p "${TEMP}" + mkdir -p "${ZIPTEMP}" + mkdir -p "${LOGS}" + rm -rf "${TEMP}/"* + rm -rf "${ZIPTEMP}/"* + rm -rf "${LOGS}/"* +fi + +# Now that destination is mounted and prepared, run the groomer +./groomer.sh diff --git a/shell_utils/copy_groomer_to_image.sh b/shell_utils/copy_groomer_to_image.sh index 0651aa2..8337982 100755 --- a/shell_utils/copy_groomer_to_image.sh +++ b/shell_utils/copy_groomer_to_image.sh @@ -1,11 +1,9 @@ #!/bin/bash -set -e set -x -# mount /dev/sdb2 /mnt/rpi - cp /media/sf_ubuntu-shared/Circlean-Ubuntu/fs_filecheck/opt/groomer/init.sh /mnt/rpi/opt/groomer/init.sh cp /media/sf_ubuntu-shared/Circlean-Ubuntu/fs_filecheck/opt/groomer/groomer.sh /mnt/rpi/opt/groomer/groomer.sh cp /media/sf_ubuntu-shared/Circlean-Ubuntu/fs_filecheck/opt/groomer/config.sh /mnt/rpi/opt/groomer/config.sh -cp /media/sf_ubuntu-shared/Circlean-Ubuntu/fs_filecheck/etc/rc.local /mnt/rpi/etc/rc.local \ No newline at end of file +cp /media/sf_ubuntu-shared/Circlean-Ubuntu/fs_filecheck/opt/groomer/mount_dest.sh /mnt/rpi/opt/groomer/mount_dest.sh +cp /media/sf_ubuntu-shared/Circlean-Ubuntu/fs_filecheck/etc/rc.local /mnt/rpi/etc/rc.local