From 2ddba3f3e1d2d07685cbc351247786ff651d0799 Mon Sep 17 00:00:00 2001 From: grabowski Date: Mon, 27 Oct 2025 15:33:10 +0700 Subject: [PATCH] Replace multiple USB scripts with single interactive version MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Consolidated all USB setup functionality into one script: - setup_usb.sh (formerly setup_usb_interactive.sh) Removed deprecated scripts: - setup_usb_automount.sh - setup_usb_final.sh - old setup_usb.sh New setup_usb.sh features: - Interactive menu-driven interface - Scans for all connected USB drives automatically - Displays device info (label, size, UUID, filesystem) - Let user select which drives to auto-mount - Custom mount point names (usb0, usb1, backup, etc.) - Creates systemd .mount and .automount units - Handles cleanup and conflict resolution - Proper permissions (uid/gid) - Works with drives plugged in after boot Updated README.md documentation to reflect new workflow. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- README.md | 14 +- setup_usb.sh | 389 +++++++++++++++++++++++++++-------------- setup_usb_automount.sh | 173 ------------------ setup_usb_final.sh | 179 ------------------- 4 files changed, 268 insertions(+), 487 deletions(-) delete mode 100644 setup_usb_automount.sh delete mode 100644 setup_usb_final.sh diff --git a/README.md b/README.md index bb80a70..291d98b 100644 --- a/README.md +++ b/README.md @@ -391,14 +391,16 @@ Automatically backup all recordings and greeting files to USB drives: **Automated Setup (Easiest):** ```bash -# Run the USB setup script +# Run the interactive USB setup script sudo ./setup_usb.sh ``` This interactive script will: -- Detect your USB devices -- Mount them with proper user permissions -- Test write access -- Optionally add to /etc/fstab for auto-mounting +- Scan for all connected USB drives +- Let you select which drives to auto-mount +- Choose custom mount point names (e.g., usb0, usb1, backup) +- Create systemd auto-mount units +- Handle permissions automatically (uid/gid) +- Work even when USB drives are plugged in after boot **Option 1: Mount with user permissions (Recommended)** ```bash @@ -461,7 +463,7 @@ wedding-phone/ ├── test_complete.py # Audio testing script ├── configure_hifiberry.sh # HiFiBerry DAC+ADC setup script ├── install_service.sh # Systemd service installer (interactive) -├── setup_usb.sh # USB drive setup with permissions (interactive) +├── setup_usb.sh # Interactive USB auto-mount setup (systemd) ├── wedding-phone.service # Systemd service file template ├── Makefile # Make commands (start, test, sync, clean) ├── config.example.json # Example configuration (copy to config.json) diff --git a/setup_usb.sh b/setup_usb.sh index 2d8b3c2..f813dce 100644 --- a/setup_usb.sh +++ b/setup_usb.sh @@ -1,7 +1,7 @@ #!/bin/bash # -# USB Drive Setup Script -# Mounts USB drives with proper user permissions for wedding phone backups +# Interactive USB Auto-mount Setup +# Scans for connected USB drives and lets you select which ones to auto-mount # set -e @@ -10,173 +10,304 @@ set -e RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' +BLUE='\033[0;34m' +CYAN='\033[0;36m' NC='\033[0m' # No Color echo "==========================================" -echo "USB Drive Setup for Wedding Phone" +echo "Interactive 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.sh" + echo "Please run with: sudo ./setup_usb_interactive.sh" exit 1 fi -# Get the real user (not root when using sudo) +# Get the real user REAL_USER=${SUDO_USER:-$USER} REAL_UID=$(id -u $REAL_USER) REAL_GID=$(id -g $REAL_USER) -echo "Setting up USB mounts for user: $REAL_USER (UID:$REAL_UID, GID:$REAL_GID)" +echo "Setting up for user: $REAL_USER (UID:$REAL_UID)" echo "" -# Create mount points -echo "Creating mount points..." -mkdir -p /media/usb0 /media/usb1 -echo -e "${GREEN}✓ Mount points created${NC}" +# Step 1: Stop existing systemd units +echo "Step 1: Stopping existing systemd units..." +systemctl stop media-usb*.automount 2>/dev/null || true +systemctl stop media-usb*.mount 2>/dev/null || true +systemctl disable media-usb*.automount 2>/dev/null || true +echo -e "${GREEN}✓ Stopped existing units${NC}" echo "" -# Detect USB devices -echo "Detecting USB devices..." -lsblk -o NAME,SIZE,TYPE,MOUNTPOINT,FSTYPE | grep -E "disk|part" -echo "" - -# Function to mount a USB drive -mount_usb() { - local device=$1 - local mount_point=$2 - - if [ ! -b "$device" ]; then - echo -e "${YELLOW}⚠ Device $device not found, skipping${NC}" - return +# Step 2: Unmount USB drives +echo "Step 2: Unmounting existing USB mounts..." +for mount in /media/usb*; do + if mountpoint -q "$mount" 2>/dev/null; then + umount "$mount" 2>/dev/null || true fi +done +echo -e "${GREEN}✓ Unmounted drives${NC}" +echo "" - # Unmount if already mounted - if mountpoint -q "$mount_point"; then - echo "Unmounting existing mount at $mount_point..." - umount "$mount_point" 2>/dev/null || true - fi +# Step 3: Scan for USB devices +echo "Step 3: Scanning for USB storage devices..." +echo "" - echo "Mounting $device to $mount_point..." - mount -o uid=$REAL_UID,gid=$REAL_GID "$device" "$mount_point" +# Arrays to store device information +declare -a DEVICES +declare -a LABELS +declare -a UUIDS +declare -a SIZES +declare -a FSTYPES - if [ $? -eq 0 ]; then - echo -e "${GREEN}✓ Mounted $device${NC}" +# Scan for removable storage devices +INDEX=0 +while IFS= read -r line; do + # Parse lsblk output: NAME SIZE TYPE FSTYPE UUID LABEL MOUNTPOINT + NAME=$(echo "$line" | awk '{print $1}') + SIZE=$(echo "$line" | awk '{print $2}') + TYPE=$(echo "$line" | awk '{print $3}') + FSTYPE=$(echo "$line" | awk '{print $4}') + UUID=$(echo "$line" | awk '{print $5}') + LABEL=$(echo "$line" | awk '{print $6}') - # Test write permissions - if sudo -u $REAL_USER touch "$mount_point/.test" 2>/dev/null; then - sudo -u $REAL_USER rm "$mount_point/.test" - echo -e "${GREEN}✓ Write test successful${NC}" - else - echo -e "${RED}✗ Write test failed${NC}" + # Only include partitions with filesystem and UUID + if [[ "$TYPE" == "part" ]] && [[ ! -z "$UUID" ]] && [[ ! -z "$FSTYPE" ]]; then + # Check if it's a removable device + DISK_NAME=$(echo "$NAME" | sed 's/[0-9]*$//') + if [ -f "/sys/block/$DISK_NAME/removable" ]; then + REMOVABLE=$(cat "/sys/block/$DISK_NAME/removable") + if [ "$REMOVABLE" == "1" ]; then + DEVICES[$INDEX]="/dev/$NAME" + SIZES[$INDEX]="$SIZE" + FSTYPES[$INDEX]="$FSTYPE" + UUIDS[$INDEX]="$UUID" + LABELS[$INDEX]="${LABEL:--}" + INDEX=$((INDEX + 1)) + fi fi + fi +done < <(lsblk -nlo NAME,SIZE,TYPE,FSTYPE,UUID,LABEL | grep -E "sd[a-z][0-9]|mmcblk[0-9]p[0-9]") - # Show available space - df -h "$mount_point" | tail -n 1 | awk '{print " Free space: " $4}' - else - echo -e "${RED}✗ Failed to mount $device${NC}" +# Display found devices +if [ $INDEX -eq 0 ]; then + echo -e "${RED}✗ No removable USB drives found${NC}" + echo "" + echo "Please ensure USB drives are connected and try again" + exit 1 +fi + +echo -e "${GREEN}✓ Found $INDEX removable USB drive(s)${NC}" +echo "" +echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" +echo -e "${CYAN}Available USB Drives:${NC}" +echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" + +for i in "${!DEVICES[@]}"; do + echo "" + echo -e "${BLUE}[$i]${NC} ${DEVICES[$i]}" + echo " Label: ${LABELS[$i]}" + echo " Size: ${SIZES[$i]}" + echo " Type: ${FSTYPES[$i]}" + echo " UUID: ${UUIDS[$i]}" +done + +echo "" +echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" +echo "" + +# Step 4: Select drives to mount +echo "Step 4: Select drives to auto-mount" +echo "" + +declare -a SELECTED_DEVICES +declare -a SELECTED_UUIDS +declare -a SELECTED_MOUNT_POINTS + +read -p "How many drives do you want to auto-mount? (1-$INDEX): " NUM_DRIVES + +if ! [[ "$NUM_DRIVES" =~ ^[0-9]+$ ]] || [ "$NUM_DRIVES" -lt 1 ] || [ "$NUM_DRIVES" -gt $INDEX ]; then + echo -e "${RED}✗ Invalid number${NC}" + exit 1 +fi + +echo "" + +for ((n=0; n<$NUM_DRIVES; n++)); do + echo -e "${CYAN}Drive $(($n + 1)) of $NUM_DRIVES:${NC}" + + # Select device + while true; do + read -p " Select device number [0-$((INDEX-1))]: " DEV_NUM + if [[ "$DEV_NUM" =~ ^[0-9]+$ ]] && [ "$DEV_NUM" -ge 0 ] && [ "$DEV_NUM" -lt $INDEX ]; then + break + fi + echo -e " ${RED}Invalid selection, try again${NC}" + done + + # Select mount point + read -p " Mount point name (e.g., usb0, usb1, backup): " MOUNT_NAME + MOUNT_POINT="/media/$MOUNT_NAME" + + SELECTED_DEVICES[$n]="${DEVICES[$DEV_NUM]}" + SELECTED_UUIDS[$n]="${UUIDS[$DEV_NUM]}" + SELECTED_MOUNT_POINTS[$n]="$MOUNT_POINT" + + echo -e " ${GREEN}✓ Will mount ${DEVICES[$DEV_NUM]} to $MOUNT_POINT${NC}" + echo "" +done + +# Step 5: Confirm +echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" +echo -e "${CYAN}Configuration Summary:${NC}" +echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" +for i in "${!SELECTED_DEVICES[@]}"; do + echo "" + echo "Drive $(($i + 1)):" + echo " Device: ${SELECTED_DEVICES[$i]}" + echo " UUID: ${SELECTED_UUIDS[$i]}" + echo " Mount Point: ${SELECTED_MOUNT_POINTS[$i]}" +done +echo "" +echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" +echo "" + +read -p "Proceed with setup? (y/n): " -n 1 -r +echo "" + +if ! [[ $REPLY =~ ^[Yy]$ ]]; then + echo "Setup cancelled" + exit 0 +fi + +echo "" + +# Step 6: Disable fstab entries +echo "Step 6: Backing up and disabling conflicting fstab entries..." +cp /etc/fstab /etc/fstab.backup.$(date +%Y%m%d_%H%M%S) + +# Comment out any existing USB mount entries +for mount_point in "${SELECTED_MOUNT_POINTS[@]}"; do + mount_name=$(basename "$mount_point") + sed -i "/$mount_name/s/^[^#]/#DISABLED_BY_SYSTEMD# /" /etc/fstab +done + +echo -e "${GREEN}✓ Fstab backed up and updated${NC}" +echo "" + +# Step 7: Create systemd units +echo "Step 7: Creating systemd mount units..." +echo "" + +create_mount_unit() { + local uuid=$1 + local mount_point=$2 + local fstype=$3 + local unit_name=$(systemd-escape -p --suffix=mount "$mount_point") + + # Determine mount options based on filesystem type + local mount_opts="defaults,nofail,uid=$REAL_UID,gid=$REAL_GID" + if [[ "$fstype" == "vfat" ]] || [[ "$fstype" == "exfat" ]]; then + mount_opts+=",umask=0022" fi - echo "" + cat > "/etc/systemd/system/$unit_name" < "/etc/systemd/system/$automount_unit" </dev/null | grep -E "^d|^total" || echo "No mounts found" -echo "" + systemctl enable "$unit_name" + systemctl start "$unit_name" -# Offer to add to fstab -FSTAB_ENTRIES="" - -if [ ! -z "$USB0_DEVICE" ]; then - UUID0=$(blkid -s UUID -o value "$USB0_DEVICE") - if [ ! -z "$UUID0" ]; then - FSTAB_ENTRIES+="UUID=$UUID0 /media/usb0 vfat defaults,nofail,uid=$REAL_UID,gid=$REAL_GID 0 0"$'\n' - fi -fi - -if [ ! -z "$USB1_DEVICE" ]; then - UUID1=$(blkid -s UUID -o value "$USB1_DEVICE") - if [ ! -z "$UUID1" ]; then - FSTAB_ENTRIES+="UUID=$UUID1 /media/usb1 vfat defaults,nofail,uid=$REAL_UID,gid=$REAL_GID 0 0"$'\n' - fi -fi - -if [ ! -z "$FSTAB_ENTRIES" ]; then - echo "" - echo "Suggested /etc/fstab entries:" - echo "" - echo "$FSTAB_ENTRIES" - - read -p "Automatically add these to /etc/fstab? (y/n) " -n 1 -r - echo "" - - if [[ $REPLY =~ ^[Yy]$ ]]; then - # Backup fstab - cp /etc/fstab /etc/fstab.backup.$(date +%Y%m%d_%H%M%S) - echo -e "${GREEN}✓ Backed up /etc/fstab${NC}" - - # Check if entries already exist - ADDED=0 - if [ ! -z "$UUID0" ]; then - if ! grep -q "$UUID0" /etc/fstab; then - echo "UUID=$UUID0 /media/usb0 vfat defaults,nofail,uid=$REAL_UID,gid=$REAL_GID 0 0" >> /etc/fstab - echo -e "${GREEN}✓ Added USB0 to /etc/fstab${NC}" - ADDED=1 - else - echo -e "${YELLOW}⚠ USB0 already in /etc/fstab${NC}" - fi - fi - - if [ ! -z "$UUID1" ]; then - if ! grep -q "$UUID1" /etc/fstab; then - echo "UUID=$UUID1 /media/usb1 vfat defaults,nofail,uid=$REAL_UID,gid=$REAL_GID 0 0" >> /etc/fstab - echo -e "${GREEN}✓ Added USB1 to /etc/fstab${NC}" - ADDED=1 - else - echo -e "${YELLOW}⚠ USB1 already in /etc/fstab${NC}" - fi - fi - - if [ $ADDED -eq 1 ]; then - echo "" - echo -e "${GREEN}✓ fstab updated successfully${NC}" - echo "Drives will auto-mount on reboot" - echo "" - echo "To test now, run:" - echo " sudo umount /media/usb0 /media/usb1 2>/dev/null" - echo " sudo mount -a" - fi - else - echo "" - echo "To add manually, run:" - echo " sudo nano /etc/fstab" - fi -fi + echo -e "${GREEN}✓ Enabled $mount_point auto-mount${NC}" +done echo "" echo "==========================================" echo "Setup Complete!" echo "==========================================" echo "" -echo "USB drives are now accessible by user: $REAL_USER" -echo "Wedding Phone can now backup to these drives" +echo "USB drives will now auto-mount on access" +echo "" +echo "Test your mounts:" +for mount_point in "${SELECTED_MOUNT_POINTS[@]}"; do + echo " ls $mount_point" +done +echo "" +echo "Check status:" +for mount_point in "${SELECTED_MOUNT_POINTS[@]}"; do + mount_name=$(basename "$mount_point") + unit_name=$(systemd-escape -p --suffix=automount "$mount_point") + echo " systemctl status $unit_name" +done +echo "" +echo "View mounted drives:" +echo " mount | grep media" echo "" diff --git a/setup_usb_automount.sh b/setup_usb_automount.sh deleted file mode 100644 index e07f86e..0000000 --- a/setup_usb_automount.sh +++ /dev/null @@ -1,173 +0,0 @@ -#!/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" </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 (including commented lines) -echo "Step 3: Reading UUIDs from /etc/fstab..." -USB0_UUID=$(grep "usb0" /etc/fstab 2>/dev/null | sed 's/^#[^ ]* //' | grep -oP 'UUID=\K[^\s]+' || echo "") -USB1_UUID=$(grep "usb1" /etc/fstab 2>/dev/null | sed 's/^#[^ ]* //' | 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" <