FirstServed Tech Blog - FirstServed and the Art of Server Tuning

Archive for the ‘RAID’ Category

Booting XenServer or XenEnterprise from software RAID

Thursday, June 7th, 2007

Since XenServer and XenEnterprise do not support installing the operating system on an MD software RAID device during the installation, you’ll have to undertake a few steps afterwards if you want to mirror your boot disks.

This article is largely based on Harry de Jong’s solution, as posted on the XenSource forums, with a few differences:

  • There’s no need to copy mdadm.static from another server to the Xen host;
  • The initrd ramdisk’s configuration is a bit simpler;
  • There’s no need to swap the physical disks, which means the procedure can be accomplished remotely.

Create identical partitions to your boot disk /dev/sda on your second disk /dev/sdb

If /dev/sda looks like this:

# fdisk -l /dev/sda
Disk /dev/sda: 249.3 GB, 249376538624 bytes
255 heads, 63 sectors/track, 30318 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1         499     4008186   83  Linux
/dev/sda2             500         998     4008217+  83  Linux
/dev/sda3             999       30318   235512900   83  Linux

/dev/sdb should look like this:

# fdisk -l /dev/sdb
Disk /dev/sdb: 249.3 GB, 249376538624 bytes
255 heads, 63 sectors/track, 30318 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1   *           1         499     4008186   fd  Linux raid autodetect
/dev/sdb2             500         998     4008217+  fd  Linux raid autodetect
/dev/sdb3             999       30318   235512900   fd  Linux raid autodetect

Do not forget to set the partition type to ‘fd’ (Linux RAID autodetect) instead of the default 83 (Linux).

Create the necessary device nodes

# mknod /dev/md0 b 9 0
# mknod /dev/md1 b 9 1
# mknod /dev/md2 b 9 2

Create your MD RAID 1 arrays (with missing disks)

# mdadm –create /dev/md0 –level=1 –raid-devices=2 /dev/sdb1 missing
# mdadm –create /dev/md1 –level=1 –raid-devices=2 /dev/sdb2 missing
# mdadm –create /dev/md2 –level=1 –raid-devices=2 /dev/sdb3 missing

/dev/sda2 is just an empty, unmounted partition used by XenServer for upgrades.

 

Copy the Xen Storage Manager data over to RAID

# pvcreate /dev/md2
# vgextend VG_XenStorage-3553b468-fca7-46c0-baeb-7cd471a6a9ab /dev/md1
# pvmove /dev/sda3 /dev/md2

Replace the uuid in the vgextend line above with the uuid of your own Xen SR.  Use ‘sm info’ to display your SR’s uuid.

Remove /dev/sda3 from the SR volume group and add it to the RAID array

# vgreduce VG_XenStorage-3553b468-fca7-46c0-baeb-7cd471a6a9ab /dev/sda3
# pvremove /dev/sda3
# mdadm -a /dev/md2 /dev/sda3

Mount /dev/md0 and copy the filesystem to it

# mkfs.ext3 /dev/md0
# mount /dev/md0 /mnt
# cd /
# cp -axv . /mnt

Make a new initrd ramdisk containing the MD RAID drivers

Modify /mnt/etc/fstab so the system will mount / from /dev/md0 instead of LABEL=/-main.  Replace "LABEL=/-main" by "/dev/md0".

Create a new boot image and uncompress it:

# mkdir /mnt/root/initrd-raid
# mkinitrd –fstab=/mnt/etc/fstab /mnt/root/initrd-raid/initrd-2.6.16.38-xs3.2.0.531.3960xen-raid.img 2.6.16.38-xs3.2.0.531.3960xen
# cd /mnt/root/initrd-raid
# zcat initrd-2.6.16.38-xs3.2.0.531.3960xen-raid.img | cpio -i

Since mkinitrd looks at /etc/fstab to determine what device the root volume is on, it has now added the necessary raid drivers to the new boot image.

Uncompress the current ramdisk and add the raid drivers to it

# mkdir /root/initrd
# cd /root/initrd
# zcat /boot/initrd-2.6.16.38-xs3.2.0.531.3960xen.img | cpio -i

Now add the raid module from the new ramdisk and modify the init file:

# cp /root/initrd-raid/lib/raid1.ko lib
# vi init

Add the following lines before the second line containing "/sbin/udevstart":

echo "Loading raid1.ko module"
insmod /lib/raid1.ko

 

Add the following lines before the line containing "echo Creating root device":

raidautorun /dev/md0
raidautorun /dev/md1
raidautorun /dev/md2

Note: if you’ve created other MD raid devices, add a ‘raidautorun’ statement for them as well.

Copy the new ramdisk to the /mnt/boot folder and add modify GRUB’s boot menu

# find . -print | cpio -o -Hnewc | gzip -c > /mnt/boot/initrd-2.6.16.38-xs3.2.0.531.3960xen-raid.img
# rm /mnt/boot/initrd-2.6-xen.img
rm: remove symbolic link `/mnt/boot/initrd-2.6-xen.img’? y
# ln -s initrd-2.6.16.38-xs3.2.0.531.3960xen-raid.img /mnt/boot/initrd-2.6-xen.img
# vi /mnt/boot/grub/menu.lst

Replace "root=LABEL=/-main" by "root=/dev/md0" in all menu entries.

Unmount /dev/md0

# cd /root
# umount /dev/md0
# sync

Set up the Master Boot Record on /dev/sdb

# grub

grub> device (hd0) /dev/sdb

grub> root (hd0,0)
 Filesystem type is ext2fs, partition type 0xfd

grub> setup (hd0)
 Checking if "/boot/grub/stage1" exists… yes
 Checking if "/boot/grub/stage2" exists… yes
 Checking if "/boot/grub/e2fs_stage1_5" exists… yes
 Running "embed /boot/grub/e2fs_stage1_5 (hd0)"…  16 sectors are embedded.
succeeded
 Running "install /boot/grub/stage1 (hd0) (hd0)1+16 p (hd0,0)/boot/grub/stage2
/boot/grub/grub.conf"… succeeded
Done.

grub> quit

 

Modify GRUB’s boot menu to boot from the second disk

# vi /boot/grub/menu.lst

Replace the line in the first XenServer entry that says "root (hd0,0)" with "root (hd1,0)". Also replace "root=LABEL=/-main" by "root=/dev/md0" in all menu entries.  This will allow you to boot from /dev/md0 instead of /dev/sda.  In case something goes wrong, you could always reboot using one of the other entries, which still point to /dev/sda.

Reboot

# shutdown -r now

If all goes well, you should see your system mounting /dev/md0 as the filesystem root.  If not, reboot using one of the other GRUB menu entries and check out the previous steps.

Change /dev/sda’s partition types from Linux to Linux RAID autodetect

# fdisk /dev/sda

The result should be something like this:

Disk /dev/sda: 249.3 GB, 249376538624 bytes
255 heads, 63 sectors/track, 30318 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1         499     4008186   fd  Linux raid autodetect
/dev/sda2             500         998     4008217+  fd  Linux raid autodetect
/dev/sda3             999       30318   235512900   fd  Linux raid autodetect

Add the missing partitons to the RAID arrays

# mdadm -a /dev/md0 /dev/sda1
mdadm: hot added /dev/sda1
# mdadm -a /dev/md1 /dev/sda2
mdadm: hot added /dev/sda2
# mdadm -a /dev/md2 /dev/sda3
mdadm: hot added /dev/sda3

Then wait for the sync to complete:

# watch cat /proc/mdstat

Copy the running RAID setup to /etc/mdadm.conf

# mdadm –detail –scan >> /etc/mdadm.conf

Be sure to add a line ‘DEVICE partitions’ at the top of /etc/mdadm.conf if you want to define MD devices that span a whole disk instead of just a partition.  Without this line, MD won’t be able to detect your drives as MD components at boot.

That’s it!

Grub, RAID 1 and USB sticks

Thursday, May 10th, 2007

We recently took to installing our Linux servers from a USB stick.  This is much more efficient than downloading four of five cd iso’s time and again, since Linux distros like Fedora and CentOS keep spawning new releases constantly.  So we just write the rescue cd iso to a usb stick, and proceed with a network install.  This works like a charm, except that for a few times running, after completing the installation, instead of being met with the grub boot menu, we were dropped to the Grub shell brutally.  This had us stmied for a while, until we realized that for some dumb reason or other, Grub insists on recognizing the USB stick we install from as device hd0.  This means that during installation, Grub writes itself a nice config file containing the line

root (hd1,0)

which points to the second drive, in our case our first hard disk, /dev/sda.  After installation – without the USB stick, Grub sees /dev/sda as the first device, hd0, and proceeds to boot the hd1 device, which now is /dev/sdb instead of /dev/sda.  Since the MBR (master boot record) was only written to /dev/sda, there’s no way Grub can boot from /dev/sdb.

In order to solve all this, from within the Grub shell, do the following:

device (hd0) /dev/sda
root (hd0,0)
setup (hd0)

device (hd0) /dev/sdb
root (hd0,0)
setup (hd0)

reboot

This will instruct Grub to see both the hard disks as hd0 – since in case the first hard disk fails, /dev/sdb is effectively the first device, and install the MBR and Grub on them.  After this, reboot and do not forget to edit your grub.conf.  Change the line that reads

root (hd1,0)

to

root (hd0,0)

This way, Grub will once again boot from its first device as it was meant to.

Installing Grub and the MBR on the second device of your RAID 1 array should be done in any case, even if you don’t experience USB stick troubles.  If you do not, in case of a failure of your first hard disk, your system will be unable to boot until you install the MBR and Grub on your second hard disk.