Chapter 6. Managing LVM volume groups

This section describes the commands that perform the various aspects of volume group administration.

6.1. Volume groups

Physical volumes are combined into volume groups (VGs). This creates a pool of disk space out of which logical volumes can be allocated.

Within a volume group, the disk space available for allocation is divided into units of a fixed-size called extents. An extent is the smallest unit of space that can be allocated. Within a physical volume, extents are referred to as physical extents.

A logical volume is allocated into logical extents of the same size as the physical extents. The extent size is thus the same for all logical volumes in the volume group. The volume group maps the logical extents to physical extents.

6.2. Displaying volume groups

There are two commands you can use to display properties of LVM volume groups: vgs and vgdisplay. The vgscan command, which scans all supported LVM block devices in the system for volume groups, can also be used to display the existing volume groups.

The vgs command provides volume group information in a configurable form, displaying one line per volume group. The vgs command provides a great deal of format control, and is useful for scripting.

The vgdisplay command displays volume group properties (such as size, extents, number of physical volumes, and so on) in a fixed form. The following example shows the output of the vgdisplay command for the volume group new_vg. If you do not specify a volume group, all existing volume groups are displayed.

# vgdisplay new_vg
  --- Volume group ---
  VG Name               new_vg
  System ID
  Format                lvm2
  Metadata Areas        3
  Metadata Sequence No  11
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1
  Open LV               0
  Max PV                0
  Cur PV                3
  Act PV                3
  VG Size               51.42 GB
  PE Size               4.00 MB
  Total PE              13164
  Alloc PE / Size       13 / 52.00 MB
  Free  PE / Size       13151 / 51.37 GB
  VG UUID               jxQJ0a-ZKk0-OpMO-0118-nlwO-wwqd-fD5D32

The following example shows the output of the vgscan command.

# vgscan
Reading all physical volumes.  This may take a while...
Found volume group "new_vg" using metadata type lvm2
Found volume group "officevg" using metadata type lvm2

6.3. Combining volume groups

To combine two volume groups into a single volume group, use the vgmerge command. You can merge an inactive "source" volume with an active or an inactive "destination" volume if the physical extent sizes of the volume are equal and the physical and logical volume summaries of both volume groups fit into the destination volume groups limits.

The following command merges the inactive volume group my_vg into the active or inactive volume group databases giving verbose runtime information.

# vgmerge -v databases my_vg

6.4. Splitting a volume group

In this example procedure, an existing volume group consists of three physical volumes. If there is enough unused space on the physical volumes, a new volume group can be created without adding new disks.

In the initial set up, the logical volume mylv is carved from the volume group myvg, which in turn consists of the three physical volumes, /dev/sda1, /dev/sdb1, and /dev/sdc1.

After completing this procedure, the volume group myvg will consist of /dev/sda1 and /dev/sdb1. A second volume group, yourvg, will consist of /dev/sdc1.

  1. Use the pvscan command to determine how much free space is currently available in the volume group.

    # pvscan
      PV /dev/sda1  VG myvg   lvm2 [17.15 GB / 0    free]
      PV /dev/sdb1  VG myvg   lvm2 [17.15 GB / 12.15 GB free]
      PV /dev/sdc1  VG myvg   lvm2 [17.15 GB / 15.80 GB free]
      Total: 3 [51.45 GB] / in use: 3 [51.45 GB] / in no VG: 0 [0   ]
  2. Move all the used physical extents in /dev/sdc1 to /dev/sdb1 with the pvmove command. The pvmove command can take a long time to execute.

    In a cluster, the pvmove command can move only logical volume that are active exclusively on a single node.

    # pvmove /dev/sdc1 /dev/sdb1
      /dev/sdc1: Moved: 14.7%
      /dev/sdc1: Moved: 30.3%
      /dev/sdc1: Moved: 45.7%
      /dev/sdc1: Moved: 61.0%
      /dev/sdc1: Moved: 76.6%
      /dev/sdc1: Moved: 92.2%
      /dev/sdc1: Moved: 100.0%

    After moving the data, you can see that all of the space on /dev/sdc1 is free.

    # pvscan
      PV /dev/sda1   VG myvg   lvm2 [17.15 GB / 0    free]
      PV /dev/sdb1   VG myvg   lvm2 [17.15 GB / 10.80 GB free]
      PV /dev/sdc1   VG myvg   lvm2 [17.15 GB / 17.15 GB free]
      Total: 3 [51.45 GB] / in use: 3 [51.45 GB] / in no VG: 0 [0   ]
  3. To create the new volume group yourvg, use the vgsplit command to split the volume group myvg.

    The following command splits the volume group yourvg from the volume group myvg, moving the physical volume /dev/sdc1 into the new volume group yourvg.

    # lvchange -a n /dev/myvg/mylv
    # vgsplit myvg yourvg /dev/sdc1
      Volume group "yourvg" successfully split from "myvg"

    You can use the vgs command to see the attributes of the two volume groups.

    # vgs
      VG     #PV #LV #SN Attr   VSize  VFree
      myvg     2   1   0 wz--n- 34.30G 10.80G
      yourvg   1   0   0 wz--n- 17.15G 17.15G
  4. After creating the new volume group, create the new logical volume yourlv.

    # lvcreate -L 5G -n yourlv yourvg
      Logical volume "yourlv" created
  5. Create a file system on the new logical volume and mount it.

    # mkfs.ext4 /dev/yourvg/yourlv
    mke2fs 1.44.3 (10-July-2018)
    Creating filesystem with 524288 4k blocks and 131072 inodes
    Filesystem UUID: 616da032-8a48-4cd7-8705-bd94b7a1c8c4
    Superblock backups stored on blocks:
            32768, 98304, 163840, 229376, 294912
    
    Allocating group tables: done
    Writing inode tables: done
    Creating journal (16384 blocks): done
    Writing superblocks and filesystem accounting information: done
    
    # mount /dev/yourvg/yourlv /mnt

6.5. Renaming LVM volume groups

This procedure renames an existing volume group using the command-line LVM interface.

Procedure

  1. If the volume group exists in a clustered environment, deactivate the volume group on all nodes where it is active. Use the following command on each such node:

    [root@node-n]# vgchange --activate n vg-name
  2. Use the vgrename utility to rename an existing volume group:

    # vgrename original-vg-name new-vg-name

    Optionally, you can specify the full paths to the devices:

    # vgrename /dev/original-vg-name  /dev/new-vg-name

Additional resources

  • The vgrename(8) man page

6.6. Moving a volume group to another system

You can move an entire LVM volume group to another system. It is recommended that you use the vgexport and vgimport commands when you do this.

Note

You can use the --force argument of the vgimport command. This allows you to import volume groups that are missing physical volumes and subsequently run the vgreduce --removemissing command.

The vgexport command makes an inactive volume group inaccessible to the system, which allows you to detach its physical volumes. The vgimport command makes a volume group accessible to a machine again after the vgexport command has made it inactive.

To move a volume group from one system to another, perform the following steps:

  1. Make sure that no users are accessing files on the active volumes in the volume group, then unmount the logical volumes.
  2. Use the -a n argument of the vgchange command to mark the volume group as inactive, which prevents any further activity on the volume group.
  3. Use the vgexport command to export the volume group. This prevents it from being accessed by the system from which you are removing it.

    After you export the volume group, the physical volume will show up as being in an exported volume group when you execute the pvscan command, as in the following example.

    # pvscan
      PV /dev/sda1    is in exported VG myvg [17.15 GB / 7.15 GB free]
      PV /dev/sdc1    is in exported VG myvg [17.15 GB / 15.15 GB free]
      PV /dev/sdd1   is in exported VG myvg [17.15 GB / 15.15 GB free]
      ...

    When the system is next shut down, you can unplug the disks that constitute the volume group and connect them to the new system.

  4. When the disks are plugged into the new system, use the vgimport command to import the volume group, making it accessible to the new system.
  5. Activate the volume group with the -a y argument of the vgchange command.
  6. Mount the file system to make it available for use.

6.7. Removing physical volumes from a volume group

To remove unused physical volumes from a volume group, use the vgreduce command. The vgreduce command shrinks a volume group’s capacity by removing one or more empty physical volumes. This frees those physical volumes to be used in different volume groups or to be removed from the system.

Before removing a physical volume from a volume group, you can make sure that the physical volume is not used by any logical volumes by using the pvdisplay command.

# pvdisplay /dev/hda1

-- Physical volume ---
PV Name               /dev/hda1
VG Name               myvg
PV Size               1.95 GB / NOT usable 4 MB [LVM: 122 KB]
PV#                   1
PV Status             available
Allocatable           yes (but full)
Cur LV                1
PE Size (KByte)       4096
Total PE              499
Free PE               0
Allocated PE          499
PV UUID               Sd44tK-9IRw-SrMC-MOkn-76iP-iftz-OVSen7

If the physical volume is still being used you will have to migrate the data to another physical volume using the pvmove command. Then use the vgreduce command to remove the physical volume.

The following command removes the physical volume /dev/hda1 from the volume group my_volume_group.

# vgreduce my_volume_group /dev/hda1

If a logical volume contains a physical volume that fails, you cannot use that logical volume. To remove missing physical volumes from a volume group, you can use the --removemissing parameter of the vgreduce command, if there are no logical volumes that are allocated on the missing physical volumes.

If the physical volume that fails contains a mirror image of a logical volume of a mirror segment type, you can remove that image from the mirror with the vgreduce --removemissing --mirrorsonly --force command. This removes only the logical volumes that are mirror images from the physical volume.

6.8. Removing LVM volume groups

This procedure removes an existing volume group using the command-line LVM interface.

Prerequisites

Procedure

  1. If the volume group exists in a clustered environment, stop the lockspace of the volume group on all other nodes. Use the following command on all nodes except the node where you are performing the removing:

    [root@node-n]# vgchange --lockstop vg-name

    Wait for the lock to stop.

  2. To remove the volume group, use the vgremove utility:

    # vgremove vg-name
      Volume group "vg-name" successfully removed

Additional resources

  • The vgremove(8) man page

6.9. Additional resources

  • The vgchange(8) man page