### Create mdadm RAID on a RaspPi and store the / partition on it ### I recently replaced my old VIA-based home server by a fresh new RaspberryPI B+ micro server. The RaspberryPI doesn't have any disk itself, it stores its operating system (in my case: Raspbian) on a small SD card. This is obviously not viable for a server usage that have many write operations. I bought an external USB enclosure for two disks, put my 3.5" HDDs in it, and connected to one of the USB ports of the RaspberryPI device. The idea was to use a RAID1 (mirror) configuration on two USB disks with mdadm, and store everything on this array. I won't go into details about how to initialize the RAID here, since I already wrote an article about mdadm some time ago. Let's assume a RAID is already created, and available as /dev/md0. Mounting such RAID as an additional mount point is easy, but what if I'd like to move *all* the system to it? First of all, I have to clarify that I didn't end up moving all the system, I had to leave one small part on the SD card: the /boot partition, as well as the boot MBR. This is no big problem, since there is hardly any writes ever to these areas, so I don't care that much (and it's only a few MBs, so easily backupable). The trick I had to achieve, however, is to move the root partition (/) to my USB-powered RAID array. === no RAID-related modules at boot -> use an initramfs === The problem that I had to deal with is that the Raspbian kernel doesn't include mdadm support compiled in, so RAID arrays are not detected during the boot, and can be accessed only after the root directory is mounted (which is obviously too late if I want the root directory to be stored on the RAID...). The solution to this problem is to make the Raspbian system use an initramfs during boot. By default, it doesn't use one. The initramfs is a small filesystem that is mounted in memory during boot, and provides all necessary files/modules/drivers for a proper boot. Here, we will make sure it contains raid-related modules. Add the following lines to the /etc/initramfs-tools/modules file: raid1 md_mod ext4 Then, (re)build the initramfs image: # update-initramfs -c -k `uname -r` Having an initramfs image is not all - yet we need to tell the system to use it during the boot. This we will do by editing the /boot/config.txt file. But before, let's check what kernel exactly we are running: # uname -r It will be something in the form of 3.10.XX+. Then, add the following 2 lines to the end of your /boot/config.txt: kernel=kernel.img initramfs initrd.img-3.12.35+ followkernel === copy the root partition to the raid === Now that we have a RAID-aware initramfs, we can finally make use of our RAID to store the root partition. You will have to partition your RAID device to your needs, choose the partition that will hold your root data (let's say /dev/md0p1), create a filesystem on it, and mount it somewhere (let's say under /mnt/newroot). Then, copy the current content of your root partition over the new root partition you prepared: # cp -ax / /mnt/newroot/ === make the boot loader to use your md0 device for boot === Finally, we will be able to ask the system to use our new root partition during boot. The RaspberryPI boot loader is configurable via the /boot/cmdline.txt file. It should look something along these lines: # cat /boot/cmdline.txt dwc_otg.lpm_enable=0 console=ttyAMA0,115200 console=tty1 root=/dev/md0p1 rootfstype=ext4 elevator=deadline boot_delay=32 rootdelay=10 rootwait You should also modify your /etc/fstab file accordingly (although I noticed that Raspbian seems to care little about the / entry in its fstab). Now, if all went fine, we should be able to reboot, and the RaspberryPI will happily mount its root partition on our newly-created RAID. |