2023-01-10 17:48:39 +01:00
|
|
|
#!/bin/bash
|
|
|
|
#=======================================================================
|
|
|
|
# FILE: cd-umount.sh
|
2023-01-10 21:11:07 +01:00
|
|
|
# USAGE: cd-umount.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
|
|
|
|
# CREATED: 2023-01-10T16:32Z
|
|
|
|
# REVISION:
|
|
|
|
#=======================================================================
|
2023-01-10 21:11:07 +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
|
2023-01-10 17:48:39 +01:00
|
|
|
|
|
|
|
#-----------------------------------------------------------------------
|
2023-01-10 21:11:07 +01:00
|
|
|
# Search for matching images
|
2023-01-10 17:48:39 +01:00
|
|
|
#-----------------------------------------------------------------------
|
2023-01-10 21:11:07 +01:00
|
|
|
|
|
|
|
#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"
|