13.8. Converting a root filesystem to LVM 1

CautionBackup Your System
 

It is strongly recommended that you take a full backup of your system before attempting to convert to root on LVM 1.

WarningUpgrade Complications
 

Having your root filesystem on LVM 1 can significantly complicate upgrade procedures (depending on your distribution) so it should not be attempted lightly. Particularly, you must consider how you will insure that the LVM 1 kernel module (if you do not have LVM 1 compiled into the kernel) as well as the vgscan/vgchange tools are available before, during, and after the upgrade.

WarningRecovery Complications
 

Having your root filesystem on LVM 1 can significantly complicate recovery of damaged filesystems. If you lose your initrd, it will be very difficult to boot your system. You will need to have a recover disk that contains the kernel, LVM 1 module, and LVM 1 tools, as well as any tools necessary to recover a damaged filesystem. Be sure to make regular backups and have an up-to-date alternative boot method that allows for recovery of LVM 1.

In this example the whole system was installed in a single root partition with the exception of /boot. The system had a 2 gig disk partitioned as:

/dev/hda1  /boot 
/dev/hda2  swap
/dev/hda3  /
        

The / partition covered all of the disk not used by /boot and swap. An important prerequisite of this procedure is that the root partition is less that half full (so that a copy of it can be created in a logical volume). If this is not the case then a second disk drive should be used. The procedure in that case is similar but there is no need to shrink the existing root partition and /dev/hda4 should be replaced with (eg) /dev/hdb1 in the examples.

To do this it is easiest to use GNU parted. This software allows you to grow and shrink partitions that contain filesystems. It is possible to use resize2fs and fdisk to do this but GNU parted makes it much less prone to error. It may be included in your distribution, if not you can download it from ftp://ftp.gnu.org/pub/gnu/parted.

Once you have parted on your system AND YOU HAVE BACKED THE SYSTEM UP:

13.8.1. Boot single user

Boot into single user mode (type linux S at the LILO prompt) This is important. Booting single-user ensures that the root filesystem is mounted read-only and no programs are accessing the disk.

13.8.2. Run Parted

Run parted to shrink the root partition Do this so there is room on the disk for a complete copy of it in a logical volume. In this example a 1.8 gig partition is shrunk to 1 gigabyte This displays the sizes and names of the partitions on the disk

# parted /dev/hda
(parted) p
.
.
.
          

Now resize the partition:

(parted) resize 3 145 999
          
The first number here the partition number (hda3), the second is the same starting position that hda3 currently has. Do not change this. The last number should make the partition around half the size it currently is.

Create a new partition

(parted) mkpart primary ext2 1000 1999
          
This makes a new partition to hold the initial LVM 1 data. It should start just beyond the newly shrunk hda3 and finish at the end of the disk.

Quit parted

(parted) q
          

13.8.3. Reboot

Reboot the system

13.8.4. Verify kernel config options

Make sure that the kernel you are currently running works with LVM 1 and has CONFIG_BLK_DEV_RAM and CONFIG_BLK_DEV_INITRD set in the config file.

13.8.5. Adjust partition type

Change the partition type on the newly created partition from Linux to LVM (8e). Parted doesn't understand LVM 1 partitions so this has to be done using fdisk.

# fdisk /dev/hda
Command (m for help): t
Partition number (1-4): 4
Hex code (type L to list codes): 8e
Changed system type of partition 4 to 8e (Unknown)
Command (m for help): w
          

13.8.6. Set up LVM 1 for the new scheme

13.8.7. Create the Filesystem

Make a filesystem in the logical volume and copy the root files onto it.

# mke2fs /dev/vg/root
# mount /dev/vg/root /mnt/
# find / -xdev | cpio -pvmd /mnt
          

13.8.8. Update /etc/fstab

Edit /mnt/etc/fstab on the new root so that / is mounted on /dev/vg/root. For example:

  /dev/hda3       /    ext2       defaults 1 1
          
becomes:

  /dev/vg/root    /    ext2       defaults 1 1
          

13.8.9. Create an LVM 1 initial RAM disk


# lvmcreate_initrd
        

Make sure you note the name that lvmcreate_initrd calls the initrd image. It should be in /boot.

13.8.10. Update /etc/lilo.conf

Add an entry in /etc/lilo.conf for LVM 1. This should look similar to the following:

  image   = /boot/KERNEL_IMAGE_NAME
  label   = lvm
  root    = /dev/vg/root
  initrd  = /boot/INITRD_IMAGE_NAME
  ramdisk = 8192
          
Where KERNEL_IMAGE_NAME is the name of your LVM 1 enabled kernel, and INITRD_IMAGE_NAME is the name of the initrd image created by lvmcreate_initrd. The ramdisk line may need to be increased if you have a large LVM 1 configuration, but 8192 should suffice for most users. The default ramdisk size is 4096. If in doubt check the output from the lvmcreate_initrd command, the line that says:

lvmcreate_initrd -- making loopback file (6189 kB)
          
and make the ramdisk the size given in brackets.

You should copy this new lilo.conf onto /etc in the new root fs as well.

# cp /etc/lilo.conf /mnt/etc/
          

13.8.11. Run LILO to write the new boot sector


# lilo
        

13.8.12. Reboot to lvm

Reboot - at the LILO prompt type "lvm" The system should reboot into Linux using the newly created Logical Volume.

If that worked then you should make lvm the default LILO boot destination by adding the line

default=lvm
          
in the first section of /etc/lilo.conf

If it did not work then reboot normally and try to diagnose the problem. It could be a typing error in lilo.conf or LVM 1 not being available in the initial RAM disk or its kernel. Examine the message produced at boot time carefully.

13.8.13. Add remainder of disk

Add the rest of the disk into LVM 1. When you are happy with this setup you can then add the old root partition to LVM 1 and spread out over the disk.

First set the partition type to 8e(LVM)

# fdisk /dev/hda

Command (m for help): t
Partition number (1-4): 3
Hex code (type L to list codes): 8e
Changed system type of partition 3 to 8e (Unknown)
Command (m for help): w
          

Convert it into a PV and add it to the volume group:

# pvcreate /dev/hda3
# vgextend vg /dev/hda3
          

mirror server hosted at Truenetwork, Russian Federation.