CentOS / RHEL : Converting an Existing Root Filesystem to LVM Partition

Question: Root Filesystem is full and it is not LVM. We need to move this to new partition which is LVM. How this can be achieved?

Answer :
Procedure to convert an existing root file system on a normal disk partition to LVM volume is as follows :

1. First Create an LVM partition:
Select a free disks and create an LVM partition on it.

# pvcreate /dev/sdb1
# vgcreate vg_new_root  /dev/sdb1
# lvcreate -L 4G -n lv0 vg_new_root

2. Format this new LVM partition with ext3 or ext4 (or as needed). The example given below creates aext4 file system :

# mkfs.ext4 /dev/vg_new_root/lv0

3. Create a new mount point and then mount the LVM Partition on it:

# mkdir /mnt/NEW_ROOT_PARTITION
# mount /dev/vg_new_root/lv0  /mnt/NEW_ROOT_PARTITION

4. Copy all contents of “/” to the newly mounted folder:

# tar -cvpf - --one-file-system --acls --xattrs --selinux / | tar -C /mnt/NEW_ROOT_PARTITION -xf
# cp -aux /dev /mnt/NEW_ROOT_PARTITION

5. Now edit the file – /mnt/NEW_ROOT_PARTITION/etc/fstab to reflect the new root:

/dev/vg_new_root/lv0      /       ext4    defaults     1 1

6. Now it is needed to chroot to new filesystem and create initrd with raid and lvm support:

# mount --bind /dev /mnt/NEW_ROOT_PARTITION
# chroot /mnt/NEW_ROOT_PARTITION
# mount -t proc /proc /proc
# mount -t sysfs /sys /sys
# vgscan
# vgchange -ay
# mkinitrd -v /boot/initrd-`uname -r`.lvm.img `uname -r`
# umount /sys
# umount /proc
# exit
# mv /mnt/lv0/boot/initrd-`uname -r`.lvm.img /boot

7. Edit /boot/grub/grub.conf to point to new root /dev/vg_new_root/lv0. Backup the old initrd in “/boot” directory and rename the new initrd to the existing one or edit the entry in grub.conf.

8. Reboot

# shutdown -r now
Related Post