Wednesday, March 17, 2010

Cloning an LVM system partition to a larger disk

Let's face it, sometimes bigger is better. You installed Linux on your computer with the default installation choices, and now you have outgrown the disk space available. You want to throw in a larger disk, but you don't want to reinstall your highly customized system. What you want to do is make a disk image of your current system, put in the new disk, and then restore the old image to your spanking new disk using something like Clonezilla.

If you are using Fedora, or Redhat Enterprise, or CentOS, then you probably created a Logical Volume file system for your system partition. Restoring the original image to the new, larger disk recreates your original disk partition size and logical volumes. This means that you aren't using the complete disk. Your file system is still the same size at it was originally.

We need to resize the partition to allow us to expand the volume. Because the partition is a logical volume, it can't be resized with a simple to use graphical tool such as gparted. We have to resize the partition, the filesystem (ext2 or ext3) and the logical volume with a set of command line tools.

First, we need to change the partition table on the disk. The old partition table only uses the disk space available on the old disk. We need to change that. Here is where the tricky part comes in. The default install will have created a swap partition at the end of the original disk. To expand the system partition into the unused portion of the disk, you have to remove or move that swap partition. If you remove the swap partition entirely, then you will want to create a swap file on your system disk when you are finished.

We can't work on a live filesystem without creating massive corruption of our data. Instead, we will need to boot from a rescue CD/installation media to perform all of the delicate operations. Note that Redhat Enterprise 4 does not have the resize2fs utility available in the rescue mode. You will need to obtain a different rescue disk to continue.

Boot the computer from the installation CD. At the boot prompt type

boot: linux rescue 


This will boot the system into rescue mode. When asked, you want to skip the step for mounting the your system disk at /mnt/sysimage. You need this disk unmounted in order to modify the partition table and file system.

At the prompt, you want to run the fdisk utility on the drive which contains your system partition. In my situation, my computer was an HP Proliant server with a raid array. The system partition was the first partition on the device named /dev/cciss/c0d1. For most systems, this will probably be a device named /dev/sda instead. The principle and process is exactly the same whether the disk is a raid array or a single drive. The drive controller protects you from having to think about the individual disks in the array.

The first command to give fdisk is to have it print out the partition table for the drive.

# fdisk /dev/cciss/c0d1

The number of cylinders for this disk is set to 53546.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
(e.g., DOS FDISK, OS/2 FDISK)

Command (m for help): p

Disk /dev/cciss/c0d1: 440.4 GB, 440430842880 bytes
255 heads, 63 sectors/track, 53546 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/cciss/c0d1p1 1 17848 143364060+ 8e Linux LVM
/dev/cciss/c0d1p2 17848 18432 4690980 82 Linux swap


As you can see, we have one partition containing a logical volume and then a swap partition. The remainder of the disk is unused. There are over 50000 cylinders, but we are using less than half of them. What we need to do is delete these partitions and replace them with a new partition which uses the entire disk.

Carefully note down the Start, End and ID for each of the partitions. Just in case you change your mind and want to restore the partition table.

While still in fdisk, use the d command to delete each of these partitions. We are just modifying the partition table, not the data or file system on the disk. So long as we create a new partition which begins at the same location, we shouldn't lose any data!

Now we want to create the new partition with the n command. We create a new primary partition which Starts at the same cylinder as the original partition. You have your choice for the end of the partition. You can either use all of the disk space for this partition and use a swap file or, you can save space at the end of the drive for a separate swap partition. I recommend using a swap file rather than having a separate partition, but others will probably disagree.

Next we have to set the partition ID to be a Linux LVM partition. You do this with the t command in fdisk. Change the partition id on your new partition to the hex code, 8e.

Print out the new partition table with the p command and verify that all is as you intended. Once you are sure that there are no mistakes, write the new partition table to the drive with the w command and exit out of fdisk.

Okay, now we have to activate the Logical Volumes so that we can work on it. We do that with vgchange command to lvm.

lvm vgchange -a y


This should activate your volumes and make them available with a name such as /dev/VolGroup00/LogVol00. On Redhat derived system, this is the default naming scheme.

To see what logical volumes you have, use the lvdisplay command. You will see something like this.

# lvm lvdisplay
--- Logical volume ---
LV Name /dev/VolGroup00/LogVol00
VG Name VolGroup00
LV UUID z05FHf-2zdO-RxCE-eH9G-1EbA-p748-xKqSyG
LV Write Access read/write
LV Status available
# open 1
LV Size 166.00 GB
Current LE 38280
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:0


One thing that you will notice is that our logical volume is the original size. We need to resize it. Before we can do that, we need to tell LVM about the new size of the physical volume. So far, we haven't told the system about the size of the new disk. We will not be able to expand the partition until we have changed the system's knowledge of the physical drive. We can do this with the pvresize command to lvm like this

lvm pvresize --setphysicalvolumesize 440.4G /dev/VolGroup00/LogVol00


Now that the system knows about the dimensions of the new disk, we can resize the filesystem and the logical volume.

We begin with the resize2fs command to resize the ext2/3 filesystem.

resize2fs /dev/VolGroup00/LogVol00 440GB


Once this has completed, the final step is to resize the logical volume with lvresize. We want to expand the logical volume to include the entire file system, so we make it the same size.

lvm lvresize -L440G /dev/VolGroup00/LogVol00


Once this is completed, you can reboot the system. If you removed the swap partition in favor of a swap file, then you will need to create the swap file and add it to /etc/fstab after you reboot.

Followers