[VMware] 在 CentOS7 上安裝 VMware vSphere CLI (vcli) 操作 VM

[VMware] 在 CentOS7 上安裝 VMware vSphere CLI (vcli) 操作 VM

很久以前寫過一篇使用 VMware vSphere CLI

那時候以為 vmware-cmd 沒辦法用來做快照 (snapshot),所以又另外用 vim-cmd 來做 VM 的快照

最近才發現在 vmware-cmd 是可以做 snapshot 的,

只是在 CentOS7 上安裝時遇到了不少麻煩,下面記錄一下~

 

參考資料:

VMware PERL SDK installation issue on CentOS 6

The Pain and Fury of vmware-cli on CentOS 7

 

1. 下載 VMware vSphere CLI

目前最新版是 VMware vSphere CLI 6.0

到這個頁面後找適合的檔案下載,因為我的是 CentOS 7 x64,

所以抓的是 VMware-vSphere-CLI-6.0.0-2503617.x86_64.tar.gz~

 

2. 安裝相關套件

執行下面的指令,先將一些必要的套件安裝起來:

sudo yum install openssl-devel libuuid-devel

 

接著用 cpan 安裝/更新一些套件…

下面套件的列表很長,這些是安裝 VMware vSphere CLI 時會指出來的,

所以你也可以先不裝,等到安裝程式警告時再裝也可以:

sudo cpan Devel::StackTrace Class::Data::Inheritable Convert::ASN1 Crypt::OpenSSL::RSA Crypt::X509 Exception::Class UUID::Random Archive::Zip Path::Class Try::Tiny Class::MethodMaker UUID Data::Dump SOAP::Lite LWP::Protocol::https Socket6 IO::Socket::INET6 Net::INET6Glue JSON::PP MIME::Base64

 

3. 安裝 VMware vSphere CLI

將 VMware-vSphere-CLI-6.0.0-2503617.x86_64.tar.gz 解開後,

執行 vmware-install.pl 開始安裝:

tar zxvf VMware-vSphere-CLI-6.0.0-2503617.x86_64.tar.gz
cd vmware-vsphere-cli-distrib/
sudo ./vmware-install.pl

 

一開始會先顯示 EULA,輸入 yes 確認:

Creating a new vSphere CLI installer database using the tar4 format.
Installing vSphere CLI 6.0.0 build-2503617 for Linux.
You must read and accept the vSphere CLI End User License Agreement to
continue.
Press enter to display it.
VMware(r) vSphere Software Development Kit License Agreement
......
Do you accept? (yes/no) yes
Thank you.

 

接下來一步很重要,問你是否要直接使用安裝程式預帶的 Perl 套件,

一開始我選了 yes,後來安裝雖然成功,但完全無法使用,

想要列出 VM 時會卡很久,接著吐出下面的錯誤訊息:

testuser@localhost ~ $ vmware-cmd -H 172.22.1.1 -U root -P mypasswd -l
length() used on @array (did you mean "scalar(@array)"?) at /usr/lib64/perl5/IO/Compress/Zlib/Extra.pm line 198.
SOAP request error - possibly a protocol issue: 500 read timeout

 

因此請還是乖乖地先在步驟 2 用 cpan 把需要的套件都先裝好,

然後在這一步選擇 no 吧:

This vSphere CLI installer includes precompiled Perl modules for RHEL.
Answering yes will install the precompiled modules, and answering no will
install from CPAN.
Do you want to install precompiled Perl modules for RHEL?
[yes] no

 

如果你先跳過了步驟 2 裡安裝 cpan 套件的部分,

接下來就會看到說哪些套件還沒有安裝,可以這時候再安裝

(步驟 2 那串長長的 cpan 列表就是這麼來的):

Network is unavailable, please configure the network first otherwise please
install the following modules manually for use by vSphere CLI:
Devel::StackTrace 1.31 or newer
Class::Data::Inheritable 0.08 or newer
Convert::ASN1 0.26 or newer
Crypt::OpenSSL::RSA 0.28 or newer
Crypt::X509 0.51 or newer
Exception::Class 1.37 or newer
UUID::Random 0.04 or newer
Archive::Zip 1.28 or newer
Path::Class 0.33 or newer
Try::Tiny 0.22 or newer
Class::MethodMaker 2.10 or newer
UUID 0.03 or newer
Data::Dump 1.15 or newer
SOAP::Lite 0.710.08 or newer
LWP::Protocol::https 5.805 or newer
Socket6  0.23 or newer
IO::Socket::INET6 2.71 or newer
Net::INET6Glue 0.600_1 or newer

 

假設 cpan 套件都已經先裝好了,再執行 vmware-install.pl,就可以繼續安裝完成了:

In which directory do you want to install the executable files?
[/usr/bin]
Please wait while copying vSphere CLI files...
The installation of vSphere CLI 6.0.0 build-2503617 for Linux completed
successfully. You can decide to remove this software from your system at any
time by invoking the following command:
"/usr/bin/vmware-uninstall-vSphere-CLI.pl".
This installer has successfully installed both vSphere CLI and the vSphere SDK
for Perl.
The following Perl modules were found on the system but may be too old to work
with vSphere CLI:
Enjoy,
--the VMware team

 

4. 使用 vmware-cmd 來執行 VM 操作

相關的指令可以打 vmware-cmd -h 看基本的操作,

或是參考 vmware-cmd: perform virtual machine operations 有更詳盡的參數說明~

 

舉例來說,要列出 ESXi 上所有的 VM 時,可以用 -l 參數:

testuser@localhost ~ $ vmware-cmd -H 172.22.1.1 -U root -P mypasswd -l
/vmfs/volumes/548ee46b-eaa0c112-9dd2-bc305bf541db/CentOS_7.0_build_machine/CentOS_7.0_build_machine.vmx
/vmfs/volumes/548ee46b-eaa0c112-9dd2-bc305bf541db/CentOS_6.2_build_machine/CentOS_6.2_build_machine.vmx
/vmfs/volumes/548ee46b-eaa0c112-9dd2-bc305bf541db/CentOS_5.4_build_machine/CentOS_5.4_build_machine.vmx
/vmfs/volumes/548ee46b-eaa0c112-9dd2-bc305bf541db/WinXP SP3 en/WinXP SP3 en.vmx

 

要對某個 VM 做操作時,得把 .vmx 的路徑加上去,再加上要操作的動作,

像是要看 CentOS_7.0_build_machine 這台 VM 有沒有快照的話,用 hassnapshot 參數:

testuser@localhost ~ $ vmware-cmd -H 172.22.1.1 -U root -P mypasswd /vmfs/volumes/548ee46b-eaa0c112-9dd2-bc305bf541db/CentOS_7.0_build_machine/CentOS_7.0_build_machine.vmx hassnapshot
hassnapshot () = 0

 

建立快照可以用 createsnapshot:

testuser@localhost ~ $ vmware-cmd -H 172.22.1.1 -U root -P mypasswd /vmfs/volumes/548ee46b-eaa0c112-9dd2-bc305bf541db/CentOS_7.0_build_machine/CentOS_7.0_build_machine.vmx createsnapshot "Init" "" 1 0
createsnapshot (Init  1 0) = 1

 

用 start 可以將 VM 開機:

testuser@localhost ~ $ vmware-cmd -H 172.22.1.1 -U root -P mypasswd /vmfs/volumes/548ee46b-eaa0c112-9dd2-bc305bf541db/CentOS_7.0_build_machine/CentOS_7.0_build_machine.vmx start
start() = 1

 

當然這樣子可以使用的指令還是不算多,不過對於透過 SSH,

來將 VM 開關機/建立快照/回復快照來說,已經夠用了~^^

 

(本頁面已被瀏覽過 1,112 次)

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

這個網站採用 Akismet 服務減少垃圾留言。進一步了解 Akismet 如何處理網站訪客的留言資料