Skip to content

Instantly share code, notes, and snippets.

@sugamkarki
Created January 28, 2025 19:05
Show Gist options
  • Save sugamkarki/4187c1a7b84b2df8edba7cf0e27171c1 to your computer and use it in GitHub Desktop.
Save sugamkarki/4187c1a7b84b2df8edba7cf0e27171c1 to your computer and use it in GitHub Desktop.
Auto Mount SSD Fedora
#!/usr/bin/env bash
# Variables
UUID="d1b955f9-1fce-4139-8024-c424e37707db"
MOUNT_POINT="/mnt/extssd"
FSTAB_FILE="/etc/fstab"
BACKUP_FILE="/etc/fstab.backup"
FILESYSTEM_TYPE="btrfs"
# Check if running as root
if [ "$EUID" -ne 0 ]; then
echo "Please run this script as root (use sudo)."
exit 1
fi
# Step 1: Create the mount point if it doesn't exist
if [ ! -d "$MOUNT_POINT" ]; then
echo "Creating mount point at $MOUNT_POINT..."
mkdir -p "$MOUNT_POINT"
fi
# Step 2: Backup the /etc/fstab file
if [ ! -f "$BACKUP_FILE" ]; then
echo "Backing up $FSTAB_FILE to $BACKUP_FILE..."
cp "$FSTAB_FILE" "$BACKUP_FILE"
else
echo "Backup already exists at $BACKUP_FILE. Skipping backup."
fi
# Step 3: Add the UUID entry to /etc/fstab if not already present
if ! grep -q "$UUID" "$FSTAB_FILE"; then
echo "Adding entry to $FSTAB_FILE..."
echo "UUID=$UUID $MOUNT_POINT $FILESYSTEM_TYPE defaults,nofail 0 2" >> "$FSTAB_FILE"
else
echo "UUID $UUID is already present in $FSTAB_FILE. Skipping entry addition."
fi
# Step 4: Test the /etc/fstab configuration
echo "Testing /etc/fstab configuration..."
if mount -a; then
echo "Mount successful. The drive is mounted at $MOUNT_POINT."
else
echo "Failed to mount. Please check your /etc/fstab for errors."
exit 1
fi
# Final Message
echo "Auto-mount setup completed successfully!"
echo "Reboot your system to verify the changes."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment