ESXI虚拟机扩容/目录(添加新硬盘)

时间:2022-04-03 14:30:10

ESXI虚拟机扩容/目录(添加新硬盘)

背景:

线上服务器,磁盘Linux的虚拟机根分区已经使用90%,触发了磁盘告警,再一顿操作删除后,勉勉强强回到了82%,现在需要对根目录进行扩容。

进入到EXSI管理平台,看到原来的sda磁盘只有30G【图一】,新添加一块100G 的新硬盘【图二】

ESXI虚拟机扩容/目录(添加新硬盘)

图一

ESXI虚拟机扩容/目录(添加新硬盘)

图二

登录机器,查看磁盘,发现了多了一块sdb

  1. [root@localhost ~]# lsblk
  2. NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
  3. sda 8:0 0 30G 0 disk
  4. ├─sda1 8:1 0 1G 0 part /boot
  5. └─sda2 8:2 0 29G 0 part
  6. ├─centos-root 253:0 0 26G 0 lvm /
  7. └─centos-swap 253:1 0 3G 0 lvm [SWAP]
  8. sdb 8:16 0 100G 0 disk
  9. sr0 11:0 1 1024M 0 rom

第一步:分区

  1. [root@localhost ~]# fdisk /dev/sdb
  2. Welcome to fdisk (util-linux 2.23.2).
  3. Changes will remain in memory only, until you decide to write them.
  4. Be careful before using the write command.
  5. Device does not contain a recognized partition table
  6. Building a new DOS disklabel with disk identifier 0x3cb140d1.
  7. Command (m for help): n
  8. Partition type:
  9. p primary (0 primary, 0 extended, 4 free)
  10. e extended
  11. Select (default p): p
  12. Partition number (1-4, default 1): 1
  13. First sector (2048-209715199, default 2048):
  14. Using default value 2048
  15. Last sector, +sectors or +size{K,M,G} (2048-209715199, default 209715199):
  16. Using default value 209715199
  17. Partition 1 of type Linux and of size 100 GiB is set
  18. Command (m for help): w
  19. The partition table has been altered!
  20. Calling ioctl() to re-read partition table.
  21. Syncing disks.

再次查看磁盘,发现多了个sdb1

  1. [root@localhost ~]# lsblk
  2. NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
  3. sda 8:0 0 30G 0 disk
  4. ├─sda1 8:1 0 1G 0 part /boot
  5. └─sda2 8:2 0 29G 0 part
  6. ├─centos-root 253:0 0 26G 0 lvm /
  7. └─centos-swap 253:1 0 3G 0 lvm [SWAP]
  8. sdb 8:16 0 100G 0 disk
  9. └─sdb1 8:17 0 100G 0 part
  10. sr0 11:0 1 1024M 0 rom

第二步:格式化

格式化硬盘,这里使用xfs格式,建议使用和系统一样的文件格式

可以看到/分区 【/dev/mapper/centos-root】 使用的是xfs 的文件系统

  1. [root@localhost ~]# blkid
  2. /dev/sda1: UUID="e1e8eda5-8b7a-45f0-a763-8868777e3f5f" TYPE="xfs"
  3. /dev/sda2: UUID="DQGZrB-2WRU-JneT-xKKw-WFSN-ozM9-rnW0S4" TYPE="LVM2_member"
  4. /dev/mapper/centos-root: UUID="a8e67b40-8e6e-4010-a911-21b4f95b90bf" TYPE="xfs"
  5. /dev/mapper/centos-swap: UUID="326cb3bd-ea93-460d-9f2b-cded2d3836c8" TYPE="swap"

把/dev/sdb1 格式化成 xfs文件系统

  1. [root@localhost ~]# mkfs.xfs /dev/sdb1
  2. meta-data=/dev/sdb1 isize=512 agcount=4, agsize=6553536 blks
  3. = sectsz=512 attr=2, projid32bit=1
  4. = crc=1 finobt=0, sparse=0
  5. data = bsize=4096 blocks=26214144, imaxpct=25
  6. = sunit=0 swidth=0 blks
  7. naming =version 2 bsize=4096 ascii-ci=0 ftype=1
  8. log =internal log bsize=4096 blocks=12799, version=2
  9. = sectsz=512 sunit=0 blks, lazy-count=1
  10. realtime =none extsz=4096 blocks=0, rtextents=0

可以看到/dev/sdb1 的文件系统为 xfs

  1. [root@localhost ~]# blkid
  2. /dev/sda1: UUID="e1e8eda5-8b7a-45f0-a763-8868777e3f5f" TYPE="xfs"
  3. /dev/sda2: UUID="DQGZrB-2WRU-JneT-xKKw-WFSN-ozM9-rnW0S4" TYPE="LVM2_member"
  4. /dev/mapper/centos-root: UUID="a8e67b40-8e6e-4010-a911-21b4f95b90bf" TYPE="xfs"
  5. /dev/mapper/centos-swap: UUID="326cb3bd-ea93-460d-9f2b-cded2d3836c8" TYPE="swap"
  6. /dev/sdb1: UUID="2eca6693-8885-4efe-a09e-320a0fe4eefa" TYPE="xfs"

第三步:lvm扩容

通过lvdisplay 可以看到 / 分区 的lv 名称为 root

  1. [root@localhost ~]# lvdisplay
  2. ..............
  3. --- Logical volume ---
  4. LV Path /dev/centos/root
  5. LV Name root
  6. VG Name centos
  7. LV UUID upKlP7-AbiZ-e3Yh-aYYN-5Eel-ULsG-TVXVer
  8. LV Write Access read/write
  9. LV Creation host, time localhost, 2021-09-26 05:16:02 -0400
  10. LV Status available
  11. # open 1
  12. LV Size <26.00 GiB
  13. Current LE 6655
  14. Segments 1
  15. Allocation inherit
  16. Read ahead sectors auto
  17. - currently set to 8192
  18. Block device 253:0

1、创建pv物理卷

  1. [root@localhost ~]# pvcreate /dev/sdb1
  2. WARNING: xfs signature detected on /dev/sdb1 at offset 0. Wipe it? [y/n]: y
  3. Wiping xfs signature on /dev/sdb1.
  4. Physical volume "/dev/sdb1" successfully created.

查看下pv物理卷

  1. [root@localhost ~]# pvs
  2. PV VG Fmt Attr PSize PFree
  3. /dev/sda2 centos lvm2 a-- <29.00g 0
  4. /dev/sdb1 lvm2 --- <100.00g <100.00g

2、向vg卷组 添加 pv物理卷

使用vgdisplay 查看下vg卷组名称为 centos

  1. [root@localhost ~]# vgdisplay
  2. --- Volume group ---
  3. VG Name centos
  4. System ID
  5. Format lvm2
  6. Metadata Areas 1
  7. Metadata Sequence No 3
  8. VG Access read/write
  9. VG Status resizable
  10. MAX LV 0
  11. Cur LV 2
  12. Open LV 2
  13. Max PV 0
  14. Cur PV 1
  15. Act PV 1
  16. VG Size <29.00 GiB
  17. PE Size 4.00 MiB
  18. Total PE 7423
  19. Alloc PE / Size 7423 / <29.00 GiB
  20. Free PE / Size 0 / 0
  21. VG UUID GzMH6f-8Hne-1SF4-h89q-n95E-0xft-x8JRkb

使用vgextend 添加pv到vg中

  1. [root@localhost ~]# vgextend centos /dev/sdb1
  2. Volume group "centos" successfully extended

使用vgdisplay,可以看到空闲的PE 容量大约为100G

  1. [root@localhost ~]# vgdisplay
  2. --- Volume group ---
  3. VG Name centos
  4. System ID
  5. Format lvm2
  6. Metadata Areas 2
  7. Metadata Sequence No 4
  8. VG Access read/write
  9. VG Status resizable
  10. MAX LV 0
  11. Cur LV 2
  12. Open LV 2
  13. Max PV 0
  14. Cur PV 2
  15. Act PV 2
  16. VG Size 128.99 GiB
  17. PE Size 4.00 MiB
  18. Total PE 33022
  19. Alloc PE / Size 7423 / <29.00 GiB
  20. Free PE / Size 25599 / <100.00 GiB
  21. VG UUID GzMH6f-8Hne-1SF4-h89q-n95E-0xft-x8JRkb

3、扩容逻辑卷 lvm

通过lvdisplay 可以看到 / 分区 的lv 名称为 root

  1. [root@localhost ~]# lvdisplay
  2. .....................
  3. --- Logical volume ---
  4. LV Path /dev/centos/root
  5. LV Name root
  6. VG Name centos
  7. LV UUID upKlP7-AbiZ-e3Yh-aYYN-5Eel-ULsG-TVXVer
  8. LV Write Access read/write
  9. LV Creation host, time localhost, 2021-09-26 05:16:02 -0400
  10. LV Status available
  11. # open 1
  12. LV Size <26.00 GiB
  13. Current LE 6655
  14. Segments 1
  15. Allocation inherit
  16. Read ahead sectors auto
  17. - currently set to 8192
  18. Block device 253:0

使用lvextend 增加逻辑卷 100G

  1. [root@localhost ~]# lvextend -L +100G /dev/centos/root
  2. Insufficient free space: 25600 extents needed, but only 25599 available

提示没有那么多的空闲的pe去添加,那我们就添加99G 【其实,上面的操作在往vg卷组里添加pv 的时候,也说明了 可用容量< 100G】

  1. [root@localhost ~]# lvextend -L +99G /dev/centos/root
  2. Size of logical volume centos/root changed from <26.00 GiB (6655 extents) to <125.00 GiB (31999 extents).
  3. Logical volume centos/root successfully resized.

添加成功,我们看下磁盘大小,和挂载的情况

  1. [root@localhost ~]# lsblk
  2. NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
  3. sda 8:0 0 30G 0 disk
  4. ├─sda1 8:1 0 1G 0 part /boot
  5. └─sda2 8:2 0 29G 0 part
  6. ├─centos-root 253:0 0 125G 0 lvm /
  7. └─centos-swap 253:1 0 3G 0 lvm [SWAP]
  8. sdb 8:16 0 100G 0 disk
  9. └─sdb1 8:17 0 100G 0 part
  10. └─centos-root 253:0 0 125G 0 lvm /
  11. sr0 11:0 1 1024M 0 rom
  12. [root@localhost ~]# df -h
  13. Filesystem Size Used Avail Use% Mounted on
  14. devtmpfs 1.9G 0 1.9G 0% /dev
  15. tmpfs 1.9G 0 1.9G 0% /dev/shm
  16. tmpfs 1.9G 8.9M 1.9G 1% /run
  17. tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
  18. /dev/mapper/centos-root 26G 1.5G 25G 6% /
  19. /dev/sda1 1014M 150M 865M 15% /boot
  20. tmpfs 379M 0 379M 0% /run/user/0

通过 lsblk 可以看到/ 分区磁盘的容量变成了125G 了,但是 磁盘挂载那边还是没有变化

有两个解决方法:

1、重启

2、刷新下文件系统

这里,我们采用第二种方法,刷新文件系统

注意:不同的文件系统的刷新命令不一样

resize2fs 逻辑卷名 (只适用于ext文件系统)

xfs_growfs 挂载点 (只适用于xfs文件系统)

通过blkid 查看到/ 分区的文件系统为 xfs 格式【/dev/mapper/centos-root】

  1. [root@localhost ~]# df -h
  2. Filesystem Size Used Avail Use% Mounted on
  3. devtmpfs 1.9G 0 1.9G 0% /dev
  4. tmpfs 1.9G 0 1.9G 0% /dev/shm
  5. tmpfs 1.9G 8.9M 1.9G 1% /run
  6. tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
  7. /dev/mapper/centos-root 26G 1.5G 25G 6% /
  8. /dev/sda1 1014M 150M 865M 15% /boot
  9. tmpfs 379M 0 379M 0% /run/user/0
  10. [root@localhost ~]# blkid
  11. /dev/sda1: UUID="e1e8eda5-8b7a-45f0-a763-8868777e3f5f" TYPE="xfs"
  12. /dev/sda2: UUID="DQGZrB-2WRU-JneT-xKKw-WFSN-ozM9-rnW0S4" TYPE="LVM2_member"
  13. /dev/mapper/centos-root: UUID="a8e67b40-8e6e-4010-a911-21b4f95b90bf" TYPE="xfs"
  14. /dev/mapper/centos-swap: UUID="326cb3bd-ea93-460d-9f2b-cded2d3836c8" TYPE="swap"
  15. /dev/sdb1: UUID="kuZdno-Kaue-WMEV-d6jh-zTjc-0eAa-YKcDxB" TYPE="LVM2_member"

使用xfs_growfs 刷新下文件系统

  1. [root@localhost ~]# xfs_growfs /
  2. meta-data=/dev/mapper/centos-root isize=512 agcount=4, agsize=1703680 blks
  3. = sectsz=512 attr=2, projid32bit=1
  4. = crc=1 finobt=0 spinodes=0
  5. data = bsize=4096 blocks=6814720, imaxpct=25
  6. = sunit=0 swidth=0 blks
  7. naming =version 2 bsize=4096 ascii-ci=0 ftype=1
  8. log =internal bsize=4096 blocks=3327, version=2
  9. = sectsz=512 sunit=0 blks, lazy-count=1
  10. realtime =none extsz=4096 blocks=0, rtextents=0
  11. data blocks changed from 6814720 to 32766976

刷新后发现,/ 分区总的磁盘大小变成了125G

  1. [root@localhost ~]# df -h
  2. Filesystem Size Used Avail Use% Mounted on
  3. devtmpfs 1.9G 0 1.9G 0% /dev
  4. tmpfs 1.9G 0 1.9G 0% /dev/shm
  5. tmpfs 1.9G 8.9M 1.9G 1% /run
  6. tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
  7. /dev/mapper/centos-root 125G 1.5G 124G 2% /
  8. /dev/sda1 1014M 150M 865M 15% /boot
  9. tmpfs 379M 0 379M 0% /run/user/0
  10. [root@localhost ~]# lsblk
  11. NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
  12. sda 8:0 0 30G 0 disk
  13. ├─sda1 8:1 0 1G 0 part /boot
  14. └─sda2 8:2 0 29G 0 part
  15. ├─centos-root 253:0 0 125G 0 lvm /
  16. └─centos-swap 253:1 0 3G 0 lvm [SWAP]
  17. sdb 8:16 0 100G 0 disk
  18. └─sdb1 8:17 0 100G 0 part
  19. └─centos-root 253:0 0 125G 0 lvm /
  20. sr0 11:0 1 1024M 0 rom

好了,本次ESXI虚拟机扩容/目录 成功

原文链接:https://www.toutiao.com/a7046211467451908611/