Setting up software RAID on a running Debian system
If you have a running Debian (or any Debian based distro, like Ubuntu) system and want to configure a RAID1 (mirror) without losing your existing data, this can be accomplished quite easily in a few steps.
The idea is to create a degraded RAID1 on a new drive, copy the data into this newly create RAID1 and then add the old drive into the array.
Let’s assume the original drive is /dev/sda and the new drive is /dev/sdb.
Follow this step by step guide to mirror the exising system (this works perfectly in Debian 7):
1. Install required packages
# apt-get install mdadm initramfs-tools rsync
2. Install and configure the new drive
Basically you install the drive and partition it with the tool of your choice (fdisk, GNU parted, etc). Set the type of the partitions (for all partitions) to fd (linux raid autodetect).
There’s no need to use the same partition layout you have in your original drive.
For this example, I will create 3 partitions:
- /dev/sdb1 for /
- /dev/sdb5 for /home
- /dev/sdb6 for swap
3. Create a degraded RAID1 array
# mdadm --create /dev/md0 --level=1 --raid-devices=2 /dev/sdb1 missing # mdadm --create /dev/md1 --level=1 --raid-devices=2 /dev/sdb5 missing # mdadm --create /dev/md2 --level=1 --raid-devices=2 /dev/sdb6 missing
4. Create the filesystems on the new devices and mount them
# mkfs.xfs -isize=2048 /dev/md0 # mkfs.xfs -isize=2048 /dev/md1 # mkswap /dev/md2 # swapon /dev/md2 # mkdir /mnt/root /mnt/home # mount /dev/md0 /mnt/root # mount /dev/md1 /mnt/home
Notice that you don’t need to use the same type of filesystem you have in the original drive. In this example, I’m using xfs with an inode size of 2048 because I plan to store extended attributes on that filesystems.
5. Copy the data to the new filesystems
I use rsync to do so. It would be a good idea to disable any service that you have on the server that might use the filesystem before starting the copy.
# rsync -auHxXv --exclude=/proc/* --exclude=/sys/* --exclude=/tmp/* --exclude=/home/* /* /mnt/root/ # rsync -auHxXv /home/ /mnt/home/
6. Create mdadm.conf in the RAID drive
# /usr/share/mdadm/mkconf > /mnt/root/etc/mdadm/mdadm.conf
7. Modify fstab in the RAID drive
First, get the UUID of the new filesystems:
# blkid | grep /dev/md /dev/md0: UUID="a6ade565-fb52-46ba-ae89-4a5f9740e52d" TYPE="xfs" /dev/md1: UUID="65ad423f-42e9-408d-83d0-d534bc3a719c" TYPE="xfs" /dev/md2: UUID="3adb7f5d-c892-454e-ba11-20cc14e317c5" TYPE="swap"
Then modify the values of /, /home and swap with the new UUID values in /mnt/root/etc/fstab
UUID=a6ade565-fb52-46ba-ae89-4a5f9740e52d / xfs defaults,inode64 0 1 UUID=65ad423f-42e9-408d-83d0-d534bc3a719c /home xfs defaults,inode64 0 2 UUID=ed5d5320-e252-4fbf-9e3a-be1ef8b599a5 swap sw 0 0
8. Install GRUB in the RAID drive
# mount -o bind /dev /mnt/root/dev # mount -t proc none /mnt/root/proc # mount -t sysfs none /mnt/root/sys # chroot /mnt/root # update-initramfs -u -k all # update-grub # grub-install --recheck /dev/sdb
Note: If the BIOS of your computer does NOT allow to select which drive to boot from (only happens in very ancient computers) then you should also update GRUB in the original drive with this command:
# grub-install --recheck /dev/sda
9. Boot using the new RAID drive
# init 6
and select from the BIOS the new RAID drive to boot from.
10. Repartition the old drive
First, make sure everything is running properly:
# mount | grep /dev/md /dev/md0 on / type xfs (rw,relatime,attr2,delaylog,noquota) /dev/md1 on /home type xfs (rw,relatime,attr2,delaylog,inode64,noquota) # swapon -s Filename Type Size Used Priority /dev/md2 partition 8384444 0 -1
If everything is fine, then clone the partition layout of the new RAID into the old drive:
# sfdisk -d /dev/sdb | sfdisk --force /dev/sda
Note: this step is necessary only if the new and old drives has different layouts, if that’s not the case you can ignore this step and just change the type of the old partitions to fd
11. Add the old drive into the RAID
# mdadm /dev/md0 -a /dev/sda1 # mdadm /dev/md1 -a /dev/sda5 # mdadm /dev/md2 -a /dev/sda6
The mirroring will take some time, to check the sync status:
# watch cat /proc/mdstat
12. Install GRUB in the old drive
This is the final step, execute the following:
# grub-install --recheck /dev/sda
Now you have a mirrored system that can boot from both drives.
Leave a Reply