esxi-linux-lvm磁盘扩容

before:esxi调整磁盘空间

在这里插入图片描述

修改磁盘

1、查看磁盘分配:lsblk

可以看到sda总300,sda3挂载的lv只使用了120

root@sin:~# lsblk
NAME                     MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda                        8:0    0  300G  0 disk 
├─sda1                     8:1    0    1M  0 part 
├─sda2                     8:2    0    1G  0 part /boot
└─sda3                     8:3    0  119G  0 part 
  └─ubuntu--vg-ubuntu_lv 253:0    0  119G  0 lvm  /
sr0                       11:0    1  1.2G  0 rom 

2、fdisk /dev/sda 进入调整

root@sin:~# fdisk /dev/sda

Welcome to fdisk (util-linux 2.34).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

GPT PMBR size mismatch (251658239 != 629145599) will be corrected by write.

3、新建分区 n

Command (m for help): n
Partition number (4-128, default 4): 	# 回车默认4分区
First sector (251656192-629145566, default 251656192): 	# 回车默认磁盘开始山区号
Last sector, +/-sectors or +/-size{K,M,G,T,P} (251656192-629145566, default 629145566):   # 回车默认磁盘结束扇区号

Created a new partition 4 of type 'Linux filesystem' and of size 180 GiB.	# 提示新增分区成功

4、修改分区格式 t

输入8e,修改分区为LVM类型(8e就是Linux LVM的hex代码,可以输入L列出所有代码进行了解)

Command (m for help): t
Partition number (1-4, default 4): 
Partition type (type L to list all types): 8e

Type of partition 4 is unchanged: Linux filesystem.

5、操作写入磁盘 w

fdisk操作缓存在内存里,只有w写入磁盘后才生效

Command (m for help): w
The partition table has been altered.
Syncing disks.

6、查看磁盘分区后状况

root@sin:~# lsblk
NAME                     MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda                        8:0    0  300G  0 disk 
├─sda1                     8:1    0    1M  0 part 
├─sda2                     8:2    0    1G  0 part /boot
├─sda3                     8:3    0  119G  0 part 
│ └─ubuntu--vg-ubuntu_lv 253:0    0  119G  0 lvm  /
└─sda4                     8:4    0  180G  0 part 
sr0                       11:0    1  1.2G  0 rom

7、格式化新建分区为ext4(ubuntu20)

新增测试:扩容过程中完全可以省略格式化这一步

ext4文件系统mkfs.ext4 /dev/sda4
xfs文件系统mkfs.xfs /dev/sda4

root@sin:~# mkfs.ext4 /dev/sda4 
mke2fs 1.45.5 (07-Jan-2020)
Discarding device blocks: done                            
Creating filesystem with 47185920 4k blocks and 11819520 inodes
Filesystem UUID: c6511f12-c696-4f33-830e-0d0563477e69
Superblock backups stored on blocks: 
	32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 
	4096000, 7962624, 11239424, 20480000, 23887872

8、新分区添加到vg,扩容vg

root@sin:~# lvm
lvm> pvcreate /dev/sda4
WARNING: ext4 signature detected on /dev/sda4 at offset 1080. Wipe it? [y/n]: y
  Wiping ext4 signature on /dev/sda4.
  Physical volume "/dev/sda4" successfully created.
lvm> vgdisplay
  --- Volume group ---
  VG Name               ubuntu-vg
  System ID             
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  2
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1
  Open LV               1
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               <119.00 GiB
  PE Size               4.00 MiB
  Total PE              30463
  Alloc PE / Size       30463 / <119.00 GiB
  Free  PE / Size       0 / 0   
  VG UUID               INAlPn-Pu6F-xNOe-PFg7-UjcY-yBm6-hkBQpe

lvm> vgextend ubuntu-vg /dev/sda4
  Volume group "ubuntu-vg" successfully extended
lvm> vgdisplay
  --- Volume group ---
  VG Name               ubuntu-vg
  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               298.99 GiB
  PE Size               4.00 MiB
  Total PE              76542
  Alloc PE / Size       30463 / <119.00 GiB
  Free  PE / Size       46079 / <180.00 GiB
  VG UUID               INAlPn-Pu6F-xNOe-PFg7-UjcY-yBm6-hkBQpe

9、扩容对应的LV,即将Free PE分配给该LV(Free PE / Size 46079 / <180.00 GiB)

如果加了-r扩容失败,可以使用xfs_growfs命令来实现文件系统的扩容,xfs文件系统只能动态扩容无法动态缩减
-l 默认加byte,-L直接上大单位G

lvm> lvextend -l +46079 /dev/ubuntu-vg/ubuntu_lv
  Size of logical volume ubuntu-vg/ubuntu_lv changed from <119.00 GiB (30463 extents) to 298.99 GiB (76542 extents).
  Logical volume ubuntu-vg/ubuntu_lv successfully resized.
lvm> lvdisplay
  --- Logical volume ---
  LV Path                /dev/ubuntu-vg/ubuntu_lv
  LV Name                ubuntu_lv
  VG Name                ubuntu-vg
  LV UUID                ibarYI-YqIa-UHbU-9uc4-JdyQ-IM1b-ic1Ps3
  LV Write Access        read/write
  LV Creation host, time ubuntu-server, 2022-07-13 15:07:52 +0000
  LV Status              available
  # open                 1
  LV Size                298.99 GiB
  Current LE             76542
  Segments               2
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0

10、文件系统扩容 resize2fs

如果是xfs格式(centos)的话使用xfs_growfs命令来实现文件系统的扩容
xfs文件系统只能动态扩容无法动态缩减。
测试centos7的操作步骤和ubuntu扩容唯一的一句区别就是resize2fs换成xfs_growfs
如:xfs_growfs /dev/mapper/centos_node1-root

root@sin:/dev# resize2fs /dev/mapper/ubuntu--vg-ubuntu_lv 
resize2fs 1.45.5 (07-Jan-2020)
Filesystem at /dev/mapper/ubuntu--vg-ubuntu_lv is mounted on /; on-line resizing required
old_desc_blocks = 15, new_desc_blocks = 38
The filesystem on /dev/mapper/ubuntu--vg-ubuntu_lv is now 78379008 (4k) blocks long.

root@sin:/dev# df -Th
Filesystem                       Type      Size  Used Avail Use% Mounted on
udev                             devtmpfs  7.8G     0  7.8G   0% /dev
tmpfs                            tmpfs     1.6G  1.3M  1.6G   1% /run
/dev/mapper/ubuntu--vg-ubuntu_lv ext4      294G   70G  212G  25% /

end lvm磁盘架构图

在这里插入图片描述

文章参考:VMware ESXi 虚拟机 LVM磁盘在线扩容

centos和ubuntu及各版本操作有差异,新版的不少操作自动化后可以省略


版权声明:本文为sin_404原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。