LVM commands - Index of commands

mail

pvresize

Usage

Flags

Flag Usage
--devices PV
  • resize the PV physical volume
  • looks like this flag is not supported in all versions of this utility (?), in such case just specify PV without --devices
--setphysicalvolumesize newSize
  • declare the new size of the PV to resize
  • newSize can be a number with unit, like 50G
-t --test run in test mode

Example

a basic resize

  • pvresize -t /dev/sdb --setphysicalvolumesize 100G
    TEST MODE: Metadata will NOT be updated and volumes will not be (de)activated.
    Physical volume "/dev/sdb" changed
    1 physical volume(s) resized or updated / 0 physical volume(s) not resized
  • pvresize /dev/sdb --setphysicalvolumesize 100G
    Physical volume "/dev/sdb" changed
    1 physical volume(s) resized or updated / 0 physical volume(s) not resized
  • --- Physical volume ---
    PV Name			/dev/sdb
    VG Name			VolGroup
    PV Size			<100,00 GiB / not usable 3,00 MiB
    Allocatable		yes
    PE Size			4,00 MiB
    Total PE		25599
    Free PE			12800
    Allocated PE		12799
    PV UUID			0q1N12-UMre-cfj8-ZtPO-iuFb-6TwI-mTfqBW
mail

pvs

Usage

pvs : physical volume status.

Display fields (sources : 1, 2) :

PV		VG	Fmt	Attr	PSize		PFree
/dev/sda2	rhel	lvm2	a--	19,00g		0
/dev/sdb1	rhel	lvm2	a--	20,00g		0
/dev/sdc1		lvm2	---	50,00g		50,00g
/dev/sdd1		lvm2	---	50,00g		50,00g
/dev/sde1		lvm2	---	1023,00m	1023,00m
/dev/sdf1		lvm2	---	1023,00m	1023,00m
Field Meaning
PV physical volume name
VG volume group name
Fmt The metadata format of the physical volume (lvm2 or lvm1)
Attr Attributes of the physical volume (source) :
  • a :
    • this PV is allocated to a VG
    • the Internetz often mention "allocatable" instead of "allocated", which doesn't mean the same thing
    • NOT having this attribute means this PV is not used and can safely be "pvremoved"
  • x : the corresponding VG has been exported with vgexport
PSize Size of the physical volume
PFree Free space on the physical volume

Flags

Flag Usage
--all Include information in the output about devices that have not been initialized with pvcreate
--segments Produces one line of output for each contiguous allocation of space on each physical volume, showing the start (pvseg_start) and length (pvseg_size) in units of physical extents.
mail

lvcreate

Usage

Create a logical volume in an existing volume group. You can check the result of lvcreate with lvdisplay.

lvcreate [options] -n newLogicalVolume existingVolumeGroup

Flags

Flag Usage
-I stripeSizeKb --stripesize stripeSizeKb stripeSizeKb must be a power of 2, while not exceeding the physical extent size (more)
-i stripes --stripe stripes stripes is the number of physical volumes the striped logical volume will span onto
-L size --size size Size to allocate for the new logical volume. size is formatted like numberUnit
  • with Unit : K for kilobytes, M for megabytes, G for gigabytes, and so on
  • to use the whole remaining space :
    • size becomes 100%FREE
    • see this instead
-l sizeInNbOfExtents
--extents sizeInNbOfExtents
Size to allocate for the new logical volume in number of extents
To allocate : specify :
the whole size of the volume group -l 100%VG
the remaining free space of the volume group -l 100%FREE
-m nbCopies --mirrors nbCopies Create a mirrored logical volume with nbCopies copies : -m 1 would result in a mirror with two-sides; that is, a linear volume plus one copy
-n newLogicalVolumeName
--name newLogicalVolumeName
Name of the new logical volume
-s --snapshot Create a snapshot logical volume (or snapshot) for an existing, so called original logical volume (or origin). Snapshots provide a "frozen image" of the contents of the origin while the origin can still be updated. They enable consistent backups and online recovery of removed/overwritten data/files. The snapshot does not need the same amount of storage the origin has. In a typical scenario, 15-20% might be enough. In case the snapshot runs out of storage, use lvextend to grow it. Shrinking a snapshot is supported by lvreduce as well.

Setting an optimal stripe size :

It's hard to find good advice on this, most of the documentation / tutorials around just show arbitrary values of stripe size without explaining how these values were computed . Some hints :
  • You want stripes which will be narrower than most of the files you will be using (source)
  • The ideal stripe size is a multiple of the size of the blocks in the filesystem. Reads and writes work most efficiently when they can run a block at a time, rather than having to buffer up fragments. (source)
  • Bigger stripe sizes generally offer the best performance so long as your data isn't being predominantly handled at sizes smaller than the stripes (source)
  • The stripe size should be a multiple of the disk sector size (source)
  • The extent size (available with vgdisplay and others) should be a multiple of this stripe size (source)
These guidelines usually leave you with a stripe which is between 1-4x your block size.

Example

lvcreate --size 500M --snapshot --name backup_mysql_$(date +%H%M) /dev/vg/mysql
This new logical volume will be created inside the /dev/vg/mysql volume group.
lvcreate --size 1.2t --name safe myLVM
This creates the /dev/myLVM/safe new logical volume (get this with lvdisplay), which you can use like a "standard" disc (e.g. /dev/sdxn).
mail

lvextend

Usage

Extend the size of a logical volume.
When it comes to resizing file systems, resizing a logical volume and resizing a file system are 2 different things. In other words :
  • resizing a logical volume is about upgrading a container only. This can be done without unmounting filesystems since they don't change at this step.
  • resizing a file system is THE thing about storage made available to the kernel. Some filesystems can be extended online (i.e. without umounting them first), but this is not really recommended since it may corrupt the filesystem (source 1, 2, 3).
    The ideal sequence would then be :
    1. lvextend
    2. umount
    3. resize (commands are filesystem-specific)
    4. mount
  • it is always necessary to unmount a filesystem to shrink it (which requires a LiveCD / Rescue Mode when working on the root filesystem / , sources : 1, 2)
  • but it is possible to grow a logical volume and the underlying filesystem online with lvextend -r

Flags

Flag Usage
-l --extents Extend or set the logical volume size in units of logical extents.
-L --size Extend or set the logical volume size in units of megabytes. A size suffix of M for megabytes, G for gigabytes, T for terabytes, ..., is optional. With the + sign the value is added to the actual size of the logical volume and without it, the value is taken as an absolute one :
  • lvextend -L finalSizeWithUnit /dev/mapper/myLogicalVolume
  • lvextend -L +addedSizeWithUnit /dev/mapper/myLogicalVolume
-r --resizefs
  • resize underlying filesystem together with the logical volume using fsadm
  • expects the filesystem to be mounted and will complain otherwise :
    xfs_info: /tmp/fsadm_2635518772/m is not a mounted XFS filesystem
    fsadm: Cannot parse xfs_info output.
    /usr/sbin/fsadm failed: 1
-t --test Run in test mode : commands will not update metadata.
This is implemented by disabling all metadata writing but nevertheless returning success to the calling function. This may lead to unusual error messages in multi-stage operations if a tool relies on reading back metadata it believes has changed but hasn't.

Example

Extend the size of logical volume vg01/lv01 by the amount of free space on physical volume /dev/sdk3 :

lvextend /dev/vg01/lv01 /dev/sdk3
This means adding the full capacity of /dev/sdk3 to the logical volume.
This is equivalent to specifying -l +100%PVS on the command line.

Add some gigabytes to the existing logical volume /dev/mapper/myLogicalVolume so that it reaches 20G :

newSize='20G'; logicalVolume='/dev/mapper/myLogicalVolume'; umount "$logicalVolume"; lvextend -L "$newSize" "$logicalVolume"; e2fsck -f "$logicalVolume"; resize2fs "$logicalVolume" "$newSize"; mount "$logicalVolume"

mail

lvreduce

Usage

Reduce the size of a logical volume.

Flags

Flag Usage
-L sizeInMB
--size sizeInMB
Reduce or set the logical volume size in number of MB. You can add a size suffix: k, m, g or t.
lvreduce --size targetSize logicalVolume
lvreduce --size -removedSize logicalVolume
-l numberOfExtents
--extents numberOfExtents
Reduce or set the logical volume size in number of logical extents.
lvreduce --extents targetSize logicalVolume
lvreduce --extents -removedSize logicalVolume
numberOfExtents can also be expressed with a suffix :
  • %VG : percentage of the total space in the volume group
  • %LV : relative to the existing size of the logical volume
  • %FREE : percentage of the remaining free space in the volume group
-r --resizefs resize underlying filesystem together with the logical volume
mail

vgs

Usage

vgs : volume group status

Flags

Flag Usage
(none) --all List all volume groups.
--noheadings Suppress the headings line that is normally the first line of output. Useful if grepping the output.
--units hHbBsSkKmMgGtTpPeE Output sizes in these units: human-readable, bytes, sectors, kilobytes, megabytes, gigabytes, terabytes, petabytes, exabytes.
Lowercase units are powers of 1024.
Uppercase units are powers of 1000.
mail

pvcreate

Usage

Initialize a disk or partition for use by LVM :

pvcreate [options] physicalVolume

physicalVolume can be :

For whole disk devices, the partition table must be erased, which will effectively destroy all data on that disk. This can be done by zeroing the first sector with :

dd if=/dev/zero of=physicalVolume bs=512 count=1

mail

vgdisplay

Usage

Display attributes of volume groups :

vgdisplay [options] volumeGroupName

Flags

Flag Usage
-s --short short display
-v --verbose Display verbose information containing long listings of physical and logical volumes. If given twice, also display verbose runtime information of vgdisplay's activities.

Example

How to get the size of the volume group volumeGroupName ?

  • Get the size in gigabytes with : echo "$(vgdisplay -c volumeGroupName | cut -d':' -f12)/1024/1024" | bc -l
  • OR : vgdisplay volumeGroupName
  • OR : vgs volumeGroupName

How to list the logical volumes of the volume group volumeGroupName ?

lvs volumeGroupName
mail

pvdisplay

Usage

Display attributes of a physical volume

Flags

Flag Usage
-m --maps Display the mapping of physical extents to logical volumes and logical extents.
-s --short Only display the size of the given physical volumes.
mail

lvdisplay

Usage

Display attributes of a logical volume

Flags

Flag Usage
-m --maps Display the mapping of logical extents to physical volumes and physical extents.
To map physical extents to logical extents use: pvs --segments -o+lv_name,seg_start_pe,segtype
-v --verbose (explicit)
mail

lvmdiskscan

Usage

Scan for all devices visible to LVM2.

lvmdiskscan is /sbin/lvmdiskscan, so root or sudo only.

Example

Listing storage devices :

lvmdiskscan
/dev/caramba-vg/root	[	  20.00 GiB]
/dev/sda1		[	 243.00 MiB]
/dev/caramba-vg/var	[	  12.00 GiB]
/dev/caramba-vg/swap_1	[	   3.80 GiB]
/dev/sda3		[	  40.00 GiB] LVM physical volume
/dev/caramba-vg/tmp	[	 380.00 MiB]
/dev/caramba-vg/home	[	  24.41 GiB]
/dev/sda5		[	  39.76 GiB] LVM physical volume
4 disks
2 partitions
0 LVM physical volume whole disks
2 LVM physical volumes
lsblk
NAME                   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda                      8:0    0   80G  0 disk
├─sda1                   8:1    0  243M  0 part /boot
├─sda2                   8:2    0    1K  0 part
├─sda3                   8:3    0   40G  0 part
│ ├─caramba--vg-root   254:0    0   20G  0 lvm  /
│ └─caramba--vg-var    254:1    0   12G  0 lvm  /var
└─sda5                   8:5    0 39.8G  0 part
  ├─caramba--vg-root   254:0    0   20G  0 lvm  /
  ├─caramba--vg-var    254:1    0   12G  0 lvm  /var
  ├─caramba--vg-swap_1 254:2    0  3.8G  0 lvm  [SWAP]
  ├─caramba--vg-tmp    254:3    0  380M  0 lvm  /tmp
  └─caramba--vg-home   254:4    0 24.4G  0 lvm  /home
sr0                     11:0    1 1024M  0 rom
mail

lvs

Usage

lvs : logical volume status.

Flags

Flag Usage
--noheadings Suppress the headings line that is normally the first line of output. Useful if grep-ping the output.
-o options
--options options
output format : comma-separated ordered list of columns.
To list available fields : lvs -o help
example : lvs -o +layout,devices
--segments show segment information
mail

vgexport

Make volume groups unknown to the system, which prevents activity on them.
mail

pvremove

Usage

Wipe the label on a device so that LVM will no longer recognize it as a physical volume
mail

vgcreate

Create a volume group :

vgcreate volumeGroupName physicalVolume [physicalVolume2]

mail

vgremove

Remove one or more volume groups :

vgremove volumeGroupToRemove

mail

lvremove

Usage

Remove one or more logical volumes

Flags

Flag Usage
-f --force Do not prompt for confirmation before removing a logical volume

Example

Remove a single logical volume :
lvremove volumeGroup/logicalVolumeToRemove
Remove all logical volumes of the specified volume group :
lvremove volumeGroup
mail

vgreduce

Usage

Remove a physical volume from a volume group :

vgreduce volumeGroupName physicalVolume

mail

vgextend

Usage

Add a physical volume to a volume group :

vgextend volumeGroupName physicalVolume

Flags

Flag Usage
-t --test run in test mode