创建lvm

  • 创建物理卷 PV

物理卷(Physical Volumes)简称PV,是在磁盘的物理分区或与磁盘分区具有同样功能的设备(如RAID)上创建而来。它只是在物理分区中划出了一个特殊的区域,用于记载与LVM相关的管理参数

lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sr0 11:0 1 1024M 0 rom
vda 252:0 0 512G 0 disk
├─vda1 252:1 0 200M 0 part /boot
├─vda2 252:2 0 99.8G 0 part
│ └─centos-root 253:0 0 511.8G 0 lvm /
└─vda3 252:3 0 412G 0 part
└─centos-root 253:0 0 511.8G 0 lvm /
vdb 252:16 0 2G 0 disk

将整块磁盘初始化为物理卷
# pvcreate /dev/vdb
Physical volume "/dev/vdb" successfully created.
# pvs
PV VG Fmt Attr PSize PFree
/dev/vda2 centos lvm2 a-- 99.80g 0
/dev/vda3 centos lvm2 a-- <412.00g 0
/dev/vdb lvm2 --- 2.00g 2.00g

  • 创建卷组 VG

卷组(Volume Group)简称VG,它是一个或者多个物理卷的组合。卷组将多个物理卷组合在一起,形成一个可管理的单元,它类似于非LVM系统中的物理硬盘

# vgcreate vg-data /dev/vdb   创建为vg-data卷组,该卷包含/dev/sdb一个物理卷
Volume group "vg-data" successfully created
# vgdisplay
--- Volume group ---
VG Name vg-data
System ID
Format lvm2
Metadata Areas 1
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 1
Act PV 1
VG Size <2.00 GiB
PE Size 4.00 MiB
Total PE 511
Alloc PE / Size 0 / 0
Free PE / Size 511 / <2.00 GiB
VG UUID gPEexr-FwEl-2QHI-F9Mw-0aZ2-2G67-zuRCYI

  • 创建逻辑卷 LV

逻辑卷(Logical Volumes)简称LV,是在卷组中划分的一个逻辑区域,类似于非LVM系统中的硬盘分区

从vg-data卷组中创建一个lv-data逻辑卷,指定95%的空间,因为VG卷组中PE占用一部分
# lvcreate --wipesignatures y -n lv-data vg-data -l 95%VG
Logical volume "lv-data" created.
# lvdisplay
--- Logical volume ---
LV Path /dev/vg-data/lv-data
LV Name lv-data
VG Name vg-data
LV UUID TA7YgD-iDBK-rwmT-gf2O-2Gbi-lnVX-2SsD05
LV Write Access read/write
LV Creation host, time slave-11, 2021-01-20 20:49:31 +0800
LV Status available
# open 0
LV Size 1.89 GiB
Current LE 485
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:1

删除逻辑卷
# lvremove /dev/vg-data/lv-data
Do you really want to remove active logical volume vg-data/lv-data? [y/n]: y
Logical volume "lv-data" successfully removed

通过指定空间大小创建逻辑卷
# lvcreate --wipesignatures y -n lv-data vg-data -L 1.9G
Rounding up size to full physical extent 1.90 GiB
Logical volume "lv-data" created.
  • 创建文件系统
# mkfs.xfs /dev/vg-data/lv-data
meta-data=/dev/vg-data/lv-data isize=512 agcount=4, agsize=124672 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=498688, 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

挂载目录
# mkdir /data
# mount /dev/vg-data/lv-data /data/
# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 512G 12G 500G 3% /
devtmpfs 36G 0 36G 0% /dev
tmpfs 36G 0 36G 0% /dev/shm
tmpfs 36G 6.3M 36G 1% /run
tmpfs 36G 0 36G 0% /sys/fs/cgroup
/dev/vda1 197M 131M 66M 67% /boot
tmpfs 7.1G 0 7.1G 0% /run/user/0
/dev/mapper/vg--data-lv--data 1.9G 33M 1.9G 2% /data

设置开机自启
# echo '/dev/vg-data/lv-data /data xfs defaults 0 0' >>/etc/fstab

热扩容磁盘之后扩容逻辑卷

  • 在虚拟化平台上/dev/vdb 热扩容了3G
# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sr0 11:0 1 1024M 0 rom
vda 252:0 0 512G 0 disk
├─vda1 252:1 0 200M 0 part /boot
├─vda2 252:2 0 99.8G 0 part
│ └─centos-root 253:0 0 511.8G 0 lvm /
└─vda3 252:3 0 412G 0 part
└─centos-root 253:0 0 511.8G 0 lvm /
vdb 252:16 0 5G 0 disk
└─vg--data-lv--data 253:1 0 1.9G 0 lvm /data
  • 扩展物理卷
# pvresize /dev/vdb
Physical volume "/dev/vdb" changed
1 physical volume(s) resized or updated / 0 physical volume(s) not resized
# pvdisplay
--- Physical volume ---
PV Name /dev/vdb
VG Name vg-data
PV Size <5.00 GiB / not usable 3.00 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 1279
Free PE 792
Allocated PE 487
PV UUID vej7dt-Nqmm-WZwF-LhmO-uOq8-XO2z-hObKKg
  • 扩展逻辑卷
# lvextend -l +100%FREE /dev/vg-data/lv-data
Size of logical volume vg-data/lv-data changed from 1.90 GiB (487 extents) to <5.00 GiB (1279 extents).
Logical volume vg-data/lv-data successfully resized.
# lvdisplay
--- Logical volume ---
LV Path /dev/vg-data/lv-data
LV Name lv-data
VG Name vg-data
LV UUID LOmTC1-78oq-j0lO-MijV-7FLB-EHdS-s8PsHc
LV Write Access read/write
LV Creation host, time slave-11, 2021-01-20 20:50:20 +0800
LV Status available
# open 1
LV Size <5.00 GiB
Current LE 1279
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:1

  • 扩展文件系统

使用 resize2fs或xfs_growfs 对挂载目录在线扩容
resize2fs 针对文件系统ext2 ext3 ext4
xfs_growfs 针对文件系统xfs

# xfs_growfs /dev/vg-data/lv-data
meta-data=/dev/mapper/vg--data-lv--data isize=512 agcount=4, agsize=124672 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0 spinodes=0
data = bsize=4096 blocks=498688, 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 498688 to 1309696
# df -h|grep data
/dev/mapper/vg--data-lv--data 5.0G 33M 5.0G 1% /data

通过添加磁盘来扩容逻辑卷

  • 虚拟化平台上创建一块磁盘
# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sr0 11:0 1 1024M 0 rom
vda 252:0 0 512G 0 disk
├─vda1 252:1 0 200M 0 part /boot
├─vda2 252:2 0 99.8G 0 part
│ └─centos-root 253:0 0 511.8G 0 lvm /
└─vda3 252:3 0 412G 0 part
└─centos-root 253:0 0 511.8G 0 lvm /
vdb 252:16 0 5G 0 disk
└─vg--data-lv--data 253:1 0 5G 0 lvm /data
vdc 252:32 0 5G 0 disk
  • 创建物理卷
# pvcreate /dev/vdc
Physical volume "/dev/vdc" successfully created.
  • 扩展卷组
# vgextend vg-data /dev/vdc
Volume group "vg-data" successfully extended
  • 扩展逻辑卷
# lvextend -l +100%FREE /dev/vg-data/lv-data
Size of logical volume vg-data/lv-data changed from <5.00 GiB (1279 extents) to 9.99 GiB (2558 extents).
Logical volume vg-data/lv-data successfully resized.
# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
root centos -wi-ao---- <511.80g
lv-data vg-data -wi-ao---- 9.99g
  • 扩展文件系统
# xfs_growfs /dev/vg-data/lv-data
meta-data=/dev/mapper/vg--data-lv--data isize=512 agcount=11, agsize=124672 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0 spinodes=0
data = bsize=4096 blocks=1309696, 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 1309696 to 2619392
# df -h|grep data
/dev/mapper/vg--data-lv--data 10G 34M 10G 1% /data