View Single Post
  #1   (View Single Post)  
Old 15th February 2010
J65nko J65nko is offline
Administrator
 
Join Date: May 2008
Location: Budel - the Netherlands
Posts: 3,975
Default Adding 'noatime' and 'softdep' mount options to '/etc/fstab'

On my current desktop I have the following /etc/fstab:
Code:
/dev/wd0a / ffs rw 1 1
/dev/wd0m /home ffs rw,nodev,nosuid 1 2
/dev/wd0e /home/xx ffs rw,nodev,nosuid 1 2
/dev/wd0d /tmp ffs rw,nodev,nosuid 1 2
/dev/wd0k /usr ffs rw,nodev 1 2
/dev/wd0l /usr/local ffs rw,nodev 1 2
/dev/wd0f /var ffs rw,nodev,nosuid 1 2
/dev/wd0h /var/log ffs rw,nodev,nosuid 1 2
/dev/wd0g /var/tmp ffs rw,nodev,nosuid 1 2
For adding the mount options 'noatime' and 'softdep' I use the following script, which is easily adapted to another disk layout.

Code:
# ---------------------------------------------------------
FILE=/etc/fstab
FILE=$( basename ${FILE} )
ORIG=${FILE}.orig

LABELS=adefghiml
OPTIONS='noatime,softdep'       # no trailing "," after last option !
DISK=wd
NR=0

cat <<END
Enabling mount option(s) "${OPTIONS}" on disk ${DISK}${NR} for label(s): "${LABELS}"
END

cp -p ${FILE} ${ORIG} 
sed -e "/${DISK}[${NR}][${LABELS}]/  s/rw/rw,${OPTIONS}/" ${ORIG} >${FILE}
As you can see, there are no hardcoded values for the disk name and nr, no mention of "wd0" or "wd1".
The same applies to the mount options to be added and the labels. Even the file name and the name of the backup of the original, have been factored out.

As a test we first copy the /etc/fstab to the current directory. Then we run the script with the sh -vx options to check if the variables are being expanded correctly.

Code:
$ cp /etc/fstab . 
$ sh -vx   softdep-noatime-adefghiml
# ---------------------------------------------------------
FILE=/etc/fstab
+ FILE=/etc/fstab
FILE=$( basename ${FILE} )
+ basename /etc/fstab
+ FILE=fstab
ORIG=${FILE}.orig
+ ORIG=fstab.orig

LABELS=adefghiml
+ LABELS=adefghiml
OPTIONS='noatime,softdep'       # no trailing "," after last option !
+ OPTIONS=noatime,softdep
DISK=wd
+ DISK=wd
NR=0
+ NR=0

cat <<END
Enabling mount option(s) "${OPTIONS}" on disk ${DISK}${NR} for label(s): "${LABELS}"
END
+ cat
+ << END 
Enabling mount option(s) "noatime,softdep" on disk wd0 for label(s): "adefghiml"

cp -p ${FILE} ${ORIG} 
+ cp -p fstab fstab.orig
sed -e "/${DISK}[${NR}][${LABELS}]/  s/rw/rw,${OPTIONS}/" ${ORIG} >${FILE}
+ sed -e /wd[0][adefghiml]/  s/rw/rw,noatime,softdep/ fstab.orig
+ > fstab
The result is
Code:
/dev/wd0a / ffs rw,noatime,softdep 1 1
/dev/wd0m /home ffs rw,noatime,softdep,nodev,nosuid 1 2
/dev/wd0e /home/xx ffs rw,noatime,softdep,nodev,nosuid 1 2
/dev/wd0d /tmp ffs rw,noatime,softdep,nodev,nosuid 1 2
/dev/wd0k /usr ffs rw,nodev 1 2
/dev/wd0l /usr/local ffs rw,noatime,softdep,nodev 1 2
/dev/wd0f /var ffs rw,noatime,softdep,nodev,nosuid 1 2
/dev/wd0h /var/log ffs rw,noatime,softdep,nodev,nosuid 1 2
/dev/wd0g /var/tmp ffs rw,noatime,softdep,nodev,nosuid 1 2
A break down of the sed(1) command which accomplishes this result:
Code:
sed -e /wd[0][adefghiml]/  s/rw/rw,noatime,softdep/ fstab.orig >fstab

-e              : the sed command(s) follow in-line

On all lines matching the pattern:

        /wd[0][adefghiml]/

substitute the 'rw' with 'rw,noatime,softdep' 

The  pattern selecting on which lines to do this substitution or replacement:

/               : start of search pattern
w               : a "w" character followed by a
d               : "d" character followed by a
[0]             : the "0' character. In this case the two brackets could
                  have been left out. A 'wd[01]' would match both a 'wd0' 
                  and a 'wd1' string 
                  So a '0' followed by one of the followin charaters between
                  the '[' and ']'
[               : start of the character class
adefghiml       : the  members of this class
]               : end of character class
                  This means that 'wd0a', 'wd0d' and 'wd0e' up to 'wd0l[/b]
                  will match.
/               ; end of search pattern

The 'subsitute' or 'search and replace' operation.     

s               : we do a substitution
/               : delimiter for start of the pattern
rw              : a 'r" followed by a 'w'
/               : end delimiter of search pattern, start delimiter of
                  replacement pattern
rw,             : the string sequence 'rw' followed by a comma, followed 
noatime,        : by the strings 'noatime,' and 
softdep         : the string 'softdep'
/               : delimiter signalling end of replacement pattern
__________________
You don't need to be a genius to debug a pf.conf firewall ruleset, you just need the guts to run tcpdump
Reply With Quote