From 67f550b00a82113f1a9a225062481094feabb072 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krizs=C3=A1n=20P=C3=A9ter?= Date: Sun, 24 Nov 2024 23:18:05 +0100 Subject: [PATCH] Renamed file, fixed a bug and applied coding standard --- libs/create_fstab.sh | 104 +++++++++++++++++++++++++++++++++++ libs/functions.sh | 126 ------------------------------------------- 2 files changed, 104 insertions(+), 126 deletions(-) create mode 100644 libs/create_fstab.sh delete mode 100644 libs/functions.sh diff --git a/libs/create_fstab.sh b/libs/create_fstab.sh new file mode 100644 index 0000000..bcfade7 --- /dev/null +++ b/libs/create_fstab.sh @@ -0,0 +1,104 @@ +#!/bin/bash + +# Function that creates an fstab based on the incoming folder. +# If called without a parameter then it creates one for the current running system. +_create_fstab() +{ + # Check if we got a mount point as a parameter + local d_root= + local d_boot= + if [ -n "$1" ] ; then + # In case of an invalid mount point (not existing folder) we return here + if [ ! -d "$1" ] ; then + echo "The given mount point is invalid: $1" + return 1 + fi + + # If a mount point is given then we generate fstab based on that mount point. + d_root="$(realpath -sm "$1")" + d_boot="$(realpath -sm "$1")/boot" + else + # No mount point is given. We generate fstab for the current running system. + d_root="/" + d_boot="/boot" + fi + + local gap_size=4 # Num of spaces between columns + local col_0_name="# " + local col_1_name="File system (PARTUUID)" + local col_2_name="mount-point" + local col_3_name="type" + local col_4_name="options" + local col_5_name="dump" + local col_6_name="fsck-order" + local col_0_size=${#col_0_name} + local col_1_size=${#col_1_name} + local col_2_size=${#col_2_name} + local col_3_size=${#col_3_name} + local col_4_size=${#col_4_name} + local col_5_size=${#col_5_name} + local col_6_size=${#col_6_name} + + local mount_point= + local device_path= + local partition_uuid= + local file_system_type= + local options= + local dump=0 + local fsck=0 + local swap_priority=0 + local values= + local IFS=$'\n' + for line in $(lsblk -P -o MOUNTPOINT,PATH,PARTUUID,FSTYPE | grep -vE '(MOUNTPOINT="")|(PATH="")|(PARTUUID="")|(FSTYPE="")') ; do + mount_point=$(awk '{gsub("MOUNTPOINT=","",$1); print $1}' <<< "$line") + device_path=$(awk '{gsub("PATH=","",$2); print $2}' <<< "$line") + partition_uuid=$(awk '{gsub("PARTUUID=","",$3); print $3}' <<< "$line") + file_system_type=$(awk '{gsub("FSTYPE=","",$4); print $4}' <<< "$line") + + if [ -z "$mount_point" ] || [ -z "$device_path" ] || [ -z "$partition_uuid" ] || [ -z "$file_system_type" ]; then + continue + fi + + partition_uuid="PARTUUID=$partition_uuid" + options="defaults" + dump=0 + fsck=0 + + if [ "$mount_point" == "\"$d_root\"" ] ; then + device_path="/" + dump=1 + fsck=1 + elif [ "$mount_point" == "\"$d_boot\"" ] ; then + device_path="/boot" + options="noauto,$options" + elif [ "$mount_point" == "\"[SWAP]\"" ] ; then + device_path="swap" + swap_priority=$((swap_priority+1)) + options="pri=$swap_priority" + else + device_path=${device_path//\/dev\//\/mnt\/} + fi + + values="$values$partition_uuid $device_path $file_system_type $options $dump $fsck\n" + + if [ ${#partition_uuid} -gt "$col_1_size" ]; then col_1_size=${#partition_uuid} ; fi + if [ ${#device_path} -gt "$col_2_size" ]; then col_2_size=${#device_path} ; fi + if [ ${#file_system_type} -gt "$col_3_size" ]; then col_3_size=${#file_system_type} ; fi + if [ ${#options} -gt "$col_4_size" ]; then col_4_size=${#options} ; fi + if [ ${#dump} -gt "$col_5_size" ]; then col_5_size=${#dump} ; fi + if [ ${#fsck} -gt "$col_6_size" ]; then col_6_size=${#fsck} ; fi + done + + # Print out the header part of our fstab + local print_format="%-$((col_1_size+gap_size))s%-$((col_2_size+gap_size))s%-$((col_3_size+gap_size))s%-$((col_4_size+gap_size))s%-$((col_5_size+gap_size))s%-$((col_6_size+gap_size))s\n" + printf "# Begin /etc/fstab\n\n" + printf "%-${col_0_size}s$print_format\n" "$col_0_name" "$col_1_name" "$col_2_name" "$col_3_name" "$col_4_name" "$col_5_name" "$col_6_name" + + for value in $(echo -e "$values") ; do + for ((i = 1; i <= col_0_size; i++)) ; do printf " " ; done + xargs printf "$print_format" <<< "$value" + done + + # Print out the closing line of fstab + printf "\n# End /etc/fstab\n" +} \ No newline at end of file diff --git a/libs/functions.sh b/libs/functions.sh deleted file mode 100644 index 077c4c0..0000000 --- a/libs/functions.sh +++ /dev/null @@ -1,126 +0,0 @@ -#!/bin/bash - -# Helper function to pach the packages.ent -# Parameters are in this order: major, minor, patch, md5 and full path to packages.ent -patchKernelVersion() -{ - if [ ! -f "$5" ] ; then - echo "The packages.ent file is not exist or not defined." >&2 - echo "File: $5" - return 4 - fi - - sed -i -E "s@(<\!ENTITY linux-major-version \"+)(.+\">)@\1$1\">@" "$5" - sed -i -E "s@(<\!ENTITY linux-minor-version \"+)(.+\">)@\1$2\">@" "$5" - sed -i -E "s@(<\!ENTITY linux-md5 \"+)(.+\">)@\1$4\">@" "$5" - - if [ "-" == "$3" ] ; then - sed -i -E '//g;}' "$5" - sed -i -E '/linux-minor-version;">-->/{s///g;}' "$5" - sed -i -E '//g;}' "$5" - else - sed -i -E '///g;}' "$5" - sed -i -E "s@(<\!ENTITY linux-patch-version \"+)(.+\">)@\1$3\">@" "$5" - sed -i -E '/linux-patch-version;">-->/{s///g;}' "$5" - sed -i -E '//g;}' "$5" - fi -} - - -# Function that createst an fstab based on the incoming folder -createFstab() -{( - # Check if we got a mount point as a parameter - local DIR_ROOT - local DIR_BOOT - if [ -n "$1" ] ; then - # In case of an invalid mount point (not existing folder) we return here - if [ ! -d "$1" ] ; then - echo "The given mount point is invalid: $1" - return 1 - fi - - # If a mount point is given then we generate fstab based on that mount point. - - DIR_ROOT="$(realpath -sm "$1") " - DIR_BOOT="$(realpath -sm "$1")/boot " - else - # No mount point is given. We generate fstab for the current running system. - DIR_ROOT="/ " - DIR_BOOT="/boot " - fi - - local GS=4 # Num of spaces between columns - local C0N="# " - local C1N="File system (PARTUUID)" - 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 PAT - local FST - local OPT - local DUMP=0 - local FSCK=0 - local PRI=0 - local VALUES - local IFS=$'\n' - for L in $(lsblk -l -o MOUNTPOINT,PATH,NAME,PKNAME,PARTUUID,FSTYPE | tail -n +2) ; do - PUID=$(awk '{print $5}' <<< "$L") - if [ -z "$PUID" ] ; then - continue - fi - PUID="PARTUUID=$PUID" - FST=$(awk '{print $6}' <<< "$L") - OPT="defaults" - DUMP=0 - FSCK=0 - - if [[ $L == "$DIR_ROOT"* ]] ; then - PAT="/" - DUMP=1 - FSCK=1 - elif [[ $L == "$DIR_BOOT"* ]] ; then - PAT="/boot" - OPT="noauto,$OPT" - elif [[ $L == "[SWAP]"* ]] ; then - PAT="swap" - PRI=$((PRI+1)) - OPT="pri=$PRI" - else - PAT=$(awk '{print $2}' <<< "$L" | sed "s|/dev/|/mnt/|g") - 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 - - # Print out the closing line of fstab - printf "\n# End /etc/fstab\n" -)} \ No newline at end of file