Use of LVM logical volumes

Use of LVM logical volumes

LVM logical volume concept address:
First of all, create a new virtual machine, we use CentOS-7-x86_64-DVD-1511 by default.
Mirror address:
link: https://pan.baidu.com/s/1Myl_GXnUg7t3OR01mCuMrQ
extraction code: 1511

①Install virtual machine, configure ip, yum source

Install virtual machine, configure ip, configure yum source address tutorial

②Add hard disk

In VMwareWorkstation, open the virtual machine settings and
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
finally click Finish, restart the virtual machine, and use the command to view

[root@localhost ~]# lsblk   (查看磁盘)
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0   20G  0 disk 
├─sda1            8:1    0  500M  0 part /boot
└─sda2            8:2    0 19.5G  0 part 
  ├─centos-root 253:0    0 17.5G  0 lvm  /
  └─centos-swap 253:1    0    2G  0 lvm  [SWAP]
sdb               8:16   0   20G  0 disk 
sr0              11:0    1    4G  0 rom  

③Use of lvm logical volume

disk partition

[root@localhost ~]# fdisk /dev/sdb
欢迎使用 fdisk (util-linux 2.23.2)。

更改将停留在内存中,直到您决定将更改写入磁盘。
使用写入命令前请三思。

Device does not contain a recognized partition table
使用磁盘标识符 0xdf6b7d5f 创建新的 DOS 磁盘标签。

命令(输入 m 获取帮助):p

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

   设备 Boot      Start         End      Blocks   Id  System

命令(输入 m 获取帮助):n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): 
Using default response p
分区号 (1-4,默认 1):
起始 扇区 (2048-41943039,默认为 2048):
将使用默认值 2048
Last 扇区, +扇区 or +size{
    
    K,M,G} (2048-41943039,默认为 41943039)+5G
分区 1 已设置为 Linux 类型,大小设为 5 GiB

命令(输入 m 获取帮助):n
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): 
Using default response p
分区号 (2-4,默认 2):
起始 扇区 (10487808-41943039,默认为 10487808):
将使用默认值 10487808
Last 扇区, +扇区 or +size{
    
    K,M,G} (10487808-41943039,默认为 41943039)+5G
分区 2 已设置为 Linux 类型,大小设为 5 GiB

命令(输入 m 获取帮助):p

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

   设备 Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048    10487807     5242880   83  Linux
/dev/sdb2        10487808    20973567     5242880   83  Linux

命令(输入 m 获取帮助):w
The partition table has been altered!

Calling ioctl() to re-read partition table.
正在同步磁盘。

Check the disk again and find a new partition
Insert picture description here

Create a physical volume for the new partition

[root@localhost ~]# pvcreate /dev/sdb1 /dev/sdb2 (对俩新分区创建新的物理卷)
  Physical volume "/dev/sdb1" successfully created
  Physical volume "/dev/sdb2" successfully created
[root@localhost ~]# pvs    (查看简单物理卷信息)
  PV         VG     Fmt  Attr PSize  PFree 
  /dev/sda2  centos lvm2 a--  19.51g 40.00m
  /dev/sdb1         lvm2 ---   5.00g  5.00g
  /dev/sdb2         lvm2 ---   5.00g  5.00g
[root@localhost ~]# pvdisplay    (查看详细物理卷信息)
  --- Physical volume ---
  PV Name               /dev/sda2
  VG Name               centos
  PV Size               19.51 GiB / not usable 3.00 MiB
  Allocatable           yes 
  PE Size               4.00 MiB
  Total PE              4994
  Free PE               10
  Allocated PE          4984
  PV UUID               L4L1ve-YRxL-AV6j-evy0-WqWe-AqOa-6lvuAX
   
  "/dev/sdb2" is a new physical volume of "5.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdb2
  VG Name               
  PV Size               5.00 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               auisMi-9Hr2-KkK0-eq86-7sC7-UB5U-J1JaBS
   
  "/dev/sdb1" is a new physical volume of "5.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdb1
  VG Name               
  PV Size               5.00 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               3OW1BR-W3jq-Lge0-YenU-ukJT-8Sha-ISOSEf

Create volume group

[root@localhost ~]# vgcreate myvg /dev/sdb[1-2] (对俩物理卷创建卷组)
  Volume group "myvg" successfully created
[root@localhost ~]# vgs    (查看简单卷组信息)
  VG     #PV #LV #SN Attr   VSize  VFree 
  centos   1   2   0 wz--n- 19.51g 40.00m
  myvg     2   0   0 wz--n-  9.99g  9.99g
[root@localhost ~]# vgdisplay    (查看详细卷组信息)
  --- Volume group ---
  VG Name               centos
  System ID             
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  3
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               2
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               19.51 GiB
  PE Size               4.00 MiB
  Total PE              4994
  Alloc PE / Size       4984 / 19.47 GiB
  Free  PE / Size       10 / 40.00 MiB
  VG UUID               2lZ6d7-4zFf-vY1X-0Qe9-86Ca-q1vV-5ZPW0e
   
  --- Volume group ---
  VG Name               myvg
  System ID             
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  1
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                0
  Open LV               0
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               9.99 GiB
  PE Size               4.00 MiB
  Total PE              2558
  Alloc PE / Size       0 / 0   
  Free  PE / Size       2558 / 9.99 GiB
  VG UUID               3xuvpY-xxcC-aFn9-tYmP-DFxj-T3TD-1DsDrR

  When multiple physical volumes are combined into a volume group, LVM will do a similar formatting work on all physical volumes, cutting each physical volume into a piece of space, and this piece of space is called PE ( Physical Extent), its default size is 4MB.
  Due to the limitation of the kernel, a logical volume (Logic Volume) can only contain 65536 PEs (PhysicalExtent) at most, so the size of a PE determines the maximum capacity of the logical volume, and a PE of 4MB determines the maximum capacity of a single logical volume. 256 GB. If you want to use a logical volume larger than 256 Gb, you need to specify a larger PE when creating a volume group.

Delete the volume group and recreate it

[root@localhost ~]# vgremove myvg    (删除卷组)
  Volume group "myvg" successfully removed
[root@localhost ~]# vgcreate -s 16m myvg /dev/sdb[1-2]    (创建16MB的PE)
  Volume group "myvg" successfully created
[root@localhost ~]# vgdisplay    (查看信息,可以看到PE成功便成16MB)

Insert picture description here

Add physical volume

  Add a physical volume to the volume group myvg, and then divide a /dev/sdb3 partition on /dev/sdb, and add the partition to the volume group myvg.

[root@localhost ~]# fdisk /dev/sdb
欢迎使用 fdisk (util-linux 2.23.2)。

更改将停留在内存中,直到您决定将更改写入磁盘。
使用写入命令前请三思。


命令(输入 m 获取帮助):n
Partition type:
   p   primary (2 primary, 0 extended, 2 free)
   e   extended
Select (default p): 
Using default response p
分区号 (3,4,默认 3):
起始 扇区 (20973568-41943039,默认为 20973568):
将使用默认值 20973568
Last 扇区, +扇区 or +size{
    
    K,M,G} (20973568-41943039,默认为 41943039)+5G
分区 3 已设置为 Linux 类型,大小设为 5 GiB

命令(输入 m 获取帮助):w
The partition table has been altered!

Calling ioctl() to re-read partition table.
正在同步磁盘。

Insert picture description here
  Add the created /dev/sdb3 to the myvg volume group. In the process of adding, /dev/sdb3 will be automatically created as a physical volume

[root@localhost ~]# vgextend myvg /dev/sdb3
  Physical volume "/dev/sdb3" successfully created
  Volume group "myvg" successfully extended
[root@localhost ~]# vgs
  VG     #PV #LV #SN Attr   VSize  VFree 
  centos   1   2   0 wz--n- 19.51g 40.00m
  myvg     3   0   0 wz--n- 14.95g 14.95g

Create logical volume

[root@localhost ~]# lvcreate -L +5G -n mylv myvg    (创建逻辑卷)
  Logical volume "mylv" created.
[root@localhost ~]# lvs    (查看逻辑卷)
  LV   VG     Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  root centos -wi-ao---- 17.47g                                                    
  swap centos -wi-ao----  2.00g                                                    
  mylv myvg   -wi-a-----  5.00g                                                    
[root@localhost ~]# lvscan    (扫描lv逻辑卷)
  ACTIVE            '/dev/myvg/mylv' [5.00 GiB] inherit
  ACTIVE            '/dev/centos/swap' [2.00 GiB] inherit
  ACTIVE            '/dev/centos/root' [17.47 GiB] inherit

Format the logical volume mylv using the ext4 file system

[root@localhost ~]# mkfs.ext4 /dev/mapper/myvg-mylv
mke2fs 1.42.9 (28-Dec-2013)
文件系统标签=
OS type: Linux
块大小=4096 (log=2)
分块大小=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
327680 inodes, 1310720 blocks
65536 blocks (5.00%) reserved for the super user
第一个数据块=0
Maximum filesystem blocks=1342177280
40 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks: 
	32768, 98304, 163840, 229376, 294912, 819200, 884736

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

Verify logical volume

Mount the logical volume mylv under /mnt and verify

[root@localhost ~]# mount /dev/mapper/myvg-mylv /mnt/
[root@localhost ~]# df -h
文件系统                 容量  已用  可用 已用% 挂载点
/dev/mapper/centos-root   18G  855M   17G    5% /
devtmpfs                 1.9G     0  1.9G    0% /dev
tmpfs                    1.9G     0  1.9G    0% /dev/shm
tmpfs                    1.9G  8.7M  1.9G    1% /run
tmpfs                    1.9G     0  1.9G    0% /sys/fs/cgroup
/dev/sda1                497M  125M  373M   25% /boot
tmpfs                    378M     0  378M    0% /run/user/0
/dev/mapper/myvg-mylv    4.8G   20M  4.6G    1% /mnt

Expand logical volume

[root@localhost ~]# lvextend -L +1G /dev/mapper/myvg-mylv    (对逻辑卷扩容1G)
  Size of logical volume myvg/mylv changed from 5.00 GiB (320 extents) to 6.00 GiB (384 extents).
  Logical volume mylv successfully resized.

Insert picture description here

You can see that the logical volume information shows 6GB but there is no change in the mount information, so the file system needs to be expanded.

[root@localhost ~]# resize2fs /dev/mapper/myvg-mylv
resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/mapper/myvg-mylv is mounted on /mnt; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 1
The filesystem on /dev/mapper/myvg-mylv is now 1572864 blocks long.

[root@localhost ~]# df -h
文件系统                 容量  已用  可用 已用% 挂载点
/dev/mapper/centos-root   18G  855M   17G    5% /
devtmpfs                 1.9G     0  1.9G    0% /dev
tmpfs                    1.9G     0  1.9G    0% /dev/shm
tmpfs                    1.9G  8.7M  1.9G    1% /run
tmpfs                    1.9G     0  1.9G    0% /sys/fs/cgroup
/dev/sda1                497M  125M  373M   25% /boot
tmpfs                    378M     0  378M    0% /run/user/0
/dev/mapper/myvg-mylv    5.8G   20M  5.5G    1% /mnt

This is a success!

Guess you like

Origin blog.csdn.net/planetoid_a/article/details/109261372