Adding Virtual Disk in Linux using LVM Framework – Part 1

LVM or Logical Volume Manager is a device mapper framework to organize and manage virtual/physical disks mounted on linux. We can use lvm to create volume groups of underlay physical disks and allocate blocks from this group to different partitions of our Linux system. This framework is supported on various linux distribution, for this blog however I am working on Ubuntu 20.04 setup.

Figure 1

In actual world, we may have two scenarios when adding disk and resizing partitions. I will be covering both in two blog series.

  • Scenario 1, where we have single Virtual Disk with unused free space ready to be used for allocations to logical volumes
  • Scenario 2, where our existing Virtual Disk is fully utilized and we have to add additional volume as Virtual Disk first, before allocating to logical volumes
Figure 2

In this blog, we are covering scenario 1, in which for one of my Ubuntu 20.04 machine, I have unused space and I want to allocate it to different logical volumes.

Step 1

Verify existing blocks, volume group and free space.

root@lxubuntu20:~# lsblk
NAME                            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
vda                             252:0    0   50G  0 disk
├─vda1                          252:1    0    1M  0 part
├─vda2                          252:2    0    1G  0 part /boot
└─vda3                          252:3    0    9G  0 part
  ├─ubuntu--vg-ubuntu--lv--root 253:0    0    4G  0 lvm  /
  ├─ubuntu--vg-ubuntu--lv--var  253:1    0    2G  0 lvm  /var
  └─ubuntu--vg-ubuntu--lv--home 253:2    0    3G  0 lvm  /home
root@lxubuntu20:~# vgs
  VG        #PV #LV #SN Attr   VSize  VFree
  ubuntu-vg   1   3   0 wz--n- <9.00g    0

We can see here although by Virtual Disk 1 (vda) is having total size of 50G, volume group only have 9G of it added with another 1G for boot, thereby leaving rest of 40G unused. I can check allocation to different logical volumes from exiting volume group.

root@lxubuntu20:~# lvs
  LV             VG        Attr       LSize  
  ubuntu-lv-home ubuntu-vg -wi-ao----  3.00g
  ubuntu-lv-root ubuntu-vg -wi-ao---- <4.00g
  ubuntu-lv-var  ubuntu-vg -wi-ao----  2.00g

Step 2

To begin allocation, we must first create new disk partition from existing virtual disk (vda). We will use fdisk utlility for it.

root@lxubuntu20:~# fdisk /dev/vda

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

GPT PMBR size mismatch (20971519 != 104857599) will be corrected by write.
The backup GPT table is not on the end of the device. This problem will be corrected by write.

Command (m for help): n
Partition number (4-128, default 4): 4
First sector (20969472-104857566, default 20969472):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (20969472-104857566, default 104857566):

Created a new partition 4 of type 'Linux filesystem' and of size 40 GiB.

Command (m for help): w
The partition table has been altered.
Syncing disks.

Verify new partition with size 40G using lsblk.

root@lxubuntu20:~# lsblk
NAME                            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
vda                             252:0    0   50G  0 disk
├─vda1                          252:1    0    1M  0 part
├─vda2                          252:2    0    1G  0 part /boot
├─vda3                          252:3    0    9G  0 part
│ ├─ubuntu--vg-ubuntu--lv--root 253:0    0    4G  0 lvm  /
│ ├─ubuntu--vg-ubuntu--lv--var  253:1    0    2G  0 lvm  /var
│ └─ubuntu--vg-ubuntu--lv--home 253:2    0    3G  0 lvm  /home
└─vda4                          252:4    0   40G  0 part

Step 3

Next, we need to add this 40G partition in our existing volume group using vgextend <volume-group> <new-partition>

root@lxubuntu20:~# vgextend ubuntu-vg /dev/vda4
Physical volume "/dev/vda4" successfully created.
Volume group "ubuntu-vg" successfully extended

Verify new free space now available in volume group.

root@lxubuntu20:~# vgs
  VG        #PV #LV #SN Attr   VSize  VFree
  ubuntu-vg   2   3   0 wz--n- 48.99g <40.00g

Step 4

We can now allocate 10G to our three logical volumes viz. var, home and root using lvresize -L +(Size) <logical volume>

root@lxubuntu20:~# lvresize -L +10G /dev/mapper/ubuntu--vg-ubuntu--lv--root
  Size of logical volume ubuntu-vg/ubuntu-lv-root changed from <4.00 GiB (1023 extents) to <14.00 GiB (3583 extents).
  Logical volume ubuntu-vg/ubuntu-lv-root successfully resized.

root@lxubuntu20:~# lvresize -L +10G /dev/mapper/ubuntu--vg-ubuntu--lv--var
  Size of logical volume ubuntu-vg/ubuntu-lv-var changed from 2.00 GiB (512 extents) to 12.00 GiB (3072 extents).
  Logical volume ubuntu-vg/ubuntu-lv-var successfully resized.

root@lxubuntu20:~# lvresize -L +10G /dev/mapper/ubuntu--vg-ubuntu--lv--home
  Size of logical volume ubuntu-vg/ubuntu-lv-home changed from 3.00 GiB (768 extents) to 13.00 GiB (3328 extents).
  Logical volume ubuntu-vg/ubuntu-lv-home successfully resized.

Step 5

Post Step 4, allocations shall be visible but our file systems for each logical volumes need to be updated as well to ensure new size are persistant even after system reboot. I have all three logical volumes added as xfs file system so I will use xfs_growfs <logical volume>. You may check your logical volume file system type from /etc/fstab entries.

root@lxubuntu20:~# xfs_growfs /dev/mapper/ubuntu--vg-ubuntu--lv--root
meta-data=/dev/mapper/ubuntu--vg-ubuntu--lv--root isize=512    agcount=4, agsize=261888 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=1, sparse=1, rmapbt=0
         =                       reflink=1
data     =                       bsize=4096   blocks=1047552, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0, ftype=1
log      =internal log           bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 1047552 to 3668992


root@lxubuntu20:~# xfs_growfs /dev/mapper/ubuntu--vg-ubuntu--lv--var
meta-data=/dev/mapper/ubuntu--vg-ubuntu--lv--var isize=512    agcount=4, agsize=131072 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=1, sparse=1, rmapbt=0
         =                       reflink=1
data     =                       bsize=4096   blocks=524288, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0, ftype=1
log      =internal log           bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 524288 to 3145728


root@lxubuntu20:~# xfs_growfs /dev/mapper/ubuntu--vg-ubuntu--lv--home
meta-data=/dev/mapper/ubuntu--vg-ubuntu--lv--home isize=512    agcount=4, agsize=196608 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=1, sparse=1, rmapbt=0
         =                       reflink=1
data     =                       bsize=4096   blocks=786432, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0, ftype=1
log      =internal log           bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 786432 to 3407872

Verify new logical volume sizes now,

root@lxubuntu20:~# lvs
  LV             VG        Attr       LSize   
  ubuntu-lv-home ubuntu-vg -wi-ao----  13.00g
  ubuntu-lv-root ubuntu-vg -wi-ao---- <14.00g
  ubuntu-lv-var  ubuntu-vg -wi-ao----  12.00g

In Part 2, We will add new additional disk and resize volumes again to cover Scenario 2 of Figure 2.

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s