Created
March 27, 2024 02:20
-
-
Save Knogle/a99e4bc7237d2bced8bfe457abdc36fe to your computer and use it in GitHub Desktop.
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 automatically create multiple files of random sizes between 2G and 10G, set up and mount them" | |
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 Create multiple files of random sizes between 2G and 10G and set them up as loop devices." | |
echo "" | |
echo "Note: Run this script with root permissions or using sudo." | |
} | |
# Function to create, set up, and mount loop devices with files of random sizes between 2G and 10G | |
automount() { | |
for i in {1..24}; do | |
# Generate a random size between 2GB and 10GB | |
RANDOM_SIZE=$(shuf -i 2-10 -n 1) | |
SIZE="${RANDOM_SIZE}G" | |
# Call the existing setup process with the random size | |
create_and_setup "$SIZE" | |
done | |
} | |
# Function to create, set up, and mount a loop device with a file of a specified size | |
create_and_setup() { | |
SIZE=$1 | |
GUID=$(uuidgen) | |
FILENAME="${GUID}.img" | |
FILEPATH="/tmp/${FILENAME}" | |
SIZE_IN_GB=${SIZE%G} # Extrahiert die Zahlen aus der Größenangabe, entfernt 'G' | |
# Convert GB to MB for dd command | |
COUNT=$((SIZE_IN_GB * 1024)) | |
# Create a file of the desired size | |
dd if=/dev/zero of="$FILEPATH" bs=1M count=$COUNT status=none | |
# 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" | |
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 for simplicity | |
# Inform the user about the created device | |
echo "To detach, run: sudo losetup -d $LOOPDEVICE" | |
} | |
# Main logic | |
case "$1" in | |
--help) | |
show_help | |
;; | |
automount) | |
automount | |
;; | |
*) | |
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