bs2-fusecds/cd-mount.sh

99 lines
3.4 KiB
Bash
Raw Normal View History

2023-01-10 17:48:39 +01:00
#!/bin/bash
#=======================================================================
# FILE: cd-mount.sh
2023-01-10 20:46:38 +01:00
# USAGE: cd-mount.sh [<Imageverzeichnis>]
2023-01-10 17:48:39 +01:00
# DESCRIPTION:
# OPTIONS:
# REQUIREMENTS:
# BUGS:
# NOTES:
# AUTHOR: Jonas Tobias Hopusch <hopusch.jonastobias@fh-swf.de>
# COMPANY: Fachhochschule Südwestfalen
# VERSION: 1
2023-01-10 20:46:38 +01:00
# CREATED: 2023-01-10T16:32Z
2023-01-10 17:48:39 +01:00
# REVISION:
#=======================================================================
2023-01-10 20:46:38 +01:00
#-----------------------------------------------------------------------
# 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`" \
2023-01-10 20:46:38 +01:00
>/dev/null 2>&1
2023-01-10 17:48:39 +01:00
#-----------------------------------------------------------------------
2023-01-10 20:46:38 +01:00
# Per Image, do the following
2023-01-10 17:48:39 +01:00
#-----------------------------------------------------------------------
2023-01-10 20:46:38 +01:00
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
2023-01-10 20:46:38 +01:00
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"