Linux系统——lvm逻辑卷
- 一、lvm逻辑卷
- 1、lvm操作流程
- 2、操作指令
- 二、逻辑卷操作
- 1、创建逻辑卷
- 1.1 /dev/cloud/openstack 5G xfs /cloud/openstack
- 1.2 /dev/cloud/docker 10G ext4 /cloud/docker
- 2、逻辑卷扩容
- 2.1 扩容流程
- 2.2 需求一:扩容ext4文件系统的逻辑卷
- 2.3 需求二:扩展xfs文件系统的逻辑卷
- 2.2.4 需求三:扩展逻辑卷(卷组空间不足)
- 3、逻辑卷缩容
- 3.1 缩容流程
- 3.2 需求一:将逻辑卷docker缩减到5G
- 4、删除逻辑卷
一、lvm逻辑卷
Logical Volume Managemant
优势: 在线扩容、缩容
1、lvm操作流程
2、操作指令
- 查看PV
[root@martin-host ~]# pvscan
PV /dev/sda2 VG centos lvm2 [19.21 GiB / 0 free]
Total: 1 [19.21 GiB] / in use: 1 [19.21 GiB] / in no VG: 0 [0 ]
- 查看VG
[root@martin-host ~]# pvscan
PV /dev/sda2 VG centos lvm2 [19.21 GiB / 0 free]
Total: 1 [19.21 GiB] / in use: 1 [19.21 GiB] / in no VG: 0 [0 ]
[root@martin-host ~]#
[root@martin-host ~]# vgscan
Reading volume groups from cache.
Found volume group "centos" using metadata type lvm2
[root@martin-host ~]# vgdisplay centos
--- 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.21 GiB
PE Size 4.00 MiB
Total PE 4919
Alloc PE / Size 4919 / 19.21 GiB
Free PE / Size 0 / 0
VG UUID Pgh5Zq-mdOk-I64Z-NDPa-XkUb-RpsR-JRzCsk
- 查看lv
[root@martin-host ~]# lvscan
ACTIVE '/dev/centos/swap' [1.00 GiB] inherit
ACTIVE '/dev/centos/root' [18.21 GiB] inherit
[root@martin-host ~]# ls -l /dev/centos/root
lrwxrwxrwx 1 root root 7 10月 11 09:02 /dev/centos/root -> ../dm-0
[root@martin-host ~]# ls -l /dev/dm-0
brw-rw---- 1 root disk 253, 0 10月 11 09:02 /dev/dm-0
二、逻辑卷操作
1、创建逻辑卷
1.1 /dev/cloud/openstack 5G xfs /cloud/openstack
- 确认裸设备
[root@martin-host ~]# blkid /dev/sde
[root@martin-host ~]# blkid /dev/sdf
- 创建pv物理卷
[root@martin-host ~]# pvcreate /dev/sde /dev/sdf
Physical volume "/dev/sde" successfully created.
Physical volume "/dev/sdf" successfully created.
[root@martin-host ~]#
[root@martin-host ~]# pvscan
PV /dev/sda2 VG centos lvm2 [19.21 GiB / 0 free]
PV /dev/sdf lvm2 [20.00 GiB]
PV /dev/sde lvm2 [20.00 GiB]
Total: 3 [59.21 GiB] / in use: 1 [19.21 GiB] / in no VG: 2 [40.00 GiB]
- 创建卷组,名称cloud
[root@martin-host ~]# vgcreate cloud /dev/sde /dev/sdf
Volume group "cloud" successfully created
[root@martin-host ~]# vgdisplay cloud
--- Volume group ---
VG Name cloud
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 39.99 GiB
PE Size 4.00 MiB
Total PE 10238
Alloc PE / Size 0 / 0
Free PE / Size 10238 / 39.99 GiB
VG UUID 2cyeQa-pxwF-TDQ3-3buR-04Tc-My5z-WINKYY
- 创建逻辑卷,名称为openstack
[root@martin-host ~]# lvcreate -L 5G -n openstack cloud
Logical volume "openstack" created.
[root@martin-host ~]#
[root@martin-host ~]# lvscan
ACTIVE '/dev/cloud/openstack' [5.00 GiB] inherit
- 挂载使用
[root@martin-host ~]# mkfs -t xfs /dev/cloud/openstack
[root@martin-host ~]#
[root@martin-host ~]# mkdir -p /cloud/openstack
[root@martin-host ~]# tail -n 1 /etc/fstab
/dev/cloud/openstack /cloud/openstack xfs defaults 0 0
[root@martin-host ~]#
[root@martin-host ~]# mount -a
[root@martin-host ~]# df -hT
文件系统 类型 容量 已用 可用 已用% 挂载点
/dev/mapper/cloud-openstack xfs 5.0G 33M 5.0G 1% /cloud/openstack
[root@martin-host ~]# touch /cloud/openstack/{1..20}
[root@martin-host ~]# ls /cloud/openstack/
1 10 11 12 13 14 15 16 17 18 19 2 20 3 4 5 6 7 8 9
[root@martin-host ~]#
1.2 /dev/cloud/docker 10G ext4 /cloud/docker
[root@martin-host ~]# lvcreate -L 10G -n docker cloud
Logical volume "docker" created.
[root@martin-host ~]# lvscan
ACTIVE '/dev/cloud/openstack' [5.00 GiB] inherit
ACTIVE '/dev/cloud/docker' [10.00 GiB] inherit
ACTIVE '/dev/centos/swap' [1.00 GiB] inherit
ACTIVE '/dev/centos/root' [18.21 GiB] inherit
[root@martin-host ~]# df -hT | grep cloud
/dev/mapper/cloud-openstack xfs 5.0G 33M 5.0G 1% /cloud/openstack
/dev/mapper/cloud-docker ext4 9.8G 37M 9.2G 1% /cloud/docker
2、逻辑卷扩容
2.1 扩容流程
1、确认卷组有足够的剩余空间
2、先扩展逻辑卷的物理大小
3、再扩展逻辑卷的文件系统大小
2.2 需求一:扩容ext4文件系统的逻辑卷
[root@martin-host ~]# blkid /dev/cloud/docker
/dev/cloud/docker: UUID="722ade99-6f1d-40a9-8731-2097b6b8e385" TYPE="ext4"
- 查看卷组的剩余空间
[root@martin-host ~]# vgdisplay cloud | grep -i free
Free PE / Size 6398 / 24.99 GiB
- 扩展逻辑卷物理大小
[root@martin-host ~]# lvextend -L 15G /dev/cloud/docker
Size of logical volume cloud/docker changed from 10.00 GiB (2560 extents) to 15.00 GiB (3840 extents).
Logical volume cloud/docker successfully resized.
[root@martin-host ~]#
[root@martin-host ~]# lvscan
ACTIVE '/dev/cloud/openstack' [5.00 GiB] inherit
ACTIVE '/dev/cloud/docker' [15.00 GiB] inherit
ACTIVE '/dev/centos/swap' [1.00 GiB] inherit
ACTIVE '/dev/centos/root' [18.21 GiB] inherit
[root@martin-host ~]#
[root@martin-host ~]# df -hT | grep "docker"
/dev/mapper/cloud-docker ext4 9.8G 37M 9.2G 1% /cloud/docker
- 扩展文件系统大小
// resize2fs:只适用于ext文件系统
[root@martin-host ~]# resize2fs /dev/cloud/docker
resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/cloud/docker is mounted on /cloud/docker; on-line resizing required
old_desc_blocks = 2, new_desc_blocks = 2
The filesystem on /dev/cloud/docker is now 3932160 blocks long.
[root@martin-host ~]#
[root@martin-host ~]# lvscan | grep "docker"
ACTIVE '/dev/cloud/docker' [15.00 GiB] inherit
[root@martin-host ~]# df -hT | grep "docker"
/dev/mapper/cloud-docker ext4 15G 41M 14G 1% /cloud/docker
[root@martin-host ~]# ls /cloud/docker/
file01 file010 file02 file03 file04 file05 file06 file07 file08 file09
2.3 需求二:扩展xfs文件系统的逻辑卷
[root@martin-host ~]# blkid /dev/cloud/openstack
/dev/cloud/openstack: UUID="6ef5d418-8fa7-410f-8447-d01f636a791c" TYPE="xfs"
[root@martin-host ~]#
[root@martin-host ~]# vgdisplay cloud | grep -i free
Free PE / Size 5118 / 19.99 GiB
[root@martin-host ~]#
[root@martin-host ~]# lvextend -L 20G /dev/cloud/openstack
Size of logical volume cloud/openstack changed from 5.00 GiB (1280 extents) to 20.00 GiB (5120 extents).
Logical volume cloud/openstack successfully resized.
[root@martin-host ~]#
// xfs_growfs: 适用于xfs文件系统
[root@martin-host ~]# xfs_growfs /dev/cloud/openstack
meta-data=/dev/mapper/cloud-openstack isize=512 agcount=4, agsize=327680 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0 spinodes=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 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 1310720 to 5242880
[root@martin-host ~]#
[root@martin-host ~]#
[root@martin-host ~]# lvscan | grep openstack
ACTIVE '/dev/cloud/openstack' [20.00 GiB] inherit
[root@martin-host ~]#
[root@martin-host ~]# df -hT | grep opensta
/dev/mapper/cloud-openstack xfs 20G 33M 20G 1% /cloud/openstack
[root@martin-host ~]#
[root@martin-host ~]#
[root@martin-host ~]# ls /cloud/openstack/
1 10 11 12 13 14 15 16 17 18 19 2 20 3 4 5 6 7 8 9
2.2.4 需求三:扩展逻辑卷(卷组空间不足)
[root@martin-host ~]# vgdisplay cloud | grep -i free
Free PE / Size 1278 / 4.99 GiB
- 扩容卷组的容量
[root@martin-host ~]# pvcreate /dev/sdg
Physical volume "/dev/sdg" successfully created.
[root@martin-host ~]# vgextend cloud /dev/sdg
Volume group "cloud" successfully extended
[root@martin-host ~]# vgdisplay cloud | grep -i free
Free PE / Size 6397 / <24.99 GiB
- 扩展逻辑卷
[root@martin-host ~]# lvextend -L 20G /dev/cloud/docker
[root@martin-host ~]# resize2fs /dev/cloud/docker
3、逻辑卷缩容
注意:
1、谨慎操作、数据无价
2、xfs文件系统不支持缩减
3.1 缩容流程
1、卸载设备, 不支持在线
2、先缩减文件系统大小
3、再缩减逻辑卷的物理大小
3.2 需求一:将逻辑卷docker缩减到5G
- 将数据备份
[root@martin-host ~]# tar czf /backup/docker.tar.gz /cloud/docker/
tar: 从成员名中删除开头的“/”
[root@martin-host ~]#
- 卸载逻辑卷
[root@martin-host ~]# umount /dev/cloud/docker
[root@martin-host ~]#
[root@martin-host ~]# df -hT | grep "docker"
[root@martin-host ~]#
- 检查文件系统是否有损坏
[root@martin-host ~]# e2fsck -f /dev/cloud/docker
e2fsck 1.42.9 (28-Dec-2013)
第一步: 检查inode,块,和大小
第二步: 检查目录结构
第3步: 检查目录连接性
Pass 4: Checking reference counts
第5步: 检查簇概要信息
/dev/cloud/docker: 21/1310720 files (0.0% non-contiguous), 126311/5242880 blocks
- 先缩减文件系统
[root@martin-host ~]# resize2fs /dev/cloud/docker 5G
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/cloud/docker to 1310720 (4k) blocks.
The filesystem on /dev/cloud/docker is now 1310720 blocks long.
- 缩减逻辑卷的物理大小
[root@martin-host ~]# lvreduce -L 5G /dev/cloud/docker
WARNING: Reducing active logical volume to 5.00 GiB.
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce cloud/docker? [y/n]: y
Size of logical volume cloud/docker changed from 20.00 GiB (5120 extents) to 5.00 GiB (1280 extents).
Logical volume cloud/docker successfully resized.
- 重新挂载,验证数据
[root@martin-host ~]# mount /dev/cloud/docker /cloud/docker/
[root@martin-host ~]#
[root@martin-host ~]# ls /cloud/docker/
file01 file010 file02 file03 file04 file05 file06 file07 file08 file09 lost+found
[root@martin-host ~]#
[root@martin-host ~]# lvscan
ACTIVE '/dev/cloud/openstack' [20.00 GiB] inherit
ACTIVE '/dev/cloud/docker' [5.00 GiB] inherit
ACTIVE '/dev/centos/swap' [1.00 GiB] inherit
ACTIVE '/dev/centos/root' [18.21 GiB] inherit
[root@martin-host ~]#
[root@martin-host ~]# df -hT | grep docker
/dev/mapper/cloud-docker ext4 4.8G 33M 4.6G 1% /cloud/docker
[root@martin-host ~]#
4、删除逻辑卷
- 卸载设备
- 删除fstab文件中的挂载条目
- 删除逻辑卷
[root@martin-host ~]# lvremove /dev/cloud/openstack
Do you really want to remove active logical volume cloud/openstack? [y/n]: y
Logical volume "openstack" successfully removed