From b9cd48bbc6d5bd4d9a277dd683477f41ed6ba285 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 11 Jun 2013 16:34:44 -0700 Subject: [PATCH] builder/virtualbox: Create a virtual disk --- builder/virtualbox/builder.go | 1 + builder/virtualbox/step_create_disk.go | 60 ++++++++++++++++++++++++++ builder/virtualbox/step_create_vm.go | 2 - 3 files changed, 61 insertions(+), 2 deletions(-) create mode 100644 builder/virtualbox/step_create_disk.go diff --git a/builder/virtualbox/builder.go b/builder/virtualbox/builder.go index 051d0cfe3..ec882ef0c 100644 --- a/builder/virtualbox/builder.go +++ b/builder/virtualbox/builder.go @@ -60,6 +60,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) packer new(stepPrepareOutputDir), new(stepSuppressMessages), new(stepCreateVM), + new(stepCreateDisk), } // Setup the state bag diff --git a/builder/virtualbox/step_create_disk.go b/builder/virtualbox/step_create_disk.go new file mode 100644 index 000000000..a558e6b99 --- /dev/null +++ b/builder/virtualbox/step_create_disk.go @@ -0,0 +1,60 @@ +package virtualbox + +import ( + "fmt" + "github.com/mitchellh/multistep" + "github.com/mitchellh/packer/packer" + "path/filepath" + "strings" + "time" +) + +// This step creates the virtual disk that will be used as the +// hard drive for the virtual machine. +type stepCreateDisk struct{ + diskPath string +} + +func (s *stepCreateDisk) Run(state map[string]interface{}) multistep.StepAction { + config := state["config"].(*config) + driver := state["driver"].(Driver) + ui := state["ui"].(packer.Ui) + + format := "VDI" + path := filepath.Join(config.OutputDir, fmt.Sprintf("%s.%s", config.VMName, strings.ToLower(format))) + + command := []string{ + "createhd", + "--filename", path, + "--size", "40", + "--format", format, + "--variant", "Standard", + } + + ui.Say("Creating hard drive...") + err := driver.VBoxManage(command...) + if err != nil { + ui.Error(fmt.Sprintf("Error creating hard drive: %s", err)) + return multistep.ActionHalt + } + + // Set the path so that we can delete it later + s.diskPath = path + + time.Sleep(15 * time.Second) + return multistep.ActionContinue +} + +func (s *stepCreateDisk) Cleanup(state map[string]interface{}) { + if s.diskPath == "" { + return + } + + driver := state["driver"].(Driver) + ui := state["ui"].(packer.Ui) + + ui.Say("Unregistering and deleting hard disk...") + if err := driver.VBoxManage("closemedium", "disk", s.diskPath, "--delete"); err != nil { + ui.Error(fmt.Sprintf("Error deleting hard drive: %s", err)) + } +} diff --git a/builder/virtualbox/step_create_vm.go b/builder/virtualbox/step_create_vm.go index a27dbd087..da61ff986 100644 --- a/builder/virtualbox/step_create_vm.go +++ b/builder/virtualbox/step_create_vm.go @@ -4,7 +4,6 @@ import ( "fmt" "github.com/mitchellh/multistep" "github.com/mitchellh/packer/packer" - "time" ) // This step creates the actual virtual machine. @@ -42,7 +41,6 @@ func (s *stepCreateVM) Run(state map[string]interface{}) multistep.StepAction { } } - time.Sleep(15 * time.Second) return multistep.ActionContinue }