From ad34461d2722d019a12b81c1e4d6654dbceae95b Mon Sep 17 00:00:00 2001 From: grabowski Date: Mon, 27 Oct 2025 13:23:25 +0700 Subject: [PATCH] Improve setup_usb.sh with automatic fstab updates MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Added automatic /etc/fstab modification option - Creates timestamped backup before modifying - Checks if UUID entries already exist - Appends new entries only if not present - Provides clear feedback for each step - Enhanced user prompts with better guidance - Added test instructions after fstab update Previously the script only displayed the fstab entries for manual addition. Now it can automatically add them with a single confirmation. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- setup_usb.sh | 71 +++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 56 insertions(+), 15 deletions(-) diff --git a/setup_usb.sh b/setup_usb.sh index 4312efb..2d8b3c2 100644 --- a/setup_usb.sh +++ b/setup_usb.sh @@ -104,31 +104,72 @@ ls -la /media/usb0 /media/usb1 2>/dev/null | grep -E "^d|^total" || echo "No mou echo "" # Offer to add to fstab -read -p "Add these mounts to /etc/fstab for automatic mounting? (y/n) " -n 1 -r -echo "" +FSTAB_ENTRIES="" -if [[ $REPLY =~ ^[Yy]$ ]]; then +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 "Add the following lines to /etc/fstab:" + echo "Suggested /etc/fstab entries:" + echo "" + echo "$FSTAB_ENTRIES" + + read -p "Automatically add these to /etc/fstab? (y/n) " -n 1 -r echo "" - if [ ! -z "$USB0_DEVICE" ]; then - UUID0=$(blkid -s UUID -o value "$USB0_DEVICE") + 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 - echo "UUID=$UUID0 /media/usb0 vfat defaults,nofail,uid=$REAL_UID,gid=$REAL_GID 0 0" + 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 - fi - if [ ! -z "$USB1_DEVICE" ]; then - UUID1=$(blkid -s UUID -o value "$USB1_DEVICE") if [ ! -z "$UUID1" ]; then - echo "UUID=$UUID1 /media/usb1 vfat defaults,nofail,uid=$REAL_UID,gid=$REAL_GID 0 0" + 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 - fi - echo "" - echo "To add automatically, run:" - echo " sudo nano /etc/fstab" + 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 ""