虚拟机LVM卷扩展实际操作
一、LVM
LVM是逻辑盘卷管理(Logical Volume Manager)的简称,它是Linux环境下对磁盘分区进行管理的一种机制,LVM是建立在硬盘和分区之上的一个逻辑层,来提高磁盘分区管理的灵活性。LVM是在磁盘分区和文件系统之间添加的一个逻辑层,来为文件系统屏蔽下层磁盘分区布局,提供一个抽象的盘卷,在盘卷上建立文件系统。物理卷(physical volume)物理卷就是指硬盘分区或从逻辑上与磁盘分区具有同样功能的设备(如RAID),是LVM的基本存储逻辑块,但和基本的物理存储介质(如分区、磁盘等)比较,却包含有与LVM相关的管理参数。
结构:在整个LVM结构当中以此分为:“物理卷、物理卷分区、卷组、逻辑卷”,下面我详细解释一下这四种结构:
物理卷:Physical Volume,简称PV,一个物理卷只不过是一个有LVM管理数据添加在里面的物理存储介质。要使用LVM系统,首先对要用于LVM的磁盘进行初始化,初始化的目的就是将磁盘或分区标识为LVM 的物理卷。使用pvcreate 命令可以将一个磁盘标记为 LVM 物理卷。
物理分区:Physical Extents,简称PE,LVM将每个物理卷分别叫做物理分区的可寻址存储单元,存储单元的大小通常为几MB。磁盘的开头部分为LVM元数据,之后从索引为零开始,每个物理分区的索引依次递增一,按顺序进行分配。
卷组:Volume Group,简称VG,物理卷可以组织为卷组。卷组可以由一个或多个物理卷组成,同时系统中可以有多个卷组。创建了卷组之后,该卷组(而不是磁盘)便是表示数据存储的实体。因此,尽管以前是将磁盘从一个系统移动到另一个系统,使用了 LVM 之后,会将卷组从一个系统移动到另一个系统。出于这种原因,通常在一个系统上创建多个卷组会比较方便。
逻辑分区:Logical Extents,简称LE,逻辑卷的基本分配单元称为逻辑分区。逻辑分区映射到物理分区,因此,如果物理分区的尺寸小为4MB,那么逻辑分区的尺寸也将为4MB。逻辑卷的大小取决于所分配的逻辑分区数量。
二、实际操作
场景:扩展虚拟机已有磁盘空间。
[root@localhost ~]# cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core)
1.vmware为虚拟机增加存储空间
扩展前
[root@localhost ~]# df -hT
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/centos-root xfs 47G 2.0G 45G 5% /
devtmpfs devtmpfs 903M 0 903M 0% /dev
tmpfs tmpfs 915M 0 915M 0% /dev/shm
tmpfs tmpfs 915M 9.4M 906M 2% /run
tmpfs tmpfs 915M 0 915M 0% /sys/fs/cgroup
/dev/sda1 xfs 1014M 146M 869M 15% /boot
tmpfs tmpfs 183M 0 183M 0% /run/user/0
[root@localhost ~]# fdisk -l
Disk /dev/sda: 53.7 GB, 53687091200 bytes, 104857600 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
Disk label type: dos
Disk identifier: 0x000ad192
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 104857599 51379200 8e Linux LVM
Disk /dev/mapper/centos-root: 50.5 GB, 50457477120 bytes, 98549760 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
Disk /dev/mapper/centos-swap: 2147 MB, 2147483648 bytes, 4194304 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
- 扩展操作
关闭虚拟机-设置-硬盘(SCSI)-扩展即可!
扩展后(增加10G)
注意:fdisk -l 执行结果,空间增加约10G,但此时df -hT看不出变化。
[root@localhost ~]# df -hT
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/centos-root xfs 47G 2.1G 45G 5% /
devtmpfs devtmpfs 903M 0 903M 0% /dev
tmpfs tmpfs 915M 0 915M 0% /dev/shm
tmpfs tmpfs 915M 9.3M 906M 2% /run
tmpfs tmpfs 915M 0 915M 0% /sys/fs/cgroup
/dev/sda1 xfs 1014M 146M 869M 15% /boot
tmpfs tmpfs 183M 0 183M 0% /run/user/0
[root@localhost ~]# fdisk -l
Disk /dev/sda: 64.4 GB, 64424509440 bytes, 125829120 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
Disk label type: dos
Disk identifier: 0x000ad192
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 104857599 51379200 8e Linux LVM
Disk /dev/mapper/centos-root: 50.5 GB, 50457477120 bytes, 98549760 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
Disk /dev/mapper/centos-swap: 2147 MB, 2147483648 bytes, 4194304 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
2.创建分区
说明:
m-帮助
n-新建分区
t-修改分区类型(与原先保持一致,都是8e,代表Linux LVM)
w-保存
q-退出
[root@localhost ~]# fdisk /dev/sda
Welcome to fdisk (util-linux 2.23.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Command (m for help): m
Command action
a toggle a bootable flag
b edit bsd disklabel
c toggle the dos compatibility flag
d delete a partition
g create a new empty GPT partition table
G create an IRIX (SGI) partition table
l list known partition types
m print this menu
n add a new partition
o create a new empty DOS partition table
p print the partition table
q quit without saving changes
s create a new empty Sun disklabel
t change a partition's system id
u change display/entry units
v verify the partition table
w write table to disk and exit
x extra functionality (experts only)
Command (m for help): p
Disk /dev/sda: 64.4 GB, 64424509440 bytes, 125829120 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
Disk label type: dos
Disk identifier: 0x000ad192
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 104857599 51379200 8e Linux LVM
Command (m for help): n
Partition type:
p primary (2 primary, 0 extended, 2 free)
e extended
Select (default p):
Using default response p
Partition number (3,4, default 3):
First sector (104857600-125829119, default 104857600):
Using default value 104857600
Last sector, +sectors or +size{K,M,G} (104857600-125829119, default 125829119):
Using default value 125829119
Partition 3 of type Linux and of size 10 GiB is set
Command (m for help): p
Disk /dev/sda: 64.4 GB, 64424509440 bytes, 125829120 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
Disk label type: dos
Disk identifier: 0x000ad192
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 104857599 51379200 8e Linux LVM
/dev/sda3 104857600 125829119 10485760 83 Linux
Command (m for help): t
Partition number (1-3, default 3): 3
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 C: c6 DRDOS/sec (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
9 AIX bootable 4f QNX4.x 3rd part 93 Amoeba e1 DOS access
a OS/2 Boot Manag 50 OnTrack DM 94 Amoeba BBT e3 DOS R/O
b W95 FAT32 51 OnTrack DM6 Aux 9f BSD/OS e4 SpeedStor
c W95 FAT32 (LBA) 52 CP/M a0 IBM Thinkpad hi eb BeOS fs
e W95 FAT16 (LBA) 53 OnTrack DM6 Aux a5 FreeBSD ee GPT
f W95 Ext'd (LBA) 54 OnTrackDM6 a6 OpenBSD ef EFI (FAT-12/16/
10 OPUS 55 EZ-Drive a7 NeXTSTEP f0 Linux/PA-RISC b
11 Hidden FAT12 56 Golden Bow a8 Darwin UFS f1 SpeedStor
12 Compaq diagnost 5c Priam Edisk a9 NetBSD f4 SpeedStor
14 Hidden FAT16 <3 61 SpeedStor ab Darwin boot f2 DOS secondary
16 Hidden FAT16 63 GNU HURD or Sys af HFS / HFS+ fb VMware VMFS
17 Hidden HPFS/NTF 64 Novell Netware b7 BSDI fs fc VMware VMKCORE
18 AST SmartSleep 65 Novell Netware b8 BSDI swap fd Linux raid auto
1b Hidden W95 FAT3 70 DiskSecure Mult bb Boot Wizard hid fe LANstep
1c Hidden W95 FAT3 75 PC/IX be Solaris boot ff BBT
1e Hidden W95 FAT1 80 Old Minix
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'
Command (m for help): p
Disk /dev/sda: 64.4 GB, 64424509440 bytes, 125829120 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
Disk label type: dos
Disk identifier: 0x000ad192
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 104857599 51379200 8e Linux LVM
/dev/sda3 104857600 125829119 10485760 8e Linux LVM
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.
[root@localhost ~]# partprobe
Warning: Unable to open /dev/sr0 read-write (Read-only file system). /dev/sr0 has been opened read-only.
[root@localhost ~]# reboot
3.创建物理卷
注意:
/dev/sda3 的Allocatable为NO,PE Size为0
[root@localhost ~]# pvdisplay
--- Physical volume ---
PV Name /dev/sda2
VG Name centos
PV Size <49.00 GiB / not usable 3.00 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 12543
Free PE 1
Allocated PE 12542
PV UUID 9X3qlf-viCH-Cymx-D1eI-vKdd-Ua14-F4bweF
[root@localhost ~]# pvcreate /dev/sda3
Physical volume "/dev/sda3" successfully created.
[root@localhost ~]# pvdisplay
--- Physical volume ---
PV Name /dev/sda2
VG Name centos
PV Size <49.00 GiB / not usable 3.00 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 12543
Free PE 1
Allocated PE 12542
PV UUID 9X3qlf-viCH-Cymx-D1eI-vKdd-Ua14-F4bweF
"/dev/sda3" is a new physical volume of "10.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sda3
VG Name
PV Size 10.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID dR5sDM-j21d-xGiB-32WH-xhe4-Sa96-t03jtb
4.扩展卷组
[root@localhost ~]# vgdisplay
--- 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 <49.00 GiB
PE Size 4.00 MiB
Total PE 12543
Alloc PE / Size 12542 / 48.99 GiB
Free PE / Size 1 / 4.00 MiB
VG UUID 682d9M-yRsn-60BJ-cY7f-nAVI-WA24-KmnhD1
[root@localhost ~]# vgextend centos /dev/sda3
Volume group "centos" successfully extended
[root@localhost ~]# vgdisplay
--- Volume group ---
VG Name centos
System ID
Format lvm2
Metadata Areas 2
Metadata Sequence No 4
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 2
Open LV 2
Max PV 0
Cur PV 2
Act PV 2
VG Size 58.99 GiB
PE Size 4.00 MiB
Total PE 15102
Alloc PE / Size 12542 / 48.99 GiB
Free PE / Size 2560 / 10.00 GiB
VG UUID 682d9M-yRsn-60BJ-cY7f-nAVI-WA24-KmnhD1
5.逻辑卷扩展
[root@localhost ~]# lvdisplay
--- Logical volume ---
LV Path /dev/centos/swap
LV Name swap
VG Name centos
LV UUID DmOkUA-Fq7K-eTwy-I338-jwiv-Qt7g-s9kdFW
LV Write Access read/write
LV Creation host, time localhost, 2020-01-08 11:35:41 -0500
LV Status available
# open 2
LV Size 2.00 GiB
Current LE 512
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:1
--- Logical volume ---
LV Path /dev/centos/root
LV Name root
VG Name centos
LV UUID KZEsXa-9uNA-Nei7-gnu3-1tmU-BDwq-LSFo7Q
LV Write Access read/write
LV Creation host, time localhost, 2020-01-08 11:35:42 -0500
LV Status available
# open 1
LV Size 46.99 GiB
Current LE 12030
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:0
[root@localhost ~]# lvextend -l +100%free /dev/mapper/centos-root
Size of logical volume centos/root changed from 46.99 GiB (12030 extents) to 56.99 GiB (14590 extents).
Logical volume centos/root successfully resized.
6.动态格式化扩容磁盘
[root@localhost ~]# lvextend -l +100%free /dev/mapper/centos-root
Size of logical volume centos/root changed from 46.99 GiB (12030 extents) to 56.99 GiB (14590 extents).
Logical volume centos/root successfully resized.
[root@localhost ~]# man lvextend
[root@localhost ~]# xfs_growfs /dev/mapper/centos-root
meta-data=/dev/mapper/centos-root isize=512 agcount=4, agsize=3079680 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0 spinodes=0
data = bsize=4096 blocks=12318720, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal bsize=4096 blocks=6015, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
data blocks changed from 12318720 to 14940160
7.验证(成功)
[root@localhost ~]# df -hT
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/centos-root xfs 57G 2.1G 55G 4% /
devtmpfs devtmpfs 903M 0 903M 0% /dev
tmpfs tmpfs 915M 0 915M 0% /dev/shm
tmpfs tmpfs 915M 9.3M 906M 2% /run
tmpfs tmpfs 915M 0 915M 0% /sys/fs/cgroup
/dev/sda1 xfs 1014M 146M 869M 15% /boot
tmpfs tmpfs 183M 0 183M 0% /run/user/0
三、汇总
调整磁盘大小前强烈建议对重要数据进行备份,特别是数据库数据。
df -hT
# 列出磁盘
fdisk -l
# 操作磁盘/dev/sda
fdisk /dev/sda
Command (m for help): p
Command (m for help): n
Command (m for help): t
Command (m for help): w
# 同步磁盘状态(若同步失败,重启虚拟机)
partprobe
reboot
# 查看物理卷
pvdisplay
# 创建物理卷
pvcreate /dev/sda3
# 查看卷组(1个或多个)
vgdisplay
# 扩展卷组centos
vgextend centos /dev/sda3
# 查看lvm卷
lvdisplay
# 扩展lvm卷/dev/mapper/centos-root
lvextend -l +100%free /dev/mapper/centos-root
# 动态扩容
xfs_growfs /dev/mapper/centos-root
# 确认
df -hT
若宿主机新增一块磁盘;第2步(创建分区)改为操作磁盘/dev/sdb即可!
```shell
[root@localhost ~]# fdisk /dev/sdb
转载:
LVM扩展卷