1

I have a setup in which I have a Raspberry Pi B+ with Raspbian as file server, sharing a 5 disk usb rack. I don't whant the rack to be on all the time, so I turn it on when I'm going to use it and turn it off when it isn't needed anymore.

So far, I have been using samba to share the disks. Using samba, I can unmount the disks and turn off the rack without problems. When I turn the rack on again, the drives are automatically mounted and the samba shares are available again.

Due poor performance of the samba shares, I wanted to use NFS shares the same way. However, when I try to unmount the shared drives, I always get the "device is busy" error, so I need first to stop the nfs-kernel-server to be able to turn off the rack. That means that when I turn on the rack again, the shares will not be available until I manually restart the nfs-kernel-server again.

So, is there a way to reproduce the same behaviour that with samba (unmount drives and automatically share them when the rack is turned on)?

Alternatively, is there a way to automatically start the nfs-kernel-server when the drives are automounted?

Thank you and best regards.

1 Answer 1

0

Figure out what the device nodes of the drives are; mount and/or ls /dev should give you a clue if you are not sure, but on raspbian the first one is probably /dev/sda and the first partition on it is /dev/sda1.

If there are multiple device nodes for the multiple drives, you probably want to use the last partition on the last drive below instead of sda1.

Create a file in /etc/udev/rules.d. You need root privileges to do this. Add a line:

ACTION=="add", KERNEL=="sda1", RUN+="/usr/local/bin/dowhatever.sh"

Beware to use == and += as indicated, not =. The path for the RUN command can be anything you want but it should be an absolute path regardless. Then in dowhatever.sh:

#!/bin/sh

service nfs-kernel-server restart

Or whatever commands it is you want to run when the disks appear.

Beware that means this command will also be run if the disks are off and you plug in, e.g., a usb stick or something. If that doesn't matter (because whatever it is will just fail), then don't worry about it. I believe you might be able to use UUIDs from the partitions here to get around this, but maybe not -- if you look at the udev docs I think UUID is an ENV thing and those actually aren't available when a device is added. A better idea might be to just check the UUID yourself with blkid in the script.

uuid="abcd-123"
check=$(blkid -o value /dev/sda1 | head -n 1)
if [ -z $check ] || [ $check != $uuid ]; then
     exit 0
fi

"abdb-123" here should be the output from blkid -o value /dev/sda1 | head -n 1 when you know the correct drive is attached (i.e., it's the UUID of the first partition, if it has one). Beware that if you reformat that partition, the UUID will change.

With regard to unmounting, you could create a parallel script in /usr/local/bin that stops the server then umounts the drives. Then you just call that instead of doing the umount yourself.

2
  • Thank you. I will look at using UUID and post the solution if I manage to do it.
    – Saikamur
    Dec 5, 2014 at 7:59
  • I've added the check you could do using the shell and blkid if you want to do that.
    – goldilocks
    Dec 5, 2014 at 14:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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