Tuesday, June 19, 2012

How To Configure RAID 1 on a LVM System


 This article explains how to set up software RAID1 on an already running LVM  system (Linux Mint 13 Maya). The GRUB2 bootloader will be configured in such a way that the system will still be able to boot if one of the hard drives fails (no matter which one). 

In this tutorial I'm using a Linux Mint 13 Maya system with two hard drives, /dev/sda and /dev/sdb which are identical in size. /dev/sdb is currently unused, and /dev/sda  has the following partitions (this is the default Linux Mint LVM partitioning scheme
  • /dev/sda1: /boot partition, ext4;
  • /dev/sda2: extended, contains /dev/sda5;
  • /dev/sda5: is used for LVM (volume group test-linuxhowto.in) and contains / (volume root) and swap (volume swap_1).
In the end I want to have the following situation:
  • /dev/md0 (made up of /dev/sda1 and /dev/sdb1): /boot partition, ext4;
  • /dev/md1 (made up of /dev/sda5 and /dev/sdb5): LVM (volume group test-linuxhowto.in), contains / (volume root) and swap (volume swap_1).
This is the current situation:

root@test-linuxhowto.in:~# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/ test-linuxhowto.in -root
                      4.5G  720M  3.6G  17% /
tmpfs                 249M     0  249M   0% /lib/init/rw
udev                  244M  104K  244M   1% /dev
tmpfs                 249M     0  249M   0% /dev/shm
/dev/sda1             228M   16M  201M   8% /boot

root@test-linuxhowto.in:~# fdisk -l

Disk /dev/sda: 5368 MB, 5368709120 bytes
255 heads, 63 sectors/track, 652 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0003717c

    Device Boot  Start  End   Blocks  Id  System
 /dev/sda1   *    1      32   248832  83  Linux 
Partition 1 does not end on cylinder boundary.
 /dev/sda2       32     653   4990977  5  Extended
Partition 2 does not end on cylinder boundary.
 /dev/sda5       32     653  4990976  8e  Linux LVM

Disk /dev/sdb: 5368 MB, 5368709120 bytes
255 heads, 63 sectors/track, 652 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

Disk /dev/sdb doesn't contain a valid partition table

Disk /dev/dm-0: 4844 MB, 4844421120 bytes
255 heads, 63 sectors/track, 588 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

Disk /dev/dm-0 doesn't contain a valid partition table

Disk /dev/dm-1: 264 MB, 264241152 bytes
255 heads, 63 sectors/track, 32 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

Disk /dev/dm-1 doesn't contain a valid partition table
root@test-linuxhowto.in:~#

root@test-linuxhowto.in:~# pvdisplay
  --- Physical volume ---
  PV Name               /dev/sda5
  VG Name               test-linuxhowto.in
  PV Size               4.76 GiB / not usable 2.00 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              1218
  Free PE               0
  Allocated PE          1218
  PV UUID               485485430-438954w8493-jfdjfsrefe-9949r3

 
root@test-linuxhowto.in:~# vgdisplay
  --- Volume group ---
  VG Name               test-linuxhowto.in
  System ID
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  3
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               2
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               4.76 GiB
  PE Size               4.00 MiB
  Total PE              1218
  Alloc PE / Size       1218 / 4.76 GiB
  Free  PE / Size       0 / 0
  VG UUID               fajfdfj9-dae9a9dw92-394qerqjwdq-2rqe

 
root@test-linuxhowto.in:~# lvdisplay
  --- Logical volume ---
  LV Name                /dev/test-linuxhowto.in/root
  VG Name                test-linuxhowto.in
  LV UUID                fdjfioasdfjsa9r-38e473q84qh-32e4qre-eqewr8q3
  LV Write Access        read/write
  LV Status              available
  # open                 1
  LV Size                4.51 GiB
  Current LE             1155
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           254:0

  --- Logical volume ---
  LV Name                /dev/test-linuxhowto.in/swap_1
  VG Name                test-linuxhowto.in
  LV UUID                fadhfadhfsar-39qruq9rq-rqefjqefjq-r329rqr3
  LV Write Access        read/write
  LV Status              available
  # open                 1
  LV Size                252.00 MiB
  Current LE             63
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           254:1

root@test-linuxhowto.in:~#

Installing mdadm

The most important tool for setting up RAID is mdadm. Let's install it like this:

apt-get install initramfs-tools mdadm
MD arrays needed for the root file system: <-- all

Afterwards, we load a few kernel modules (to avoid a reboot):

modprobe linear
modprobe multipath
modprobe raid0
modprobe raid1
modprobe raid5
modprobe raid6
modprobe raid10

Now run

root@test-linuxhowto.in:~# cat /proc/mdstat
Personalities : [linear] [multipath] [raid0] [raid1] [raid6] [raid5] [raid4] [raid10]
unused devices: <none>
root@test-linuxhowto.in:~#

Preparing /dev/sdb

To create a RAID1 array on our already running system, we must prepare the /dev/sdb hard drive for RAID1, then copy the contents of our /dev/sda hard drive to it, and finally add /dev/sda to the RAID1 array.

First, we copy the partition table from /dev/sda to /dev/sdb so that both disks have exactly the same layout:

root@test-linuxhowto.in:~# sfdisk -d /dev/sda | sfdisk --force /dev/sdb
Checking that no-one is using this disk right now ...
OK

Disk /dev/sdb: 652 cylinders, 255 heads, 63 sectors/track

sfdisk: ERROR: sector 0 does not have an msdos signature
 /dev/sdb: unrecognized partition table type
Old situation:
No partitions found
Warning: extended partition does not start at a cylinder boundary.
DOS and Linux will interpret the contents differently.
New situation:
Units = sectors of 512 bytes, counting from 0

   Device Boot    Start       End   #sectors  Id  System
/dev/sdb1   *      2048    499711     497664  83  Linux
/dev/sdb2        501758  10483711    9981954   5  Extended
/dev/sdb3             0         -          0   0  Empty
/dev/sdb4             0         -          0   0  Empty
/dev/sdb5        501760  10483711    9981952  8e  Linux LVM
Warning: partition 1 does not end at a cylinder boundary
Successfully wrote the new partition table

Re-reading the partition table ...

If you created or changed a DOS partition, /dev/foo7, say, then use dd(1)
to zero the first 512 bytes:  dd if=/dev/zero of=/dev/foo7 bs=512 count=1
(See fdisk(8).)
root@test-linuxhowto.in:~#

The command fdisk -l should now show that both HDDs have the same layout:

root@test-linuxhowto.in:~# fdisk -l

Disk /dev/sda: 5368 MB, 5368709120 bytes
255 heads, 63 sectors/track, 652 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0003717c

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          32      248832   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              32         653     4990977    5  Extended
Partition 2 does not end on cylinder boundary.
/dev/sda5              32         653     4990976   8e  Linux LVM

Disk /dev/sdb: 5368 MB, 5368709120 bytes
255 heads, 63 sectors/track, 652 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1   *           1          32      248832   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sdb2              32         653     4990977    5  Extended
Partition 2 does not end on cylinder boundary.
/dev/sdb5              32         653     4990976   8e  Linux LVM

Disk /dev/dm-0: 4844 MB, 4844421120 bytes
255 heads, 63 sectors/track, 588 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

Disk /dev/dm-0 doesn't contain a valid partition table

Disk /dev/dm-1: 264 MB, 264241152 bytes
255 heads, 63 sectors/track, 32 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

Disk /dev/dm-1 doesn't contain a valid partition table
root@test-linuxhowto.in:~#

Next we must change the partition type of our three partitions on /dev/sdb to Linux raid autodetect:

root@test-linuxhowto.in:~# fdisk /dev/sdb

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

Command (m for help): <-- m
Command action
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition
   l   list known partition types
   m   print this menu
   n   add a new partition
   o   create a new empty DOS partition table
   p   print the partition table
   q   quit without saving changes
   s   create a new empty Sun disklabel
   t   change a partition's system id
   u   change display/entry units
   v   verify the partition table
   w   write table to disk and exit
   x   extra functionality (experts only)

Command (m for help): <-- t
Partition number (1-5): <-- 1
Hex code (type L to list codes): <-- L

  
Hex code (type L to list codes): <-- fd
Changed system type of partition 1 to fd (Linux raid autodetect)

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

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

Calling ioctl() to re-read partition table.
Syncing disks.
root@test-linuxhowto.in:~#

The command fdisk -l should now show that /dev/sdb1 and /dev/sdb5 are of the type Linux raid autodetect:

root@test-linuxhowto.in:~# fdisk -l

Disk /dev/sda: 5368 MB, 5368709120 bytes
255 heads, 63 sectors/track, 652 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0003717c

    Device Boot  Start    End      Blocks   Id  System
 /dev/sda1   *    1        32      248832   83  Linux
Partition 1 does not end on cylinder boundary.
 /dev/sda2        32      653     4990977    5  Extended
Partition 2 does not end on cylinder boundary.
 /dev/sda5        32      653     4990976   8e  Linux LVM

Disk /dev/sdb: 5368 MB, 5368709120 bytes
255 heads, 63 sectors/track, 652 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

    Device Boot  Start    End      Blocks   Id  System
 /dev/sdb1   *
 1          32      248832   fd  Linux raid autodetect
Partition 1 does not end on cylinder boundary.
/dev/sdb2              32         653     4990977    5  Extended
Partition 2 does not end on cylinder boundary.
 /dev/         32         653     4990976   fd  Linux raid autodetect

Disk /dev/dm-0: 4844 MB, 4844421120 bytes
255 heads, 63 sectors/track, 588 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

Disk /dev/dm-0 doesn't contain a valid partition table

Disk /dev/dm-1: 264 MB, 264241152 bytes
255 heads, 63 sectors/track, 32 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

Disk /dev/dm-1 doesn't contain a valid partition table
root@test-linuxhowto.in:~#

To make sure that there are no remains from previous RAID installations on /dev/sdb, we run the following commands:

mdadm --zero-superblock /dev/sdb1
mdadm --zero-superblock /dev/sdb5

If there are no remains from previous RAID installations, each of the above commands will throw an error like this one (which is nothing to worry about):

root@test-linuxhowto.in:~# mdadm --zero-superblock /dev/sdb1
mdadm: Unrecognised md component device - /dev/sdb1
root@test-linuxhowto.in:~#

Otherwise the commands will not display anything at all.

DISCLAIMER: The information provided on this website comes without warranty of any kind and is distributed AS IS. Every effort has been made to provide the information as accurate as possible, but no warranty or fitness is implied. The information may be incomplete, may contain errors or may have become out of date. The use of this information described herein is your responsibility, and to use it in your own environments do so at your own risk.





Copyright © 2012 LINUXHOWTO.IN


No comments:

Post a Comment