0x01 前言

我在刚接触虚拟化的时候选择了KVM技术栈,为了通过qemu相关的命令管理虚拟机,我特意在每台虚拟机里安装了qemu guest agent,因此,我大致上了解vmtools主要的功能。

自从将虚拟化技术栈更换为vmware之后,我也一直给虚拟机安装vmtools,而这个vmtools就相当于qemu guest agent,方便对虚拟机进行操作、监控与快照等操作。

0x02 主要功能

根据vmware的相关文档,vmtools有以下功能:

  • 正常执行虚拟机的电源操作。
  • 在执行各种电源操作期间在客户机操作系统中运行 VMware 提供的脚本或用户配置的脚本。
  • 在客户机操作系统中运行应用程序、命令和文件系统操作,以增强客户机自动化。
  • 针对客户机用户操作进行身份验证。
  • 定期从客户机收集网络、磁盘和内存使用情况信息。
  • 生成从客户机操作系统到主机的检测信号,以便 VMware High Availability 可以确定客户机操作系统的可用性。
  • 在客户机操作系统与主机或客户端桌面之间同步时钟。
  • 使客户机文件系统进入静默状态,以便主机可以捕获与文件系统一致的客户机快照。
  • 在使客户机文件系统进入静默状态时,运行 pre-freeze-script.bat 和 post-thaw-script.bat。
  • 打开虚拟机电源后立即自定义客户机操作系统。
  • 在 VMware Workstation 和 VMware Fusion 上,在主机文件系统与客户机文件系统之间启用“共享文件夹”。
  • 支持在客户机操作系统与主机或客户端桌面之间复制和粘贴文本、图形和文件。

没有安装vmtools的虚拟机,通过vsphere client查看虚拟机信息时,是看不到虚拟机IP地址与hostname的:

当需要通过vsphere client关闭虚拟机时,只能选择强制断开电源,而不能走正常的关机流程。这时候会带来一个严重的问题,当服务器启动关机流程时,因为没有安装vmtools的关系,虚拟机将无法正常关闭,极有可能导致虚拟机数据丢失甚至无法启动:

在我的环境中,还会经常使用快照这个功能,在没有安装vmtools的情况下是无法使虚拟机的文件系统处于静默状态,这时候创建快照则有可能导致文件损坏:

此外还有监控CPU、内存等使用状况;如果需要启用了HA或其他vcenter的高级功能,则必须安装vmtools,它会检测虚拟机的可用性,以此实现高可用。

0x03 安装

vmtools的安装一般在操作系统安装完成后进行,windows的安装较为简单,也并无其他设置项,只需要跟随安装引导进行即可,首先给虚拟机加载vmtools镜像:

然后到虚拟机里执行安装程序:

安装完成后即可在任务栏找到vmtools的图标,可查看软件版本:

完成安装后再次检查vsphere client,即可查看相关信息:

安装完成后即可取消vmtools镜像的挂载或删除虚拟的CD驱动器:

Centos的安装也需要先挂载vmtools镜像,然后通过root用户登入并挂载:

[root@centos7-t1 ~]# mkdir /root/cdrom && mount /dev/cdrom /root/cdrom
mount: /dev/sr0 写保护,将以只读方式挂载

然后解压到root目录:

[root@centos7-t1 ~]# tar zvxf /root/cdrom/VMwareTools-10.2.1-8267844.tar.gz -C /root/

最后安装:

[root@centos7-t1 ~]# /root/vmware-tools-distrib/vmware-install.pl

如果使用最小化安装,则有可能缺少perl:

[root@centos7-t1 ~]# /root/vmware-tools-distrib/vmware-install.pl 
-bash: /root/vmware-tools-distrib/vmware-install.pl: /usr/bin/perl: 坏的解释器: 没有那个文件或目录

这时候使用以下命令安装perl即可:

[root@centos7-t1 ~]# yum install perl -y

然后再执行安装命令:

# 执行安装
[root@centos7-t1 ~]# /root/vmware-tools-distrib/vmware-install.pl 

# 询问是否继续安装,默认为否
open-vm-tools packages are available from the OS vendor and VMware recommends 
using open-vm-tools packages. See http://kb.vmware.com/kb/2073803 for more 
information.
Do you still want to proceed with this installation? [no] yes

# 安装路径,默认即可
Creating a new VMware Tools installer database using the tar4 format.

Installing VMware Tools.

In which directory do you want to install the binary files? 
[/usr/bin] 

# 自动启动文件路径,默认即可
What is the directory that contains the init directories (rc0.d/ to rc6.d/)? 
[/etc/rc.d] 

# init文件夹路径,默认即可
What is the directory that contains the init scripts? 
[/etc/rc.d/init.d] 

# sbin文件夹路径,默认即可
In which directory do you want to install the daemon files? 
[/usr/sbin] 

# vmware-tools库目录路径,默认即可
In which directory do you want to install the library files? 
[/usr/lib/vmware-tools] 

# vmware-tools库目录不存在,需要创建,默认即可
The path "/usr/lib/vmware-tools" does not exist currently. This program is 
going to create it, including needed parent directories. Is this what you want?
[yes] 

# agent library文件夹路径,默认即可
In which directory do you want to install the common agent library files? 
[/usr/lib] 

# agent transient文件夹路径,默认即可
In which directory do you want to install the common agent transient files? 
[/var/lib] 

# 帮助文档路径,默认即可
In which directory do you want to install the documentation files? 
[/usr/share/doc/vmware-tools] 

# 帮助文档文件夹并不存在,需要创建,默认即可
The path "/usr/share/doc/vmware-tools" does not exist currently. This program 
is going to create it, including needed parent directories. Is this what you 
want? [yes] 

# 执行configure脚本,默认即可
Before running VMware Tools for the first time, you need to configure it by 
invoking the following command: "/usr/bin/vmware-config-tools.pl". Do you want 
this program to invoke the command for you now? [yes] 

# 是否启用文件文件夹共享,esxi环境无法使用,默认即可
The VMware Host-Guest Filesystem allows for shared folders between the host OS 
and the guest OS in a Fusion or Workstation virtual environment.  Do you wish 
to enable this feature? [no] 

# vmblock,不支持esxi环境,默认即可
The vmblock enables dragging or copying files between host and guest in a 
Fusion or Workstation virtual environment.  Do you wish to enable this feature?
[no] 

# 是否启用VMware automatic kernel modules,默认即可
Would you like to enable VMware automatic kernel modules?
[yes] 

# 是否启用vgauth,默认即可
Do you want to enable Guest Authentication (vgauth)? Enabling vgauth is needed 
if you want to enable Common Agent (caf). [yes] 

# 是否启用Common Agent,默认即可
Do you want to enable Common Agent (caf)? [no] 

如果没有安装桌面环境,则会有以下提示,忽略即可:

No X install found.

随后便开始安装过程:

Creating a new initrd boot image for the kernel.
Generating the key and certificate files.
Successfully generated the key and certificate files.
manageSELinux install: The 'semanage' utility was not found.
There was an error configuring the SELinux security context for VMware Tools.  
Please make certain that SELinux is configured correctly.

The configuration of VMware Tools 10.2.1 build-8267844 for Linux for this 
running kernel completed successfully.

You must restart your X session before any mouse or graphics changes take 
effect.

To enable advanced X features (e.g., guest resolution fit, drag and drop, and 
file and text copy/paste), you will need to do one (or more) of the following:
1. Manually start /usr/bin/vmware-user
2. Log out and log back into your desktop session
3. Restart your X session.

Enjoy,

--the VMware team

Found VMware Tools CDROM mounted at /root/cdrom. Ejecting device /dev/sr0 ...

安装完成后会自动卸载cdrom,如果有需要,可以删除一开始新建的文件夹和解压出来的文件夹:

[root@centos7-t1 ~]# rm -rf /root/cdrom/ && rm -rf /root/vmware-tools-distrib/

通过以下命令即可检查该服务的状态:

[root@centos7-t1 ~]# systemctl status vmware-tools.service
● vmware-tools.service - SYSV: Manages the services needed to run VMware software
   Loaded: loaded (/etc/rc.d/init.d/vmware-tools; bad; vendor preset: disabled)
   Active: active (running) since 二 2018-08-21 23:45:44 CST; 6min ago
     Docs: man:systemd-sysv-generator(8)
  Process: 19582 ExecStart=/etc/rc.d/init.d/vmware-tools start (code=exited, status=0/SUCCESS)
   CGroup: /system.slice/vmware-tools.service
           ├─19730 /usr/sbin/vmtoolsd
           └─19757 /usr/lib/vmware-vgauth/VGAuthService -s

8月 21 23:45:43 centos7-t1 systemd[1]: Starting SYSV: Manages the services needed to run VMware software...
8月 21 23:45:43 centos7-t1 vmware-tools[19582]: Checking acpi hot plug[  OK  ]
8月 21 23:45:43 centos7-t1 vmware-tools[19582]: Starting VMware Tools services in the virtual machine:
8月 21 23:45:44 centos7-t1 vmware-tools[19582]: Switching to guest configuration:[  OK  ]
8月 21 23:45:44 centos7-t1 vmware-tools[19582]: VMware Automatic Kmods:[  OK  ]
8月 21 23:45:44 centos7-t1 vmware-tools[19582]: Guest operating system daemon:[  OK  ]
8月 21 23:45:44 centos7-t1 vmware-tools[19582]: VGAuthService:[  OK  ]
8月 21 23:45:44 centos7-t1 systemd[1]: Started SYSV: Manages the services needed to run VMware software.

最后和Windows一样,可以删除虚拟机的虚拟CD驱动器。

0x04 结语

因为我是在家里使用,所以很多高级功能我都没用上,平常也就用来看看IP地址而已。