Pages

Wednesday, December 5, 2018

Debian and OpenMediaVault on Zyxtel NSA310 using Kirkwood U-Boot and Kernel

I've a Zyxtel NSA310 NAS laying around that sometimes does not boot.
This was an inspiration for me to try installing Debian on this kind of device.
On the NAS central web page you can find a lot of usefull information about this NAS. I'm speaking of the following page: http://zyxel.nas-central.org/wiki/Category:NSA-310


If you are planning to install Debian on a NAS device, a good place to look at is the doozan forum.

This is my brief instruction, that I've extract from the doozan forum, to install Debian and OpenMediaVault to this NAS.

Warning! Updating your NAS following this instructions may brick your device. Everything you do at your own risk.

First of all a quick investigation on this device reports that the booting problems I encountered was caused by nand bad sectorson the kernel_1 mtd.

The process consists of three steps
  1. install the u-boot
  2. install the rootfs - Debian system
  3. setup Debian: install OpenMediaVault
Step 1) install the u-boot and u-boot environment
This is the dangerous part. Updating the mtd0 partition may brick your device.
In order to install Debian on this device, we have to load the kirkwood uboot.
You can download the latest version of the kirkwood uboot for NSA310 from this doozan forum page: https://forum.doozan.com/read.php?3,12381. The one I'm using is uboot.2017.07-tld-1.nsa310.bodhi.tar.
Also download the environemnt file, which is uboot.2016.05-tld-1.environment.bodhi.tar

Remember to check the MD5 value for those files with the one provided at the doozan forum. If they match untar that files.
To makes things simpler rename the uboot to uboot.kwb and environment file to uboot.img
For me that is done with the command:
mv uboot.2017.07-tld-1.nsa310.mtd0.kwb uboot.kwb
mv uboot.2016.05-tld-1.environment.img uboot.img

We are going to install the uboot on the mtd0 partition.
To check your partitions login to the ssh shell of your NSA and run the following command
cat /proc/mtd

The mtd partitions of your NSA310 should look like this:
dev:    size   erasesize  name
mtd0: 00100000 00020000 "uboot"
mtd1: 00080000 00020000 "uboot_env"
mtd2: 00080000 00020000 "key_store"
mtd3: 00080000 00020000 "info"
mtd4: 00a00000 00020000 "etc"
mtd5: 00a00000 00020000 "kernel_1"
mtd6: 02fc0000 00020000 "rootfs1"
mtd7: 00a00000 00020000 "kernel_2"
mtd8: 02fc0000 00020000 "rootfs2"

You should backup your mtd0 partition, just in case.
To dump your partition download the uboot tools from this page https://forum.doozan.com/read.php?3,27280 extract it to a folder of your NAS, it may be a shared one, jump to that folder using your terminal, and run the following commad:
nanddump --noecc --omitoob -l 0x80000 -f mtd0 /dev/mtd0

It should have built the mtd0 file, which is your original mtd0 partition.
You should also save your actual env running:
fw_printenv > current_envs.txt
This time the current_envs.txt file is the environments variables content.

Now that you have dumped your actual configuration, before loading the new u-boot you MUST check that your device does not have bad sector on the mtd0 partion, running the command:
dmesg | grep -i 'bad'


We are going to use a usb key to load the uboot to your NAS.
FAT format a usb key. Copy the uboot.kwb and uboot.img files to your key.
Connect your NAS using an UART adapter. The baudrate you have to use it's 115200.
Boot your NAS up, you should see a message on your UART terminal asking to press any key to interrupt the boot process, so press that key.
You can check RX, TX, GND ground contacts on the picture above. Also note that I've added an heat sink on the main processor, it helps.
Now you are in the u-boot command terminal.
Plug the USB key and load the kernel to ram
fatload usb 0 0x0 0x800000 uboot.kwb
If it's all ok, you are ready to erase the nand for mtd0 partition
nand erase 0x0 0x1000000
then write then new uboot
nand write.e 0x800000 0x0 0x100000
Now load the usb uboot_env
fatload usb 0 0x0 0x800000 uboot.img
and write the uboot_env
nand write 0x800000 0xc0000 0x20000
Run printenv for test purpose
printenv
and save environement
saveenv
Cross your finger and reboot using command
reset

If it reboot you can now set environments. Once again press enter to jump in the uboot terminal.
Run the following command in order to properly set environment
setenv arcNumber 4022
setenv machid
setenv mtdparts 'mtdparts=orion_nand:0x100000(uboot),0x80000(uboot_env)'
then save environement
saveenv
and reboot
reset

2) install the rootfs - Debian system
Now you can install the Debian system. This is going to be simpler then the uboot installation.
Download the latest Debian kirkwood rootfs from this link: https://forum.doozan.com/read.php?2,12096
I'm running Debian-4.12.1-kirkwood-tld-1-rootfs-bodhi.tar.bz2
My choice is to run the Debian distribution on a 8Gb usb key, and use the NSA hard drive as storage.
Format a usb key in ext3, you also can format it as ext4 with the newer kirkwood boot but to me ext3 is enough, and set the label of this disk as "rootfs".
Mount the usb key and login as root in your terminal, you must be logged as root, the sudo command is not enough.
Copy the bz2 file your have download to your key.
Jump to the usb root mount folder, and extract the file. For me it's the command:
tar -xjf Debian-4.12.1-kirkwood-tld-1-rootfs-bodhi.tar.bz2
Now edit the file etc/network/interface cause eth0 will be named enp1s0 on NSA310 as follow:
  auto enp1s0
  allow-hotplug enp1s0
  iface enp1s0 inet dhcp
If you want to set a static IP, suppose 192.168.1.20, with the gateway and main dns server 192.168.1.1 and the dns server 8.8.8.8, on a domain domain.com the those lines should look like
  auto enp1s0
  allow-hotplug enp1s0
  iface enp1s0 inet static
    address 192.168.1.20
    netmask 255.255.255.0
    gateway 192.168.1.1
    dns-search domain.com
    dns-nameservers 192.168.1.1 8.8.8.8
Plug the usb key to the NSA310 and boot it.

The NSA should boot up with Debian.
If you set your network interface as above you should found it on your DHCP leases.
You should be able to log in the ssh terminal using the default username and passwod: root for both.

One logged in renew the ssh keys by using the following 
rm /etc/ssh/ssh_host*
ssh-keygen -A
apt-get update
apt-get upgrade

And change your root password using passwd.

If you want to change the hostname edit /etc/hostname and add the new name to /etc/hosts.

To boot with systemd you can touch the /boot/uEnv.txt file and add this content to that file:
custom_params=init=/bin/systemd
Then you have to install systemd
apt-get install systemd

You are now ready to install things on your Debian NAS.

3) setup Debian: install OpenMediaVault
The most usefull software you can install in your NAS, to me, it's OpenMediaVault.
Installing OpenMediaVault in your new NAS is really simple, you just have to add the openmediavault repository package, using the command:
cat <<EOF >> /etc/apt/sources.list.d/openmediavault.list
deb http://packages.openmediavault.org/public arrakis main
# deb http://downloads.sourceforge.net/project/openmediavault/packages arrakis main
## Uncomment the following line to add software from the proposed repository.
# deb http://packages.openmediavault.org/public arrakis-proposed main
# deb http://downloads.sourceforge.net/project/openmediavault/packages arrakis-proposed main
## This software is not part of OpenMediaVault, but is offered by third-party
## developers as a service to OpenMediaVault users.
# deb http://packages.openmediavault.org/public arrakis partner
# deb http://downloads.sourceforge.net/project/openmediavault/packages arrakis partner
EOF

Then run the following come to install the openmediavault software:
export LANG=C
export DEBIAN_FRONTEND=noninteractive
export APT_LISTCHANGES_FRONTEND=none
apt-get update
apt-get --allow-unauthenticated install openmediavault-keyring
apt-get update
apt-get --yes --auto-remove --show-upgraded \
    --allow-downgrades --allow-change-held-packages \
    --no-install-recommends \
    --option Dpkg::Options::="--force-confdef" \
    --option DPkg::Options::="--force-confold" \
    install postfix openmediavault
# Initialize the system and database.
omv-initsystem

Now you should be able to login to the web interface of your NSA310 openmediavault using the default username and password, that are admin and openmediavault.

From the OpenMediaVault panel you can customize your NAS.
You can format and mount your hard drive, and setup your sharing.
You can find a lot usefull information on the openmendiavault documentation page https://openmediavault.readthedocs.io/en/latest/

If you like me used an external USB key for your primary hdd, you can set your storage disk to spin down. I was not able to make my disk spindown in time using hdparm, so I'm using hd-idle. In order to use it I've got to compile it. Here you can find the hd-idle 1.05 armel package I've compiled:


To install hd-init copy the package on your device and run
dpkg -i hd-idle_1.05_armel.deb
Remember to disable the spindown on hdparm and on Storage/Physical Disks openmediavault settings.
Now you can edit the file /etc/default/hd-idle
Suppose you want to disable spindown for all disk except sda, and set a 10 minutes spindown for sda, that file should look like:
START_HD_IDLE=true
HD_IDLE_OPTS="-i 0 -a /dev/sda -i 600"
To start hd-idle at boot add the following command on System/Scheduled Jobs openmediavault setting:
service hd-idle start
use options reboot and user root.


Notes
  • read risk disclaimer
  • excuse my bad english