2013-10-01

Mirror is a simple way to gain redundancy on a single Unix server. By building mirrored root disks (as a RAID1 Group), system would be safer in case one disk failed. System still accessible anytime a single disk failure happen.

In this scenario we will try to build a mirrored root disk on Debian Linux server. We will prepare new disk drive as first component of RAID1 group. Then we will copy all content of root disk into it. After data has been copied, we will reboot the server and force the server to boot using the new disk. Once the system up, we will attach existing root disk as second component of RAID1 group. At the end we can use both disk as bootable disk.

Preparation

Lets start by identified existing system :

Platform : this test environment is using Debian Linux release 7.1 (code name : Wheezy) :

Physical disks : we can check installed disks using fdisk -l command as shown in the following example :

On the example above, we can see 2 disk drives installed. First is the active root disk (/dev/sda) and the second one is new disk (/dev/sdb).

ASSUMPTION : For the sake of simplicity we will call the existing root disk (/dev/sda) as “rootdisk”. And the second disk (/dev/sdb) as “rootmirror”.

Filesystem : we also need to know what filesystem used by each partition. To do that we can use blkid command that also can provide us UUID information for each partition :

Partition structure : we can check existing partition structure & its mountpoint by examining the /etc/fstab file :

Or we can use the simplest but slightly less informative df command :

From those two output, we know that :

/dev/sda1 contains /boot filesystem

/dev/sda2 contains root filesystem

/dev/sda3 used as swap space

/dev/sda4 contains /home filesystem

We will use mdadm package for manage RAID group and build the mirror. To install mdadm package in Debian-way, we can use apt-get install command as shown below :

Before moving forward, we need to backup several important files (/etc/fstab,/etc/default/grub, and /etc/mdadm/mdadm.conf) as shown in the following example :

Next we need to load several module to the running kernel :

Information of active & running RAID group can be seen inside /proc/mdstat file :

We can’t see any information yet since we don’t have active RAID group yet. Soon we will go back checking /proc/mdstat when the RAID1 group has been created.

RAID1 Group Setup

We will start create new RAID1 Group for join both disks as mirrored configuration.

First we will work on the new (/dev/sdb) or what we called rootmirror. We need to prepare rootmirror before put it in the RAID1 group. Since rootmirror is new disk, it doesn’t have valid partition table yet :

What we need to do is copy the partition table from the existing root disk (/dev/sda) using sfdisk command as shown below :

After copy the partition table, we can see that rootmirror now has the same partition layout as the root disk :

Since we will join rootmirror into a RAID1 group, we need to set every partition under it to use “Linux raid auto” type. We use fdisk command to set the partition type, here is the sample :

Here is the explanation about what happen on the example above :

Start fdisk utility by typing “fdisk /dev/sdb” followed by Enter. fdisk utility will ready to work on /dev/sdb.

Type t to start setting the partition type. Press Enter to continue.

Then type the number of partition we want to set. Press Enter to continue.

There are many partition type available, we can see all available type by type L followed by Enter.

In this case we want to use partition type “Linux raid auto” so we type fd followed by Enter.

After all partition set up, we must finalize the changes by typing w (as in “Write this changes to the disk”) followed by Enter.

With partition type already set, we can continue create md device a.k.a “virtual partition”. md is abrreviation for Multiple Devices. Before create new md device, we should clean up the partition using the following command :

Actually this step only applicable if the partition has been used as md device. In this case it didn’t really matter since we use new empty disk.

To start create md device, we use mdadm --create command. Here is the syntax to create md device using mdadm command :

We will create md device for each partition. Since we want to create RAID1 disk group, we set --level=1. And because we only have 2 physical disks involved, then we set --raid-disks=2.

So lets start to prepare first partition on rootmirror as first md device :

Because we don’t want to touch the existing root disk (/dev/sda) yet, we will mark it as “missing”.

Then we can continue with second partition of rootmirror :

Finish by create virtual partition for third and fourth partition of rootmirror :

Now we have 4 virtual partition, so the next step is to make the new filesystem on each partition. We will use ext3 filesystem for /dev/md0, /dev/md1, and /dev/md3. To create a ext3 filesystem we use mkfs.ext3 command as shown in the following example :

For /dev/md2, we will marked it as swap space using mkswap command as shown in the following example :

Each virtual partition need to be started during the boot process. So we need to register all the virtual partition we had made into mdadm.conf file. What we need to register is the output of the following command :

We can copy those output to /etc/mdadm/mdadm.conf manually using text editor, or we can use simple redirection as shown below :

In the Linux system each partition will has its own mountpoint (except for swap partition). Information about the partition and related mountpoint stored inside /etc/fstab file. Here is the sample of /etc/fstab file :

Each partition displayed with its UUID (Universally Unique Identifier). Those UUID is belongs to existing root disk (/dev/sda). We need to modify the partition address using new virtual partition identity. Here is the final /etc/fstab after modification :

Next step is to modify /etc/default/grub. We need to set GRUB_DISABLE_LINUX_UUID parameter to true. By default GRUB_DISABLE_LINUX_UUID = true was commented, so what we need is to remove # to uncommented that line.

Before start copying all existing root disk files, we need to mount each virtual partition. Here is the sample how we mount all virtual partitions :

We don’t mount /dev/md2 since it is swap partition.

With all virtual partition mounted, we can start copy files from existing root disk :

Then we need to install the boot loader on the virtual partition. We will do it from chroot environment. There are some steps to prepare chroot environment as shown in the following example :

We can continue to enter chroot environment & execute update-grub command to fix the boot loader as shown in the following example :

Still from chroot environment of virtual partitions, we also need to generate the initramfs image. initramfs is used during boot process to load all root filesystem.

We can ignore those kind of errors for now.

Last things must be done from inside chroot is reinstall boot loader on both disk drives. Debian system use GRUB boot loader by default. To reinstall boot loader we use grub-install command as shown below :

We can exit chroot environment and shutdown the system :

Then we will boot the server using the second disk (/dev/sdb). If all the setup above success, then system can boot using /dev/sdb (rootmirror) disk.

Attach Root Disk To RAID1 Group

After reboot system should already use the virtual partition. As we can see from df command output below :

We can check the RAID1 information for each virtual partition using the following command :

At this stage RAID1 Group only have single component which is rootmirror disk we prepared on previous section. Since we define the RAID1 group to have 2 component, then mdadm -D command will report the degraded state of every virtual partition.

This section will finish mirroring with attaching the previous rootdisk (/dev/sda) to the RAID1 Group.

We need to set the partition type on rootdisk to “Linux raid auto” :

We can check that all partition inside /dev/sda (rootdisk) now has been converted to Linux raid auto type.

Now we can put rootdisk to the existing RAID1 Group. Each partition under rootdisk will be attached to existing virtual partition (md device).

After all partition of rootdisk join the RAID1 group, system will start recovery/resynchronization automatically. We can monitor the recovery process by looking at /proc/mdstat file :

Or we can use mdadm -D command as shown in the following example :

When recovery/resync process completed, both disks will be in sync state. No more degraded status in the mdadm -D output :

Now both /dev/sda & /dev/sdb successfully joined as mirror disks. Both disks will hold the same files. We can verify it by rebooting the server using each disk.

Root Disk Mirroring On Debian Linux

Show more