Post by ajo on Apr 13, 2014 15:05:14 GMT 7
In this topic I present two methods to install Debian to the Thecus N2200 NAS. We will use a custom rootfs designed for the WD MyBook White Light which has almost identical hardware to our NAS. Our installation will be more complicated because Thecus has put bootloader, kernel and initrd all on flash instead of HDD. That's why we cannot follow the instructions on the MyBook page.
Obligatory disclaimer: I am not responsible for any damage to you, your data, your hardware, your network, your friendships etc. etc. as a result of your actions based on this guide. Don't start tinkering when there is valuable data on your NAS and/or HDDs. I also do not guarantee that this guide is correct, free from stupidity, good for anything, or that I'll have time to answer your questions. I explicitly do not guarantee that the commands and scripts listed here will leave you with a functional device. However, I can tell you that this is what I did to my N2200 and it works. If you find any errors I would like to be notified.
You can choose between two methods, both of which I have tested on my own N2200. Obviously you should only try one method at a time, and if you want to switch to the other one, you have to revert the current one.
A. Double Pivot_Root Mod: this is a safe method that can be completely reverted by wiping the HDD and running Thecus Smart Utility for a reinstall of the original firmware. You can perform the mod from Windows, Linux or Mac OS. The downside is that you can only run Debian from a SATA HDD, not from a USB stick.
B. Initrd Mod: this is a more elegant method that allows you to run Debian from a USB stick, so if you don't want any SATA HDD then that's possible. You will need a Linux PC for this. The major downside is that you need to write a new initrd image to the N2200 flash memory. If this goes wrong you may brick the device. To undo this mod, you need to flash the original initrd back.
Either way, you can start by downloading the WD White Light custom firmware package and extracting rootfs.arm.ext2. I run the rootfs from debian-lenny-for-white-light-v2.2-RC2.tar.gz on my N2200 but I'm pretty sure the other versions should work too. Rename rootfs.arm.ext2 to debian.ext and put it on a USB drive. Now write down the admin (root) password that you currently have set on the Thecus NAS, because you will need it to revert the mod in the future. If you're going for the safe mod, read on. If you want to flash your initrd, go to the first reply in this thread.
One final note, the methods here should in principle work with any rootfs containing ARM binaries that will work with the Thecus 2.6.24 kernel (which we're not replacing). I just don't know of any such rootfs besides the White Light Debian one and I'm not interested in developing any, but I'm sure if you put enough time in it you can create (for example) an ArchLinux rootfs. The point of this thread is just to show you how to get it on the N2200.
Double Pivot_Root Mod
Before we start you will need another file. Copy/paste the following script into a text file, call it init.new and put it on your USB drive.
Now make sure all your important data is off the N2200 and get started!
1. Use Thecus Smart Utility to do a firmware install on an empty HDD in the N2200. You can skip this step if you already have a working installation.
2. Install the Dropbear mod via the N2200 web interface
3. Copy the custom rootfs image (e.g. "debian.ext"), as well as the "init.new" script to a USB drive. Connect this drive to the N2200, then login as root via Secure Shell (SSH).
4. Make a backup of the original Thecus "ramdisk" image:
mkdir -p /mnt/{minix,usb,debian,thecus}
mount /dev/md11 /mnt/minix
mount /dev/sdg1 /mnt/usb
mv /mnt/minix/ramdisk /mnt/usb/ramdisk.old
5. Copy drivers from the Thecus image into the Debian image:
mount /mnt/usb/ramdisk.old /mnt/thecus
mount /mnt/usb/debian.ext /mnt/debian
cp -a /mnt/thecus/lib/modules/2.6.24 /mnt/debian/lib/modules
umount /mnt/debian
umount /mnt/thecus
umount /mnt/usb
6. Use fdisk /dev/sda to modify or completely recreate the partition table of the N2200 internal HDD. The simplest option is to keep the standard Thecus table and only change the system id of the partition /dev/sda2 from xfs to ext (since it will host our custom Debian rootfs): use commands t ; 2 ; 83 ; w.
You can also design your own table as long as it contains at least one 512 MB partition of type fd (Linux RAID autodetect), and one partition of type 83 (ext Linux) for the custom rootfs. You could for example add swap and/or data partitions. Write the new table and reboot the N2200.
7. Login to the N2200 via SSH. If you decided to keep the standard Thecus partition table, move on to step 10 now. Otherwise, continue with step 8.
(8.) If you have built a custom partition table, you need to setup the obligatory minix RAID volume (required by the Thecus linuxrc script in initrd, which we cannot modify since it's on flash). If you decided to keep the Thecus table and just change the xfs partition to ext, you can skip this step because the original minix partition is still alive and well.
Assuming your 512 MB type fd partition is at /dev/sda3:
mdadm --create /dev/md11 --level=linear --force --raid-devices=1 /dev/sda3
mkfs.minix /dev/md11
mkdir /mnt/minix
mount /dev/md11 /mnt/minix
Then copy back the "ramdisk" file, of which we have a backup on USB:
mount /dev/sdg1 /mnt/usb
cp /mnt/usb/ramdisk.old /mnt/minix/ramdisk
(9.) Before we put our custom pivot_root script into place, we have to update it so that it points to the correct location of the custom rootfs. The default value that I put into the script is /dev/sda2, which is good if you decided to keep the standard Thecus table. Otherwise, edit the "init.new" script and replace all instances of /dev/sda2 with the correct partition. Also update the line "echo 0x0802 > /proc/sys/kernel/real-root-dev", instructions are provided in the script.
The N2200 has a vi editor you can use.
10. We must now modify the "ramdisk" image by replacing the /sbin/init binary with our script that performs the second pivot_root. First mount the necessary filesystems, including the ramdisk image:
mkdir -p /mnt/{minix,usb,ramdisk}
mount /dev/md11 /mnt/minix
mount /dev/sdg1 /mnt/usb
mount /mnt/minix/ramdisk /mnt/ramdisk
Now make a backup copy of the original init binary, and put the new init (the script) in place:
mv /mnt/ramdisk/sbin/init /mnt/ramdisk/sbin/init.old
cp /mnt/usb/init.new /mnt/ramdisk/sbin/init
chmod +x /mnt/ramdisk/sbin/init
umount /mnt/ramdisk
umount /mnt/minix
11. Write the custom rootfs (e.g. "debian.ext") from the USB disk to the ext partition, which is /dev/sda2 if you're using the standard Thecus partition table:
dd if=/mnt/usb/debian.ext of=/dev/sda2 bs=2M
Check to see if it was written correctly, and if it contains the required kernel modules:
mkdir /mnt/debian
mount /dev/sda2 /mnt/debian
ls /mnt/debian/lib/modules/
Now create some empty directories required for the second pivot_root process:
mkdir -p /mnt/debian/{initrd,initrd2,sysroot,sysroot2}
Finally, unmount and reboot:
umount /mnt/debian
umount /mnt/usb
reboot
12. Hopefully, you should now hear the harddrive making more noise than usual as your custom rootfs is starting up. Note that if installation was successful and you can connect to SSH, your root password will have changed to the password of the custom rootfs ("mybook").
Removing the Double Pivot_Root Mod
Simply delete all partitions off your HDD with fdisk (you can do this from the N2200 or by temporarily moving the HDD to a pc). Then poweroff the N2200, put the empty HDD in, start it up and run Thecus Smart Utility from your pc. This restoration process also works if anything went wrong during the mod installation.
Obligatory disclaimer: I am not responsible for any damage to you, your data, your hardware, your network, your friendships etc. etc. as a result of your actions based on this guide. Don't start tinkering when there is valuable data on your NAS and/or HDDs. I also do not guarantee that this guide is correct, free from stupidity, good for anything, or that I'll have time to answer your questions. I explicitly do not guarantee that the commands and scripts listed here will leave you with a functional device. However, I can tell you that this is what I did to my N2200 and it works. If you find any errors I would like to be notified.
You can choose between two methods, both of which I have tested on my own N2200. Obviously you should only try one method at a time, and if you want to switch to the other one, you have to revert the current one.
A. Double Pivot_Root Mod: this is a safe method that can be completely reverted by wiping the HDD and running Thecus Smart Utility for a reinstall of the original firmware. You can perform the mod from Windows, Linux or Mac OS. The downside is that you can only run Debian from a SATA HDD, not from a USB stick.
B. Initrd Mod: this is a more elegant method that allows you to run Debian from a USB stick, so if you don't want any SATA HDD then that's possible. You will need a Linux PC for this. The major downside is that you need to write a new initrd image to the N2200 flash memory. If this goes wrong you may brick the device. To undo this mod, you need to flash the original initrd back.
Either way, you can start by downloading the WD White Light custom firmware package and extracting rootfs.arm.ext2. I run the rootfs from debian-lenny-for-white-light-v2.2-RC2.tar.gz on my N2200 but I'm pretty sure the other versions should work too. Rename rootfs.arm.ext2 to debian.ext and put it on a USB drive. Now write down the admin (root) password that you currently have set on the Thecus NAS, because you will need it to revert the mod in the future. If you're going for the safe mod, read on. If you want to flash your initrd, go to the first reply in this thread.
One final note, the methods here should in principle work with any rootfs containing ARM binaries that will work with the Thecus 2.6.24 kernel (which we're not replacing). I just don't know of any such rootfs besides the White Light Debian one and I'm not interested in developing any, but I'm sure if you put enough time in it you can create (for example) an ArchLinux rootfs. The point of this thread is just to show you how to get it on the N2200.
Double Pivot_Root Mod
Before we start you will need another file. Copy/paste the following script into a text file, call it init.new and put it on your USB drive.
#!/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin
/bin/echo "In the second pivot_root script ..."
cd /
mount -t proc /proc /proc
# cleanup of previous pivot_root leftovers
umount /initrd
mount -w -o remount,rw /dev/root /
freeramdisk /dev/ram0
if [ -b /dev/sda2 ]; then
modprobe ext3
mkdir /sysroot2
echo "Mounting custom root filesystem on harddisk"
mount /dev/sda2 /sysroot2
if [ $? -eq 0 ]; then
echo 0x0802 > /proc/sys/kernel/real-root-dev
# This must contain the major and minor device numbers that identify the new rootfs partition
# For the original Thecus ramdisk this was (1,1) or actually 0x0101, pointing to /dev/ram1.
# But we are using a rootfs on SATA harddisk instead so this number changes.
# If your rootfs is at /dev/sda3 the correct number is (8,3) or actually 0x0803 in hex
# A device identifier contains a major number (e.g. 3 for /dev/hd* versus 8 for /dev/sd*)
# and a minor number (e.g. 0 for /dev/hda versus 1 for /dev/hda1 versus 64 for /dev/hdb etc.)
# Examples (major,minor): /dev/ram0 (1,0) /dev/ram1 (1,1)
# /dev/hda (3,0) /dev/hda1 (3,1) /dev/hda2 (3,2) /dev/hdb (3,64) /dev/hdb1 (3,65) /dev/hdb2 (3,66)
# /dev/sda (8,0) /dev/sda1 (8,1) /dev/sda2 (8,2) /dev/sdb (8,16) /dev/sdb1 (8,17) /dev/sdb2 (8,18)
# /dev/md0 (9,0) /dev/md1 (9,1) /dev/md2 (9,2) /dev/md3 (9,3)
# The number must be input in hex, translate them like this: (3,65) --> 0x0365 (or 0x365)
# For more info, see devices.txt by Alan Cox in the Linux kernel documentation
cd /sysroot2
/sbin/pivot_root . initrd2
umount /initrd2/proc
exec /usr/sbin/chroot . sh -c 'exec /sbin/init' <dev/console >dev/console 2>&1
fi
fi
Now make sure all your important data is off the N2200 and get started!
1. Use Thecus Smart Utility to do a firmware install on an empty HDD in the N2200. You can skip this step if you already have a working installation.
2. Install the Dropbear mod via the N2200 web interface
3. Copy the custom rootfs image (e.g. "debian.ext"), as well as the "init.new" script to a USB drive. Connect this drive to the N2200, then login as root via Secure Shell (SSH).
4. Make a backup of the original Thecus "ramdisk" image:
mkdir -p /mnt/{minix,usb,debian,thecus}
mount /dev/md11 /mnt/minix
mount /dev/sdg1 /mnt/usb
mv /mnt/minix/ramdisk /mnt/usb/ramdisk.old
5. Copy drivers from the Thecus image into the Debian image:
mount /mnt/usb/ramdisk.old /mnt/thecus
mount /mnt/usb/debian.ext /mnt/debian
cp -a /mnt/thecus/lib/modules/2.6.24 /mnt/debian/lib/modules
umount /mnt/debian
umount /mnt/thecus
umount /mnt/usb
6. Use fdisk /dev/sda to modify or completely recreate the partition table of the N2200 internal HDD. The simplest option is to keep the standard Thecus table and only change the system id of the partition /dev/sda2 from xfs to ext (since it will host our custom Debian rootfs): use commands t ; 2 ; 83 ; w.
You can also design your own table as long as it contains at least one 512 MB partition of type fd (Linux RAID autodetect), and one partition of type 83 (ext Linux) for the custom rootfs. You could for example add swap and/or data partitions. Write the new table and reboot the N2200.
7. Login to the N2200 via SSH. If you decided to keep the standard Thecus partition table, move on to step 10 now. Otherwise, continue with step 8.
(8.) If you have built a custom partition table, you need to setup the obligatory minix RAID volume (required by the Thecus linuxrc script in initrd, which we cannot modify since it's on flash). If you decided to keep the Thecus table and just change the xfs partition to ext, you can skip this step because the original minix partition is still alive and well.
Assuming your 512 MB type fd partition is at /dev/sda3:
mdadm --create /dev/md11 --level=linear --force --raid-devices=1 /dev/sda3
mkfs.minix /dev/md11
mkdir /mnt/minix
mount /dev/md11 /mnt/minix
Then copy back the "ramdisk" file, of which we have a backup on USB:
mount /dev/sdg1 /mnt/usb
cp /mnt/usb/ramdisk.old /mnt/minix/ramdisk
(9.) Before we put our custom pivot_root script into place, we have to update it so that it points to the correct location of the custom rootfs. The default value that I put into the script is /dev/sda2, which is good if you decided to keep the standard Thecus table. Otherwise, edit the "init.new" script and replace all instances of /dev/sda2 with the correct partition. Also update the line "echo 0x0802 > /proc/sys/kernel/real-root-dev", instructions are provided in the script.
The N2200 has a vi editor you can use.
10. We must now modify the "ramdisk" image by replacing the /sbin/init binary with our script that performs the second pivot_root. First mount the necessary filesystems, including the ramdisk image:
mkdir -p /mnt/{minix,usb,ramdisk}
mount /dev/md11 /mnt/minix
mount /dev/sdg1 /mnt/usb
mount /mnt/minix/ramdisk /mnt/ramdisk
Now make a backup copy of the original init binary, and put the new init (the script) in place:
mv /mnt/ramdisk/sbin/init /mnt/ramdisk/sbin/init.old
cp /mnt/usb/init.new /mnt/ramdisk/sbin/init
chmod +x /mnt/ramdisk/sbin/init
umount /mnt/ramdisk
umount /mnt/minix
11. Write the custom rootfs (e.g. "debian.ext") from the USB disk to the ext partition, which is /dev/sda2 if you're using the standard Thecus partition table:
dd if=/mnt/usb/debian.ext of=/dev/sda2 bs=2M
Check to see if it was written correctly, and if it contains the required kernel modules:
mkdir /mnt/debian
mount /dev/sda2 /mnt/debian
ls /mnt/debian/lib/modules/
Now create some empty directories required for the second pivot_root process:
mkdir -p /mnt/debian/{initrd,initrd2,sysroot,sysroot2}
Finally, unmount and reboot:
umount /mnt/debian
umount /mnt/usb
reboot
12. Hopefully, you should now hear the harddrive making more noise than usual as your custom rootfs is starting up. Note that if installation was successful and you can connect to SSH, your root password will have changed to the password of the custom rootfs ("mybook").
Removing the Double Pivot_Root Mod
Simply delete all partitions off your HDD with fdisk (you can do this from the N2200 or by temporarily moving the HDD to a pc). Then poweroff the N2200, put the empty HDD in, start it up and run Thecus Smart Utility from your pc. This restoration process also works if anything went wrong during the mod installation.