Skip to content

Instantly share code, notes, and snippets.

@joshenders
Last active November 19, 2023 19:04
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshenders/3803617 to your computer and use it in GitHub Desktop.
Save joshenders/3803617 to your computer and use it in GitHub Desktop.
Create mdraid mirror from an existing Wheezy installation

Create mdraid mirror from an existing Debian installation

Throughout this guide, /dev/sda will be referred to as the old drive and /dev/sdb will be referred to as the new drive. If possible, I suggest disconnecting all other disks attached to the system while performing this maintenance.

Prerequisite software

apt-get install mdadm rsync

1. Partition the new drive.

sfdisk -d /dev/sda | sfdisk /dev/sdb 
sfdisk --change-id /dev/sdb 1 DA # change partition-type to, "0xDA" aka "non-fs data" as "linux raid auto" is deprecated

2. Create degraded RAID array.

mdadm --create /dev/md0 --level=1 --raid-devices=2 missing /dev/sdb1 # md/v1.x metadata okay if grub-pc version is 1.98+20100720-1 or later [3]
mkfs.ext4 /dev/md0
mdadm --detail --scan >> /etc/mdadm/mdadm.conf
dpkg-reconfigure mdadm

3. Copy existing data onto the new drive.

mkdir /tmp/destroot
mount /dev/md0 /tmp/destroot
rsync -auHxv --exclude=/proc/* --exclude=/sys/* --exclude=/tmp/* /* /tmp/destroot/

4. Update initramfs and install bootloader on both drives

mount -o bind /dev /tmp/destroot/dev 
mount -t proc none /tmp/destroot/proc 
mount -t sysfs none /tmp/destroot/sys
chroot /tmp/destroot /bin/bash
update-initramfs -u -k all
rm /boot/grub/device.map
update-grub
grub-install /dev/sda
grub-install /dev/sdb
blkid /dev/md0 # replace UUID line in /etc/fstab (in chroot)
exit

5. Reboot. Boot into the degraded array (new drive) and test system.

reboot # Pivot bios boot order to boot to new drive (/dev/sdb) 
mount | grep $(blkid -o value /dev/md0 | head -n1) # verify booted from "/dev/md0".

6. Add original drive to the degraded array and resync.

sfdisk --change-id /dev/sda 1 DA # change partition-type to, "0xDA" aka "non-fs data" as "linux raid auto" is deprecated
mdadm /dev/md0 -a /dev/sda1
watch -n1 cat /proc/mdstat

7. Test booting off of the old drive (/dev/sda). Simulate failure by disconnecting new drive (/dev/sdb).

poweroff # Verify bios boot to old drive (/dev/sda). Preferably BIOS handles this failover automatically.
mount | grep $(blkid -o value /dev/md0 | head -n1) # verify booted from "/dev/md0".
poweroff

8. Reconnect new drive (/dev/sdb) and resync drives.

mdadm /dev/md0 -a /dev/sdb1
watch -n1 cat /proc/mdstat

9. Test booting off of the new drive (/dev/sdb). Simulate failure by disconnecting old drive (/dev/sda).

poweroff # Verify bios boot to "/dev/sdb". Preferably BIOS handles this failover automatically.
mount | grep $(blkid -o value /dev/md0 | head -n1) # verify booted from "/dev/md0".
poweroff

10. Reconnect old drive (/dev/sda) and resync drives.

mdadm /dev/md0 -a /dev/sda1
watch -n1 cat /proc/mdstat

11. Reboot with the two drives

reboot
mount | grep $(blkid -o value /dev/md0 | head -n1) # verify booted from "/dev/md0"

References

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment