Created
October 22, 2025 13:59
-
-
Save godber/dee320e4b7c578890964bcd32a9c0be2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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" |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.