Mr Lazy

November 18, 2009

Auto remount of WDTV network shares on NAS startup

Filed under: NFS,WD TV — Mr Lazy @ 17:08
Tags: , , , , , , , ,

You don’t always want a NAS to be on 24×7 – some NASs have a scheduled power-off/on feature.  Unfortunately when I enabled this feature on my Synology DiskStation I discovered that the WD TV would produce a ‘Folder is empty’ message for the NFS shares after the NAS had restarted.  Playing around with the NFS mount parameters did not produce a solution.  Here’s my workaround…

The below procedure will install OpenSSH* on the WDTV via optware, set up SSH between the Synology DiskStation and the WDTV and run a script on the NAS to connect to the WDTV and re-mount the NFS shares.  A basic knowledge of Linux (ie. navigation via CLI, editing text files etc.) is required.  Although written for the DiskStation, it should work with any NAS that runs an embedded Linux and supports NFS and SSH.  I’ve tested it on ext3-boot, and hopefully it will work on WDLXTV with perhaps a little tweaking of file locations necessary.

1. first you need to ensure you have optware installed on your WDTV.  Available from: http://b-rad.cc/optware-for-wdtv

2. install OpenSSH:

# /opt/bin/ipkg update # /opt/bin/ipkg install openssh

3. now we need to set up SSH.  On the NAS, log in as root:

DiskStation> ssh-keygen Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is: c5:49:8a:39:a2:06:19:c4:26:2c:f7:a4:1a:81:ce:a5 root@DiskStation
The key's randomart image is:
+--[ RSA 2048]----+
|*.        .      |
|=*...  o + .     |
|Ooo+. + . +      |
|.E.... . .       |
| oo     S        |
|..               |
|                 |
|                 |
|                 |
+-----------------+

Obviously your fingerprint and randomart image will be different, but you can ignore these anyway.

4. on the WDTV log in as root.  Unless you’ve changed it, the home directory of the root user on ext3-boot will be ‘/’, so that’s what I’ll assume here.  Create a hidden directory called .ssh below ‘/’, set the necessary permissions and change to the new directory:

# cd
# mkdir .ssh
# chmod og= .ssh # cd .ssh

5. now you need to get the public key that was generated on the NAS over to the WDTV.  The public key was stored in the file /root/.ssh/id_rsa.pub on the NAS.  On the WDTV the public key needs to be stored in the file ‘authorized_keys’ in the .ssh directory.  There are several ways of doing this.  The most reliable is to copy the file to a network share that the WDTV has access to, then use the following command:

# mv /pathtoshare/id_rsa.pub /root/.ssh/authorized_keys

Alternatively a crude but simple method is just to use the Windows clipboard.  On the NAS type:

DiskStation> cat /root/.ssh/id_rsa.pub

and select the output (from the first ‘s’ of ‘ssh-rsa’ to the last character ie. the ‘n’ of ‘DiskStation’, but not beyond) with your mouse to copy it to the Windows clipboard.  Back on the WDTV, type echo, then space, then paste (right-click) followed by a ‘>’ followed by ‘authorized_keys’.  So the full command should look something like this:

# echo ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA0rPE8n6R/rslffzIPZL3KNmIXL7Hn3176hJkL[SNIP]+wGOQ== root@DiskStation > authorized_keys

your public key will be a lot longer than this, I’ve shortened it for the sake of clarity.  I’ve also added spaces either side of the ‘>’ but this is not necessary.

6. now we need to set the correct permissions on the authorized_keys file:

# chmod og= authorized_keys

7. you should now be able to connect from the NAS to the WDTV without entering a username and password.  Try it by typing:

DiskStation> ssh root@wdtv.lan

8. now we need to set up some scripts on the WDTV to do some unmounting and mounting.  Type mount -t nfs.  Here are some of my NFS mounts:

192.168.1.7:/volume1/OurMusic on /tmp/media/usb/USB1/OurMusic type nfs (rw,v3,rsize=8192,wsize=16384,hard,lock,proto=udp,addr=192.168.1.7)
192.168.1.7:/volume1/OurMusic on /tmp/media/usb/OurMusic type nfs (rw,v3,rsize=8192,wsize=16384,hard,lock,proto=udp,addr=192.168.1.7)
192.168.1.7:/volume1/OurMusic on /tmp/media/usb/USB1/8AA6-71D0/OurMusic type nfs (rw,v3,rsize=8192,wsize=16384,hard,lock,proto=udp,addr=192.168.1.7)
192.168.1.7:/volume1/DVD on /tmp/media/usb/USB1/DVD type nfs (rw,v3,rsize=8192,wsize=16384,hard,lock,proto=udp,addr=192.168.1.7)
192.168.1.7:/volume1/DVD on /tmp/media/usb/DVD type nfs (rw,v3,rsize=8192,wsize=16384,hard,lock,proto=udp,addr=192.168.1.7)
192.168.1.7:/volume1/DVD on /tmp/media/usb/USB1/8AA6-71D0/dvd type nfs (rw,v3,rsize=8192,wsize=16384,hard,lock,proto=udp,addr=192.168.1.7)

the items in bold are the mount points and these are what will need to be unmounted by our script.  So, I created a file called ‘unmount’ in /usr/bin that looks like this:

#!/bin/sh
umount /tmp/media/usb/USB1/DVD
umount /tmp/media/usb/DVD
umount /tmp/media/usb/USB1/8AA6-71D0/DVD
umount /tmp/media/usb/USB1/OurMusic
umount /tmp/media/usb/OurMusic
umount /tmp/media/usb/USB1/8AA6-71D0/OurMusic

notice the ‘#!/bin/sh’ at the top.  In very basic terms, this tells Linux that it’s a shell script.  The umount command does accept wildcards, but I found that this was unreliable as a) it tried to unmount files that were on my USB stick and b) if it tried to un-mount a sub-directory it would give a ‘resource busy’ error.  Of course, there will be a way to automate this, but for the sake of typing a few lines and using copy/paste I don’t see the point.

9. there’s no point creating a new script to mount the NFS shares again, because we already have one – it’s called net.mounts.  Turn the file into a script by adding ‘#!/bin/sh’ as the first line.

10. I created a script called ‘remount’ in the /usr/bin directory which looks like this:

#!/bin/sh
/usr/bin/unmount
/boot/net.mounts

of course, I could have just put the umount commands in this script, but I thought it would be useful to be able to unmount all the NFS shares at any time without necessarily wanting to mount them again.

11. set the necessary permissions on the scripts:

# chmod 755 remount unmount
# chmod 755 /boot/net.mounts
# ls -l remount unmount
-rwxr-xr-x    1 root     root           73 Nov 17 21:19 remount
-rwxr-xr-x    1 root     root          450 Oct 14 15:58 unmount
# ls -l /boot/net.mounts
-rwxr-xr-x    1 root     root         1050 Nov  9 20:57 /boot/net.mounts

12. as always, test that the scripts work by running them. For example, you should be able to just type ‘remount’ regardless of which directory you are currently in

13. if all is well, now test things from the NAS.  Type:

DiskStation> ssh root@wdtv.lan /usr/bin/remount

and check from the WDTV to watch your shares disappear from ‘mount -t nfs’ and reappear.

14. now we need to add a script to the startup of the NAS.  Go to /usr/syno/etc/rc.d and choose an appropriate name for your script.  I started mine ‘S99…’ as I figured it needed to start after most of the others services (ie. NFS, SSH) had started.  Don’t forget to add a ‘.sh’ suffix otherwise it won’t get started by the DiskStation boot process.  Just two lines:

#!/bin/sh
ssh root@wdtv.lan /usr/bin/remount

15. remember to set the correct permissions:

DiskStation> chmod 755 S99myscript.sh
DiskStation> ls -l S99myscript.sh
-rwxr-xr-x    1 root     root          105 Nov 17 16:53 S99myscript.sh

16. and that’s it!  Test that it works by going into DSM and selecting Shutdown -> Restart.

If any WDLXTV users try this please drop me a note to let me know what you had to change, many thanks.

Now all I need is for Synology to allow users to disable the annoying startup beep :-)

*You could try using the dropbear SSH server if you want – this is pre-installed with ext3-boot or you can download the app.bin for WDLXTV.  I couldn’t get it to work, it wouldn’t recognise the correct permissions on root’s home directory even when I moved it to one level below ‘/’, but I’m now wondering if I needed to generate new host keys – I didn’t bother trying again.  The only advantage of using dropbear is if you’re running loads of apps as it takes up less space than OpenSSH.  The advantage of OpenSSH is that it’s the same as what’s running on the Synology so no worries about compatibility plus the commands are the same on both systems.

About these ads

3 Comments »

  1. Good stuf.. I am an owner of a WDTV and am thinking of purchasing a ds110j. However your text, in my browser (firefox3.6, on winxp pro) is too small, and the code text is almost unreadable. What would be the disadvantage to making all the text a couple of sizes larger?. Remember a greying audience has eyes that need all the help they can get. I realize a seventeen inch monitor at 1280x 1025 doesn’t really cut it any more, but I can only afford to upgrade one thing at a time, LOL.
    I think 17 is still the most used size .. plus, think of all those laptop users?….

    Comment by Ron b — June 17, 2010 @ 00:30

  2. Ron, thanks for your feedback, mostly it’s down to the wordpress theme. I eventually chose this one because it was 2-column with a wide main column that allowed most code text to fit without being wrapped. Unfortunately some code text extends beyond the RH edge of the column but that can’t be helped. If I made the text bigger (not even sure you can without using headings, and that might upset other things) this problem would be worse. Try using the zoom facility in your browser, which zooms the whole page or you can just change the text size from medium to large. I just had a look using my work monitor which is 17″ 1280×1024 and I would say it looks fine although I agree the code text is not the easiest to read but I think that’s mostly because it has to be fixed width and it’s not the easiest font to read anyway.

    Comment by Mr Lazy — June 17, 2010 @ 09:17

  3. Nice article. Cool blog you have here. Thank you

    Comment by Brad MccormackBrad Mccormack — March 10, 2012 @ 02:16


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

The Rubric Theme. Create a free website or blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: