Close

LVM Root Disk

Computers need disks to store their operating system, programs and data. Good old disk partition management has not changed much over time. Using either MBR (created in 1983) or modern GPT paritions you are still using statically allocated partitions and filesystems. It can be challenging to deal with static partitions because initial choices can not be changed easily.

Unfortunately it is more complex to use Logical Volume Manager for Linux (wikipedia). Most factory installed computers use traditional partitioning. The added complexity is not usually needed but when it is needed it is really needed. The benefits of using LVM include the ability to more easily resize partitions and backup via snapshots. Server hardware can even replace disks using hot swapping. LVM is commonly used along with virtualization via LXC, Eucalyptus, OpenStack or other similar systems. Here are the steps needed to convert a root partition to LVM and mirror it on a redhat system.

1. Create a tar backup of your filesystem.

# tar czpf /root/redhat.tar --exclude=/var/tmp/portage/* --exclude=/root/* --exclude=/usr/portage/* --exclude=*.deb --exclude=/tmp/* --exclude=*.rpm --exclude=/sys/* --exclude=/proc/* --exclude=/dev/* --exclude=/mnt/* --exclude=/media/* --exclude=/home/*  --exclude=/var/lib/libvirt/images/* --exclude=/oracle/*  --exclude=redhat.tar

2. Use fdisk to create /boot  and 1 LVM partition on the new disk.

/dev/sda1   *   1    100      803218+  83  Linux
/dev/sda2 101121601  975956782+ 8e Linux LVM

3. Set /dev/sda1 to be bootable.

# parted /dev/sda set 1 boot on

4.  Create the new LVM partition.

# pvcreate /dev/sda2
# vgcreate vg /dev/sda2
# lvcreate -L 200G /dev/vg -n root
# mkfs /dev/vg/root
# mkfs /dev/sda1
# mount /dev/vg/root /mnt
# mount /dev/sda1 /mnt/boot

5. Extract the tar file to /mnt

# tar xpf /root/redhat.tar -C /mnt/

6. Modify the following files:

/mnt/grub/menu.list:

Modify the kernel line to support LVM by adding the following LVM details:

rd_LVM_VG=vg rd_LVM_LV=root

Also ensure that initrd and kernel does not have /boot/ in the location.

Example:

kernel /vmlinuz-2.6.32-279.2.1.el6.x86_64 ro root=/dev/mapper/vg-root LANG=en_US.UTF-8 SYSFONT=latarcyrheb-sun16 KEYBOARDTYPE=pc KEYTABLE=us crashkernel=128M  rd_LVM_VG=vg rd_LVM_LV=root rhgb quiet
initrd /initramfs-2.6.32-279.2.1.el6.x86_64.img

/mnt/etc/fstab:

Change the /boot and / entries to LVM:

/dev/sda1 /boot ext4 defaults 0 0
/dev/mapper/vg-root /      ext4    defaults        1 1

7. Mount and configure the new environment:

# mount /dev/vg/root /mnt
# mount /dev/sda1 /mnt/boot
# mount -o bind /sys /mnt/sys
# mount -o bind /dev /mnt/dev
# mount -o bind /proc /mnt/proc
# grep -v rootfs /proc/mounts  > /mnt/etc/mtab

Modify /mnt/etc/mtab and add:

/dev/sda1 /boot ext4 rw 0 0

Change the apparent root directory for the current running process and its children:

# chroot /mnt

8. Install GRUB and reconfigure the ram disk image:

# grub-install --recheck /dev/sda
# dracut --force

9. Unmount and reboot:

Type exit to exit the chroot environment

# cd /
# umount /mnt/*
# umount /mnt
# reboot

Set your system to boot from the disk known as /dev/sda

10. Initialize and format your original boot disk.

Just like we did for /dev/sda.

 Device Boot      Start         End      Blocks   Id  System
/dev/sdc1   *           1       100      803218+  83  Linux
/dev/sdc2             101    121601   975956782+  8e  Linux LVM

11. Add /dev/sdc to the volume group.

# vgextend /dev/vg /dev/sdc2

12. Format the boot partition on the drive and set it bootable:

# mkfs /dev/sdc1
# parted /dev/sdc set 1 boot on

13. Mirror the boot disk:

# lvconvert -m1 /dev/vg/root

We hope this helps you with your disks. Berkeley LUG meetings continue every second and fourth Sunday each month at Bobby Gs Pizzeria from noon to three in Berkeley. Please join us.