Skip to content

Instantly share code, notes, and snippets.

@adde88
Last active July 1, 2024 02:20
Show Gist options
  • Save adde88/3ec6e2fdf2742e4d61e19c76d3fec260 to your computer and use it in GitHub Desktop.
Save adde88/3ec6e2fdf2742e4d61e19c76d3fec260 to your computer and use it in GitHub Desktop.
mount-listed-partitions.sh
#!/usr/bin/env bash
#
# Written by: Andreas Nilsen - <adde88@gmail.com>
#
# This script is custom made to mount my drives, USB drives, and SWAP to their correct directory, due to issues with fstab and systemctl on my system.
# it will attempt to detect any drives that are already mounted correctly and uncorrectly, and then behave accordingly
# it will attempt to fix any NTFS partitions by using 'ntfsfix' on them before mounting them.
# it is meant to be run as a cronjob every minutes, and will ONLY do anything IF it detects that a drive is NOT mounted correctly.
#
# Latest update: 28.June.2024
#
# Check if the current user has root privileges, if not, exit the script as failed.
[ $EUID -ne 0 ] && echo -e "This mount-all partitions script requires root privileges. Run it as root, or with sudo." && exit 2
# Define an associative array of partition UUIDs and their corresponding correct mount locations on this local GNU/Linux file system (Ubuntu 22.04 Jammy (x64 bit) - Kernel: 6.5.0-41-generic
declare -A PARTITION_LIST=(
["50963A88963A6F1A"]="/mnt/C-NVME1"
["01D0E2B08B0A64A0"]="/mnt/D"
["1B01460B16524A91"]="/mnt/E"
["58E598C976FE128F"]="/mnt/G-NVME2"
["6C65142154DD7A0B"]="/mnt/J"
["0CD3316F521AE1C4"]="/mnt/P"
["183C0D75766AE0DE"]="/mnt/O"
["3874ECE574ECA6BA"]="/mnt/W"
["35EA91E17685448C"]="/mnt/X"
["DF4K91E176CA6BAS"]="/mnt/Z"
)
# Un-mount incorrect CD/DVD/BLU-RAY mount-location, Then remove the device to its correct location, specified within the EXTRA_PARTITIONS associative array above.
eject "/dev/sr0" >/dev/null 2>&1
# Tweaking script also for LDM partitions, as they are not mounted correctly by default.
ldmtool create all >/dev/null 2>&1
# Loop through the entire array of partitions UUID, then mount / un-mount all the local drives according to their correct disk partition UUID's mount location, all specified in the associative array above.
for UUID in "${!PARTITION_LIST[@]}"; do
LOCATION="${PARTITION_LIST[$UUID]}"
if ! findmnt --source "UUID=$UUID" --target "$LOCATION" >/dev/null; then
umount "UUID=$UUID" >/dev/null 2>&1
blkid -o value -s TYPE -s PTTYPE -s PARTTYPE -l -t "UUID=$UUID" >/dev/null 2>&1 | grep -q "ntfs" && ntfsfix "UUID=$UUID" >/dev/null 2>&1
mount -o remount,rw uid=1000,gid=1000 "UUID=$UUID" "$LOCATION" >/dev/null 2>&1
fi
done
# Enable SWAP
swapon -a 2>&1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment