LVM Basic Operations: Difference between revisions

From WikiMLT
mNo edit summary
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 LVs > Ch.1: Overview 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 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.
Requirements: the package <code>'''''lvm2'''''</code> must be installed.


Line 27: Line 28:
</syntaxhighlight>
</syntaxhighlight>


== Physical Volumes (PV) ==
==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]'''''
*'''''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.
Line 35: Line 36:
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="root-prompt code-continue">
<syntaxhighlight lang="shell" line="1" class="root-prompt code-continue">
sudo pvdisplay
pvdisplay
</syntaxhighlight>
</syntaxhighlight>
<syntaxhighlight lang="shell-session">
<syntaxhighlight lang="shell-session">
Line 54: Line 55:
'''<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="root-prompt code-continue">
<syntaxhighlight lang="shell" line="1" class="root-prompt code-continue">
sudo pvs
pvs
</syntaxhighlight>
</syntaxhighlight>
<syntaxhighlight lang="shell-session">
<syntaxhighlight lang="shell-session">
Line 84: Line 85:


<syntaxhighlight lang="shell" line="1" class="root-prompt">
<syntaxhighlight lang="shell" line="1" class="root-prompt">
sudo pvscan
pvscan
</syntaxhighlight>
</syntaxhighlight>


Line 92: 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" class="root-prompt">
<syntaxhighlight lang="shell" line="1" class="root-prompt">
sudo pvcreate /dev/nvme0n1p3
pvcreate /dev/nvme0n1p3
</syntaxhighlight>
</syntaxhighlight>


Line 105: Line 106:
</syntaxhighlight>Another example: Create Physicals volume by the partition <code>/dev/vda1</code> and <code>/dev/vdb1</code>.
</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">
<syntaxhighlight lang="shell" line="1" class="root-prompt code-continue mlw-shell-gray">
sudo pvcreate /dev/vda1 /dev/vdb1
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 111: 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.
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">
<syntaxhighlight lang="shell" line="1" class="root-prompt">
sudo pvremove /dev/nvme0n2p1
pvremove /dev/nvme0n2p1
</syntaxhighlight>
</syntaxhighlight>


Line 121: 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]'''''
*'''''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 131: 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.
<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 lang="shell" line="1" class="root-prompt code-continue">
sudo vgdisplay
vgdisplay
</syntaxhighlight>
</syntaxhighlight>
<syntaxhighlight lang="shell-session">
<syntaxhighlight lang="shell-session">
Line 159: Line 160:


</syntaxhighlight>The following example shows the output of the <code>vgdisplay</code> command for the volume group ''<code>vg_name</code>''.
</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 lang="shell" line="1" class="root-prompt code-continue mlw-shell-gray">
sudo vgdisplay vg_name
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><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">
<syntaxhighlight lang="shell" line="1" class="root-prompt code-continue">
sudo vgs
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>'''<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">
<syntaxhighlight lang="shell" line="1" class="root-prompt code-continue">
sudo vgscan
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.
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">
<syntaxhighlight lang="shell" line="1" class="root-prompt code-continue">
sudo vgcreate vg_name /dev/nvme0n1p3
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>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">
<syntaxhighlight lang="shell" line="1" class="root-prompt code-continue mlw-shell-gray">
sudo vgcreate another_vg /dev/vda1 /dev/vdb1
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>
</syntaxhighlight>


=== Extend VG ===
===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&shy;missing</code>).
'''<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&shy;missing</code>).


Line 192: Line 193:


Example 1: Occupy a partition that is not yet defined as Physical volume.<syntaxhighlight lang="shell" line="1" class="root-prompt code-continue">
Example 1: Occupy a partition that is not yet defined as Physical volume.<syntaxhighlight lang="shell" line="1" class="root-prompt code-continue">
sudo vgextend vg_name /dev/nvme0n2p1
vgextend vg_name /dev/nvme0n2p1
</syntaxhighlight><syntaxhighlight lang="shell-session">
</syntaxhighlight><syntaxhighlight lang="shell-session">
Physical volume "/dev/nvme0n2p1" successfully created.
Physical volume "/dev/nvme0n2p1" successfully created.
Line 201: Line 202:
<syntaxhighlight lang="shell" line="1" class="root-prompt code-continue mlw-shell-gray">
<syntaxhighlight lang="shell" line="1" class="root-prompt code-continue mlw-shell-gray">
lsblk # Check the devices
lsblk # Check the devices
sudo pvcreat /dev/sdb     # Create a Phisical volume at /dev/sdb
pvcreat /dev/sdb     # Create a Phisical volume at /dev/sdb
sudo pvdisplay # Check the list of the Phisical volumes
pvdisplay # Check the list of the Phisical volumes
</syntaxhighlight>
</syntaxhighlight>
<syntaxhighlight lang="shell" line="1" class="root-prompt code-continue">
<syntaxhighlight lang="shell" line="1" class="root-prompt code-continue">
sudo vgextend another_vg /dev/sdb # Extend the existing VG to occupy the new PV
vgextend another_vg /dev/sdb # Extend the existing VG to occupy the new PV
</syntaxhighlight>
</syntaxhighlight>
<syntaxhighlight lang="shell" line="1" class="root-prompt code-continue mlw-shell-gray">
<syntaxhighlight lang="shell" line="1" class="root-prompt code-continue mlw-shell-gray">
df -hT # Check the list of the available devices
df -hT # Check the list of the available devices
sudo vgdisplay # Check the VGs information
vgdisplay # Check the VGs information
</syntaxhighlight>
</syntaxhighlight>


=== Merge VG: Combine VGs ===
===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.
<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.


Merge the inactive volume group <code>another_vg</code> into the active or inactive volume group <code>vg_name</code> giving verbose runtime information.
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">
<syntaxhighlight lang="shell" line="1" class="root-prompt code-continue">
sudo vgmerge -v vg_name another_vg
vgmerge -v vg_name another_vg
</syntaxhighlight>
</syntaxhighlight>
=== Reduce VG: Remove PVs from a VG ===
===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.
'''<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">
'''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">
sudo pvmove /dev/vdb3
pvmove /dev/vdb3
</syntaxhighlight><syntaxhighlight lang="shell-session">
</syntaxhighlight><syntaxhighlight lang="shell-session">
/dev/vdb3: Moved: 2.0%
/dev/vdb3: Moved: 2.0%
Line 230: Line 231:
</syntaxhighlight>'''2.''' If there are no enough free extents on the other physical volumes in the existing volume group.
</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">
<syntaxhighlight lang="shell" line="1" class="root-prompt code-continue mlw-shell-gray">
sudo pvcreate /dev/vdb4          # Create a new physical volume from /dev/vdb4
pvcreate /dev/vdb4          # Create a new physical volume from /dev/vdb4
sudo vgextend vg_name /dev/vdb4  # Add the newly created PV to the vg_name VG
vgextend vg_name /dev/vdb4  # Add the newly created PV to the vg_name VG
sudo pvmove /dev/vdb3 /dev/vdb4  # Move the data from /dev/vdb3 to /dev/vdb4
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>'''3.''' Remove the physical volume <code>/dev/vdb3</code> from the volume group.
<syntaxhighlight lang="shell" line="1" class="root-prompt code-continue">
<syntaxhighlight lang="shell" line="1" class="root-prompt code-continue">
sudo pvmove /dev/vdb3
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>.
</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 ===
===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].
<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">
<syntaxhighlight lang="shell" line="1" class="root-prompt code-continue">
sudo vgsplit myvg yourvg /dev/vdb3
vgsplit myvg yourvg /dev/vdb3
</syntaxhighlight><syntaxhighlight lang="shell-session">
</syntaxhighlight><syntaxhighlight lang="shell-session">
Volume group "yourvg" successfully split from "myvg"
Volume group "yourvg" successfully split from "myvg"
</syntaxhighlight>
</syntaxhighlight>


=== Rename VG ===
=== 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.  
<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">
<syntaxhighlight lang="shell" line="1" class="root-prompt code-continue">
sudo vgchange --activate n vg_name            # Deactivate the volume group
vgchange --activate n vg_name            # Deactivate the volume group
</syntaxhighlight>
</syntaxhighlight>
<syntaxhighlight lang="shell" line="1" class="root-prompt code-continue">
<syntaxhighlight lang="shell" line="1" class="root-prompt code-continue">
sudo vgrename vg_name new_vg_name            # Rename an existing volume group
vgrename vg_name new_vg_name            # Rename an existing volume group
</syntaxhighlight>
</syntaxhighlight>
<syntaxhighlight lang="shell" line="1" class="root-prompt code-continue mlw-shell-gray">
<syntaxhighlight lang="shell" line="1" class="root-prompt code-continue mlw-shell-gray">
sudo vgrename /dev/vg_name /dev/new_vg_name  # Rename by specifying the full paths to the devices
vgrename /dev/vg_name /dev/new_vg_name  # Rename by specifying the full paths to the devices
</syntaxhighlight>
</syntaxhighlight>


=== Remove VG ===
===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.
'''<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&shy;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.
If one or more PVs in the VG are lost, consider <code class="noTypo">vgreduce --remove&shy;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) ==
==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/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]'''''
*'''''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]'''''
* '''''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]'''''


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.
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.
Line 275: Line 275:
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]):
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
*Linear volumes
* Striped logical volumes
*Striped logical volumes
* RAID 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...
*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
*Snapshot volumes
* Thin-provisioned snapshot volumes
*Thin-provisioned snapshot volumes
* Cache volumes
*Cache volumes


=== Create LV ===
===Create LV===
[https://manpages.ubuntu.com/manpages/jammy/en/man8/lvcreate.8.html '''lvcreate'''] - Create LVM '''logical volume''' at <code>lvm-vm-group</code>:
[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">
<syntaxhighlight lang="shell" line="1" class="root-prompt">
sudo lvcreate -n vm-win-01 -L 60g lvm-vm-group
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 volume (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.
Line 308: Line 308:
</syntaxhighlight>Another example of creating 4MiB LV (Ref: [[pve:Recover_From_Grub_Failure|Recovering PVE from grub "disk not found" error when booting from LVM]]):
</syntaxhighlight>Another example of creating 4MiB LV (Ref: [[pve:Recover_From_Grub_Failure|Recovering PVE from grub "disk not found" error when booting from LVM]]):
<syntaxhighlight lang="shell" line="1" class="root-prompt">
<syntaxhighlight lang="shell" line="1" class="root-prompt">
sudo vgscan # Assume you have "pve" volume group
vgscan
sudo lvcreate -L 4M pve -n grubtemp
lvcreate -L 4M pve -n grubtemp
</syntaxhighlight>
 
*Assume you have <code>pve</code> volume group.
* <code>grubtemp</code> is the name of the new logical volume (LV).
 
===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>
</syntaxhighlight>


=== Resize LV ===
// TODO
----
----


<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
##  Extend logical volume - note there mus have enoug free space in the volume group where the existing LV is located
##  Extend logical volume - note there mus have enoug free space in the volume group where the existing LV is located
sudo lvextend -L +10G /dev/mapper/lv_name
lvextend -L +10G /dev/mapper/lv_name
sudo rsize2fs /dev/mapper/lv_name
rsize2fs /dev/mapper/lv_name
df -h
df -h
### for swap file sudo mkswap /dev/mapper/xxxx-swap_1
### for swap file mkswap /dev/mapper/xxxx-swap_1
### then edit /etc/fstab and chenge the UUID if it is mounted by it...
### then edit /etc/fstab and chenge the UUID if it is mounted by it...


## Exten to all available space and resize the FS in the same time
## Exten to all available space and resize the FS in the same time
sudo lvextend --reziefs -l +100%FREE /dev/mapper/lv_name
lvextend --reziefs -l +100%FREE /dev/mapper/lv_name
df -h
df -h


# Shrink logical volume
# Shrink logical volume
     1  lsblk
     1  lsblk
     2  sudo apt update
     2  apt update
     3  sudo apt install gnome-disk-utility
     3  apt install gnome-disk-utility
     4  ll
     4  ll
     5  lsblk
     5  lsblk
Line 335: Line 365:
     7  ll /dev/kali-x-vg/root
     7  ll /dev/kali-x-vg/root
     8  ll /dev/dm-0
     8  ll /dev/dm-0
     9  sudo lvreduce --resizefs -L 60G kali-x-vg/root
     9  lvreduce --resizefs -L 60G kali-x-vg/root
   10  lsblk
   10  lsblk
   11  df -h
   11  df -h
Line 342: Line 372:
## Create a new volume group on newly attached block device /dev/sdc
## Create a new volume group on newly attached block device /dev/sdc
lsblk # check
lsblk # check
sudo pvcreat /dev/sdbc # crrate phisical volume
pvcreat /dev/sdbc # crrate phisical volume
sudo vgcreate vg_name /dev/sdbc # create volume group
vgcreate vg_name /dev/sdbc # create volume group
sudo vgdisplay # check
vgdisplay # check


## Create a new logical volume
## Create a new logical volume
sudo lvcreate vg_name -L 5G -n lv_name # create 5GB logical volume
lvcreate vg_name -L 5G -n lv_name # create 5GB logical volume
sudo lvdisplay # check
lvdisplay # check
sudo mkfs.ext4 /dev/mapper/vg_name-lv_name # format the logical folume as Ext4
mkfs.ext4 /dev/mapper/vg_name-lv_name # format the logical folume as Ext4


# mount the new logical volume
# mount the new logical volume
sudo mkdir -p /mnt/volume # create mount point
mkdir -p /mnt/volume # create mount point
sudo mount /dev/mapper/vg_name-lv_name /mnt/volume # mount the volume
mount /dev/mapper/vg_name-lv_name /mnt/volume # mount the volume
df -h # check
df -h # check


# mount it permanently via /etc/fstab (we can munt it by using the mapped path, but it is preferable to use UUID)
# mount it permanently via /etc/fstab (we can munt it by using the mapped path, but it is preferable to use UUID)
sudo blkid /dev/mapper/vg_name-lv_name # get the UUID (universal unique identifier) of the logical volume
blkid /dev/mapper/vg_name-lv_name # get the UUID (universal unique identifier) of the logical volume
> /dev/mapper/vg_name-lv_name: UUID="b6ddc49d-...-...c90" BLOCK_SIZE="4096" TYPE="ext4"
> /dev/mapper/vg_name-lv_name: UUID="b6ddc49d-...-...c90" BLOCK_SIZE="4096" TYPE="ext4"


sudo cp /etc/fstab{,.bak} # backup the fstab file
cp /etc/fstab{,.bak} # backup the fstab file
sudo umount /mnt/volume # unmount the nel volume
umount /mnt/volume # unmount the nel volume
df -h # check
df -h # check


sudo nano /etc/fstab
nano /etc/fstab
># <file system>                            <mount point>  <type>  <options>      <dump>  <pass>  
># <file system>                            <mount point>  <type>  <options>      <dump>  <pass>  
>    UUID=b6ddc49d-...-...c90 /mnt/volume    ext4        defaults 0   2
>    UUID=b6ddc49d-...-...c90 /mnt/volume    ext4        defaults 0   2


# Test /etc/fsstab for errors, by remount everything listed inside
# Test /etc/fsstab for errors, by remount everything listed inside
sudo mount -a # no output means everything is correctly mounted
mount -a # no output means everything is correctly mounted
df -h # check
df -h # check


Line 377: Line 407:
</syntaxhighlight>
</syntaxhighlight>


== Snapshots of Logical Volumes ==
==Snapshots of Logical Volumes==


=== Create a Snapshot ===
* '''''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 ===
{{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="root-prompt code-continue">
<syntaxhighlight lang="shell" line="1" class="root-prompt code-continue">
sudo lvcreate /dev/mapper/vg_name-lv_name -L 5G -s -n lv_name_ss_at_date_$(date +%y%m%d)
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="root-prompt code-continue mlw-shell-gray">
<syntaxhighlight lang="shell" line="1" class="root-prompt code-continue mlw-shell-gray">
sudo lvs  
lvs  
</syntaxhighlight>
</syntaxhighlight>
<syntaxhighlight lang="terraform" line="1" highlight="3">
<syntaxhighlight lang="terraform" line="1" highlight="3">
Line 401: Line 438:
</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.
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 lang="shell" line="1" class="root-prompt code-continue mlw-shell-gray">
sudo mkdir /mnt/snapshot
mkdir /mnt/snapshot
</syntaxhighlight>
</syntaxhighlight>
<syntaxhighlight lang="shell" line="1" class="root-prompt code-continue">
<syntaxhighlight lang="shell" line="1" class="root-prompt code-continue">
sudo mount -r /dev/mapper/vg_name-lv_name_ss_at_date_220908 /mnt/snapshot
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.
</syntaxhighlight>You can use a mounted snapshot (of the root filesystem) to create a backup while the OS is running.


=== 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 423: Line 460:
{{collapse/end}}
{{collapse/end}}
<syntaxhighlight lang="shell" line="1" class="root-prompt code-continue mlw-shell-gray">
<syntaxhighlight lang="shell" line="1" class="root-prompt code-continue mlw-shell-gray">
sudo umount  /lv_name-mountpoint
umount  /lv_name-mountpoint
</syntaxhighlight>
</syntaxhighlight>
<syntaxhighlight lang="shell" line="1" class="root-prompt code-continue">
<syntaxhighlight lang="shell" line="1" class="root-prompt code-continue">
sudo lvconvert --merge /dev/mapper/vg_name-lv_name_ss_at_date_220908
lvconvert --merge /dev/mapper/vg_name-lv_name_ss_at_date_220908
</syntaxhighlight>
</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.
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 437: Line 474:
{{collapse/end}}
{{collapse/end}}
<syntaxhighlight lang="shell" line="1" class="root-prompt code-continue">
<syntaxhighlight lang="shell" line="1" class="root-prompt code-continue">
sudo lvchange -an /dev/mapper/vg_name-lv_name
lvchange -an /dev/mapper/vg_name-lv_name
sudo lvchange -ay /dev/mapper/vg_name-lv_name
lvchange -ay /dev/mapper/vg_name-lv_name
</syntaxhighlight>
</syntaxhighlight>
<syntaxhighlight lang="shell" line="1" class="root-prompt code-continue mlw-shell-gray">
<syntaxhighlight lang="shell" line="1" class="root-prompt code-continue mlw-shell-gray">
sudo lvchange -ay /dev/mapper/vg_name-lv_name -K
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="root-prompt code-continue">
<syntaxhighlight lang="shell" line="1" class="root-prompt code-continue">
sudo mount -a  # remount via /etc/fstab
mount -a  # remount via /etc/fstab
</syntaxhighlight>
</syntaxhighlight>
<syntaxhighlight lang="shell" line="1" class="root-prompt code-continue mlw-shell-gray">
<syntaxhighlight lang="shell" line="1" class="root-prompt code-continue mlw-shell-gray">
Line 452: Line 489:
</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.
<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 lang="shell" line="1" class="root-prompt code-continue">
sudo lvremove /dev/vg_name/lv_name_ss_at_date_220908
lvremove /dev/vg_name/lv_name_ss_at_date_220908
</syntaxhighlight>
</syntaxhighlight>
<syntaxhighlight lang="shell" line="1" class="root-prompt code-continue mlw-shell-gray">
<syntaxhighlight lang="shell" line="1" class="root-prompt code-continue mlw-shell-gray">
sudo lvremove /dev/mapper/vg_name-lv_name_ss_at_date_220908
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 logical volumes''']</u>
*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>
<div id='devStage'>
<div id="devStage">
{{devStage  
{{devStage  
  | Прндл  = Linux Server
  | Прндл  = Linux Server

Revision as of 22:34, 4 May 2023

In the fol­low­ing sec­tions is pro­vid­ed in­for­ma­tion about the most used LVM re­lat­ed op­er­a­tions and ba­sic con­cepts. There are much more ca­pa­bil­i­ties of the Log­i­cal Vol­ume Man­ag­er – you can get fa­mil­iar with them by read­ing the main source of this ar­ti­cle Red Hat Docs > [9] > Con­fig­ur­ing and man­ag­ing log­i­cal vol­umes .

Overview of Log­i­cal Vol­ume Man­age­ment (LVM)

Figure 1. LVM log­i­cal vol­ume com­po­nents.

Log­i­cal vol­ume man­age­ment (LVM) cre­ates a lay­er of ab­strac­tion over phys­i­cal stor­age, which helps you to cre­ate log­i­cal stor­age vol­umes. This pro­vides much greater flex­i­bil­i­ty in a num­ber of ways than us­ing phys­i­cal stor­age di­rect­ly.

In ad­di­tion, the hard­ware stor­age con­fig­u­ra­tion is hid­den from the soft­ware so it can be re­sized and moved with­out stop­ping ap­pli­ca­tions or un­mount­ing file sys­tems. This can re­duce op­er­a­tional costs.

The fol­low­ing are the com­po­nents of LVM – they are il­lus­trat­ed on the di­a­gram shown at Fig­ure 1:

  • Vol­ume group: A vol­ume group (VG) is a col­lec­tion of phys­i­cal vol­umes (PVs), which cre­ates a pool of disk space out of which log­i­cal vol­umes can be al­lo­cat­ed. For more in­for­ma­tion, see Man­ag­ing LVM vol­ume groups.

Re­quire­ments: the pack­age lvm2 must be in­stalled.

apt install lvm2

Phys­i­cal Vol­umes (PV)

The phys­i­cal vol­ume (PV) is a par­ti­tion or whole disk des­ig­nat­ed for LVM use. To use the de­vice for an LVM log­i­cal vol­ume, the de­vice must be ini­tial­ized as a phys­i­cal vol­ume.

If you are us­ing a whole disk de­vice for your phys­i­cal vol­ume, the disk must have no par­ti­tion ta­ble. For DOS disk par­ti­tions, the par­ti­tion id should be set to 0x8e us­ing the fdisk or cfdisk com­mand or an equiv­a­lent. If you are us­ing a whole disk de­vice for your phys­i­cal vol­ume, the disk must have no par­ti­tion ta­ble. Any ex­ist­ing par­ti­tion ta­ble must be erased, which will ef­fec­tive­ly de­stroy all da­ta on that disk. You can re­move an ex­ist­ing par­ti­tion ta­ble us­ing the wipefs ‑a <Phys­i­calVol­ume>` com­mand as root.

Dis­play PV

pvdis­play – Dis­play var­i­ous at­trib­ut­es of phys­i­cal volume(s). pvdis­play shows the at­trib­ut­es of PVs, like size, phys­i­cal ex­tent size, space used for the VG de­scrip­tor 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 – Dis­play in­for­ma­tion about phys­i­cal vol­umes. pvs is a pre­ferred al­ter­na­tive of pvdis­play that shows the same in­for­ma­tion and more, us­ing a more com­pact and con­fig­urable out­put for­mat.

pvs
PV             VG        Fmt  Attr PSize   PFree  
/dev/nvme0n1p3 vg_name   lvm2 a--  930.53g 626.53g

The com­mand ls­blk al­so out­puts use­ful in­for­ma­tion about PV-VG-LV re­la­tions.

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

pvs­can – List all phys­i­cal vol­umes. The pvs­can com­mand scans all sup­port­ed LVM block de­vices in the sys­tem for phys­i­cal vol­umes. You can de­fine a fil­ter in the lvm.conf file so that this com­mand avoids scan­ning spe­cif­ic phys­i­cal vol­umes.

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   ]

Cre­ate PV

The phys­i­cal vol­ume could be cre­at­ed on the en­tire dri­ve or on a par­ti­tion. It is prefer­able to par­ti­tion the disk – cre­ate a sin­gle par­ti­tion that cov­ers the whole disk to la­bel as an LVM phys­i­cal vol­ume, – oth­er­wise some tools like CloneZil­la wont han­dle the dri­ve cor­rect­ly when LVM is in­stalled di­rect­ly on the dri­ve (with­out par­ti­tion­ing), etc.. How to par­ti­tion the disk from the CLI is de­scribed in the ar­ti­cle Lin­ux Ba­sic Par­ti­tion­ing.

Cre­ate Phys­i­cals vol­ume by the par­ti­tion /​​​dev/​​​nvme0n1p3.

pvcreate /dev/nvme0n1p3
Physical volume "/dev/nvme0n1p3" successfully created.

An­oth­er ex­am­ple: Cre­ate Phys­i­cals vol­ume by the par­ti­tion /​​​dev/​​​vda1 and /​​​dev/​​​vdb1.

pvcreate /dev/vda1 /dev/vdb1
Physical volume "/dev/vda1" successfully created.
Physical volume "/dev/vdb1" successfully created.

Re­move PV

If a de­vice is no longer re­quired for use by LVM, you can re­move the LVM la­bel by us­ing the pvre­move com­mand. Ex­e­cut­ing the pvre­move com­mand ze­roes the LVM meta­da­ta on an emp­ty phys­i­cal vol­ume.

pvremove /dev/nvme0n2p1
Labels on physical volume "/dev/nvme0n2p1" successfully wiped.

Then use pvs and ver­i­fy if the re­quired vol­ume is re­moved. If the phys­i­cal vol­ume you want to re­move is cur­rent­ly part of a vol­ume group, you must re­move it from the vol­ume group with the vgre­duce com­mand. For more in­for­ma­tion, see Re­mov­ing phys­i­cal vol­umes from a vol­ume group.

Vol­ume Groups (VG)

A vol­ume group (VG) is a col­lec­tion of phys­i­cal vol­umes (PVs), which cre­ates a pool of disk space out of which log­i­cal vol­umes (LVs) can be al­lo­cat­ed.

With­in a vol­ume group, the disk space avail­able for al­lo­ca­tion is di­vid­ed in­to units of a fixed-size called ex­tents. An ex­tent is the small­est unit of space that can be al­lo­cat­ed. With­in a phys­i­cal vol­ume, ex­tents are re­ferred to as phys­i­cal ex­tents.

A log­i­cal vol­ume is al­lo­cat­ed in­to log­i­cal ex­tents of the same size as the phys­i­cal ex­tents. The ex­tent size is thus the same for all log­i­cal vol­umes in the vol­ume group. The vol­ume group maps the log­i­cal ex­tents to phys­i­cal ex­tents.

Dis­play VG

vgdis­play – Dis­play vol­ume group in­for­ma­tion. The vgdis­play com­mand dis­plays vol­ume group prop­er­ties such as size, ex­tents, num­ber of phys­i­cal vol­umes, and oth­er op­tions in a fixed form. If you do not spec­i­fy a vol­ume group, all ex­ist­ing vol­ume groups are dis­played.

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 fol­low­ing ex­am­ple shows the out­put of the vgdis­play com­mand for the vol­ume group vg_​​​name.

vgdisplay vg_name

vgs – Dis­play in­for­ma­tion about vol­ume groups. It is a pre­ferred al­ter­na­tive of vgdis­play that shows the same in­for­ma­tion and more, us­ing a more com­pact and con­fig­urable out­put for­mat.

vgs
VG        #PV  #LV  #SN  Attr    VSize    VFree  
vg_name     1    6    2  wz--n-  930.53g  566.53g

vgscan – Search for all vol­ume groups. vgscan scans all sup­port­ed LVM block de­vices in the sys­tem for VGs.

vgscan
Found volume group "vg_name" using metadata type lvm2

Cre­ate VG

Cre­ate Vol­ume group named vg_​​​name by us­ing the /​​​dev/​​​nvme0n1p3 Phys­i­cal vol­ume.

vgcreate vg_name /dev/nvme0n1p3
Volume group "vg_name" successfully created.

An­oth­er ex­am­ple: Cre­ate Vol­ume group named another_​​​vg by us­ing /​​​dev/​​​vda1 and /​​​dev/​​​vdb1 Phys­i­cal vol­umes.

vgcreate another_vg /dev/vda1 /dev/vdb1
Volume group "another_vg" successfully created.

Ex­tend VG

vgextend – Add phys­i­cal vol­umes to a vol­ume group. vgextend adds one or more PVs to a VG. This in­creas­es the space avail­able for LVs in the VG. Al­so, PVs that have gone miss­ing and then re­turned, e.g. due to a tran­sient de­vice fail­ure, can be added back to the VG with­out re-ini­tial­iz­ing them (see --restore­missing).

If the spec­i­fied PVs have not yet been ini­tial­ized with pvcreate, vgextend will ini­tial­ize them. In this case pvcreate op­tions can be used, e.g. --label­sector, --meta­data­size, --meta­data­ignore, --pv­meta­data­copies, --data­align­ment, --data­align­ment­offset.

Ex­am­ple 1: Oc­cu­py a par­ti­tion that is not yet de­fined as Phys­i­cal vol­ume.

vgextend vg_name /dev/nvme0n2p1
Physical volume "/dev/nvme0n2p1" successfully created.
Volume group "vg_name" successfully extended.

Ex­am­ple 2: Cre­ate a new Phys­i­cal vol­ume and then ex­tend an ex­ist­ing Vol­ume 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: Com­bine VGs

vg­merge – Merge vol­ume groups. To com­bine two vol­ume groups in­to a sin­gle vol­ume group, use the vg­merge com­mand. You can merge an in­ac­tive "source" vol­ume with an ac­tive or an in­ac­tive "des­ti­na­tion" vol­ume if the phys­i­cal ex­tent sizes of the vol­ume are equal and the phys­i­cal and log­i­cal vol­ume sum­maries of both vol­ume groups fit in­to the des­ti­na­tion vol­ume groups lim­its.

Merge the in­ac­tive vol­ume group another_​​​vg in­to the ac­tive or in­ac­tive vol­ume group vg_​​​name giv­ing ver­bose run­time in­for­ma­tion.

vgmerge -v vg_name another_vg

Re­duce VG: Re­move PVs from a VG

vgre­duce – Re­move phys­i­cal volume(s) from a vol­ume group. The vgre­duce com­mand shrinks a vol­ume group’s ca­pac­i­ty by re­mov­ing one or more emp­ty phys­i­cal vol­umes. This frees those phys­i­cal vol­umes to be used in dif­fer­ent vol­ume groups or to be re­moved from the sys­tem.

1. If the Phys­i­cal vol­ume is still be­ing used, mi­grate the da­ta to an­oth­er Phys­i­cal vol­ume from the same Vol­ume group by pv­move.

pvmove /dev/vdb3
/dev/vdb3: Moved: 2.0%
...v/vdb3: Moved: 79...
/dev/vdb3: Moved: 100.0%

2. If there are no enough free ex­tents on the oth­er phys­i­cal vol­umes in the ex­ist­ing vol­ume 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. Re­move the phys­i­cal vol­ume /​​​dev/​​​vdb3 from the vol­ume group.

pvmove /dev/vdb3

4. Ver­i­fy if the /​​​dev/​​​vdb3 phys­i­cal vol­ume is re­moved from the vg_​​​name vol­ume group by the help of pvs.

Split VG

vgsplit – Move phys­i­cal vol­umes in­to a new or ex­ist­ing vol­ume group. More de­tails at Red Hat's man­u­al – sec­tion Split­ting a LVM vol­ume group.

vgsplit myvg yourvg /dev/vdb3
Volume group "yourvg" successfully split from "myvg"

Re­name VG

vgrename – Re­name a vol­ume group. vgchange – Change vol­ume group at­trib­ut­es.

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

Re­move VG

vgre­move – Re­move vol­ume group(s). vgre­move re­moves one or more VGs. If LVs ex­ist in the VG, a prompt is used to con­firm LV re­moval.

If one or more PVs in the VG are lost, con­sid­er vgreduce --remove­missing to make the VG meta­da­ta con­sis­tent again. Re­peat the force op­tion (-ff) to forcibly re­move LVs in the VG with­out con­fir­ma­tion.

Log­i­cal Vol­umes (LV)

A log­i­cal vol­ume is a vir­tu­al, block stor­age de­vice that a file sys­tem, data­base, or ap­pli­ca­tion can use. To cre­ate an LVM log­i­cal vol­ume, the phys­i­cal vol­umes (PVs) are com­bined in­to a vol­ume group (VG). This cre­ates a pool of disk space out of which LVM log­i­cal vol­umes (LVs) can be al­lo­cat­ed.

An ad­min­is­tra­tor can grow or shrink log­i­cal vol­umes with­out de­stroy­ing da­ta, un­like stan­dard disk par­ti­tions. If the phys­i­cal vol­umes in a vol­ume group are on sep­a­rate dri­ves or RAID ar­rays, then ad­min­is­tra­tors can al­so spread a log­i­cal vol­ume across the stor­age de­vices.

The fol­low­ing are the dif­fer­ent types of log­i­cal vol­umes (more de­tails):

  • Lin­ear vol­umes
  • Striped log­i­cal vol­umes
  • RAID log­i­cal vol­umes
  • Thin-pro­vi­sioned log­i­cal vol­umes (thin vol­umes) – Us­ing thin-pro­vi­sioned log­i­cal vol­umes, you can cre­ate log­i­cal vol­umes that are larg­er than the avail­able phys­i­cal stor­age…
  • Snap­shot vol­umes
  • Thin-pro­vi­sioned snap­shot vol­umes
  • Cache vol­umes

Cre­ate LV

lvcre­ate – Cre­ate LVM log­i­cal vol­ume 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 log­i­cal vol­ume (de­vice), it is mater of your choice.

Check the re­sult:

 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

An­oth­er ex­am­ple of cre­at­ing 4MiB LV (Ref: Re­cov­er­ing PVE from grub "disk not found" er­ror when boot­ing from LVM):

vgscan
lvcreate -L 4M pve -n grubtemp
  • As­sume you have pve vol­ume group.
  • grubtemp is the name of the new log­i­cal vol­ume (LV).

Re­name LV

lvre­name – Re­name a log­i­cal vol­ume.

umount /mnt/my-lv-mount-point
lvrename my-volume-group my-lv-prev my-lv-new

Al­ter­na­tive­ly you can re­name LV by its full path.

lvrename /dev/my-volume-group/my-lv-prev /dev/my-volume-group/my-lv-new

Re­move LV

lvre­move – Re­move log­i­cal volume(s) from the sys­tem.

If the log­i­cal vol­ume is cur­rent­ly mount­ed, un­mount the vol­ume.

umount /mnt/my-lv-name-mount-point

If the log­i­cal vol­ume ex­ists in a clus­tered en­vi­ron­ment, de­ac­ti­vate the log­i­cal vol­ume on all nodes where it is ac­tive. Use the fol­low­ing com­mand on each such node.

lvchange --activate n my-volume-group/my-lv-name

Re­move the log­i­cal vol­ume us­ing the lvre­move util­i­ty.

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

Re­size LV

// TO­DO


##  Extend logical volume - note there mus have enoug free space in the volume group where the existing LV is located
lvextend -L +10G /dev/mapper/lv_name
rsize2fs /dev/mapper/lv_name
df -h
### for swap file mkswap /dev/mapper/xxxx-swap_1
### then edit /etc/fstab and chenge the UUID if it is mounted by it...

## Exten to all available space and resize the FS in the same time
lvextend --reziefs -l +100%FREE /dev/mapper/lv_name
df -h

# Shrink logical volume
    1  lsblk
    2  apt update
    3  apt install gnome-disk-utility
    4  ll
    5  lsblk
    6  ll /dev/kali-x-vg
    7  ll /dev/kali-x-vg/root
    8  ll /dev/dm-0
    9  lvreduce --resizefs -L 60G kali-x-vg/root
   10  lsblk
   11  df -h


## Create a new volume group on newly attached block device /dev/sdc
lsblk 																						# check
pvcreat /dev/sdbc						# crrate phisical volume
vgcreate vg_name /dev/sdbc				# create volume group
vgdisplay								# check

## Create a new logical volume
lvcreate vg_name -L 5G -n lv_name		# create 5GB logical volume 		
lvdisplay								# check
mkfs.ext4 /dev/mapper/vg_name-lv_name	# format the logical folume as Ext4

# mount the new logical volume
mkdir -p /mnt/volume																															# create mount point
mount /dev/mapper/vg_name-lv_name	/mnt/volume	# mount the volume
df -h																																												# check

# mount it permanently via /etc/fstab (we can munt it by using the mapped path, but it is preferable to use UUID)
blkid /dev/mapper/vg_name-lv_name			# get the UUID (universal unique identifier) of the logical volume
> /dev/mapper/vg_name-lv_name: UUID="b6ddc49d-...-...c90" BLOCK_SIZE="4096" TYPE="ext4"

cp /etc/fstab{,.bak}			# backup the fstab file
umount /mnt/volume		# unmount the nel volume
df -h														# check

nano /etc/fstab
># <file system>                            <mount point>   <type>  <options>       <dump>  <pass> 
>     UUID=b6ddc49d-...-...c90 /mnt/volume     ext4        defaults			 0				  2

# Test /etc/fsstab for errors, by remount everything listed inside
mount -a			# no output means everything is correctly mounted
df -h								# check

Snap­shots of Log­i­cal Vol­umes

Us­ing the LVM snap­shot fea­ture, you can cre­ate vir­tu­al im­ages of a vol­ume, for ex­am­ple, /​​​dev/​​​sda, at a par­tic­u­lar in­stant with­out caus­ing a ser­vice in­ter­rup­tion.

When you mod­i­fy the orig­i­nal vol­ume (the ori­gin) af­ter you take a snap­shot, the snap­shot fea­ture makes a copy of the mod­i­fied da­ta area as it was pri­or to the change so that it can re­con­struct the state of the vol­ume. When you cre­ate a snap­shot, full read and write ac­cess to the ori­gin stays pos­si­ble.…

Cre­ate a Snap­shot

lvcre­ate – Cre­ate a log­i­cal vol­ume.

#Ex­pla­na­tion
  • -s, --snapshot – Cre­ate a snap­shot. Snap­shots pro­vide a "frozen im­age" of an ori­gin LV. The snap­shot LV can be used, e.g. for back­ups, while the ori­gin LV con­tin­ues to be used. This op­tion can cre­ate a COW (copy on write) snap­shot, or a thin snap­shot (in a thin pool.) Thin snap­shots are cre­at­ed when the ori­gin is a thin LV and the size op­tion is NOT spec­i­fied.
  • -n, --name – Spec­i­fies the name of a new LV. When un­spec­i­fied, a de­fault name of lvol# is gen­er­at­ed, where # is a num­ber gen­er­at­ed by LVM.

Cre­ate 5GB snap­shot of a log­i­cal vol­ume named lv_​​​name from vol­ume 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 Snap­shot

The snap­shot could be mount­ed in or­der to fetch files in their snap­shot state. In this case it is good to mount them in read on­ly mode.

mkdir /mnt/snapshot
mount -r /dev/mapper/vg_name-lv_name_ss_at_date_220908 /mnt/snapshot

You can use a mount­ed snap­shot (of the root filesys­tem) to cre­ate a back­up while the OS is run­ning.

Re­store a Snap­shot

Note af­ter the restor­ing of a snap­shot with merge op­tion in use the snap­shot will be re­moved, so you must cre­ate an­oth­er snap­shot for a lat­er use of the same state of the log­i­cal vol­ume.

For restor­ing a snap­shot we will need to use the com­mands lv­con­vert and lvchange.

Lv­Con­vert: Con­vert-merge the snap­shot with the source LV

lv­con­vert – Change log­i­cal vol­ume lay­out.

#Ex­pla­na­tion
  • --merge – An alias for --mergethin, --mergemirrors, or --mergesnapshot, de­pend­ing on the type of LV (log­i­cal vol­ume).
  • --mergesnapshot – Merge COW snap­shot LV in­to its ori­gin. When merg­ing a snap­shot, if both the ori­gin and snap­shot LVs are not open, the merge will start im­me­di­ate­ly. Oth­er­wise, the merge will start the first time ei­ther the ori­gin or snap­shot LV are ac­ti­vat­ed and both are closed. Merg­ing a snap­shot in­to an ori­gin that can­not be closed, for ex­am­ple a root filesys­tem, is de­ferred un­til the next time the ori­gin vol­ume is ac­ti­vat­ed. When merg­ing starts, the re­sult­ing LV will have the origin's name, mi­nor num­ber and UUID. While the merge is in progress, reads or writes to the ori­gin ap­pear as be­ing di­rect­ed to the snap­shot be­ing merged. When the merge fin­ish­es, the merged snap­shot is re­moved. Mul­ti­ple snap­shots may be spec­i­fied on the com­mand line or a @tag may be used to spec­i­fy mul­ti­ple snap­shots be merged to their re­spec­tive ori­gin.
umount  /lv_name-mountpoint
lvconvert --merge /dev/mapper/vg_name-lv_name_ss_at_date_220908

If it is a root filesys­tem, and the source log­i­cal vol­ume is not op­er­a­tional at all, we need to boot in live Lin­ux ses­sion in or­der to per­form the steps, be­cause it must be un­mount­ed. It it is not a root filesys­tem, we need to un­mount the log­i­cal vol­ume first.

LvChange: De­ac­ti­vate, re­ac­ti­vate and re­mount the LV

lvchange – Change the at­trib­ut­es of log­i­cal volume(s).

#Ex.
  • -a|, --activate y|n|ay – Change the ac­tive state of LVs. An ac­tive LV can be used through a block de­vice, al­low­ing da­ta on the LV to be ac­cessed. y makes LVs ac­tive, or avail­able. n makes LVs in­ac­tive, or un­avail­able…
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 work­ing with­in a live Lin­ux ses­sion – i.e. we are restor­ing a root filesys­tem, – these steps will be per­formed at the next re­boot. Oth­er­wise we need to run the fol­low­ing steps in or­der to ap­ply the changes with­out re­boot.

mount -a   # remount via /etc/fstab
df -h           # list the mounted devices

Re­move a Snap­shot

lvre­move – Re­move log­i­cal volume(s) from the sys­tem.

lvremove /dev/vg_name/lv_name_ss_at_date_220908
lvremove /dev/mapper/vg_name-lv_name_ss_at_date_220908

Thin Vol­umes, En­able Caching and Cre­ate Meth­ods…

Ref­er­ences:

Ref­er­ences