LVM: Difference between revisions
Breaking out filesystem expansions |
No edit summary |
||
| Line 210: | Line 210: | ||
}} | }} | ||
==== XFS ==== | ====XFS==== | ||
For XFS filesystems, after expanding the volume, resize the filesystem with <code>xfs_growfs</code>: | For XFS filesystems, after expanding the volume, resize the filesystem with <code>xfs_growfs</code>: | ||
{{Highlight | {{Highlight | ||
| Line 238: | Line 238: | ||
}} | }} | ||
==== EXT2, EXT3, EXT4 ==== | ====EXT2, EXT3, EXT4==== | ||
For the EXT type filesystems, use <code>resize2fs</code> to expand the filesystem. | For the EXT type filesystems, use <code>resize2fs</code> to expand the filesystem. | ||
{{Highlight | {{Highlight | ||
| code = ## If using ext4, use resize2fs | | code = ## If using ext4, use resize2fs | ||
# resize2fs /dev/mapper/vg-lv | # resize2fs /dev/mapper/vg-lv | ||
| lang = terminal | |||
}} | |||
== Troubleshooting == | |||
=== LVM warns with "open failed: No medium found" === | |||
When running any LVM related commands, you may get the following warning messages complaining about "No medium found": | |||
{{Highlight | |||
| code = # lvs | |||
/dev/sdb: open failed: No medium found | |||
/dev/sdc: open failed: No medium found | |||
/dev/sdb: open failed: No medium found | |||
/dev/sdc: open failed: No medium found | |||
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert | |||
ondemand VG_OOD -wi-ao---- 25.00g | |||
root VG_OOD -wi-ao---- 10.00g | |||
swap VG_OOD -wi-ao---- 4.00g | |||
var VG_OOD -wi-ao---- 50.00g | |||
| lang = terminal | |||
}} | |||
This is most likely because your system has removable devices such as a SD card reader or a Dell iDRAC virtual device and is presenting a block device to the system. You can verify this by running <code>ls /dev/sd*</code> or <code>ls /dev/disk/by-id/*</code> and checking if the device is listed. In the case above, <code>/dev/sdb</code> and <code>/dev/sdc</code> were virtual iDRAC devices. | |||
{{Highlight | |||
| code = # ls -al /dev/disk/by-id/ | |||
... | |||
lrwxrwxrwx 1 root root 9 Jun 1 12:00 usb-iDRAC_LCDRIVE_20120430-0:0 -> ../../sdb | |||
lrwxrwxrwx 1 root root 9 Jun 1 12:00 usb-iDRAC_Virtual_CD_20120430-0:0 -> ../../sr1 | |||
lrwxrwxrwx 1 root root 9 Jun 1 12:00 usb-iDRAC_Virtual_Floppy_20120430-0:1 -> ../../sdc | |||
... | |||
| lang = terminal | |||
}} | |||
As a workaround, you may configure LVM commands from scanning these devices by adding a filter: | |||
{{Highlight | |||
| code = filter = [ "r{{!}}/dev/disk/by-id/usb-iDRAC.*{{!}}" ] | |||
| lang = terminal | | lang = terminal | ||
}} | }} | ||
Revision as of 22:07, 1 June 2021
The Logical Volume Manager or LVM is a volume manager available on Linux. Functionally, volumes are similar to traditional partitions but with the following benefits:
- Resizable volumes
- Snapshots, A point-in-time copy of data useful for making consistent backups
- Ability for a volume to span multiple disks
Cheat Sheet
| Action | Command |
|---|---|
| Show physical volume groups (Eg. disks) | pvs
|
| Show volume groups | vgs
|
| Create a volume group (with name and backing device) | vgcreate vg_name /dev/disk
|
| Deactivate a volume group | vgchange -a n vg_name
|
| Remove a volume group | vgremove vg_name
|
| Show logical volumes | lvs
|
| Create a logical volume
% of free space in vg with |
lvcreate -n lv_name -l 50%FREE vg_name
|
| Create a snapshot capable of holding 10G of deltas | lvcreate -s -n snap_lv_name --size 10G /dev/vg_name/lv_name
|
Resize a logical volume, +Size to increase, -Size to decrease.
|
lvresize -L +10G /dev/vg_name/lv_name
|
| Remove a logical volume | lvremove lv_name
|
Not strictly LVM, but after resizing a volume, you will need to resize the filesystem with these commands:
| Action | Command |
|---|---|
| XFS Filesystem | xfs_growfs /
|
| EXT2/3/4 | resize2fs /dev/vg_name/lv_name
|
Using LVM
LVM allows the creation of volumes which functionally is similar to partitions. This is done by allocating storage into a volume group (VG) which can then be broken up into separate logical volumes (LV).
Unlike a traditional partition scheme, LVM breaks down the backing storage (called physical volumes) into many small chunks called physical extents (PE) which are typically 4MB in size. These physical extents are assigned to their respective logical volumes (referred to as Logical Extents) and can be added or removed after creation allowing for volumes to grow or shrink as needed. One of the added benefits with this design allows for the ability to implement copy-on-write (COW) allowing for quick and space-efficient snapshotting.
Creating a volume group and logical volume
To quickly get a filesystem up and running using LVM, we will need to create a volume group and then a logical volume within the volume group.
# vgcreate ug_vg /dev/sdb
Volume group "ug_vg " successfully created
# vgdisplay ug_vg
--- Volume group ---
VG Name ug_vg
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 1
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 0
Open LV 0
Max PV 0
Cur PV 1
Act PV 1
VG Size 836.62 GiB
PE Size 4.00 MiB
Total PE 214175
Alloc PE / Size 0 / 0
Free PE / Size 214175 / 836.62 GiB
VG UUID WeggQU-zQf3-j3mk-OAFd-iUCQ-EDTo-ezxhcb
# lvcreate -n ug -l 90%FREE ug_vg
Logical volume "ug" created.
## After creating a LV, the available free PEs decrease as expected
# vgdisplay ug_vg
...
VG Size 836.62 GiB
PE Size 4.00 MiB
Total PE 214175
Alloc PE / Size 192757 / 752.96 GiB
Free PE / Size 21418 / 83.66 GiB
# LVM volumes are placed under /dev with the VG name and LV name.
# mkfs.ext4 /dev/ug_vg/ug
## If a VG needs to be renamed, use the vgrename command:
# vgrename uga ug_vg
Mounting and unmounting a volume
LVM volumes are exposed as a block device. You will need to format a filesystem of your choice and then mount it like any other device.
Continuing with the example above, we can mount /dev/ug_vg/ug as an ext4 filesystem with the usual mount command:
# mount /dev/ug_vg/ug /mnt/ug
# umount /mnt/ug
Similarly, you can add the filesystem to the /etc/fstab file so it is mounted automatically on start up.
Using snapshots
A snapshot creates a point-in-time copy of a volume. Unlike ZFS, a LVM snapshot creates a separate volume which appears as a separate block device which needs to be mounted separately. There is no need to worry about creating an unclean filesystem with the snapshot because LVM will make the necessary system calls and work so that the filesystem is synced and checkpointed so that the snapshot copy is consistent and clean.
Creating, listing, and deleting snapshots
To create a new snapshot of an existing volume, use the lvcreate command with the -s or --snapshot option. You will need to specify the size of the snapshot. More on this later. Once created, the snapshot will appear in lvs with a s attribute and can be removed with the lvremove command.
# lvcreate --size 1G -s -n logicalvol_snap /dev/mapper/volgroup-logicalvol
Logical volume "logicalvol_snap" created.
# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
logicalvol volgroup owi-aos--- 220.00g
logicalvol_snap volgroup swi-a-s--- 1.00g logicalvol 29.54
# lvremove -f /dev/volgroup/logicalvol_snap
Logical volume "metrics_snap" successfully removed
Snapshot volume size
A new snapshot takes nearly no additional space. Any changes that are made to the origin volume or the snapshotted volume will need to be tracked in order for the snapshot to function. This is done transparently through the copy-on-write feature of LVM, but unlike ZFS, the size requirement of this copy-on-write is deducted from the snapshot volume rather than the whole storage pool as a whole. In other words, as the source volume or snapshot volume changes, the size requirement of the snapshot volume will grow in tandem to a maximum of the source volume size.
On a busy filesystem or for snapshots that you intend to keep around for a long time, the snapshot size should match the source volume size. The snapshot will become corrupt when the snapshot volume runs out of space.
If you have a snapshot that is about to run out of space, increase the snapshot volume size with lvextend:
# lvextend -L +10G /dev/volgroup/logicalvol_snap
You may also configure LVM to automatically extend snapshot volumes by editing /etc/lvm/lvm.conf and setting the snapshot_autoextend_threshold value.
Tasks
Recovering lost volumes
# vgscan # Generates /etc/lvm/backup/VolGroup01
# cd /etc/lvm/backup
# vgcfgrestore -f VolGroup01 VolGroup01 # Restore VG using /etc/lvm/backup/VolGroup01
# vgscan # verifies volume groups
# pvscan # Looks for physical volumes
# vgchange VolGroup01 -a y # Activates the volume group
# lvscan # Looks for logical volumes
# mount /dev/VG01/LV00 /mnt/foo # LVM volumes should be in /dev/vg/lv and should be mountable
Use vgscan to rescan for volume groups. This will create a file of the same name as the volume in /etc/lvm/backup/. To restore a volume, use the vgcfgrestore command. With the volume groups restored, use pvscan to look for physical volumes. Activate the volume using vgchange VolGroup01 -a y
With the volume activated, look for logical volumes using lvscan. Logical volumes should show up in /dev/VolGroup01/LogVol00 for example and can be mounted.
See also:
Expanding an existing volume
Here's a quick guide on expanding a virtual disk that is using LVM.
- Expand the virtual disk
- The OS might not be aware of the disk size change until a reboot. Reboot if the size is still incorrect.
- Resize the partition that LVM is using using
fdisk.- Delete the partition
- Recreate the partition with the same starting location but with a larger size
- Run
partprobeto update the partition size
- Resize the LVM physical volume with
pvresize /dev/sda1 pvscanshould show the LVM having free space- Resize the LV. Eg: Add 1GB with
lvresize -L +1G /dev/centos/root - Resize the filesystem.
An example from a virtual machine where the disk was expanded from 20GB to 40GB.
## Expand the partition <code>/dev/sda2</code> using <code>fdisk</code>. Then do the following.
# pvresize /dev/sda2
Physical volume "/dev/sda2" changed
1 physical volume(s) resized or updated / 0 physical volume(s) not resized
## Verify
# pvscan
PV /dev/sda2 VG centos lvm2 [<39.00 GiB / 20.00 GiB free]
Total: 1 [<39.00 GiB] / in use: 1 [<39.00 GiB] / in no VG: 0 [0 ]
## Resize the LVs as required
# lvresize -L +1G /dev/centos/root
Size of logical volume centos/root changed from <17.00 GiB (4351 extents) to <18.00 GiB (4607 extents).
Logical volume centos/root successfully resized.
# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
root centos -wi-ao---- <18.00g
swap centos -wi-ao---- 2.00g
XFS
For XFS filesystems, after expanding the volume, resize the filesystem with xfs_growfs:
## Expand your filesystems. If using XFS:
# xfs_growfs /
meta-data=/dev/mapper/centos-root isize=512 agcount=4, agsize=1113856 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0 spinodes=0
data = bsize=4096 blocks=4455424, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal bsize=4096 blocks=2560, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
data blocks changed from 4455424 to 4717568
# xfs_info /
meta-data=/dev/mapper/centos-root isize=512 agcount=5, agsize=1113856 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0 spinodes=0
data = bsize=4096 blocks=4717568, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal bsize=4096 blocks=2560, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
EXT2, EXT3, EXT4
For the EXT type filesystems, use resize2fs to expand the filesystem.
## If using ext4, use resize2fs
# resize2fs /dev/mapper/vg-lv
Troubleshooting
LVM warns with "open failed: No medium found"
When running any LVM related commands, you may get the following warning messages complaining about "No medium found":
# lvs
/dev/sdb: open failed: No medium found
/dev/sdc: open failed: No medium found
/dev/sdb: open failed: No medium found
/dev/sdc: open failed: No medium found
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
ondemand VG_OOD -wi-ao---- 25.00g
root VG_OOD -wi-ao---- 10.00g
swap VG_OOD -wi-ao---- 4.00g
var VG_OOD -wi-ao---- 50.00g
This is most likely because your system has removable devices such as a SD card reader or a Dell iDRAC virtual device and is presenting a block device to the system. You can verify this by running ls /dev/sd* or ls /dev/disk/by-id/* and checking if the device is listed. In the case above, /dev/sdb and /dev/sdc were virtual iDRAC devices.
# ls -al /dev/disk/by-id/
...
lrwxrwxrwx 1 root root 9 Jun 1 12:00 usb-iDRAC_LCDRIVE_20120430-0:0 -> ../../sdb
lrwxrwxrwx 1 root root 9 Jun 1 12:00 usb-iDRAC_Virtual_CD_20120430-0:0 -> ../../sr1
lrwxrwxrwx 1 root root 9 Jun 1 12:00 usb-iDRAC_Virtual_Floppy_20120430-0:1 -> ../../sdc
...
As a workaround, you may configure LVM commands from scanning these devices by adding a filter:
filter = [ "r|/dev/disk/by-id/usb-iDRAC.*|" ]