bs2-fusecds/cd-mount.sh

99 lines
3.4 KiB
Bash
Executable File

#!/bin/bash
#=======================================================================
# FILE: cd-mount.sh
# USAGE: cd-mount.sh [<Imageverzeichnis>]
# DESCRIPTION:
# OPTIONS:
# REQUIREMENTS:
# BUGS:
# NOTES:
# AUTHOR: Jonas Tobias Hopusch <hopusch.jonastobias@fh-swf.de>
# COMPANY: Fachhochschule Südwestfalen
# VERSION: 1
# CREATED: 2023-01-10T16:32Z
# REVISION:
#=======================================================================
#-----------------------------------------------------------------------
# Abort script if undefined veriables are referenced
# and properly handle pipe failure
#-----------------------------------------------------------------------
set -uo pipefail
#-----------------------------------------------------------------------
# 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[@]}"
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"