tencent cloud

TencentOS Server

LVM Logical Volume Management

Download
Focus Mode
Font Size
Last updated: 2026-10-19 17:22:50
AI-Translated

Introduction to LVM

LVM (Logical Volume Manager) is a device mapper framework that provides logical volume management capabilities. Logical volumes reside between block devices and specific file systems, allowing upper-layer applications to ignore the physical layout of block devices and achieve more convenient and flexible device usage.

Physical Volume Management

Creating a Physical Volume

Use the pvcreate command to initialize a block device as an LVM physical volume. The following example uses /dev/vdb:
Warning:
pvcreate writes LVM labels and metadata to the target device. If a file system or other signatures already exist on the device, continuing the operation may destroy the original data. Before proceeding, confirm that the target device is correct and that you have backed up any data you need to keep.
pvcreate /dev/vdb
Output example:
Physical volume "/dev/vdb" successfully created.
If a file system signature already exists on the target device, the command prompts you to confirm whether to clear the signature. The following shows an example output:
WARNING: ext4 signature detected on /dev/vdb at offset 1080. Wipe it? [y/n]:
By default, the object is created with the maximum size currently available on the device.

Viewing Physical Volumes

The pvdisplay command displays attribute information of a Physical Volume, including its name, UUID, size, status, and allocation details.
pvdisplay /dev/vdb
Output example:
--- Physical volume ---
PV Name /dev/vdb
VG Name test
PV Size 200.00 GiB / not usable 4.00 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 51199
Free PE 51199
Allocated PE 0
PV UUID gQB42U-IKt2-517n-RB3q-p9ob-bAz7-Ggvjtd
Run the pvdisplay command directly without any parameters to list information about all physical volumes in the system. You can also specify one or more physical volume names as parameters to display information only for the specified physical volumes. The output of the pvdisplay command includes the following information:
PV Name: The name of the physical volume.
UUID: The UUID of the physical volume.
Total PE / Free PE: The size of the physical volume.
Allocatable / Exported / Missing: The status of the physical volume.
Attributes of the physical volume (for example, whether it is encrypted, whether it is read-only, and so on)
VG Name: The volume group to which the physical volume belongs.
LV Name: The logical volume on the physical volume.
The pvs command displays the status of physical volumes:
pvs
Output example:
PV VG Fmt Attr PSize PFree
/dev/vdb test lvm2 a-- <200.00g <200.00g

Modifying Physical Volume Attributes

The pvchange command is a command in LVM (Logical Volume Manager) on Linux systems, used to change the attributes of a physical volume. The following describes how to use the pvchange command:
pvchange [options] [PhysicalVolumePath...]
Here, PhysicalVolumePath is the path of the physical volume whose attributes you want to change. You can specify multiple physical volume paths, separated by spaces. The following are some common options:
-a, --all: Change the attributes of all physical volumes.
-x, --allocatable y/n: Set whether the physical volume is allocatable. y indicates allocatable, and n indicates not allocatable.
-t, --test: Test mode. It does not actually change the attributes of the physical volume, but displays the operations that would be performed.
Note:
When using the pvchange command, back up important data and proceed with caution to avoid data loss or other unnecessary damage.
The following examples modify a single physical volume, all physical volumes, and use test mode to verify the configuration:
1. Set the physical volume /dev/sdb1 to non-allocatable (you must first assign it to a logical volume group, and the same applies below):
pvchange -x n /dev/sdb1
2. Set all physical volumes to allocatable:
pvchange -x y -a
3. Test the operation of setting the physical volume /dev/sdb1 to non-allocatable:
pvchange -x n -t /dev/sdb1

Deleting a Physical Volume

Deleting a physical volume clears the LVM metadata on that physical volume. Before deletion, confirm that the physical volume no longer belongs to any volume group and that the data you need to retain has been backed up:
Warning:
If the physical volume still belongs to a volume group, use vgreduce to remove it from the volume group first. Otherwise, pvremove will fail. Forcibly deleting a physical volume that belongs to a volume group may cause data loss in that volume group.
pvremove /dev/vdb
Output example:
Labels on physical volume "/dev/vdb" successfully wiped.

Logical Volume Group Management

Creating a Volume Group

The vgcreate command creates a Volume group (VG) and adds one or more physical volumes (PV) to it. Before execution, confirm that the target physical volumes have been initialized by pvcreate and are not used by any other Volume group.
The following example creates a volume group named myvg and adds the physical volume /dev/sdb1 to it:
vgcreate myvg /dev/sdb1
Output example:
Volume group "myvg" successfully created
To add multiple physical volumes when creating a volume group, specify the device paths in order at the end of the command:
vgcreate myvg /dev/sdb1 /dev/sdc1 /dev/sdd1
Common options are as follows:
-s, --physicalextentsize: Specifies the size of the physical extent (PE).
-l, --maxlogicalvolumes: Specifies the maximum number of logical volumes that can be created in the volume group.
-p, --maxphysicalvolumes: Specifies the maximum number of physical volumes that the volume group can contain.
-A, --autobackup y|n: Specifies whether to automatically back up volume group metadata after changes. It is recommended to keep this option set to y.
The following example creates a volume group named myvg with a PE size of 16 MiB:
vgcreate -s 16M myvg /dev/sdb1
After creation, run vgdisplay myvg or vgs myvg to verify the volume group status. The PE size is difficult to adjust after the volume group is created, so plan it properly based on your business needs before creation.

Viewing Volume Groups

The vgdisplay command is used to view detailed information about a volume group, including its UUID, size, status, number of physical volumes, number of logical volumes, and free PEs. The following example views the volume group myvg:
vgdisplay myvg
Output example (excerpt):
--- Volume group ---
VG Name myvg
VG Status resizable
Cur LV 0
Cur PV 1
VG Size <200.00 GiB
PE Size 4.00 MiB
Total PE 51199
Alloc PE / Size 0 / 0
Free PE / Size 51199 / <200.00 GiB
To view all volume groups in a table format, run the following command:
vgs
Output example:
VG #PV #LV #SN Attr VSize VFree
myvg 1 0 0 wz--n- <200.00g <200.00g
You can determine whether the volume group is available and whether the remaining space is sufficient based on fields in the output, such as VG Status, Cur PV, Cur LV, VG Size, and VFree.

Modifying Volume Group Attributes

The vgchange command is used to modify attributes of a volume group and its logical volumes, such as the activation status and whether the volume group allows adding or removing physical volumes. Common options are as follows:
-a, --activate y|n|ay: Activates or disables logical volumes in the volume group. y means activate, n means disable, and ay means automatically activate.
-x, --resizeable y|n: Allows or prohibits adding or removing physical volumes through vgextend and vgreduce.
-u, --uuid: Generates a new random UUID for the volume group. Before modifying the UUID, ensure that the volume group is inactive.
Warning:
Disabling a volume group makes the logical volumes in it inaccessible and may interrupt services that are using these logical volumes. Before doing so, unmount the relevant file systems and confirm that no processes are using the corresponding logical volumes.
1. Disable the logical volumes in volume group myvg (no output is returned on success):
vgchange -a n myvg
2. Reactivate the logical volumes in volume group myvg:
vgchange -a y myvg
Output example:
1 logical volume(s) in volume group "myvg" now active
After the modification is complete, you can run lvs -o lv_name,vg_name,lv_attr to verify the activation status of the logical volume.

Scaling a Volume Group

Extension

The vgextend command is used to add one or more physical volumes to an existing volume group, thereby increasing the space that the volume group can allocate to logical volumes. Before doing so, confirm that the devices to be added are not used by other volume groups.
The following example adds the physical volume /dev/sdc1 to the volume group myvg:
vgextend myvg /dev/sdc1
Output example:
Volume group "myvg" successfully extended
To add multiple physical volumes at once, specify the device paths in order at the end of the command:
vgextend myvg /dev/sdc1 /dev/sdd1
Common options are as follows:
-A, --autobackup y|n: Specifies whether to automatically back up volume group metadata after changes. It is recommended to keep this option set to y.
-t, --test: Performs a test operation without writing volume group metadata.
-v, --verbose: Displays detailed execution information.
The PE size of a volume group is determined by vgcreate -s at creation time and cannot be modified through vgextend. After the extension is complete, run the following command to verify the volume group space:
vgs myvg
Confirm that the number of physical volumes and the free space in the output meet expectations.

Reduction

The vgreduce command is used to remove one or more unused physical volumes from a volume group. If the physical volume to be removed still contains allocated PEs, use pvmove first to migrate data to other physical volumes in the same volume group.
Warning:
Removing a physical volume that still contains data can result in an incomplete logical volume and data loss. Before proceeding, use pvs -o pv_name,pv_used to confirm that the PUsed value of the target physical volume is 0, and back up important data.
The command syntax is as follows:
vgreduce [options] <VG> <PV> [<PV>...]
<VG> represents the volume group name, and <PV> represents the path of the physical volume to be removed. Common options are as follows:
-a, --all: Removes all physical volumes in the volume group that are free and do not contain data.
-f, --force: Forces the operation, which may cause data loss and is not recommended for regular scenarios.
-t, --test: Performs a test operation without writing volume group metadata.
The example command is as follows:
1. Remove the physical volume /dev/sdb1 from the volume group vg01:
vgreduce vg01 /dev/sdb1
2. Remove all free physical volumes from the volume group vg01:
vgreduce -a vg01
3. Use test mode to check the operation of removing /dev/sdb1 from the volume group vg01:
vgreduce -t vg01 /dev/sdb1
After the removal is complete, run pvs or vgs -o vg_name,pv_count to verify the number of physical volumes in the volume group.

Deleting a Volume Group

The vgremove command is used to delete a volume group and the logical volumes in it. After deletion, the physical volumes in the volume group retain their LVM physical volume tags and can be reused by other volume groups.
Warning:
Deleting a volume group deletes all logical volumes in it and their data, and this operation cannot be undone. Before doing so, unmount the file systems corresponding to the logical volumes in the volume group, confirm that relevant data has been backed up, and preferably use test mode for verification.
The command syntax is as follows:
vgremove [options] <VG>
<VG> represents the name of the volume group to be deleted. Common options are as follows:
-f, --force: Forces the operation and bypasses some confirmations. It is not recommended for regular scenarios.
-t, --test: Performs a test operation without writing volume group metadata.
The example command is as follows:
1. Use test mode to check the operation of deleting the volume group vg01:
vgremove -t vg01
2. After confirmation, delete the volume group vg01:
vgremove vg01
Output example:
Volume group "vg01" successfully removed
After the deletion is complete, run vgs to confirm that vg01 no longer appears in the volume group list.

Logical Volume Management

Creating a Logical Volume

The lvcreate command is used to create logical volumes from the free space of a volume group. This command only creates block devices and does not create file systems such as ext4 or xfs.
The command syntax is as follows:
lvcreate -L <size> -n <LV> <VG>
<size> represents the logical volume size, <LV> represents the logical volume name, and <VG> represents the volume group name. Common options are as follows:
-L, --size: Specifies the size of the logical volume. Units such as K, M, G, and T can be used.
-l, --extents: Specifies the logical volume size by the number of PEs or as a percentage of the volume group space.
-n, --name: Specifies the logical volume name.
-r, --readahead: Specifies the number of read-ahead sectors for the logical volume. It can also be set to auto or none.
Warning:
Running mkfs on a logical volume clears existing data on that logical volume. Format only logical volumes that are newly created or confirmed to contain no data that needs to be retained.
The example command is as follows:
1. Create a 10GiB logical volume named lv01 in the volume group vg01:
lvcreate -L 10G -n lv01 vg01
Output example:
Logical volume "lv01" created.
2. Create a 100MiB logical volume named lv02, and then create an ext4 file system on it:
lvcreate -L 100M -n lv02 vg01
mkfs.ext4 /dev/vg01/lv02
3. Create a 1TiB logical volume named lv03 with 256 read-ahead sectors:
lvcreate -L 1T -n lv03 -r 256 vg01
After creation is complete, run lvs -o lv_name,vg_name,lv_size,lv_attr to verify the logical volume status.

Viewing Logical Volumes

The lvdisplay command is used to view detailed information about a logical volume, including its name, path, size, status, and volume group.
The example command is as follows:
1. View detailed information about the logical volume lv01:
lvdisplay /dev/vg01/lv01
Output example (excerpt):
--- Logical volume ---
LV Path /dev/vg01/lv01
LV Name lv01
VG Name vg01
LV Status available
LV Size 10.00 GiB
2. View all logical volumes in the system:
lvdisplay
3. View the logical volume lv01 in colon-separated format:
lvdisplay -c /dev/vg01/lv01
To view logical volumes in a table format, use the lvs command.

Extending a Logical Volume

The lvextend command is used to add capacity to a logical volume from the free space of a volume group. Common options are as follows:
-L, --size: Specifies the absolute size after extension. When the + prefix is used, it indicates that capacity is added to the current size.
-l, --extents: Extends the logical volume by the number of PEs or as a percentage of the volume group space.
-r, --resizefs: After the logical volume is extended, resizes the supported file system accordingly.
Note:
Before extending, ensure that the volume group has sufficient free space and back up important data. If -r is not used, extend the file system separately according to its type after extending the logical volume.
The example command is as follows:
1. Extend the capacity of logical volume lv01 to 20GiB:
lvextend -L 20G /dev/vg01/lv01
2. Extend logical volume lv02 to 200MiB and resize the file system accordingly:
lvextend -L 200M -r /dev/vg01/lv02
After the extension is complete, run lvs /dev/vg01/lv01 /dev/vg01/lv02 to verify the logical volume capacity. If -r is not used, the ext4 file system can be extended with resize2fs, and a mounted xfs file system should be extended with xfs_growfs <mount point>.

Resizing a Logical Volume

The lvresize command can both extend and shrink a logical volume. Common options are as follows:
-L, --size: Specifies the absolute size after adjustment. When the + or - prefix is used, it indicates that the specified capacity is increased or decreased.
-l, --extents: Adjusts the logical volume size by the number of PEs or as a percentage of the volume group space.
-r, --resizefs: Resizes the supported file system accordingly.
Warning:
Shrinking a logical volume may truncate file system data and cause unrecoverable data loss. Before doing so, back up your data, confirm that the file system supports shrinking, and ensure that the file system has been safely shrunk to a size not greater than the target logical volume capacity. The xfs file system does not support in-place shrinking.
The example command is as follows:
1. Extend logical volume lv01 to 20GiB and resize the file system accordingly:
lvresize -L 20G -r /dev/vg01/lv01
2. The following command applies only to file systems that have been confirmed safe to shrink. It shrinks logical volume lv02 to 100MiB and resizes the file system accordingly:
lvresize -L 100M -r /dev/vg01/lv02
After the adjustment is complete, run lvs and the corresponding file system check commands to verify the status of the logical volume and file system.

Deleting a Logical Volume

The lvremove command is used to delete a logical volume. After deletion, the file system and data in the logical volume are also lost.
Warning:
Deleting a logical volume permanently deletes the data in it, and this operation cannot be undone. Before doing so, back up important data and unmount the file system on the logical volume first. The -f and -y options bypass some confirmations, so do not use them without verifying the target logical volume.
The command syntax is as follows:
lvremove [options] <LV>
Common options are as follows:
-f, --force: Forces the operation and bypasses some checks.
-y, --yes: Automatically answers "yes" to confirmation prompts.
The example command is as follows:
1. Unmount the file system on logical volume lv01, and then delete the logical volume:
umount /dev/vg01/lv01
lvremove /dev/vg01/lv01
Output example:
Logical volume "lv01" successfully removed.
2. After confirming that the target is correct, force delete the logical volume lv02:
lvremove -f /dev/vg01/lv02
After the deletion is complete, run lvs to confirm that the target logical volume is no longer in the logical volume list.

File System Management

After a logical volume is created, you can use it as a regular block device to create a file system and mount it. The following example uses the logical volume /dev/vg01/lv01 and the ext4 file system.
Warning:
Creating a file system erases all existing data on the target logical volume, and the data cannot be recovered. Before proceeding, confirm that the target logical volume is correct and that you have backed up the data you want to keep.
1. Confirm that the logical volume exists and is in a normal state:
lvs /dev/vg01/lv01
Output example:
LV VG Attr LSize
lv01 vg01 -wi-a----- 10.00g
2. Create an ext4 file system on the logical volume:
mkfs.ext4 /dev/vg01/lv01
Output example (excerpt):
Creating filesystem with 2621440 4k blocks and 655360 inodes
Allocating group tables: done
Writing inode tables: done
Creating journal: done
Writing superblocks and filesystem accounting information: done
3. Create a mount point (no output is returned on success):
mkdir -p /mnt/myfs
4. Temporarily mount the logical volume to /mnt/myfs (no output is returned on success):
mount /dev/vg01/lv01 /mnt/myfs
5. Verify that the file system has been mounted successfully:
findmnt /mnt/myfs
Output example:
TARGET SOURCE FSTYPE OPTIONS
/mnt/myfs /dev/mapper/vg01-lv01 ext4 rw,relatime
The preceding mount command creates a temporary mount, which becomes invalid after a system reboot. To make the mount permanent, first run blkid /dev/vg01/lv01 to obtain the file system UUID. Then add the following configuration to /etc/fstab and replace the sample UUID with the actual value:
UUID=<actual UUID> /mnt/myfs ext4 defaults 0 2
After saving, run the following command to verify the /etc/fstab configuration. No output is returned on success:
mount -a
To unmount the file system, first confirm that no process is using the mount point, and then run:
umount /mnt/myfs
After unmounting, you can run findmnt /mnt/myfs to verify. No output indicates that the mount point has been unmounted.

Help and Support

Was this page helpful?

Help us improve! Rate your documentation experience in 5 mins.

Feedback