« 2011年1月 | トップページ | 2011年5月 »

2011/04/26

[ VirtualBox ] How to save the state of a virtual machine and stop it without GUI.

Have you ever want to save the state of a virtual machines and stop them with CUI? I'll illustrate how to save the state of the VM to disk and then stop it.

It takes 2 steps. First, checking which virtual machine is running. To do it, use VBoxManage command with list runningvms options. For example:

VirtualBox list runningvms

Then you'll get the information which virtual machine is running like following:

"ubuntu10_04" {57e88170-bb7e-4728-ba05-4724cdfe6cdb}
"squeeze_x64" {0d584575-84f6-4800-b74c-ef6b22cfbe9a}

Each line consists of two elements: VM name on the front and UUID on the back. At the previous, "ubuntu10_04" and "squeese_x64" are VM name (note, double quotes are enclosed). {57e88170-bb7e-4728-ba05-4724cdfe6cdb} and {0d584575-84f6-4800-b74c-ef6b22cfbe9a} are UUID.

UUID is the internal unique identifier that VirtualBox uses to refer to the virtual machine.

Second, Saving the sate of VM and then stopping it. To do it, use VBoxManage command with cntrolvm <VM> savestate options(*1). Note, when you execute command, please replace <VM> to a VM name or an UUID. For example, you want to operate to "squeeze_x64", input following:

VBoxManage controlvm "squeeze_x64" savestate

or

VBoxManage controlvm {0d584575-84f6-4800-b74c-ef6b22cfbe9a} savestate

done.

WARNING
An another option "controlvm <VM> poweroff" also can stop a virtual machine. But the state is NOT saved! Data may be LOST and system may be BROKEN.
Reference
VirtualBox Manual Chapter8. VBoxManage

| | コメント (0) | トラックバック (0)

[ 備忘録 ] コマンドラインから Virtual Box の仮想マシンを停止する方法

2012-11-14 修正
起動している仮想マシンを確認するコマンドを修正しました。
yu さん、ご指摘ありがとうございました。

VirtualBox で仮想マシン実行中に、ホストをシャットダウンしようとすると警告が表示されます。 もちろん、警告を無視して VirtualBox を強制終了させればホストをシャットダウンできますが、 この場合は VirtualBox の設定が壊れたり仮想マシンが壊れてしまうことがあります。

震災の影響で電力供給が不安定ないまだと、UPS ( 無停電電源装置 ) のシャットダウンシグナルをトリガーとしてマシンを停止させたい(*1)といったこともあるでしょう。UPS からシャットダウンシグナルが送られてくる場合はその地域に停電が発生している可能性もあり、マシンにリモート接続してシャットダウンといった方法は取れないことも考えられます。安全に仮想マシンを停止するにはどうすればいいのでしょうか?

結論からいってしまうと、VirtualBox には VBoxManage という仮想マシンの設定や状態を管理するコマンドが用意されているので、これを利用します。VBoxManage コマンドですべての仮想マシンを終了するスクリプトを作成しておき、シャットダウンシグナル受信時にそのスクリプトが実行されるようにしておきます。

VBoxManage コマンドで仮想マシンを停止するには次の 2 つのステップで操作します。

  1. 起動している仮想マシンを確認する
  2. 起動している仮想マシンを停止する

1. 起動している仮想マシンを確認する
VBoxManage コマンドに "list runningvms" オプションをつけて実行します。

> VBoxManage list runningvms
"ubuntu10_04" {57e88170-bb7e-4728-ba05-4724cdfe6cdb}
"squeeze_x64" {0d584575-84f6-4800-b74c-ef6b22cfbe9a}

ダブルクォートで囲まれている部分、上記の例では "ubuntu10_04" と "squeeze_x64" が、GUI の仮想マシン名にあたるものです。 その横の波かっこ {} で囲まれている部分、上記の例では {57e88170-bb7e-4728-ba05-4724cdfe6cdb} と {0d584575-84f6-4800-b74c-ef6b22cfbe9a} が UUID と呼ばれるもので、VirtualBox がそれぞれの仮想マシンの管理に使用している固有の識別子です。

2. 起動している仮想マシンを停止する
VBoxManage コマンドに "cntrolvm <VM> savestate" オプションをつけて実行します(*2)。
<VM> の部分は、先ほど確認した仮想マシン名または UUID を使用します。

たとえば、先ほど確認した中にある squeeze_x64 を停止する場合は次のようになります。
100% と表示されてプロンプトに復帰すれば停止処理は完了しています。

> VBoxManage controlvm {0d584575-84f6-4800-b74c-ef6b22cfbe9a} savestate
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%

起動中の仮想マシンすべてを停止するスクリプトを作成し、シャットダウン時に実行するようにしておきましょう(*3)。

(*1) シャットダウンシグナルを送ってくれるかどうかは UPS 次第です。安物だと何もしてくれないことも・・・

(*2) "cntrolvm <VM> poweroff" というオプションもありますが、こちらは「いきなり電源ケーブルを引っこ抜く」ことに相当します。つまり、こちらを実行するとデータは保存されませんし、仮想マシンが壊れる可能性もあります。

(*3) Windows の場合はグループポリシーで設定します。XP/Vista/7 では通常は設定用画面は見えませんが、MMC というツールを使用することで設定できます (下位エディション除く )。

Reference
VirtualBox Manual Chapter8. VBoxManage

| | コメント (2) | トラックバック (0)

« 2011年1月 | トップページ | 2011年5月 »