LVM Basic Operations: Difference between revisions

From WikiMLT
Line 122: Line 122:
</syntaxhighlight>
</syntaxhighlight>


* <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. The block device for the LV is added or removed from the system using device-mapper in the kernel. A symbolic link <code>/dev/VGName/LVName</code> pointing to the device node is also added/removed. All software and scripts should access the device through the symbolic link and present this as the name of the device. The location and name of the underlying device node may depend on the distribution, configuration (e.g. <code>udev</code>), or release version. <code>ay</code> specifies auto-activation, which is used by system-generated activation commands. By default, LVs are auto-activated.  An auto activation property can be set on a VG or LV to disable auto-activation, see <code class="noTypo">--setautoactivation y|n</code> in <code>vgchange</code>, <code>lvchange</code>, <code>vgcreate</code>, and <code>lvcreate</code>. Display the property with <code>vgs</code> or <code>lvs "-o autoactivation"</code>. The lvm.conf(5) <code>auto_activation_volume_list</code> includes names of VGs or LVs that should be auto-activated, and anything not listed is not auto-activated. When <code>auto_activation_volume_list</code> is undefined (the default), it has no effect. If <code>auto_activation_volume_list</code> is defined and empty, no LVs are auto activated. Items included by <code>auto_activation_volume_list</code> will not be auto-activated if the activation property has been disabled. See lvmlockd(8) for more information about activation options <code>ey</code> and <code>sy</code> for shared VGs.
* <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.  
<syntaxhighlight lang="shell" line="1" class="mlw-continue mlw-shell-gray">
** The block device for the LV is added or removed from the system using device-mapper in the kernel. A symbolic link <code>/dev/VGName/LVName</code> pointing to the device node is also added/removed.  
sudo mount -a
** All software and scripts should access the device through the symbolic link and present this as the name of the device. The location and name of the underlying device node may depend on the distribution, configuration (e.g. <code>udev</code>), or release version.
** <code>ay</code> specifies auto-activation, which is used by system-generated activation commands. By default, LVs are auto-activated.  An auto activation property can be set on a VG or LV to disable auto-activation, see <code class="noTypo">--setautoactivation y|n</code> in <code>vgchange</code>, <code>lvchange</code>, <code>vgcreate</code>, and <code>lvcreate</code>. Display the property with <code>vgs</code> or <code>lvs "-o autoactivation"</code>.
** The ''<code>lvm.conf(5)</code>'' <code>auto_activation_volume_list</code> includes names of VGs or LVs that should be auto-activated, and anything not listed is not auto-activated. When <code>auto_activation_volume_list</code> is undefined (the default), it has no effect. If <code>auto_activation_volume_list</code> is defined and empty, no LVs are auto activated. Items included by <code>auto_activation_volume_list</code> will not be auto-activated if the activation property has been disabled. See ''<code>lvmlockd(8)</code>'' for more information about activation options <code>ey</code> and <code>sy</code> for shared VGs.
<syntaxhighlight lang="shell" line="1" class="mlw-continue">
sudo mount -a  # remount via /etc/fstab
</syntaxhighlight><syntaxhighlight lang="shell" line="1" class="mlw-continue mlw-shell-gray">
df -h          # list the mounted devices
</syntaxhighlight><syntaxhighlight lang="shell">
</syntaxhighlight><syntaxhighlight lang="shell">


Line 131: Line 137:
# Deactivate and reactivate the logical volume
# Deactivate and reactivate the logical volume


# Remount via /etc/fstab


df -h
 
 


# --
# --

Revision as of 09:53, 12 September 2022

#### https://www.youtube.com/watch?v=MeltFN-bXrQ&t=2090s
# `pvdisplay` Display various attributes of physical volume(s). Shows the attributes of PVs, like size, physical extent size, space used for the VG descriptor area, etc.
# `pvs` is a preferred alternative that shows the same information and more, using a more compact and configurable output format.
sudo pvdisplay

# `lvdisplay` - Display information about a logical volume. Shows the attributes of LVs, like size, read/write status, snapshot information, etc.
# `lvs` is a preferred alternative that shows the same information and more, using a more compact and configurable output format.

## Extend VG  which is located on /dev/sda to encompass also /dev/sdb 
lsblk							# check the devices
sudo pvcreat /dev/sdb		    # create phisical volume at /dev/sdb
sudo pvdisplay					# check
sudo vgextend vg_name /dev/sdb 	# extend  the existing volume group
df -hT							# check
sudo vgdisplay					# check
# At this point you can create a new logical volume or extend an existing one

##  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
sudo rsize2fs /dev/mapper/lv_name
df -h
### for swap file sudo 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
sudo lvextend --reziefs -l +100%FREE /dev/mapper/lv_name
df -h

# Shrink logical volume
    1  lsblk
    2  sudo apt update
    3  sudo 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  sudo 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
sudo pvcreat /dev/sdbc						# crrate phisical volume
sudo vgcreate vg_name /dev/sdbc				# create volume group
sudo vgdisplay								# check

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

# mount the new logical volume
sudo mkdir -p /mnt/volume																															# create mount point
sudo 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)
sudo 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"

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

sudo 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
sudo mount -a			# no output means everything is correctly mounted
df -h								# check

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

Cre­ate a Snap­shot

Cre­ate 5GB snap­shot of a log­i­cal vol­ume named lv_​​​name from vol­ume group named vg_​​​name.

sudo lvcreate /dev/mapper/vg_name-lv_name -L 5G -s -n lv_name_ss_at_date_$(date +%y%m%d)
sudo 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.

sudo mkdir /mnt/snapshot
sudo 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.

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

If it is a root filesys­tem 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.

sudo umount  /lv_name-mountpoint
sudo lvconvert --merge /dev/mapper/vg_name-lv_name_ss_at_date_220908
  • --merge – An alias for --mergethin, --mergemirrors, or --mergesnapshot, de­pend­ing on the type of LV.
  • --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.

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

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.

sudo lvchange -an /dev/mapper/vg_name-lv_name
sudo lvchange -ay /dev/mapper/vg_name-lv_name
  • -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.
    • The block de­vice for the LV is added or re­moved from the sys­tem us­ing de­vice-map­per in the ker­nel. A sym­bol­ic link /​​​dev/​​​VGName/​​​LVName point­ing to the de­vice node is al­so added/​​​removed.
    • All soft­ware and scripts should ac­cess the de­vice through the sym­bol­ic link and present this as the name of the de­vice. The lo­ca­tion and name of the un­der­ly­ing de­vice node may de­pend on the dis­tri­b­u­tion, con­fig­u­ra­tion (e.g. udev), or re­lease ver­sion.
    • ay spec­i­fies au­to-ac­ti­va­tion, which is used by sys­tem-gen­er­at­ed ac­ti­va­tion com­mands. By de­fault, LVs are au­to-ac­ti­vat­ed. An au­to ac­ti­va­tion prop­er­ty can be set on a VG or LV to dis­able au­to-ac­ti­va­tion, see --setautoactivation y|n in vgchange, lvchange, vgcre­ate, and lvcre­ate. Dis­play the prop­er­ty with vgs or lvs "-o au­toac­ti­va­tion".
    • The lvm.conf(5) auto_​​​activation_​​​volume_​​​list in­cludes names of VGs or LVs that should be au­to-ac­ti­vat­ed, and any­thing not list­ed is not au­to-ac­ti­vat­ed. When auto_​​​activation_​​​volume_​​​list is un­de­fined (the de­fault), it has no ef­fect. If auto_​​​activation_​​​volume_​​​list is de­fined and emp­ty, no LVs are au­to ac­ti­vat­ed. Items in­clud­ed by auto_​​​activation_​​​volume_​​​list will not be au­to-ac­ti­vat­ed if the ac­ti­va­tion prop­er­ty has been dis­abled. See lvmlockd(8) for more in­for­ma­tion about ac­ti­va­tion op­tions ey and sy for shared VGs.
sudo mount -a   # remount via /etc/fstab
df -h           # list the mounted devices
# Deactivate and reactivate the logical volume





# --

# Remove snapshots
sudo lvremove /dev/kali-x-vg/root_at_20220822

Ref­er­ences


As­sum­ing we want to cre­ate LVM and we want to oc­cu­py the en­tire disk space at /​​​dev/​​​sdb. You can fdisk or gdisk to cre­ate a new GPT par­ti­tion ta­ble that will wipe all par­ti­tions and cre­ate new par­ti­tions you need:

  • If the de­vice /​​​dev/​​​sdb will be used used as boot de­vice you can cre­ate two par­ti­tions one for /​​​boot – Lin­ux ext4 and one for the root fs / – Lin­ux LVM.
  • If the de­vice /​​​dev/​​​sdb won't be used as boot de­vice you can cre­ate on­ly one for LVM.

Cre­ate par­ti­tion ta­ble:

sudo fdisk /dev/sdb
#fdisk
# create a new empty GPT partition table
Command (m for help): g
# add a new partition
Command (m for help): n
Partition number (1-128, default 1): 1
First sector (2048-488397134, default 2048):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-488397134, default 488397134): +1G

Created a new partition 1 of type 'Linux filesystem' and of size 1 GiB.
# add a new partition
Command (m for help): n
Partition number (2-128, default 2):
First sector (2099200-488397134, default 2099200):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2099200-488397134, default 488397134):

Created a new partition 2 of type 'Linux filesystem' and of size 231.9 GiB.
# change a partition type
Command (m for help): t
Partition number (1,2, default 2): 2
Partition type (type L to list all types): 31

Changed type of partition 'Linux filesystem' to 'Linux LVM'.
# print the partition table
Command (m for help): p
Disk /dev/sdb: 232.91 GiB, 250059350016 bytes, 488397168 sectors
Disk model: CT250MX500SSD1
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: gpt
Disk identifier: 005499CA-7834-434B-9C36-5306537C8CF1

Device       Start       End   Sectors   Size Type
/dev/sdb1     2048   2099199   2097152     1G Linux filesystem
/dev/sdb2  2099200 488397134 486297935 231.9G Linux LVM

Filesystem/RAID signature on partition 1 will be wiped.
#  verify the partition table
Command (m for help): v
No errors detected.
Header version: 1.0
Using 2 out of 128 partitions.
A total of 0 free sectors is available in 0 segments (the largest is (null)).
# write table to disk and exit
Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

For­mat the first par­ti­tion /​​​dev/​​​sdb1 to ext4:

sudo mkfs.ext4 /dev/sdb1
#mke2fs
mke2fs 1.45.5 (07-Jan-2020)
Creating filesystem with 262144 4k blocks and 65536 inodes
Filesystem UUID: a6a72cfe-46f1-4caa-b114-6bf03f1efe7f
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376

Allocating group tables: done
Writing inode tables: done
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done

Cre­ate LVM phys­i­cals vol­ume at the sec­ond par­ti­tion /​​​dev/​​​sdb1:

sudo pvcreate /dev/sdb2
#lvm2 pvc
Physical volume "/dev/sdb2" successfully created.

Cre­ate LVM vol­ume group at /​​​dev/​​​sdb1:

sudo vgcreate lvm-vm-group /dev/sdb2
#lvm2 vgc
Physical volume "/dev/sdb2" successfully created.
  • lvm-vm-group is the name of the group, it is mater of your choice.

Cre­ate LVM log­i­cal vol­ume at lvm-vm-group:

sudo lvcreate -n vm-win-01 -L 60g lvm-vm-group
#lvm2 lvc
Logical volume "vm-win-01" created.
  • vm-win-01 is the name of the log­i­cal de­vice, it is mater of your choice.

Check the re­sult:

 lsblk | grep -P 'sdb|lvm'
#Out­put
├─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

Ref­er­ences: