Thursday, September 19, 2013

// // Leave a Comment

Convert root filesystem to LVM

I converted root filesystem to lvm since the root partition was huge and I needed more flexibility in managing the partitions. Besides, lvm would also enable for easy backups with lvm snapshots.
I had a sizable swap partition of 2GB which I used to transfer my root files to and rebooted to it, prior to the conversion.
Please know what you are doing prior and make sure to create backups.
  1. This is what the partitions looked like prior to the change:
    # df -h
    Filesystem            Size  Used Avail Use% Mounted on
    /dev/sda3             228G  1.4G  215G   1% /
    /dev/sda1              99M   17M   77M  19% /boot
    none                 1014M     0 1014M   0% /dev/shm
    
    # fdisk -l
    Disk /dev/sda: 250.0 GB, 250000000000 bytes
    255 heads, 63 sectors/track, 30394 cylinders
    Units = cylinders of 16065 * 512 = 8225280 bytes
    
       Device Boot      Start         End      Blocks   Id  System
    /dev/sda1   *           1          13      104391   83  Linux
    /dev/sda2              14         267     2040255   82  Linux swap
    /dev/sda3             268       30394   241995127+  83  Linux
    
  2. With the current setup, I have a better sized partition, and can further create/resize partitions to add or remove at will:
    # df -h
    Filesystem            Size  Used Avail Use% Mounted on
    /dev/mapper/vg0-lvroot0
                           24G  1.3G   22G   6% /
    /dev/sda1              99M   19M   75M  21% /boot
    none                 1014M     0 1014M   0% /dev/shm
    
    # fdisk -l
    Disk /dev/sda: 250.0 GB, 250000000000 bytes
    255 heads, 63 sectors/track, 30394 cylinders
    Units = cylinders of 16065 * 512 = 8225280 bytes
    
       Device Boot      Start         End      Blocks   Id  System
    /dev/sda1   *           1          13      104391   83  Linux
    /dev/sda2              14         267     2040255   83  Linux swap
    /dev/sda3             268       30394   241995127+  8e  Linux LVM
    
  3. I had a sizable swap partition of 2GB, so I turned it off.
    # swapoff -a
  4. Formated the swap partition as ext3 and copied over the root files:
    # mke2fs -j /dev/sda2
    # mount /dev/sda2 /mnt/
    # find / -xdev | cpio -pvmd /mnt
  5. Once the files were transfered. I then edited the "/mnt/etc/fstab" file to reflect the new root.
    /dev/sda2      /    ext3       defaults 1 1
    
  6. Add a grub section to use "/dev/sda2" as the new root.
    title CentOS (2.6.9-55.0.9.ELsmp.sda2)
            root (hd0,0)
            kernel /vmlinuz-2.6.9-55.0.9.ELsmp ro root=/dev/sda2
            initrd /initrd-2.6.9-55.0.9.ELsmp.img
    
  7. After reboot the original root (/dev/sda3) is no longer mounted and is free to work with.
  8. Change the partition type from Linux to LVM (8e).
          # fdisk /dev/sda
          Command (m for help): t
          Partition number (1-4): 3
          Hex code (type L to list codes): 8e
          Changed system type of partition 4 to 8e (Unknown)
          Command (m for help): w
  9. Inform the Operating System for the partition table changes.
          #partprobe
  10. Initialize LVM
          # vgscan
  11. Make the new partition into a Physical Volume.
          # pvcreate /dev/sda3          
  12. Create a new volume group.
          # vgcreate vg0 /dev/sda3
  13. Create a logical volume to hold the new root (/).
          # lvcreate -L 24G -n lvroot0 vg0
  14. Make a filesystem in the logical volume and copy the root files onto it.
          # mke2fs -j /dev/vg0/lvroot0
          # mount /dev/vg0/lvroot0 /mnt
          # yum update kernel
          # find / -xdev | cpio -pvmd /mnt
  15. Additionally, since the new root is now lvm, the initrd image file should have support for it else kernel will not be able to find root on reboot and end up with a kernel panic.
    Note: At this stage, instead of creating a new image, I update the kernel so that the new image supports lvm. LVM support can also be checked via extracting the initrd image file and checking for lvm binary in the bin folder.
    # mkdir -p /tmp/kill/00
    # cd /tmp/kill/00
    # gunzip < /boot/initrd-2.6.9-55.0.9.ELsmp.img | cpio -id
    # ls -al bin/lvm
    -r-x------ 1 root root 1417512 May 24 14:38 bin/lvm
    If image does not have the lvm binary, create and move it to the /boot folder.
    # chroot /mnt
    # mount -t proc /proc /proc
    # mount -t sysfs /sys /sys
    # vgmknodes -v
    # mkinitrd -v initrd-2.6.9-55.0.9.ELsmp.lv.img 2.6.9-55.0.9.ELsmp
  16. Edit "/mnt/etc/fstab" on the new root so that / is mounted on /dev/vg0/lvroot0.
    /dev/vg0/lvroot0       /    ext3       defaults 1 1
    
  17. Add grub section to use "/dev/vg0/lvroot0" to boot to.
    title CentOS (2.6.9-55.0.9.ELsmp.lvroot0)
            root (hd0,0)
            kernel /vmlinuz-2.6.9-55.0.9.ELsmp ro root=/dev/vg0/lvroot0
            initrd /initrd-2.6.9-55.0.9.ELsmp.lv.img
    
  18. Reboot, and enjoy the flexibility of lvm!!

0 comments:

Post a Comment