#!/bin/bash # # USB Auto-mount Setup using systemd # This creates systemd mount units that auto-mount when USB drives are inserted # set -e # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' NC='\033[0m' # No Color echo "==========================================" echo "USB Auto-mount Setup (systemd)" echo "==========================================" echo "" # Check if running as root if [ "$EUID" -ne 0 ]; then echo -e "${RED}This script needs sudo privileges${NC}" echo "Please run with: sudo ./setup_usb_automount.sh" exit 1 fi # Get the real user (not root when using sudo) REAL_USER=${SUDO_USER:-$USER} REAL_UID=$(id -u $REAL_USER) REAL_GID=$(id -g $REAL_USER) echo "Setting up auto-mount for user: $REAL_USER (UID:$REAL_UID, GID:$REAL_GID)" echo "" # Create mount points echo "Creating mount points..." mkdir -p /media/usb0 /media/usb1 chown $REAL_UID:$REAL_GID /media/usb0 /media/usb1 echo -e "${GREEN}✓ Mount points created${NC}" echo "" # Detect USB devices and get UUIDs echo "Detecting USB devices..." lsblk -f | grep -E "sd[a-z][0-9]|vfat|exfat|ntfs" echo "" # Function to create systemd mount unit create_mount_unit() { local uuid=$1 local mount_point=$2 local unit_name=$(systemd-escape -p --suffix=mount "$mount_point") echo "Creating systemd mount unit: $unit_name" cat > "/etc/systemd/system/$unit_name" < "/etc/systemd/system/$automount_unit" <