From 61fbf5729ad03777430019b06479294a1cf62538 Mon Sep 17 00:00:00 2001 From: Jonas Tobias Hopusch Date: Tue, 10 Jan 2023 21:11:07 +0100 Subject: [PATCH] Implement reversal script cd-umount.sh --- cd-umount.sh | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 81 insertions(+), 2 deletions(-) mode change 100644 => 100755 cd-umount.sh diff --git a/cd-umount.sh b/cd-umount.sh old mode 100644 new mode 100755 index 3b7587a..5d75285 --- a/cd-umount.sh +++ b/cd-umount.sh @@ -1,7 +1,7 @@ #!/bin/bash #======================================================================= # FILE: cd-umount.sh -# USAGE: cd-umount.sh [] +# USAGE: cd-umount.sh [] # DESCRIPTION: # OPTIONS: # REQUIREMENTS: @@ -13,7 +13,86 @@ # CREATED: 2023-01-10T16:32Z # REVISION: #======================================================================= +#----------------------------------------------------------------------- +# Abort script if undefined veriables are referenced +# and properly handle pipe failure +#----------------------------------------------------------------------- +set -uo pipefail #----------------------------------------------------------------------- -# COMMENT +# Script configuration #----------------------------------------------------------------------- +imageFileEnding='.image' +defaultTargetDir="$HOME/CDs/" + +#----------------------------------------------------------------------- +# Check if fuseiso and fusermount are present +#----------------------------------------------------------------------- +if ! which 'fuseiso' >/dev/null 2>&1; then + echo 'fuseiso is not installed' + exit 1 +elif ! which 'fusermount' >/dev/null 2>&1; then + echo 'fusermount is not installed' + exit 1 +fi + +#----------------------------------------------------------------------- +# Determine target directory +#----------------------------------------------------------------------- +if [ -v 1 ]; then + targetDir="$1" +else + targetDir="$defaultTargetDir" +fi + +#----------------------------------------------------------------------- +# Search for matching images +#----------------------------------------------------------------------- + +#shellcheck disable=SC2006 +readarray -t imageFiles \ + <<<"`find "$targetDir" -type f -iname "*$imageFileEnding" 2>/dev/null`" \ + >/dev/null 2>&1 + +#----------------------------------------------------------------------- +# Per Image, do the following +#----------------------------------------------------------------------- +amountOfImages="${#imageFiles[@]}" +successfullyUnmountedImages=0 +for image in "${imageFiles[@]}"; do + # Count how many there are in total + #((amountOfImages++)) + + # Validate that it is a valid regular file + if ! [ -f "$image" ]; then + echo "$image is not a regular file. Skipping." + continue + fi + + # Determine the location the image should be unmounted from + mountDirName="${image:0:((${#image} - ${#imageFileEnding}))}" + if ! [ -d "$mountDirName" ]; then + echo "Directory $mountDirName does not exist. Skipping!" + continue + fi + + # Check if there's already an image mounted at the destination + #shellcheck disable=SC2006 + fullPathMountDir="`cd "$mountDirName" && pwd`" + if ! grep "^fuseiso $fullPathMountDir " /proc/mounts >/dev/null 2>&1; then + echo "No image is mounted to $mountDirName. Skipping" + continue + fi + + # Attempt to mount the image to the destination directory + if fusermount -u "$mountDirName"; then + echo "$image has been unmounted from $mountDirName" + ((successfullyUnmountedImages++)) + fi + + rmdir "$mountDirName" +done + +find "$targetDir/" -mindepth 1 -maxdepth 1 -type d -delete 2>/dev/null + +echo "$successfullyUnmountedImages/$amountOfImages images have been unmounted successfully"