Information about starting and operating an ISP or corporate Intranet using Linux servers.

Increasing the size of a logical volume

First, check if there is free space available in the physical volume:

$ sudo pvs
  PV         VG   Fmt  Attr PSize  PFree
  /dev/hda2  vg01 lvm2 a-   18.50G 4.50G

$ sudo vgs
  VG   #PV #LV #SN Attr   VSize  VFree
  vg01   1   2   0 wz--n- 18.50G 4.50G

There is 4.5GB of free space in this case. The root partition is running out of space, so we can proceed to increase its size by 1GB.

Look at the logical volumes defined:

$ sudo lvs
  LV   VG   Attr   LSize  Origin Snap%  Move Log Copy%  Convert
  lv01 vg01 -wi-ao 10.00G
  lv02 vg01 -wi-ao  4.00G

$ df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vg01-lv01
                    9.7G  7.8G  1.5G  85% /

The root partition ”/” is logical volume /dev/mapper/vg01-lv01.

pvresize would be used to expand the physical volume if we had unallocated space on a disk and wanted to add some of it to a partition. That's not what we will use here, since the space is already allocated to the physical volume. We just need to expand the logical volume to use it.

lvresize is used to expand a logical volume. Syntax is:

lvresize --size [+]LogicalVolumeSize[kKmMgGtT]} [-t/--test] [-v/--verbose] LogicalVolumePath

First perform a test run:

$ sudo lvresize --size +1G --test --verbose /dev/mapper/vg01-lv01
  Test mode: Metadata will NOT be updated.
  Finding volume group vg01
  Test mode: Skipping archiving of volume group.
Extending logical volume lv01 to 11.00 GB
  Test mode: Skipping volume group backup.
  Found volume group "vg01"
  Found volume group "vg01"
  Found volume group "vg01"
Logical volume lv01 successfully resized
  Test mode: Wiping internal cache
  Wiping internal VG cache

Looks good. Let's proceed.

$ sudo lvresize --size +1G --verbose /dev/mapper/vg01-lv01
  Finding volume group vg01
  Archiving volume group "vg01" metadata (seqno 6).
Extending logical volume lv01 to 11.00 GB
  Creating volume group backup "/etc/lvm/backup/vg01" (seqno 7).
  Found volume group "vg01"
  Found volume group "vg01"
  Loading vg01-lv01 table
  Suspending vg01-lv01 (253:0) with device flush
  Found volume group "vg01"
  Resuming vg01-lv01 (253:0)
Logical volume lv01 successfully resized

Now if we look at df we see nothing has changed, yet lvs shows the extra space. So we have the extra space allocated to the logical volume, but the file-system is not yet aware of it. The next step is to grow the file-system.

For an EXT2 or EXT3 file-system the resize2fs tool is used. There are similar tools for XFS and other file-systems.

$ sudo resize2fs -p /dev/mapper/vg01-lv01
resize2fs 1.39 (29-May-2006)
Filesystem at /dev/mapper/vg01-lv01 is mounted on /; on-line resizing required
Performing an on-line resize of /dev/mapper/vg01-lv01 to 2883584 (4k) blocks.
The filesystem on /dev/mapper/vg01-lv01 is now 2883584 blocks long.

Now check the free space:

$ df -h /
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vg01-lv01
                       11G  7.8G  2.4G  77% /

Eureka, it worked! :-)

Navigation
Print/export
Toolbox