LVM (logical volume mammager)逻辑卷创建
名词解释
PP(physical parttion) 物理分区
PV(physical volume) 物理卷 在lvm最底层,一般一个PV对应一个PP
PE(physical extends) 物理区域 PV中用于分配的最小存储单位,同一个VG所有的PV中的PE大小相同。
VG(Volume group) 卷组 卷组创建在PV之上,可以划分多个PV
LE(Logical extends) 逻辑扩展单元 LE是组成LV的基本单元,一个LE对应一个PE
LV(logical volume) 逻辑卷 创建在VG之上,是一个可以动态扩容的分区概念
创建LVM流程
1.物理分区阶段,针对物理磁盘或分区,进行fdisk格式化,修改该系统ID,默认为83,改为8e类型(LVM类型)
2.PV阶段,通过pvcreate,pvdisplay将系统分区改为物理卷PV
3.创建VG阶段,通过vgcreate,vgdisplay,将创建好的物理卷PV改为物理卷组VG
4.创建LV,通过lvcreate,将卷组分为若干个逻辑卷
常用命令
一、PV物理卷
pvcreate 创建物理卷
pvscan 扫描物理卷信息
pvdispaky 显示详细物理卷信息
pvremove 删除物理卷
二、VG卷组
vgcreate 创建卷组
vgscan
vgdisplay
vgreduce 缩小卷组,把物理卷从卷组中移除
vgextend 扩大卷组,把新物理卷加入卷组中
vgremove 删除卷组
三、LV逻辑卷
lvcreate
lvscan
lvs
lvdisplay
lvextend 扩容
lvreduce 缩小
lvremove 删除
实验环境:2块硬盘sdb/sdc,创建物理卷并进行测试
1.查看磁盘情况后进行创建PV物理卷组
[root@7 ~]# pvcreate /dev/sdb /dev/sdc WARNING: ext4 signature detected on /dev/sdb at offset 1080. Wipe it? [y/n]: y Wiping ext4 signature on /dev/sdb. WARNING: dos signature detected on /dev/sdb at offset 510. Wipe it? [y/n]: y Wiping dos signature on /dev/sdb. Physical volume "/dev/sdb" successfully created. Physical volume "/dev/sdc" successfully created.
[root@7 ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sdb lvm2 --- 10.00g 10.00g
/dev/sdc lvm2 --- 10.00g 10.00g
注意住过创建不成功记得检查df-h是否有挂载冲突。或是硬盘类型改为8e才可创建pv卷组fdisk t 8e
2.创建vg卷组
[root@7 ~]# vgcreate imp_vg1 /dev/sdb /dev/sdc Volume group "imp_vg1" successfully created [root@7 ~]# vgs VG #PV #LV #SN Attr VSize VFree imp_vg1 2 0 0 wz--n- 19.99g 19.99g
3.扩容/缩小卷组(自理解为物理层)
[root@7 ~]# vgreduce imp_vg1 /dev/sdc #缩小卷组(移除SDC硬盘) Removed "/dev/sdc" from volume group "imp_vg1"
[root@7 ~]# vgs VG #PV #LV #SN Attr VSize VFree imp_vg1 1 0 0 wz--n- <10.00g <10.00g
[root@7 ~]# vgextend imp_vg1 /dev/sdc #扩容卷组(添加新硬盘到已知imp_vg1卷组中)
Volume group "imp_vg1" successfully extended
4.创建逻辑卷组LV
[root@7 ~]# lvcreate -n lv1 -L +1G imp_vg1 # -n 指定名称 -L 指定大小 WARNING: ext4 signature detected on /dev/imp_vg1/lv1 at offset 1080. Wipe it? [y/n]: ye Wiping ext4 signature on /dev/imp_vg1/lv1. Logical volume "lv1" created.
[root@7 ~]# lvdisplay
--- Logical volume ---
LV Path /dev/imp_vg1/lv1
LV Name lv1
VG Name imp_vg1
LV UUID VOpcvi-CaP8-pJli-cOM8-lRqD-qrQu-M5i8d4
LV Write Access read/write
LV Creation host, time 7.2Samba-11, 2020-06-04 13:37:06 +0800
LV Status available
# open 0
LV Size 1.00 GiB
Current LE 256
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:0
5.逻辑卷LV进行格式化
[root@7 ~]# mkfs.xfs /dev/imp_vg1/lv1 meta-data=/dev/imp_vg1/lv1 isize=512 agcount=4, agsize=65536 blks = sectsz=512 attr=2, projid32bit=1 = crc=1 finobt=0, sparse=0 data = bsize=4096 blocks=262144, 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
6.创建存储位置并进行挂载测试
[root@7 ~]# mkfs.xfs /dev/imp_vg1/lv1 meta-data=/dev/imp_vg1/lv1 isize=512 agcount=4, agsize=65536 blks = sectsz=512 attr=2, projid32bit=1 = crc=1 finobt=0, sparse=0 data = bsize=4096 blocks=262144, 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 [root@7 ~]# mkdir /data/imp_lv1 [root@7 ~]# mount /dev/imp_vg1/lv1 /data/imp_lv1/ [root@7 ~]# lsblk -f NAME FSTYPE LABEL UUID MOUNTPOINT sda ├─sda1 xfs b43e381f-0c13-47e1-9947-52bf12c6e306 /boot ├─sda2 swap 99145d65-4125-460f-a65f-a4fd9531ac19 [SWAP] └─sda3 xfs 707161dc-2a6e-40f5-b052-c985aa579eb9 / sdb LVM2_member wodOpO-OdK7-ztlH-vC8q-8s2N-gIRv-NrEzTR └─imp_vg1-lv1 xfs 5314b33e-1c69-4a7b-9d04-a5cbe5dd91b0 /data/imp_lv1 sdc LVM2_member QFF6D2-Yrt3-92Xw-v3wh-3wBf-2QMT-Ay5oPP sr0 iso9660 CentOS 7 x86_64 2018-11-25-23-54-16-00
7.写入开机自启动挂载项中
[root@7 ~]# cat /etc/fstab # /etc/fstab # Created by anaconda on Wed Sep 4 15:30:18 2019 # # Accessible filesystems, by reference, are maintained under '/dev/disk' # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info # UUID=707161dc-2a6e-40f5-b052-c985aa579eb9 / xfs defaults 0 0 UUID=b43e381f-0c13-47e1-9947-52bf12c6e306 /boot xfs defaults 0 0 UUID=99145d65-4125-460f-a65f-a4fd9531ac19 swap swap defaults 0 0 UUID=5314b33e-1c69-4a7b-9d04-a5cbe5dd91b0 /data/imp_lv1 xfs defaults 0 0
8.扩容逻辑卷
[root@7 ~]# lvextend -L +3G /dev/imp_vg1/lv1 Size of logical volume imp_vg1/lv1 changed from 1.00 GiB (256 extents) to 4.00 GiB (1024 extents). Logical volume imp_vg1/lv1 successfully resized. [root@7 ~]# lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert lv1 imp_vg1 -wi-ao---- 4.00g [root@7 ~]# lvdisplay --- Logical volume --- LV Path /dev/imp_vg1/lv1 LV Name lv1 VG Name imp_vg1 LV UUID VOpcvi-CaP8-pJli-cOM8-lRqD-qrQu-M5i8d4 LV Write Access read/write LV Creation host, time 7.2Samba-11, 2020-06-04 13:37:06 +0800 LV Status available # open 1 LV Size 4.00 GiB Current LE 1024 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 8192 Block device 253:0
9.调整XFS文件系统大小,否则无法读取容量
[root@7 ~]# df -h #没有调整文件大小之前
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 27G 1.9G 26G 7% /
devtmpfs 980M 0 980M 0% /dev
tmpfs 991M 0 991M 0% /dev/shm
tmpfs 991M 9.5M 981M 1% /run
tmpfs 991M 0 991M 0% /sys/fs/cgroup
/dev/sda1 1014M 127M 888M 13% /boot
/dev/mapper/imp_vg1-lv1 1014M 33M 982M 4% /data/imp_lv1
tmpfs 199M 0 199M 0% /run/user/0
[root@7 ~]# xfs_growfs /dev/imp_vg1/lv1 meta-data=/dev/mapper/imp_vg1-lv1 isize=512 agcount=4, agsize=65536 blks = sectsz=512 attr=2, projid32bit=1 = crc=1 finobt=0 spinodes=0 data = bsize=4096 blocks=262144, 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 262144 to 1048576 [root@7 ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/sda3 27G 1.9G 26G 7% / devtmpfs 980M 0 980M 0% /dev tmpfs 991M 0 991M 0% /dev/shm tmpfs 991M 9.5M 981M 1% /run tmpfs 991M 0 991M 0% /sys/fs/cgroup /dev/sda1 1014M 127M 888M 13% /boot /dev/mapper/imp_vg1-lv1 4.0G 33M 4.0G 1% /data/imp_lv1 tmpfs 199M 0 199M 0% /run/user/0