LVM explanation

what is LVM

LVM is the abbreviation of Logical Volume Manager. It is a mechanism for managing disk partitions in the Linux environment. A common and difficult problem that Linux users encounter when installing the Linux operating system is how to correctly evaluate The size of each partition to allocate the appropriate hard disk space. Ordinary disk partition management methods cannot change the size of logical partitions after they are divided. When a logical partition cannot store a file, the file cannot be stored across multiple partitions due to the limitation of the upper file system. cannot be placed on other disks at the same time. When encountering a partition that runs out of space, the solution is usually to use a symbolic link or use a tool to adjust the partition size, but this is only a temporary solution and does not fundamentally solve the problem. With the emergence of Linux's logical volume management function, these problems are solved, and users can easily adjust the size of each partition without downtime.

How LVM works

Enter image description

The specific operation steps are as follows:

  1. Adding a disk, the current server disks all support hot swapping, and the system can recognize the new disk after adding the disk directly without shutting down. In the experimental environment of using a virtual machine, after adding a disk, the system needs to restart the virtual machine before the system recognizes the new disk. To the newly added disk, as shown below, /dev/sdb is our newly added disk
[root@localhost ~]# fdisk -l

磁盘 /dev/sda:21.5 GB, 21474836480 字节,41943040 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x000a5fed

   设备 Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048      411647      204800   83  Linux
/dev/sda2          411648     4507647     2048000   82  Linux swap / Solaris
/dev/sda3         4507648    41943039    18717696   83  Linux

磁盘 /dev/sdb:10.7 GB, 10737418240 字节,20971520 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节

  1. Partition the new disk and change the ID to 8e. (This place can also be made into a physical volume without changing it to 8e, but it is said that there will be problems in the process of use, so it is best to strictly follow the steps) We divided the disk into three partitions, and each partition gave The capacity of 1G, and the ID is changed to 8e, because the steps of dividing three times are exactly the same, so only the picture of dividing the first partition is uploaded.

Enter image description

  1. Make the partition a physical volume; use the command: pvcreate /dev/sdb1. This command is not installed in a minimally installed Linux system, you need to run yum intstall -y lvm2 to install
[root@localhost ~]# pvcreate /dev/sdb1
  Physical volume "/dev/sdb1" successfully created.
[root@localhost ~]# pvcreate /dev/sdb2
  Physical volume "/dev/sdb2" successfully created.
[root@localhost ~]# pvcreate /dev/sdb3
  Physical volume "/dev/sdb3" successfully created.

After the physical volume is created, you can use the pvdisplay or dvs command to view the current physical volume. If you cannot view it, you can run the partprobe command first.

[root@localhost ~]# pvs
  PV         VG Fmt  Attr PSize PFree
  /dev/sdb1     lvm2 ---  1.00g 1.00g
  /dev/sdb2     lvm2 ---  1.00g 1.00g
  /dev/sdb3     lvm2 ---  1.00g 1.00g

  1. Then create a volume group, vgcreate vg1 /dev/sdb1 /dev/sdb2 This command means that the physical volumes /dev/sdb1 and /dev/sdb2 form a volume group vg1. After the creation is complete, you can use the vgdisplay or vgs command to view the volume group
[root@localhost ~]# vgcreate vg1 /dev/sdb1 /dev/sdb2
  Volume group "vg1" successfully created
[root@localhost ~]# vgs
  VG  #PV #LV #SN Attr   VSize VFree
  vg1   2   0   0 wz--n- 1.99g 1.99g

  1. Create a logical volume and specify a logical volume size of 100M; lvcreate -L 100M -n lv1 vg1, after the creation is complete, you can use the lvdisplay or lvs command to view the logical volume information.
[root@localhost ~]# lvcreate -L 100M -n lv1 vg1
  Logical volume "lv1" created.
[root@localhost ~]# lvs
  LV   VG  Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  lv1  vg1 -wi-a----- 100.00m                                                    
[root@localhost ~]# lvdisplay
  --- Logical volume ---
  LV Path                /dev/vg1/lv1
  LV Name                lv1
  VG Name                vg1
  LV UUID                h418at-aYwA-m8Ah-lAji-ICmF-uVW0-eDjAdQ
  LV Write Access        read/write
  LV Creation host, time localhost.localdomain, 2018-02-28 08:44:57 +0800
  LV Status              available
  # open                 0
  LV Size                100.00 MiB
  Current LE             25
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0

  1. Format the logical volume as ext4: mkfs.ext4 /dev/vg1/lv1 After formatting, mount the logical volume under /mnt/:
[root@localhost ~]# mkfs.ext4 /dev/vg1/lv1 
mke2fs 1.42.9 (28-Dec-2013)
文件系统标签=
OS type: Linux
块大小=1024 (log=0)
分块大小=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
25688 inodes, 102400 blocks
5120 blocks (5.00%) reserved for the super user
第一个数据块=1
Maximum filesystem blocks=33685504
13 block groups
8192 blocks per group, 8192 fragments per group
1976 inodes per group
Superblock backups stored on blocks: 
	8193, 24577, 40961, 57345, 73729

Allocating group tables: 完成                            
正在写入inode表: 完成                            
Creating journal (4096 blocks): 完成
Writing superblocks and filesystem accounting information: 完成 

[root@localhost ~]# mount /dev/vg1/lv1 /mnt
[root@localhost ~]# ls /mnt
lost+found

At this point, even if LVM is completed, how to expand the capacity when the logical volume space is insufficient?

LVM logical volume expansion (logical volume format is ext4)

  1. Unmount the logical volume umount /mnt
  2. Specify the new capacity lvresize -L 200M /dev/vg1/lv1
  3. Check for errors e2fsck -f /dev/vh1/lv1
  4. Update logical volume information resize2fs /dev/vg1/lv1
  5. Mount the logical volume mount /dev/vg1/lv1 /mnt and view df -h
[root@localhost ~]# umount /mnt
umount: /mnt:未挂载
[root@localhost ~]# lvresize -L 200M /dev/vg1/lv1
  Size of logical volume vg1/lv1 changed from 100.00 MiB (25 extents) to 200.00 MiB (50 extents).
  Logical volume vg1/lv1 successfully resized.
[root@localhost ~]# e2fsck -f /dev/vg1/lv1
e2fsck 1.42.9 (28-Dec-2013)
第一步: 检查inode,块,和大小
第二步: 检查目录结构
第3步: 检查目录连接性
Pass 4: Checking reference counts
第5步: 检查簇概要信息
/dev/vg1/lv1: 13/25688 files (7.7% non-contiguous), 8899/102400 blocks
[root@localhost ~]# resize2fs /dev/vg1/lv1
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/vg1/lv1 to 204800 (1k) blocks.
The filesystem on /dev/vg1/lv1 is now 204800 blocks long.

[root@localhost ~]# mount /dev/vg1/lv1 /mnt
[root@localhost ~]# df -h /mnt
文件系统             容量  已用  可用 已用% 挂载点
/dev/mapper/vg1-lv1  190M  1.6M  175M    1% /mnt

LVM shrink operation (rarely used, not supported by xfs)

  1. unmount umount /mnt
  2. Check for errors e2fsck -f /dev/vh1/lv1
  3. Specify the new capacity lvresize -L 100M /dev/vg1/lv1
  4. Check if the capacity is reduced lvdisplay or lvs
  5. mount mount /dev/vg1/lv1
[root@localhost ~]# e2fsck -f /dev/vg1/lv1
e2fsck 1.42.9 (28-Dec-2013)
第一步: 检查inode,块,和大小
第二步: 检查目录结构
第3步: 检查目录连接性
Pass 4: Checking reference counts
第5步: 检查簇概要信息
/dev/vg1/lv1: 13/49400 files (7.7% non-contiguous), 11887/204800 blocks
[root@localhost ~]# lvresize -L 100M /dev/vg1/lv1
  WARNING: Reducing active logical volume to 100.00 MiB.
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce vg1/lv1? [y/n]: y
  Size of logical volume vg1/lv1 changed from 200.00 MiB (50 extents) to 100.00 MiB (25 extents).
  Logical volume vg1/lv1 successfully resized.
[root@localhost ~]# lvs
  LV   VG  Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  lv1  vg1 -wi-a----- 100.00m                                                    
[root@localhost ~]# mount /dev/vg1/lv1 /mnt
mount: 文件系统类型错误、选项错误、/dev/mapper/vg1-lv1 上有坏超级块、
       缺少代码页或助手程序,或其他错误

       有些情况下在 syslog 中可以找到一些有用信息- 请尝试
       dmesg | tail  这样的命令看看。

When the logical volume format is xfs type, the operation of expanding the logical volume

In order to meet the experimental requirements, first format /dev/vg1/lv1 as xfs type, mkfs.xfs -f /dev/vg1/lv1

  1. Mount the logical volume after formatting it as xfs mount /dev/vg1/lv1 /mnt
  2. lvs command to view logical volume size
  3. Expansion; lvresize -L 300M /dev/vg1/lv1
  4. lvs view size
  5. Use df-h to find that the capacity has not changed, you need to execute the xfs_growfs /dev/vg1/lv1 command
  6. Use the df -h command to view
[root@localhost ~]# mkfs.xfs -f /dev/vg1/lv1
meta-data=/dev/vg1/lv1           isize=256    agcount=2, agsize=6656 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=0
data     =                       bsize=4096   blocks=13312, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=0
log      =internal log           bsize=4096   blocks=853, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
[root@localhost ~]# mount /dev/vg1/lv1 /mnt
[root@localhost ~]# ls /mnt
[root@localhost ~]# lvs
  LV   VG  Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  lv1  vg1 -wi-ao---- 52.00m                                                    
[root@localhost ~]# lvresize -L 300M  /dev/vg1/lv1
  Size of logical volume vg1/lv1 changed from 52.00 MiB (13 extents) to 300.00 MiB (75 extents).
  Logical volume vg1/lv1 successfully resized.
[root@localhost ~]# lvs
  LV   VG  Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  lv1  vg1 -wi-ao---- 300.00m                                                    
[root@localhost ~]# df -h /mnt
文件系统             容量  已用  可用 已用% 挂载点
/dev/mapper/vg1-lv1   49M  2.7M   46M    6% /mnt
[root@localhost ~]# xfs_grows /dev/vg1/lv1
-bash: xfs_grows: 未找到命令
[root@localhost ~]# xfs_growfs /dev/vg1/lv1
meta-data=/dev/mapper/vg1-lv1    isize=256    agcount=2, agsize=6656 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=0
data     =                       bsize=4096   blocks=13312, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=0
log      =internal               bsize=4096   blocks=853, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 13312 to 76800
[root@localhost ~]# df -h /mnt
文件系统             容量  已用  可用 已用% 挂载点
/dev/mapper/vg1-lv1  297M  3.1M  294M    2% /mnt

It can be seen that the capacity has been successfully expanded from 50M to 300M

Expansion volume group

As can be seen from the above content, the upper limit of logical volume expansion is the capacity of the volume group. How to expand the capacity if the capacity of the volume group is insufficient? At the beginning of the experiment, we created three physical volumes, but only two physical volumes were used when creating the volume group. When the capacity of the volume group is not enough, we can add the idle third physical volume to the volume group.

  1. Add the physical volume to the original volume group vgextend vg1 /dev/sdbx
  2. Use vgs to verify whether the expansion is successful
[root@localhost ~]# vgs
  VG  #PV #LV #SN Attr   VSize VFree 
  vg1   2   1   0 wz--n- 1.99g <1.70g

[root@localhost ~]# vgextent vg1 /dev/sdb3
-bash: vgextent: 未找到命令
[root@localhost ~]# vgex
vgexport  vgextend  
[root@localhost ~]# vgextend vg1 /dev/sdb3
  Volume group "vg1" successfully extended
[root@localhost ~]# vgdisplay
  --- Volume group ---
  VG Name               vg1
  System ID             
  Format                lvm2
  Metadata Areas        3
  Metadata Sequence No  7
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1
  Open LV               1
  Max PV                0
  Cur PV                3
  Act PV                3
  VG Size               <2.99 GiB
  PE Size               4.00 MiB
  Total PE              765
  Alloc PE / Size       75 / 300.00 MiB
  Free  PE / Size       690 / <2.70 GiB
  VG UUID               gT48qq-JCCo-I17e-H82v-gQoV-d5f8-9IoYeR
   

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325026609&siteId=291194637
LVM