# MDRaid

Procedures for manipulating Linux mdraid


# Convert to Software Raid

This guide assumes we have our OS currently installed at /dev/sda and we are adding an identical size disk at /dev/sdb

# Preparation

Prepare the machine by installing the required software.

apt-get install initramfs-tools mdadm
modprobe linear
modprobe multipath
modprobe raid0
modprobe raid1
modprobe raid5
modprobe raid6
modprobe raid10
cat /proc/mdstat

# Copy sda to sdb

sfdisk -d /dev/sda | sfdisk --force /dev/sdb
fdisk -l

# change sdb to raid partitions

Change all of our partitions on the new disk to the type Linux RAID

sfdisk --change-id /dev/sdb 1 fd
sfdisk --change-id /dev/sdb 2 fd

# Clear previous raid

Make sure no trace of a previous raid setup is present.

mdadm --zero-superblock /dev/sdb1
mdadm --zero-superblock /dev/sdb2

# Create degraded raid

We'll create the new raid with the /dev/sda disk partitions missing. This means we'll add the new /dev/sdb partitions to the raid first.

mdadm --create /dev/md0 --level=1 --raid-disks=2 missing /dev/sdb1
mdadm --create /dev/md1 --level=1 --raid-disks=2 missing /dev/sdb2
  • Check the status of the newly created raid cat /proc/mdstat

# Prep the New Partitions

Create a new filesystem on the new raid device

  • mkfs.ext4 /dev/md0

# Prep the swap disk

If you had a swap disk, recreate it here. mkswap /dev/md1

# mdadm config

Backup your existing mdadm config and then add your new raid disks in to it.

cp /etc/mdadm/mdadm.conf /etc/mdadm/mdadm.conf_orig
mdadm --examine --scan >> /etc/mdadm/mdadm.conf

Confirm the contents of the file

  • cat /etc/mdadm/mdadm.conf

# Mount for copying

Mount your new raid disk so that you can copy over your existing disk.

mkdir /mnt/md0
mount /dev/md0 /mnt/md0

# Get UUID for fstab

Run blkid to get the UUIDs of /dev/md0 and /dev/md1

# Add to fstab

  • nano /etc/fstab

# Edit mtab, not for swap

sed -e "s/dev\/sda1/dev\/md0/" -i /etc/mtab
sed -e "s/dev\/sda2/dev\/md1/" -i /etc/mtab

# Set up bootloader option

cp /etc/grub.d/40_custom /etc/grub.d/09_swraid1_setup
nano /etc/grub.d/09_swraid1_setup

# For /boot on / partition

menuentry 'Ubuntu, with Linux 3.5.0-17-generic' --class ubuntu --class gnu-linux --class gnu --class os {
    recordfail
    insmod mdraid1x
    insmod ext2
    set root='(md/0)'
    linux   /boot/vmlinuz-3.5.0-17-generic root=/dev/md0 ro   quiet
    initrd  /boot/initrd.img-3.5.0-17-generic
}

# For /boot on own partition

menuentry 'Ubuntu, with Linux 3.5.0-17-generic' --class ubuntu --class gnu-linux --class gnu --class os {
    recordfail
    insmod mdraid1x
    insmod ext2
    set root='(md/0)'
    linux   /boot/vmlinuz-3.5.0-17-generic root=/dev/md1 ro   quiet
    initrd  /boot/initrd.img-3.5.0-17-generic
}

# Update grub

update-grub
update-initramfs -u

# Copy files

cp -dpRx / /mnt/md0
cp -dpRx /boot /mnt/md1

# Install grub

grub-install /dev/sda
grub-install /dev/sdb

# Reboot

  • reboot

# Check disks

df -h
cat /proc/mdstat

# Change sda to raid partitions

sfdisk --change-id /dev/sda 1 fd
sfdisk --change-id /dev/sda 2 fd

Confirm changes

  • fdisk -l

# Add sda to the raid

mdadm --add /dev/md0 /dev/sda1
mdadm --add /dev/md1 /dev/sda2

Check state of the raid

  • cat /proc/mdstat

Add disks to the raid config

  • mdadm --examine --scan >> /etc/mdadm/mdadm.conf

Confirm contents of the raid config

  • cat /etc/mdadm/mdadm.conf

# Finish

update-grub
update-initramfs -u
grub-install /dev/sda
grub-install /dev/sdb
  • Reboot!
Last Updated: 2019/03/02 11:51+00:00