| Logical Volume Manager AdministrationChapter 4. LVM Administration with CLI CommandsThis chapter summarizes the individual administrative tasks you can perform with the LVM Command Line Interface (CLI) commands to create and maintain logical volumes. There are several general features of all LVM CLI commands. When sizes are required in a command line argument, units can always be specified explicitly. If you do not specify a unit, then a default is assumed, usually KB or MB. LVM CLI commands do not accept fractions. When specifying units in a command line argument, LVM is case-insensitive; specifying M or m is equivalent, for example, and powers of 2 (multiples of 1024) are used. However, when specifying the --units argument in a command, lower-case indicates that units are in multiples of 1024 while upper-case indicates that units are in multiples of 1000. Where commands take volume group or logical volume names as arguments, the full path name is optional. A logical volume called lvol0 in a volume group called vg0 can be specified as vg0/lvol0 . Where a list of volume groups is required but is left empty, a list of all volume groups will be substituted. Where a list of logical volumes is required but a volume group is given, a list of all the logical volumes in that volume group will be substituted. For example, the lvdisplay vg0 command will display all the logical volumes in volume group vg0 . All LVM commands accept a -v argument, which can be entered multiple times to increase the output verbosity. For example, the following examples shows the default output of the lvcreate command. # lvcreate -L 50MB new_vg Rounding up size to full physical extent 52.00 MB Logical volume "lvol0" created The following command shows the output of the lvcreate command with the -v argument. # lvcreate -v -L 50MB new_vg Finding volume group "new_vg" Rounding up size to full physical extent 52.00 MB Archiving volume group "new_vg" metadata (seqno 4). Creating logical volume lvol0 Creating volume group backup "/etc/lvm/backup/new_vg" (seqno 5). Found volume group "new_vg" Creating new_vg-lvol0 Loading new_vg-lvol0 table Resuming new_vg-lvol0 (253:2) Clearing start of logical volume "lvol0" Creating volume group backup "/etc/lvm/backup/new_vg" (seqno 5). Logical volume "lvol0" created You could also have used the -vv , -vvv or the -vvvv argument to display increasingly more details about the command execution. The -vvvv argument provides the maximum amount of information at this time. The following example shows only the first few lines of output for the lvcreate command with the -vvvv argument specified. # lvcreate -vvvv -L 50MB new_vg #lvmcmdline.c:913 Processing: lvcreate -vvvv -L 50MB new_vg#lvmcmdline.c:916 O_DIRECT will be used#config/config.c:864 Setting global/locking_type to 1#locking/locking.c:138 File-based locking selected.#config/config.c:841 Setting global/locking_dir to /var/lock/lvm#activate/activate.c:358 Getting target version for linear#ioctl/libdm-iface.c:1569 dm version OF [16384]#ioctl/libdm-iface.c:1569 dm versions OF [16384]#activate/activate.c:358 Getting target version for striped#ioctl/libdm-iface.c:1569 dm versions OF [16384]#config/config.c:864 Setting activation/mirror_region_size to 512... You can display help for any of the LVM CLI commands with the --help argument of the command. # commandname --help
To display the man page for a command, execute the man command: # man commandname The man lvm command provides general online information about LVM. All LVM objects are referenced internally by a UUID, which is assigned when you create the object. This can be useful in a situation where you remove a physical volume called /dev/sdf which is part of a volume group and, when you plug it back in, you find that it is now /dev/sdk . LVM will still find the physical volume because it identifies the physical volume by its UUID and not its device name. For information on specifying the UUID of a physical volume when creating a physical volume, see Section 6.4, "Recovering Physical Volume Metadata". 4.2. Physical Volume AdministrationThis section describes the commands that perform the various aspects of physical volume administration. 4.2.1. Creating Physical VolumesThe following subsections describe the commands used for creating physical volumes. 4.2.1.1. Setting the Partition TypeIf you are using a whole disk device for your physical volume, the disk must have no partition table. For DOS disk partitions, the partition id should be set to 0x8e using the fdisk or cfdisk command or an equivalent. For whole disk devices only the partition table must be erased, which will effectively destroy all data on that disk. You can remove an existing partition table by zeroing the first sector with the following command: # dd if=/dev/zero of=PhysicalVolume bs=512 count=1 4.2.1.2. Initializing Physical VolumesUse the pvcreate command to initialize a block device to be used as a physical volume. Initialization is analogous to formatting a file system. The following command initializes /dev/sdd , /dev/sde , and /dev/sdf as LVM physical volumes for later use as part of LVM logical volumes. # pvcreate /dev/sdd /dev/sde /dev/sdf To initialize partitions rather than whole disks: run the pvcreate command on the partition. The following example initializes the partition /dev/hdb1 as an LVM physical volume for later use as part of an LVM logical volume. # pvcreate /dev/hdb1 4.2.1.3. Scanning for Block DevicesYou can scan for block devices that may be used as physical volumes with the lvmdiskscan command, as shown in the following example. # lvmdiskscan /dev/ram0 [ 16.00 MB] /dev/sda [ 17.15 GB] /dev/root [ 13.69 GB] /dev/ram [ 16.00 MB] /dev/sda1 [ 17.14 GB] LVM physical volume /dev/VolGroup00/LogVol01 [ 512.00 MB] /dev/ram2 [ 16.00 MB] /dev/new_vg/lvol0 [ 52.00 MB] /dev/ram3 [ 16.00 MB] /dev/pkl_new_vg/sparkie_lv [ 7.14 GB] /dev/ram4 [ 16.00 MB] /dev/ram5 [ 16.00 MB] /dev/ram6 [ 16.00 MB] /dev/ram7 [ 16.00 MB] /dev/ram8 [ 16.00 MB] /dev/ram9 [ 16.00 MB] /dev/ram10 [ 16.00 MB] /dev/ram11 [ 16.00 MB] /dev/ram12 [ 16.00 MB] /dev/ram13 [ 16.00 MB] /dev/ram14 [ 16.00 MB] /dev/ram15 [ 16.00 MB] /dev/sdb [ 17.15 GB] /dev/sdb1 [ 17.14 GB] LVM physical volume /dev/sdc [ 17.15 GB] /dev/sdc1 [ 17.14 GB] LVM physical volume /dev/sdd [ 17.15 GB] /dev/sdd1 [ 17.14 GB] LVM physical volume 7 disks 17 partitions 0 LVM physical volume whole disks 4 LVM physical volumes 4.2.2. Displaying Physical VolumesThere are three commands you can use to display properties of LVM physical volumes: pvs , pvdisplay , and pvscan . The pvs command provides physical volume information in a configurable form, displaying one line per physical volume. The pvs command provides a great deal of format control, and is useful for scripting. For information on using the pvs command to customize your output, see Section 4.8, "Customized Reporting for LVM". The pvdisplay command provides a verbose multi-line output for each physical volume. It displays physical properties (size, extents, volume group, etc.) in a fixed format. The following example shows the output of the pvdisplay command for a single physical volume. # pvdisplay --- Physical volume --- PV Name /dev/sdc1 VG Name new_vg PV Size 17.14 GB / not usable 3.40 MB Allocatable yes PE Size (KByte) 4096 Total PE 4388 Free PE 4375 Allocated PE 13 PV UUID Joqlch-yWSj-kuEn-IdwM-01S9-XO8M-mcpsVe The pvscan command scans all supported LVM block devices in the system for physical volumes. The following command shows all physical devices found: # pvscan PV /dev/sdb2 VG vg0 lvm2 [964.00 MB / 0 free] PV /dev/sdc1 VG vg0 lvm2 [964.00 MB / 428.00 MB free] PV /dev/sdc2 lvm2 [964.84 MB] Total: 3 [2.83 GB] / in use: 2 [1.88 GB] / in no VG: 1 [964.84 MB] 4.2.3. Preventing Allocation on a Physical VolumeYou can prevent allocation of physical extents on the free space of one or more physical volumes with the pvchange command. This may be necessary if there are disk errors, or if you will be removing the physical volume. The following command disallows the allocation of physical extents on /dev/sdk1 . # pvchange -x n /dev/sdk1 You can also use the -xy arguments of the pvchange command to allow allocation where it had previously been disallowed. 4.2.4. Resizing a Physical VolumeIf you need to change the size of an underlying block device for any reason, use the pvresize command to update LVM with the new size. You can execute this command while LVM is using the physical volume. 4.2.5. Removing Physical VolumesIf a device is no longer required for use by LVM, you can remove the LVM label with the pvremove command. Executing the pvremove command zeroes the LVM metadata on an empty physical volume. # pvremove /dev/ram15 Labels on physical volume "/dev/ram15" successfully wiped 4.3. Volume Group AdministrationThis 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 . # vgcreate vg1 /dev/sdd1 /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, refer to 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: /dev/vg /lv / For example, if you create two volume groups myvg1 and myvg2 , each with three logical volumes named lvo1 , lvo2 , and lvo3 , this create six device special files: /dev/myvg1/lv01/dev/myvg1/lv02/dev/myvg1/lv03/dev/myvg2/lv01/dev/myvg2/lv02/dev/myvg2/lv03 The maximum device size with LVM is 8 Exabytes on 64-bit CPUs. 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 next 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 to be added to an existing logical volume 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.14.3, "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. If you rely upon any layout behaviour 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 ClusterYou create volume groups in a cluster environment with the vgcreate command, just as you create them on a single node. By default, volume groups created with CLVM 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 -c n 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 . # vgcreate -c n vg1 /dev/sdd1 /dev/sde1 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. # 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 4.3.4. Adding Physical Volumes to a Volume GroupTo 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 . # vgextend vg1 /dev/sdf1 4.3.5. Displaying Volume GroupsThere are two commands you can use to display properties of LVM volume groups: vgs and vgdisplay . 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, etc.) in a fixed form. The following example shows the output of a 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 4.3.6. Scanning Disks for Volume Groups to Build the Cache FileThe vgscan command scans all supported disk devices in the system looking for LVM physical volumes and volume groups. This builds the LVM cache 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 a vgcreate command or when LVM detects an inconsistency. 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. The following example shows the output of a 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 4.3.7. Removing Physical Volumes from a Volume GroupTo 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/hda1VG Name myvgPV Size 1.95 GB / NOT usable 4 MB [LVM: 122 KB]PV# 1PV Status availableAllocatable yes (but full)Cur LV 1PE Size (KByte) 4096Total PE 499Free PE 0Allocated PE 499PV 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 4.3.8. Changing the Parameters of a Volume GroupThe following command changes the maximum number of logical volumes of volume group vg00 to 128. # vgchange -l 128 /dev/vg00 For a description of the volume group parameters you can change with the vgchange command, see the vgchange (8) man page. 4.3.9. Activating and Deactivating Volume GroupsWhen 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 . # vgchange -a n 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. 4.3.10. Removing Volume GroupsTo remove a volume group that contains no logical volumes, use the vgremove command. # vgremove officevg Volume group "officevg" successfully removed 4.3.11. Splitting a Volume GroupTo 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 off the new volume group smallvg from the original volume group bigvg . # vgsplit bigvg smallvg /dev/ram15 Volume group "smallvg" successfully split from "bigvg" 4.3.12. Combining Volume GroupsTo 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 4.3.13. Backing Up Volume Group MetadataMetadata backups and archives are automatically created on every volume group and logical volume configuration change 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/archives file. You can manually back up the metadata to the /etc/lvm/backup file with the vgcfgbackup command. The vgcfrestore command restores the metadata of a volume group from the archive to all the physical volumes in the volume groups. 4.3.14. Renaming a Volume GroupUse the vgrename command to rename an existing volume group. Either of the following commands renames the existing volume group vg02 to my_volume_group # vgrename /dev/vg02 /dev/my_volume_group # vgrename vg02 my_volume_group 4.3.15. Moving a Volume Group to Another SystemYou 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. 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 form 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. # 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. 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 DirectoryTo 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 removes unused ones. You can incorporate the vgmknodes command into the vgscan command by specifying the mknodes argument to the vgscan command. |
| |