1

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?

2 Answers 2

1

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 ...

4
  • 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, 2010 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, 2010 at 22:37
  • Yep, UUID would be a better way. I didn't know those were referenced there like that.
    – Alex
    Jun 5, 2010 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, 2010 at 0:07
1

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .