Converting a Linux machine to LVM

Requirements

You will need a Knoppix CD and a USB hard drive capable of holding the entirety of the server. The server will need an internet connection for this operation.
Preparation

Install the following packages:

  • lvm2
  • lilo
  • xfsdump

Run apt-get clean

Cat /etc/fstab and write down the list of filesystems, so that later, you know what to mount where, and what mount options to use.

Software RAID:

cat /proc/mdstat and write down which disk partitions go with which md devices.
Boot Knoppix

I used Knoppix 5.1. Knoppix 4.x will not work as it lacks the LVM utilities. boot using the 'knoppix 2' boot option, to suppress the GUI. Do not have the USB drive powered on at this time.
Mount the USB device

At this point, power on the USB drive. The console should tell you what device node was assigned. You should now partiton/format the USB drive. I suggest you format it XFS, for performance reasons.
Mount the system drives

Software RAID: Start the arrays

For each of your software RAID arrays, issue the following command:

mdadm -A /dev/mdx /dev/sdax /dev/sdbx

Substitute sdax and sdbx for your disk partitions.

cd to /mnt

mount each filesystem to its pre-created mount point. For software RAID, you'll have to create some md mount points. DO NOT USE THE sdx MOUNT POINTS WITH SOFTWARE RAID!

Don't forget to mount /home (and anything that mounts to a subdirectory of /home) with usrquota,grpquota
Copy the data

Use xfsdump to transfer the data from any XFS filesystems, so that quotas and ACLs are preserved.

xfsdump -f /mnt/sdb1 /dev/sda9

where /mnt/sdb1 is the mount point for the USB drive and /dev/sda9 is the device name of the /home filesystem.

For filesystems that don't use EAs, a simple tarball for each filesystem should suffice.

cd /mnt/sda1
tar -czvf /mnt/sdb1/xxx.tar.gz.

When done, unmount the USB drive and turn it off to protect it from accidental erasure.
Repartition the system drives

unmount all system partitions

Stop any software RAID arrays

Run cfdisk.

Remove all partitions from all devices, and create one large partition on each device.

Make it bootable.

If you are using hardware RAID, make the partition type 8E (Linux LVM) If you are using software RAID, make one partition per disk, type FD

When you write the partition table, cfdisk will tell you if it re-read the table successfully. If it did not, you will have to reboot now. UNMOUNT AND TURN OFF THE USB DRIVE BEFORE REBOOTING! Most of these servers enumerate the USB devices before the RAID, which will reorder your SCSI device names. To keep things simple, shut down the USB drive during the reboot and turn it back on after.

If you are using software RAID, configure your array at this time (after any necessary reboot). Don't forget to use metadata level 0.9 when creating the array, or LILO will fail to work.

modprobe md
mdadm --create /dev/md0 -e0 -l1 -n2 /dev/sda1 /dev/sdb1

It is a good idea, although not necessary, to allow the array to finish synchronizing before continuing.
Set up the LVM

Run pvcreate /dev/sda1 (/dev/sda1 is the name of your system disk or RAID device)

Run vgcreate vgname /dev/sda1

For each of the logical volumes, run lvcreate -L size -n name where size is the size of the LV and name is its name.

Place filesystems on your logical volumes
Transfer the data back

Mount your logical volumes to directories in /mnt (you have to create those directories) don't forget to mount /home with usrquota,grpquota

Connect and mount the USB drive

Extract the tarballs and dumpfiles to their correct locations

Unmount and shut down the USB drive when done.
Chroot

Unmount all the filesystems but root

chroot into the root filesystem

mount -t proc proc /proc
mount -t sysfs sys /sys

Run /etc/init.d/lvm start. This will create the device nodes needed to mount the LVs.

mount your other filesystems (home, tmp, usr, var) in the correct location
Setup the LVM

As far as I can tell, there is no setup needed here, as long as lvm2 was installed before you began

Software RAID: Fix /etc/mdadm/mdadm.conf

run mdadm --detail --scan >> /etc/mdadm/mdadm.conf

Edit /etc/mdadm/mdadm.conf and remove the old raid lines.
Fix /etc/fstab

Edit /etc/fstab.

Replace the device names with the new names of the logical volumes (ie., /dev/mapper/test-home, etc.)
Install LILO

The liloconfig program will not work for this setup; Copy /etc/lilo.conf from another server to this one, or use a sample file from /usr/share/doc, edited to your tastes. IMPORTANT: Edit your new /etc/lilo.conf to ensure that the settings pertain to your situation (the boot= and root= lines, among others.)
Reconfigure the kernel

Run apt-get remove --purge grub

Edit /etc/kernel-img.conf. Remove the postinst_hook and postrm_hook lines

Run dpkg-reconfigure linux-image-<version>-<arch>

Run LILO to install the new MBR
Reboot

Unmount /home /tmp /usr /var

Unmount /proc and /sys

Exit the chroot

umount /mnt/root

Reboot the system. If the system boots correctly, you're done.