Convert to RAID

From VVCWiki
Revision as of 15:39, 22 July 2009 by Vvc (talk | contribs) (→‎Install bootloader on second disk)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

Partition new disk

[root@centos ~]# fdisk -l /dev/hda

Disk /dev/hda: 20.0 GB, 20020396032 bytes
255 heads, 63 sectors/track, 2434 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/hda1   *           1          13      104391   83  Linux
/dev/hda2              14        2434    19446682+  8e  Linux LVM
[root@centos ~]# fdisk -l /dev/hdb

Disk /dev/hdb: 40.0 GB, 40000000000 bytes
255 heads, 63 sectors/track, 4863 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System

[root@centos ~]# cat /etc/fstab 
/dev/vg0/root           /                       ext3    noatime         1 1
/dev/vg0/tmp            /tmp                    ext2    noatime         1 2
/dev/vg0/home           /home                   ext3    noatime         1 2
/dev/vg0/var            /var                    ext3    noatime         1 2
/dev/vg0/usr            /usr                    ext3    noatime         1 2
LABEL=/boot             /boot                   ext2    noatime         1 2
tmpfs                   /dev/shm                tmpfs   defaults        0 0
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0
/dev/vg0/swap           swap                    swap    defaults        0 0

[root@centos ~]# fdisk /dev/hdb 

The number of cylinders for this disk is set to 4863.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
   (e.g., DOS FDISK, OS/2 FDISK)

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-4863, default 1): 
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-4863, default 4863): 13

Command (m for help): t
Selected partition 1
Hex code (type L to list codes): FD
Changed system type of partition 1 to fd (Linux raid autodetect)

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 2
First cylinder (14-4863, default 14): 
Using default value 14
Last cylinder or +size or +sizeM or +sizeK (14-4863, default 4863): +512M

Command (m for help): t
Partition number (1-4): 2
Hex code (type L to list codes): 82
Changed system type of partition 2 to 82 (Linux swap / Solaris)

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 3
First cylinder (77-4863, default 77): 
Using default value 77
Last cylinder or +size or +sizeM or +sizeK (77-4863, default 4863): 2434

Command (m for help): t
Partition number (1-4): 3
Hex code (type L to list codes): FD
Changed system type of partition 3 to fd (Linux raid autodetect)

Command (m for help): p

Disk /dev/hdb: 40.0 GB, 40000000000 bytes
255 heads, 63 sectors/track, 4863 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/hdb1               1          13      104391   fd  Linux raid autodetect
/dev/hdb2              14          76      506047+  82  Linux swap / Solaris
/dev/hdb3              77        2434    18940635   fd  Linux raid autodetect

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

The new disk was larger, partition sizes were calculated based on old disk size.

Switch to new swap partition

Note: You have to make a decision what is more important to you. If you want your system to continue to run in case of a hard disk failure, you need to go with 'swap on raid ' approach: don't create separate swap partitions, but instead create a swap logical volume in the new volume group. If performance is more important, then you split your swap between two disks and create separate swap partitions on each disk, as I did in this document.
[root@centos ~]# mkswap -L swapb /dev/hdb2 
Setting up swapspace version 1, size = 518184 kB
LABEL=swapb, no uuid

[root@centos ~]# swapon /dev/hdb2 
[root@centos ~]# swapoff /dev/vg0/swap 

Install required packages

yum -y install mdadm dump

Create degraded RAID arrays

[root@centos ~]# mdadm --create /dev/md0 --level=1 --raid-devices=2 /dev/hdb1 missing
mdadm: array /dev/md0 started.
[root@centos ~]# mdadm --create /dev/md1 --level=1 --raid-devices=2 /dev/hdb3 missing
mdadm: array /dev/md1 started.
[root@centos ~]# mdadm --examine --scan > /etc/mdadm.conf

Create new volume group

[root@centos ~]# pvcreate /dev/md1 
  Physical volume "/dev/md1" successfully created

[root@centos ~]# vgcreate -s 32M vg1 /dev/md1
  Volume group "vg1" successfully created

[root@centos ~]# lvcreate --size 512M --name root vg1
  Logical volume "root" created
[root@centos ~]# lvcreate --size 4G --name usr vg1
  Logical volume "usr" created
[root@centos ~]# lvcreate --size 1G --name var vg1
  Logical volume "var" created
[root@centos ~]# lvcreate --size 1G --name home vg1
  Logical volume "home" created
[root@centos ~]# lvcreate --size 256M --name tmp vg1
  Logical volume "tmp" created

Format new filesystems

mkfs -j /dev/vg1/root 
mkfs -j /dev/vg1/usr
mkfs -j /dev/vg1/var
mkfs -j /dev/vg1/home
mkfs /dev/vg1/tmp
mkfs /dev/md0

Shutdown all services

Keep sshd running, stop the rest

service  crond stop
service syslog stop
...

Mount/transfer root volume

mount -t ext3 /dev/vg1/root /mnt/
cd /mnt/
dump -0 -f - /dev/vg0/root | restore -r -f -
rm restoresymtable

Mount/transfer remaining volumes

mount -t ext3 /dev/vg1/usr /mnt/usr/
mount -t ext3 /dev/vg1/var /mnt/var/
mount -t ext3 /dev/vg1/home /mnt/home/
mount -t ext2 /dev/vg1/tmp /mnt/tmp/
chmod 1777 /mnt/tmp
mount -t ext2 /dev/md0 /mnt/boot/

cd /mnt/usr
dump -0 -f - /dev/vg0/usr | restore -r -f -
rm restoresymtable

cd /mnt/var
dump -0 -f - /dev/vg0/var | restore -r -f -
rm restoresymtable

cd /mnt/home
dump -0 -f - /dev/vg0/home | restore -r -f -
rm restoresymtable

cd /mnt/boot
dump -0 -f - /dev/hda1 | restore -r -f -
rm restoresymtable

No need to dump/restore /tmp

chroot to new system

 
mkdir /mnt/dev/pts
mount -t devpts devpts /mnt/dev/pts
mount -t sysfs sysfs /mnt/sys 
mount -t proc proc /mnt/proc 
mkdir /mnt/dev/shm
mount -t tmpfs tmpfs /mnt/dev/shm

chroot /mnt
start_udev

adjust /etc/fstab

[root@centos /]# cat /etc/fstab 
/dev/vg1/root           /                       ext3    noatime         1 1
/dev/vg1/tmp            /tmp                    ext2    noatime         1 2
/dev/vg1/home           /home                   ext3    noatime         1 2
/dev/vg1/var            /var                    ext3    noatime         1 2
/dev/vg1/usr            /usr                    ext3    noatime         1 2
/dev/md0	        /boot                   ext2    noatime         1 2
tmpfs                   /dev/shm                tmpfs   defaults        0 0
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0
LABEL=swapb             swap                    swap    pri=1           0 0

adjust /etc/grub.conf

default=1
timeout=5
#splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
title CentOS (2.6.18-128.2.1.el5)
	root (hd0,0)
	kernel /vmlinuz-2.6.18-128.2.1.el5 ro root=/dev/vg0/root
	initrd /initrd-2.6.18-128.2.1.el5.img
title CentOS (2.6.18-128.2.1.el5)
	root (hd1,0)
	kernel /vmlinuz-2.6.18-128.2.1.el5 ro root=/dev/vg1/root
	initrd /initrd-2.6.18-128.2.1.el5.img

adjust /etc/mdadm.conf

DEVICE partitions
MAILADDR root
ARRAY /dev/md0 level=raid1 num-devices=2 UUID=8ca072a8:085c91c0:52eada7c:6cc0931c
ARRAY /dev/md1 level=raid1 num-devices=2 UUID=898f5b3c:670dcb6c:d422cdf5:eddafb24

recreate initrd image

cd /boot
mkinitrd -f initrd-2.6.18-128.2.1.el5.img -v 2.6.18-128.2.1.el5

copy new grub.conf to old disk

exit # from chroot
cp /mnt/etc/grub.conf /etc/grub.conf

Update bootloader configuration

 grub-install --recheck /dev/hda

reboot to new system on the raid

reboot

verify new installation

Make sure system is operational and everything is in order. Up to this step all changes made were non-destructive and you could boot your old system from the grub menu.

Delete old volume group

This step is not really necessary, you could have reformatted the first disk, but this OS-friendly approach.

lvremove /dev/vg0/root
lvremove /dev/vg0/usr
lvremove /dev/vg0/var
lvremove /dev/vg0/tmp
lvremove /dev/vg0/swap
vgremove vg0
pvremove /dev/hda2

delete and create new partitions on the old disk

You the same commands as before

[root@centos ~]# fdisk -l /dev/hda

Disk /dev/hda: 20.0 GB, 20020396032 bytes
255 heads, 63 sectors/track, 2434 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/hda1               1          13      104391   fd  Linux raid autodetect
/dev/hda2              14          76      506047+  82  Linux swap / Solaris
/dev/hda3              77        2434    18940635   fd  Linux raid autodetect

Add additional swap

 
mkswap -L swapa /dev/hda2

Add entry to /etc/fstab

LABEL=swapa             swap                    swap    pri=1           0 0

Add partitions to raid

mdadm --add /dev/md0 /dev/hda1
mdadm --add /dev/md1 /dev/hda3

wait until RAID fully synchronized:

# watch cat /proc/mdstat

 Every 2.0s: cat /proc/mdstat                                                                    Wed Jul 22 18:21:38 2009

Personalities : [raid1]
md0 : active raid1 hda1[1] hdb1[0]
      104320 blocks [2/2] [UU]

md1 : active raid1 hda3[2] hdb3[0]
      18940544 blocks [2/1] [U_]
      [=>...................]  recovery =  7.3% (1398400/18940544) finish=11.3min speed=25826K/sec

unused devices: <none>

Adjust grub.conf

default=0
timeout=5
hiddenmenu
title CentOS (2.6.18-128.2.1.el5)
	root (hd0,0)
	kernel /vmlinuz-2.6.18-128.2.1.el5 ro root=/dev/vg1/root
	initrd /initrd-2.6.18-128.2.1.el5.img

Install bootloader on second disk

# grub
grub> device (hd0) /dev/hdb
grub> root (hd0,0)
grub> setup (hd0)
grub> quit

Final reboot

shutdown -r now

Enjoy