LVM: Difference between revisions
mNo edit summary |
|||
| Line 1: | Line 1: | ||
The Logical Volume Manager or LVM is a volume manager used on Linux. It facilitates the management of storage volumes which are similar to disk partitions but with the added flexibility of: | The Logical Volume Manager or LVM is a volume manager used on Linux. It facilitates the management of storage volumes which are similar to disk partitions but with the added flexibility of: | ||
== | *Resizing volumes | ||
*Snapshotting volumes for consistent backups | |||
*Span multiple disks together into a single large volume similar to JBOD. | |||
== Usage == | |||
{| class="wikitable" | |||
!Action | |||
!Command | |||
|- | |||
|Show volume groups | |||
|<code>vgs</code> | |||
<code>vgdisplay</code> | |||
|- | |||
|Create a volume group (with name and backing device) | |||
|<code>vgcreate vg_name /dev/disk</code> | |||
|- | |||
|Deactivate a volume group | |||
|<code>vgchange -a n vg_name</code> | |||
|- | |||
|Remove a volume group | |||
|<code>vgremove vg_name</code> | |||
|- | |||
|Show logical volumes | |||
|<code>lvs</code><code>lvdisplay</code> | |||
|- | |||
|Create a logical volume | |||
% of free space in vg with <code>-l</code>, or specific size with <code>-L</code> | |||
|<code>lvcreate -n lv_name -l 50%FREE vg_name</code> | |||
<code>lvcreate -n lv_name -L 50G vg_name</code> | |||
|- | |||
|Resize a logical volume, <code>+Size</code> to increase, <code>-Size</code> to decrease. | |||
|<code>lvresize -L +10G /dev/vg_name/lv_name</code> | |||
|- | |||
|Remove a logical volume | |||
|<code>lvremove lv_name</code> | |||
|} | |||
==Introduction== | |||
To get started with LVM, first create a Volume Group (VG) that specifies which storage (the Physical Volumes, PV) to use. A VG will contain 4MB Physical Extents (PE) that are 'blocks' of storage that can be assigned to a Logical Volume (LV). PEs are mapped to Logical Extents (LE) inside LVs and are mapped by LVM with the mapping data saved in the VG's layout header. Since PEs are assigned, LVs can be resized by adding or removing PEs. However, the filesystem utilizing the volume must be shrunk first or expanded after the change using something like {{code|fs2resize}}. | |||
To quickly get a filesystem up and running using LVM: | To quickly get a filesystem up and running using LVM: | ||
{{highlight|lang=terminal|code= | {{highlight|lang=terminal|code= | ||
# vgcreate ug_vg /dev/sdb | |||
Volume group "ug_vg " successfully created | Volume group "ug_vg " successfully created | ||
# vgdisplay ug_vg | |||
--- Volume group --- | --- Volume group --- | ||
VG Name ug_vg | VG Name ug_vg | ||
| Line 36: | Line 69: | ||
VG UUID WeggQU-zQf3-j3mk-OAFd-iUCQ-EDTo-ezxhcb | VG UUID WeggQU-zQf3-j3mk-OAFd-iUCQ-EDTo-ezxhcb | ||
# lvcreate -n ug -l 90%FREE ug_vg | |||
Logical volume "ug" created. | Logical volume "ug" created. | ||
## After creating a LV, the available free PEs decrease as expected | ## After creating a LV, the available free PEs decrease as expected | ||
# vgdisplay ug_vg | |||
... | ... | ||
VG Size 836.62 GiB | VG Size 836.62 GiB | ||
| Line 49: | Line 82: | ||
# LVM volumes are placed under /dev with the VG name and LV name. | # 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: | ## If a VG needs to be renamed, use the vgrename command: | ||
# vgrename uga ug_vg | |||
}} | }} | ||
=== Recovering Volumes === | === Recovering Volumes === | ||
{{highlight|lang=terminal|code= | {{highlight|lang=terminal|code= | ||
# vgscan # Generates /etc/lvm/backup/VolGroup01 | # vgscan # Generates /etc/lvm/backup/VolGroup01 | ||
| Line 78: | Line 98: | ||
# lvscan # Looks for logical volumes | # lvscan # Looks for logical volumes | ||
# mount /dev/VG01/LV00 /mnt/foo # LVM volumes should be in /dev/vg/lv and should be mountable | # mount /dev/VG01/LV00 /mnt/foo # LVM volumes should be in /dev/vg/lv and should be mountable | ||
}} | }}Use {{code|vgscan}} to rescan for volume groups. This will create a file of the same name as the volume in {{code|/etc/lvm/backup/}}. To restore a volume, use the {{code|vgcfgrestore}} command. With the volume groups restored, use {{code|pvscan}} to look for physical volumes. Activate the volume using {{code|vgchange VolGroup01 -a y}} | ||
With the volume activated, look for logical volumes using {{code|lvscan}}. Logical volumes should show up in {{code|/dev/VolGroup01/LogVol00}} for example and can be mounted. | |||
See also: | See also: | ||
*http://thecususergroup.proboards.com/thread/4156/recover-thecus-raid-pc | |||
=== Mounting / Unmounting Volumes === | |||
===Mounting / Unmounting Volumes=== | |||
If you have a LVM partition, you can see what physical volumes there are under the LVM by running: | If you have a LVM partition, you can see what physical volumes there are under the LVM by running: | ||
| Line 106: | Line 129: | ||
== Expanding Volume == | ==Expanding 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. | #Expand the virtual disk | ||
# Resize the partition that LVM is using using {{code|fdisk}}. | #The OS might not be aware of the disk size change until a reboot. Reboot if the size is still incorrect. | ||
## Delete the partition | #Resize the partition that LVM is using using {{code|fdisk}}. | ||
## Recreate the partition with the same starting location but with a larger size | ##Delete the partition | ||
## Run {{code|partprobe}} to update the partition size | ##Recreate the partition with the same starting location but with a larger size | ||
# Resize the LVM physical volume with {{code|pvresize /dev/sda1}} | ##Run {{code|partprobe}} to update the partition size | ||
# {{code|pvscan}} should show the | #Resize the LVM physical volume with {{code|pvresize /dev/sda1}} | ||
# Resize the LV. Eg: Add 1GB with {{code|lvresize -L +1G /dev/centos/root}} | #{{code|pvscan}} should show the LVM having free space | ||
# Resize the filesystem. | #Resize the LV. Eg: Add 1GB with {{code|lvresize -L +1G /dev/centos/root}} | ||
#Resize the filesystem. | |||
An example from a virtual machine where the disk was expanded from 20GB to 40GB. | An example from a virtual machine where the disk was expanded from 20GB to 40GB. | ||
{{highlight|lang=terminal|code= | {{highlight|lang=terminal|code= | ||
## Expand the partition {{code|/dev/sda2}} using {{code|fdisk}}. Then do the following. | ## Expand the partition {{code|/dev/sda2}} using {{code|fdisk}}. Then do the following. | ||
# pvresize /dev/sda2 | |||
Physical volume "/dev/sda2" changed | Physical volume "/dev/sda2" changed | ||
1 physical volume(s) resized or updated / 0 physical volume(s) not resized | 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] | 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 ] | 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). | 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. | Logical volume centos/root successfully resized. | ||
# lvs | |||
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert | LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert | ||
root centos -wi-ao---- <18.00g | root centos -wi-ao---- <18.00g | ||
swap centos -wi-ao---- 2.00g | swap centos -wi-ao---- 2.00g | ||
## Expand your filesystems. If using XFS: | |||
# xfs_growfs / | |||
meta-data=/dev/mapper/centos-root isize=512 agcount=4, agsize=1113856 blks | meta-data=/dev/mapper/centos-root isize=512 agcount=4, agsize=1113856 blks | ||
= sectsz=512 attr=2, projid32bit=1 | = sectsz=512 attr=2, projid32bit=1 | ||
| Line 146: | Line 177: | ||
realtime =none extsz=4096 blocks=0, rtextents=0 | realtime =none extsz=4096 blocks=0, rtextents=0 | ||
data blocks changed from 4455424 to 4717568 | data blocks changed from 4455424 to 4717568 | ||
# xfs_info / | |||
meta-data=/dev/mapper/centos-root isize=512 agcount=5, agsize=1113856 blks | meta-data=/dev/mapper/centos-root isize=512 agcount=5, agsize=1113856 blks | ||
= sectsz=512 attr=2, projid32bit=1 | = sectsz=512 attr=2, projid32bit=1 | ||
| Line 156: | Line 187: | ||
= sectsz=512 sunit=0 blks, lazy-count=1 | = sectsz=512 sunit=0 blks, lazy-count=1 | ||
realtime =none extsz=4096 blocks=0, rtextents=0 | realtime =none extsz=4096 blocks=0, rtextents=0 | ||
## If using ext4, use resize2fs | |||
# resize2fs /dev/mapper/vg-lv | |||
}} | }} | ||
== See Also == | ==See Also== | ||
* [https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/6/html/storage_administration_guide/xfsgrow Expanding XFS File System] | |||
*[https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/6/html/storage_administration_guide/xfsgrow Expanding XFS File System] | |||
{{Navbox Linux}}[[Category:Linux]] | {{Navbox Linux}} | ||
[[Category:Linux]] | |||
[[Category:LinuxUtilities]] | [[Category:LinuxUtilities]] | ||
Revision as of 20:59, 25 January 2021
The Logical Volume Manager or LVM is a volume manager used on Linux. It facilitates the management of storage volumes which are similar to disk partitions but with the added flexibility of:
- Resizing volumes
- Snapshotting volumes for consistent backups
- Span multiple disks together into a single large volume similar to JBOD.
Usage
| Action | Command |
|---|---|
| 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 | lvslvdisplay
|
| Create a logical volume
% of free space in vg with |
lvcreate -n lv_name -l 50%FREE vg_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
|
Introduction
To get started with LVM, first create a Volume Group (VG) that specifies which storage (the Physical Volumes, PV) to use. A VG will contain 4MB Physical Extents (PE) that are 'blocks' of storage that can be assigned to a Logical Volume (LV). PEs are mapped to Logical Extents (LE) inside LVs and are mapped by LVM with the mapping data saved in the VG's layout header. Since PEs are assigned, LVs can be resized by adding or removing PEs. However, the filesystem utilizing the volume must be shrunk first or expanded after the change using something like fs2resize.
To quickly get a filesystem up and running using LVM:
# 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
Recovering 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:
Mounting / Unmounting Volumes
If you have a LVM partition, you can see what physical volumes there are under the LVM by running:
# pvs
PV VG Fmt Attr PSize PFree
/dev/hda2 VolGroup01 lvm2 a- 148.94G 32.00M
The volume group (VG) is VolumeGroup01. To view the logical volumes under this volume group, run:
# lvdisplay /dev/VolumeGroup01
Once you have located the logical volume you want to mount, say LogVol00, run:
# mount /dev/VolumeGroup01/LogVol00 /mnt/disk
Expanding 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
## 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
## If using ext4, use resize2fs
# resize2fs /dev/mapper/vg-lv