How To Set Up Linux RAID On Ubuntu 22.04

Originally Published: July 13, 2022

Update (Sep 14, 2022): I’ve switched to using ZFS because it has checksums for every block — I can’t imagine trusting a storage without checksums. This blog post covers how to setup ZFS and switch away from mdadm.


# We want to create a RAID 0 on /dev/sda and /dev/sdb drives.

# Create Partitions on drives first.

$ sudo fdisk /dev/sda

Welcome to fdisk (util-linux 2.37.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

The device contains 'linux_raid_member' signature and it will be removed by a write command. See fdisk(8) man page and --wipe option for more details.

Device does not contain a recognized partition table.
Created a new DOS disklabel with disk identifier 0xdad7c228.

Command (m for help): n
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-3907029167, default 2048):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-3907029167, default 3907029167):

Created a new partition 1 of type 'Linux' and of size 1.8 TiB.

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

# Same for the other drive: /dev/sdb

# Create a /dev/md0 across the two partitions.
$ sudo mdadm --create /dev/md0 --name=data --level=0 --raid-devices=2 /dev/sda1 /dev/sdb1
$ sudo mkfs -t ext4 /dev/md0 # Create EXT4 filesystem
$ sudo mount /dev/md0 /data  # Mount them on /data

$ cat /proc/mdstat
> Personalities : [raid0] [linear] [multipath] [raid1] [raid6] [raid5] [raid4] [raid10]
> md0 : active raid0 sdb1[1] sda1[0]
>       2000142336 blocks super 1.2 512k chunks
>
> unused devices: <none>

$ sudo mdadm --detail --scan
> ARRAY /dev/md/mrjn-workstation:0 metadata=1.2 name=mrjn-workstation:0 UUID=49749d20:407900e2:8f5613e8:679a5226

# This goes into /etc/mdadm/mdadm.conf

DEVICE /dev/sda1 /dev/sdb1
ARRAY /dev/md/mrjn...

$ sudo update-initramfs -u

$ sudo blkid /dev/md0
> /dev/md0: UUID="1fb7c99e-02af-45c0-86e8-7a29d8f64ee9" BLOCK_SIZE="4096" TYPE="ext4"

# This goes into /etc/fstab

UUID=1fb7c99e-02af-45c0-86e8-7a29d8f64ee9 /data ext4 defaults,nofail,discard 0 0

# Check with lsblk
$ sudo lsblk # should look something like this

sda                   8:0    0 953.9G  0 disk
└─sda1                8:1    0 953.9G  0 part
  └─md0               9:0    0   1.9T  0 raid0 /data
sdb                   8:16   0 953.9G  0 disk
└─sdb1                8:17   0 953.9G  0 part
  └─md0               9:0    0   1.9T  0 raid0 /data

# Now, on a reboot, the raid array should mount on /data fine.


Date
August 8, 2022