User avatar
narcisgarcia
Posts: 9
Joined: Tue Jun 21, 2016 2:21 pm

How to boot to USB RAID

Wed Jun 23, 2021 2:32 pm

Alternative: A simpler guide for a single USB pendrive is available: How to boot to USB device

This guide (near) doubles the filesystem i/o speed by using RAID0 to (near) parallelize data transfers. Same procedure can be used for other RAID levels by software (mirroring, etc.) and for more USB pendrives as you need.

This recipe is tested on Raspbian GNU/Linux 10 (buster), no other computer needed, and by plugging the only 2 USB pendrives seen as /dev/sda and /dev/sdb respectively.

1. Clean USB devices (all data there will be lost!)
sudo dd if=/dev/zero of=/dev/sda bs=1M count=10
sudo dd if=/dev/zero of=/dev/sdb bs=1M count=10

2. Prepare software
Edit /etc/default/raspberrypi-kernel and enable initramfs generation&use there:

Code: Select all

INITRD=Yes
RPI_INITRD=Yes
sudo apt --no-install-recommends install mdadm

3. Following assumes both devices are of same size (otherwise, only smallest size of both is useful)

3.1. sudo parted /dev/sda
mklabel
Create primary partition from 2048 to -1
set 1 raid on
3.2. sudo parted /dev/sdb
mklabel
Create primary partition from 2048 to -1
set 1 raid on

4. Create RAID
sudo mdadm --create /dev/md0 --level=raid0 --raid-devices=2 /dev/sda1 /dev/sdb1

5. Re/Generate initramfs
sudo dpkg-reconfigure raspberrypi-kernel

6. Format volume
Label=MyRaid
sudo mkfs -t ext4 -E root_owner=0:0 -L "$Label" /dev/md0

7. Mount and clone current filesystem
sudo mkdir /mnt/md0
sudo mount /dev/md0 /mnt/md0
sudo touch /SDCARD.id
sudo touch /tmp/md0/RAID.id
sudo cp -a -x -u /tmp /mnt/md0/
sudo umount /mnt/md0
sudo rmdir /mnt/md0

sudo mkdir /tmp/md0
sudo mount /dev/md0 /tmp/md0
cd /
Stuff="$(ls -1 | grep -ve ^tmp -ve ^lost -ve '\.id')"
sudo cp -a -x -u $Stuff /tmp/md0/
sudo mkdir /tmp/md0/mnt/sdcard
sudo umount /tmp/md0
sudo rmdir /tmp/md0

8. Adapt etc/fstab for RAID environment: Edit /mnt/md0/etc/fstab
for device /dev/md0
Replace old root entry to be mounted on /mnt/sdcard
Replicate old root entry to be from device /dev/md0

Examples:

Code: Select all

PARTUUID=cfd4a3f3-02  /mnt/sdcard               ext4    defaults,noatime  0       1
/dev/md0  /               ext4    defaults,noatime  0       1

9. Enable initramfs boot with image according to working kernel:
ls -1 /boot/ | grep -e "initrd.*$(uname -r)$"
# Add line at bottom of /boot/config.txt (version example; see previous result as yours)
[all]
initramfs initrd.img-5.10.17-v7l+

10. Specify RAID volume as root filesystem for next stage to kernel. Edit /boot/cmdline.txt
root=/dev/md0

11. Reboot and enjoy
Last edited by narcisgarcia on Wed Aug 25, 2021 7:22 am, edited 8 times in total.

swampdog
Posts: 899
Joined: Fri Dec 04, 2015 11:22 am

Re: How to boot to USB RAID

Fri Jun 25, 2021 4:02 am

Let us know how reliable it turns out to be. I haven't tried raid on an rpi, as usb in general can be a bit flakey.

You should also make a note about kernel updates. I know booting rootfs off LVM requires a manual "hunt the new kernel" stage prior to the reboot. I keep meaning to look into it but more pressing matters are getting in the way.

User avatar
narcisgarcia
Posts: 9
Joined: Tue Jun 21, 2016 2:21 pm

Re: How to boot to USB RAID

Fri Jun 25, 2021 6:56 am

Very reliable. no LVM involved.
  • When I transferred OS data (and big documents), this operation was really faster than other I did before with SDCard only (class 10)
  • I use Kodi software in this device: All operations with some i/o impact have been accelerated. Most of them 2x and some of them 3x and 4x faster than before with SDCard only
I haven't any kernel/update issue. I didn't perform any patch, nor any other operations than documented in this guide.
An this works fine at any time:

Code: Select all

sudo apt update
sudo apt full-upgrade

swampdog
Posts: 899
Joined: Fri Dec 04, 2015 11:22 am

Re: How to boot to USB RAID

Fri Jun 25, 2021 7:59 am

I might give raid a try sometime then. :-)

Maybe raid support is built into the kernel? I wondered because you created "initrd.img-5.10.17-v7l+" - or perhaps the bug(*) has been fixed?

(*) Not really a bug. When the kernel version changes, the update script regenerates "initrd" for active kernel not the new one. It all goes wrong from the outset because /boot/ is fat32 and so the attempt to create a link upon /boot/ fails. For my LVM rpi I must 'update-initramfs -c -kFOO' prior to reboot, where FOO=5.10.17-v7l+ for instance.

User avatar
narcisgarcia
Posts: 9
Joined: Tue Jun 21, 2016 2:21 pm

Re: How to boot to USB RAID

Fri Jun 25, 2021 8:07 am

mdadm is what loads RAID support for initrd.
Raspbian Linux kernel does not include RAID support: it loads initramfs and this previous-stage OS loads mdadm stuff prior to looking for final root volume.

epoch1970
Posts: 8061
Joined: Thu May 05, 2016 9:33 am
Location: France

Re: How to boot to USB RAID

Fri Jun 25, 2021 10:16 am

Raid 0 rootfs over USB and filesystem journal disabled, that’s what I call living on the edge…
Don’t forget to backup important data.
"S'il n'y a pas de solution, c'est qu'il n'y a pas de problème." Les Shadoks, J. Rouxel

User avatar
dickon
Posts: 2263
Joined: Sun Dec 09, 2012 3:54 pm
Location: Home, in Towcester

Re: How to boot to USB RAID

Fri Jun 25, 2021 3:57 pm

epoch1970 wrote:
Fri Jun 25, 2021 10:16 am
Raid 0 rootfs over USB and filesystem journal disabled, that’s what I call living on the edge…
That's certainly a novel spelling of 'madness'...
Don’t forget to backup important data.
Seconded, as a heartfelt plea...
As it is apparently board policy to disallow any criticism of anything, as it appears to criticise something is to criticise all the users of that something, I will no longer be commenting in threads which are not directly relevant to my uses of the Pi.

User avatar
narcisgarcia
Posts: 9
Joined: Tue Jun 21, 2016 2:21 pm

Re: How to boot to USB RAID

Fri Jun 25, 2021 7:06 pm

I'm waiting to check if it's necessary to register an initramfs hook to update /boot/config.txt on kernel upgrades.

And I don't know if scripts at /etc/initramfs-tools/hooks/ are run before or after image file creation. If it's before, then any hook should go to update-initramfs caller (something at raspberrypi-kernel configuration).
Last edited by narcisgarcia on Wed Aug 25, 2021 6:48 am, edited 1 time in total.

robertalexa
Posts: 15
Joined: Wed Aug 18, 2021 9:45 pm

Re: How to boot to USB RAID

Sun Sep 19, 2021 9:16 pm

hey OP, have you have a chance to do a kernel update and see how it holds? I was playing with a similar setup and kernel updates broke everything. Despite updating the bootloader image manually and editing the config before reboot. Thanks

Return to “Advanced users”