Linux CentOS 8(LVM的配置与管理)
Posted 正月十六工作室
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux CentOS 8(LVM的配置与管理)相关的知识,希望对你有一定的参考价值。
Linux CentOS 8(LVM的配置与管理)
目录
一、项目介绍
本节将介绍Linux(Centos8)中LVM的配置与管理。
二、相关概念
2.1 LVM简介
LVM是 Logical Volume Manager(逻辑卷管理)的简写,它是Linux环境下对磁盘分区进行管理的一种机制。Linux用户安装Linux操作系统时遇到的一个常见的难以决定的问题就是如何正确地评估各分区大小,以分配合适的硬盘空间。普通的磁盘分区管理方式在逻辑分区划分好之后就无法改变其大小,当一个逻辑分区存放不下某个文件时,这个文件因为受上层文件系统的限制,也不能跨越多个分区来存放,所以也不能同时放到别的磁盘上。而遇到出现某个分区空间耗尽时,解决的方法通常是使用符号链接,或者使用调整分区大小的工具,但这只是暂时解决办法,没有从根本上解决问题。随着Linux的逻辑卷管理功能的出现,这些问题都迎刃而解,用户在无需停机的情况下可以方便地调整各个分区大小。
2.2 基本术语
物理卷(Physical Volume,PV):指系统的物理存储设备:磁盘,如:/dev/hda、/dev/sda等,是存储系统最底层的存储单元;
物理拓展(Physical Extend,PE):物理卷的最小单元,LV=PE*n;
卷组(Volume Group,VG):类似于非LVM系统中的物理磁盘,其由一个或多个物理卷PV组成。可以在卷组上创建一个或多个LV(逻辑卷);
逻辑卷(Logical Volume,LV):类似于非LVM系统中的磁盘分区,逻辑卷建立在卷组VG之上。在逻辑卷LV之上可以建立文件系统(比如/home或者/usr等)。
2.3 LVM创建过程
创建过程,如下图1所示。
LVM创建过程:
1)多个物理磁盘被格式化为多个PV,每个PV中有多个PE组成;
2)多个PV组成一个VG,多个PV中的PE都在VG中,生成一个PE池;
3)一个LV可由多个PE组成,其中的PE可来自不同的物理硬盘;
4)LV通过格式化并挂载后即可直接使用。
2.4 LVM的特点
- 卷组VG可以使多个硬盘空间看起来像是一个大硬盘。
- 逻辑卷LV可以创建跨多个硬盘空间的分区。
- 在使用逻辑卷LV时,可以在空间不足时动态调整大小,不需要考虑逻辑卷LV在硬盘上的位置,不用担心没有可用的连续的空间。
- 可以在线对卷组VG、逻辑卷LV进行创建、删除、调整大小等操作。但LVM上的文件系统也需要重新调整大小。
- LVM允许创建快照,用来保存文件系统的备份。
注意:LVM是软件的卷管理方式,RAID是磁盘管理的方法。对于重要的数据使,用RAID保护物理硬盘不会因为故障而中断业务,再用LVM来实现对卷的良性管理,更好的利用硬盘资源。
三、任务操作
任务1- 创建LVM
图2 创建逻辑卷的步骤1.1 创建两个1G的主分区,并修改系统类型为8e(LVM)
[root@localhost ~]# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.32.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Command (m for help): n //创建新的分区
Partition type
p primary (0 primary, 0 extended, 4 free)
e extended (container for logical partitions)
Select (default p): p //分区类型为主分区
Partition number (1-4, default 1):
First sector (2048-41943039, default 2048):
Last sector, +sectors or +sizeK,M,G,T,P (2048-41943039, default 41943039): +1G
Created a new partition 1 of type 'Linux' and of size 1 GiB.
Command (m for help): t //更改分区ID
Selected partition 1
Hex code (type L to list all codes): L //查看可选项
0 Empty 24 NEC DOS 81 Minix / old Lin bf Solaris
1 FAT12 27 Hidden NTFS Win 82 Linux swap / So c1 DRDOS/sec (FAT-
2 XENIX root 39 Plan 9 83 Linux c4 DRDOS/sec (FAT-
3 XENIX usr 3c PartitionMagic 84 OS/2 hidden or c6 DRDOS/sec (FAT-
4 FAT16 <32M 40 Venix 80286 85 Linux extended c7 Syrinx
……
(FAT-
4 FAT16 <32M 40 Venix 80286 85 Linux extended c7 Syrinx
5 Extended 41 PPC PReP Boot 86 NTFS volume set da Non-FS data
6 FAT16 42 SFS 87 NTFS volume set db CP/M / CTOS / .
7 HPFS/NTFS/exFAT 4d QNX4.x 88 Linux plaintext de Dell Utility
8 AIX 4e QNX4.x 2nd part 8e Linux LVM df BootIt
# 在最后一行倒数第二个,可以看到Linux LVM的16进制编码为8e
Hex code (type L to list all codes): 8e //Linux LVM的16进制编码为8e
Changed type of partition 'Linux' to 'Linux LVM'.
Command (m for help): n //创建新的分区
Partition type
p primary (1 primary, 0 extended, 3 free)
e extended (container for logical partitions)
Select (default p): p //分区类型为主分区
Partition number (2-4, default 2):
First sector (2099200-41943039, default 2099200):
Last sector, +sectors or +sizeK,M,G,T,P (2099200-41943039, default 41943039): +1G
Created a new partition 2 of type 'Linux' and of size 1 GiB.
Command (m for help): t //更改分区ID
Partition number (1,2, default 2):
Hex code (type L to list all codes): 8e //Linux LVM的16进制编码为8e
Changed type of partition 'Linux' to 'Linux LVM'.
Command (m for help): p //再次查看分区情况
Disk /dev/sdb: 20 GiB, 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
Disklabel type: dos
Disk identifier: 0x99cdba61
Device Boot Start End Sectors Size Id Type
/dev/sdb1 2048 2099199 2097152 1G 8e Linux LVM
/dev/sdb2 2099200 4196351 2097152 1G 8e Linux LVM
Command (m for help): w //保存退出
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
1.2 创建物理卷,主要是添加LVM属性信息并划分PE存储单元.
[root@localhost ~]# pvcreate /dev/sdb1
Physical volume "/dev/sdb1" successfully created.
1.3 创建卷组vg0
[root@localhost ~]# vgcreate vg0 /dev/sdb1
Volume group "vg0" successfully created //物理卷创建好才可建卷组
1.4 创建逻辑卷lv0,大小为300M
[root@localhost ~]# lvcreate -L 300M -n lv0 vg0
Logical volume "lv0" created.
// -L 指定逻辑卷的大小 -n 逻辑卷的名字,卷组创建好才可建逻辑卷
1.5 创建xfs文件系统
[root@localhost ~]# mkfs.xfs /dev/vg0/lv0
meta-data=/dev/vg0/lv0 isize=512 agcount=4, agsize=19200 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=1, sparse=1, rmapbt=0
= reflink=1
data = bsize=4096 blocks=76800, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0, ftype=1
log =internal log bsize=4096 blocks=1368, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
1.6 挂载在/mnt目录下,并设置开机自动挂载
[root@localhost ~]# mount /dev/vg0/lv0 /mnt
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 751M 0 751M 0% /dev
tmpfs 769M 0 769M 0% /dev/shm
tmpfs 769M 8.7M 760M 2% /run
tmpfs 769M 0 769M 0% /sys/fs/cgroup
/dev/mapper/cl-root 17G 1.5G 16G 9% /
/dev/sda1 976M 142M 767M 16% /boot
tmpfs 154M 0 154M 0% /run/user/0
/dev/mapper/vg0-lv0 295M 18M 278M 6% /mnt
[root@localhost ~]# vim /etc/fstab
# /etc/fstab
# Created by anaconda on Thu Jun 3 22:25:51 2021
#
# 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.
#
# After editing this file, run 'systemctl daemon-reload' to update systemd
# units generated from this file.
#
/dev/mapper/cl-root / xfs defaults 0 0
UUID=ad229ff4-8126-495a-b98f-954230112e1a /boot ext4 defaults 1 2
/dev/mapper/cl-swap swap swap defaults 0 0
/dev/vg0/lv0 /mnt xfs defaults 0 0
[root@localhost ~]# mount -a //自动重新挂载
1.7 查看lv0的属性
[root@localhost ~]# ll /dev/vg0/lv0
lrwxrwxrwx. 1 root root 7 Jul 28 22:20 /dev/vg0/lv0 -> ../dm-2
[root@localhost ~]# ll /dev/mapper/vg0-lv0
lrwxrwxrwx. 1 root root 7 Jul 28 22:20 /dev/mapper/vg0-lv0 -> ../dm-2
任务2- 逻辑卷的扩展、缩减与删除
2.1 当vg0足够时,将逻辑卷扩展500M
[root@localhost ~]# lvextend -L +500M /dev/vg0/lv0
Size of logical volume vg0/lv0 changed from 300.00 MiB (75 extents) to 800.00 MiB (200 extents).
Logical volume vg0/lv0 successfully resized.
[root@localhost ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
root cl -wi-ao---- <17.00g
swap cl -wi-ao---- 2.00g
lv0 vg0 -wi-a----- 800.00m
[root@localhost ~]# xfs_growfs /dev/vg0/lv0 //拉伸文件系统
meta-data=/dev/mapper/vg0-lv0 isize=512 agcount=4, agsize=19200 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=1, sparse=1, rmapbt=0
= reflink=1
data = bsize=4096 blocks=76800, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0, ftype=1
log =internal log bsize=4096 blocks=1368, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
data blocks changed from 76800 to 204800
2.2 当vg0不足时
[root@localhost ~]# pvcreate /dev/sdb2 //扩展物理卷
Physical volume "/dev/sdb2" successfully created.
[root@localhost ~]# vgextend vg0 /dev/sdb2 //扩展卷组
Volume group "vg0" successfully extended
[root@localhost ~]# lvextend -L 1200M /dev/vg0/lv0
Size of logical volume vg0/lv0 changed from 800.00 MiB (200 extents) to 1.17 GiB (300 extents).
Logical volume vg0/lv0 successfully resized.
[root@localhost ~]# xfs_growfs /dev/vg0/lv0 //拉伸文件系统
meta-data=/dev/mapper/vg0-lv0 isize=512 agcount=11, agsize=19200 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=1, sparse=1, rmapbt=0
= reflink=1
data = bsize=4096 blocks=204800, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0, ftype=1
log =internal log bsize=4096 blocks=1368, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
data blocks changed from 204800 to 307200
[root@localhost ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
root cl -wi-ao---- <17.00g
swap cl -wi-ao---- 2.00g
lv0 vg0 -wi-ao---- 1.17g
2.3 修改文件系统为ext4,并进行扩展
[root@localhost ~]# umount /mnt //先卸载
[root@localhost ~]# mkfs.ext4 /dev/vg0/lv0 //修改文件系统
mke2fs 1.45.4 (23-Sep-2019)
/dev/vg0/lv0 contains a xfs file system
Proceed anyway? (y,N) y
Creating filesystem with 307200 4k blocks and 76800 inodes
Filesystem UUID: 805db18b-c8aa-49a9-a71f-76a2f37626e8
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912
Allocating group tables: done
Writing inode tables: done
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done
[root@localhost ~]# mount /dev/vg0/lv0 /mnt/
[root@localhost ~]# lvextend -L 1500M /dev/vg0/lv0 //扩展逻辑卷
Size of logical volume vg0/lv0 changed from 1.17 GiB (300 extents) to 1.46 GiB (375 extents).
Logical volume vg0/lv0 successfully resized.
[root@localhost ~]# resize2fs /dev/vg0/lv0 //拉伸文件系统
resize2fs 1.45.4 (23-Sep-2019)
Filesystem at /dev/vg0/lv0 is mounted on /mnt; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 1
The filesystem on /dev/vg0/lv0 is now 384000 (4k) blocks long.
注:xfs文件系统只能扩大不能缩小,ext4文件系统缩放都可以
2.4 LVM的缩减(ext4文件系统下)
[root@localhost ~]# umount /mnt/ //卸载
[root@localhost ~]# e2fsck -f /dev/vg0/lv0 //扫描
e2fsck 1.45.4 (23-Sep-2019)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/vg0/lv0: 11/92160 files (0.0% non-contiguous), 14888/384000 blocks
[root@localhost ~]# resize2fs /dev/vg0/lv0 500M //缩小文件系统到500M
resize2fs 1.45.4 (23-Sep-2019)
Resizing the filesystem on /dev/vg0/lv0 to 128000 (4k) blocks.
The filesystem on /dev/vg0/lv0 is now 128000 (4k) blocks long.
[root@localhost ~]# mount /dev/vg0/lv0 /mnt //重新挂载
[root@localhost ~]# lvreduce -L 500M /dev/vg0/lv0 //缩小设备到500M
WARNING: Reducing active logical volume to 500.00 MiB.
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce vg0/lv0? [y/n]: y
Size of logical volume vg0/lv0 changed from 1.46 GiB (375 extents) to 500.00 MiB (125 extents).
Logical volume vg0/lv0 successfully resized.
[root@localhost ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
root cl -wi-ao---- <17.00g
swap cl -wi-ao---- 2.00g
lv0 vg0 -wi-a----- 500.00m //LV缩减到500M
注:文件系统大小和逻辑卷大小一定要保持一致才行。如果逻辑卷大于文件系统,由于部分区域未格式化成文件系统会造成空间的浪费。如果逻辑卷小于文件系统,哪数据就出问题了。
2.5 LVM物理分区的缩减
[root@localhost ~]# pvmove /dev/sdb1 /dev/sdb2 //转移空间数据
/dev/sdb1: Moved: 2.40%
/dev/sdb1: Moved: 100.00%
[root@localhost ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 cl lvm2 a-- <19.00g 0
/dev/sdb1 vg0 lvm2 a-- 1020.00m 1020.00m
/dev/sdb2 vg0 lvm2 a-- 1020.00m 520.00m //可看出数据转移完成
[root@localhost ~]# vgreduce vg0 /dev/sdb1 //缩减vg
Removed "/dev/sdb1" from volume group "vg0"
[root@localhost ~]# pvremove /dev/sdb1 //移除pv
Labels on physical volume "/dev/sdb1" successfully wiped.
[root@localhost ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 cl lvm2 a-- <19.00g 0
/dev/sdb2 vg0 lvm2 a-- 1020.00m 520.00m
linux CentOS 7 中LVM讲解配置实例及磁盘扩容等