[root@Z ~]# df -TH
文件系统 类型 容量 已用 可用 已用% 挂载点
/dev/mapper/centos-root xfs 38G 2.4G 36G 7% /
devtmpfs devtmpfs 1.1G 0 1.1G 0% /dev
tmpfs tmpfs 1.1G 0 1.1G 0% /dev/shm
tmpfs tmpfs 1.1G 10M 1.1G 1% /run
tmpfs tmpfs 1.1G 0 1.1G 0% /sys/fs/cgroup
/dev/sda1 xfs 1.1G 139M 925M 14% /boot
tmpfs tmpfs 208M 0 208M 0% /run/user/0
[root@Z ~]# fdisk -l
磁盘 /dev/sda:64.4 GB, 64424509440 字节,125829120 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x000b05f5
设备 Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 83886079 40893440 8e Linux LVM
磁盘 /dev/mapper/centos-root:37.7 GB, 37706792960 字节,73646080 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘 /dev/mapper/centos-swap:4160 MB, 4160749568 字节,8126464 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
[root@Z ~]# fdisk /dev/sda
欢迎使用 fdisk (util-linux 2.23.2)。
更改将停留在内存中,直到您决定将更改写入磁盘。
使用写入命令前请三思。
命令(输入 m 获取帮助):p (p是查看当前分区)
磁盘 /dev/sda:64.4 GB, 64424509440 字节,125829120 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x000b05f5
设备 Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 83886079 40893440 8e Linux LVM
命令(输入 m 获取帮助):n(n是新建分区)
Partition type:
p primary (2 primary, 0 extended, 2 free)
e extended
Select (default p): (回车默认选择p主分区)
Using default response p
分区号 (3,4,默认 3): (回车默认3)
起始 扇区 (83886080-125829119,默认为 83886080):
将使用默认值 83886080
Last 扇区, +扇区 or +size{K,M,G} (83886080-125829119,默认为 125829119):+19G(这里给新建分区19G)
分区 3 已设置为 Linux 类型,大小设为 19 GiB
命令(输入 m 获取帮助):t(指定类型)
分区号 (1-3,默认 3):(要指定的分区号,刚刚默认3,这里也默认3)
Hex 代码(输入 L 列出所有代码):8e (8e代表LVM)
已将分区“Linux”的类型更改为“Linux LVM”
命令(输入 m 获取帮助):w (w保存)
The partition table has been altered!
Calling ioctl() to re-read partition table.
WARNING: Re-reading the partition table failed with error 16: 设备或资源忙.
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)
正在同步磁盘。
### 注意:###
保存后,最后几行提示设备资源忙,这时同步不了磁盘
需要重启服务器或者执行partprobe,我这里是执行的命令,如果这里不做操作的话,下面格式化磁盘的时候会报错!!!
[root@Z ~]# fdisk -l
磁盘 /dev/sda:64.4 GB, 64424509440 字节,125829120 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x000b05f5
设备 Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 83886079 40893440 8e Linux LVM
/dev/sda3 83886080 123731967 19922944 8e Linux LVM
磁盘 /dev/mapper/centos-root:37.7 GB, 37706792960 字节,73646080 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
### 格式化分区 ###
[root@Z ~]# mkfs.xfs -f /dev/sda3
meta-data=/dev/sda3 isize=512 agcount=4, agsize=1245184 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=4980736, 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@Z ~]# mkfs.xfs -f /dev/sda3
/dev/sda3: 没有那个文件或目录
### 转换物理卷 ###
[root@Z ~]# pvcreate /dev/sda3
WARNING: xfs signature detected on /dev/sda3 at offset 0. Wipe it? [y/n]: y
Wiping xfs signature on /dev/sda3.
Physical volume "/dev/sda3" successfully created.
### 查看物理卷 ###
[root@Z ~]# pvdisplay
--- Physical volume ---
PV Name /dev/sda2
VG Name centos
PV Size <39.00 GiB / not usable 3.00 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 9983
Free PE 1
Allocated PE 9982
PV UUID v5DSQS-Cumb-xtyK-BWsx-ayfS-T0iT-FC3rbE
"/dev/sda3" is a new physical volume of "19.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sda3
VG Name
PV Size 19.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID Xkbr0u-Ce9h-jvqW-JSZr-nwxY-qYNo-OSqqWm
[root@Z ~]# 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 <39.00 GiB
PE Size 4.00 MiB
Total PE 9983
Alloc PE / Size 9982 / 38.99 GiB
Free PE / Size 1 / 4.00 MiB
VG UUID hTO0Fm-c4q8-yboo-cSig-XLcK-CXo7-JoTInz
[root@Z ~]# vgextend centos /dev/sda3
Volume group "centos" successfully extended
[root@Z ~]# 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 57.99 GiB
PE Size 4.00 MiB
Total PE 14846
Alloc PE / Size 9982 / 38.99 GiB
Free PE / Size 4864 / 19.00 GiB
VG UUID hTO0Fm-c4q8-yboo-cSig-XLcK-CXo7-JoTInz
[root@Z ~]# lvdisplay
--- Logical volume ---
LV Path /dev/centos/swap
LV Name swap
VG Name centos
LV UUID vfUMhU-5XVp-s4uB-lVs3-436b-29Vh-StDsYb
LV Write Access read/write
LV Creation host, time localhost, 2022-03-30 16:58:54 +0800
LV Status available
# open 2
LV Size <3.88 GiB
Current LE 992
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 4KsFu6-jk8H-WsUa-1RVM-2DUq-gyPE-Nb6Rcx
LV Write Access read/write
LV Creation host, time localhost, 2022-03-30 16:58:54 +0800
LV Status available
# open 1
LV Size <35.12 GiB
Current LE 8990
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:0
[root@Z ~]# lvextend -l +4864 /dev/centos/root
Size of logical volume centos/root changed from <35.12 GiB (8990 extents) to <54.12 GiB (13854 extents).
Logical volume centos/root successfully resized.
[root@Z ~]# lvdisplay
--- Logical volume ---
LV Path /dev/centos/swap
LV Name swap
VG Name centos
LV UUID vfUMhU-5XVp-s4uB-lVs3-436b-29Vh-StDsYb
LV Write Access read/write
LV Creation host, time localhost, 2022-03-30 16:58:54 +0800
LV Status available
# open 2
LV Size <3.88 GiB
Current LE 992
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 4KsFu6-jk8H-WsUa-1RVM-2DUq-gyPE-Nb6Rcx
LV Write Access read/write
LV Creation host, time localhost, 2022-03-30 16:58:54 +0800
LV Status available
# open 1
LV Size <54.12 GiB
Current LE 13854
Segments 2
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:0
[root@Z ~]# xfs_growfs /dev/centos/root
meta-data=/dev/mapper/centos-root isize=512 agcount=4, agsize=2301440 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0 spinodes=0
data = bsize=4096 blocks=9205760, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal bsize=4096 blocks=4495, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
data blocks changed from 9205760 to 14186496
[root@Z ~]# df -TH
文件系统 类型 容量 已用 可用 已用% 挂载点
/dev/mapper/centos-root xfs 59G 2.4G 56G 5% /
devtmpfs devtmpfs 1.1G 0 1.1G 0% /dev
tmpfs tmpfs 1.1G 0 1.1G 0% /dev/shm
tmpfs tmpfs 1.1G 10M 1.1G 1% /run
tmpfs tmpfs 1.1G 0 1.1G 0% /sys/fs/cgroup
/dev/sda1 xfs 1.1G 139M 925M 14% /boot
tmpfs tmpfs 208M 0 208M 0% /run/user/0
LVM工作原理总结 将分区按照LVM的格式首先创建成PV(物理卷),把物理卷加到卷组(VG)里,再通过逻辑卷分出LV(逻辑卷),实现分区的扩容, 所以使用LVM进行扩容实际上是将分出的逻辑卷扩容给根分区。
如何使用LVM扩容步骤总结 准备一个分好区的新磁盘→修改磁盘类型为8e→创建逻辑卷(PV)(将新添加的磁盘分区作为物理卷)→创建卷组(VG)→创建逻辑卷(LV)→格式化和挂载逻辑卷→实现扩容
简单来说就是:
PV: 是物理的磁盘分区
VG: LVM中的物理的磁盘分区,也就是PV,必须加入VG,可以将VG理解为一个仓库或者是几个大的硬盘
LV: 从VG中划分的逻辑分区
总览步骤
#把硬盘分区创建成pv
pvresize /dev/sdb1 (新分区)
#把创建成pv的分区 加入vg
vgextend centos /dev/sdb1(新分区)
# 给vg中的/dev/centos/home 扩容
lvextend -L +500G /dev/centos/home
#调整XFS文件系统以利用新增的空间。使用xfs_growfs命令来调整XFS文件系统的大小
xfs_growfs /dev/centos/home (xfs)
#调整文件系统大小以利用新增的空间。如果逻辑卷上运行的是ext2、ext3或ext4文件系统,可以使用resize2fs命令调整文件系统大小
resize2fs /dev/centos/home (ext4)
招贤纳士