Skip to content

Instantly share code, notes, and snippets.

@godber
Created October 22, 2025 13:59
Show Gist options
  • Select an option

  • Save godber/dee320e4b7c578890964bcd32a9c0be2 to your computer and use it in GitHub Desktop.

Select an option

Save godber/dee320e4b7c578890964bcd32a9c0be2 to your computer and use it in GitHub Desktop.
#!/bin/bash
# --- fstab Line Generator ---
# Usage: ./fstaby.sh <block_device> <mount_point>
# Example: ./fstaby.sh /dev/sdb1 /srv
# --- Configuration ---
FSTAB_OPTIONS="defaults" # Common options: rw, suid, dev, exec, auto, nouser, async
DUMP_FIELD="0" # Disable backup utility (dump)
FSCK_FIELD="0" # Disable file system check at boot (fsck)
# ---------------------
# 1. Check for required arguments
if [ -z "$1" ] || [ -z "$2" ]; then
echo "Error: Missing arguments."
echo "Usage: $0 <block_device> <mount_point>" >&2
echo "Example: $0 /dev/sdb1 /mnt/data" >&2
exit 1
fi
DEVICE="$1"
MOUNTPOINT="$2"
# 2. Validate device existence
if [ ! -b "$DEVICE" ]; then
echo "Error: Block device '$DEVICE' does not exist." >&2
exit 1
fi
# 3. Use blkid to extract UUID and FSTYPE
# -o export provides key=value format for easy parsing
DEVICE_INFO=$(sudo blkid -o export "$DEVICE" 2>/dev/null)
UUID=$(echo "$DEVICE_INFO" | grep -E "^UUID=" | cut -d'=' -f2 | tr -d '"')
FSTYPE=$(echo "$DEVICE_INFO" | grep -E "^TYPE=" | cut -d'=' -f2 | tr -d '"')
# 4. Final check and output
if [ -z "$UUID" ] || [ -z "$FSTYPE" ]; then
echo "Error: Could not retrieve UUID or FSTYPE for $DEVICE. Is the device formatted?" >&2
exit 1
fi
# Print the complete, formatted fstab line to stdout
echo "UUID=$UUID $MOUNTPOINT $FSTYPE $FSTAB_OPTIONS $DUMP_FIELD $FSCK_FIELD"
@godber
Copy link
Author

godber commented Oct 23, 2025

This is dumb, I know, but I got tired of writing fstab entries, this probably has numerous unconsidered edge cases, but simple cases might be handy. Apologies to the world for being this lazy.

Maybe those options should be configurable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment