First Implementation of cd-mount.sh

This commit is contained in:
Jonas Tobias Hopusch 2023-01-10 20:46:38 +01:00
parent 478566553d
commit 3ee30d837f
Signed by: jotoho
GPG key ID: 913BDF1196DCF600

85
cd-mount.sh Normal file → Executable file
View file

@ -1,7 +1,7 @@
#!/bin/bash #!/bin/bash
#======================================================================= #=======================================================================
# FILE: cd-mount.sh # FILE: cd-mount.sh
# USAGE: cd-mount.sh [<Verzeichnis, welches bearbeitet werden sollte>] # USAGE: cd-mount.sh [<Imageverzeichnis>]
# DESCRIPTION: # DESCRIPTION:
# OPTIONS: # OPTIONS:
# REQUIREMENTS: # REQUIREMENTS:
@ -10,10 +10,89 @@
# AUTHOR: Jonas Tobias Hopusch <hopusch.jonastobias@fh-swf.de> # AUTHOR: Jonas Tobias Hopusch <hopusch.jonastobias@fh-swf.de>
# COMPANY: Fachhochschule Südwestfalen # COMPANY: Fachhochschule Südwestfalen
# VERSION: 1 # VERSION: 1
# CREATED: 2023-01-08T14:10Z # CREATED: 2023-01-10T16:32Z
# REVISION: # 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"