labunix's blog

labunixのラボUnix

bashで動く「wakeonvm.sh」を作ってみた。

■bashで動く「wakeonvm.sh」を作ってみた。

 sshでホストにログインして仮想マシンを起動したり、
 Web管理画面から仮想マシンを起動したりは出来るけど、
 もうワンステップ減らしたい。

 vmware-toolsがどうとかvmxnet3がどうとか考えずに、
 WOL(Wake On Lan)っぽく仮想マシンの電源を入れたい。

■前提として、Linuxからssh接続してESXiのコマンドを実行するだけなので、
 ホスト側でsshサービスを起動しておく必要がある。

# esxcli system version get
   Product: VMware ESXi
   Version: 6.5.0
   Build: Releasebuild-5310538
   Update: 0
   Patch: 19

$ lsb_release -a
No LSB modules are available.
Distributor ID:	Debian
Description:	Debian GNU/Linux 9.2 (stretch)
Release:	9.2
Codename:	stretch

■引数なしで実行すると仮想マシンのリストを取得する。

$ sudo ./myscripts/wakeonvm.sh
Password: 
Vmid       Name                            File                             Guest OS          Version                                                   Annotation                                                
1      jessie          [datastore1] jessie/jessie.vmx                 debian8_64Guest         vmx-11                                                                                                              
21     ontap812        [datastore1] ontap812/ontap812.vmx             freebsd64Guest          vmx-07                                                                                                              
25     w2k12r2         [datastore1] w2k12r2/w2k12r2.vmx               windows8Server64Guest   vmx-11                                                                                                              
32     stretch         [datastore1] stretch/stretch.vmx               debian8_64Guest         vmx-11                                                                                                              
34     vsa-jessie      [datastore1] vsa-jessie/vsa-jessie.vmx         debian8_64Guest         vmx-13                                                                                                              
36     vm-apache       [datastore1] vm-apache/vm-apache.vmx           debian9_64Guest         vmx-13                                                                                                              
37     vm-datasearch   [datastore1] vm-datasearch/vm-datasearch.vmx   debian9_64Guest         vmx-13                                                                                                              
38     vm-android      [datastore1] vm-android/vm-android.vmx         oracleLinux7_64Guest    vmx-13                                                                                                              
39     vm-cuckoo       [datastore1] vm-cuckoo/vm-cuckoo.vmx           debian9_64Guest         vmx-13                                   

■引数はひとつだけ許可するので、仮想マシンの「Vmid」を指定する。
 ひとまず「0<[Vmid]<100」に制限した。
 起動状態の確認のために3秒のウエイトをかけたので、
 sshのパスワードを2回要求されるけど、ここは諦めよう。

$ sudo ./myscripts/wakeonvm.sh 38
Password: 
Powering on VM:
Power on failed
waiting... 3
waiting... 2
waiting... 1
Password: 
Retrieved runtime info
Powered on

■引数に上記以外の値を指定した場合は以下のように使用方法を表示する。

$ sudo ./myscripts/wakeonvm.sh -h
Usage: ./myscripts/wakeonvm.sh [Vmid]
Get a list of virtual machines if there is no argument
Giving a numerical value greater than 0 and less than 100 as an Vmid in the argument start the virtual machine.

$ sudo ./myscripts/wakeonvm.sh 100
Usage: ./myscripts/wakeonvm.sh [Vmid]
Get a list of virtual machines if there is no argument
Giving a numerical value greater than 0 and less than 100 as an Vmid in the argument start the virtual machine.

$ sudo ./myscripts/wakeonvm.sh 0
Usage: ./myscripts/wakeonvm.sh [Vmid]
Get a list of virtual machines if there is no argument
Giving a numerical value greater than 0 and less than 100 as an Vmid in the argument start the virtual machine.

■ソースは以下。
 ローカルの実行者もroot権限を必要とするようにしたけど、
 ホストのrootパスワードが分かるユーザなら、「root check」は要らないかも知れないけど。

$ cat myscripts/wakeonvm.sh 
#!/bin/bash

TARGETVMHOST="root@vmhost"

# root check

if [ `id -u` -ne "0" ];then
  echo "You must be root to run $0"
  exit 1
fi

if [ $# -eq 0 ];then
  ssh ${TARGETVMHOST} vim-cmd vmsvc/getallvms
elif [ $# -eq 1 ];then
  VM=$(($1%100))
  if [ ${VM} -gt 0 ] && [ ${VM} -lt 100 ];then
    ssh ${TARGETVMHOST} vim-cmd vmsvc/power.on ${VM}
    for n in $(seq 3 -1 1);do
      echo "waiting... $n"
    done
    ssh ${TARGETVMHOST} vim-cmd vmsvc/power.getstate ${VM}
  else
    echo "Usage: $0 [Vmid]"
    echo "Get a list of virtual machines if there is no argument"
    echo "Giving a numerical value greater than 0 and less than 100 as an Vmid in the argument start the virtual machine."
  fi
fi

|