Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using sed to change /etc/fstab

Tags:

unix

sed

I would like to change /etc/fstab inside a script. I want to add the aclattribute to the root partition.

One fstabline 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?

like image 591
Razer Avatar asked Feb 02 '12 18:02

Razer


2 Answers

Who needs some 3rd party tool when we all have awk?

awk '$2~"^/$"{$4="acl,"$4}1' OFS="\t" /etc/fstab

Example Output

$ 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

Explanation

  1. $2~"^/$" Search the 2nd field $2 to see if it matches a forward slash by itself ^/$
  2. {$4="acl,"$4} If we see a match, prepend acl to the beginning of the 4th field $4
  3. }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)
  4. OFS="\t" Set the Output Field Separator OFS to a tab \t. The default is space
  5. /etc/fstab The file we want to use as input
like image 81
SiegeX Avatar answered Sep 28 '22 15:09

SiegeX


I 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

like image 37
eleanor Avatar answered Sep 28 '22 16:09

eleanor