Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have a mount point — let it be /media/question — and two possible devices: a physical HDD and a remote NFS folder. Sometimes I plug the device in physically, in other cases I mount it via NFS.

Is there a way to specify both of them in fstab so that executing mount /media/question will preferably choose physical volume, and when it's not available — NFS?

share|improve this question

2 Answers 2

up vote 1 down vote accepted

You need to make a Bash script that check if the physical device exist, if exist mount the device else mount the NFS share.

Something like this :

if [ -d /media/disk ] ; then {
mount /media/disk ...
}
else{ ...

you can put this script in /etc/init.d for running at startup or you can add to cron for check every 5 minutes or another range of time ...

share|improve this answer
    
That basic idea should work, but your example is showing the mount point (/media/disk), and not the physical device (/dev/sd?). Also, if you are checking the physical device, the "-d" check won't work because it isn't a directory. You'd have to use "-e". –  Alex Jun 5 '10 at 21:22
3  
Don't use /dev/sdxy, use the UUID (this is the only information I've found, but it should match other distros as well). You'll find the device files in /dev/disk/by-uuid. –  Marian Jun 5 '10 at 22:37
    
Yep, UUID would be a better way. I didn't know those were referenced there like that. –  Alex Jun 5 '10 at 23:15
    
That's a solution for sure, however, I'm looking for something more common in the way it works :) –  kolypto Jun 6 '10 at 0:07

You could also look at udev if it is installed. This permits actions to take place when a device is added or removed; such as when a network interface comes up. To see this in action look in the files that cover network access in /etc/udev/rules.d/ (called 77-network.rules on my SuSE box). You can define actions when an interface goes up/down:

/etc/udev/rules.d/77-network.rules

# SUBSYSTEM=="net",

ENV{INTERFACE}=="ppp*|ippp*|isdn*|plip*|lo*|irda*|dummy*|ipsec*|tun*|tap*|bond*|br*|vlan*|modem*|dsl*",GOTO="skip_ifup"

SUBSYSTEM=="net", ACTION=="add", RUN+="/sbin/ifup $env{INTERFACE} -o hotplug"

SUBSYSTEM=="net", ACTION=="remove", RUN+="/sbin/ifdown %k -o hotplug"

LABEL="skip_ifup"

(the “ENV(INTERF....” line is used to ignore non-standard network interfaces, if matched it jumps to the corresponding LABEL statement. The SUBSYTEM lines are triggered when the ACTION statement matches – i.e. an interface is ADDed or REMOVed”)

So, if you change the “add/remove” ACTION to call your script, which would call ifup/ifdown then run whatever else you want to after that. .e.g. call “ifup $@” then check interface IP and mount corresponding entries. If the mounts were listed in fstab with the “noauto” option then you only need a simple naming convention to locate the entry and issue a mount.

In a similar fashion the script /etc/udev/rules.d/56-idedma.rules covers IDE (or sata drives) and would be run when the drives are detected. This script would allow local mounts to be initiated based on presence, using an alternate RUN script.

The naming and ordering of rules is similar to that used in the init system. Have a look at the help and rule files for more examples and options.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.