You can create physical volumes (PVs) out of whole disks or disk partitions by using LVM.

Red Hat recommends that you create a single partition that covers the whole disk to label as an LVM physical volume.

In this post I want to show how we can extend this disk partition our physical volumes (PVs) are placed on. When using and initializing the whole disk for a physical volume (PV), we can use the LVM out of the box command pvresize as shown in my following post https://blog.matrixpost.net/vsphere-linux-virtual-machines-extend-disks-logical-volumes-and-the-file-system/#anchor_resize_cmd.

This command doesn’t work when we placed a volume group and logical volumes on disk partitions, but therefore we can use the growpart command as shown in this post.

Note that although LVM supports using a non-partitioned disk as physical volume, it is recommended to create a single, whole-disk partition because creating a PV without a partition can be problematic in a mixed operating system environment. Other operating systems may interpret the device as free, and overwrite the PV label at the beginning of the drive.

Source: https://docs.redhat.com/en/documentation/red_hat_enterprise_linux/9/html/configuring_and_managing_logical_volumes/managing-lvm-physical-volumes_configuring-and-managing-logical-volumes#multiple-partitions-on-a-disk_managing-lvm-physical-volumes



Introduction

Initializing a block device as a physical volume places a label near the start of the device. The following describes the LVM label:

  • An LVM label provides correct identification and device ordering for a physical device. An unlabeled, non-LVM device can change names across reboots depending on the order they are discovered by the system during boot. An LVM label remains persistent across reboots and throughout a cluster.
  • The LVM label identifies the device as an LVM physical volume. It contains a random unique identifier, the UUID for the physical volume. It also stores the size of the block device in bytes, and it records where the LVM metadata will be stored on the device.
  • By default, the LVM label is placed in the second 512-byte sector. You can overwrite this default setting by placing the label on any of the first 4 sectors when you create the physical volume. This allows LVM volumes to co-exist with other users of these sectors, if necessary.

The following describes the LVM metadata:

  • The LVM metadata contains the configuration details of the LVM volume groups on your system. By default, an identical copy of the metadata is maintained in every metadata area in every physical volume within the volume group. LVM metadata is small and stored as ASCII.
  • Currently LVM allows you to store 0, 1, or 2 identical copies of its metadata on each physical volume. The default is 1 copy. Once you configure the number of metadata copies on the physical volume, you cannot change that number at a later time. The first copy is stored at the start of the device, shortly after the label. If there is a second copy, it is placed at the end of the device. If you accidentally overwrite the area at the beginning of your disk by writing to a different disk than you intend, a second copy of the metadata at the end of the device will allow you to recover the metadata.

The following diagram illustrates the layout of an LVM physical volume. The LVM label is on the second sector, followed by the metadata area, followed by the usable space on the device.

Source: https://docs.redhat.com/en/documentation/red_hat_enterprise_linux/9/html/configuring_and_managing_logical_volumes/managing-lvm-physical-volumes_configuring-and-managing-logical-volumes




Set up the Disks

For this post I will create a new virtual machine in Microsoft Hyper-V to demonstrate using the growpart command and finally extending the logical volumes and resizing the file system.

I will create for each disks (OS + data disks) a raw partition which I will use to initialize a LVM physical volume (PV).


For the EFI System Partition disk the partition sda1 is created by default when creating the boot partition.


For the OS I will create a volume group and logical volume. The volume group I will place on disk /dev/sdb and its previously created raw partition /dev/sdb1.


For the volume group system_vg I was creating two logical volumes with root and swap.


Later I will create another volume group for the data which I will place on the disks /dev/sdc, /dev/sdd and /dev/sde and their corresponding raw partitions /dev/sdc1, /dev/sdd1 and /dev/sde1.


So far we just created a volume group named system_vg with two logical volumes (root and swap) placed on the disk partition /dev/sdb1.


I will now create as mentioned three further LVM physical volumes on the remaining disk partitions (sdc1, sdd1 and sde1) to place our volume group and logical volumes for the data.

More about how to create a LVM volume group and logical volumes you will find in my following post.


Therefore we can use as usual the pvcreate command, but this time instead initializing the whole disks like /dev/sdc, we initialize the disks partitions like /dev/sdc1.

# pvcreate /dev/sdc1 /dev/sdd1 /dev/sde1


Next I will create a new volume group (VG) and assigning all free disk partitions to by using the vgcreate command.

# vgcreate data_vg /dev/sdc1 /dev/sdd1 /dev/sde1


I can now create three new logical volumes on the new volume group named data_vg by using the lvcreate command and creating a file system on.

# lvcreate -L 50G -n data1 data_vg
# lvcreate -L 50G -n data2 data_vg
# lvcreate -L 50G -n data3 data_vg

To use all of the unallocated space we can use the following command:
# lvcreate -l 100%FREE -n data1 data_vg


For the files systems I will use ext4 and can use therefore the mkfs.ext4 command.

# mkfs.ext4 /dev/<volume group>/<logical volume>

# mkfs.ext4 /dev/data_vg/data1
# mkfs.ext4 /dev/data_vg/data2
# mkfs.ext4 /dev/data_vg/data3



Finally we can mount our logical volumes. Therefore I was first creating three new directories named data1data2 & data3 on the root file system.

# mkdir /data1
# mkdir /data2
# mkdir /data3

# mount /dev/data_vg/data1 /data1
# mount /dev/data_vg/data2 /data2
# mount /dev/data_vg/data3 /data3





Expanding Virtual Hard Disk in Microsoft Hyper-V

To demonstrate extending a disk partition by using the growpart command/tool, I will first expand the disk in Micrsoft Hyper-V.




I will add 30 GB to finally extend my /dev/sdc disk to 80 GB.





Rescan and Resize the Disks in Linux

By executing the lsblk command we can see that the OS so far doesn’t recognize the new size of our /dev/sdc disk. It is shown still at 50 GB.

We can now run the rescan command to make the OS aware of the new size. After that the size is shown with 80 GB. Note that the corresponding disk partition /dev/sdc1 is of course not resized by default and still at 50 GB.

# echo 1 > /sys/block/sdc/device/rescan


Our physical volume (PV) is still at 50 GB and so far not extended.


In case we initialized the whole disk as physical volume (PV), we can now usually run the pvresize command to also extend the physical volume, because the whole disk in this case is already extended and resized.

But here we are using a disk partition for the physical volume instead a whole disk, and this disk partition so far is not extended and resized, the pvresize command won’t work therefore.


As mentioned to the beginning, when using a disk partition for the physical volume (PV), we can use the growpart command/tool to first extend the underlying disk partition before we can finally extend the physical volume (PV) by using the pvresize command.

Growpart can only be used for the last disk partition, therefore to be as flexible as possible when using disk partitions for the LVM physical volume, you should just create one partition per disk to still be able to extend and resize logical volumes online.

!! Note !!
growpart is one of the utility to extend the last partition of the disk to fill the available free space on the disk. It changes the sector position to the end sector of the disk.

It only extends the last partition. It doesn’t create or delete any existing partition.
It can be run online. Dry run test can also be done to check the same.

Source: https://access.redhat.com/solutions/5540131


For SUSE Linux Enterprise we first need to connect to the SUSE Customer Center (SCC) to install growpart.


To finally install growpart on SLES we can use:

# zypper install growpart


To resize the /dev/sdc1 partition we can now execute the growpart command as shown below. This will resize partition 1 on /dev/sdc.

# growpart -v /dev/sdc 1

-v flag for verbose output


When we now checking the size of /dev/sdc1 again by using the lsblk command, we can see that also our disk partition was resized successfully to 80 GB.


The size of our physical volume (PV) so far is not extended to 80 GB and still at 50 GB. Therefore we can now use the pvresize command.


# pvresize /dev/sdc1


Now we can finally extend our logical volume(s) and their file system by 30 GB. Below I will for example extend the logical volume named data2 by 30 GB.

# lvextend -L+30G /dev/data_vg/data2


Finally we also need to resize the file system on /dev/data_vg/data2.

# resize2fs /dev/<volume group>/<logical volume>

Grow the file system with all available space
# resize2fs /dev/data_vg/data2

Increase the file system to 20 GiB
# resize2fs /dev/data_vg/data2 20G

Accepted size units for file system block sizes are:
S - 512 byte sectors
K - kilobytes
M - megabytes
G - gigabytes


So finally we resized our logical volume and file system placed on a disk partition successfully.





Links

What is growpart utility and how to use it?
https://access.redhat.com/solutions/5540131

Auto expand last partition to use all unallocated space, using parted in batch mode
https://unix.stackexchange.com/questions/373063/auto-expand-last-partition-to-use-all-unallocated-space-using-parted-in-batch-m

Managing LVM physical volumes
https://docs.redhat.com/en/documentation/red_hat_enterprise_linux/9/html/configuring_and_managing_logical_volumes/managing-lvm-physical-volumes_configuring-and-managing-logical-volumes

Multiple partitions on a disk
https://docs.redhat.com/en/documentation/red_hat_enterprise_linux/9/html/configuring_and_managing_logical_volumes/managing-lvm-physical-volumes_configuring-and-managing-logical-volumes#multiple-partitions-on-a-disk_managing-lvm-physical-volumes

Initializing disks or disk partitions
https://tldp.org/HOWTO/LVM-HOWTO/initdisks.html