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 devices
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. (really optional) Tune for even faster filesystem
Volume=/dev/md0
# Default to write data first and Journal after (?)
sudo tune2fs -o journal_data_writeback "$Volume"
# Disable Journal (?)
sudo tune2fs -O ^has_journal "$Volume"
# Volume revision on each mount
sudo tune2fs -c 1 "$Volume"
# Revision now to assure superblock changes
sudo e2fsck -f "$Volume"

8. Mount and clone 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

9. Clone root mount entry at /etc/fstab
for device /dev/md0
Replace old root entry to be mounted on /mnt/sdcard

10. Enable initramfs boot with image according to installed kernel: (version example)
ls -1 /boot/ | grep -e initrd
uname -r
# Add line at bottom of /boot/config.txt (version example; see uname report to choose yours)
[all]
initramfs initrd.img-5.10.17-v7l+

11. Specify RAID volume as root filesystem for next stage to kernel. Edit /boot/cmdline.txt
root=/dev/md0
Other useful and optional parameters:
fsck.repair=yes fsck.mode=auto

12. Reboot and enjoy
Last edited by narcisgarcia on Fri Jun 25, 2021 8:05 am, edited 2 times in total.

swampdog
Posts: 718
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: 718
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: 6685
Joined: Thu May 05, 2016 9:33 am
Location: Paris, 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: 1933
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

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…
Don’t forget to backup important data.
+1

Return to “Advanced users”