Last active
March 12, 2024 19:35
-
-
Save Knogle/e690b8134637abe9fa473cc9eae58f98 to your computer and use it in GitHub Desktop.
Mount Loop devices. sudo ./mount-loop.sh /path/to/your/file.img
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Function to display help message | |
show_help() { | |
echo "Usage: $0 [OPTION]... [FILE]" | |
echo "Set up and mount a loop device for a given file, or create a new file of specified size and mount it." | |
echo "" | |
echo " --help display this help and exit" | |
echo " automount <Size> automatically create a file of specified size, set up and mount it" | |
echo " <FilePath> path to the existing file to set up and mount" | |
echo "" | |
echo "Examples:" | |
echo " $0 /path/to/your/file.img Set up an existing file as a loop device." | |
echo " $0 automount 1M Create a 1M-sized file and set it up as a loop device." | |
echo "" | |
echo "Note: Run this script with root permissions or using sudo." | |
} | |
# Function to create, set up, and mount a loop device with a file of a specified size | |
automount() { | |
# Generate a random GUID for the file name | |
GUID=$(uuidgen) | |
FILENAME="${GUID}.img" | |
FILEPATH="/tmp/${FILENAME}" | |
FILESIZE=$1 | |
# Create a file of the desired size | |
dd if=/dev/zero of="$FILEPATH" bs=$FILESIZE count=1 | |
# Proceed with setting up and mounting the loop device | |
setup_and_mount "$FILEPATH" | |
} | |
# Function to set up and mount the loop device | |
setup_and_mount() { | |
FILEPATH="$1" | |
# Create and associate a loop device with the specified file | |
LOOPDEVICE=$(sudo losetup -fP --show "$FILEPATH") | |
if [ -z "$LOOPDEVICE" ]; then | |
echo "Error creating the loop device." | |
exit 1 | |
fi | |
echo "Loop device set up: $LOOPDEVICE" | |
# No separate mount step required here | |
# Wait for user input before proceeding to detach the loop device | |
read -p "Press [Enter] to detach the loop device." | |
cleanup "$LOOPDEVICE" "$FILEPATH" | |
} | |
# Function for cleanup: detaching the loop device, and deleting the file if created by automount | |
cleanup() { | |
LOOPDEVICE=$1 | |
FILEPATH=$2 | |
# Detach the loop device | |
sudo losetup -d "$LOOPDEVICE" | |
echo "Loop device detached: $LOOPDEVICE" | |
# Delete the file if it was created by automount | |
if [[ "$FILEPATH" == /tmp/*.img ]]; then | |
rm -f "$FILEPATH" | |
echo "Temporary file deleted: $FILEPATH" | |
fi | |
} | |
# Main logic | |
case "$1" in | |
--help) | |
show_help | |
;; | |
automount) | |
if [ "$#" -ne 2 ]; then | |
echo "Usage: $0 automount <Size>" | |
exit 1 | |
fi | |
automount $2 | |
;; | |
*) | |
if [ "$#" -ne 1 ]; then | |
show_help | |
exit 1 | |
fi | |
setup_and_mount $1 | |
;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment