Since I started out with Linux (so about six years ago), I always used the Linux Logical Volume Manager (LVM) to partition my tables. First it was just because it seemed easier to configure my harddisks with it (also, the installer usually provided a nice option to do so), but in the last few months I had the possibility to work more with LVM and got to know some nice features.

One thing I regularly have to do is to extend an existing logical volume on a server. This article focuses on extending a logical volume with the help of LVM.

Initial setup

To begin, let’s define our goals: We have a partition (mounted as /), which we want to extend from 7G to 16G. We will do this by following these steps:

I am using the latest Debian 6.0.4 release with EXT4 as the filesystem on my virtual machine, but the same steps apply to an EXT3 partition. This is the default layout that Debian created for me when I selected the “Use all diskspace and setup LVM” option during the installation:

				
					meysama@Centos7:~$ df -h

Filesystem     Size   Used Avail Use% Mounted on

/dev/mapper/vg--systems-lv--oracle

                    7.3G  843M   6.1G   12%  /
tmpfs               502M   0     502M    0%  /lib/init/rw
udev                497M  136K   496M    1%  /dev
tmpfs               502M   0     502M    0%  /dev/shm
/dev/sda1           228M  16M    201M    8%  /boot

				
			

Now we add a new disk to the system. This is done by physically adding a new disk or in my case, by adding a new disk via iSCSI. Using /var/log/messages, we can determine that the new disk is accessible under /dev/sda. So let’s prepare this new disk…

Prepare the new disk

Change to root and use the fdisk utility to partition the new disk:

				
					meysama@Centos7:~$ su -
Password: 
root@vg-systems:~# fdisk /dev/sda
[..]

Command (m for help): n
Command action
 e extended
 p primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-1044, default 1): 
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-1044, default 1044): 
Using default value 1044

Command (m for help): p

Disk /dev/sda: 8589 MB, 8589934592 bytes
255 heads, 63 sectors/track, 1044 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x4fce54ed

 Device Boot    Start    End    Blocks    Id    System
/dev/sda1          1    1044   8385898+   83    Linux

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
				
			

Now we have our new partition accessible under /dev/sda1 (as seen above). Now you could use the mkfs tools to create a new filesystem on this partition and use it without LVM. But that is not what we want to do, so let’s continue by configuring LVM itself

Configure LVM

LVM itself consists of three logical “layers”:

So we want to configure LVM in that order. First, we’ll create a new physical volume by adding the partition we created above to the LVM configuration. Then, we will add this newly created physical volume to the existing volume group and then extend the existing logical volume containing the root filesystem.

Alright, let’s add our newly created partition to the LVM configuration:

				
					root@vg-systems:~# pvcreate /dev/sda1

     Physical volume "/dev/sda1" successfully created
				
			
Nice. So let’s see which physical volumes we have:
				
					root@vg-systems:~# pvdisplay
 --- Physical volume ---
 PV Name               /dev/sda5
 VG Name               vg-systems
 PV Size               7.76 GiB / not usable 2.00 MiB
 Allocatable           yes (but full)
 PE Size               4.00 MiB
 Total PE              1986
 Free PE               0
 Allocated PE          1986
 PV UUID               HsOAt3-snnh-MskC-mlE4-Runf-IkYx-KuDJ5j

 "/dev/sda1" is a new physical volume of "8.00 GiB"
 --- NEW Physical volume ---
 PV Name               /dev/sda1
 VG Name 
 PV Size               8.00 GiB
 Allocatable           NO
 PE Size               0 
 Total PE              0
 Free PE               0
 Allocated PE          0
 PV UUID               Oddlkr-jSoL-2PKR-kyxG-Wy33-T0Hh-JvflWQ
				
			

Ah nice, so we can see that LVM has a “NEW Physical volume”.

Now that is done, let’s extend our existing volume group (named “vg-systems” in my case, use vgdisplay to check the name for your volume group). Using the vgextend command we can extend the volume group:

				
					root@vg-systems:~# vgextend vg-systems /dev/sda1
 Volume group " vg-systems " successfully extended
root@vg-systems:~# vgdisplay
 --- Volume group ---
 VG Name              vg-systems
 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              15.75 GiB
 PE Size              4.00 MiB
 Total PE             4033
 Alloc PE / Size      1986 / 7.76 GiB
 Free PE / Size       2047 / 8.00 GiB
 VG UUID              hTcf4N-Srft-Myz2-YQYc-AC9T-pfaA-LIL1KZ
				
			

That went well. Now we’re basically done with adding new space to our volume group. We can now use the newly added physical volume and assign it to different logical volumes that are then used by the filesystem.
So remember what we wanted to do in the first place? Right, we wanted to add the new disk to the root filesystem. So let’s check which logical volumes we have by entering lvdisplay:

				
					root@vg-systems:~# lvdisplay
 --- Logical volume ---
 LV Name               /dev/vg-systems/lv-oracle
 VG Name               vg-systems
 LV                    UUID UJQUwV-f3rI-Tsd3-dQYO-exIk-LSpq-2qls13
 LV Write Access       read/write
 LV Status             available
 # open                1
 LV Size               7.39 GiB
 Current LE            1892
 Segments              1
 Allocation            inherit
 Read ahead sectors    auto
 - currently set to    256
 Block device          254:0

 --- Logical volume ---
 LV Name               /dev/vg-systems/lv-swap_1
 VG Name               vg-systems
 LV                    UUID KHBPkh-qrv8-uZkS-dU4C-f3vF-SpHW-d3vtMH
 LV Write Access       read/write
 LV Status             available
 # open                1
 LV Size               376.00 MiB
 Current LE            94
 Segments              1
 Allocation            inherit
 Read ahead sectors    auto
 - currently set to    256
 Block device          254:1

				
			

We can see that on this machine, we have two logical volumes, one for the root filesystem and one for our swap partition. Since we want to extend the root filesystem, we simply take the LV name (“/dev/vg-systems/lv-oracle”) and use the following command to extend that logical volume with all the space available on the specified VG (in my case “vg-systems”):

				
					root@vg-systems:~# lvextend -l 100%FREE /dev/vg-systems/lv-oracle
 Extending logical volume root to 15.39 GiB
 Logical volume root successfully resized

				
			

Alternatively, you may specify the amount of space that is added to the logical volume (e.g. lvextend -L+1G /dev/vg-systems/lv-oracle).
Update: A commenter noticed that you can resize the filesystem while you extend the logical volume using the -r switch like so:

				
					# lvextend -r -l 100%FREE /dev/vg-systems/lv-oracle

				
			
Update:Another commenter noted that he did not get the expected result when using the above command, but had to add a “+” sign like this for the command to work as expected (YMMV):
				
					lvextend -l+100%FREE /dev/vg-systems/lv-oracle

				
			

Extend the filesystem

Now the underlying volume /dev/vg-systems/lv-oracle was successfully extended, we can extend the filesystem using the resize2fs tool. The tool will choose to do an on-line resizing, which is what we want:

				
					root@vg-systems:~# resize2fs /dev/mapper/vg--systems-lv--oracle
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/mapper/vg--systems-lv--oracle is mounted on /; on-line resizing re
quired
old desc_blocks = 1, new_desc_blocks = 1
Performing an on-line resize of /dev/mapper/vg--systems-lv--oracle to 4033536 (4k) bl
ocks.
The filesystem on /dev/mapper/vg--systems-lv--oracle is now 4033536 blocks long.
				
			
Or use this
				
					root@vg-systems:~# xfs_growfs /dev/mapper/vg--systems-lv--oracle
				
			
Done. To validate, let’s run the same command we ran at the beginning of this article.
				
					root@vg-systems:~# exit
logout
meysama@Centos7:~$ df -h
Filesystem            Size   Used Avail Use% Mounted on
/dev/mapper/vg--systems-lv--oracle
                       16G   845M   14G   6%   /
tmpfs                  502M     0   502M  0%   /lib/init/rw
udev                   497M  144K   496M  1%   /dev
tmpfs                  502M     0   502M  0%   /dev/shm
/dev/sda1              228M   16M   201M  8%   /boot
meysama@Centos7:~$
				
			

Leave A Comment