LoginSignup
42
68

More than 5 years have passed since last update.

VMware ESX仮想マシンをコマンドで操作

Last updated at Posted at 2015-02-21

自宅ではESXを使っています。
デスクトップはMacを使っているのですが、MacではvSphere Clientが使えず、仮想マシンの起動・停止のためだけにWindowsマシンを使うのも面倒なので、コマンドで操作する方法を調べてみたのでメモ。

SSHでESXにログインできることが前提です。

ESXで使えるコマンド

  • vim-cmd: いろいろできるコマンド。基本的な操作はこれを使えばよさそう。
  • esxcli: ネットワークやホストの操作が可能
  • vmkfstools: vmdk等のディスクイメージ操作

vim-cmdコマンド

コマンド体系

以下のように、操作する対象によってグループがわかれているようです。

~ # vim-cmd
Commands available under /:
hbrsvc/       internalsvc/  solo/         vmsvc/
hostsvc/      proxysvc/     vimsvc/       help

コマンドの実行方法

グループとコマンドを以下のように指定して実行

~ # vim-cmd vmsvc/getallvms

vim-cmd vmsvc/xxx で使えるコマンドの一覧

VMの操作はvmsvc以下のコマンドで実行できます。だいたい名前を見れば分かりそうです。

~ # vim-cmd vmsvc
Commands available under vmsvc/:
acquiremksticket                 get.snapshotinfo
acquireticket                    get.spaceNeededForConsolidation
connect                          get.summary
convert.toTemplate               get.tasklist
convert.toVm                     getallvms
createdummyvm                    gethostconstraints
destroy                          login
device.connection                logout
device.connusbdev                message
device.disconnusbdev             power.getstate
device.diskadd                   power.hibernate
device.diskaddexisting           power.off
device.diskremove                power.on
device.getdevices                power.reboot
device.toolsSyncSet              power.reset
device.vmiadd                    power.shutdown
device.vmiremove                 power.suspend
devices.createnic                power.suspendResume
disconnect                       queryftcompat
get.capability                   reload
get.config                       setscreenres
get.config.cpuidmask             snapshot.create
get.configoption                 snapshot.dumpoption
get.datastores                   snapshot.get
get.disabledmethods              snapshot.remove
get.environment                  snapshot.removeall
get.filelayout                   snapshot.revert
get.filelayoutex                 snapshot.setoption
get.guest                        tools.cancelinstall
get.guestheartbeatStatus         tools.install
get.managedentitystatus          tools.upgrade
get.networks                     unregister
get.runtime                      upgrade

コマンドのヘルプ

以下のようにしてヘルプを参照できます。

~ # vim-cmd help vmsvc/getallvms
Usage: getallvms
~ # vim-cmd help vmsvc/power.on
Usage: power.on vmid

Power on the specified virtual machine.

コマンドの実行例

VM操作のコマンド実行の流れは

  1. vmsvc/getallvm で vmidを取得
  2. vmidを指定して各種操作を実行

VMの一覧取得

~ # vim-cmd vmsvc/getallvm

こんな感じででてきます。

~ # vim-cmd vmsvc/getallvms
Vmid             Name                                            File                                        Guest OS          Version                  Annotation
1008   vCenter Server Appliance   [datastore1] vCenter Server Appliance/vCenter Server Appliance.vmx   sles11_64Guest          vmx-07    VMware vCenter Server Appliance
1009   Windows 8.1 (x64)          [datastore1] Windows 8.1 (x64)/Windows 8.1 (x64).vmx                 windows8_64Guest        vmx-08
1012   NetScaler vpx01            [datastore1] NetScaler vpx01/NetScaler vpx01.vmx                     solaris10_64Guest       vmx-07
944    RHEL6.1                    [datastore1] RHEL6.1/RHEL6.1.vmx                                     rhel6_64Guest           vmx-07

VMの起動

vmid 944を指定してRHEL6.1を起動

~ # vim-cmd vmsvc/power.on 944
Powering on VM:

電源状態の確認

電源ONされたことを確認

~ # vim-cmd vmsvc/power.getstate 944
Retrieved runtime info
Powered on

ステータスや設定情報の確認

以下のコマンドでステータスや設定が確認できます。

~ # vim-cmd vmsvc/get.summary vmid

以下のように、かなり詳細な情報が確認できます。

~ # vim-cmd vmsvc/get.summary 944
Listsummary:

(vim.vm.Summary) {
   dynamicType = <unset>,
   vm = 'vim.VirtualMachine:944',
   runtime = (vim.vm.RuntimeInfo) {
      dynamicType = <unset>,
      device = (vim.vm.DeviceRuntimeInfo) [
         (vim.vm.DeviceRuntimeInfo) {
            dynamicType = <unset>,
            runtimeState = (vim.vm.DeviceRuntimeInfo.VirtualEthernetCardRuntimeState) {
               dynamicType = <unset>,
               vmDirectPathGen2Active = false,
               vmDirectPathGen2InactiveReasonOther = (string) [
                  "vmNptIncompatibleNetwork"
               ],
               vmDirectPathGen2InactiveReasonExtended = <unset>,
            },
            key = 4000,
         }
      ],
      host = 'vim.HostSystem:ha-host',
      connectionState = "connected",
      powerState = "poweredOn",
      faultToleranceState = "notConfigured",
      dasVmProtection = (vim.vm.RuntimeInfo.DasProtectionState) null,
...

esxcliコマンド

ESXホストのシャットダウン

# いきなりはshutdownできない
~ # esxcli system shutdown poweroff -r maintenance
System is not in maintenance mode. Cannot perform requested operation.

# まずはmaintanance modeにする
~ # esxcli system maintenanceMode get
Disables
~ # esxcli system maintenanceMode set --timeout 0 --enable yes
~ # esxcli system maintenanceMode get
Enabled

# esxcliからシャットダウン。poweroffかrebootが可能
~ # esxcli system shutdown --help
Usage: esxcli system shutdown {cmd} [cmd options]

Available Commands:
  poweroff              Power off the system. The host must be in maintenance mode.
  reboot                Reboot the system. The host must be in maintenance mode.

# -rオプションで理由の指定が必須
~ # esxcli system shutdown poweroff --help
Usage: esxcli system shutdown poweroff [cmd options]

Description:
  poweroff              Power off the system. The host must be in maintenance mode.

Cmd options:
  -d|--delay=<long>     Delay interval in seconds
  -r|--reason=<str>     Reason for performing the operation (required)

# poweroff 実行  
~ # esxcli system shutdown poweroff -r maintenance

一連のコマンド

esxcli system maintenanceMode get
esxcli system maintenanceMode set --timeout 0 --enable yes
esxcli system maintenanceMode get
esxcli system shutdown poweroff -r maintenance

参考

かなりよくまとまった記事がいくつかあるので、あとはこちらを見れば大抵のことはできそう。

42
68
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
42
68