Migrating to RAID1 live

Just a couple of days ago had migrated one Ubuntu 8.04 LTS server from single disk to software RAID1. Live! Well, almost live as there were a few reboots required. Here are the set up and sequence of actions performed.

Initial situation:

Device Boot Start End Blocks Id System
/dev/sda1 1 262 2104514+ 82 Linux swap / Solaris
/dev/sda2 263 48641 388604317+ 83 Linux

Installed /dev/sdb, which is identical to /dev/sda. And now I want to have a software RAID1 (mirror).

Instruction used were compiled from:
http://www.howtoforge.com/software-raid1-grub-boot-debian-etch
http://www.debian-administration.org/articles/238

What I did:

  • install software raid management utils: apt-get install mdadm
  • change the system types on partitions you want to mirror on the old drive to fd (raid autodetect), if the system says that kernel is still using the old partition scheme - REBOOT!
  • copy partition table from old disk to new, I used: dd if=/dev/sda of=/dev/sdb bs=512 count=1
  • REBOOT!
  • load kernel modules
    • modprobe -v md
    • modprobe -v linear
    • modprobe -v multipath
    • modprobe -v raid0
    • modprobe -v raid1
  • create arrays with missing drives (we're still running from /dev/sda)
    • mdadm -vv --create /dev/md1 --level 1 --raid-devices=2 missing /dev/sdb1
    • mdadm -vv --create /dev/md2 --level 1 --raid-devices=2 missing /dev/sdb2
  • take a look that there're two RAID1 devices with missing drives: cat /proc/mdstat
  • make file systems on the new RAID
    • mkswap /dev/md1
    • mkfs.ext3 -v /dev/md2
  • adjust mdadm.conf
    • cp -pv /etc/mdadm/mdadm.conf /etc/mdadm/mdadm.conf_orig
    • mdadm --examine --scan >> /etc/mdadm/mdadm.conf
  • modify /etc/fstab to reflect new RAID instead of old drive:
    /dev/md1 none swap sw 0 0
    /dev/md2 / ext3 defaults 0 0
  • modify /etc/lilo.conf so that we can boot from old drive, but the root file system would be mounted from our new RAID:
    boot=/dev/sda
    root=/dev/md2
  • clean apt-get cache (to make file copying from old drive to RAID just a little bit faster):
    • apt-get autoremove
    • apt-get autoclean
    • apt-get clean
  • update initramfs: update-initramfs -u
  • mount new RAID for file copying: mount -v -t ext3 /dev/md2 /mnt
  • copy all files from old drive into new RAID: cp -dpvRx / /mnt
  • update boot loader with new config: /sbin/lilo -H
  • finish swap setup on new RAID:
    • swapoff -a
    • mdadm -vv --add /dev/md1 /dev/sda1
    • wait for md1 to sync: watch cat /proc/mdstat
  • REBOOT!
  • add missing disk into root fs RAID: mdadm -vv --add /dev/md2 /dev/sda2
  • wait for arrays to sync: watch cat /proc/mdstat
  • modify lilo.conf to reflect final system setup with RAID1:
    boot=/dev/md2
    root=/dev/md2
    raid-extra-boot=mbr-only
  • update initramfs: update-initramfs -u
  • update boot loader with new config: /sbin/lilo
  • REBOOT!

The server now is happy running from the /dev/md2 RAID1 made of two identical partitions on separate disks (/dev/sda2 and /dev/sdb2).