From 3ee30d837f19bee55d2735ed21d84a97f5b522ed Mon Sep 17 00:00:00 2001 From: Jonas Tobias Hopusch Date: Tue, 10 Jan 2023 20:46:38 +0100 Subject: [PATCH] First Implementation of cd-mount.sh --- cd-mount.sh | 85 +++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 82 insertions(+), 3 deletions(-) mode change 100644 => 100755 cd-mount.sh diff --git a/cd-mount.sh b/cd-mount.sh old mode 100644 new mode 100755 index ec7908b..78feed9 --- a/cd-mount.sh +++ b/cd-mount.sh @@ -1,7 +1,7 @@ #!/bin/bash #======================================================================= # FILE: cd-mount.sh -# USAGE: cd-mount.sh [] +# USAGE: cd-mount.sh [] # DESCRIPTION: # OPTIONS: # REQUIREMENTS: @@ -10,10 +10,89 @@ # AUTHOR: Jonas Tobias Hopusch # COMPANY: Fachhochschule Südwestfalen # VERSION: 1 -# CREATED: 2023-01-08T14:10Z +# 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"`" \ + >/dev/null 2>&1 + +#----------------------------------------------------------------------- +# Per Image, do the following +#----------------------------------------------------------------------- +amountOfImages="${#imageFiles[@]}" +successfullyMountedImages=0 +for image in "${imageFiles[@]}"; do + # Count how many there are in total + #((amountOfImages++)) + + # Validate that it can be accessed + if ! [ -f "$image" ]; then + echo "$image is not a regular file. Skipping." + continue + elif ! [ -r "$image" ]; then + echo "$image is not readable. Skipping." + continue + fi + + # Determine the location the image should be mounted to and try to ensure + # it exists + mountDirName="${image:0:((${#image} - ${#imageFileEnding}))}" + if ! mkdir -p "$mountDirName"; then + echo "Could not create mount destination directory $mountDirName. 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 "Something is already mounted to $mountDirName. Skipping" + continue + fi + + # Attempt to mount the image to the destination directory + if fuseiso "$image" "$mountDirName"; then + echo "$image has been mounted to $mountDirName" + ((successfullyMountedImages++)) + fi +done + +echo "$successfullyMountedImages/$amountOfImages images have been mounted successfully"