VirtualBox wrapper now uses import to create a VM rather than createvm

This commit is contained in:
Mitchell Hashimoto 2010-01-22 13:34:46 -08:00
parent 148cd091bb
commit 92dd514c4d
2 changed files with 7 additions and 71 deletions

View File

@ -1,69 +1,13 @@
class VirtualBox
class <<self
def create(name, options = {})
modify_options = {
# Set up base system: memory, cpus, etc.
"memory" => options[:memory] || "360",
"vram" => options[:vram] || "12",
"ostype" => "Ubuntu",
"acpi" => "on",
"ioapic" => "off",
"cpus" => "1",
"pae" => "on",
"hwvirtex" => "on",
"hwvirtexexcl" => "off",
"nestedpaging" => "off",
"vtxvpid" => "off",
"accelerate3d" => "off",
"biosbootmenu" => "messageandmenu",
"boot1" => "disk",
"boot2" => "dvd",
"boot3" => "none",
"boot4" => "none",
"firmware" => "bios",
# Networking
"nic1" => "bridged",
"nictype1" => "Am79C973",
"cableconnected1" => "on",
"nictrace1" => "off",
"bridgeadapter1" => "en0: Ethernet",
"macaddress1" => "08002771F257",
# Ports
"audio" => "none",
"clipboard" => "bidirectional",
"usb" => "off",
"usbehci" => "off"
}
def create(name, options = {})
# To create the VM, we simply import the base OVF which takes care
# of matching up the hardware and setting up the configuration.
command("import #{File.expand_path("~/.hobo/base/base.ovf")} --vsys 0 --vmname #{name}")
# Create the raw machine itself. In this incarnation, nothing
# is yet configured.
HOBO_LOGGER.info("Creating VM #{name}...")
command("createvm --name #{name} --register")
# Modify the VM with the options set
modify_options.each { |key, value| modify(name, key, value) }
# Clone the hard drive that we'll be using
# TODO: Change hardcoded paths to use config module when ready
HOBO_LOGGER.info("Cloning from base disk...")
clone_disk("/Users/mitchellh/.hobo/disks/base.vmdk", "/Users/mitchellh/.hobo/disks/#{name}.vmdk")
# Attach storage controllers
HOBO_LOGGER.info("Attaching clone disk to VM...")
command("storagectl #{name} --name \"IDE Controller\" --add ide --controller PIIX4 --sataportcount 2")
command("storageattach #{name} --storagectl \"IDE Controller\" --port 0 --device 0 --type hdd --medium \"/Users/mitchellh/.hobo/disks/#{name}.vmdk\"")
end
def modify(name, key, value)
# Wrap values with spaces in quotes
value = "\"#{value}\"" if value =~ /\s/
command("modifyvm #{name} --#{key} #{value}")
end
def clone_disk(sourcepath, destpath)
# TODO: Escape filepaths
command("clonehd #{sourcepath} #{destpath}")
# We must manually modify the mac address to match the base VM
# so that eth0 will still work
command("modifyvm #{name} --macaddress1 08002771F257")
end
def command(cmd)

View File

@ -5,12 +5,4 @@ class VirtualBoxTest < Test::Unit::TestCase
# Stub out command so nothing actually happens
VirtualBox.stubs(:command)
end
context "modifying VMs" do
should "wrap double quotes around values with spaces" do
# TODO: how to use mocha with regex expectations?
#VirtualBox.expects(:command).with(/"my value"/)
VirtualBox.modify(@name, "key", "my value")
end
end
end