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.
- 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
- 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
- I had a sizable swap partition of 2GB, so I turned it off.
- Formated the swap partition as ext3 and copied over the root files:
- Once the files were transfered. I then edited the "/mnt/etc/fstab" file to reflect the new root.
/dev/sda2 / ext3 defaults 1 1
- 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
- After reboot the original root (/dev/sda3) is no longer mounted and is free to work with.
- Change the partition type from Linux to LVM (8e).
- Inform the Operating System for the partition table changes.
- Initialize LVM
- Make the new partition into a Physical Volume.
- Create a new volume group.
- Create a logical volume to hold the new root (/).
- Make a filesystem in the logical volume and copy the root files onto it.
- 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.If image does not have the lvm binary, create and move it to the /boot folder.
- Edit "/mnt/etc/fstab" on the new root so that / is mounted on /dev/vg0/lvroot0.
/dev/vg0/lvroot0 / ext3 defaults 1 1
- 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
- Reboot, and enjoy the flexibility of lvm!!