Note: There is a newer version of this document here.
For all steps listed, I’m working with an Ubuntu 10.04 LTS virtual machine. It consists of a single disk (/dev/sda) that I didn’t size properly when I originally created the VM.
Later I added a second disk (/dev/sdb, independent of snapshots) to hold audio files from ripping cds prior to transferring to them to a player. I did not add this disk to the existing logical volume.
Now we’re going add a third disk (/dev/sdc) to the VM and place it in the same volume group as the original disk to help alleviate the space problem as shown here:
Filesystem Size Used Avail Use% Mounted on /dev/mapper/ubuntu-root 7.3G 6.5G 487M 94% / none 245M 264K 244M 1% /dev none 249M 1.1M 248M 1% /dev/shm none 249M 92K 249M 1% /var/run none 249M 0 249M 0% /var/lock none 249M 0 249M 0% /lib/init/rw /dev/sda1 236M 54M 170M 24% /boot /dev/sdb1 30G 497M 30G 2% /media/bucket .host:/ 79G 68G 12G 86% /mnt/hgfs
Step by Step
Adding the new disk
Go about adding the virtual disk through the method you’re familiar with, keeping in mind that it will be combined with the existing LVM group already in place.
Creating the physical volume
I’ve found that you don’t have to partition the disk prior to running this command.
Physical volume "/dev/sdc" successfully created
Listing the existing volume groups
--- Volume group --- VG Name ubuntu System ID Format lvm2 Metadata Areas 1 Metadata Sequence No 3 VG Access read/write VG Status resizable MAX LV 0 Cur LV 2 Open LV 2 Max PV 0 Cur PV 1 Act PV 1 VG Size 7.76 GiB PE Size 4.00 MiB Total PE 1986 Alloc PE / Size 1986 / 7.76 GiB Free PE / Size 0 / 0 VG UUID t8CdgQ-IyfB-SqI5-C9Jc-M6w2-oNhe-1UuNZy
Extending the existing volume group
The ubuntu volume group was found, so let’s add to that one.
vgextend ubuntu /dev/sdc
Volume group "ubuntu" successfully extended
Using the new disk
The logical volume manager doesn’t know anything about the contents of its volumes, so you must do your resizing at both the volume and filesystem levels. The order depends on the specific operation. Reductions must be filesystem-first, and enlargements must be volume-first. Don’t memorize these rules: just think about what’s actually happening and use common sense.
Resizing the volume group
We added a 20 GB virtual disk and added the space to the volume group with the last command, but we haven’t resized the logical volume (think of it as a container for a partition) to use any of the new space.
I’ve tried specifying the space I wanted to extend the logical volume in megabtyes, but I prefer specifying it via logical extents (the units of space allocation within a volume group) instead. I find this easier for some reason.
To do that, we need to get the number of logical extents available. We’ll use vgdisplay for this.
--- Volume group --- VG Name ubuntu System ID Format lvm2 Metadata Areas 2 Metadata Sequence No 4 VG Access read/write VG Status resizable MAX LV 0 Cur LV 2 Open LV 2 Max PV 0 Cur PV 2 Act PV 2 VG Size 27.75 GiB PE Size 4.00 MiB Total PE 7105 Alloc PE / Size 1986 / 7.76 GiB Free PE / Size 5119 / 20.00 GiB VG UUID t8CdgQ-IyfB-SqI5-C9Jc-M6w2-oNhe-1UuNZy
What we’re looking for is prefaced with Free PE / Size and and in this case it’s 5119.
lvresize -l +5119 ubuntu/root
Extending logical volume root to 27.37 GiB Logical volume root successfully resized
Resizing the file system
Now that the container has grown large enough to hold a bigger file system, we resize the file system to fill it.
resize2fs /dev/ubuntu /root
resize2fs 1.41.11 (14-Mar-2010) Filesystem at /dev/ubuntu/root is mounted on /; on-line resizing required old desc_blocks = 1, new_desc_blocks = 2 Performing an on-line resize of /dev/ubuntu/root to 7175168 (4k) blocks. The filesystem on /dev/ubuntu/root is now 7175168 blocks long.
Here’s our file system/disk space usage:
root@ubuntu:~# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/ubuntu-root 27G 6.5G 20G 26% / none 245M 264K 244M 1% /dev none 249M 156K 249M 1% /dev/shm none 249M 88K 249M 1% /var/run none 249M 0 249M 0% /var/lock none 249M 0 249M 0% /lib/init/rw /dev/sda1 236M 54M 170M 24% /boot /dev/sdb1 30G 497M 30G 2% /media/bucket .host:/ 79G 68G 12G 86% /mnt/hgfs