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.

Revisions

  1. godber created this gist Oct 22, 2025.
    44 changes: 44 additions & 0 deletions fstaby.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,44 @@
    #!/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"