LVM Basic Operations: Difference between revisions
(31 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
<noinclude>{{ContentArticleHeader/Linux_Server|toc=off}}{{ContentArticleHeader/Linux_Desktop}}</noinclude> | <noinclude>{{ContentArticleHeader/Linux_Server|toc=off}}{{ContentArticleHeader/Linux_Desktop}}</noinclude> | ||
''In the following sections is provided information about the most used LVM related operations and basic concepts. There are much more capabilities of the Logical Volume Manager - you can get familiar with them by reading the main source of this article '''[https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/9/html/configuring_and_managing_logical_volumes/overview-of-logical-volume-management_configuring-and-managing-logical-volumes Red Hat Docs > <nowiki>[9]</nowiki> > Configuring and managing logical volumes] .''''' | |||
== Overview of Logical Volume Management (LVM) == | == Overview of Logical Volume Management (LVM) == | ||
* '''''Source: [https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/9/html/configuring_and_managing_logical_volumes/overview-of-logical-volume-management_configuring-and-managing-logical-volumes Red Hat Docs > <nowiki>[9]</nowiki> > Configuring and managing | *'''''Source: [https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/9/html/configuring_and_managing_logical_volumes/overview-of-logical-volume-management_configuring-and-managing-logical-volumes Red Hat Docs > <nowiki>[9]</nowiki> > Configuring and managing LVs > Ch.1: Overview LVM]''''' | ||
{{Media | {{Media | ||
| n = 1 | | n = 1 | ||
Line 16: | Line 17: | ||
The following are the components of LVM - they are illustrated on the diagram shown at {{Media-cite|f|1}}: | The following are the components of LVM - they are illustrated on the diagram shown at {{Media-cite|f|1}}: | ||
* '''Physical volume''': A physical volume (PV) is a partition or whole disk designated for LVM use. For more information, see [https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/9/html/configuring_and_managing_logical_volumes/managing-lvm-physical-volumes_configuring-and-managing-logical-volumes Managing LVM physical volumes]. | *'''Physical volume''': A physical volume (PV) is a partition or whole disk designated for LVM use. For more information, see [https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/9/html/configuring_and_managing_logical_volumes/managing-lvm-physical-volumes_configuring-and-managing-logical-volumes Managing LVM physical volumes]. | ||
* '''Volume group''': A volume group (VG) is a collection of physical volumes (PVs), which creates a pool of disk space out of which logical volumes can be allocated. For more information, see [https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/9/html/configuring_and_managing_logical_volumes/managing-lvm-volume-groups_configuring-and-managing-logical-volumes Managing LVM volume groups]. | *'''Volume group''': A volume group (VG) is a collection of physical volumes (PVs), which creates a pool of disk space out of which logical volumes can be allocated. For more information, see [https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/9/html/configuring_and_managing_logical_volumes/managing-lvm-volume-groups_configuring-and-managing-logical-volumes Managing LVM volume groups]. | ||
* '''Logical volume''': A logical volume represents a mountable storage device. For more information, see [https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/9/html/configuring_and_managing_logical_volumes/managing-lvm-logical-volumes_configuring-and-managing-logical-volumes Managing LVM logical volumes]. | *'''Logical volume''': A logical volume represents a mountable storage device. For more information, see [https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/9/html/configuring_and_managing_logical_volumes/managing-lvm-logical-volumes_configuring-and-managing-logical-volumes Managing LVM logical volumes]. | ||
Requirements: the package <code>'''''lvm2'''''</code> must be installed. | |||
<syntaxhighlight lang="shell" line="1" class="root-prompt code-continue"> | |||
apt install lvm2 | |||
</syntaxhighlight> | |||
==Physical Volumes (PV)== | |||
*'''''Source: [https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/9/html/configuring_and_managing_logical_volumes/managing-lvm-physical-volumes_configuring-and-managing-logical-volumes Red Hat Docs > <nowiki>[9]</nowiki> > Configuring and managing LVs > Ch.2: Managing LVM PV]''''' | |||
The physical volume (PV) is a partition or whole disk designated for LVM use. To use the device for an LVM logical volume, the device must be initialized as a physical volume. | The physical volume (PV) is a partition or whole disk designated for LVM use. To use the device for an LVM logical volume, the device must be initialized as a physical volume. | ||
If 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 <code>fdisk</code> or <code>cfdisk</code> command or an equivalent. If you are using a whole disk device for your physical volume, the disk must have no partition table. Any existing partition table must be erased, which will effectively destroy all data on that disk. You can remove an existing partition table using the <code>wipefs -a <PhysicalVolume>`</code> command as root. | If 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 <code>fdisk</code> or <code>cfdisk</code> command or an equivalent. If you are using a whole disk device for your physical volume, the disk must have no partition table. Any existing partition table must be erased, which will effectively destroy all data on that disk. You can remove an existing partition table using the <code>wipefs -a <PhysicalVolume>`</code> command as root. | ||
=== Display PV === | === Display PV=== | ||
'''<code>[https://manpages.ubuntu.com/manpages/jammy/en/man8/pvdisplay.8.html pvdisplay]</code>''' - Display various attributes of physical volume(s). <code>pvdisplay</code> shows the attributes of PVs, like size, physical extent size, space used for the VG descriptor area, etc. | '''<code>[https://manpages.ubuntu.com/manpages/jammy/en/man8/pvdisplay.8.html pvdisplay]</code>''' - Display various attributes of physical volume(s). <code>pvdisplay</code> shows the attributes of PVs, like size, physical extent size, space used for the VG descriptor area, etc. | ||
<syntaxhighlight lang="shell" line="1" class=" | <syntaxhighlight lang="shell" line="1" class="root-prompt code-continue"> | ||
pvdisplay | |||
</syntaxhighlight> | </syntaxhighlight> | ||
<syntaxhighlight lang="shell-session"> | <syntaxhighlight lang="shell-session"> | ||
Line 45: | Line 54: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
'''<code>[https://manpages.ubuntu.com/manpages/jammy/en/man8/pvs.8.html pvs]</code>''' - Display information about physical volumes. <code>pvs</code> is a preferred alternative of <code>pvdisplay</code> that shows the same information and more, using a more compact and configurable output format. | '''<code>[https://manpages.ubuntu.com/manpages/jammy/en/man8/pvs.8.html pvs]</code>''' - Display information about physical volumes. <code>pvs</code> is a preferred alternative of <code>pvdisplay</code> that shows the same information and more, using a more compact and configurable output format. | ||
<syntaxhighlight lang="shell" line="1" class=" | <syntaxhighlight lang="shell" line="1" class="root-prompt code-continue"> | ||
pvs | |||
</syntaxhighlight> | </syntaxhighlight> | ||
<syntaxhighlight lang="shell-session"> | <syntaxhighlight lang="shell-session"> | ||
Line 52: | Line 61: | ||
/dev/nvme0n1p3 vg_name lvm2 a-- 930.53g 626.53g | /dev/nvme0n1p3 vg_name lvm2 a-- 930.53g 626.53g | ||
</syntaxhighlight>The command <code>[[Linux List All Mount Points|lsblk]]</code> also outputs useful information about PV-VG-LV relations.<syntaxhighlight lang="shell" line="1" class=" | </syntaxhighlight>The command <code>[[Linux List All Mount Points (refs)|lsblk]]</code> also outputs useful information about PV-VG-LV relations. | ||
<syntaxhighlight lang="shell" line="1" class="root-prompt code-continue"> | |||
lsblk -M | lsblk -M | ||
</syntaxhighlight> | </syntaxhighlight> | ||
<syntaxhighlight lang="shell-session" class=" | <syntaxhighlight lang="shell-session" class="shrink-line-height"> | ||
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS | NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS | ||
nvme0n1 259:0 0 931.5G 0 disk | nvme0n1 259:0 0 931.5G 0 disk | ||
Line 70: | Line 80: | ||
│ └──vg-lv_1_ss_at_date_220908 254:5 0 60G 0 lvm | │ └──vg-lv_1_ss_at_date_220908 254:5 0 60G 0 lvm | ||
└─────vg-lv_2_ss_at_date_220912 254:9 0 60G 0 lvm | └─────vg-lv_2_ss_at_date_220912 254:9 0 60G 0 lvm | ||
</syntaxhighlight> | </syntaxhighlight> | ||
<syntaxhighlight lang="shell" line="1"> | '''<code>[https://manpages.ubuntu.com/manpages/jammy/man8/pvscan.8.html pvscan]</code>''' - List all physical volumes. The <code>pvscan</code> command scans all supported LVM block devices in the system for physical volumes. You can define a filter in the <code>[[PVE HDD Sleep (Suspend)#Setup Proxmox Vgscan/Pvestatd|lvm.conf]]</code> file so that this command avoids scanning specific physical volumes. | ||
<syntaxhighlight lang="shell" line="1" class="root-prompt"> | |||
pvscan | |||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 81: | Line 93: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
=== Create PV === | ===Create PV=== | ||
The physical volume could be created on the entire drive or on a partition. It is preferable to partition the disk - [https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/9/html/configuring_and_managing_logical_volumes/managing-lvm-physical-volumes_configuring-and-managing-logical-volumes#multiple-partitions-on-a-disk_managing-lvm-physical-volumes create a single partition that covers the whole disk to label as an LVM physical volume], - otherwise some tools like [https://clonezilla.org/downloads.php CloneZilla] wont handle the drive correctly when LVM is installed directly on the drive (without partitioning), etc.. How to partition the disk from the CLI is described in the article [[Linux Basic Partitioning]]. | The physical volume could be created on the entire drive or on a partition. It is preferable to partition the disk - [https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/9/html/configuring_and_managing_logical_volumes/managing-lvm-physical-volumes_configuring-and-managing-logical-volumes#multiple-partitions-on-a-disk_managing-lvm-physical-volumes create a single partition that covers the whole disk to label as an LVM physical volume], - otherwise some tools like [https://clonezilla.org/downloads.php CloneZilla] wont handle the drive correctly when LVM is installed directly on the drive (without partitioning), etc.. How to partition the disk from the CLI is described in the article [[Linux Basic Partitioning]]. | ||
Create Physicals volume by the partition <code>/dev/nvme0n1p3</code>. | Create Physicals volume by the partition <code>/dev/nvme0n1p3</code>. | ||
<syntaxhighlight lang="shell" line="1"> | <syntaxhighlight lang="shell" line="1" class="root-prompt"> | ||
pvcreate /dev/nvme0n1p3 | |||
</syntaxhighlight> | </syntaxhighlight> | ||
<syntaxhighlight lang="shell-session"> | <syntaxhighlight lang="shell-session"> | ||
Physical volume "/dev/nvme0n1p3" successfully created. | Physical volume "/dev/nvme0n1p3" successfully created. | ||
</syntaxhighlight>Another example: Create Physicals volume by the partition <code>/dev/vda1</code> and <code>/dev/vdb1</code>.<syntaxhighlight lang="shell" line="1" class=" | </syntaxhighlight>'''Another example:''' Create Physicals volume by the partition <code>/dev/vda1</code> and <code>/dev/vdb1</code>. | ||
<syntaxhighlight lang="shell" line="1" class="root-prompt code-continue mlw-shell-gray"> | |||
pvcreate /dev/vda1 /dev/vdb1 | |||
</syntaxhighlight><syntaxhighlight lang="shell-session"> | </syntaxhighlight><syntaxhighlight lang="shell-session"> | ||
Physical volume "/dev/vda1" successfully created. | Physical volume "/dev/vda1" successfully created. | ||
Line 99: | Line 112: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
=== Remove PV === | ===Remove PV=== | ||
If a device is no longer required for use by LVM, you can remove the LVM label by using the <code>pvremove</code> command. Executing the <code>pvremove</code> command zeroes the LVM metadata on an empty physical volume.<syntaxhighlight lang="shell" line="1"> | If a device is no longer required for use by LVM, you can remove the LVM label by using the <code>pvremove</code> command. Executing the <code>pvremove</code> command zeroes the LVM metadata on an empty physical volume. | ||
<syntaxhighlight lang="shell" line="1" class="root-prompt"> | |||
pvremove /dev/nvme0n2p1 | |||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 108: | Line 122: | ||
</syntaxhighlight>Then use <code>pvs</code> and verify if the required volume is removed. If the physical volume you want to remove is currently part of a volume group, you must remove it from the volume group with the <code>[https://manpages.ubuntu.com/manpages/jammy/en/man8/vgreduce.8.html vgreduce]</code> command. For more information, see [https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/9/html/configuring_and_managing_logical_volumes/managing-lvm-volume-groups_configuring-and-managing-logical-volumes#removing-physical-volumes-from-a-volume-group_managing-lvm-volume-groups Removing physical volumes from a volume group]. | </syntaxhighlight>Then use <code>pvs</code> and verify if the required volume is removed. If the physical volume you want to remove is currently part of a volume group, you must remove it from the volume group with the <code>[https://manpages.ubuntu.com/manpages/jammy/en/man8/vgreduce.8.html vgreduce]</code> command. For more information, see [https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/9/html/configuring_and_managing_logical_volumes/managing-lvm-volume-groups_configuring-and-managing-logical-volumes#removing-physical-volumes-from-a-volume-group_managing-lvm-volume-groups Removing physical volumes from a volume group]. | ||
== Volume Groups (VG) == | ==Volume Groups (VG)== | ||
*'''''Source: [https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/9/html/configuring_and_managing_logical_volumes/managing-lvm-volume-groups_configuring-and-managing-logical-volumes Red Hat Docs > <nowiki>[9]</nowiki> > Configuring and managing LVs > Ch.3: Managing LVM VG]''''' | |||
A volume group (VG) is a collection of physical volumes (PVs), which creates a pool of disk space out of which logical volumes (LVs) can be allocated. | A volume group (VG) is a collection of physical volumes (PVs), which creates a pool of disk space out of which logical volumes (LVs) can be allocated. | ||
Line 115: | Line 132: | ||
A logical volume is allocated into logical extents of the same size as the physical extents. The extent size is thus the same for all logical volumes in the volume group. The volume group maps the logical extents to physical extents. | A logical volume is allocated into logical extents of the same size as the physical extents. The extent size is thus the same for all logical volumes in the volume group. The volume group maps the logical extents to physical extents. | ||
=== Display VG === | ===Display VG=== | ||
<code>'''[https://manpages.ubuntu.com/manpages/jammy/en/man8/vgdisplay.8.html vgdisplay]'''</code> - Display volume group information. The <code>vgdisplay</code> command displays volume group properties such as size, extents, number of physical volumes, and other options in a fixed form. If you do not specify a volume group, all existing volume groups are displayed.<syntaxhighlight lang="shell" line="1" class=" | <code>'''[https://manpages.ubuntu.com/manpages/jammy/en/man8/vgdisplay.8.html vgdisplay]'''</code> - Display volume group information. The <code>vgdisplay</code> command displays volume group properties such as size, extents, number of physical volumes, and other options in a fixed form. If you do not specify a volume group, all existing volume groups are displayed. | ||
<syntaxhighlight lang="shell" line="1" class="root-prompt code-continue"> | |||
</syntaxhighlight><syntaxhighlight lang="shell-session"> | vgdisplay | ||
</syntaxhighlight> | |||
<syntaxhighlight lang="shell-session"> | |||
--- Volume group --- | --- Volume group --- | ||
VG Name vg_name | VG Name vg_name | ||
Line 140: | Line 159: | ||
VG UUID NWWfr6-xxfN-0d22-muPn-Rgqj-vAcA-z5P5jV | VG UUID NWWfr6-xxfN-0d22-muPn-Rgqj-vAcA-z5P5jV | ||
</syntaxhighlight>The following example shows the output of the <code>vgdisplay</code> command for the volume group ''<code>vg_name</code>''.<syntaxhighlight lang="shell" line="1" class=" | </syntaxhighlight>The following example shows the output of the <code>vgdisplay</code> command for the volume group ''<code>vg_name</code>''. | ||
<syntaxhighlight lang="shell" line="1" class="root-prompt code-continue mlw-shell-gray"> | |||
</syntaxhighlight><code>'''[https://manpages.ubuntu.com/manpages/jammy/en/man8/vgs.8.html vgs]'''</code> - Display information about volume groups. It is a preferred alternative of <code>vgdisplay</code> that shows the same information and more, using a more compact and configurable output format.<syntaxhighlight lang="shell" line="1" class=" | vgdisplay vg_name | ||
</syntaxhighlight><code>'''[https://manpages.ubuntu.com/manpages/jammy/en/man8/vgs.8.html vgs]'''</code> - Display information about volume groups. It is a preferred alternative of <code>vgdisplay</code> that shows the same information and more, using a more compact and configurable output format. | |||
<syntaxhighlight lang="shell" line="1" class="root-prompt code-continue"> | |||
vgs | |||
</syntaxhighlight><syntaxhighlight lang="shell-session"> | </syntaxhighlight><syntaxhighlight lang="shell-session"> | ||
VG #PV #LV #SN Attr VSize VFree | VG #PV #LV #SN Attr VSize VFree | ||
vg_name 1 6 2 wz--n- 930.53g 566.53g | vg_name 1 6 2 wz--n- 930.53g 566.53g | ||
</syntaxhighlight>'''<code>[https://manpages.ubuntu.com/manpages/jammy/en/man8/vgscan.8.html vgscan]</code>''' - Search for all volume groups. <code>vgscan</code> scans all supported LVM block devices in the system for VGs.<syntaxhighlight lang="shell" line="1" class=" | </syntaxhighlight>'''<code>[https://manpages.ubuntu.com/manpages/jammy/en/man8/vgscan.8.html vgscan]</code>''' - Search for all volume groups. <code>vgscan</code> scans all supported LVM block devices in the system for VGs. | ||
<syntaxhighlight lang="shell" line="1" class="root-prompt code-continue"> | |||
vgscan | |||
</syntaxhighlight><syntaxhighlight lang="shell-session"> | </syntaxhighlight><syntaxhighlight lang="shell-session"> | ||
Found volume group "vg_name" using metadata type lvm2 | Found volume group "vg_name" using metadata type lvm2 | ||
</syntaxhighlight> | </syntaxhighlight> | ||
=== Create VG === | ===Create VG=== | ||
Create Volume group named <code>vg_name</code> by using the <code>/dev/nvme0n1p3</code> Physical volume.<syntaxhighlight lang="shell" line="1" class=" | Create Volume group named <code>vg_name</code> by using the <code>/dev/nvme0n1p3</code> Physical volume. | ||
<syntaxhighlight lang="shell" line="1" class="root-prompt code-continue"> | |||
vgcreate vg_name /dev/nvme0n1p3 | |||
</syntaxhighlight><syntaxhighlight lang="shell-session"> | </syntaxhighlight><syntaxhighlight lang="shell-session"> | ||
Volume group "vg_name" successfully created. | Volume group "vg_name" successfully created. | ||
</syntaxhighlight>Another example: Create Volume group named <code>another_vg</code> by using <code>/dev/vda1</code> and <code>/dev/vdb1</code> Physical volumes.<syntaxhighlight lang="shell" line="1" class=" | </syntaxhighlight>'''Another example:''' Create Volume group named <code>another_vg</code> by using <code>/dev/vda1</code> and <code>/dev/vdb1</code> Physical volumes. | ||
<syntaxhighlight lang="shell" line="1" class="root-prompt code-continue mlw-shell-gray"> | |||
vgcreate another_vg /dev/vda1 /dev/vdb1 | |||
</syntaxhighlight><syntaxhighlight lang="shell-session"> | </syntaxhighlight><syntaxhighlight lang="shell-session"> | ||
Volume group "another_vg" successfully created. | Volume group "another_vg" successfully created. | ||
</syntaxhighlight>'''Another example:''' Create a new '''physical volume''' and then a new '''volume group''' on newly attached block device - in this case <code>/dev/sdc</code>.<syntaxhighlight lang="shell" line="1" class="root-prompt code-continue"> | |||
lsblk # check | |||
pvcreat /dev/sdbc # crate phisical volume | |||
vgcreate vg_name /dev/sdbc # create volume group | |||
vgdisplay # check | |||
</syntaxhighlight> | </syntaxhighlight> | ||
===Extend VG=== | |||
'''<code class="noTypo">[https://manpages.ubuntu.com/manpages/jammy/en/man8/vgextend.8.html vgextend]</code>''' - Add physical volumes to a volume group. <code class="noTypo">vgextend</code> adds one or more PVs to a VG. This increases the space available for LVs in the VG. Also, PVs that have gone missing and then returned, e.g. due to a transient device failure, can be added back to the VG without re-initializing them (see <code class="noTypo">--restore­missing</code>). | |||
=== | If the specified PVs have not yet been initialized with <code class="noTypo">pvcreate</code>, <code class="noTypo">vgextend</code> will initialize them. In this case <code class="noTypo">pvcreate</code> options can be used, e.g. <code class="noTypo">--label­sector</code>, <code class="noTypo">--meta­data­size</code>, <code class="noTypo">--meta­data­ignore</code>, <code class="noTypo">--pv­meta­data­copies</code>, <code class="noTypo">--data­align­ment</code>, <code class="noTypo">--data­align­ment­offset</code>. | ||
Example 1: Occupy a partition that is not yet defined as Physical volume.<syntaxhighlight lang="shell" line="1" class="root-prompt code-continue"> | |||
vgextend vg_name /dev/nvme0n2p1 | |||
</syntaxhighlight><syntaxhighlight lang="shell-session"> | |||
Physical volume "/dev/nvme0n2p1" successfully created. | |||
Volume group "vg_name" successfully extended. | |||
</syntaxhighlight> | |||
Example 2: Create a new Physical volume and then extend an existing Volume group named <code>another_vg</code>. | |||
<syntaxhighlight lang="shell" line="1" class="root-prompt code-continue mlw-shell-gray"> | |||
lsblk # Check the devices | |||
pvcreat /dev/sdb # Create a Phisical volume at /dev/sdb | |||
pvdisplay # Check the list of the Phisical volumes | |||
</syntaxhighlight> | |||
<syntaxhighlight lang="shell" line="1" class="root-prompt code-continue"> | |||
vgextend another_vg /dev/sdb # Extend the existing VG to occupy the new PV | |||
</syntaxhighlight> | |||
<syntaxhighlight lang="shell" line="1" class="root-prompt code-continue mlw-shell-gray"> | |||
df -hT # Check the list of the available devices | |||
vgdisplay # Check the VGs information | |||
</syntaxhighlight> | |||
=== | ===Merge VG: Combine VGs=== | ||
<code>'''[https://manpages.ubuntu.com/manpages/jammy/en/man8/vgmerge.8.html vgmerge]'''</code> - Merge volume groups. To combine two volume groups into a single volume group, use the <code>vgmerge</code> 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. | |||
<syntaxhighlight lang="shell" line="1"> | Merge the inactive volume group <code>another_vg</code> into the active or inactive volume group <code>vg_name</code> giving verbose runtime information. | ||
<syntaxhighlight lang="shell" line="1" class="root-prompt code-continue"> | |||
vgmerge -v vg_name another_vg | |||
</syntaxhighlight> | |||
===Reduce VG: Remove PVs from a VG=== | |||
'''<code>[https://manpages.ubuntu.com/manpages/jammy/en/man8/vgreduce.8.html vgreduce]</code>''' - Remove physical volume(s) from a volume group. The <code>vgreduce</code> 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. | |||
'''1.''' If the Physical volume is still being used, migrate the data to another Physical volume from the same Volume group by <code>[https://manpages.ubuntu.com/manpages/jammy/en/man8/pvmove.8.html pvmove]</code>.<syntaxhighlight lang="shell" line="1" class="root-prompt code-continue"> | |||
pvmove /dev/vdb3 | |||
</syntaxhighlight><syntaxhighlight lang="shell-session"> | |||
/dev/vdb3: Moved: 2.0% | |||
...v/vdb3: Moved: 79... | |||
/dev/vdb3: Moved: 100.0% | |||
</syntaxhighlight>'''2.''' If there are no enough free extents on the other physical volumes in the existing volume group. | |||
<syntaxhighlight lang="shell" line="1" class="root-prompt code-continue mlw-shell-gray"> | |||
pvcreate /dev/vdb4 # Create a new physical volume from /dev/vdb4 | |||
vgextend vg_name /dev/vdb4 # Add the newly created PV to the vg_name VG | |||
pvmove /dev/vdb3 /dev/vdb4 # Move the data from /dev/vdb3 to /dev/vdb4 | |||
</syntaxhighlight>'''3.''' Remove the physical volume <code>/dev/vdb3</code> from the volume group. | |||
<syntaxhighlight lang="shell" line="1" class="root-prompt code-continue"> | |||
pvmove /dev/vdb3 | |||
</syntaxhighlight>'''4.''' Verify if the <code>/dev/vdb3</code> physical volume is removed from the <code>vg_name</code> volume group by the help of <code>pvs</code>. | |||
===Split VG=== | |||
<code>'''[https://manpages.ubuntu.com/manpages/jammy/en/man8/vgsplit.8.html vgsplit]'''</code> - Move physical volumes into a new or existing volume group. More details at Red Hat's manual - section [https://manpages.ubuntu.com/manpages/jammy/en/man8/vgsplit.8.html Splitting a LVM volume group]. | |||
<syntaxhighlight lang="shell" line="1" class="root-prompt code-continue"> | |||
vgsplit myvg yourvg /dev/vdb3 | |||
</syntaxhighlight><syntaxhighlight lang="shell-session"> | |||
Volume group "yourvg" successfully split from "myvg" | |||
</syntaxhighlight> | |||
=== Rename VG=== | |||
<code>'''[https://manpages.ubuntu.com/manpages/jammy/en/man8/vgrename.8.html vgrename]'''</code> - Rename a volume group. <code>'''[https://manpages.ubuntu.com/manpages/jammy/en/man8/vgchange.8.html vgchange]'''</code> - Change volume group attributes. | |||
<syntaxhighlight lang="shell" line="1" class="root-prompt code-continue"> | |||
vgchange --activate n vg_name # Deactivate the volume group | |||
</syntaxhighlight> | |||
<syntaxhighlight lang="shell" line="1" class="root-prompt code-continue"> | |||
vgrename vg_name new_vg_name # Rename an existing volume group | |||
</syntaxhighlight> | |||
<syntaxhighlight lang="shell" line="1" class="root-prompt code-continue mlw-shell-gray"> | |||
vgrename /dev/vg_name /dev/new_vg_name # Rename by specifying the full paths to the devices | |||
</syntaxhighlight>'''Rename volume group by UUID:''' This is useful when you have attached to the system a device that have a VG with the same name as one already existing. Use <code>vgdisplay</code> to identify the target volume group and its UUID. | |||
<syntaxhighlight lang="shell" line="1" class="root-prompt code-continue mlw-shell-gray"> | |||
vgdisplay | |||
</syntaxhighlight><syntaxhighlight lang="shell" line="1" class="root-prompt code-continue"> | |||
vgrename ZTIHAQ-rFck-3IEK-V2mx-mckk-R1zJ-nNKWox old_pve | |||
</syntaxhighlight><syntaxhighlight lang="bash" class="root-prompt code-continue"> | |||
WARNING: VG name "pve" is used by VGs "ZTIHAQ-rFck-3IEK-V2mx-mckk-R1zJ-nNKWox" and "hXgkkq-MdYE-dobE-VPrC-0c6f-p5Oi-L4XEZu". | |||
Fix duplicate VG names with vgrename uuid, a device filter, or system IDs. | |||
Processing VG pve because of matching UUID ZTIHAQ-rFck-3IEK-V2mx-mckk-R1zJ-nNKWox | |||
Volume group "ZTIHAQ-rFck-3IEK-V2mx-mckk-R1zJ-nNKWox" successfully renamed to "old_pve" | |||
</syntaxhighlight> | |||
===Remove VG=== | |||
'''<code>[https://manpages.ubuntu.com/manpages/jammy/en/man8/vgremove.8.html vgremove]</code>''' - Remove volume group(s). <code>vgremove</code> removes one or more VGs. If LVs exist in the VG, a prompt is used to confirm LV removal. | |||
If one or more PVs in the VG are lost, consider <code class="noTypo">vgreduce --remove­missing</code> to make the VG metadata consistent again. Repeat the force option (<code>-ff</code>) to forcibly remove LVs in the VG without confirmation. | |||
==Logical Volumes (LV)== | |||
*'''''Source: [https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/9/html/configuring_and_managing_logical_volumes/managing-lvm-logical-volumes_configuring-and-managing-logical-volumes Red Hat Docs > <nowiki>[9]</nowiki> > Configuring and managing LVs > Ch.4: Managing LVM LV]''''' | |||
*'''''Source: [https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/9/html/configuring_and_managing_logical_volumes/modifying-the-size-of-a-logical-volume_configuring-and-managing-logical-volumes Red Hat Docs > <nowiki>[9]</nowiki> > Configuring and managing LVs > Ch.5: Modifying the size of a LV]''''' | |||
A logical volume is a virtual, block storage device that a file system, database, or application can use. To create an LVM logical volume, the physical volumes (PVs) are combined into a volume group (VG). This creates a pool of disk space out of which LVM logical volumes (LVs) can be allocated. | |||
An administrator can grow or shrink logical volumes without destroying data, unlike standard disk partitions. If the physical volumes in a volume group are on separate drives or RAID arrays, then administrators can also spread a logical volume across the storage devices. | |||
The following are the different types of logical volumes ([https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/9/html/configuring_and_managing_logical_volumes/managing-lvm-logical-volumes_configuring-and-managing-logical-volumes more details]): | |||
*Linear volumes | |||
*Striped logical volumes | |||
*RAID logical volumes | |||
*Thin-provisioned logical volumes (thin volumes) - Using thin-provisioned logical volumes, you can create logical volumes that are larger than the available physical storage... | |||
*Snapshot volumes | |||
*Thin-provisioned snapshot volumes | |||
*Cache volumes | |||
===Create LV=== | |||
[https://manpages.ubuntu.com/manpages/jammy/en/man8/lvcreate.8.html '''lvcreate'''] - Create LVM '''logical volume''' at <code>lvm-vm-group</code>: | |||
<syntaxhighlight lang="shell" line="1" class="root-prompt"> | |||
lvcreate -n vm-win-01 -L 60g lvm-vm-group | |||
</syntaxhighlight> | </syntaxhighlight> | ||
<syntaxhighlight lang="sh"> | <syntaxhighlight lang="sh"> | ||
Logical volume "vm-win-01" created. | Logical volume "vm-win-01" created. | ||
</syntaxhighlight> | </syntaxhighlight> | ||
* <code>vm-win-01</code> is the name of the logical device, it is mater of your choice. | * <code>vm-win-01</code> is the name of the logical volume (device), it is mater of your choice. | ||
Check the result: | Check the result: | ||
<syntaxhighlight lang="shell" line="1"> | <syntaxhighlight lang="shell" line="1" class="root-prompt"> | ||
lsblk | grep -P 'sdb|lvm' | lsblk | grep -P 'sdb|lvm' | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 193: | Line 321: | ||
└─sdb2 8:18 0 231.9G 0 part | └─sdb2 8:18 0 231.9G 0 part | ||
└─lvm--vm--group-vm--win--01 253:0 0 60G 0 lvm | └─lvm--vm--group-vm--win--01 253:0 0 60G 0 lvm | ||
</syntaxhighlight>'''Another example:''' For [[pve:Recover_From_Grub_Failure|Recovering PVE from grub "disk not found" error when booting from LVM]] by creating 4MiB LV. | |||
<syntaxhighlight lang="shell" line="1" class="root-prompt"> | |||
vgscan | |||
lvcreate -L 4M pve -n grubtemp | |||
</syntaxhighlight> | </syntaxhighlight> | ||
---- | *Assume you have <code>pve</code> volume group. | ||
* <code>grubtemp</code> is the name of the new logical volume (LV). | |||
'''Another example:''' Create a new logical volume and format it as Ext4, and mount it permanently via <code>/etc/fstab</code> by its UUID. | |||
<syntaxhighlight lang="shell" line="1" class="root-prompt code-continue"> | |||
lvcreate vg_name -L 50G -n lv_name # create 50GB logical volume | |||
lvdisplay # check | |||
mkfs.ext4 /dev/mapper/vg_name-lv_name # format the logical folume as Ext4 | |||
</syntaxhighlight><syntaxhighlight lang="shell" line="1" class="root-prompt code-continue mlw-shell-gray"> | |||
mount /dev/mapper/vg_name-lv_name /mnt/volume # Mount it manually | |||
df -h /mnt/volume # Check | |||
umount /mnt/volume | |||
</syntaxhighlight><syntaxhighlight lang="shell" line="1" class="root-prompt code-continue mlw-shell-gray"> | |||
blkid /dev/mapper/vg_name-lv_name | |||
</syntaxhighlight><syntaxhighlight lang="shell" class="root-prompt code-continue mlw-shell-gray"> | |||
/dev/mapper/vg_name-lv_name: UUID="6cb60ea8-d45e..." BLOCK_SIZE="4096" TYPE="ext4" | |||
</syntaxhighlight><syntaxhighlight lang="shell" line="1" class="root-prompt code-continue mlw-shell-gray"> | |||
nano /etc/fstab | |||
</syntaxhighlight><syntaxhighlight lang="shell-session" class="root-prompt code-continue mlw-shell-gray"> | |||
UUID=6cb60ea8-d4... /mnt/volume ext4 defaults 0 2 | |||
</syntaxhighlight> | |||
<syntaxhighlight lang=" | '''Another example:''' Create a new logical volume and format it as Swap, and mount it permanently via <code>/etc/fstab</code>. by <code>vg_name/lv_name</code>. | ||
## | <syntaxhighlight lang="shell" line="1" class="root-prompt code-continue"> | ||
lvcreate vg_name -L 16G -n swap_1 # create 16GB logical volume | |||
lvdisplay # check | |||
mkswap /dev/mapper/vg_name-swap_1 # format the logical folume as Swap | |||
</syntaxhighlight><syntaxhighlight lang="shell" line="1" class="root-prompt code-continue mlw-shell-gray"> | |||
swapon /dev/mapper/vg_name-swap_1 | |||
</syntaxhighlight><syntaxhighlight lang="shell" line="1" class="root-prompt code-continue mlw-shell-gray"> | |||
nano /etc/fstab | |||
</syntaxhighlight><syntaxhighlight lang="shell-session" class="root-prompt code-continue mlw-shell-gray"> | |||
/dev/vg_name/swap_1 none swap sw 0 0 | |||
</syntaxhighlight> | |||
===Rename LV=== | |||
<code>'''[https://manpages.ubuntu.com/manpages/jammy/en/man8/lvrename.8.html lvrename]'''</code> - Rename a logical volume.<syntaxhighlight lang="shell" line="1" class="root-prompt code-continue mlw-shell-gray"> | |||
umount /mnt/my-lv-mount-point | |||
</syntaxhighlight><syntaxhighlight lang="shell" line="1" class="root-prompt"> | |||
lvrename my-volume-group my-lv-prev my-lv-new | |||
</syntaxhighlight>Alternatively you can rename LV by its full path.<syntaxhighlight lang="shell" line="1" class="root-prompt"> | |||
lvrename /dev/my-volume-group/my-lv-prev /dev/my-volume-group/my-lv-new | |||
</syntaxhighlight> | |||
=== Remove LV=== | |||
<code>'''[https://manpages.ubuntu.com/manpages/jammy/en/man8/lvremove.8.html lvremove]'''</code> - Remove logical volume(s) from the system. | |||
If the logical volume is currently mounted, unmount the volume.<syntaxhighlight lang="shell" line="1" class="root-prompt code-continue mlw-shell-gray"> | |||
umount /mnt/my-lv-name-mount-point | |||
</syntaxhighlight>If the logical volume exists in a clustered environment, deactivate the logical volume on all nodes where it is active. Use the following command on each such node.<syntaxhighlight lang="shell" line="1" class="root-prompt code-continue mlw-shell-gray"> | |||
lvchange --activate n my-volume-group/my-lv-name | |||
</syntaxhighlight>Remove the logical volume using the <code>lvremove</code> utility.<syntaxhighlight lang="shell" line="1" class="root-prompt"> | |||
lvremove /dev/my-volume-group/my-lv-name | |||
</syntaxhighlight> | |||
<syntaxhighlight lang="shell-session"> | |||
Do you really want to remove active logical volume my-volume-group/my-lv-name? [y/n]: y | |||
Logical volume "my-lv-name" successfully removed | |||
</syntaxhighlight> | |||
=== Resize LV (extend|reduce) === | |||
<code>'''[https://manpages.ubuntu.com/manpages/jammy/en/man8/lvresize.8.html lvresize]'''</code> - Resize a logical volume. <code>lvresize</code> resizes an LV in the same way as <code>[https://manpages.ubuntu.com/manpages/jammy/en/man8/lvextend.8.html lvextend]</code> and <code>[https://manpages.ubuntu.com/manpages/jammy/en/man8/lvreduce.8.html lvreduce]</code>. | |||
> / | |||
'''Example 0:''' '''Resize''' (Extend) an LV by 16MB using specific physical extents.<syntaxhighlight lang="shell" line="1" class="root-prompt"> | |||
lvresize -L+16M vg1/lv1 /dev/sda:0-1 /dev/sdb:0-1 | |||
</syntaxhighlight>'''Example 1:''' '''Extend''' a logical volume '''by 10GiB''' and '''resize the file-system manually'''. (note there mus have enough free space in the volume group where the existing LV is located.<syntaxhighlight lang="shell" line="1" class="root-prompt"> | |||
lvextend -L +10G /dev/mapper/vg_name-lv_name | |||
rsize2fs /dev/mapper/vg_name-lv_name | |||
df -h # To see the changes if the LV is mounted | |||
</syntaxhighlight>'''Example 2:''' '''Extend''' a logical volume, '''occupy 100%''' of the available space in the VG and '''resize the file-system''' in the same time.<syntaxhighlight lang="shell" line="1" class="root-prompt"> | |||
lvextend --reziefs -l +100%FREE /dev/mapper/vg_name-lv_name | |||
df -h # To see the changes if the LV is mounted | |||
</syntaxhighlight>'''Example 3:''' '''Reduce''' (Resize|Shrink) the LV and its file-system (from larger size) to exactly 60GiB. Note this may be dangerous an you may need to take a backup of the file-system before do it.<syntaxhighlight lang="shell" line="1" class="root-prompt"> | |||
lvreduce --resizefs -L 60G vg_name/lv_name | |||
df -h # To see the changes if the LV is mounted | |||
</syntaxhighlight> | |||
==Snapshots of Logical Volumes== | |||
* '''''Source: [https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/9/html/configuring_and_managing_logical_volumes/snapshot-of-logical-volumes_configuring-and-managing-logical-volumes Red Hat Docs > <nowiki>[9]</nowiki> > Configuring and managing LVs > Ch.7: Snapshot of LV]''''' | |||
Using the LVM snapshot feature, you can create virtual images of a volume, for example, ''/dev/sda'', at a particular instant without causing a service interruption. | |||
When you modify the original volume (the origin) after you take a snapshot, the snapshot feature makes a copy of the modified data area as it was prior to the change so that it can reconstruct the state of the volume. When you create a snapshot, full read and write access to the origin stays possible.... | |||
=== Create a Snapshot === | ===Create a Snapshot === | ||
{{collapse/begin}} | {{collapse/begin}} | ||
<code>[https://manpages.ubuntu.com/manpages/jammy/en/man8/lvcreate.8.html lvcreate]</code> - Create a logical volume. | '''<code>[https://manpages.ubuntu.com/manpages/jammy/en/man8/lvcreate.8.html lvcreate]</code>''' - Create a logical volume. | ||
{{collapse/div|#Explanation}} | {{collapse/div|#Explanation}} | ||
* <code class="noTypo">-s</code>, <code class="noTypo">--snapshot</code> - Create a snapshot. Snapshots provide a "frozen image" of an origin LV. The snapshot LV can be used, e.g. for backups, while the origin LV continues to be used. This option can create a COW (copy on write) snapshot, or a ''thin snapshot'' (in a thin pool.) Thin snapshots are created when the origin is a thin LV and the size option is NOT specified. | * <code class="noTypo">-s</code>, <code class="noTypo">--snapshot</code> - Create a snapshot. Snapshots provide a "frozen image" of an origin LV. The snapshot LV can be used, e.g. for backups, while the origin LV continues to be used. This option can create a COW (copy on write) snapshot, or a ''thin snapshot'' (in a thin pool.) Thin snapshots are created when the origin is a thin LV and the size option is NOT specified. | ||
* <code class="noTypo">-n</code>, <code class="noTypo">--name</code> - Specifies the name of a new LV. When unspecified, a default name of <code>lvol#</code> is generated, where <code>#</code> is a number generated by LVM. | * <code class="noTypo">-n</code>, <code class="noTypo">--name</code> - Specifies the name of a new LV. When unspecified, a default name of <code>lvol#</code> is generated, where <code>#</code> is a number generated by LVM. | ||
{{collapse/end}} | {{collapse/end}} | ||
Create 5GB snapshot of a logical volume named <code>lv_name</code> from volume group named <code>vg_name</code>. | Create 5GB snapshot of a logical volume named <code>lv_name</code> from volume group named <code>vg_name</code>. | ||
<syntaxhighlight lang="shell" line="1" class=" | |||
<syntaxhighlight lang="shell" line="1" class="root-prompt code-continue"> | |||
lvcreate /dev/mapper/vg_name-lv_name -L 5G -s -n lv_name_ss_at_date_$(date +%y%m%d) | |||
</syntaxhighlight> | </syntaxhighlight> | ||
<syntaxhighlight lang="shell" line="1" class=" | |||
<syntaxhighlight lang="shell" line="1" class="root-prompt code-continue mlw-shell-gray"> | |||
lvs | |||
</syntaxhighlight> | </syntaxhighlight> | ||
<syntaxhighlight lang="terraform" line="1" highlight="3"> | <syntaxhighlight lang="terraform" line="1" highlight="3"> | ||
Line 291: | Line 434: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
=== Mount a Snapshot === | ===Mount a Snapshot=== | ||
The snapshot could be mounted in order to fetch files in their snapshot state. In this case it is good to mount them in read only mode.<syntaxhighlight lang="shell" line="1" class=" | The snapshot could be mounted in order to fetch files in their snapshot state. In this case it is good to mount them in read only mode. | ||
<syntaxhighlight lang="shell" line="1" class="root-prompt code-continue mlw-shell-gray"> | |||
</syntaxhighlight><syntaxhighlight lang="shell" line="1" class=" | mkdir /mnt/snapshot | ||
</syntaxhighlight> | |||
</syntaxhighlight>You can use a mounted snapshot (of the root filesystem) to create a backup while the OS is running. | <syntaxhighlight lang="shell" line="1" class="root-prompt code-continue"> | ||
mount -r /dev/mapper/vg_name-lv_name_ss_at_date_220908 /mnt/snapshot | |||
</syntaxhighlight>You can use a mounted snapshot (of the root filesystem) to create a backup while the OS is running: | |||
* DevConnected: [https://devconnected.com/lvm-snapshots-backup-and-restore-on-linux/ LVM Snapshots Backup and Restore on Linux] | |||
=== Restore a Snapshot === | ===Restore a Snapshot=== | ||
Note after the restoring of a snapshot with merge option in use the snapshot will be removed, so you must create another snapshot for a later use of the same state of the logical volume. | Note after the restoring of a snapshot with merge option in use the snapshot will be removed, so you must create another snapshot for a later use of the same state of the logical volume. | ||
For restoring a snapshot we will need to use the commands [https://manpages.ubuntu.com/manpages/jammy/en/man8/lvconvert.8.html <code>lvconvert</code>] and [https://manpages.ubuntu.com/manpages/jammy/man8/lvchange.8.html <code>lvchange</code>]. | For restoring a snapshot we will need to use the commands [https://manpages.ubuntu.com/manpages/jammy/en/man8/lvconvert.8.html <code>lvconvert</code>] and [https://manpages.ubuntu.com/manpages/jammy/man8/lvchange.8.html <code>lvchange</code>]. | ||
==== LvConvert: Convert-merge the snapshot with the source LV ==== | ====LvConvert: Convert-merge the snapshot with the source LV==== | ||
{{collapse/begin}} | {{collapse/begin}} | ||
<code>[https://manpages.ubuntu.com/manpages/jammy/en/man8/lvconvert.8.html lvconvert]</code> - Change logical volume layout. | <code>[https://manpages.ubuntu.com/manpages/jammy/en/man8/lvconvert.8.html lvconvert]</code> - Change logical volume layout. | ||
Line 310: | Line 457: | ||
* <code class="noTypo">--mergesnapshot</code> - Merge COW snapshot LV into its origin. When merging a snapshot, if both the origin and snapshot LVs are not open, the merge will start immediately. Otherwise, the merge will start the first time either the origin or snapshot LV are activated and both are closed. Merging a snapshot into an origin that cannot be closed, for example a root filesystem, is deferred until the next time the origin volume is activated. When merging starts, the resulting LV will have the origin's name, minor number and UUID. While the merge is in progress, reads or writes to the origin appear as being directed to the snapshot being merged. When the merge finishes, the merged snapshot is removed. Multiple snapshots may be specified on the command line or a @tag may be used to specify multiple snapshots be merged to their respective origin. | * <code class="noTypo">--mergesnapshot</code> - Merge COW snapshot LV into its origin. When merging a snapshot, if both the origin and snapshot LVs are not open, the merge will start immediately. Otherwise, the merge will start the first time either the origin or snapshot LV are activated and both are closed. Merging a snapshot into an origin that cannot be closed, for example a root filesystem, is deferred until the next time the origin volume is activated. When merging starts, the resulting LV will have the origin's name, minor number and UUID. While the merge is in progress, reads or writes to the origin appear as being directed to the snapshot being merged. When the merge finishes, the merged snapshot is removed. Multiple snapshots may be specified on the command line or a @tag may be used to specify multiple snapshots be merged to their respective origin. | ||
{{collapse/end}} | {{collapse/end}} | ||
<syntaxhighlight lang="shell" line="1" class=" | <syntaxhighlight lang="shell" line="1" class="root-prompt code-continue mlw-shell-gray"> | ||
umount /lv_name-mountpoint | |||
</syntaxhighlight><syntaxhighlight lang="shell" line="1" class=" | </syntaxhighlight> | ||
<syntaxhighlight lang="shell" line="1" class="root-prompt code-continue"> | |||
</syntaxhighlight>If it is a root filesystem, and the source logical volume is not operational at all, we need to boot in live Linux session in order to perform the steps, because it must be unmounted. It it is not a root filesystem, we need to unmount the logical volume first. | lvconvert --merge /dev/mapper/vg_name-lv_name_ss_at_date_220908 | ||
</syntaxhighlight> | |||
If it is a root filesystem, and the source logical volume is not operational at all, we need to boot in live Linux session in order to perform the steps, because it must be unmounted. It it is not a root filesystem, we need to unmount the logical volume first. | |||
==== LvChange: Deactivate, reactivate and remount the LV ==== | ====LvChange: Deactivate, reactivate and remount the LV==== | ||
{{collapse/begin}} | {{collapse/begin}} | ||
<code>[https://manpages.ubuntu.com/manpages/jammy/man8/lvchange.8.html lvchange]</code> - Change the attributes of logical volume(s). | <code>[https://manpages.ubuntu.com/manpages/jammy/man8/lvchange.8.html lvchange]</code> - Change the attributes of logical volume(s). | ||
Line 322: | Line 471: | ||
* <code>-a|</code>, <code class="noTypo">--activate y|n|ay</code> - Change the active state of LVs. An active LV can be used through a block device, allowing data on the LV to be accessed. <code>'''y'''</code> makes LVs active, or available. <code>'''n'''</code> makes LVs inactive, or unavailable... | * <code>-a|</code>, <code class="noTypo">--activate y|n|ay</code> - Change the active state of LVs. An active LV can be used through a block device, allowing data on the LV to be accessed. <code>'''y'''</code> makes LVs active, or available. <code>'''n'''</code> makes LVs inactive, or unavailable... | ||
{{collapse/end}} | {{collapse/end}} | ||
<syntaxhighlight lang="shell" line="1" class=" | <syntaxhighlight lang="shell" line="1" class="root-prompt code-continue"> | ||
lvchange -an /dev/mapper/vg_name-lv_name | |||
lvchange -ay /dev/mapper/vg_name-lv_name | |||
</syntaxhighlight> | |||
<syntaxhighlight lang="shell" line="1" class="root-prompt code-continue mlw-shell-gray"> | |||
lvchange -ay /dev/mapper/vg_name-lv_name -K | |||
</syntaxhighlight> | </syntaxhighlight> | ||
If we are working within a live Linux session - i.e. we are restoring a root filesystem, - these steps will be performed at the next reboot. Otherwise we need to run the following steps in order to apply the changes without reboot. | If we are working within a live Linux session - i.e. we are restoring a root filesystem, - these steps will be performed at the next reboot. Otherwise we need to run the following steps in order to apply the changes without reboot. | ||
<syntaxhighlight lang="shell" line="1" class=" | |||
<syntaxhighlight lang="shell" line="1" class="root-prompt code-continue"> | |||
</syntaxhighlight><syntaxhighlight lang="shell" line="1" class=" | mount -a # remount via /etc/fstab | ||
</syntaxhighlight> | |||
<syntaxhighlight lang="shell" line="1" class="root-prompt code-continue mlw-shell-gray"> | |||
df -h # list the mounted devices | df -h # list the mounted devices | ||
</syntaxhighlight> | </syntaxhighlight> | ||
=== Remove a Snapshot === | ===Remove a Snapshot=== | ||
<code>[https://manpages.ubuntu.com/manpages/jammy/en/man8/lvremove.8.html lvremove]</code> - Remove logical volume(s) from the system.<syntaxhighlight lang="shell" line="1" class=" | <code>[https://manpages.ubuntu.com/manpages/jammy/en/man8/lvremove.8.html lvremove]</code> - Remove logical volume(s) from the system. | ||
<syntaxhighlight lang="shell" line="1" class="root-prompt code-continue"> | |||
</syntaxhighlight><syntaxhighlight lang="shell" line="1" class=" | lvremove /dev/vg_name/lv_name_ss_at_date_220908 | ||
</syntaxhighlight> | |||
<syntaxhighlight lang="shell" line="1" class="root-prompt code-continue mlw-shell-gray"> | |||
lvremove /dev/mapper/vg_name-lv_name_ss_at_date_220908 | |||
</syntaxhighlight> | </syntaxhighlight> | ||
== References == | == Thin Volumes, Enable Caching and Create Methods... == | ||
'''''References:''''' | |||
* ''[https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/9/html/configuring_and_managing_logical_volumes/creating-and-managing-thin-provisioned-volumes_configuring-and-managing-logical-volumes Red Hat Docs > <nowiki>[9]</nowiki> > Configuring and managing LVs > '''Ch.8: Creating and managing thin provisioned volumes (thin volumes)''']'' | |||
* ''[https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/9/html/configuring_and_managing_logical_volumes/enabling-caching-to-improve-logical-volume-performance_configuring-and-managing-logical-volumes#doc-wrapper Red Hat Docs > <nowiki>[9]</nowiki> > Configuring and managing LVs > '''Ch.9: Enabling caching to improve logical volume performance''']'' | |||
==References== | |||
* Red Hat Docs: <u>[https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/9/html/configuring_and_managing_logical_volumes/index#doc-wrapper Red Hat > '''<nowiki>[9]</nowiki>''' > '''Configuring and managing | *Red Hat Docs: <u>[https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/9/html/configuring_and_managing_logical_volumes/index#doc-wrapper Red Hat > '''<nowiki>[9]</nowiki>''' > '''Configuring and managing LVs''']</u> | ||
* Learn Linux TV: [https://www.learnlinux.tv/linux-logical-volume-manager-lvm-deep-dive-tutorial/ '''Linux Logical Volume Manager (LVM) Deep Dive Tutorial'''] | *Learn Linux TV: [https://www.learnlinux.tv/linux-logical-volume-manager-lvm-deep-dive-tutorial/ '''Linux Logical Volume Manager (LVM) Deep Dive Tutorial'''] | ||
* Learn Linux TV at YouTube: [https://youtu.be/MeltFN-bXrQ Linux Logical Volume Manager (LVM) Deep Dive Tutorial] | *Learn Linux TV at YouTube: [https://youtu.be/MeltFN-bXrQ Linux Logical Volume Manager (LVM) Deep Dive Tutorial] | ||
* Linux Hint: [https://linuxhint.com/lvm-list-selectively-remove-snapshots/ LVM: How to List and Selectively Remove Snapshots] | *Linux Hint: [https://linuxhint.com/lvm-list-selectively-remove-snapshots/ LVM: How to List and Selectively Remove Snapshots] | ||
* DigitalOcean: [https://www.digitalocean.com/community/tutorials/how-to-use-lvm-to-manage-storage-devices-on-ubuntu-18-04 How To Use LVM To Manage Storage Devices on Ubuntu 18.04] | *DigitalOcean: [https://www.digitalocean.com/community/tutorials/how-to-use-lvm-to-manage-storage-devices-on-ubuntu-18-04 How To Use LVM To Manage Storage Devices on Ubuntu 18.04] | ||
* Ubuntu Community Wiki: [https://help.ubuntu.com/community/UbuntuDesktopLVM '''Ubuntu Desktop LVM'''] | *Ubuntu Community Wiki: [https://help.ubuntu.com/community/UbuntuDesktopLVM '''Ubuntu Desktop LVM'''] | ||
* SleepLessBeastie's notes: [https://sleeplessbeastie.eu/2021/02/26/how-to-fix-device-excluded-by-a-filter/ '''How to fix device excluded by a filter'''] | *SleepLessBeastie's notes: [https://sleeplessbeastie.eu/2021/02/26/how-to-fix-device-excluded-by-a-filter/ '''How to fix device excluded by a filter'''] | ||
<noinclude> | <noinclude> | ||
Line 355: | Line 517: | ||
| Прндл = Linux Server | | Прндл = Linux Server | ||
| Прндл1 = Linux Desktop | | Прндл1 = Linux Desktop | ||
| Стадий = | | Стадий = 6 | ||
| Фаза = | | Фаза = Утвърждаване | ||
| Статус = | | Статус = Утвърден | ||
| ИдтПт = Spas | | ИдтПт = Spas | ||
| РзбПт = {{REVISIONUSER}} | | РзбПт = Spas | ||
| АвтПт = Spas | |||
| УтвПт = {{REVISIONUSER}} | |||
| ИдтДт = 2.07.2022 | | ИдтДт = 2.07.2022 | ||
| РзбДт = {{Today}} | | РзбДт = 24.05.2023 | ||
| АвтДт = 24.05.2023 | |||
| УтвДт = {{Today}} | |||
| ИдтРв = [[Special:Permalink/27032|27032]] | | ИдтРв = [[Special:Permalink/27032|27032]] | ||
| РзбРв = {{REVISIONID}} | | РзбРв = [[Special:Permalink/32575|32575]] | ||
| АвтРв = [[Special:Permalink/32576|32576]] | |||
| УтвРв = {{REVISIONID}} | |||
}} | }} | ||
</div> | </div> | ||
</noinclude> | </noinclude> |
Latest revision as of 12:35, 4 February 2024
In the following sections is provided information about the most used LVM related operations and basic concepts. There are much more capabilities of the Logical Volume Manager – you can get familiar with them by reading the main source of this article Red Hat Docs > [9] > Configuring and managing logical volumes .
Overview of Logical Volume Management (LVM)
Logical volume management (LVM) creates a layer of abstraction over physical storage, which helps you to create logical storage volumes. This provides much greater flexibility in a number of ways than using physical storage directly.
In addition, the hardware storage configuration is hidden from the software so it can be resized and moved without stopping applications or unmounting file systems. This can reduce operational costs.
The following are the components of LVM – they are illustrated on the diagram shown at Figure 1:
- Physical volume: A physical volume (PV) is a partition or whole disk designated for LVM use. For more information, see Managing LVM physical volumes.
- Volume group: A volume group (VG) is a collection of physical volumes (PVs), which creates a pool of disk space out of which logical volumes can be allocated. For more information, see Managing LVM volume groups.
- Logical volume: A logical volume represents a mountable storage device. For more information, see Managing LVM logical volumes.
Requirements: the package lvm2
must be installed.
apt install lvm2
Physical Volumes (PV)
The physical volume (PV) is a partition or whole disk designated for LVM use. To use the device for an LVM logical volume, the device must be initialized as a physical volume.
If 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. If you are using a whole disk device for your physical volume, the disk must have no partition table. Any existing partition table must be erased, which will effectively destroy all data on that disk. You can remove an existing partition table using the wipefs ‑a <PhysicalVolume>`
command as root.
Display PV
pvdisplay
– Display various attributes of physical volume(s). pvdisplay
shows the attributes of PVs, like size, physical extent size, space used for the VG descriptor area, etc.
pvdisplay
--- Physical volume ---
PV Name /dev/nvme0n1p3
VG Name vg_name
PV Size <930.54 GiB / not usable 4.00 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 238216
Free PE 155272
Allocated PE 82944
PV UUID BKK3Cm-CAzY-rNah-o3Ox-L7FY-aNIY-ysUSIL
pvs
– Display information about physical volumes. pvs
is a preferred alternative of pvdisplay
that shows the same information and more, using a more compact and configurable output format.
pvs
PV VG Fmt Attr PSize PFree
/dev/nvme0n1p3 vg_name lvm2 a-- 930.53g 626.53g
The command lsblk
also outputs useful information about PV-VG-LV relations.
lsblk -M
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
nvme0n1 259:0 0 931.5G 0 disk
├─nvme0n1p1 259:1 0 512M 0 part /boot/efi
├─nvme0n1p2 259:2 0 488M 0 part /boot
└─nvme0n1p3 259:3 0 930.5G 0 part
┌─> ├─vg-lv_1-real 254:0 0 60G 0 lvm
│ │ └─vg-lv_1 254:1 0 60G 0 lvm /
┌─>│ ├─vg-lv_2-real 254:2 0 60G 0 lvm
│ │ │ └─vg-lv_2 254:7 0 60G 0 lvm /home
└┬>│ ├─vg-lv_2_ss_at_date_220912-cow 254:3 0 60G 0 lvm
│ └┬> ├─vg-lv_1_ss_at_date_220908-cow 254:4 0 60G 0 lvm
│ │ └─vg-swap_1 254:6 0 4G 0 lvm [SWAP]
│ └──vg-lv_1_ss_at_date_220908 254:5 0 60G 0 lvm
└─────vg-lv_2_ss_at_date_220912 254:9 0 60G 0 lvm
pvscan
– List all physical volumes. The pvscan
command scans all supported LVM block devices in the system for physical volumes. You can define a filter in the lvm.conf
file so that this command avoids scanning specific physical volumes.
pvscan
PV /dev/nvme0n1p3 VG vg_name lvm2 [930.53 GiB / 566.53 GiB free]
Total: 1 [930.53 GiB] / in use: 1 [930.53 GiB] / in no VG: 0 [0 ]
Create PV
The physical volume could be created on the entire drive or on a partition. It is preferable to partition the disk – create a single partition that covers the whole disk to label as an LVM physical volume, – otherwise some tools like CloneZilla wont handle the drive correctly when LVM is installed directly on the drive (without partitioning), etc.. How to partition the disk from the CLI is described in the article Linux Basic Partitioning.
Create Physicals volume by the partition /dev/nvme0n1p3
.
pvcreate /dev/nvme0n1p3
Physical volume "/dev/nvme0n1p3" successfully created.
Another example: Create Physicals volume by the partition /dev/vda1
and /dev/vdb1
.
pvcreate /dev/vda1 /dev/vdb1
Physical volume "/dev/vda1" successfully created.
Physical volume "/dev/vdb1" successfully created.
Remove PV
If a device is no longer required for use by LVM, you can remove the LVM label by using the pvremove
command. Executing the pvremove
command zeroes the LVM metadata on an empty physical volume.
pvremove /dev/nvme0n2p1
Labels on physical volume "/dev/nvme0n2p1" successfully wiped.
Then use pvs
and verify if the required volume is removed. If the physical volume you want to remove is currently part of a volume group, you must remove it from the volume group with the vgreduce
command. For more information, see Removing physical volumes from a volume group.
Volume Groups (VG)
A volume group (VG) is a collection of physical volumes (PVs), which creates a pool of disk space out of which logical volumes (LVs) can be allocated.
Within a volume group, the disk space available for allocation is divided into units of a fixed-size called extents. An extent is the smallest unit of space that can be allocated. Within a physical volume, extents are referred to as physical extents.
A logical volume is allocated into logical extents of the same size as the physical extents. The extent size is thus the same for all logical volumes in the volume group. The volume group maps the logical extents to physical extents.
Display VG
vgdisplay
– Display volume group information. The vgdisplay
command displays volume group properties such as size, extents, number of physical volumes, and other options in a fixed form. If you do not specify a volume group, all existing volume groups are displayed.
vgdisplay
--- Volume group ---
VG Name vg_name
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 19
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 6
Open LV 4
Max PV 0
Cur PV 1
Act PV 1
VG Size 930.53 GiB
PE Size 4.00 MiB
Total PE 238216
Alloc PE / Size 93184 / 364.00 GiB
Free PE / Size 145032 / 566.53 GiB
VG UUID NWWfr6-xxfN-0d22-muPn-Rgqj-vAcA-z5P5jV
The following example shows the output of the vgdisplay
command for the volume group vg_name
.
vgdisplay vg_name
vgs
– Display information about volume groups. It is a preferred alternative of vgdisplay
that shows the same information and more, using a more compact and configurable output format.
vgs
VG #PV #LV #SN Attr VSize VFree
vg_name 1 6 2 wz--n- 930.53g 566.53g
vgscan
– Search for all volume groups. vgscan
scans all supported LVM block devices in the system for VGs.
vgscan
Found volume group "vg_name" using metadata type lvm2
Create VG
Create Volume group named vg_name
by using the /dev/nvme0n1p3
Physical volume.
vgcreate vg_name /dev/nvme0n1p3
Volume group "vg_name" successfully created.
Another example: Create Volume group named another_vg
by using /dev/vda1
and /dev/vdb1
Physical volumes.
vgcreate another_vg /dev/vda1 /dev/vdb1
Volume group "another_vg" successfully created.
Another example: Create a new physical volume and then a new volume group on newly attached block device – in this case /dev/sdc
.
lsblk # check
pvcreat /dev/sdbc # crate phisical volume
vgcreate vg_name /dev/sdbc # create volume group
vgdisplay # check
Extend VG
vgextend
– Add physical volumes to a volume group. vgextend
adds one or more PVs to a VG. This increases the space available for LVs in the VG. Also, PVs that have gone missing and then returned, e.g. due to a transient device failure, can be added back to the VG without re-initializing them (see --restoremissing
).
If the specified PVs have not yet been initialized with pvcreate
, vgextend
will initialize them. In this case pvcreate
options can be used, e.g. --labelsector
, --metadatasize
, --metadataignore
, --pvmetadatacopies
, --dataalignment
, --dataalignmentoffset
.
Example 1: Occupy a partition that is not yet defined as Physical volume.
vgextend vg_name /dev/nvme0n2p1
Physical volume "/dev/nvme0n2p1" successfully created.
Volume group "vg_name" successfully extended.
Example 2: Create a new Physical volume and then extend an existing Volume group named another_vg
.
lsblk # Check the devices
pvcreat /dev/sdb # Create a Phisical volume at /dev/sdb
pvdisplay # Check the list of the Phisical volumes
vgextend another_vg /dev/sdb # Extend the existing VG to occupy the new PV
df -hT # Check the list of the available devices
vgdisplay # Check the VGs information
Merge VG: Combine VGs
vgmerge
– Merge 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.
Merge the inactive volume group another_vg
into the active or inactive volume group vg_name
giving verbose runtime information.
vgmerge -v vg_name another_vg
Reduce VG: Remove PVs from a VG
vgreduce
– Remove physical volume(s) from a volume group. 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.
1. If the Physical volume is still being used, migrate the data to another Physical volume from the same Volume group by pvmove
.
pvmove /dev/vdb3
/dev/vdb3: Moved: 2.0%
...v/vdb3: Moved: 79...
/dev/vdb3: Moved: 100.0%
2. If there are no enough free extents on the other physical volumes in the existing volume group.
pvcreate /dev/vdb4 # Create a new physical volume from /dev/vdb4
vgextend vg_name /dev/vdb4 # Add the newly created PV to the vg_name VG
pvmove /dev/vdb3 /dev/vdb4 # Move the data from /dev/vdb3 to /dev/vdb4
3. Remove the physical volume /dev/vdb3
from the volume group.
pvmove /dev/vdb3
4. Verify if the /dev/vdb3
physical volume is removed from the vg_name
volume group by the help of pvs
.
Split VG
vgsplit
– Move physical volumes into a new or existing volume group. More details at Red Hat's manual – section Splitting a LVM volume group.
vgsplit myvg yourvg /dev/vdb3
Volume group "yourvg" successfully split from "myvg"
Rename VG
vgrename
– Rename a volume group. vgchange
– Change volume group attributes.
vgchange --activate n vg_name # Deactivate the volume group
vgrename vg_name new_vg_name # Rename an existing volume group
vgrename /dev/vg_name /dev/new_vg_name # Rename by specifying the full paths to the devices
Rename volume group by UUID: This is useful when you have attached to the system a device that have a VG with the same name as one already existing. Use vgdisplay
to identify the target volume group and its UUID.
vgdisplay
vgrename ZTIHAQ-rFck-3IEK-V2mx-mckk-R1zJ-nNKWox old_pve
WARNING: VG name "pve" is used by VGs "ZTIHAQ-rFck-3IEK-V2mx-mckk-R1zJ-nNKWox" and "hXgkkq-MdYE-dobE-VPrC-0c6f-p5Oi-L4XEZu".
Fix duplicate VG names with vgrename uuid, a device filter, or system IDs.
Processing VG pve because of matching UUID ZTIHAQ-rFck-3IEK-V2mx-mckk-R1zJ-nNKWox
Volume group "ZTIHAQ-rFck-3IEK-V2mx-mckk-R1zJ-nNKWox" successfully renamed to "old_pve"
Remove VG
vgremove
– Remove volume group(s). vgremove
removes one or more VGs. If LVs exist in the VG, a prompt is used to confirm LV removal.
If one or more PVs in the VG are lost, consider vgreduce --removemissing
to make the VG metadata consistent again. Repeat the force option (-ff
) to forcibly remove LVs in the VG without confirmation.
Logical Volumes (LV)
- Source: Red Hat Docs > [9] > Configuring and managing LVs > Ch.4: Managing LVM LV
- Source: Red Hat Docs > [9] > Configuring and managing LVs > Ch.5: Modifying the size of a LV
A logical volume is a virtual, block storage device that a file system, database, or application can use. To create an LVM logical volume, the physical volumes (PVs) are combined into a volume group (VG). This creates a pool of disk space out of which LVM logical volumes (LVs) can be allocated.
An administrator can grow or shrink logical volumes without destroying data, unlike standard disk partitions. If the physical volumes in a volume group are on separate drives or RAID arrays, then administrators can also spread a logical volume across the storage devices.
The following are the different types of logical volumes (more details):
- Linear volumes
- Striped logical volumes
- RAID logical volumes
- Thin-provisioned logical volumes (thin volumes) – Using thin-provisioned logical volumes, you can create logical volumes that are larger than the available physical storage…
- Snapshot volumes
- Thin-provisioned snapshot volumes
- Cache volumes
Create LV
lvcreate – Create LVM logical volume at lvm-vm-group
:
lvcreate -n vm-win-01 -L 60g lvm-vm-group
Logical volume "vm-win-01" created.
vm-win-01
is the name of the logical volume (device), it is mater of your choice.
Check the result:
lsblk | grep -P 'sdb|lvm'
├─sdb1 8:17 0 1G 0 part
└─sdb2 8:18 0 231.9G 0 part
└─lvm--vm--group-vm--win--01 253:0 0 60G 0 lvm
Another example: For Recovering PVE from grub "disk not found" error when booting from LVM by creating 4MiB LV.
vgscan
lvcreate -L 4M pve -n grubtemp
- Assume you have
pve
volume group. grubtemp
is the name of the new logical volume (LV).
Another example: Create a new logical volume and format it as Ext4, and mount it permanently via /etc/fstab
by its UUID.
lvcreate vg_name -L 50G -n lv_name # create 50GB logical volume
lvdisplay # check
mkfs.ext4 /dev/mapper/vg_name-lv_name # format the logical folume as Ext4
mount /dev/mapper/vg_name-lv_name /mnt/volume # Mount it manually
df -h /mnt/volume # Check
umount /mnt/volume
blkid /dev/mapper/vg_name-lv_name
/dev/mapper/vg_name-lv_name: UUID="6cb60ea8-d45e..." BLOCK_SIZE="4096" TYPE="ext4"
nano /etc/fstab
UUID=6cb60ea8-d4... /mnt/volume ext4 defaults 0 2
Another example: Create a new logical volume and format it as Swap, and mount it permanently via /etc/fstab
. by vg_name/lv_name
.
lvcreate vg_name -L 16G -n swap_1 # create 16GB logical volume
lvdisplay # check
mkswap /dev/mapper/vg_name-swap_1 # format the logical folume as Swap
swapon /dev/mapper/vg_name-swap_1
nano /etc/fstab
/dev/vg_name/swap_1 none swap sw 0 0
Rename LV
lvrename
– Rename a logical volume.
umount /mnt/my-lv-mount-point
lvrename my-volume-group my-lv-prev my-lv-new
Alternatively you can rename LV by its full path.
lvrename /dev/my-volume-group/my-lv-prev /dev/my-volume-group/my-lv-new
Remove LV
lvremove
– Remove logical volume(s) from the system.
If the logical volume is currently mounted, unmount the volume.
umount /mnt/my-lv-name-mount-point
If the logical volume exists in a clustered environment, deactivate the logical volume on all nodes where it is active. Use the following command on each such node.
lvchange --activate n my-volume-group/my-lv-name
Remove the logical volume using the lvremove
utility.
lvremove /dev/my-volume-group/my-lv-name
Do you really want to remove active logical volume my-volume-group/my-lv-name? [y/n]: y
Logical volume "my-lv-name" successfully removed
Resize LV (extend|reduce)
lvresize
– Resize a logical volume. lvresize
resizes an LV in the same way as lvextend
and lvreduce
.
Example 0: Resize (Extend) an LV by 16MB using specific physical extents.
lvresize -L+16M vg1/lv1 /dev/sda:0-1 /dev/sdb:0-1
Example 1: Extend a logical volume by 10GiB and resize the file-system manually. (note there mus have enough free space in the volume group where the existing LV is located.
lvextend -L +10G /dev/mapper/vg_name-lv_name
rsize2fs /dev/mapper/vg_name-lv_name
df -h # To see the changes if the LV is mounted
Example 2: Extend a logical volume, occupy 100% of the available space in the VG and resize the file-system in the same time.
lvextend --reziefs -l +100%FREE /dev/mapper/vg_name-lv_name
df -h # To see the changes if the LV is mounted
Example 3: Reduce (Resize|Shrink) the LV and its file-system (from larger size) to exactly 60GiB. Note this may be dangerous an you may need to take a backup of the file-system before do it.
lvreduce --resizefs -L 60G vg_name/lv_name
df -h # To see the changes if the LV is mounted
Snapshots of Logical Volumes
Using the LVM snapshot feature, you can create virtual images of a volume, for example, /dev/sda, at a particular instant without causing a service interruption.
When you modify the original volume (the origin) after you take a snapshot, the snapshot feature makes a copy of the modified data area as it was prior to the change so that it can reconstruct the state of the volume. When you create a snapshot, full read and write access to the origin stays possible.…
Create a Snapshot
lvcreate
– Create a logical volume.
-s
,--snapshot
– Create a snapshot. Snapshots provide a "frozen image" of an origin LV. The snapshot LV can be used, e.g. for backups, while the origin LV continues to be used. This option can create a COW (copy on write) snapshot, or a thin snapshot (in a thin pool.) Thin snapshots are created when the origin is a thin LV and the size option is NOT specified.-n
,--name
– Specifies the name of a new LV. When unspecified, a default name oflvol#
is generated, where#
is a number generated by LVM.
Create 5GB snapshot of a logical volume named lv_name
from volume group named vg_name
.
lvcreate /dev/mapper/vg_name-lv_name -L 5G -s -n lv_name_ss_at_date_$(date +%y%m%d)
lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
lv_name vg_name owi-aos--- 60.00g
lv_name_ss_at_date_220908 vg_name swi-a-s--- 5.00g lv_name 0.01
Mount a Snapshot
The snapshot could be mounted in order to fetch files in their snapshot state. In this case it is good to mount them in read only mode.
mkdir /mnt/snapshot
mount -r /dev/mapper/vg_name-lv_name_ss_at_date_220908 /mnt/snapshot
You can use a mounted snapshot (of the root filesystem) to create a backup while the OS is running:
- DevConnected: LVM Snapshots Backup and Restore on Linux
Restore a Snapshot
Note after the restoring of a snapshot with merge option in use the snapshot will be removed, so you must create another snapshot for a later use of the same state of the logical volume.
For restoring a snapshot we will need to use the commands lvconvert
and lvchange
.
LvConvert: Convert-merge the snapshot with the source LV
lvconvert
– Change logical volume layout.
--merge
– An alias for--mergethin
,--mergemirrors
, or--mergesnapshot
, depending on the type of LV (logical volume).--mergesnapshot
– Merge COW snapshot LV into its origin. When merging a snapshot, if both the origin and snapshot LVs are not open, the merge will start immediately. Otherwise, the merge will start the first time either the origin or snapshot LV are activated and both are closed. Merging a snapshot into an origin that cannot be closed, for example a root filesystem, is deferred until the next time the origin volume is activated. When merging starts, the resulting LV will have the origin's name, minor number and UUID. While the merge is in progress, reads or writes to the origin appear as being directed to the snapshot being merged. When the merge finishes, the merged snapshot is removed. Multiple snapshots may be specified on the command line or a @tag may be used to specify multiple snapshots be merged to their respective origin.
umount /lv_name-mountpoint
lvconvert --merge /dev/mapper/vg_name-lv_name_ss_at_date_220908
If it is a root filesystem, and the source logical volume is not operational at all, we need to boot in live Linux session in order to perform the steps, because it must be unmounted. It it is not a root filesystem, we need to unmount the logical volume first.
LvChange: Deactivate, reactivate and remount the LV
lvchange
– Change the attributes of logical volume(s).
-a|
,--activate y|n|ay
– Change the active state of LVs. An active LV can be used through a block device, allowing data on the LV to be accessed.y
makes LVs active, or available.n
makes LVs inactive, or unavailable…
lvchange -an /dev/mapper/vg_name-lv_name
lvchange -ay /dev/mapper/vg_name-lv_name
lvchange -ay /dev/mapper/vg_name-lv_name -K
If we are working within a live Linux session – i.e. we are restoring a root filesystem, – these steps will be performed at the next reboot. Otherwise we need to run the following steps in order to apply the changes without reboot.
mount -a # remount via /etc/fstab
df -h # list the mounted devices
Remove a Snapshot
lvremove
– Remove logical volume(s) from the system.
lvremove /dev/vg_name/lv_name_ss_at_date_220908
lvremove /dev/mapper/vg_name-lv_name_ss_at_date_220908
Thin Volumes, Enable Caching and Create Methods…
References:
- Red Hat Docs > [9] > Configuring and managing LVs > Ch.8: Creating and managing thin provisioned volumes (thin volumes)
- Red Hat Docs > [9] > Configuring and managing LVs > Ch.9: Enabling caching to improve logical volume performance
References
- Red Hat Docs: Red Hat > [9] > Configuring and managing LVs
- Learn Linux TV: Linux Logical Volume Manager (LVM) Deep Dive Tutorial
- Learn Linux TV at YouTube: Linux Logical Volume Manager (LVM) Deep Dive Tutorial
- Linux Hint: LVM: How to List and Selectively Remove Snapshots
- DigitalOcean: How To Use LVM To Manage Storage Devices on Ubuntu 18.04
- Ubuntu Community Wiki: Ubuntu Desktop LVM
- SleepLessBeastie's notes: How to fix device excluded by a filter