FC4 Root on LVM Post-Install Migration HowTo

from Guanosoft

Table of Contents

  1. Introduction
  2. Kernel Configuration
  3. Initial RAM Disk
  4. Update Boot Loader
  5. Copy Files to Logical Volume (Probably rename this section.)
  6. Extending Logical Volumes Containing ext2 or ext3
  7. Future Kernel Upgrades
  8. Troubleshooting

Introduction

This howto suggests one way of migrating the root filesystem of your already-installed-and-running Fedora Core 4 system to LVM.

If you are installing FC4 from scratch, you can select LVM during the installation procedure and your life will be much easier than people who actually have to follow the suggestions in this howto.  In that case, the following article may help you.

FedoraNEWS Article about Installing Fedora on LVM

If you have installed FC4 recently and haven't customized your system much, think seriously about reinstalling if you want your root filesystem on LVM!  This stuff isn't that hard when you're following the nice instructions below, but if you run into a snag, your life might become LVM for a while.

This procedure can also help with LVM on RAID, but no RAID information is mentioned here.

(Do stock FC4 initrds support lvm on root automatically?  Need to test this.)

This document assumes you have some knowledge of disk partitioning in Linux.

Kernel Configuration

The kernel must support an initial RAM disk to scan for volume groups.  Fedora Core 4 stock kernels have this support, so no change is necessary.  If you use a pre-packaged kernel not from Fedora, check for the configuration names below in /proc/config.gz or the .config file in the sources.  RPMs for kernel sources for pre-packaged kernels are sometimes named -devel.  If you use a custom kernel, configure it as follows.
  • Say Y or M to Device Drivers->Block devices->Loopback device support (CONFIG_BLK_DEV_LOOP)
    • (Not entirely sure on this one.  Verify.)
  • Say Y to Device Drivers->Block devices->RAM disk support (CONFIG_BLK_DEV_RAM)
  • Say Y to Device Drivers->Block devices->Initial RAM disk (initrd) support (CONFIG_BLK_DEV_INITRD)
  • Say Y to Device Drivers->Multi-device support (RAID and LVM)->Multiple... (CONFIG_MD)
  • Say Y or M to Device Drivers->Multi-device support (RAID and LVM)->Device mapper support (CONFIG_BLK_DEV_DM)

Initial RAM Disk

The initial RAM disk image must contain the necessary kernel modules for your disks and for LVM.  It must also call the lvm tools to scan for volume groups.  If you don't have your root filesystem (/) mounted on LVM, mkinitrd won't add the necessary commands to the init script.  This will be the case when you're migrating to LVM.  

(Does FC4 mkinitrd support LVM, but it didn't put it in the init script, because I didn't have root mounted on LVM?  I had fstab configured for root on LVM.)

If you don't have an initial RAM disk image for your kernel (/boot/initrd-<version>.img), make one with the mkinitrd command.  (Your current running kernel version can be found with 'uname -r'.)  Add "--with=" parameters for any modules required for the disks containing your root filesystem.  In the following example, the ata_piix module is included for a SATA disk connected to an Intel PIIX controller.  Including ata_piix automatically includes modules it depends on, such as scsi_mod.  sd_mod is also required for SATA disks.

mkinitrd -v --with=ata_piix --with=sd_mod initrd-2.6.14.5-2.img 2.6.14.5-2

If you configured the Device mapper support as a module ("M"), you will also need "--with=dm_mod".

To add volume group probing, extract the image with the following command.

zcat initrd-2.6.14.5-2.img | cpio -i

The contents of the image look like a normal Linux filesystem.  Copy /sbin/lvm.static to the bin directory of the image.  Edit the init script and add the following lines before the mkrootdev command.

mkdmnod
/sbin/lvm.static vgscan --mknodes --ignorelockingfailure
/sbin/lvm.static vgchange -a y --ignorelockingfailure

Recreate the image file now that the LVM support is configured.  Make a list of the files you want in the image, and then create it.

find * > ../filelist
cpio -o -c < ../filelist | gzip --best -n > ../initrd-2.6.14.5-2.img

Copy the image to /boot.

Update Boot Loader

If you weren't using an initial RAM disk before, update your boot loader.

If you use GRUB, add a line to /boot/grub/grub.conf like "initrd /initrd-2.6.14.5-2.img" after your "kernel" line.

If you use LILO, add a line to /etc/lilo.conf like "initrd=/boot/initrd-2.6.14.5-2.img" after your "root=" line.

Copy Files to Logical Volume (Probably rename this section.)

The following example shows how to set up LVM if you have a spare partition large enough for your root filesystem and its device is /dev/hda2.  Read the LVM HOWTO and the manpages for more details.

pvcreate /dev/hda2
vgcreate vgmain /dev/hda2
vgchange -ay vgmain
pvdisplay

Look at the output and record the number of extents.  This assumes the number is 243.

lvcreate -n root -l 243 vgmain

Your boot loader must now specify the root device as the logical volume.  Change the "root=" in the "kernel" line of /boot/grub/grub.conf to "root=/dev/vgmain/root" or change the "root=" line in /etc/lilo.conf.  Better yet, make two bootloader choices for that kernel, one with root pointing to LVM and one with root pointing to your current root.

Boot a live cd (Gentoo Live CD recommended, the following instructions assume you used that).  Initialize LVM with 'vgscan' and 'vgchange -ay'.  Mount the normal root filesystem (this assumes /dev/hda1) with 'mkdir /mnt/root' and 'mount /dev/hda1 /mnt/root'.  Mount the logical volume with 'mkdir /mnt/newroot' and 'mount /dev/vgmain/root'.  Copy the files with 'cp -a /mnt/root /mnt/newroot'.

Reboot.  Breathe.

Extending Logical Volumes Containing ext2 or ext3

If you're using an ext2 or ext3 filesystem on a logical volume, extending the volume is simple.  First, convert the disk to LVM and extend the volume group and logical volume.  Then, resize the filesystem.  Assuming a disk has been added with device /dev/hdb, these instructions show how to extend the "stuff" volume through it.

pvcreate /dev/hdb
vgextend vgmain /dev/hdb
pvdisplay /dev/hdb

Read the output of the above command to find the PE Size and Total PE.  Convert the PE Size to Megabytes by dividing by 1024.  The following commands assume the Total PE is 711.

lvextend -l +711 /dev/vgmain/stuff
lvdisplay /dev/vgmain/stuff

Read the output of the above command to find the Current LE.  This is the total logical extents in the volume.  Multiply thus number by the PE Size in Megabytes.  The following command assumes a PE Size of 4MB and 1584 total logical extents.

resize2fs /dev/vgmain/stuff 6336M

If you are asked to run 'e2fsck -f' first, do it.

Future Kernel Upgrades

Once you are running your root filesystem on LVM, each time you upgrade your kernel, you must create an initial RAM disk image for it.  If you use stock Fedora Core kernels, one should be created for you automatically.  Otherwise, use a command like the following, similar to what you did above.

mkinitrd -v --with=ata_piix --with=sd_mod /boot/initrd-2.6.14.5-2.img 2.6.14.5-2

Troubleshooting

If you are unable to boot, or it's using the old root instead of LVM, you can change the initial RAM disk init script to help you troubleshoot it.  Comment out setquiet, so echo statements will be printed to the console.  Add extra echo statements to figure out which commands are causing which output.  Add sleep statements so you can read earlier output.  Use the instructions in the "Initial RAM Disk" section above to extract the image, edit the script, and recreate the image.

To check out your LVM setup, boot a live CD like the Gentoo Live CD.  On the Gentoo Live CD, first run 'vgscan; vgchange -ay' to bring up the volume groups.  Test logical volumes with 'mkdir /mnt/lv; mount /dev/vgname/lvname /mnt/lv'.

The Knoppix 4.0 CD doesn't have LVM tools.  Maybe the DVD does.


I welcome corrections and questions. If you know a better place for this information, please let me know.

Contact name@domain, where name is 5bl0hhz02 and domain is sneakemail.com

Updated January 8th, 2006