If you want to convert your existing file system to LVM, you can follow the procedure listed below.
It is strongly recommended that you take a full backup of your system before attempting to convert to root on LVM 1.
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.
Warning Recovery 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 recovery disk that contains the kernel, the LVM 1 module, and LVM 1 tools, as well as any tools necessary to recover a damaged filesystem. Be sure to take 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 10 GB disk partitioned as:
/dev/sda1 /boot
/dev/sda2 swap
/dev/sda3 /
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 you would not need to shrink the existing root partition and /dev/sda4 should be replaced with (eg) /dev/sdb1 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, boot into single user mode.
To boot into single user mode (type linux S at the grub prompt) This is important. Booting single-user ensures that the root filesystem is mounted read-only and no programs are accessing the disk.
Run parted to shrink the root partition This is necessary so that there is room on the disk for a complete copy of it in a logical volume. In this example a 10 gig partition is shrunk to 4 GB This displays the sizes and names of the partitions on the disk
# parted /dev/sda
(parted) p
Now resize the partition:
(parted) resize 3 145 999
The first number here the partition number (sda3), the second is the same starting position that sda3 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 ext3 1000 1999
This makes a new partition to hold the initial LVM 1 data. It should start just beyond the newly shrunk sda3 and finish at the end of the disk.
Quit parted
(parted) q
Reboot the system
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.
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
Set up LVM 1 for the new scheme
Initialize LVM 1 (vgscan)
# vgscan
Make the new partition into a PV
# pvcreate /dev/hda4
create a new volume group
# vgcreate vg /dev/sda4
*
Create a logical volume to hold the new root.
# lvcreate -L4G -n root vg
Create the Filesystem
Make a filesystem in the logical volume and copy the root files onto it.
# mkfs.ext3 /dev/vg/root
# mount /dev/vg/root /mnt/
# find / -xdev | cpio -pvmd /mnt
Update /etc/fstab
Edit /mnt/etc/fstab on the new root so that / is mounted on /dev/vg/root. For example:
/dev/sda3 / ext3 defaults 1 1
becomes:
/dev/vg/root / ext3 defaults 1 1
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.
Add an entry in /etc/grub.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 grub.conf onto /etc in the new root fs as well.
# cp /etc/grub.conf /mnt/etc/
Run LILO to write the new boot sector
# grub
Reboot to lvm
Reboot – at the grub 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 grub boot destination by adding the line
default=lvm
in the first section of /etc/grub.conf
If it did not work then reboot normally and try to diagnose the problem. It could be a typing error in grub.conf or LVM 1 not being available in the initial RAM disk or its kernel. Examine the message produced at boot time carefully.
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/sda
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