Tag Archives: raspberry

RAID 0 in Raspberry PI with pen drives.

I have a Raspberry PI 3 to do experiments and when I’m not using it it runs Boinc which is a distributed computing software I will talk about another day.

This software makes an intensive use of disk access and I thought of a way to save the SD card for those who use one or as in my case to make less use of the HDD I have plugged by USB, It would be to install that program on a pen drive.

It turns out that I have many pen drives. Some of a few Gigabytes but also, the older ones of some tens of megs and I thought of an experiment to make use of those old pen drives whilst I protect my HDD.

The solution I thought of was to create a RAID 0 with the pen drives.
RAID 0 is a setting for which It’s possible to mount a filesystem in a number of store devices as is they were only one.
There are other configurations as for example to mount two devices in which one is a replica of the other for error prevention.

RAIDs can be hardware in which the operating system only sees a device instead of the bunch of then that compound it, or it can be by software in which the operating system takes care of it and it’s transparent for the user and the applications.

The software I found is called mdadm and it’s very easy to install in the Linux of Raspberry PI:

sudo apt-get install -y mdadm

With the command lsblk we can see the device files of our pen drives.

Now well, let’s suppose that we have two pen drives and their devices are /dev/sdd1 and /dev/sdb1.

We would crate the RAID device with the following command:

sudo mdadm --create /dev/md0 --raid-devices=2 --level=0 /dev/sdd1 /dev/sdb1

Now we need to save the configuration for Linux to remember it next time how to mount the device /dev/md0. We use the command:

sudo mdadm --detail --brief /dev/md0 >> /etc/mdadm.conf

Now we have to “assemble” the new RAID. This internally stores some data about how mdadm has to access the devices. The order and that stuff. Remember it for later.

sudo mdadm -A /dev/md0 -f /dev/sdc1 /dev/sdb1

At this point now we can format the new device /dev/md0 as it was anyother:

sudo mkfs.ext4 /dev/md0

And to mount it. For this example I will use the folder /mnt/raid:

sudo mount /dev/md0 /mnt/raid

And now we can access it as we would with any other device even thought it’s compounded by two.

Now we have a problem. If we restart the computer it won’t mount it anymore. This is because returning to the “assembling” step there is an issue that has to do with mdadm because in previous versions it made that assembly in a way whereas in posterior versions it does it in another way and when Linux tries to reconstruct the RAID it tries in the legacy mode instead on the modern one. So I’m going to explain how to fix this scenario and afterwards I’ll fix it definitely.

First we stop the RAID:

sudo mdadm --stop /dev/md0

Now we put the value 2 inside that file to set the mode.

sudo echo 2 > /sys/module/raid0/parameters/default_layout

and assemble it again RAID

sudo mdadm -A /dev/md0 -f /dev/sdc1 /dev/sdb1

Now we can mount it as before.

As this is unpractical we need the Raspberry Pi to know the mode to re-assemble the RAID.
For this we need to pass some parameters to the kernel on bootstrap.
This is done by editing the file /boot/cmdline.txt and adding in the same line the parameter raid0.default_layout=2. In my case it’s between fsck.repair=yes and rootwait.

With this the Raspberry Pi Linux starts knowing the mode it has to assemble the RAID with.

I hope it’s helpful to you, you’ll tell me in the comments.