How to use the command 'lvm' (with examples)
- Linux
- December 25, 2023
The ’lvm’ command is used to manage physical volumes, volume groups, and logical volumes using the Logical Volume Manager (LVM) interactive shell. LVM provides a way to manage disk space efficiently by abstracting physical storage into logical volumes that can be dynamically resized.
Use case 1: Start the Logical Volume Manager interactive shell
Code:
sudo lvm
Motivation: Starting the LVM interactive shell allows you to access the commands and functionality provided by LVM. This is especially useful when you need to perform multiple LVM operations in a single session.
Explanation: The ‘sudo lvm’ command starts the LVM interactive shell with root privileges. Once inside the shell, you can enter LVM commands and perform various operations related to managing physical volumes, volume groups, and logical volumes.
Example output:
lvm>
Use case 2: List the Logical Volume Manager commands
Code:
sudo lvm help
Motivation: Listing the available LVM commands helps you explore the functionalities and options provided by LVM. This can be helpful in learning how to use LVM effectively and efficiently.
Explanation: The ‘sudo lvm help’ command displays a list of available LVM commands with a brief description of each command. This allows you to familiarize yourself with the available options and functionalities.
Example output:
Available LVM commands:
config
help
version
...
Use case 3: Initialize a drive or partition to be used as a physical volume
Code:
sudo lvm pvcreate /dev/sdXY
Motivation: Initializing a drive or partition as a physical volume is the first step in using LVM for storage management. This allows LVM to recognize and utilize the specified drive or partition as part of the logical volume configuration.
Explanation: The ‘sudo lvm pvcreate’ command creates a physical volume on the specified drive or partition (/dev/sdXY). This step prepares the drive or partition for use in LVM’s logical volume configuration.
Example output:
Physical volume "/dev/sdXY" successfully created.
Use case 4: Display information about physical volumes
Code:
sudo lvm pvdisplay
Motivation: Displaying information about physical volumes gives you an overview of the current physical volume configuration. This can be helpful in verifying the status and usage of physical volumes in your LVM setup.
Explanation: The ‘sudo lvm pvdisplay’ command displays detailed information about all available physical volumes, including their size, allocation, and metadata. This allows you to gather information about your physical volumes for monitoring and troubleshooting purposes.
Example output:
--- Physical volume ---
PV Name /dev/sdXY
VG Name vg1
...
Use case 5: Create a volume group from a physical volume
Code:
sudo lvm vgcreate vg1 /dev/sdXY
Motivation: Creating volume groups allows you to group multiple physical volumes together, creating a pool of storage that can be used for creating logical volumes. This provides flexibility in managing storage resources and allows for easier resizing and management of logical volumes.
Explanation: The ‘sudo lvm vgcreate’ command creates a volume group named vg1 using the specified physical volume (/dev/sdXY). This step combines the specified physical volume with any existing physical volumes in the volume group, forming a unified storage pool.
Example output:
Volume group "vg1" successfully created.
Use case 6: Display information about volume groups
Code:
sudo lvm vgdisplay
Motivation: Displaying information about volume groups provides you with an overview of the current volume group configuration. This can be useful in verifying the status and usage of volume groups in your LVM setup.
Explanation: The ‘sudo lvm vgdisplay’ command displays detailed information about all available volume groups, including their size, allocation, and metadata. This allows you to gather information about your volume groups for monitoring and troubleshooting purposes.
Example output:
--- Volume group ---
VG Name vg1
...
Use case 7: Create a logical volume from a volume group
Code:
sudo lvm lvcreate -L 10G vg1
Motivation: Creating logical volumes allows you to allocate storage space from volume groups for specific use cases. Logical volumes can be easily resized and managed independently from the underlying physical storage.
Explanation: The ‘sudo lvm lvcreate’ command creates a logical volume with a size of 10GB (-L 10G) from the specified volume group (vg1). This step allocates a specific amount of storage from the volume group for use as a logical volume.
Example output:
Logical volume "/dev/vg1/lvol0" successfully created.
Use case 8: Display information about logical volumes
Code:
sudo lvm lvdisplay
Motivation: Displaying information about logical volumes provides you with an overview of the current logical volume configuration. This can be helpful in verifying the status and usage of logical volumes in your LVM setup.
Explanation: The ‘sudo lvm lvdisplay’ command displays detailed information about all available logical volumes, including their size, allocation, and metadata. This allows you to gather information about your logical volumes for monitoring and troubleshooting purposes.
Example output:
--- Logical volume ---
...
LV Name /dev/vg1/lvol0
...
Conclusion:
The ’lvm’ command provides a powerful set of tools for managing physical volumes, volume groups, and logical volumes using the Logical Volume Manager (LVM) interactive shell. By following the examples provided in this article, you can effectively use the ’lvm’ command to create, manage, and monitor your storage resources using LVM.