Skip to content

Instantly share code, notes, and snippets.

@LMBernardo
Created June 22, 2021 08:40
Show Gist options
  • Save LMBernardo/495c6a22272c10621bcf765a066a3e55 to your computer and use it in GitHub Desktop.
Save LMBernardo/495c6a22272c10621bcf765a066a3e55 to your computer and use it in GitHub Desktop.
Mount all partitions on a drive in folders corresponding to the partition, e.g., /mnt/sda/sda1
#!/bin/bash
if [ "$EUID" -ne 0 ]
then echo "Please run as root"; exit
fi
if [ -z "$1" ]; then
echo "The syntax is 'mountall.sh device_path [mount_path]'"; exit
fi
if ! [[ $1 =~ /dev/.* ]]; then
echo "Expected device path to start with '/dev/'. Exiting."; exit
fi
if [[ $1 =~ /dev/.*/ ]]; then
echo "Expected file device, not a directory. Exiting."; exit
fi
# Check if mount path is unset OR empty
if [ -z "${2+x}" ]; then
MOUNTPATH='/mnt'
else
MOUNTPATH="$2"
fi
DEV=$(echo $1 | sed 's/.*\///')
echo 'Mounting partitions...'
SKIPCLOBBERWARNING=0
for PARTITION in "$1"*
do
# Skip device (e.g., '/dev/sda')
if ! [[ $PARTITION =~ .*[0-9]+ ]]; then continue; fi
# Get device name (e.g., 'sda1')
PART=$(echo $PARTITION | sed 's/.*\///')
# Check if mount path already exists
if [ -d "$MOUNTPATH/$DEV/$PART" ] && [ "$SKIPCLOBBERWARNING" != 1 ]; then
echo "Mount path $MOUNTPATH/$DEV/$PART exists!";
echo "Type 'yes' to overwrite, 'all' to overwrite and ignore future warnings, or anything else to exit.";
read CLOBBERDIR;
if [ "$CLOBBERDIR" == 'all' ]; then
SKIPCLOBBERWARNING=1
else
if [ "$CLOBBERDIR" != 'yes' ]; then exit; fi;
fi
fi
echo "Mounting partition $PART at $MOUNTPATH/$DEV/$PART"
mkdir -p "$MOUNTPATH/$DEV/$PART"
mount $PARTITION "$MOUNTPATH/$DEV/$PART"
done
echo 'Done'
#!/bin/bash
if [ "$EUID" -ne 0 ]
then echo "Please run as root"
exit
fi
if [ -z "$1" ]; then
echo "The syntax is 'umountall.sh device_path'"; exit
fi
if ! [[ $1 =~ /dev/.* ]]; then
echo "Expected device path to start with '/dev/'. Exiting.";
exit
fi
if [[ $1 =~ /dev/.*/ ]]; then
echo "Expected file device, not a directory. Exiting.";
exit
fi
DEV=$(echo $1 | sed 's/.*\///')
echo 'Unmounting partitions...'
for PARTITION in "$1"*
do
if ! [[ $PARTITION =~ .*[0-9]+ ]]; then
continue;
fi
PART=$(echo $PARTITION | sed 's/.*\///')
echo "Unmounting partition $PART"
umount $PARTITION
done
echo 'Done'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment