Add option to fstab

I need as script (awk/sed?) to add noatime option to fstab.
It should append ,noatime to whatever is in column 4 if noatime isn't already there, leaving comments alone.

input:

# /etc/fstab
# Created by anaconda on Mon Oct 31 20:44:41 2011
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/vg_centos60-lv_root /       ext4    defaults        1       1
UUID=d4c12a62-6452-490e-a345-529e76c4c30b       /boot   ext4    defaults        1       2
/dev/mapper/vg_centos60-lv_swap swap    swap    defaults        0       0
tmpfs   /dev/shm        tmpfs   defaults        0       0
devpts  /dev/pts        devpts  gid=5,mode=620  0       0
sysfs   /sys    sysfs   defaults        0       0
proc    /proc   proc    defaults        0       0

I really don't think it'd make sense to add it to 'swap' partitions. In fact, I think you should be very careful what partitions you do add it to. meanwhile, now that you're forewarned, here's what you wanted (except without swap):

$ awk '!/^#/ && ($2 != "swap") { if(!match(/noatime/, $4)) $4=$4",noatime" } 1' fstab

# /etc/fstab
# Created by anaconda on Mon Oct 31 20:44:41 2011
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/vg_centos60-lv_root / ext4 defaults,noatime 1 1
UUID=d4c12a62-6452-490e-a345-529e76c4c30b /boot ext4 defaults,noatime 1 2
/dev/mapper/vg_centos60-lv_swap swap swap defaults 0 0
tmpfs /dev/shm tmpfs defaults,noatime 0 0
devpts /dev/pts devpts gid=5,mode=620,noatime 0 0
sysfs /sys sysfs defaults,noatime 0 0
proc /proc proc defaults,noatime 0 0

$

You'd want to use it like

awk ... /etc/fstab > /tmp/$$
cat /tmp/$$ > /etc/fstab
rm /tmp/$$

And of course, test this thoroughly before letting it replace real data. Messing up your fstab's would really stink.

1 Like

This really good. 3 questions:
1) why does this seems to mess up white space? I think my original fstab is tab delimted, hte result is space delimited
2) what does the 1 do at the end of the awk command?
3) What issues would there be with putting noatime on swap (or any other fs type?)

Because I didn't change awk's output separator, and I had no indication you wanted tabs. It shouldn't matter to mount in any case.

awk -v FS='\t' ...

Prints every line. It could be an expression, too, so /slartibartfast/ instead of 1 would only print lines containing slartibartfast. Or X==1 to only do so when the variable X is set to 1. And so forth. You can leave it off completely, which would cause it to default to false.

Options are filesystem-specific, and if the filesystem doesn't understand them they may fail to mount. 'noatime' in particular makes no sense at all for swap since it doesn't even contain any files at all, so if it'd mess up anywhere, it'd mess up there.

1 Like
-v OFS='\t' works great (not FS='\t')

but if(!match(/noatime/, $4)) doesn't seem to work. I get:

proc    /proc   proc    defaults,noatime,noatime        0       0

I had it backwards, !match($4, "noatime) pardon me