Centos7 logical volume management (LVM)

1, LVM concept

1. A physical device is a storage device used to retain data stored in a logical volume. They are block devices that can be disk partitions, entire disks, RAID arrays, or SAN disks. Devices must be initialized as LVM physical volumes to work with LVM. The entire device will be used as a physical volume.

2. A physical volume (PV) is used to register the underlying physical devices for use in a volume group. LVM automatically divides PV into physical blocks (PE);

They are small pieces of data that act as the smallest storage block on a PV.

3. A volume group (VG) is a storage pool that consists of one or more physical volumes. A PV can only be assigned to one VG. VG can contain unused space and any number of logical volumes.

4. Logical volumes (LVS) are created based on free physical ranges in volume groups, providing "storage" devices used by applications, users, and operating systems. LV is a collection of logical blocks (LES) that map to physical blocks (the smallest storage block of PV). By default, each LE is mapped to a PE. Setting a specific LV option will change this mapping; for example, mirroring will cause each LE to map to two PES.

2, LVM creation

1. Prepare physical equipment

Create a new partition using fdisk, gdisk, or parted for use with LVM. On LVM partitions, always set the partition type to Linux LVM; for MBR type partitions, use 0x8e. If necessary, use partprobe to register the new partition with the kernel. You can also use full disks, RAID arrays, or SAN disks.

You need to prepare a physical device only if you do not have a prepared physical device and you need a new physical volume to create or expand a volume group.

#adopt# lsblk view disk name
[root@localhost ~]# fdisk /dev/sdb

Use m for help, p to print the existing partition table, n to create a new partition, t to change the partition type, w to write changes, and q to exit.

2. Create physical volume

Use pvcreate to label partitions (or other physical devices) for use with LVM as physical volumes. A header used to store LVM configuration data is written directly to the PV. PV is divided into multiple fixed size physical ranges (PE); for example, 4MiB blocks. Use the device names separated by spaces as parameters of pvcreate, and mark multiple devices at the same time.

[root@localhost ~]# pvcreate /dev/sdb1 /dev/sdb2 
  Physical volume "/dev/sdb1" successfully created.
  Physical volume "/dev/sdb2" successfully created.

This command marks devices / dev/sdb1 and / dev/sdb2 as PVS ready to be assigned to volume groups. You need to create a PV only if there is no free PV to create or extend a VG.

3. Create volume group

vgcreate is used to create a pool containing one or more physical volumes, called a volume group. The size of the VG is determined by the total number of physical ranges in the pool. VG is responsible for hosting one or more logical volumes by assigning free PE to LV; therefore, VG must have enough free PE available when creating Lv.

[root@localhost ~]# vgcreate vg-alpha /dev/sdb1 /dev/sdb2 
  Volume group "vg-alpha" successfully created

This command creates a VG called VG alpha, which is the total size (in PE units) of / dev/sdb1 and / dev/sdb2. You need to create a VG only if there is no existing one. Additional VG may be created for administrative reasons to manage the use of PV and LV. Otherwise, the existing VG can be expanded as needed to accommodate the new Lv.

4. Create logical volume

Lvcreate creates a new logical volume based on the available physical ranges in the volume group. Use at least the following parameters for lvcreate: set the LV name with the - n option, set the LV size in bytes with the - L option, and determine the VG name in which to create the Lv.

[root@localhost ~]# lvcreate -n hercules -L 2G vg-alpha
  Logical volume "hercules" created.

This command creates an LV named hercules in VG VG alpha with a size of 3GiB. There must be enough free physical range to allocate 2GiB, rounded to a multiple of PE cell size if necessary.

There are several ways to specify the size: - L requires a size in bytes or greater, such as megabytes (binary megabytes) and gigabytes (binary gigabytes). -The L option requires a size measured by the number of physical ranges. The following example:

lvcreate -L 128M: size the logical volume to exactly 128MiB

lvcreate -l 128: determines the size of the logical volume to exactly 128 ranges. The total number of bytes depends on the size of the physical range block on the underlying physical volume.

5. Add file system

Use mkfs to create an xfs file system on a new logical volume. Or, create a filesystem based on your preferred filesystem; for example, ext4

[root@localhost ~]# mkfs -t xfs /dev/vg-alpha/hercules 
meta-data=/dev/vg-alpha/hercules isize=512    agcount=4, agsize=131072 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=524288, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0

To make the file system available after a reboot:

Using mkdir to create mount point directory

[root@localhost ~]# mkdir /mnt/hercules

Add entries to the / etc/fstab file

[root@localhost ~]# vi /etc/fstab
/dev/vg-alpha/hercules	/mnt/hercules		xfs	defaults	0 0

Run mount -a to mount all the file systems in / etc/fstab, including the entries you just added

[root@localhost ~]# mount -a

#View results
[root@localhost ~]# df -HT
Filesystem                     Type      Size  Used Avail Use% Mounted on
/dev/mapper/centos-root        xfs        15G  1.2G   14G   8% /
devtmpfs                       devtmpfs  952M     0  952M   0% /dev
tmpfs                          tmpfs     964M     0  964M   0% /dev/shm
tmpfs                          tmpfs     964M  9.2M  955M   1% /run
tmpfs                          tmpfs     964M     0  964M   0% /sys/fs/cgroup
/dev/sda1                      xfs       1.1G  153M  912M  15% /boot
tmpfs                          tmpfs     193M     0  193M   0% /run/user/0
/dev/mapper/vg--alpha-hercules xfs       2.2G   34M  2.2G   2% /mnt/hercules

3, View LVM status information

1. Physical volumes use pvdisplay to display information about physical volumes. If no parameters are specified with the command, it lists information about all PVS on the system. If the parameter is a specific device name, only information for that specific PV will be displayed

[root@localhost ~]# pvdisplay  /dev/sdb1
  --- Physical volume ---
  PV Name               /dev/sdb1
  VG Name               vg-alpha
  PV Size               1.00 GiB / not usable 4.00 MiB
  Allocatable           yes 
  PE Size               4.00 MiB
  Total PE              255
  Free PE               254
  Allocated PE          1
  PV UUID               u2sCoW-nq5i-gSKJ-K6GU-pdtA-Lbog-lIVzAY

PV Name: map to device name

VG Name: displays the assignment of PV to a volume group

PV Size: displays the physical size of the PV, including any unavailable space

PE Size: is the physical range size, which is the minimum size that can be allocated in the logical volume. It is also a multiple of calculating the size of any value reported in PE units, such as Free PE; for example, 26 PE x 4MiB (PE Size) can provide 104MiB of free space. The logical volume size is rounded to a multiple of PE units. LVM automatically sets the PE Size, but it can also be specified

Free PE: shows how many PE units are available for assignment to a new logical volume

2. Volume group

Use vgdisplay to display information about volume groups. If no variables are specified for the command, it displays information about all VGS. Using the VG name as a variable will only display information for that particular VG

[root@localhost ~]# vgdisplay vg-alpha
  --- Volume group ---
  VG Name               vg-alpha
  System ID             
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  2
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1
  Open LV               1
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               2.99 GiB
  PE Size               4.00 MiB
  Total PE              766
  Alloc PE / Size       512 / 2.00 GiB
  Free  PE / Size       254 / 1016.00 MiB
  VG UUID               ZzGhNJ-RWH4-CkMS-MGHX-E65d-Zl0h-lU5rXb

VG Name: is the name of this volume group

VG Size: is the total size that the storage pool can use for logical volume allocation

Total PE: is the total size in PE units

Free PE / Size: displays how much free space is available in the VG to allocate to a new LV or to extend an existing LV

3. Logical volume

Use lvdisplay to display information about logical volumes. Similarly, if no parameters are specified with the command, information about all LVS is displayed; using the LV device name as a parameter only information about that particular device is displayed

[root@localhost ~]# lvdisplay /dev/vg-alpha/hercules 
  --- Logical volume ---
  LV Path                /dev/vg-alpha/hercules
  LV Name                hercules
  VG Name                vg-alpha
  LV UUID                pgcH5w-JN2R-DvId-SzAg-1WLr-fcM8-iy0qqB
  LV Write Access        read/write
  LV Creation host, time localhost.localdomain, 2019-11-07 02:01:41 -0500
  LV Status              available
  # open                 1
  LV Size                2.00 GiB
  Current LE             512
  Segments               2
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:2

LV Path: displays the device name of this logical volume. Some tools may report the device name as / dev / mapper / VGname lvname; both names represent the same LV

VG Name: displays the volume group from which LV is assigned

LV Size: displays the total size of Lv. Use file system tools to check available space and used space of data store

Current LE: displays the number of logical ranges used by this LV. LE is usually mapped to a physical range in VG and therefore to a physical volume

4, Expanding logical volumes and XFS file systems

1. Verify that the volume group has free space

Use is gdisplay to verify that there are enough physical extents available.

[root@localhost ~]# vgdisplay vg-alpha
  --- Volume group ---
  VG Name               vg-alpha
...
  Free  PE / Size       254 / 1016.00 MiB
...

Check the Free PE / Size in the output it should report a value equal to or greater than the extra space required. If there is not enough free space, the volume group must expand at least as much as it needs.

2. Expand logical volumes

lvextend expands the logical volume to a new size. Add LV device name as the last parameter of the command

[root@localhost ~]# lvextend -L +300M /dev/vg-alpha/hercules 
  Size of logical volume vg-alpha/hercules changed from 2.00 GiB (512 extents) to 2.29 GiB (587 extents).
  Logical volume vg-alpha/hercules successfully resized

This command increases the size of the logical volume hercules by 300 MIB. Note the "+" before the size, which means increasing the value to the existing size; without the symbol, the value defines the final exact size of the LV. Similar to lvcreate, there is a multi clock way to specify the size: - l usually requires a physical sector value, while - L requires a size in bytes or larger (for example, megabytes or gigabytes). An example is as follows:

lvextend -l 128: resizes a logical volume to exactly 128 ranges

lvextend -l +128: adds 128 ranges to the current size of the logical volume

lvextend -L 128M: resize the logical volume to exactly 128MiB

lvextend -L +128: adds 128MiB to the current size of the logical volume

lvextend -l +50%FREE: add 50% of the currently available space in VG to LV

3. Extended file system

xfs_growfs / mountpoint extends the file system to take up the extended LV. xfs_growfs requires that the file system be mounted while it is running; it can still be used during resizing operations.

[root@localhost ~]# xfs_growfs /mnt/hercules
meta-data=/dev/mapper/vg--alpha-hercules isize=512    agcount=4, agsize=131072 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=524288, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal               bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 524288 to 601088

#300M larger than before
[root@localhost ~]# df -HT
Filesystem                     Type      Size  Used Avail Use% Mounted on
/dev/mapper/centos-root        xfs        15G  1.2G   14G   8% /
devtmpfs                       devtmpfs  952M     0  952M   0% /dev
tmpfs                          tmpfs     964M     0  964M   0% /dev/shm
tmpfs                          tmpfs     964M  9.2M  955M   1% /run
tmpfs                          tmpfs     964M     0  964M   0% /sys/fs/cgroup
/dev/sda1                      xfs       1.1G  153M  912M  15% /boot
/dev/mapper/vg--alpha-hercules xfs       2.5G   34M  2.5G   2% /mnt/hercules
tmpfs                          tmpfs     193M     0  193M   0% /run/user/0

5, Expanding logical volumes and ext4

The steps to expand the ext4 based logical volume are the same as those to expand the LV of xfs, only the steps to adjust the file system size are different.

1. Verify that the volume group has free space

Use vgdisplay vgname to verify that there are enough physical extents available

2. Expand logical volumes

Lvextend - L + extension / dev/vgname/lvname extends the logical volume / dev/vgname/lvname by the range value

3. Extended file system

resize2fs /dev/vgname/lvname extends the file system to take up the newly extended LV. Positive and XFS_ Like growfs, you can mount and use a filesystem while it is running. Optionally, include the - p option to view the progress of the resize operation.

[root@localhost ~]# resize2fs /dev/vg-alpha/hercules

be careful:

xfs_ The main difference between growfs and resize2fs is the parameters passed to identify the file system

xfs_growfs uses mount points, while resize2fs uses logical volume names

6, Delete logical volume (LVM)

1. Preparing the file system

Move all the data that must be preserved to another file system and unmount the file system using umount. Do not forget to delete any / etc/fstab entries associated with this file system

#see
[root@localhost ~]# df -HT
Filesystem                     Type      Size  Used Avail Use% Mounted on
...
/dev/mapper/vg--alpha-hercules xfs       2.5G   34M  2.5G   2% /mnt/hercules


#Operation:
[root@localhost ~]# umount /mnt/hercules

2. Delete logical volume

Use lvremove to delete logical volumes that are no longer needed. Use device name as parameter

[root@localhost ~]# lvremove /dev/vg-alpha/hercules 
Do you really want to remove active logical volume vg-alpha/hercules? [y/n]: y
  Logical volume "hercules" successfully removed

Before running this command, you must unmount the LV file system. Before deleting Lv. Confirmation will be requested.

The physical scope of the LV will be freed and can be used to assign to an existing LV or a new LV in the volume group

3. Delete volume group

Use vgemove to delete volume groups that are no longer needed. Use VG name as parameter

[root@localhost ~]# vgremove vg-alpha
  Volume group "vg-alpha" successfully removed

The physical volume of the VG will be freed and can be used to assign to an existing or new VG in the system

4. Delete physical volume

Use pvremove to delete physical volumes that are no longer needed. Use a space separated list of PV devices to delete multiple PVS at the same time. PV metadata is purged from the partition (or disk). The partition is now free and can be reassigned or reformatted.

[root@localhost ~]# pvremove /dev/sdb1 /dev/sdb2 
  Labels on physical volume "/dev/sdb1" successfully wiped.
  Labels on physical volume "/dev/sdb2" successfully wiped.

#Check that the / dev/sdb disk is not in use
[root@localhost ~]# lsblk 
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
fd0               2:0    1    4K  0 disk 
sda               8:0    0   16G  0 disk 
├─sda1            8:1    0    1G  0 part /boot
└─sda2            8:2    0   15G  0 part 
  ├─centos-root 253:0    0 13.4G  0 lvm  /
  └─centos-swap 253:1    0  1.6G  0 lvm  [SWAP]
sdb               8:16   0    3G  0 disk 
├─sdb1            8:17   0    1G  0 part 
└─sdb2            8:18   0    2G  0 part

 

Tags: CentOS ascii Linux

Posted on Wed, 17 Jun 2020 21:17:06 -0400 by fmpros