LVM逻辑卷的使用

1、虚拟机参考(VMwear版本:17.0.0)

使用本地PC环境的VMWare Workstation软件进行实操练习,镜像使用提供的CentOS-7-x86_64-DVD-2009,硬件资源如图3-1-1所示。

图3-1-1 硬件资源

节点规划:

IP主机名节点
192.168.20.10localhostLinux服务器节点

2、部署

2.1、环境准备

2.1.1、配置IP

        进入到虚拟机界面,编辑网卡配置文件,将网络配置成192.168.20.10,命令如下:

[root@localhost ~]# vi /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens33
UUID=a39ffa78-6a6e-4d6a-8b16-fb31118d5c06
DEVICE=ens33
ONBOOT=yes
IPADDR=192.168.20.10
NATEMASK=255.255.255.0
GATEWAY=192.168.20.2
DNS1=114.114.114.114

        配置完成后,重启网络并查看IP,命令如下:

[root@localhost ~]# systemctl restart network
[root@localhost ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:d1:43:28 brd ff:ff:ff:ff:ff:ff
    inet 192.168.10.10/24 brd 192.168.10.255 scope global noprefixroute ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::3c37:5017:de01:8fda/64 scope link noprefixroute
       valid_lft forever preferred_lft forever

        配置完IP后,可以通过PC机的远程连接工具SecureCRT连接虚拟机。

2.2、添加硬盘

        在VMwareWorkstation中的虚拟机设置界面,单击下方“添加”按钮,选择“硬盘”,然后单击右下角“下一步”按钮,如图3-2-3所示。

图3-2-3 添加硬盘

        选择SCSI(S)磁盘,单击右下角“下一步”按钮,如图3-2-4所示。

图3-2-4 选择磁盘类型

        选择“创建新虚拟磁盘(V)”选项,然后单击右下角“下一步”按钮。如图3-2-5所示。

图3-2-5 选择磁盘

        指定磁盘大小为20 GB(默认为20,可调),选择“将虚拟磁盘存储为单个文件(O)”选项,如图3-2-6所示。

图3-2-6 指定磁盘容量

        文件名不做修改,使用默认名称,然后单击右下角“完成”按钮,如图3-2-7所示。

图3-2-7 指定磁盘文件

        添加完磁盘后,重启虚拟机。重启过后,使用命令查看磁盘,命令如下:

[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

         可以看到存在一块名叫sdb的块设备,大小为20 GB。

 2.3、LVM逻辑卷的使用

2.3.1、创建物理卷

        在创建物理卷之前,需要对磁盘进行分区。首先使用fdisk命令对sdb进行分区操作,分出两个大小为5 GB的分区,命令如下:

[root@localhost ~]# fdisk /dev/sdb 
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0x9e46a7c2.

Command (m for help): p

Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x9e46a7c2

   Device Boot      Start         End      Blocks   Id  System

Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
Partition number (1-4, default 1): 
First sector (2048-41943039, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): +5G
Partition 1 of type Linux and of size 5 GiB is set

Command (m for help): n
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): p
Partition number (2-4, default 2): 
First sector (10487808-41943039, default 10487808): 
Using default value 10487808
Last sector, +sectors or +size{K,M,G} (10487808-41943039, default 41943039): +5G  
Partition 2 of type Linux and of size 5 GiB is set

Command (m for help): p

Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x9e46a7c2

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

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
[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 
├─sdb1            8:17   0    5G  0 part 
└─sdb2            8:18   0    5G  0 part 
sr0              11:0    1    4G  0 rom  

         分完分区后,对这两个分区进行创建物理卷操作,命令如下:

[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               78lNjC-ofC2-YQIH-O2RA-3ZHG-N8dM-d4Hve2
   
"/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               73POMD-3fHz-k0Gj-vG64-KAA0-qnzO-ZqdvnB
   
"/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               ImEUkD-dEb9-zvE3-gyO4-9kpN-MPCb-NchoSf

 2.3.2、创建卷组

        使用刚才创建好的两个物理卷,创建名为myvg的卷组,命令如下:

[root@localhost ~]# vgcreate myvg /dev/sdb[1-2]
Volume group "myvg" successfully created

        查看卷组信息,(可以查看到创建的myvg卷组,名字为centos的卷组是系统卷组,因为在安装系统的时候,是使用LVM模式安装的)命令如下:

[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               2H21hv-L20X-mqhJ-SvzR-crT2-ln9v-jj2gTY
   
--- 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               PYGJuQ-s1Ix-ZwGf-kFaV-4Lfh-ooHl-QXcy6a

        当多个物理卷组合成一个卷组后时,LVM会在所有的物理卷上做类似格式化的工作,将每个物理卷切成一块一块的空间,这一块一块的空间就称为PE(Physical Extent),它的默认大小是4 MB。

        由于受内核限制的原因,一个逻辑卷(Logic Volume)最多只能包含65536个PE(Physical Extent),所以一个PE的大小就决定了逻辑卷的最大容量,4 MB的PE决定了单个逻辑卷最大容量为256 GB,若希望使用大于256 Gb的逻辑卷,则创建卷组时需要指定更大的PE。

        删除卷组,重新创建卷组,并指定PE大小为16 MB,命令如下:

[root@localhost ~]# vgremove myvg
Volume group "myvg" successfully removed
[root@localhost ~]# vgcreate -s 16m myvg /dev/sdb[1-2]
Volume group "myvg" successfully created
[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               2H21hv-L20X-mqhJ-SvzR-crT2-ln9v-jj2gTY
   
--- 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.97 GiB
PE Size               16.00 MiB
Total PE              638
Alloc PE / Size       0 / 0   
Free  PE / Size       638 / 9.97 GiB
VG UUID               dU0pP2-EW9d-6c0h-8tgQ-t1bN-tBIo-FDqfdR

        可以查看到现在myvg卷组的PE大小为16 MB。

        向卷组myvg中添加一个物理卷,在/dev/sdb上再分一个/dev/sdb3分区,把该分区加到卷组myvg中。命令如下:

[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 
├─sdb1            8:17   0    5G  0 part 
├─sdb2            8:18   0    5G  0 part 
└─sdb3            8:19   0    5G  0 part 
sr0              11:0    1    4G  0 rom  

        将创建的/dev/sdb3添加到myvg卷组中,在添加的过程中,会自动将/dev/sdb3创建为物理卷,命令如下:

[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
[root@localhost ~]# vgdisplay myvg
--- Volume group ---
VG Name               myvg
System ID             
Format                lvm2
Metadata Areas        3
Metadata Sequence No  2
VG Access             read/write
VG Status             resizable
MAX LV                0
Cur LV                0
Open LV               0
Max PV                0
Cur PV                3
Act PV                3
VG Size               14.95 GiB
PE Size               16.00 MiB
Total PE              957
Alloc PE / Size       0 / 0   
Free  PE / Size       957 / 14.95 GiB
VG UUID               dU0pP2-EW9d-6c0h-8tgQ-t1bN-tBIo-FDqfdR

        可以查看到现在卷组中存在3个物理卷设备。

2.3.3、创建逻辑卷

        创建逻辑卷,名称为mylv,大小为5 GB。命令如下:

[root@localhost ~]# lvcreate -L +5G -n mylv myvg
Logical volume "mylv" created.

 解析:

  •  -L:创建逻辑卷的大小large。
  •  -n:创建的逻辑卷名称name。

        查看逻辑卷,命令如下:

[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 

         扫描上一步创建的lv逻辑卷。命令如下:

[root@localhost ~]# mkfs.ext4 /dev/mapper/myvg-mylv 
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
327680 inodes, 1310720 blocks
65536 blocks (5.00%) reserved for the super user
First data block=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: done                            
Writing inode tables: done                            
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

        把逻辑卷mylv挂载到/mnt下并验证。命令如下:

[root@localhost ~]# mount /dev/mapper/myvg-mylv /mnt/
[root@localhost ~]# df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   18G  872M   17G   5% /
devtmpfs                 1.9G     0  1.9G   0% /dev
tmpfs                    1.9G     0  1.9G   0% /dev/shm
tmpfs                    1.9G  8.6M  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

        然后对创建的LVM卷扩容至1 GB。

[root@localhost ~]# lvextend -L +1G /dev/mapper/myvg-mylv
Size of logical volume myvg/mylv changed from 5.00 GiB (320 extents) to 6.00 GiB (384 extents).
Logical volume mylv successfully resized.
[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-ao----  6.00g     
[root@localhost ~]# df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   18G  872M   17G   5% /
devtmpfs                 1.9G     0  1.9G   0% /dev
tmpfs                    1.9G     0  1.9G   0% /dev/shm
tmpfs                    1.9G  8.6M  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

        可以查看到LVM卷的大小变成了6 GB,但是挂载信息中没有发生变化,这时系统还识别不了新添加的磁盘文件系统,所以还需要对文件系统进行扩容。

[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
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   18G  872M   17G   5% /
devtmpfs                 1.9G     0  1.9G   0% /dev
tmpfs                    1.9G     0  1.9G   0% /dev/shm
tmpfs                    1.9G  8.6M  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

        扩容逻辑卷成功。

3、总结

        LVM(逻辑卷管理器)是Linux系统中一种强大的磁盘管理工具,它通过将物理硬盘分区组合成卷组,并从中创建可灵活调整大小的逻辑卷来提供高度的存储灵活性。LVM的主要优势在于能够在线调整逻辑卷的大小、支持数据镜像和快照功能以增强数据保护能力,并且可以通过条带化技术提高读写性能。使用一系列命令,如pvcreate, vgcreate, lvcreate等可以方便地初始化物理卷、创建卷组及逻辑卷;而lvextendlvreduce等命令则使得扩展或缩小逻辑卷变得简单易行。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值