From 4e46989ff61b66469ab62efeb40025f13bef96ea Mon Sep 17 00:00:00 2001 From: grabowski Date: Mon, 27 Oct 2025 15:15:37 +0700 Subject: [PATCH] Add complete USB auto-mount setup with cleanup MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Created setup_usb_final.sh that handles all cleanup and setup in one automated script to solve mount point conflicts. Features: - Stops and disables existing systemd mount units - Unmounts any active USB mounts - Extracts UUIDs from existing fstab entries - Comments out fstab entries to prevent conflicts - Creates timestamped fstab backup - Creates systemd .mount and .automount units - Enables and starts auto-mount - Proper error handling and user feedback This solves the "Path is already a mount point" error that occurs when trying to set up systemd automount while drives are already mounted via fstab. Usage: sudo ./setup_usb_final.sh 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- setup_usb_final.sh | 179 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 179 insertions(+) create mode 100644 setup_usb_final.sh diff --git a/setup_usb_final.sh b/setup_usb_final.sh new file mode 100644 index 0000000..5cfd988 --- /dev/null +++ b/setup_usb_final.sh @@ -0,0 +1,179 @@ +#!/bin/bash +# +# Complete USB Auto-mount Setup +# Cleans up existing mounts and sets up systemd automount +# + +set -e + +# Colors for output +RED='\033[0;31m' +GREEN='\033[0;32m' +YELLOW='\033[1;33m' +NC='\033[0m' # No Color + +echo "==========================================" +echo "Complete USB Auto-mount Setup" +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_final.sh" + exit 1 +fi + +# Get the real user +REAL_USER=${SUDO_USER:-$USER} +REAL_UID=$(id -u $REAL_USER) +REAL_GID=$(id -g $REAL_USER) + +echo "Setting up for user: $REAL_USER (UID:$REAL_UID)" +echo "" + +# Step 1: Stop existing systemd units +echo "Step 1: Stopping existing systemd units..." +systemctl stop media-usb0.automount 2>/dev/null || true +systemctl stop media-usb1.automount 2>/dev/null || true +systemctl stop media-usb0.mount 2>/dev/null || true +systemctl stop media-usb1.mount 2>/dev/null || true +systemctl disable media-usb0.automount 2>/dev/null || true +systemctl disable media-usb1.automount 2>/dev/null || true +echo -e "${GREEN}✓ Stopped existing units${NC}" +echo "" + +# Step 2: Unmount USB drives +echo "Step 2: Unmounting USB drives..." +umount /media/usb0 2>/dev/null || true +umount /media/usb1 2>/dev/null || true +echo -e "${GREEN}✓ Unmounted drives${NC}" +echo "" + +# Step 3: Extract UUIDs from fstab +echo "Step 3: Reading UUIDs from /etc/fstab..." +USB0_UUID=$(grep "usb0" /etc/fstab 2>/dev/null | grep -v "^#" | grep -oP 'UUID=\K[^\s]+' || echo "") +USB1_UUID=$(grep "usb1" /etc/fstab 2>/dev/null | grep -v "^#" | grep -oP 'UUID=\K[^\s]+' || echo "") + +if [ -z "$USB0_UUID" ] && [ -z "$USB1_UUID" ]; then + echo -e "${YELLOW}⚠ No UUIDs found in fstab${NC}" + echo "" + echo "Please enter UUIDs manually:" + read -p "USB0 UUID (e.g., D20B-AEC0) [Enter to skip]: " USB0_UUID + read -p "USB1 UUID (e.g., E212-4316) [Enter to skip]: " USB1_UUID +else + echo -e "${GREEN}✓ Found USB0 UUID: $USB0_UUID${NC}" + echo -e "${GREEN}✓ Found USB1 UUID: $USB1_UUID${NC}" +fi +echo "" + +# Step 4: Comment out fstab entries to prevent conflicts +echo "Step 4: Disabling fstab entries (creating backup)..." +cp /etc/fstab /etc/fstab.backup.$(date +%Y%m%d_%H%M%S) +sed -i '/UUID=.*usb[01]/s/^/#DISABLED_BY_SYSTEMD# /' /etc/fstab +echo -e "${GREEN}✓ Fstab entries commented out${NC}" +echo "" + +# Step 5: Create mount points +echo "Step 5: 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 "" + +# Step 6: Create systemd mount units +echo "Step 6: Creating systemd units..." + +create_mount_unit() { + local uuid=$1 + local mount_point=$2 + local unit_name=$(systemd-escape -p --suffix=mount "$mount_point") + + cat > "/etc/systemd/system/$unit_name" < "/etc/systemd/system/$automount_unit" <