Skip to content

Instantly share code, notes, and snippets.

@mjf
Last active May 10, 2023 06:37
Show Gist options
  • Save mjf/b0a78821d59e3182b662386c2d5526c6 to your computer and use it in GitHub Desktop.
Save mjf/b0a78821d59e3182b662386c2d5526c6 to your computer and use it in GitHub Desktop.
fstab-uuidify
#! /bin/sh
# fstab-uuidify - change /dev to UUID entries in fstab(5)
# Copyright (C) 2023 Matous Jan Fialka, <https://mjf.cz/>
# Released under the terms of the "MIT" license.
# Usage: fstab2uuid /etc/fstab |tee /etc/fstab.new
oprobe=
if [ $UID -eq 0 ]; then
# extra opts passed to blkid(8) if run as root
oprobe='-p'
fi
squeeze=0
if [ "$1" = '-s' ]; then
squeeze=1
shift
fi
cat $1|
while read dev rest; do
case "$dev" in
'/dev/'?*)
# get dev UUID
uuid=$(
blkid $oprobe -o export "$dev"|
grep -E '^UUID='|
grep -Eo '[0-9a-fA-F-]+$'
# never change letter case here
)
echo "# $dev"
echo "UUID=$uuid $rest"
;;
'UUID='[0-9a-fA-F]*)
# add dev comment for existing UUID= record
uuid=$(
echo "$dev"|
grep -Eo '[0-9a-fA-F-]+$'
)
cdev=$(
# no probing for a non-device
blkid -U "$uuid"
)
echo "# $cdev"
echo "$dev $rest"
;;
*)
# keep the rest as is
echo "$dev $rest"
;;
esac
done |
if [ $squeeze -eq 1 ]; then
sed '/^UUID=/ s/[ \t]\+/ /g'
else
cat
fi
# vi:ft=sh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment