Skip to content

Instantly share code, notes, and snippets.

@stuckless
Created September 17, 2023 13:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stuckless/e79e1ad78a995176417930b336141d65 to your computer and use it in GitHub Desktop.
Save stuckless/e79e1ad78a995176417930b336141d65 to your computer and use it in GitHub Desktop.
Simple script to mount .img files in linux so that you can access the contents, copy files, etc.
#!/bin/bash
# Script Title: Loopback Image Mounter
# Description: This script facilitates the mounting of disk image files to a specified loopback device and directory.
# It supports multiple functionalities including listing partitions in the image file, unmounting,
# and flexible mounting options. It is beneficial for users working with disk images, aiding in
# efficiently managing and accessing the contents of these files.
# Collaboration: This script was developed in collaboration between a human and OpenAI's ChatGPT, leveraging
# the AI's coding expertise to implement the specified functionalities and maintain a user-friendly
# interface.
# Default values
PART=p2
MNTDIR=/mnt/loopimage
READONLY=true
# Function to display help information
display_help() {
echo "Usage: $0 [options...]"
echo
echo "Options:"
echo " -i, --img <path> Specify the path to the image file or directory (required)"
echo " -p, --part <partition> Specify the partition (default: p2)"
echo " -m, --mnt <directory> Specify the mount directory (default: /mnt/loopimage)"
echo " -w, --writemode Mount the image in write mode (default: read-only)"
echo " -u, --unmount Unmount the loopback device"
echo " --list List the partitions in the image file"
echo " -?, --help Display this help message"
echo
}
# Process command line options
while getopts "i:p:m:w?u-:" opt; do
case "${opt}" in
i) IMG="${OPTARG}" ;;
p) PART="${OPTARG}" ;;
m) MNTDIR="${OPTARG}" ;;
w) READONLY=false ;;
u) UNMOUNT=true ;;
-)
case "${OPTARG}" in
img) IMG="${!OPTIND}"; OPTIND=$((OPTIND + 1)) ;;
part) PART="${!OPTIND}"; OPTIND=$((OPTIND + 1)) ;;
mnt) MNTDIR="${!OPTIND}"; OPTIND=$((OPTIND + 1)) ;;
writemode) READONLY=false ;;
unmount) UNMOUNT=true ;;
list) LIST_PARTITIONS=true ;;
help) display_help; exit 0 ;;
*) echo "Unknown option --${OPTARG}"; display_help; exit 1 ;;
esac
;;
?) display_help; exit 0 ;;
*) echo "Unknown option -${OPTARG}"; display_help; exit 1 ;;
esac
done
# Verify that the image path has been provided
if [ -z "$IMG" ]; then
echo
echo "Error: You must specify an image path using the -i or --img option."
echo
display_help
exit 1
fi
# Check if the specified image file or directory exists
if [ ! -e "$IMG" ]; then
echo
echo "Error: The specified image path does not exist."
echo
exit 1
fi
# If IMG is a directory, find all .img files in it
if [ -d "$IMG" ]; then
mapfile -t IMG_FILES < <(find "$IMG" -type f -name "*.img")
IMG_COUNT=${#IMG_FILES[@]}
if (( IMG_COUNT == 0 )); then
echo
echo "Error: No .img files found in the specified directory."
echo
exit 1
elif (( IMG_COUNT == 1 )); then
IMG="${IMG_FILES[0]}"
else
echo "Multiple .img files found. Please select one:"
select IMG in "${IMG_FILES[@]}"; do
if [[ -n "$IMG" ]]; then
break
else
echo "Invalid selection. Please try again."
fi
done
fi
fi
# Check if unmount flag is set; if so, unmount and exit
if [ "$UNMOUNT" == "true" ]; then
echo
echo "Unmounting..."
umount "${MNTDIR}"
losetup -d "$(losetup -j "${IMG}" | cut -d ':' -f1)"
exit 0
fi
# If the list partitions flag is set, list the partitions and exit
if [ "$LIST_PARTITIONS" == "true" ]; then
LOOP=$(losetup --show -f -P -L "${IMG}")
echo
echo "Listing partitions for $IMG:"
fdisk -l "${LOOP}"
losetup -d "${LOOP}"
exit 0
fi
# Prepare the mount directory
mkdir -p "$MNTDIR"
# Set up the loop device with the image file
LOOP=$(losetup --show -f -P -L "${IMG}")
# Determine the mount options based on whether read-only or write mode is requested
MNT_OPTIONS="ro"
if [ "$READONLY" == "false" ]; then
MNT_OPTIONS="rw"
fi
# Mount the specified partition of the image file
if ! mount -o ${MNT_OPTIONS} "${LOOP}${PART}" "${MNTDIR}"; then
echo
echo "Error: Failed to mount the image. Check the partition and mount point."
echo
exit 1
fi
echo
echo -e "Listing files for \033[1;32m${IMG}\033[0m:"
ls "${MNTDIR}"
echo
echo -e "Mounted: \033[1;32m${IMG}\033[0m on \033[1;32m${MNTDIR}\033[0m"
echo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment