目录:
1.搭建LVM
2.扩展逻辑卷
3.缩减逻辑卷
4.跨主机迁移卷组
5.同一卷组迁移
1.搭建LVM
(1)添加硬盘
(2)创建物理卷
[root@CentOS7 boot]#pvcreate /dev/sd{c,d}
Physical volume "/dev/sdc" successfully created.
Physical volume "/dev/sdd" successfully created.
[root@CentOS7 boot]#pvs
PV VG Fmt Attr PSize PFree
/dev/sdc lvm2 --- 10.00g 10.00g
/dev/sdd lvm2 --- 10.00g 10.00g
[root@CentOS7 boot]#pvdisplay
"/dev/sdc" is a new physical volume of "10.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sdc
VG Name
PV Size 10.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID HNhwCX-7INB-ZZjj-NVEi-JfWe-ZqIT-JMWFw3
"/dev/sdd" is a new physical volume of "10.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sdd
VG Name
PV Size 10.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID e8exxe-RGQe-R0pt-dFhO-6wBD-fiwX-yhkai9
(3)创建卷组,关联物理卷:
[root@CentOS7 boot]#vgcreate vgtest /dev/sd{c,d}
Volume group "vgtest" successfully created
[root@CentOS7 boot]#pvs
PV VG Fmt Attr PSize PFree
/dev/sdc vgtest lvm2 a-- <10.00g <10.00g
/dev/sdd vgtest lvm2 a-- <10.00g <10.00g
[root@CentOS7 boot]#pvdisplay
--- Physical volume ---
PV Name /dev/sdc
VG Name vgtest
PV Size 10.00 GiB / not usable 4.00 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 2559
Free PE 2559
Allocated PE 0
PV UUID HNhwCX-7INB-ZZjj-NVEi-JfWe-ZqIT-JMWFw3
--- Physical volume ---
PV Name /dev/sdd
VG Name vgtest
PV Size 10.00 GiB / not usable 4.00 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 2559
Free PE 2559
Allocated PE 0
PV UUID e8exxe-RGQe-R0pt-dFhO-6wBD-fiwX-yhkai9
[root@CentOS7 ~]#vgdisplay
--- Volume group ---
VG Name vgtest
System ID
Format lvm2
Metadata Areas 2
Metadata Sequence No 3
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 1
Open LV 1
Max PV 0
Cur PV 2
Act PV 2
VG Size 19.99 GiB
PE Size 4.00 MiB
Total PE 5118
Alloc PE / Size 5118 / 19.99 GiB
Free PE / Size 0 / 0
VG UUID Mr0s0y-CeY3-dpTK-2087-puiS-2czb-JLMG8g
(4)创建逻辑卷,关联卷组
[root@CentOS7 boot]#lvcreate –L 5G –n myslq vgtest
[root@CentOS7 boot]#lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
mysql vgtest -wi-a----- 5.00g
[root@CentOS7 boot]#lvdisplay
--- Logical volume ---
LV Path /dev/vgtest/mysql
LV Name mysql
VG Name vgtest
LV UUID DrudSe-Pnnk-1HH2-e8qD-O2iy-SxrF-x3W5ls
LV Write Access read/write
LV Creation host, time CentOS7.6.localdomain, 2019-06-01 15:29:29 +0800
LV Status available
# open 0
LV Size 5.00 GiB
Current LE 1280
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:0
(5)创建文件系统
[root@CentOS7 boot]#mkfs.xfs /dev/vgtest/mysql
meta-data=/dev/vgtest/mysql isize=512 agcount=4, agsize=327680 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=1310720, 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@CentOS7 boot]#blkid
/dev/sr0: UUID="2018-11-26-14-22-58-00" LABEL="CentOS 7 x86_64" TYPE="iso9660" PTTYPE="dos"
/dev/sda1: UUID="1eae50c5-88fb-47b4-bf47-587c9a555991" TYPE="xfs"
/dev/sda2: UUID="9ad8c591-beee-4b08-af57-219a3829a8fb" TYPE="xfs"
/dev/sda3: UUID="7a149a9a-f924-4dc8-9ace-2e4d7911cf08" TYPE="xfs"
/dev/sda5: UUID="7f1bb893-e4f5-4a98-8f17-6d3acf2aed4c" TYPE="swap"
/dev/sdb1: UUID="d5f15d0c-b2d6-4ed9-839d-0cfab37a59c9" TYPE="swap"
/dev/sdb2: UUID="5562b24f-16ed-49c7-8521-e20e16e1d343" TYPE="ext4"
/data/swapfile: UUID="edb3c1c1-5086-4630-b223-aab2278bf073" TYPE="swap"
/dev/sdb3: UUID="94f0ea78-7825-48a9-b6bb-57d7920e6f21" TYPE="xfs"
/dev/sdc: UUID="HNhwCX-7INB-ZZjj-NVEi-JfWe-ZqIT-JMWFw3" TYPE="LVM2_member"
/dev/sdd: UUID="e8exxe-RGQe-R0pt-dFhO-6wBD-fiwX-yhkai9" TYPE="LVM2_member"
/dev/mapper/vgtest-mysql: UUID="49bc37f5-453a-480b-aa7d-d6847726c68a" TYPE="xfs"
(5)挂载
[root@CentOS7 boot]#mkdir /mnt/mysql
[root@CentOS7 boot]#mount /dev/vgtest/mysql /mnt/mysql/
[root@CentOS7 boot]#df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 100G 4.1G 96G 5% /
devtmpfs 975M 0 975M 0% /dev
tmpfs 991M 0 991M 0% /dev/shm
tmpfs 991M 11M 980M 2% /run
tmpfs 991M 0 991M 0% /sys/fs/cgroup
/dev/sda1 1014M 164M 851M 17% /boot
/dev/sda3 50G 5.1G 45G 11% /data
tmpfs 199M 52K 199M 1% /run/user/0
/dev/sdb3 5.0G 33M 5.0G 1% /home
/dev/mapper/vgtest-mysql 5.0G 33M 5.0G 1% /mnt/mysql
(6)测试性能
[root@CentOS7 boot]#dd if=/dev/zero of=/data/test1 bs=1M count=1024
1024+0 records in
1024+0 records out
1073741824 bytes (1.1 GB) copied, 7.86668 s, 136 MB/s
[root@CentOS7 boot]#dd if=/dev/zero of=/mnt/mysql/test2 bs=1M count=1024
1024+0 records in
1024+0 records out
1073741824 bytes (1.1 GB) copied, 1.44721 s, 742 MB/s
[root@CentOS7 boot]#dd if=/dev/zero of=/mnt/mysql/test3 bs=1M count=1024 conv=fdatasync
1024+0 records in
1024+0 records out
1073741824 bytes (1.1 GB) copied, 1.10808 s, 969 MB/s
用分区做逻辑卷,做的步骤如下:
2.扩展逻辑卷
(1)扩展lv:三种方式:1、百分制 2、PE数3、容量
[root@CentOS7 boot]#lvextend -l +100%free /dev/vgtest/mysql
Size of logical volume vgtest/mysql changed from 5.00 GiB (1280 extents) to 19.99 GiB (5118 extents).
Logical volume vgtest/mysql successfully resized.
(2)同步扩容空间文件系统
[root@CentOS7 boot]# xfs_growfs /mnt/mysql/
3.缩减逻辑卷
注意:XFS文件系统只能扩展,不能缩减,ext文件系统可以缩减
(1)取消挂载
[root@CentOS6 ~]#umount /mnt/vgtest1-lv1
(2)检查文件系统完整性
[root@CentOS6 ~]#resize2fs /dev/vgtest1/lv1 10G
resize2fs 1.41.12 (17-May-2010)
Please run 'e2fsck -f /dev/vgtest1/lv1' first.
(3)缩减文件系统大小,缩减到10G
[root@CentOS6 ~]#resize2fs /dev/vgtest1/lv1 10G
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/vgtest1/lv1 to 2621440 (4k) blocks.
The filesystem on /dev/vgtest1/lv1 is now 2621440 blocks long.
(4)缩减逻辑卷大小
[root@CentOS6 ~]#lvreduce -L 10G /dev/vgtest1/lv1
(5)重新挂载
[root@CentOS6 ~]#mount /dev/vgtest1/lv1 /mnt/vgtest1-lv1
4.跨主机迁移卷组
源主机上
(1)取消挂载旧系统所有逻辑卷,(与目标主机卷组名冲突的,需要改一下源卷组名)
[root@CentOS6 ~]# vgrename vg0 vg1
[root@CentOS6 ~]# vgdisplay
[root@CentOS6 ~]#umount /mnt/vgtest1-lv1
[root@CentOS6 ~]#umount /mnt/vgtest1-lv2
(2)禁用卷组(LV Status-NOT available)
[root@CentOS6 ~]#vgchange -an vgtest1
(3)导出卷组,拆下旧硬盘(VG Status-exported/resizable)
[root@CentOS6 ~]#vgexport vgtest1
目标机上:
(5)安装旧硬盘,导入卷组
[root@CentOS7 ~]#vgimport vgtest1
(6)启用卷组
[root@CentOS7 ~]#vgchange -ay vgtest1
(7)挂载逻辑卷
[root@CentOS7 ~]#mkdir /mnt/vgtest1-lv1;mount /dev/vgtest1/lv1 /mnt/vgtest1-lv1
[root@CentOS7 ~]#mkdir /mnt/vgtest1-lv2;mount /dev/vgtest1/lv2 /mnt/vgtest1-lv2
5.同一卷组迁移:
场景:同一卷组中,某磁盘出现问题,需要更换磁盘,此时需迁移磁盘数据(通过PE空间方式)至同卷组其他空闲位置
(1)迁移PE
[root@CentOS6 ~]# pvmove /dev/dd
(2)卷组中剔除坏的物理卷磁盘
[root@CentOS6 ~]# vgreduce vg0 /dev/sdd
(3)删除磁盘的物理卷标签
[root@CentOS6 ~]# pvremove /dev/sdd
[root@CentOS6 ~]# blkid
6.删除逻辑卷
(1)取消挂载(删除配置文件)
[root@CentOS6 ~]# umount /mnt/vg1-mysql
(2)删除逻辑卷
[root@CentOS6 ~]# lvremove /dev/vg1/mysql
(3)如果卷组上逻辑卷为空,卷组也不要了,可以删除
[root@CentOS6 ~]# vgremove vg1
(4)组成卷组的物理卷不需要的也可以删除
[root@CentOS6 ~]# pvremove /dev/sde