如何使用命令行安全地关闭VirtualBox中的Guest OS
我有Ubuntu 10.10并使用VirtualBox 3.2。 作为Guest OS,我在VirtualBox中有另一个Ubuntu。
我的Host Ubuntu启动后,我使用以下命令自动启动Guest Ubuntu:
VBoxHeadless -startvm Ubuntu --vrdp on
然后我可以使用ssh或tsclient访问它。
现在我需要在关闭主机Ubuntu后自动关闭Guest Ubuntu。 有没有人知道使用命令行自动关闭Guest Ubuntu的任何安全方法? 我发现有两种方法可以关闭Guest OS,但我不确定它们是否安全。 他们是:
VBoxManage controlvm Ubuntu acpipowerbutton
要么
VBoxManage controlvm Ubuntu poweroff
如果打开Gnome Power管理首选项,则可以设置电源按钮时要执行的操作。 如果将其设置为关闭,则在使用VBoxManage controlvm Ubuntu acpipowerbutton
命令按下虚拟电源按钮时,它将正常关闭。
要安全关闭vm,请使用以下命令:
vboxmanage controlvm Ubuntu poweroff soft
如果你在服务器关闭vm时,你必须等待vm(s)
(在这个shellcript’root’中是调用者,’theuser’是’thevm’的所有者)
我知道当命令VBoxManage list runningvms
的输出返回一个空字符串时,vms已经完成。
我在我的shell脚本中使用它
... stop(){ su -c "VBoxManage controlvm thevm acpipowerbutton" -s /bin/bash theuser while [ "`su -c 'VBoxManage list runningvms' -s /bin/bash theuser`" != "" ] do echo waiting for VMs to shutdown sleep 3 done } ...
不要这样做:
VBoxManage controlvm Ubuntu poweroff
它相当于在真正的计算机上拔电源插头。 你不想这样做!
使用ACPI关闭方法(检查像Egil建议的电源管理设置)或者尝试使用保存状态方法( savestate
)。
您应该像关闭真实硬件一样关闭虚拟Ubuntu。 从ssh会话上的commanline远程发出:
sudo poweroff
如果你想保存机器状态(类似于suspend / hibernate),请使用@htorque的答案中的第二点。
我通常通过$ sudo shutdown -h now
关闭VM(Linux) $ sudo shutdown -h now
我想这是终止VM的最优雅方式,因为它是关闭(不是断电)并且为登录用户提供宽限时间和(可能)消息。