Beautified output of createFstab function

This commit is contained in:
Krizsán Péter 2024-11-23 19:43:48 +01:00
parent 55d4a3a4cc
commit 0c7e499af6

View File

@ -49,34 +49,75 @@ createFstab()
DIR_BOOT="/boot " DIR_BOOT="/boot "
fi fi
# Print out the header part of our fstab local GS=4 # Num of spaces between columns
printf "# Begin /etc/fstab\n\n" local C0N="# "
printf '%-49s%-15s%-8s%-20s%-8s%-10s\n' "# File system (PARTUUID)" "mount-point" "type" "options" "dump" "fsck" local C1N="File system (PARTUUID)"
printf '#%104s\n\n' "order" local C2N="mount-point"
local C3N="type"
local C4N="options"
local C5N="dump"
local C6N="fsck-order"
local C0S=${#C0N}
local C1S=${#C1N}
local C2S=${#C2N}
local C3S=${#C3N}
local C4S=${#C4N}
local C5S=${#C5N}
local C6S=${#C6N}
local PUID local PUID
local FST
local PAT local PAT
local PF="PARTUUID=%-40s%-15s%-8s%-20s%-8s%-10s\n" local FST
local OPT
local DUMP=0
local FSCK=0
local PRI=0 local PRI=0
local VALUES
local IFS=$'\n' local IFS=$'\n'
for L in $(lsblk -l -o MOUNTPOINT,PATH,NAME,PKNAME,PARTUUID,FSTYPE | tail -n +2) ; do for L in $(lsblk -l -o MOUNTPOINT,PATH,NAME,PKNAME,PARTUUID,FSTYPE | tail -n +2) ; do
PUID=$(awk '{print $5}' <<< "$L") PUID=$(awk '{print $5}' <<< "$L")
if [ -z "$PUID" ] ; then if [ -z "$PUID" ] ; then
continue continue
fi fi
PUID="PARTUUID=$PUID"
FST=$(awk '{print $6}' <<< "$L") FST=$(awk '{print $6}' <<< "$L")
PAT=$(awk '{print $2}' <<< "$L" | sed "s|/dev/|/mnt/|g") OPT="defaults"
DUMP=0
FSCK=0
if [[ $L == "$DIR_ROOT"* ]] ; then if [[ $L == "$DIR_ROOT"* ]] ; then
printf "$PF" "$PUID" / "$FST" defaults 1 1 PAT="/"
DUMP=1
FSCK=1
elif [[ $L == "$DIR_BOOT"* ]] ; then elif [[ $L == "$DIR_BOOT"* ]] ; then
printf "$PF" "$PUID" /boot "$FST" noauto,defaults 0 0 PAT="/boot"
OPT="noauto,$OPT"
elif [[ $L == "[SWAP]"* ]] ; then elif [[ $L == "[SWAP]"* ]] ; then
PAT="swap"
PRI=$((PRI+1)) PRI=$((PRI+1))
printf "$PF" "$PUID" swap "$FST" "pri=$PRI" 0 0 OPT="pri=$PRI"
else else
printf "$PF" "$PUID" "$PAT" "$FST" defaults 0 0 PAT=$(awk '{print $2}' <<< "$L" | sed "s|/dev/|/mnt/|g")
fi fi
VALUES="$VALUES$(printf '%s' "$PUID $PAT $FST $OPT $DUMP $FSCK")\n"
if [ ${#PUID} -gt "$C1S" ]; then C1S=${#PUID} ; fi
if [ ${#PAT} -gt "$C2S" ]; then C2S=${#PAT} ; fi
if [ ${#FST} -gt "$C3S" ]; then C3S=${#FST} ; fi
if [ ${#OPT} -gt "$C4S" ]; then C4S=${#OPT} ; fi
if [ ${#DUMP} -gt "$C5S" ]; then C5S=${#DUMP} ; fi
if [ ${#FSCK} -gt "$C6S" ]; then C6S=${#FSCK} ; fi
done
# Print out the header part of our fstab
local FMT="%-$((C1S+GS))s%-$((C2S+GS))s%-$((C3S+GS))s%-$((C4S+GS))s%-$((C5S+GS))s%-$((C6S+GS))s\n"
printf "# Begin /etc/fstab\n\n"
printf "%-${C0S}s$FMT\n" "$C0N" "$C1N" "$C2N" "$C3N" "$C4N" "$C5N" "$C6N"
for V in $(echo -e "$VALUES") ; do
for ((i = 1; i <= C0S; i++)) ; do printf " " ; done
xargs printf "$FMT" <<< "$V"
done done
# Print out the closing line of fstab # Print out the closing line of fstab