Add RAID 1 to your pre-existing Ubuntu, originally installed on a single hdd pt.2 -- Hands-On.
September 14th, 2012 by Andrea Matesi 126 ViewsAs promised, here's the follow-up of the previous article, about the addition of a new hdd to you single hdd install of Ubuntu Server, and the subsequent RAID 1 configuration with mdadm.
To be on par with what I'm talking about, you should read my previous post.
I'll be straight with some hands-on.
root@wssrv1:/home/blackmetal# fdisk -l /dev/sdd
Disco /dev/sdd: 160.0 GB, 160041885696 byte
255 heads, 63 sectors/track, 19457 cylinders
Units = cilindri of 16065 * 512 = 8225280 bytes
Disk identifier: 0x00087cdc
Dispositivo Boot Start End Blocks Id System
/dev/sdd1 * 1 122 979933+ 83 Linux
/dev/sdd2 123 18358 146480670 83 Linux
/dev/sdd3 18359 18601 1951897+ 82 Linux swap / Solaris
/dev/sdd4 18602 19457 6875820 83 Linux
The additional disk is a similar one, with the same 160.0 GB size, void and unformatted. I could have used a different sized one (yes, you can have RAID 1 between, say, a 160Gb and a 250Gb one), the only difference is that the resulting RAID array is constrained in size to the smallest disk. Some hands-on now:
root@wssrv1:/home/blackmetal# fdisk /dev/sde
let's press p to see the situation on that disk.
(p, ENTER)
Disco /dev/sde: 160.0 GB, 160041885696 byte
255 heads, 63 sectors/track, 19457 cylinders
Units = cilindri of 16065 * 512 = 8225280 bytes
Disk identifier: 0x02e4e15c
Dispositivo Boot Start End Blocks Id System
OK, this shows us the disk is blank and unpartitioned: you'll need to create the same sdd partition table structure here, with one exception: the partitions' type should be set to Linux RAID (the code representing the Linux RAID type is “fd”).
Let's open fdisk on the new disk and let's input some commands:
fdisk /dev/sde
then let's feed fdisk with the following commands:
(o, ENTER) → will make a new partition table.
(n, p, 1, 1, 122) → will create the first partition (on my case is the /boot partition).
(n, p, 2, ENTER, 18358) → will create the secon partition (on my case this is the SLASH partition).
(n, p, 3, ENTER, 18601) → will create the third partition (on my case this is the swap partition).
(n, p, ENTER, ENTER) → will create the last partition (on my case this is the /tmp partition).
If you're with me till now, is now time to specify what type this partitions should be, so let's type, inside fdisk:
(t, 1, fd) → will make the /boot as Linux RAID
(t, 2, fd) → will make the / as Linux RAID
(t, 3, 82) → wil make the swap...swap type
(t, 4, fd) → will male the tmp as Linux RAID
Let's add the BOOT FLAG on the new HD:
(a, 1) → will switch the boot flag ON
To be sure everything is correct let's print the disk's partition table:
(p, ENTER)
Disco /dev/sde: 160.0 GB, 160041885696 byte
255 heads, 63 sectors/track, 19457 cylinders
Units = cilindri of 16065 * 512 = 8225280 bytes
Disk identifier: 0x86d37230
Dispositivo Boot Start End Blocks Id System
/dev/sde1 * 1 122 979933+ fd Autorilevamento raid di Linux
/dev/sde2 123 18358 146480670 fd Autorilevamento raid di Linux
/dev/sde3 18359 18601 1951897+ 82 Linux swap / Solaris
/dev/sde4 18602 19457 6875820 fd Autorilevamento raid di Linux
If everything is fine, we're finally done, let's write the settings on the disk's partion table:
(w, ENTER)
OK, now we should install mdadm, so let's get it:
apt-get install initramfs-tools mdadm
Let's reboot the system to be sure all the necessary md modules get loaded.
Fine, at this point we have two similar disks with the same partition table and mdadm installed; take care and review the previous steps and make sure everything is the same.
The next step consists of creating the RAID 1 array between the respective partitions. We'll use the handy mdadm command line tool. Since we have four partitions to put on RAID, in the end we'll have:
md0 → /bootmd1 → /md2 → swapmd3 → /tmp
so let's type:
mdadm --create /dev/md0 --level=1 --raid-disks=2 missing /dev/sde1
mdadm --create /dev/md1 --level=1 --raid-disks=2 missing /dev/sde2
mdadm --create /dev/md2 --level=1 --raid-disks=2 missing /dev/sde3
mdadm --create /dev/md3 --level=1 --raid-disks=2 missing /dev/sde4
Now we need to create a File System on the arrays (remember that with fdisk we created the disk's partition table, now it's time for some real filesystem):
mkfs.ext3 /dev/md0
mkfs.ext3 /dev/md1
mkswap /dev/md2
mkfs.ext3 /dev/md3
We need to load at boot time the actual array, so to store the settings inside the mdadm.conf file we can launch this command:
mdadm --examine --scan >> /etc/mdadm/mdadm.conf
To be sure it's OK, we should see at EOF something like that:
cat /etc/mdadm/mdadm.conf
[...]
ARRAY /dev/md0 level=raid1 num-devices=2 UUID=09537bd5:b63d7a70:9a857d35:0291bc81
ARRAY /dev/md1 level=raid1 num-devices=2 UUID=f7b7e6a0:fb35d21a:9a857d35:0291bc81
ARRAY /dev/md2 level=raid1 num-devices=2 UUID=aafe9a7b:9719077a:9a857d35:0291bc81
ARRAY /dev/md3 level=raid1 num-devices=2 UUID=8c893d7d:67d7a5c3:9a857d35:0291bc81
…and verify at EOF if there is mdX entries similar to the ones shown here.
Now we need to copy the contents of the running, non-RAID system, inside the array, so we need to mount the array and copy the system as-is. For this task I'd use rsync with my fav options ([LINK ARTICOLO RSYNC] -aHhv –progress with the exception of adding the additional -x option to keep the program scan only a filesystem boundary).
So let's create the mount points somewhere and let's mount the md devices:
mkdir /mnt/md0
mkdir /mnt/md1
mkdir /mnt/md3
mount /dev/md0 /mnt/md0
mount /dev/md1 /mnt/md1
mount /dev/md3 /mnt/md3
(no need to mount swap).
Before tranfering the files, we need to update the fstab with the new informations,
First let's make a backup copy of the files:
cp /etc/fstab /etc/fstab.ori
cp /etc/mtab /etc/mtab.ori
let's collect the UUIDs from the arrays:
blkid /dev/md0
/dev/md0: UUID="78b45131-7946-4989-8398-f06146c0f4de" TYPE="ext3"
root@wssrv1:/home/blackmetal# blkid /dev/md1
/dev/md1: UUID="604dbec8-d6e5-4bd4-a10e-21b951708c63" TYPE="ext3"
root@wssrv1:/home/blackmetal# blkid /dev/md2
/dev/md2: UUID="7fed7933-e14e-4e69-870c-889573861e73" TYPE="swap"
root@wssrv1:/home/blackmetal# blkid /dev/md3
/dev/md3: UUID="4e8a252f-55f1-438e-9cbd-014d7d610555" TYPE="ext3"
and the let's edit the fs files, essentially replacing sddX with mdX:
vi /etc/fstab
root@wssrv1:/home/blackmetal# cat /etc/fstab
# /etc/fstab: static file system information.
#
# <file system> <mount point> <type> <options> <dump> <pass>
proc /proc proc defaults 0 0
# /dev/md0 = sdd1 + sde1 -> /boot
UUID=78b45131-7946-4989-8398-f06146c0f4de /boot ext3 relatime 02
# /dev/md1 = sdd2 + sde2 -> /
UUID=604dbec8-d6e5-4bd4-a10e-21b951708c63 / ext3 relatime,errors=remount-ro 0 1
# /dev/md2 = sdd3 + sde3 -> swap
UUID=7fed7933-e14e-4e69-870c-889573861e73 none swap sw 0 0
# /dev/md3 = sdd4 + sde4 -> /tmp
UUID=4e8a252f-55f1-438e-9cbd-014d7d610555 /tmp ext3 relatime 0 2
# dvd-rw
/dev/scd0 /media/cdrom0 udf,iso9660 user,noauto,exec,utf8 0 0
Now let's edit the mtab, by replacing the /dev/sddX with the respective mdX:
vi /etc/mtab
root@wssrv1:/home/blackmetal# cat /etc/mtab
/dev/md1 / ext3 rw,relatime,errors=remount-ro 0 0
proc /proc proc rw,noexec,nosuid,nodev 0 0
/sys /sys sysfs rw,noexec,nosuid,nodev 0 0
varrun /var/run tmpfs rw,noexec,nosuid,nodev,mode=0755 0 0
varlock /var/lock tmpfs rw,noexec,nosuid,nodev,mode=1777 0 0
udev /dev tmpfs rw,mode=0755 0 0
devshm /dev/shm tmpfs rw 0 0
devpts /dev/pts devpts rw,gid=5,mode=620 0 0
/dev/md0 /boot ext3 rw,relatime 0 0
/dev/md3 /tmp ext3 rw,relatime 0 0
securityfs /sys/kernel/security securityfs rw 0 0
binfmt_misc /proc/sys/fs/binfmt_misc binfmt_misc rw,noexec,nosuid,nodev 0 0
We now need to update the menu.lst from grub to reflect the changes, so let's edit it and apply the changes accordingly:
vi /boot/grub/menu.lst
[...]
title Ubuntu 8.04.1, kernel 2.6.24-19-server
root (hd1,0)
kernel /vmlinuz-2.6.24-19-server root=UUID=604dbec8-d6e5-4bd4-a10e-21b951708c63 ro quiet splash
initrd /initrd.img-2.6.24-19-server
quiet
title Ubuntu 8.04.1, kernel 2.6.24-19-server (recovery mode)
root (hd1,0)
kernel /vmlinuz-2.6.24-19-server root=UUID=604dbec8-d6e5-4bd4-a10e-21b951708c63 ro single
initrd /initrd.img-2.6.24-19-server
title Ubuntu 8.04.1, memtest86+
root (hd1,0)
kernel /memtest86+.bin
quiet
### END DEBIAN AUTOMAGIC KERNELS LIST
Now we need to update the ramdisk:
update-initramfs -u
OK, time to transfer files from the running system to the new RAID 1 system with rsync:
cd / && rsync -axHhv --progress / /mnt/md1
cd /boot && rsync -axHhv --progress . /mnt/md0
cd /tmp && rsync -axHhv --progress . /mnt/md2
As soon as the transfer is done, now it is time to setup grub; so let's launch it from the command line, and let's apply all the settings:
grub
(from inside grub)
root (hd0,0)
setup (hd0)
root (hd1,0)
→ root (hd1,0)
setup (hd1)
→ setup (hd1)
Checking if "/boot/grub/stage1" exists... no
Checking if "/grub/stage1" exists... yes
Checking if "/grub/stage2" exists... yes
Checking if "/grub/e2fs_stage1_5" exists... yes
Running "embed /grub/e2fs_stage1_5 (hd2)"... 16 sectors are embedded.
succeeded
Running "install /grub/stage1 (hd2) (hd2)1+16 p (hd2,0)/grub/stage2 /grub/menu.lst"... succeeded
Done.
PERSONAL NOTE – just ignore it! I have an IDE disk and two SATA disks wich I'm putting on RAID; on my case, I had to specify the disk as HD2,0 instead than HD1,0, and so I needed to correct the grub menu.lst accordingly.
At this point, if everything was set correctly, It's time to reboot, so close your shells and cross your fingers!
reboot && logout
Posted in LINUX, System Administration | No Comments »