our goal is to create bash script that delete the unused / unnecessary UUID number/s from /etc/fstab
file ,
brief background - in our labs , we have more then 500 RHEL servers , and we want to fix the fstab
files that have incorrect fstab
configuration as unused UUID number/s or unused UUID number/s that are in Comment lines , etc
we create the following bash script as example.
#!/bin/bash
blkid_list_of_uuid=blkid | awk -F'UUID=' '{print $2}' | awk '{print $1}' | sed s'/"/ /g'
grep UUID /etc/fstab >/tmp/fstab
while read line_from_fstab
do
echo "checking if ${line_from_fstab} is unused UUID"
if [[ ! ${line_from_fstab} =~ $blkid_list_of_uuid ]]
then
#sed -i "/$line_from_fstab/d" /etc/fstab
echo "delete unused line ${line_from_fstab} from fstab"
fi
done < /tmp/fstab
we captured the blkid
number in blkid_list_of_uuid
variable. and filter the UUID lines from fstab into /tmp/fstab
file
the target of the if syntax - [[ ! ${line_from_fstab} =~ $blkid_list_of_uuid ]]
is to delete by sed ( for now in comment ) the UUID lines in /etc/fstab
that are not in used
but the regex isn't working, and script actually delete the UUID that are in used
example of blkid
blkid
/dev/mapper/vg-VOL_root: UUID="49232c87-6c49-411d-b744-c6c847cfd8ec" TYPE="xfs"
/dev/sda2: UUID="Y5MbyB-C5NN-hcPA-wd9R-jmdI-02ML-W9qIiu" TYPE="LVM2_member"
/dev/sda1: UUID="0d5c6164-bb9b-43f4-aa9b-092069801a1b" TYPE="xfs"
/dev/mapper/vg-VOL_swap: UUID="81140364-4b8e-412c-b909-ef0432162a45" TYPE="swap"
/dev/mapper/vg-VOL_var: UUID="e1574eeb-5a78-4a52-b7e3-c53e2b8a4220" TYPE="xfs"
/dev/sdb: UUID="547977e2-a899-4a75-a31c-e362195c264c" TYPE="ext4"
/dev/mapper/vg-VOL_docker: UUID="2e1a2cbf-9920-4e54-8b6b-86d0482c5f7b" TYPE="xfs"
/dev/sdc: UUID="1a289232-0cfe-4df7-9ad5-6a6e2362a1c5" TYPE="ext4"
/dev/sdd: UUID="91493d1f-ffe9-4f5f-aa6d-586d2c99f029" TYPE="ext4"
/dev/sde: UUID="f11845e7-1dcb-4b81-a1d4-9a5fe7da6240" TYPE="ext4"
blkid
and contents of/tmp/fstab
before the script runs) and expected output (contents of/tmp/fstab
after the script runs) so we can help you come up with the best solution. With what you have in your question right now we'd just be guessing at what that might be. – Ed Morton Oct 27 '23 at 12:47blkid
is 1/3 of what we need to give you the best answer and be able to test a potential solution, see my previous comment for what else we need (but I see now it's/etc/fstab
rather than/tmp/fstab
). – Ed Morton Oct 27 '23 at 15:22genfstab -U /
? It would be a lot simpler than trying to parse, modify and retain the original file without breaking anything. (Backup/Rename the original files. Check a few at random or where the size difference is the largest.) – frostschutz Oct 28 '23 at 09:00