augeas and appending lines to /etc/fstab

Ok, now that’s a specific title 🙂

The reason i’m writing this is because I googled for what appeared to be an eternity and could only find people re-hashing other peoples blog posts that really didn’t help at all.  ps. re-blogging is not the same as re-tweeting and is best avoided.

In this case I wanted to use augeas to append an entry to /etc/fstab.

[root@cobbler manifests]# augtool
augtool> print /files/etc/fstab
/files/etc/fstab/1/spec = “LABEL=/”
/files/etc/fstab/1/file = “/”
/files/etc/fstab/1/vfstype = “ext3”
/files/etc/fstab/1/opt = “defaults”
/files/etc/fstab/1/dump = “1”
/files/etc/fstab/1/passno = “1”
/files/etc/fstab/2/spec = “LABEL=/boot”
/files/etc/fstab/7/spec = “LABEL=SWAP-vda3”
/files/etc/fstab/7/file = “swap”
/files/etc/fstab/7/vfstype = “swap”
/files/etc/fstab/7/opt = “defaults”
/files/etc/fstab/7/dump = “0”
/files/etc/fstab/7/passno = “0”
Now I wanted to add a NFS entry and since /etc/fstab has line numbers (from an augeas perspective) rather than arrays you have to use a little hack to add the entry.
If you specify a line number that exists then you will replace that entry, however if you specify a line number that doesn’t exist then the line is appended.  So how can you be sure the line number doesn’t exist ?  – well there’s the hack – just put a 0 in front of it and it will never exist 🙂
augtool> set /files/etc/fstab/01/spec “”
augtool> set /files/etc/fstab/01/file “/srv/vs”
augtool> set /files/etc/fstab/01/vfstype “nfs”
augtool> set /files/etc/fstab/01/opt[1] “soft”
augtool> set /files/etc/fstab/01/opt[2] “intr”
augtool> set /files/etc/fstab/01/opt[3] “rsize=8192”
augtool> set /files/etc/fstab/01/opt[4] “wsize=8192”
augtool> set /files/etc/fstab/01/opt[5] “nosuid”
augtool> set /files/etc/fstab/01/dump “0”
augtool> set /files/etc/fstab/01/passno “0”
augtool> save
Saved 1 file(s)
[root@cobbler manifests]# cat /etc/fstab
LABEL=/                 /                       ext3    defaults        1 1
. /srv/vs nfs soft,intr,rsize=8192,wsize=8192,nosuid 0 0
So there you go.   Why the hell was I doing this?   Well it was part of a puppet deployment so it was actually part of a puppet class that looks like…
augeas{ “vs mount” :
context => “/files/etc/fstab”,
changes => [
“set 01/spec”,
“set 01/file /srv/vs”,
“set 01/vfstype nfs”,
“set 01/opt[1] soft”,
“set 01/opt[2] intr”,
“set 01/opt[3] rsize=8192”,
“set 01/opt[4] wsize=8192”,
“set 01/opt[5] nosuid”,
“set 01/dump 0”,
“set 01/passno 0”,
onlyif => “match *[spec=’’] size == 0”,
I’m an augeas newbie but given how much I had to search to come even close to the above item may mean this post is of use to someone else.  If it’s already common knowledge then no harm done 🙂

9 thoughts on “augeas and appending lines to /etc/fstab

  1. Hi,

    great post! Exactly what I was looking for. Thank you…

  2. Yeah I don’t know if I would call that a hack per se since augeas has a proper way of doing that. You can use the last() function to refer to the last node and you can do arithmetic on it to so you can call last()+1 to refer to a node and it will refer to a new node . Also you can use the ins command. You should read around the wiki on and the man page for augtool as well as the help command.

  3. @jetole – Can you provide an example of how last() would work in this case? I’m unable to get it to work, but I wouldn’t be surprised if I’m missing the proper method.

    FWIW, using 01 is at least tacitly endorsed in the Augeas Quick Tour:

  4. To use the last function, you have to modify the fstab.aug file.

    Replace the line :
    ‘let record = [ seq “mntent” .’
    ‘let record = [ label “mntent” .’

    Now you can add a line to the fstab with :

    set mntent[last()+1]/spec :
    set mntent[last()]/file
    set mntent[last()]/vfstype

  5. Thanks for your writeup. I needed to do almost this exact same thing, and this will save me quite a bit of time.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s