Logical Volume Manager (LVM) Command Examples (2024)

LVM Command Examples

Display Physical Volumes (pvs and pvdisplay)

To display Physical Volumes (PV) on a system you can issue the pvs command:

[root@rhel01a ~]# pvs PV VG Fmt Attr PSize PFree /dev/sda2 vg_rhel01 lvm2 a-- 7.51g 0 /dev/sdb1 vg_rhel01 lvm2 a-- 4.99g 0

To display more details about a PV, you can issue the pvdisplay command:

[root@rhel01a ~]# pvdisplay --- Physical volume --- PV Name /dev/sda2 VG Name vg_rhel01 PV Size 7.51 GiB / not usable 3.00 MiB Allocatable yes (but full) PE Size 4.00 MiB Total PE 1922 Free PE 0 Allocated PE 1922 PV UUID 2fkPz2-8os0-4kJN-3moF-2prj-Z37s-cMC6Rh --- Physical volume --- PV Name /dev/sdb1 VG Name vg_rhel01 PV Size 4.99 GiB / not usable 2.41 MiB Allocatable yes (but full) PE Size 4.00 MiB Total PE 1278 Free PE 0 Allocated PE 1278 PV UUID Q3lBfV-tDi9-LJOw-nZKj-fge1-pqSQ-l0Cute

Display Volume Groups (vgs and vgdisplay)

To display basic information regarding a Volume Group on a Linux system you can issue the vgs command:

[root@rhel01a ~]# vgs VG #PV #LV #SN Attr VSize VFree vg_rhel01 2 2 0 wz--n- 12.50g 0 

From the above output we can see that there are 2 Physical Volumes (PV) assigned to the Volume Group (VG) vg_rhel01

For a more detailed view of Volume Groups on a Linux system, you can use the command vgdisplay

[root@rhel01a ~]# vgdisplay vg_rhel01 --- Volume group --- VG Name vg_rhel01 System ID Format lvm2 Metadata Areas 2 Metadata Sequence No 5 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 12.50 GiB PE Size 4.00 MiB Total PE 3200 Alloc PE / Size 3200 / 12.50 GiB Free PE / Size 0 / 0 VG UUID ttO0aF-RgCq-OZM7-dKcj-sDr0-vfg1-TDFzj0

Display Logical Volume Information (lvs and lvdisplay)

To display Logical Volume (LV) information on a Linux system you can issue the lvs command:


[root@rhel01a ~]# lvs LV VG Attr LSize Pool Origin Data% Move Log Copy% Convert lv_root vg_rhel01 -wi-ao-- 10.53g lv_swap vg_rhel01 -wi-ao-- 1.97g

For a more detailed display of Logical Volumes on a Linux system you can use the command lvdisplay:

[root@rhel01a ~]# lvdisplay /dev/vg_rhel01/lv_root --- Logical volume --- LV Path /dev/vg_rhel01/lv_root LV Name lv_root VG Name vg_rhel01 LV UUID 2LwbRo-KMuD-DQne-3NMI-Jndp-gClY-Zj3uL0 LV Write Access read/write LV Creation host, time rhel01, 2014-02-19 19:54:06 +0000 LV Status available # open 1 LV Size 10.53 GiB Current LE 2696 Segments 2 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:0

If you issue the "lvdisplay" command without specifying a specific Logical Volume, then all Logical Volumes on your system will be displayed. In the above example the parameter "/dev/vg_rhel01/lv_root" was passed to the lvdisplay command.


List Partition types on a Linux system

Although this command is not a LVM command, it us useful for displaying partition information. To display partition types and disk information on a Linux system, you can use the lsblk command:

[root@rhel01a ~]# lsblkNAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTsr0 11:0 1 1024M 0 rom sda 8:0 0 8G 0 disk ├─sda1 8:1 0 500M 0 part /boot└─sda2 8:2 0 7.5G 0 part ├─vg_rhel01-lv_root (dm-0) 253:0 0 10.5G 0 lvm / └─vg_rhel01-lv_swap (dm-1) 253:1 0 2G 0 lvm [SWAP]sdb 8:16 0 5G 0 disk └─sdb1 8:17 0 5G 0 part └─vg_rhel01-lv_root (dm-0) 253:0 0 10.5G 0 lvm /sdc 8:32 0 2G 0 disk 

List Partitions on a Linux system - fdisk

To display partition information on a Linux system, you can issue the fdisk -l command:


[root@rhel01a ~]# fdisk -l /dev/sdaDisk /dev/sda: 8589 MB, 8589934592 bytes255 heads, 63 sectors/track, 1044 cylindersUnits = cylinders of 16065 * 512 = 8225280 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk identifier: 0x00029be4 Device Boot Start End Blocks Id System/dev/sda1 * 1 64 512000 83 LinuxPartition 1 does not end on cylinder boundary./dev/sda2 64 1045 7875584 8e Linux LVM 8:32 0 2G 0 disk 

If you issue the command "fdisk -l" without specifying a disk, then all disk information is displayed. Sometimes it is useful to use the command in conjunction with "grep" to limit the output displayed:

[root@rhel01a ~]# fdisk -l | grep /dev/sdDisk /dev/sda: 8589 MB, 8589934592 bytes/dev/sda1 * 1 64 512000 83 Linux/dev/sda2 64 1045 7875584 8e Linux LVMDisk /dev/sdb: 5368 MB, 5368709120 bytes/dev/sdb1 1 652 5237158+ 8e Linux LVMDisk /dev/sdc: 2147 MB, 2147483648 bytes

From the above we can see that we have a spare disk on our Red Hat system "/dev/sdc". This command is useful to help identify a disk that has just been added to a system.

Create a Physical Volume (pvcreate)

To add a new disk into a system that is to be managed by LVM, you must use the command pvcreate

[root@rhel01a ~]# pvcreate /dev/sdc Writing physical volume data to disk "/dev/sdc" Physical volume "/dev/sdc" successfully created

Remove a Physical Volume (pvremove)

To remove a Physical Volume from a system, you use the pvremove command:

[root@rhel01a ~]# pvremove /dev/sdc Labels on physical volume "/dev/sdc" successfully wiped

Create a New Volume Group (vgcreate)

To create a new Volume Group (VG) you must use the vgcreate command. You will also need to specify which Physical Volume (PV) is to be associated with the new Volume Group. In the example below we are using a newly added disk /dev/sdc. Before the newly added disk can be used we use the pvcreate command. The command pvs is used to show that it is not associated with any existing Volume Groups.

[root@rhel01a ~]# pvcreate /dev/sdc Writing physical volume data to disk "/dev/sdc" Physical volume "/dev/sdc" successfully created[root@rhel01a ~]# pvs PV VG Fmt Attr PSize PFree /dev/sda2 vg_rhel01 lvm2 a-- 7.51g 0 /dev/sdb1 vg_rhel01 lvm2 a-- 4.99g 0 /dev/sdc lvm2 a-- 2.00g 2.00g[root@rhel01a ~]# vgcreate vg01 /dev/sdc Volume group "vg01" successfully created

Now when the pvs command is issued, we can see that the storage is now associated with the new Volume Group "vg01".

[root@rhel01a ~]# pvs PV VG Fmt Attr PSize PFree /dev/sda2 vg_rhel01 lvm2 a-- 7.51g 0 /dev/sdb1 vg_rhel01 lvm2 a-- 4.99g 0 /dev/sdc vg01 lvm2 a-- 2.00g 2.00g

Rename a Volume Group (vgrename)

To rename an existing Volume Group, the vgrename command is used:

[root@rhel01a ~]# vgs VG #PV #LV #SN Attr VSize VFree vg01 1 0 0 wz--n- 2.00g 2.00g vg_rhel01 2 2 0 wz--n- 12.50g 0 [root@rhel01a ~]# vgrename vg01 test_vg01 Volume group "vg01" successfully renamed to "test_vg01"[root@rhel01a ~]# vgs VG #PV #LV #SN Attr VSize VFree test_vg01 1 0 0 wz--n- 2.00g 2.00g vg_rhel01 2 2 0 wz--n- 12.50g 0 

In the above example, we used the vgrename command to rename the existing Volume Group "vg01" to "test_vg01". The command vgs was used to display the before and after results.

Remove a Volume Group

To remove a Volume Group, the command vgremove is used:

[root@rhel01a ~]# vgremove test_vg01 Volume group "test_vg01" successfully removed

Extend a Volume Group (vgextend)

To add additional space to an existing Volume Group, the command vgextend is used. You also need to supply the name of the Physical Volume (disk) that is going to be added. In the following example, we are going to add 2GB of space to the existing Volume Group vg_rhel01

[root@rhel01a ~]# vgdisplay vg_rhel01 --- Volume group --- VG Name vg_rhel01 System ID Format lvm2 Metadata Areas 2 Metadata Sequence No 5 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 12.50 GiB PE Size 4.00 MiB Total PE 3200 Alloc PE / Size 3200 / 12.50 GiB Free PE / Size 0 / 0 VG UUID ttO0aF-RgCq-OZM7-dKcj-sDr0-vfg1-TDFzj0 [root@rhel01a ~]# vgextend vg_rhel01 /dev/sdc Volume group "vg_rhel01" successfully extended[root@rhel01a ~]# vgdisplay vg_rhel01 --- Volume group --- VG Name vg_rhel01 System ID Format lvm2 Metadata Areas 3 Metadata Sequence No 6 VG Access read/write VG Status resizable MAX LV 0 Cur LV 2 Open LV 2 Max PV 0 Cur PV 3 Act PV 3 VG Size 14.50 GiB PE Size 4.00 MiB Total PE 3711 Alloc PE / Size 3200 / 12.50 GiB Free PE / Size 511 / 2.00 GiB VG UUID ttO0aF-RgCq-OZM7-dKcj-sDr0-vfg1-TDFzj0

In the above example, we used the vgdisplay command to display the current size and associated Physical Volumes with the Volume Group "vg_rhel01". Next the vgextend command is issued along with the specified Physical Volume to use. After the command was issued, the vgdisplay command was used to display the new attributes of the specified Volume Group. From the output you can see the size and associated PV count has increased.

Remove a Physical Volume from a Volume Group (vgreduce)

To remove storage from a Volume Group, the command vgreduce is used. This command is the opposite of the vgextend command. IN the following example, we will remove the specified PV from the Volume Group:

[root@rhel01a ~]# pvs PV VG Fmt Attr PSize PFree /dev/sda2 vg_rhel01 lvm2 a-- 7.51g 0 /dev/sdb1 vg_rhel01 lvm2 a-- 4.99g 0 /dev/sdc vg_rhel01 lvm2 a-- 2.00g 2.00g[root@rhel01a ~]# vgreduce vg_rhel01 /dev/sdc Removed "/dev/sdc" from volume group "vg_rhel01"[root@rhel01a ~]# pvs PV VG Fmt Attr PSize PFree /dev/sda2 vg_rhel01 lvm2 a-- 7.51g 0 /dev/sdb1 vg_rhel01 lvm2 a-- 4.99g 0 /dev/sdc lvm2 a-- 2.00g 2.00g

The pvs command is initially used to show the relationship between the Physical Volume and the Volume Group. After the vgreduce command is issued, we use the pvs command to verify that the Physical Volume "/dev/sdc" is no longer attached to the Volume Group "vg_rhel01

Create a Logical Volume (lvcreate)

To create a Logical Volume on an existing Volume Group, the command lvcreate is used.

 [root@rhel01a ~]# lvcreate -n lv01 -l 100%VG vg01 Logical volume "lv01" created

The above example will create a Logical Volume (LV) called lv01 on the Volume Group (VG) vg01. We have also specified that the Logical Volume should utilise all available space within the Volume Group ( 100%VG )

Create a Logical Volume (lvcreate) with a specified size

IN the following example, the command lvcreate is used to create a new Logical Volume. This time, we are going to specify the amount of space to be used from the Volume Group. IN the example, we will allocate 1GB of space from the Volume Group.

 [root@rhel01a ~]# pvs PV VG Fmt Attr PSize PFree /dev/sda2 vg_rhel01 lvm2 a-- 7.51g 0 /dev/sdb1 vg_rhel01 lvm2 a-- 4.99g 0 /dev/sdc vg01 lvm2 a-- 2.00g 2.00g[root@rhel01a ~]# lvcreate -n lv01 -L1G vg01 Logical volume "lv01" created[root@rhel01a ~]# pvs PV VG Fmt Attr PSize PFree /dev/sda2 vg_rhel01 lvm2 a-- 7.51g 0 /dev/sdb1 vg_rhel01 lvm2 a-- 4.99g 0 /dev/sdc vg01 lvm2 a-- 2.00g 1020.00m

In the above example we can see that initially there is 2GB of space allocated to the Volume Group vg01. We then issued the command:

lvcreate -n lv01 -L1G vg01

The command will create a Logical Volume (LV) called lv01 with an allocation of 1GB from the Volume Group (VG) vg01

Add Space to a File System with lvextend

To add space to an existing file system, the command lvextend is used. In the example below we can see that a file system called testfs exists and currently has 924MB of available space. Before we can add space, you must verify that space is available to the Logical Volume. To see the current allocation of space to a file system we use the df command. To view available space we can issue the command pvs. IN the example below we can see that there is 1020MB of available space within Volume Group vg01.

 [root@rhel01a /]# df -h /testfsFilesystem Size Used Avail Use% Mounted on/dev/mapper/vg01-lv01 1008M 34M 924M 4% /testfs[root@rhel01a /]# pvs PV VG Fmt Attr PSize PFree /dev/sda2 vg_rhel01 lvm2 a-- 7.51g 0 /dev/sdb1 vg_rhel01 lvm2 a-- 4.99g 0 /dev/sdc vg01 lvm2 a-- 2.00g 1020.00m[root@rhel01a /]# lvextend /dev/vg01/lv01 -L+1000M -r Extending logical volume lv01 to 1.98 GiB Logical volume lv01 successfully resizedresize2fs 1.41.12 (17-May-2010)Filesystem at /dev/mapper/vg01-lv01 is mounted on /testfs; on-line resizing requiredold desc_blocks = 1, new_desc_blocks = 1Performing an on-line resize of /dev/mapper/vg01-lv01 to 518144 (4k) blocks.The filesystem on /dev/mapper/vg01-lv01 is now 518144 blocks long.

The command lvextend /dev/vg01/lv01 -L+1000M -r is used to increase the size of the Logical Volume by 1000MB. the -r option is specified as this will carry out a resize of the file system online.

Now when we use the df command, we can see that the file system has been increased in size.

[root@rhel01a /]# df -h testfs/Filesystem Size Used Avail Use% Mounted on/dev/mapper/vg01-lv01 2.0G 34M 1.9G 2% /testfs

Specify the size of a file system with lvextend

If you wanted to specify the size of a file system, we can use the option -L. Here we can then specify the size we require in MB/GB. In the example below we are going to specify a size of 1.5GB should be used with a file system. Currently there is only 435MB of space available to the file system testfs

[root@rhel01a /]# df -h /testfs/Filesystem Size Used Avail Use% Mounted on/dev/mapper/vg01-lv01 492M 33M 435M 8% /testfs[root@rhel01a /]# lvextend /dev/vg01/lv01 -L1.5GB -r Extending logical volume lv01 to 1.50 GiB Logical volume lv01 successfully resizedresize2fs 1.41.12 (17-May-2010)Filesystem at /dev/mapper/vg01-lv01 is mounted on /testfs; on-line resizing requiredold desc_blocks = 1, new_desc_blocks = 1Performing an on-line resize of /dev/mapper/vg01-lv01 to 393216 (4k) blocks.The filesystem on /dev/mapper/vg01-lv01 is now 393216 blocks long.

Reduce the size of a file system (lvreduce)

To reduce the size of a file system, the lvreduce command is used to change the size of the underlying Logical Volume. WARNING use this command with care as you can loose data if you shrink your file system.

In the example below, we will use the command lvreduce /dev/vg01/lv01 -L500M -r

[root@rhel01a /]# df -h /testfs/Filesystem Size Used Avail Use% Mounted on/dev/mapper/vg01-lv01 1.5G 34M 1.4G 3% /testfs[root@rhel01a /]# lvreduce /dev/vg01/lv01 -L500M -rDo you want to unmount "/testfs"? [Y|n] yfsck from util-linux-ng 2.17.2/dev/mapper/vg01-lv01: 11/98304 files (9.1% non-contiguous), 14756/393216 blocksresize2fs 1.41.12 (17-May-2010)Resizing the filesystem on /dev/mapper/vg01-lv01 to 128000 (4k) blocks.The filesystem on /dev/mapper/vg01-lv01 is now 128000 blocks long. Reducing logical volume lv01 to 500.00 MiB Logical volume lv01 successfully resized[root@rhel01a /]# df -h testfs/Filesystem Size Used Avail Use% Mounted on/dev/mapper/vg01-lv01 492M 33M 435M 8% /testfs

Reduce a file system by a specified amount

In the following example, we will specify the amount of size to reduce the file system by:

[root@rhel01a /]# df -h /testfs/Filesystem Size Used Avail Use% Mounted on/dev/mapper/vg01-lv01 492M 33M 435M 8% /testfs[root@rhel01a /]# lvreduce /dev/vg01/lv01 -L-235M -r Rounding size to boundary between physical extents: 232.00 MiBDo you want to unmount "/testfs"? [Y|n] yfsck from util-linux-ng 2.17.2/dev/mapper/vg01-lv01: 11/32768 files (9.1% non-contiguous), 10449/128000 blocksresize2fs 1.41.12 (17-May-2010)Resizing the filesystem on /dev/mapper/vg01-lv01 to 68608 (4k) blocks.The filesystem on /dev/mapper/vg01-lv01 is now 68608 blocks long. Reducing logical volume lv01 to 268.00 MiB Logical volume lv01 successfully resized[root@rhel01a /]# df -h /testfs/Filesystem Size Used Avail Use% Mounted on/dev/mapper/vg01-lv01 262M 33M 217M 14% /testfs

Logical Volume Manager (LVM) Command Examples (2024)

FAQs

What is LVM in Linux with example? ›

Logical Volume Manager (LVM) is used on Linux to manage hard drives and other storage devices. As the name implies, it can sort raw storage into logical volumes, making it easy to configure and use. In this guide, you'll learn how LVM works on Linux systems.

Which command is used to create a logical volume with LVM? ›

To create a logical volume, use the lvcreate command.

How do I use LVM command? ›

Landoflinux
  1. Display Physical Volumes (pvs and pvdisplay) ...
  2. Display Volume Groups (vgs and vgdisplay) ...
  3. Display Logical Volume Information (lvs and lvdisplay) ...
  4. List Partitions on a Linux system - fdisk. ...
  5. Create a Physical Volume (pvcreate) ...
  6. Create a New Volume Group (vgcreate) ...
  7. Rename a Volume Group (vgrename)

How to check free space in Linux LVM? ›

Tips to check Disk Space in Linux [10 Methods]
  1. df command.
  2. fdisk command.
  3. parted command.
  4. du command.
  5. lsblk command.
  6. blockdev command.
  7. lshw command.
  8. Checking disk size from the system logs.

How do I format a logical volume in Linux? ›

In order to format a logical volume, you have to use the “mkfs” command and specify the filesystem to be used. Now that the logical volume is formatted, you simply have to mount it on one folder on your system.

What are the commands for LVM in Linux? ›

lvm Command Examples in Linux
  • The lvm command is used to manage physical volumes, volume groups, and logical volumes in a Linux Logical Volume Manager (LVM) system. ...
  • Creating and deleting physical volumes. ...
  • # lvm. ...
  • # lvm help. ...
  • # lvm pvcreate /dev/sdXY. ...
  • # lvm pvdisplay. ...
  • # lvm vgcreate vg1 /dev/sdXY. ...
  • # lvm vgdisplay.

How to activate LVM in Linux? ›

The procedure to mount LVM partition in Linux as follows:
  1. Run vgscan command scans all supported LVM block devices in the system for VGs.
  2. Execute vgchange command to activate volume.
  3. Type lvs command to get information about logical volumes.
  4. Create a mount point using the mkdir command.
Mar 23, 2023

What is LVM and how it works? ›

Logical volume management (LVM) is a form of storage virtualization that offers system administrators a more flexible approach to managing disk storage space than traditional partitioning.

How to check LVM partition in Linux? ›

There are three commands you can use to display properties of LVM logical volumes: lvs , lvdisplay , and lvscan . The lvs command provides logical volume information in a configurable form, displaying one line per logical volume. The lvs command provides a great deal of format control, and is useful for scripting.

When to use LVM in Linux? ›

LVM is used for the following purposes: Creating single logical volumes of multiple physical volumes or entire hard disks (somewhat similar to RAID 0, but more similar to JBOD), allowing for dynamic volume resizing.

How do I resize a LVM partition? ›

How to Extend Linux LVM Logical Volume
  1. Create a new partition on hard disk.
  2. Add the partition you just created as a physical volume.
  3. Add the new physical volume to the volume group.
  4. Assign space from the volume group to the logical volume.
  5. Resize the filesystem.

How do I know if my LVM is used? ›

If the line starts with UUID=xyz , this means it's a physical partition. If the line starst with /dev/sdaX , it also means it's a physical partition. The indicator for LVM would be something with /dev/mapper/xyz .

How do I know if my Linux disk is full? ›

That command is df -H. The -H switch is for human-readable format. The output of df -H will report how much space is used, available, percentage used, and the mount point of every disk attached to your system (Figure 1).

What is the purpose of LVM in Linux? ›

The goal of LVM is to facilitate managing the sometimes conflicting storage needs of multiple end users. Using the volume management approach, the administrator is not required to allocate all disk storage space at initial setup.

What is the function of LVM in Linux? ›

LVM can be used to combine physical volumes into volume groups to unify the storage space available on a system. Afterwards, administrators can segment the volume group into arbitrary logical volumes, which act as flexible partitions.

Should I use LVM when installing Linux? ›

For those wanting a more flexible setup—logical volume manager (LVM) is the way to go—you benefit from setting up volume groups and define your logical volumes within them. LVM is a hard drive management system that allows you to create storage space across multiple physical drives.

References

Top Articles
Latest Posts
Article information

Author: Prof. An Powlowski

Last Updated:

Views: 6255

Rating: 4.3 / 5 (64 voted)

Reviews: 95% of readers found this page helpful

Author information

Name: Prof. An Powlowski

Birthday: 1992-09-29

Address: Apt. 994 8891 Orval Hill, Brittnyburgh, AZ 41023-0398

Phone: +26417467956738

Job: District Marketing Strategist

Hobby: Embroidery, Bodybuilding, Motor sports, Amateur radio, Wood carving, Whittling, Air sports

Introduction: My name is Prof. An Powlowski, I am a charming, helpful, attractive, good, graceful, thoughtful, vast person who loves writing and wants to share my knowledge and understanding with you.