Module: VirtualBox::VM::Lifecycle
- Included in:
- VirtualBox::VM
- Defined in:
- lib/virtual_box/vm/lifecycle.rb
Overview
Mix-in for the VM class covering life-cycle management.
Instance Method Summary collapse
-
#control(action, action_data = nil) ⇒ Object
Controls a started virtual machine.
-
#create_configuration(config_path = nil) ⇒ Object
Creates the virtual machine configuration in VirtualBox.
-
#delete_configuration ⇒ Object
Deletes the VM configuration.
-
#register_configuration ⇒ Object
Registers the VM configuration with the VirtualBox installation.
-
#start(options = {}) ⇒ Object
Starts the virtual machine.
-
#stop ⇒ Object
Stops the virtual machine simulation.
-
#unregister_configuration ⇒ Object
Unregisters the VM configuration from the VirtualBox installation.
Instance Method Details
#control(action, action_data = nil) ⇒ Object
Controls a started virtual machine.
The following actions are supported:
:power_off:: hard power-off (pulling the power cord from the machine)
:acpi_power_button:: Power press
:inject_nmi:: NMI (non-maskable interrupt)
:vrdp:: enables or disables the VM's RDP server (set +action_data+ to :on
or :off)
Returns true for success, and false for failure.
58 59 60 61 62 63 |
# File 'lib/virtual_box/vm/lifecycle.rb', line 58 def control(action, action_data = nil) action = action.to_s.gsub '_', '' command = "VBoxManage --nologo controlvm #{uuid} #{action}" command += " #{action_data}" if action_data VirtualBox.shell_command(command)[:status] == 0 end |
#create_configuration(config_path = nil) ⇒ Object
Creates the virtual machine configuration in VirtualBox.
Args:
config_path:: path to the VM configuration file that will be created
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/virtual_box/vm/lifecycle.rb', line 70 def create_configuration(config_path = nil) raise 'Cannot create a configuration without a VM name' unless name command = %|VBoxManage --nologo createvm --name "#{name}"| if config_path command += %| --settingsfile "#{File.expand_path config_path}"| end result = VirtualBox.shell_command command raise 'VM creation failed' unless result[:status] == 0 uuid_match = /^UUID: (.*)$/.match result[:output] unless uuid_match raise "VM creation didn't output a UUID:\n#{result[:output]}" end self.uuid = uuid_match[1] config_match = /^Settings file: '(.*)'$/.match result[:output] unless uuid_match raise "VM creation didn't output a config file path:\n#{result[:output]}" end self.config_file = config_match[1] true end |
#delete_configuration ⇒ Object
Deletes the VM configuration.
Returns: true for success, false if de-registration failed
119 120 121 122 |
# File 'lib/virtual_box/vm/lifecycle.rb', line 119 def delete_configuration File.delete config_file true end |
#register_configuration ⇒ Object
Registers the VM configuration with the VirtualBox installation.
Returns: true for success, false if registration failed
98 99 100 101 102 103 104 |
# File 'lib/virtual_box/vm/lifecycle.rb', line 98 def register_configuration raise 'Call create_configuration before registering' unless config_file command = %|VBoxManage --nologo registervm "#{config_file}"| result = VirtualBox.shell_command command result[:status] == 0 end |
#start(options = {}) ⇒ Object
Starts the virtual machine.
The following options are supported:
:gui:: if set to true, VMs will be started in a GUI; this is intended to
help debugging
:rdp:: if set to true, RDP support will be enabled; by default, RDP
support is disabled; VirtualBox OSE does not support RDP, so the
call will raise an exception
24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/virtual_box/vm/lifecycle.rb', line 24 def start( = {}) if VirtualBox.ose? raise 'Cannot enable RDP support on VirtualBox OSE' if [:rdp] end if [:gui] command = "VBoxManage --nologo startvm #{uuid} --type gui" else command = "VBoxManage --nologo startvm #{uuid} --type headless" end return false unless VirtualBox.shell_command(command)[:status] == 0 [:rdp] ? control(:vrdp, :on) : true end |
#stop ⇒ Object
Stops the virtual machine simulation.
This is equivalent to pulling the power cord from a physical machine.
Returns true for success, and false for failure.
44 45 46 |
# File 'lib/virtual_box/vm/lifecycle.rb', line 44 def stop control :power_off end |
#unregister_configuration ⇒ Object
Unregisters the VM configuration from the VirtualBox installation.
Returns: true for success, false if de-registration failed
109 110 111 112 113 114 |
# File 'lib/virtual_box/vm/lifecycle.rb', line 109 def unregister_configuration raise "Can't unregister a configuration without a UUID" unless uuid result = VirtualBox.shell_command "VBoxManage --nologo unregistervm #{uuid}" result[:status] == 0 end |