Linux LVM How-to: Adding a new partition

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:

  1. Partition the new disk
  2. Create New Physical Device (pvcreate)
  3. Extend the existing volume group
  4. Extend the logical volume
  5. Extend the filesystem (ext4 in my case)

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:

simon@pandora:~$ df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/pandora-root
                      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/sdb. So let’s prepare this new disk…

Prepare the new disk

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

simon@pandora:~$ su -
Password: 
root@pandora:~# fdisk /dev/sdb 
[..]
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/sdb: 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/sdb1 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/sdb1 (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”:

  • Physical devices (pv)
  • Volume groups (vg)
  • Logical volumes (lv)

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@pandora:~# pvcreate /dev/sdb1
  Physical volume "/dev/sdb1" successfully created

Nice. So let’s see which physical volumes we have:

root@pandora:~# pvdisplay
  --- Physical volume ---
  PV Name               /dev/sda5
  VG Name               pandora
  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/sdb1" is a new physical volume of "8.00 GiB" --- NEW Physical volume --- PV Name /dev/sdb1 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 “pandora” in my case, use vgdisplay to check the name for your volume group). Using the vgextend command we can extend the volume group:

root@pandora:~# vgextend pandora /dev/sdb1
  Volume group "pandora" successfully extended
root@pandora:~# vgdisplay
  --- Volume group ---
  VG Name               pandora
  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@pandora:~# lvdisplay
  --- Logical volume ---
  LV Name                /dev/pandora/root
  VG Name                pandora
  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/pandora/swap_1 VG Name pandora 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/pandora/root”) and use the following command to extend that logical volume with all the space available on the specified VG (in my case “pandora”):

root@pandora:~# lvextend -l 100%FREE /dev/pandora/root 
  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/pandora/root).
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/pandora/root

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/pandora/root

Extend the filesystem

Now the underlying volume /dev/pandora/root 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@pandora:~# resize2fs /dev/mapper/pandora-root 
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/mapper/pandora-root is mounted on /; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 1
Performing an on-line resize of /dev/mapper/pandora-root to 4033536 (4k) blocks.
The filesystem on /dev/mapper/pandora-root is now 4033536 blocks long.

Done. To validate, let’s run the same command we ran at the beginning of this article.

root@pandora:~# exit
logout
simon@pandora:~$ df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/pandora-root
                       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
simon@pandora:~$ 

As always, make sure to read the documentation before actually messing with your system. The Linux Documentation project has a good LVM HOWTO or you can refer to the Red Hat documentation here.

Hello world

My name is Simon Krenger, I am a Technical Account Manager (TAM) at Red Hat. I advise our customers in using Kubernetes, Containers, Linux and Open Source.

Elsewhere

  1. GitHub
  2. LinkedIn
  3. GitLab