Add option to fstab


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Add option to fstab
# 1  
Old 01-25-2012
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:
Code:
# /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


Last edited by Franklin52; 01-26-2012 at 03:40 AM.. Reason: Please use code tags for code and data samples, thank you
# 2  
Old 01-25-2012
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):
Code:
$ 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

Code:
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.
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 01-25-2012
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?)
# 4  
Old 01-25-2012
Quote:
Originally Posted by pblumenthal
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
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.

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

Quote:
2) what does the 1 do at the end of the awk command?
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.
Quote:
3) What issues would there be with putting noatime on swap (or any other fs type?)
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.
This User Gave Thanks to Corona688 For This Post:
# 5  
Old 01-25-2012
Code:
-v OFS='\t' works great (not FS='\t')

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


Last edited by Franklin52; 01-26-2012 at 03:40 AM.. Reason: Please use code tags for code and data samples, thank you
# 6  
Old 01-25-2012
I had it backwards, !match($4, "noatime) pardon me
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to add a Y/N option?

I want to be able to add to any script that gives the option to continue or not, after the the first loop thru and to keep giving you the option after the user answers "Y" then does their thing then gives that option again once done. I've been seeing examples online on how to do it, but not for it... (2 Replies)
Discussion started by: bud1738
2 Replies

2. Ubuntu

Add Option To Right Menu

hey all, I already installed nautilus-actions now , I want to add "print path" script(option) to the right context menu!.. I did : http://img853.imageshack.us/img853/6973/59818245.png http://img847.imageshack.us/img847/8758/37217230.png the script print located in... (2 Replies)
Discussion started by: eawedat
2 Replies

3. Shell Programming and Scripting

Interactive script to give user option of what text to add

Hello all, I have created a script that will remove the first two lines of one text file (body.txt) and then add the text from a different text file (header.txt) directly to the beginning of the freshly modified body.txt file. It is as follows: #!/bin/bash ## This script will add a header... (2 Replies)
Discussion started by: marcozd
2 Replies

4. Shell Programming and Scripting

recently introduced to the newer option for find...does an older option exist?

To find all the files in your home directory that have been edited in some way since the last tar file, use this command: find . -newer backup.tar.gz Is anyone familiar with an older solution? looking to identify files older then 15mins across several directories. thanks, manny (2 Replies)
Discussion started by: mr_manny
2 Replies

5. Shell Programming and Scripting

How do I add the option to change the path in a menu?

How do I add the option to change the path in a menu? I have this script. The user chooses a number and had the option of doing something, looking for log files etc. There is a possibility they might want to look at a different path other than what I have given them such as... (2 Replies)
Discussion started by: taekwondo
2 Replies

6. UNIX for Dummies Questions & Answers

How to add the landscape option...

to an existing print que? (1 Reply)
Discussion started by: NycUnxer
1 Replies

7. Shell Programming and Scripting

option followed by : taking next option if argument missing with getopts

Hi all, I am parsing command line options using getopts. The problem is that mandatory argument options following ":" is taking next option as argument if it is not followed by any argument. Below is the script: while getopts :hd:t:s:l:p:f: opt do case "$opt" in -h|-\?)... (2 Replies)
Discussion started by: gurukottur
2 Replies

8. Shell Programming and Scripting

trying to add an option to view before executing?

I am very new to scripting. I have the following script: for i in `ls -1 | grep $1 | grep -v $2` do x=`echo $i | sed 's/\.New/\.Old/g'` echo mv $i DONE/$x done This is taking files from the directory I am in and changing the name from .New to .Old and then echoing the command to move... (2 Replies)
Discussion started by: llsmr777
2 Replies

9. Windows & DOS: Issues & Discussions

How to add Win2000, Win Me option to Lilo menu?

Hi All, I have installed RH Linux 7.1, windows 2000 and windows ME in the same machine. There are two booting menu: Lilo: Linux Dos Dos : Windows2000 WindowsME If I want to use windowME, I need to boot lilo and choose dos and final choose windows ME. Have any... (3 Replies)
Discussion started by: wilsonchan1000
3 Replies
Login or Register to Ask a Question