4.3. Volume Group Administration
4.3. Volume Group Administration This section describes the commands that perform the various aspects of volume group administration. 4.3.1. Creating Volume Groups To create a volume group from one or more physical volumes, use the vgcreate command. The vgcreate command creates a new volume group by name and adds at least one physical volume to it. The following command creates a volume group named vg1 that contains physical volumes /dev/sdd1 and /dev/sde1 . When physical volumes are used to create a volume group, its disk space is divided into 4MB extents, by default. This extent is the minimum amount by which the logical volume may be increased or decreased in size. Large numbers of extents will have no impact on I/O performance of the logical volume. You can specify the extent size with the -s option to the vgcreate command if the default extent size is not suitable. You can put limits on the number of physical or logical volumes the volume group can have by using the -p and -l arguments of the vgcreate command. By default, a volume group allocates physical extents according to common-sense rules such as not placing parallel stripes on the same physical volume. This is the normal allocation policy. You can use the --alloc argument of the vgcreate command to specify an allocation policy of contiguous , anywhere , or cling . In general, allocation policies other than normal are required only in special cases where you need to specify unusual or nonstandard extent allocation. For further information on how LVM allocates physical extents, see Section 4.3.2, "LVM Allocation" . LVM volume groups and underlying logical volumes are included in the device special file directory tree in the /dev directory with the following layout: For example, if you create two volume groups myvg1 and myvg2 , each with three logical volumes named lv01 , lv02 , and lv03 , this creates six device special files: The device special files are not present if the corresponding logical volume is not currently active. The maximum device size with LVM is 8 Exabytes on 64-bit CPUs. 4.3.2. LVM Allocation When an LVM operation needs to allocate physical extents for one or more logical volumes, the allocation proceeds as follows: The complete set of unallocated physical extents in the volume group is generated for consideration. If you supply any ranges of physical extents at the end of the command line, only unallocated physical extents within those ranges on the specified physical volumes are considered. Each allocation policy is tried in turn, starting with the strictest policy ( contiguous ) and ending with the allocation policy specified using the --alloc option or set as the default for the particular logical volume or volume group. For each policy, working from the lowest-numbered logical extent of the empty logical volume space that needs to be filled, as much space as possible is allocated, according to the restrictions imposed by the allocation policy. If more space is needed, LVM moves on to the policy. The allocation policy restrictions are as follows: An allocation policy of contiguous requires that the physical location of any logical extent that is not the first logical extent of a logical volume is adjacent to the physical location of the logical extent immediately preceding it. When a logical volume is striped or mirrored, the contiguous allocation restriction is applied independently to each stripe or mirror image (leg) that needs space. An allocation policy of cling requires that the physical volume used for any logical extent be added to an existing logical volume that is already in use by at least one logical extent earlier in that logical volume. If the configuration parameter allocation/cling_tag_list is defined, then two physical volumes are considered to match if any of the listed tags is present on both physical volumes. This allows groups of physical volumes with similar properties (such as their physical location) to be tagged and treated as equivalent for allocation purposes. For more information on using the cling policy in conjunction with LVM tags to specify which additional physical volumes to use when extending an LVM volume, see Section 4.4.19, "Extending a Logical Volume with the cling Allocation Policy" . When a Logical Volume is striped or mirrored, the cling allocation restriction is applied independently to each stripe or mirror image (leg) that needs space. An allocation policy of normal will not choose a physical extent that shares the same physical volume as a logical extent already allocated to a parallel logical volume (that is, a different stripe or mirror image/leg) at the same offset within that parallel logical volume. When allocating a mirror log at the same time as logical volumes to hold the mirror data, an allocation policy of normal will first try to select different physical volumes for the log and the data. If that is not possible and the allocation/mirror_logs_require_separate_pvs configuration parameter is set to 0, it will then allow the log to share physical volume(s) with part of the data. Similarly, when allocating thin pool metadata, an allocation policy of normal will follow the same considerations as for allocation of a mirror log, based on the value of the allocation/thin_pool_metadata_require_separate_pvs configuration parameter. If there are sufficient free extents to satisfy an allocation request but a normal allocation policy would not use them, the anywhere allocation policy will, even if that reduces performance by placing two stripes on the same physical volume. The allocation policies can be changed using the vgchange command. Note If you rely upon any layout behavior beyond that documented in this section according to the defined allocation policies, you should note that this might change in future versions of the code. For example, if you supply on the command line two empty physical volumes that have an identical number of free physical extents available for allocation, LVM currently considers using each of them in the order they are listed; there is no guarantee that future releases will maintain that property. If it is important to obtain a specific layout for a particular Logical Volume, then you should build it up through a sequence of lvcreate and lvconvert steps such that the allocation policies applied to each step leave LVM no discretion over the layout. To view the way the allocation process currently works in any specific case, you can read the debug logging output, for example by adding the -vvvv option to a command. 4.3.3. Creating Volume Groups in a Cluster You create CLVM volume groups in a cluster environment with the vgcreate command, just as you create them on a single node. Note In Red Hat Enterprise Linux 7, clusters are managed through Pacemaker. Clustered LVM logical volumes are supported only in conjunction with Pacemaker clusters, and must be configured as cluster resources. For general information on configuring LVM volumes in a cluster, see Section 1.4, "LVM Logical Volumes in a Red Hat High Availability Cluster" . Volume groups that are shared by members of the cluster should be created with the clustered attribute set with the vgcreate -cy or vgchange -cy command. The clustered attribute is set automatically if if CLVMD is running. This clustered attribute signals that this volume group should be managed and protected by CLVMD. When creating any volume group that is not shared by the cluster and should only be visible to a single host, this clustered attribute should be disabled with the vgcreate -cn or vgchange -cn command. By default, volume groups created with with the clustered attribute on shared storage are visible to all computers that have access to the shared storage. It is possible, however, to create volume groups that are local, visible only to one node in the cluster, by using the -cn option of the vgcreate command. The following command, when executed in a cluster environment, creates a volume group that is local to the node from which the command was executed. The command creates a local volume named vg1 that contains physical volumes /dev/sdd1 and /dev/sde1 . You can change whether an existing volume group is local or clustered with the -c option of the vgchange command, which is described in Section 4.3.9, "Changing the Parameters of a Volume Group" . You can check whether an existing volume group is a clustered volume group with the vgs command, which displays the c attribute if the volume is clustered. The following command displays the attributes of the volume groups VolGroup00 and testvg1 . In this example, VolGroup00 is not clustered, while testvg1 is clustered, as indicated by the c attribute under the Attr heading. For more information on the vgs command, see Section 4.3.5, "Displaying Volume Groups" Section 4.8, "Customized Reporting for LVM" , and the vgs man page. 4.3.4. Adding Physical Volumes to a Volume Group To add additional physical volumes to an existing volume group, use the vgextend command. The vgextend command increases a volume group's capacity by adding one or more free physical volumes. The following command adds the physical volume /dev/sdf1 to the volume group vg1 . 4.3.5. 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 the disks for volume groups and rebuilds the LVM cache file, also displays the volume groups. For information on the vgscan command, see Section 4.3.6, "Scanning Disks for Volume Groups to Build the Cache File" . 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. For information on using the vgs command to customize your output, see Section 4.8, "Customized Reporting for LVM" . 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. 4.3.6. Scanning Disks for Volume Groups to Build the Cache File The vgscan command scans all supported disk devices in the system looking for LVM physical volumes and volume groups. This builds the LVM cache file in the /etc/lvm/cache/.cache file, which maintains a listing of current LVM devices. LVM runs the vgscan command automatically at system startup and at other times during LVM operation, such as when you execute the vgcreate command or when LVM detects an inconsistency. Note You may need to run the vgscan command manually when you change your hardware configuration and add or delete a device from a node, causing new devices to be visible to the system that were not present at system bootup. This may be necessary, for example, when you add new disks to the system on a SAN or hotplug a new disk that has been labeled as a physical volume. You can define a filter in the /etc/lvm/lvm.conf file to restrict the scan to avoid specific devices. For information on using filters to control which devices are scanned, see Section 4.5, "Controlling LVM Device Scans with Filters" . The following example shows the output of the vgscan command. 4.3.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. 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 . 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. For information on recovering from LVM mirror failure, see Section 6.2, "Recovering from LVM Mirror Failure" . For information on removing lost physical volumes from a volume group, see Section 6.5, "Removing Lost Physical Volumes from a Volume Group" 4.3.8. Activating and Deactivating Volume Groups When you create a volume group it is, by default, activated. This means that the logical volumes in that group are accessible and subject to change. There are various circumstances for which you need to make a volume group inactive and thus unknown to the kernel. To deactivate or activate a volume group, use the -a ( --available ) argument of the vgchange command. The following example deactivates the volume group my_volume_group . If clustered locking is enabled, add 'e' to activate or deactivate a volume group exclusively on one node or 'l' to activate or/deactivate a volume group only on the local node. Logical volumes with single-host snapshots are always activated exclusively because they can only be used on one node at once. You can deactivate individual logical volumes with the lvchange command, as described in Section 4.4.11, "Changing the Parameters of a Logical Volume Group" , For information on activating logical volumes on individual nodes in a cluster, see Section 4.7, "Activating Logical Volumes on Individual Nodes in a Cluster" . 4.3.9. Changing the Parameters of a Volume Group The vgchange command is used to deactivate and activate volume groups, as described in Section 4.3.8, "Activating and Deactivating Volume Groups" . You can also use this command to change several volume group parameters for an existing volume group. The following command changes the maximum number of logical volumes of volume group vg00 to 128. For a description of the volume group parameters you can change with the vgchange command, see the vgchange (8) man page. 4.3.10. Removing Volume Groups To remove a volume group that contains no logical volumes, use the vgremove command. 4.3.11. Splitting a Volume Group To split the physical volumes of a volume group and create a new volume group, use the vgsplit command. Logical volumes cannot be split between volume groups. Each existing logical volume must be entirely on the physical volumes forming either the old or the new volume group. If necessary, however, you can use the pvmove command to force the split. The following example splits the new volume group smallvg from the original volume group bigvg . 4.3.12. 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. 4.3.13. Backing Up Volume Group Metadata Metadata backups and archives are automatically created on every configuration change to a volume group or logical volume unless disabled in the lvm.conf file. By default, the metadata backup is stored in the /etc/lvm/backup file and the metadata archives are stored in the /etc/lvm/archive file. You can manually back up the metadata to the /etc/lvm/backup file with the vgcfgbackup command. The vgcfgrestore command restores the metadata of a volume group from the archive to all the physical volumes in the volume groups. For an example of using the vgcfgrestore command to recover physical volume metadata, see Section 6.3, "Recovering Physical Volume Metadata" . 4.3.14. Renaming a Volume Group Use the vgrename command to rename an existing volume group. Either of the following commands renames the existing volume group vg02 to my_volume_group 4.3.15. 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: Make sure that no users are accessing files on the active volumes in the volume group, then unmount the logical volumes. 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. 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. When the system is shut down, you can unplug the disks that constitute the volume group and connect them to the new system. 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. Activate the volume group with the -a y argument of the vgchange command. Mount the file system to make it available for use. 4.3.16. Recreating a Volume Group Directory To recreate a volume group directory and logical volume special files, use the vgmknodes command. This command checks the LVM2 special files in the /dev directory that are needed for active logical volumes. It creates any special files that are missing and removes unused ones. You can incorporate the vgmknodes command into the vgscan command by specifying the mknodes argument to the vgscan command.
vgcreate vg1 /dev/sdd1 /dev/sde1 /dev/ vg / lv / /dev/myvg1/lv01 /dev/myvg1/lv02 /dev/myvg1/lv03 /dev/myvg2/lv01 /dev/myvg2/lv02 /dev/myvg2/lv03 vgcreate -c n vg1 /dev/sdd1 /dev/sde1 vgs VG #PV #LV #SN Attr VSize VFree VolGroup00 1 2 0 wz--n- 19.88G 0 testvg1 1 1 0 wz--nc 46.00G 8.00M vgextend vg1 /dev/sdf1 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 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 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 vgreduce my_volume_group /dev/hda1 vgchange -a n my_volume_group vgchange -l 128 /dev/vg00 vgremove officevg Volume group "officevg" successfully removed vgsplit bigvg smallvg /dev/ram15 Volume group "smallvg" successfully split from "bigvg" vgmerge -v databases my_vg vgrename /dev/vg02 /dev/my_volume_group vgrename vg02 my_volume_group 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]