this blog is about:
- How to configure a Raspberry Pi to automatically mount a NAS drive at boot
- How to enable Wake-On-Lan and to power on a Zyxel NSA310 NAS drive
- How to power off a Zyxel NSA310 NAS drive
- a Python script that sends magic packets and then mounts the device.
- a Python script that unmounts a device and then powers off the device.
WARNING: this article has been verified on a Raspbian Wheezy system only. I assume though it is working on Jessie as well, however it is completely obsolete and possibly won’t work with future Jessie releases.
The default Raspberry Pi storage medium is a SD card. SD cards are reported to have limited lifetime, so I have to be prepared for the moment the SD card fails. Therefore my collected data, or better the entire SD card, need to be backed up frequently. Since I operate a Zyxel NSA310 as network backup storage in my home network, I want my Raspberry Pi’s to backup their data to this drive.
From a price/performance point of view, the Zyxel NSA310 is a very interesting device, runs on Linux and primarily serves as my media server (Twonky) and PC backup storage today. Some smart People have extended the NSA310 with loads of tools, for example added a SSH server which on the original firmware is not existing or not enabled. All that knowledge, even how to compile a Debian kernel, can be found here: http://zyxel.nas-central.org/wiki/Category:NSA-310. I have installed the MetaRepository, a rich collection of networking tools, but I haven’t worked with it yet (due to lack of time). There is a package included called “Dropbear”which they call “a lightweight SSH/SCP server and client”. On the FFP stick there is a SSH server as well . Both SSH servers I haven’t tested yet. I am going to use Telnet later in order to shut down the NSA310 whenever I think its appropriate.
Anyway, to backup data on a NAS drive, basically 2 possibilities come into my mind:
- Use FTP
- Integrate the NAS into the Raspberry Pi file system, so the NAS drive looks like an ordinary directory in my home directory.
The latter possibility is presented in this blog post. Its is more a look-up note than a tutorial, as the setup is really straight forward – after one leisure day searching the internet.
Test Supported File Systems
ls -1 /lib/modules/$(uname -r)/kernel/fs
Integrate (‘Mount’) NAS Drive at boot
edit file /etc/fstab
insert line (all in one line):
//IP-of-the -NAS/public home/nas cifs username=USER,password=PASSWORD,uid=pi,gid=debian-transmission 0 0
The folder “public” just indicates a public area (directory) on the NAS where I do not use a user/password identification to store my files. The entry consists of 6 columns, separated by white space:
- the device to be mounted
- the mount point
- the file system (in this case, its ‘cifs’)
- mount options, such as user, password etc. see documentation (need to insert a link)
- defines that the file system is used by ‘dump’ for backups. Should be set to 0
- sequence of file system verification at boot time. Normally set to 0
save file. Reboot. After reboot, the NAS is integrated in the file system and can be found at home/nas.
Dynamically integrate NAS drive
sudo mount -a
Remove NAS drive
sudo umount //ip-of-nas-device/public
df -h | grep 'ip-of-nas-device/public'
ping ip-of-nas-device -c4 | grep 'transmitted'
In my python scripts I use a slightly different ping method. -W2 specifies a timeout of 2 seconds, -c1 specifies that only one packet shall be sent.
ping -c1 -W2 ip-of-nas-device
Wake up (WoL) a Zyxel NSA310 – sending the magic Packet
The normal working scenario would be like this: the NAS is up and running 24/7. When the raspberry Pi is booted, the NAS drive is automatically mounted. However, I may not want to run the NAS all time to save power consumption. So I would like to power on the NAS drive whenever I need to access it.
To wake it up, the NSA310 must be enabled to accept “magic packets” over LAN to wake up. This is done by opening the web GUI for the NSA310 drive (simply type its IP address in a web browser and log in). Then:
- click Maintenance -> Power
- check Enable Wake On Lan
- click Apply
From a Windows PC we can wake up the Zyxel NSA310 with the Wake On Lan GUI. This is only handy for test purposes, because one has to enter the MAC address of the NAS manually, and it is not possible to schedule a WoL event.
On a Linux system, for example a Raspberry Pi, the magic packet can be sent using a perl script called wakeonlan.
sudo apt-get install wakeonlan
This script can be used to power on a device on schedule by defining a cron job. However, there is still one problem to resolve – the NAS drive needs not only to be powered on, it also needs to be mounted on the Raspberry Pi’s file system. Therefore, the tasks are:
- power on the device (send ‘magic packet’),
- wait until it is available on the network and then
- mount device on the local file system.
My Python script sends the magic packet, then pings the device’s anticipated IP address, waits for the ping to respond, and eventually mounts the device into the local file system. The man page of the tool ‘wakeonlan’ states: “The ‘magic packet’ consists of 6 times 0xFF followed by 16 times the hardware address of the NIC“.
Power off a Zyxel NSA310 over LAN
At night usually no access to the NAS drive and its data is required. Therefore I would like to turn it off, typically between midnight and 7:00 am. It is possible through the device’s web GUI to turn off the device at a certain schedule, daily, weekly or monthly. You also could reboot the device, but it is not possible to power it on once it has been shut down. It turns out powering off the device is by far more complicated than sending a wake-on-Lan packet. There is a tool on the Zyxel NSA310 which can be run to power off the device, ‘/sbin/poweroff’. However this has to be run with root rights.
This is what needs to be done:
- install a third party tool to enable SSH or Telnet on the Zyxel Nsa310.
- establish a SSH or Telnet session as root user
- execute the tool /sbin/poweroff
My Python script uses telnet, because there is a telnet library already included in Python; the tasks before the actual telnet session begins are skipped here, this is just boring python code:
import telnetlib # 1. check command line arguments, read device to power off and admin password # 2. compare with /etc/hosts # 3. if device in/etc/hosts, retrieve IP from first column # 4. call df -h and look if the nas drive is mounted # 5. unmount nas drive if it is mounted using 'sudo umount' # 6. now start a Telnet session try: tn = telnetlib.Telnet(ip,23, 5) tn.read_until('login:') # become root tn.write("root\n") tn.read_until('assword:') tn.write(password + "\n") tn.read_until('~ # ') tn.write("whoami\n") tn.read_until('\n') whoami = tn.read_until('\n') # should print "root" tn.read_until('~ # ') #now power off if "root" in whoami: print "Power off now." tn.write("/sbin/poweroff\n") print tn.read_until('~ # ') #close Telnet session tn.close() except Exception as inst: print "some error occured:" print type(inst) print inst.args
Havent’t found any good tutorial about this tool. Installing this is really complicated and requires a good idea about kernel daemons and linux file systems. – I put this on ice. See debian documentation for this tool set.
This could possibly help as well: