How to extend an LVM Volume in Linux

In this artícle I will show you how to extend a existing LVM volume in Linux.


5 min de lectura
How to extend an LVM Volume in Linux

In this artícle I will show you how to extend a existing LVM volume in Linux.

This article is one of those that I write for myself, because, I don't extend disk everyday and always I miss some step doing this procedure. Of course, also can work for you, this is a standard Linux procedure.

Let's get start, first, we going to identify our current disk in our virtual machine, we can do this running the next command:

$ df -h

For my case, this is the result:

Filesystem           Size  Used Avail Use% Mounted on
udev                 955M     0  955M   0% /dev
tmpfs                198M  1.3M  196M   1% /run
/dev/sda2             20G  4.6G   14G  25% /
tmpfs                986M     0  986M   0% /dev/shm
tmpfs                5.0M     0  5.0M   0% /run/lock
tmpfs                986M     0  986M   0% /sys/fs/cgroup
/dev/loop0            94M   94M     0 100% /snap/core/8935
/dev/loop1            90M   90M     0 100% /snap/core/8268
/dev/mapper/a-disco  969M  2.5M  900M   1% /mnt/disco2
tmpfs                198M     0  198M   0% /run/user/1000

The disk I want to extend is the following:

/dev/mapper/a-disco  969M  2.5M  900M   1% /mnt/disco2

If you can see, that disk had a size of 1gb and is mounted in /mnt/disco2 directory. In this case. I will double the space of this disk.

First, I need to add a new disk to the virtual machine, and then, I need to find the identification that the system give to that new disk. You can do it running the following command:

$ fdisk -l

The terminal should return something like this:

Disk /dev/loop0: 89.1 MiB, 93417472 bytes, 182456 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/loop1: 93.8 MiB, 98336768 bytes, 192064 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/sda: 20 GiB, 21474836480 bytes, 41943040 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: gpt
Disk identifier: EAB3EDBC-7716-4B7B-9328-3D238759C495

Device     Start      End  Sectors Size Type
/dev/sda1   2048     4095     2048   1M BIOS boot
/dev/sda2   4096 41940991 41936896  20G Linux filesystem


Disk /dev/sdb: 1 GiB, 1073741824 bytes, 2097152 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xeb92df94

Device     Boot Start     End Sectors  Size Id Type
/dev/sdb1        2048 2097151 2095104 1023M 8e Linux LVM


Disk /dev/sdc: 1 GiB, 1073741824 bytes, 2097152 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/mapper/a-disco: 1000 MiB, 1048576000 bytes, 2048000 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

Take a look that the last "entry" is our actual LVM and the before that is our new brand disk "/dev/sdc".

Disk /dev/sdc: 1 GiB, 1073741824 bytes, 2097152 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

Lets partition and format this disk to extend our current LVM volume. But first, we need to know what is the name of our current Volume Group. We can see this, running the next command:

$ vgdisplay

The result looks like this:

  --- Volume group ---
  VG Name               a
  System ID
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  2
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1
  Open LV               1
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               1020.00 MiB
  PE Size               4.00 MiB
  Total PE              255
  Alloc PE / Size       250 / 1000.00 MiB
  Free  PE / Size       5 / 20.00 MiB
  VG UUID               X18nn3-W3U7-TjLc-iogG-Fo4d-fAnH-EIw4oo

Now, we going to partition the "/dev/sdc" disk executing the following command:

$ fdisk /dev/sdc

Let get start to the partition job, be advised, that the section where say "Created a new partition 1 of type 'linux' and of size 1023 MiB" because, from there, we must type "t" and the choose the type of partition. "8e" is the "code" for LVM.

Welcome to fdisk (util-linux 2.31.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table.
Created a new DOS disklabel with disk identifier 0x26b1d169.

Command (m for help): n
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p):

Using default response p.
Partition number (1-4, default 1):
First sector (2048-2097151, default 2048):
Last sector, +sectors or +size{K,M,G,T,P} (2048-2097151, default 2097151):

Created a new partition 1 of type 'Linux' and of size 1023 MiB.

Command (m for help): t
Selected partition 1
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'.

If the results is something like you see up here, press "w" to commit the changes:

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

The next is our list to do is create an "Physical Volume", let's do this.

$ pvcreate /dev/sdc1

The terminal should return this:

 Physical volume "/dev/sdc1" successfully created.

Now, lets going to extend the "Volume Group" called "a". For this, we must write the following:

$ vgextend a /dev/sdc1

The return:

Volume group "a" successfully extended

And now, we need to extend our Logical Volume, remember the Volume Group and the name of the disk, in this case, was called "disco". In this example, I extend to 1Gb, but, will depend of you needs and space available in your new installed disk.

$ lvextend -L +1g /dev/mapper/a-disco
Size of logical volume a/disco changed from 1000.00 MiB (250 extents) to <1.98 GiB (506 extents).
  Logical volume a/disco successfully resized.

For last we need to resize the disk:

$ resize2fs /dev/mapper/a-disco
resize2fs 1.44.1 (24-Mar-2018)
Filesystem at /dev/mapper/a-disco is mounted on /mnt/disco2; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 1
The filesystem on /dev/mapper/a-disco is now 518144 (4k) blocks long.

Once that the last command execute, we need to check if our "disco" is actually had 2Gb. We can run "df-h" again and you should see that the disk is 2Gb size now.

Filesystem           Size  Used Avail Use% Mounted on
udev                 955M     0  955M   0% /dev
tmpfs                198M  1.4M  196M   1% /run
/dev/sda2             20G  4.6G   14G  25% /
tmpfs                986M     0  986M   0% /dev/shm
tmpfs                5.0M     0  5.0M   0% /run/lock
tmpfs                986M     0  986M   0% /sys/fs/cgroup
/dev/loop0            90M   90M     0 100% /snap/core/8268
/dev/loop1            94M   94M     0 100% /snap/core/8935
/dev/mapper/a-disco  2.0G  3.0M  1.9G   1% /mnt/disco2
tmpfs                198M     0  198M   0% /run/user/1000

Last words:


I hope this article, what I wrote as record for me, help you to extend your LVM volume. I ran this lab in Ubuntu Server but, this apply to CentOS, RHEL o another Debian derived OS Linux.

Let me know if this works for you. Also, this is my very first article in english and for sure, some things can be improve, so, let me know your thoughts.


baehost

SUBIR

🎉 Te has suscrito con éxito a CDUser - El Blog de Ignacio Van Droogenbroeck!
OK