Implement reversal script cd-umount.sh
This commit is contained in:
parent
3ee30d837f
commit
61fbf5729a
1 changed files with 81 additions and 2 deletions
83
cd-umount.sh
Normal file → Executable file
83
cd-umount.sh
Normal file → Executable file
|
@ -1,7 +1,7 @@
|
|||
#!/bin/bash
|
||||
#=======================================================================
|
||||
# FILE: cd-umount.sh
|
||||
# USAGE: cd-umount.sh [<Verzeichnis, welches bearbeitet werden sollte>]
|
||||
# USAGE: cd-umount.sh [<Imageverzeichnis>]
|
||||
# DESCRIPTION:
|
||||
# OPTIONS:
|
||||
# REQUIREMENTS:
|
||||
|
@ -13,7 +13,86 @@
|
|||
# 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" 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"
|
||||
|
|
Loading…
Reference in a new issue