Skip to content

Move Root Partition to LVM

Moving a root partition to LVM

When I installed CentOS onto my laptop, I wasn’t comfortable with LVM at the time. I split out a few partitions onto LVM, but neglected to create my root partition on it. Of course, it filled up a few days ago and I had two options: 1) try to figure out how to move root to LVM or 2) re-install and re-partition to suit my needs. Well, I didn’t want to re-install because that takes way too much time especially when I have my laptop setup perfectly the way I want it.

It took a bit of playing around to figure out how to do this, but once I did it worked like a charm on a test box, so I thought I’d better document it so I don’t forget. This guide assumes LVM2 is in use since there are differences between LVM1 and LVM2.

I have a 40G drive installed, and had about 6G extra sitting on LVM unused. My previous root partition was 4G, and generally full file systems are a bad thing.

Previous layout

My drive looked like:

[root@laptop ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vg0-root  4.0G  3.2G  689M  88% /
/dev/hda1              99M  9.4M   85M  11% /boot
none                  506M     0  506M   0% /dev/shm
/dev/mapper/vg0-home   20G  8.4G   11G  45% /home
/dev/mapper/vg0-opt   2.0G  542M  1.4G  29% /opt
/dev/mapper/vg0-tmp   496M   16M  455M   4% /tmp
/dev/mapper/vg0-usr_local
                     1008M  179M  779M  19% /usr/local
/dev/mapper/vg0-var   2.0G  819M  1.1G  43% /var

Some previous LVM settings:

[root@laptop ~]# vgs
  VG   #PV #LV #SN Attr   VSize  VFree
  vg0    1   5   0 wz–n- 29.62G 6.12G

[root@laptop ~]# pvs
  PV         VG   Fmt  Attr PSize  PFree
  /dev/hda5  vg0  lvm2 a-   31.66G 160.00M

Create a new logical volume under LVM

Make sure you boot into single mode before you attempt to do anything at all. To do this, when Grub comes up hit ‘e’ to edit, and ‘a’ to add ‘1′ to the end of the kernel line.

The first thing to do is create a new logical volume which will eventually be your new root file system.

[root@laptop ~]# lvcreate -L6G -n root vg0

Obviously substitute your volume group name and what you want to call your partition with your configurations.

Next, make the filesystem an ext3 filesystem.

[root@laptop ~]# mk32fs -j /dev/vg0/root

Copy the original partition to the new one

The next thing you need to do is to actually move the data from the original root partition over to the new one created in the step above. You’ll need to create a mount point, mount the new LVM partiton, and then dump the data over to it.

[root@laptop ~]# mkdir /mnt/newroot
[root@laptop ~]# mount /dev/vg0/root /mnt/newroot
[root@laptop ~]# cd /mnt/newroot
[root@laptop ~]# dump 0af - / | restore xfo -

The dump command is explained as follows: 0 means a full backup will be moved over and not an incremental, a means auto-size, f is writing the backup to a normal file.

You then pipe this to restore which pushes it onto your new parition. Restore is explained as follows: x means the backup is read from the given media, f means from file.

Change fstab & create a new boot image

In order for your new root partition to automatically be mounted, you’ll need to change the mount directory in /etc/fstab and create a new boot image for your system to boot from.

[root@laptop ~]# vim /mnt/newroot/etc/fstab

# Change this line:
LABEL=/  /  ext3  defaults        1   1

# To read:
/dev/vg0/root  / ext3  defaults       1   1

Next, you’ll have to create the new boot image with mkinitrd. Make sure you point it at the fstab file under the mounted new partition, and not the old one!

[root@laptop ~]# mkinitrd -v –fstab=/mnt/newroot/etc/fstab /boot/initrd-lvm.img 2.6.9-42.0.10.EL

Again, substitute your kernel and the name of the actual image with your parameters.

Lastly, you’ll have to edit grub.conf to point to the new root partition and to the new boot image. Otherwise, your system won’t know how to start.

[root@laptop ~]# vim /boot/grub/grub.conf

default=0
timeout=5
splashimage=(hd0,0)/grub/robin.xpm.gz
hiddenmenu
title CentOS (2.6.9-42.0.10.EL)
    root (hd0,0)
    kernel /vmlinuz-2.6.9-42.0.10.EL ro root=/dev/vg0/root rhgb quiet
    initrd /initrd-lvm.img

Reboot, but make sure you reboot into single mode again to complete the second half of finishing this job up. Everything should boot back up normal and you should be booted onto your new LVM partition.

[root@laptop ~]# mount
/dev/mapper/vg0-root on / type ext3 (rw)

That’s it for getting things going. In case you’re worried about the new boot image, don’t be. When you upgrade the system will take that into account and nothing should be affected.

Create a new physical volume and extend into volume group

Next, you’ll need to turn the old /dev/hda2 partition into a physical volume that LVM can understand.

[root@laptop ~]# pvcreate /dev/hda2

This tells LVM that /dev/hda2 is a physical volume group container. Next, extend that physical volume into the current volume group vg0. This adds the pv into the vg.

[root@laptop ~]# vgextend vg0 /dev/hda2

That’s it as far as LVM cares about. I just took the 4G partition and added that 4G into my volume group which I can then, at a later time, push out to whichever logical volume group I need.

Change partition label

The very last step that needs completed is to turn the old partiton label into the new LVM status. Before, /dev/hda2 was just a regular Linux file system.

You’ll need to change that into a Linux LVM label like the other one shown.

[root@laptop ~]# fdisk /dev/hda

Command (m for help): p
Disk /dev/hda: 40.0 GB, 40007761920 bytes
255 heads, 63 sectors/track, 4864 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/hda1   *           1          13      104391   83  Linux
/dev/hda2              14         535     4192965   83  Linux
/dev/hda3             536         731     1574370   82  Linux swap
/dev/hda4             732        4864    33198322+   5  Extended
/dev/hda5             732        4864    33198291   8e  Linux LVM

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

Command (m for help): w

Just to make sure it changed, run fdisk -l and that’s all you need to do. When you reboot, the new partition table will be read and voila.

What you’ve accomplished is take a full 4G root partition, created a 6G LVM partition out of the 6G f free space left in the volume group, and moved the 4G of data over to the 6G LVM partition. Then you’ve taken the 4G static partition, created a physical volume, and added that into the LVM volume group vg0. Now you have 4G free on /dev/hda2 and 160M free on /dev/hda5, totalling the space left on vg0 to 4.12G.

[root@laptop ~]# vgs
  VG   #PV #LV #SN Attr   VSize  VFree
  vg0    2   6   0 wz–n- 35.62G 4.12G

[root@laptop ~]# pvs
  PV         VG   Fmt  Attr PSize  PFree
  /dev/hda2  vg0  lvm2 a-    3.97G   3.97G
  /dev/hda5  vg0  lvm2 a-   31.66G 160.00M

I don’t know about what you think, but this is much easier than re-installing Linux and spending the hours tinkering getting everything set back exactly on your desktop. Cheers!