I would like to change /etc/fstab
inside a script. I want to add the acl
attribute to the root partition.
One fstab
line entry looks like this:
UUID=730aee20-52b7-4920-75cd-d0d995ef2445 / ext3 errors=remount-ro 0 1
I want to change it to:
UUID=730aee20-52b7-4920-75cd-d0d995ef2445 / ext3 acl,errors=remount-ro 0 1
I thought:
1. Search line with root partition /
2. insert acl
after /
How can I do that with sed?
Who needs some 3rd party tool when we all have awk
?
awk '$2~"^/$"{$4="acl,"$4}1' OFS="\t" /etc/fstab
$ awk '$2~"^/$"{$4="acl,"$4}1' OFS="\t" /etc/fstab
/dev/sda2 swap swap defaults 0 0
/dev/sda5 / ext4 acl,defaults 1 1
/dev/sda1 /boot ext4 defaults 1 2
/dev/sda6 /home ext4 defaults 1 2
/dev/sdb1 /backup ext4 defaults 1 2
#/dev/cdrom /mnt/cdrom auto noauto,owner,ro 0 0
/dev/fd0 /mnt/floppy auto noauto,owner 0 0
devpts /dev/pts devpts gid=5,mode=620 0 0
proc /proc proc defaults 0 0
tmpfs /dev/shm tmpfs defaults 0 0
$2~"^/$"
Search the 2nd field $2
to see if it matches a forward slash by itself ^/$
{$4="acl,"$4}
If we see a match, prepend acl to the beginning of the 4th field $4
}1
This is an awk
shortcut which is equivalent to print $0
, i.e. print the whole line (including any alterations we may have made)OFS="\t"
Set the Output Field Separator OFS
to a tab \t
. The default is space/etc/fstab
The file we want to use as inputI guess the best way would be to use augeas, which is a great tool. With augeas you can parse configuration files automatically, append/delete/update some field with XPATH, which is used to navigate through the XML.
You can find all the supported conifiguration files that augeas can correctly parse in the following directory:
/usr/share/augeas/lenses/dist/
One of the files is fstab.aug, which is the one you want. You don't actually need to do anything with that files, it's just there to let you know that augeas can do what you want :).
# ls /usr/share/augeas/lenses/dist/fstab.aug
/usr/share/augeas/lenses/dist/fstab.aug
You can also look here for more detailed example obout changing /etc/fstab through augeas.
Augeas Example
I guess you can use something like the following:
# augtool
augtool> set /files/etc/fstab/1[file='/']/opt[1] "acl"
augtool> set /files/etc/fstab/1[file='/']/opt[2] "errors=remount-ro"
augtool> print /files/etc/fstab/1
/files/etc/fstab/1
/files/etc/fstab/1/spec = "/dev/mapper/system"
/files/etc/fstab/1/file = "/"
/files/etc/fstab/1/vfstype = "ext3"
/files/etc/fstab/1/opt[1] = "acl"
/files/etc/fstab/1/opt[2] = "errors=remount-ro"
/files/etc/fstab/1/dump = "0"
/files/etc/fstab/1/passno = "1"
augtool> save
If you want to use augeas in a shell script you can just preced the above commands with augtool keyword, so if you want to change the first 'opt' to 'acl' you would do:
augtool set /files/etc/fstab/1[file='/']/opt[1] "acl"
augtool set /files/etc/fstab/1[file='/']/opt[2] "errors=remount-ro"
augtool save
Look at the original augeas page for more: Augeas Homepage
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With