在VMware ESXi中给虚拟机扩容硬盘
场景
- 最初安装 centos 7 的时候,默认分配 40GB 硬盘,现在需要扩到 200G
详细步骤
- 第1步,在 VMware ESXi 的在虚拟机列表中找到待扩容的虚拟机,将其关机,然后修改硬盘大小(如下图示)
- 第2步,将虚拟机开机,执行
fdisk -l
看到/dev/sda
已经扩容到214.7G,但文件系统/dev/mapper/centos-root
还是37.6G 没有变
[root@localhost ~]# df -Th | grep dev
devtmpfs devtmpfs 3.9G 0 3.9G 0% /dev
tmpfs tmpfs 3.9G 0 3.9G 0% /dev/shm
/dev/mapper/centos-root xfs 35G 1.3G 34G 4% /
/dev/sda1 xfs 1014M 150M 865M 15% /boot
[root@localhost ~]# fdisk -l | grep dev
Disk /dev/sda: 214.7 GB, 214748364800 bytes, 419430400 sectors
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 83886079 40893440 8e Linux LVM
Disk /dev/mapper/centos-root: 37.6 GB, 37576769536 bytes, 73392128 sectors
Disk /dev/mapper/centos-swap: 4294 MB, 4294967296 bytes, 8388608 sectors
[root@localhost ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 200G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 39G 0 part
├─centos-root 253:0 0 35G 0 lvm /
└─centos-swap 253:1 0 4G 0 lvm [SWAP]
sr0 11:0 1 1024M 0 rom
- 第3步,执行
fdisk /dev/sda
新建分区
[root@localhost ~]# fdisk /dev/sda
Command (m for help): 输入p
Disk /dev/sda: 214.7 GB, 214748364800 bytes, 419430400 sectors
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 83886079 40893440 8e Linux LVM
Command (m for help): 输入n
Partition type:
p primary (2 primary, 0 extended, 2 free)
e extended
Select (default p): 输入回车
Partition number (3,4, default 3): 输入回车
First sector (83886080-419430399, default 83886080): 输入回车
Last sector, +sectors or +size{K,M,G} (83886080-419430399, default 419430399): 输入回车
Command (m for help): 输入t
Partition number (1-3, default 3): 输入回车
Hex code (type L to list all codes): 输入8e
Changed type of partition 'Linux' to 'Linux LVM'
Command (m for help): 输入w
执行 fdisk -l
确认 /dev/sda3
已创建,且分区格式为 Linux LVM
[root@localhost ~]# fdisk -l | grep dev
Disk /dev/sda: 214.7 GB, 214748364800 bytes, 419430400 sectors
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 83886079 40893440 8e Linux LVM
/dev/sda3 83886080 419430399 167772160 8e Linux LVM
Disk /dev/mapper/centos-root: 37.6 GB, 37576769536 bytes, 73392128 sectors
Disk /dev/mapper/centos-swap: 4294 MB, 4294967296 bytes, 8388608 sectors
- 第4步,重启一下虚拟机(否则格式化分区的时候可能会提示
Could not stat /dev/sda3
),以便重读分区表
[root@localhost ~]# reboot
- 第5步,格式化新建的分区
[root@localhost ~]# mkfs.ext3 /dev/sda3
......
Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
- 第6步,利用
lvm
合并/dev/sda3
分区
[root@localhost ~]# lvm
lvm> pvcreate /dev/sda3
WARNING: ext3 signature detected on /dev/sda3 at offset 1080. Wipe it? [y/n]: 输入y
Wiping ext3 signature on /dev/sda3.
Physical volume "/dev/sda3" successfully created.
lvm> vgdisplay -v
--- Volume group ---
VG Name centos
lvm> vgextend centos /dev/sda3
Volume group "centos" successfully extended
然后 重新执行 vgdisplay -v
查看 Physical volumes
最新的详情,如下示例中 /dev/sda3
的 Free PE
容量为 40959。
lvm> vgdisplay -v
--- Physical volumes ---
PV Name /dev/sda3
PV UUID jkCZPh-NV1M-Ds6t-oSra-CR43-IMFe-6lFagf
PV Status allocatable
Total PE / Free PE 40959 / 40959
执行 df -Th
确认根目录的挂载设备,示例为 /dev/mapper/centos-root
[root@localhost ~]# df -Th | grep dev
devtmpfs devtmpfs 3.9G 0 3.9G 0% /dev
tmpfs tmpfs 3.9G 0 3.9G 0% /dev/shm
/dev/mapper/centos-root xfs 35G 1.3G 34G 4% /
/dev/sda1 xfs 1014M 150M 865M 15% /boot
将根目录 /dev/mapper/centos-root
与 /dev/sda3
的 40959
空余容量合并,然后退出。
lvm> lvextend -l+40959 /dev/mapper/centos-root
Size of logical volume centos/root changed from <35.00 GiB (8959 extents) to 194.99 GiB (49918 extents).
Logical volume centos/root successfully resized.
lvm> quit
执行 xfs_growfs /dev/mapper/centos-root
将文件系统进行扩容,让系统识别
附:ubuntu默认为ext4格式,扩容参考
resize2fs /dev/mapper/ubuntu--vg-ubuntu--lv
[root@localhost ~]# xfs_growfs /dev/mapper/centos-root
meta-data=/dev/mapper/centos-root isize=512 agcount=4, agsize=2293504 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0 spinodes=0
data = bsize=4096 blocks=9174016, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal bsize=4096 blocks=4479, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
data blocks changed from 9174016 to 51116032
- 第7步,再次查看系统分区情况,centos-root分区已经扩容成功了
[root@localhost ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 200G 0 disk
├─sda1 8:1 0 1G 0 part /boot
├─sda2 8:2 0 39G 0 part
│ ├─centos-root 253:0 0 195G 0 lvm /
│ └─centos-swap 253:1 0 4G 0 lvm [SWAP]
└─sda3 8:3 0 160G 0 part
└─centos-root 253:0 0 195G 0 lvm /
sr0 11:0 1 1024M 0 rom
[root@localhost ~]# df -Th | grep dev
devtmpfs devtmpfs 3.9G 0 3.9G 0% /dev
tmpfs tmpfs 3.9G 0 3.9G 0% /dev/shm
/dev/mapper/centos-root xfs 195G 1.3G 194G 1% /
/dev/sda1 xfs 1014M 150M 865M 15% /boot
[root@localhost ~]# fdisk -l | grep dev
Disk /dev/sda: 214.7 GB, 214748364800 bytes, 419430400 sectors
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 83886079 40893440 8e Linux LVM
/dev/sda3 83886080 419430399 167772160 8e Linux LVM
Disk /dev/mapper/centos-root: 209.4 GB, 209371267072 bytes, 408928256 sectors
Disk /dev/mapper/centos-swap: 4294 MB, 4294967296 bytes, 8388608 sectors