Migrating to a RAID1 array in RHEL5.4

Posted on November 29th, 2009 by hamstar

OK, so I just had a bitch of a time migrating from a live single disk redhat enterprise linux 5.4 box to a booting dual disk RAID 1 setup… There is pretty much nothing on the net about this for redhat especially to do with making kernels which almost none of the debian guides seem to care about.

So I played it by ear and I’m going to post how I did it so you won’t have to. You’ll need to be using mdadm. I’m going to assume the partitions you want raided are /dev/sda1 and /dev/sdb1 and that you are currently booted to /dev/sda1. A rescue disk will also be helpful.

We are going to get a mirror going with just /dev/sdb1 (empty I hope) in it. Then we’ll copy everything over to it so everything is the same after reboot when it does a resync. Makes your data a bit safer.

Here we go…

Have you tried to setup raid before on these drives? This checks them for superblocks.

mdadm --examine /dev/sda1
mdadm --examine /dev/sda1

If it spat up lines like “ARRAY blah blah UUID=34cd3:etc”, zero them. Don’t worry not dangerous to data.

mdadm --zero-superblock /dev/sda1
mdadm --zero-superblock /dev/sdb1

OK now we want to copy the partition tables over:

sfdisk -d /dev/sda1 | sfdisk /dev/sdb

Now we create the raid array in a degraded mode with the master drive missing and format it to your filesystem.

mdadm --create /dev/md0 --level 1 --raid-devices=2 missing /dev/sdb1
mkfs.ext3 /dev/md0

Have a look at your new RAID:

mdadm --detail /dev/md0
cat /proc/mdstat

Now we need to make sure that the kernel modules for scsi and raid are built in to the kernel. I am not actually sure if this step is needed but it worked on mine.

cd /usr/src/kernels/2.6.18-lots-of-numbers/
make menuconfig

Now you’ll be presented with the kernel modules config screen. Find Device Drivers then in SCSI Device Support. In this section we need to make sure a few things have the asterisk in the brackets (should mean built in on the legend). These things for me were RAID Transport Class, SCSI device support, SCSI disk support and SCSI generic support. Press spacebar on each of them to change the M or blank in the brackets to an asterisk. Then exit out of it saving your config as you leave.

Now the important part – creating the kernel image. This worked for me… at about the sixth time and exhaustive google searches:

mkinitrd --preload=raid1 --with=raid1 --builtin=raid1 --force-scsi-probe --force-raid-probe /boot/initrd-`uname -r`-raid.img `uname -r`

Now you want to modify /boot/grub/grub.conf and /boot/grub/device.map.

Need to make sure we have both devices in there
/boot/grub/device.map:

(hd0)    /dev/sda
(hd1)    /dev/sdb

Need to make a backup bootloader entry and a raided one too
/boot/grub/grub.conf:

default=0
fallback=1
timeout=5
splashimage=(hd1,0)/boot/grub/splash.xpm.gz
hiddenmenu
title RHEL RAID
        root (hd1,0)
        kernel /boot/vmlinuz-2.6.23.1-42 root=/dev/md0 ro rhgb quiet
        initrd /boot/initrd-2.6.23.1-42-raid.img

title RHEL Non-RAID
        root (hd0,0)
        kernel /boot/vmlinuz-2.6.23.1-42 root=/dev/sda1 ro rhgb quiet
        initrd /boot/initrd-2.6.23.1-42.img

That boot list boots off /dev/sdb before and loads into the system with the raid array active. This allows you to add /dev/sda1 into the array. If it booted off /dev/sda1 (e.g. hd0) then /dev/sda1 would be locked and you would not be able to do the mdadm command.

It also allows you to boot without the raid array if your raid setup failed to boot (i.e. kernel panic). The setting for root in fstab doesn’t matter if its set to /dev/md0, /dev/sda1 still becomes the root drive.

Now install grub on /dev/sdb:

grub
root (hd1)
setup (hd1)

Now change the root device in your fstab to be /dev/md0
/etc/fstab:

/dev/md0    /    ext3    defaults    1  1

Now we need to copy everything over to the raid array, however it will probably crash out copying with the running system like mine did, but not to worry we are pretty much ready to go here.

Shut down and boot into a linux rescue disk (the first cd of RHEL5.4 has it), and get to the command line, don’t bother mounting filesystems or anything.
Try this:

mdadm --assemble --scan
mdadm --assemble /dev/md0

If that doesn’t work then try this:

mdadm --create /dev/md0 --level 1 --raid-devices=2 missing /dev/sdb1 --assume-clean

and push y if it asks you to create over another array.

Create mountpoints and mount your drives:

cd /mnt
mkdir sda1 md0
mount /dev/sda1 sda1
mount /dev/md0 md0

Now copy everything over:

cp -dpRx /mnt/sda1/* /mnt/md0/

Wait a while till that is done then reboot. Hopefully you don’t get a kernel panic, and it boots up on the degraded /dev/md0 array. If it does then add your /dev/sda1 in like follows:

mdadm --add /dev/md0 /dev/sda1

Now you can watch the resync progress like this:

watch -n 2 cat /proc/mdstat

mdadm is pretty smart.

Here’s some other links that may help you out if you’re stuck:
http://wiki.clug.org.za/wiki/RAID-1_in_a_hurry_with_grub_and_mdadm
http://www.mail-archive.com/linux-raid@vger.kernel.org/msg09672.html
http://www.debian-administration.org/articles/238
http://unthought.net/Software-RAID.HOWTO/Software-RAID.HOWTO-7.html

Tags: , , , , , , , ,

Process multiple items in command line

Posted on December 8th, 2007 by hamstar

This is great, I saw it somewhere the other day but can’t remember where. It is pretty simple too… sometimes when I do stuff on the command line with cut I try to run multiple commands on the output, but it never works cos I try to it with sed but with this method it works great.

Check it out:

for i in `ps auwx|grep hamstar|cut -f2 -d’ ‘`; do kill $i; done

That would kill all the processes that I have started under my username. Notice the red part is where it takes all the entries (puts each one into $i as it uses it, and the blue part is where it does something with the entries ($i again)). Adding kill to the start and ; to the end using sed would not work for this.

Ok that might a bit tricky for some of you, especially if you haven’t come across the miracle of piping yet, let me show you a simpler one.

for i in *; do echo $i; done

Basically another form of ls but thing of the things you can do with this. Rename from lowercase to uppercase:

for i in *; do mv $i `echo $i|tr [A-Z] [a-z]`; done

So its basically limitless to what you can do with this cool thing.

I also found another cool thing while writing this. Doing the following will covert all characters in a given file to lowercase or uppercase if you changed it:

dd file.txt conv=lcase

Tags: , ,

Open a remote machines window on your desktop

Posted on August 21st, 2007 by hamstar

So I found out a little while ago that the X server can do this thing where it outputs the window from the machine its running on, to a remote machine, also running an X server. I did this a few times and its pretty damn cool. Basically you simply open a console, set the DISPLAY variable to something like 10.0.0.2:0 and it will open on that IP and that display once you add your machine to that machines xhost list. You follow me? No?

Okay, here it is broken down. Lets say, you have two linux boxes, both running GUI’s, and you want to open gedit on one machine (machine1), but display it on the other machine (machine2):

Read more »

Tags: , ,

Open a program from one linux box on another boxes screen

Posted on April 7th, 2007 by hamstar

How cool would it be to be able run a program from your main computer, but have the window for it come up on say, your laptop while you’re sitting on your bed? Thats what I do.

My main problem is that I can’t get internet on my laptop at home. I’m on dialup and my external modem only connects by serial, which my laptop doesn’t have. Problem.

Solution. I can ssh into my main box which has the modem, connect it to the net and run firefox and it will open it on my laptop screen, using the network. This is a built in feature of X, the display manager for linux.

This HOWTO assumes the following:

  1. You already have ssh setup on the main box
  2. The main box is called serj with an IP of 10.0.0.1
  3. The other box is called malakian with an IP of 10.0.0.4

Here’s how ya do it:

  1. Open up a console window
  2. Add the other box to your X “allow” list by typing the following:
    xhost serj


  3. Find out your display number and remember it (lets say it is 1). Type:
    echo $DISPLAY


  4. SSH into your main box from the other box
    ssh serj -l <em>username</em>


  5. Once you’ve logged in, you need to edit the DISPLAY variable to be the IP and display number (1) of the box you’re on and export it:
    DISPLAY=10.0.0.4:1; export DISPLAY


  6. Once you’ve done that check that it is set properly by echo’ing the DISPLAY variable again. It should show 10.0.0.4:1
    echo $DISPLAY
  7. Now simply run a program like firefox from the command line and the window will open on your box:
    firefox

When you run a program it will open with all the settings from your main box. Unfortunately it won’t work with 3D games but there are a few things it could be useful for e.g.:

  1. Administering one of your screenless servers through an actual GUI (provided you installed with KDE/gnome/some other window manager)
  2. Opening dirty sites on your friends/workmates PC. Open a console, set your display to your workmates IP/display and run
    firefox http://nastyjapanesegirls.com

    . Of course it will be running of your profile so he won’t get done for porn if you do this at work… you will.

There is one draw back though, all sound will run on the main box.

Tags: , ,