ESXi虚拟机修改硬盘类型(厚置备、精简置备)与大小

ESXi 虚拟机的web管理界面只支持硬盘容量调大,不支持调小。另外不支持修改硬盘类型。这里就需要其它一些手段了。

这里以Vmware ESXi 7.0为例。

一、开启ESXi SSH功能

首先在ESXi 的web管理界面开启SSH,如下图:

image-20230531154219831

二、执行vmkfstools命令操作虚拟硬盘(操作确实很慢,要等很久)

开启SSH后,通过XShell或者其它SSH客户端连接到ESXi服务器,进入服务器后执行以下操作:

1
#cd /vmfs/volumes/<datasotre name>/<VM folder_name>

其中datasotre name代表数据存储名称,在ESXi系统中实际为一串UUID,但同时在该目录下做了软链,因此可能通过ESXi中配置的数据存储名称进入;

其中VM folder_name代表你要修改的虚拟机名称。

进入你所在的虚拟机相关文件目录中,执行相应的命令即可,以下命令中vm-name均代表虚拟机名称。如果某个虚拟机有多个虚拟磁盘的话,可能会存在多个类似vm-name-1.vmdk的文件,请分别替换即可。

1.更改虚拟磁盘大小(只能从小改大,不能从大改小):

1
#vmkfstools -X 200g vm-name.vmdk

修改完之后删除虚拟磁盘,然后再添加一次即可(切记不要勾选“从数据存储删除文件”)

2.精简置备转换至厚置备延迟置零:

1
#vmkfstools --inflatedisk vm-name.vmdk

This option converts a thin virtual disk to eagerzeroedthick, preserving all existing data. The option allocates and zeroes out any blocks that are not already allocated.

3.厚置备置零转换至厚置备延迟置零:

1
#vmkfstools --eagerzero /vmfs/volumes/DatastoreName/VMName/VMName.vmdk

Use the vmkfstools command to convert any zeroedthick virtual disk to an eagerzeroedthick disk.

While performing the conversion, this option preserves any data on the virtual disk.

4.随意转换磁盘格式(克隆出一个新的vmdk文件,其中-d之后的参数就是虚拟磁盘类型):

1
#vmkfstools -i vm-name.vmdk vm-name-new.vmdk -d thin

If you want to convert the format of the disk, use the -d|--diskformat suboption.

This suboption is useful when you import virtual disks in a format not compatible with ESXi, for example 2gbsparse format. After you convert the disk, you can attach this disk to a new virtual machine you create in ESXi.

对于通过克隆出的新vmdk文件,可以通过添加现有硬盘的方式添加

When you create or clone a virtual disk, you can use the -d|--diskformat suboption to specify the format for the disk.

Choose from the following formats:

  • zeroedthick (default) – Space required for the virtual disk is allocated during creation. Any data remaining on the physical device is not erased during creation, but is zeroed out on demand on first write from the virtual machine. The virtual machine does not read stale data from disk.
  • eagerzeroedthick – Space required for the virtual disk is allocated at creation time. In contrast to zeroedthick format, the data remaining on the physical device is zeroed out during creation. It might take much longer to create disks in this format than to create other types of disks.
  • thin – Thin-provisioned virtual disk. Unlike with the thick format, space required for the virtual disk is not allocated during creation, but is supplied, zeroed out, on demand.
  • rdm:device – Virtual compatibility mode raw disk mapping.
  • rdmp:device – Physical compatibility mode (pass-through) raw disk mapping.
  • 2gbsparse – A sparse disk with the maximum extent size of 2 GB. You can use disks in this format with hosted VMware products, such as VMware Fusion. However, you cannot power on the sparse disk on an ESXi host unless you first re-import the disk with vmkfstools in a compatible format, such as thick or thin.

The only disk formats you can use for NFS are thin, thick, zeroedthick, and 2gbsparse.

三、延展阅读

3.1 VMware ESXi虚拟硬盘类型介绍

VMware 虚拟化平台中虚拟机硬盘有三种类型,它们分别是:厚置备延迟置零(zeroed thick)、厚置备置零(eager zeroed thick)和精简置备(thin)。

  • 一、厚置备延迟置零(zeroed thick)
    创建虚拟机时硬盘类型默认选择此种类型;创建虚拟机时为虚拟机硬盘分配的存储空间即是物理存储空间,但在分配的过程未将存储空间置零,仅当虚拟机存在写入数据时,再按量将存储空间置零,类似于快速格式化。
  • 二、厚置备置零(eager zeroed thick)
    创建虚拟机时为虚拟机硬盘分配的存储空间即是物理存储空间,并且在分配的过程将存储空间置零,类似于格式化。此类硬盘类型适用于I/O读写频繁的业务虚拟机,如数据库业务系统。
  • 三、精简置备(thin)
    创建虚拟机时为虚拟机硬盘分配的存储空间并不等于物理存储空间,虚拟机实际使用的存储空间才是它占用的物理存储空间。当虚拟机写入数据时,才为其分配物理存储空间及置零,待分配空间和置备完成后才能进行操作,这对于IO频繁造成性能会有所下降,但它的好处是节省了存储空间。

3.2 vmkfstools命令介绍

vmkfstools 是用于管理 VMFS 卷、存储设备和虚拟磁盘的 ESXi Shell 命令之一。可以使用 vmkfstools 命令执行很多存储操作。例如,可以在物理分区上创建和管理 VMFS 数据存储,或操作 VMFS 或 NFS 数据存储中存储的虚拟磁盘文件。

四、参考文档

Supported Disk Formats:https://docs.vmware.com/en/VMware-vSphere/7.0/com.vmware.vsphere.storage.doc/GUID-9CC98802-82D9-44B0-9735-97675258FAAE.html

Converting a Zeroedthick Virtual Disk to an Eagerzeroedthick Disk:https://docs.vmware.com/en/VMware-vSphere/7.0/com.vmware.vsphere.storage.doc/GUID-60B9C89A-6638-43FA-A8E3-4DACAC85FC29.html

Cloning or Converting a Virtual Disk or RDM:https://docs.vmware.com/en/VMware-vSphere/7.0/com.vmware.vsphere.storage.doc/GUID-01D3CF47-A84A-4988-8103-A0487D6441AA.html

VMware ESXI工具-vmkfstools命令:http://alafarms.com/index.php/archives/77.html 虽然初次参考了这篇文章,但是这篇文章中的部分命令有点搞反了。