diff --git a/builder/virtualbox/builder.go b/builder/virtualbox/builder.go index ab4e2314d..ba813b156 100644 --- a/builder/virtualbox/builder.go +++ b/builder/virtualbox/builder.go @@ -11,6 +11,7 @@ import ( "os" "os/exec" "strings" + "time" ) const BuilderId = "mitchellh.virtualbox" @@ -22,6 +23,7 @@ type Builder struct { } type config struct { + BootWait time.Duration `` GuestOSType string `mapstructure:"guest_os_type"` ISOMD5 string `mapstructure:"iso_md5"` ISOUrl string `mapstructure:"iso_url"` @@ -30,6 +32,8 @@ type config struct { SSHHostPortMax uint `mapstructure:"ssh_host_port_max"` SSHPort uint `mapstructure:"ssh_port"` VMName string `mapstructure:"vm_name"` + + RawBootWait string `mapstructure:"boot_wait"` } func (b *Builder) Prepare(raw interface{}) error { @@ -109,6 +113,13 @@ func (b *Builder) Prepare(raw interface{}) error { } } + if b.config.RawBootWait != "" { + b.config.BootWait, err = time.ParseDuration(b.config.RawBootWait) + if err != nil { + errs = append(errs, fmt.Errorf("Failed parsing boot_wait: %s", err)) + } + } + if b.config.SSHHostPortMin > b.config.SSHHostPortMax { errs = append(errs, errors.New("ssh_host_port_min must be less than ssh_host_port_max")) } diff --git a/builder/virtualbox/builder_test.go b/builder/virtualbox/builder_test.go index 9347a5f94..dbf6ccc2d 100644 --- a/builder/virtualbox/builder_test.go +++ b/builder/virtualbox/builder_test.go @@ -55,6 +55,25 @@ func TestBuilderPrepare_Defaults(t *testing.T) { } } +func TestBuilderPrepare_BootWait(t *testing.T) { + var b Builder + config := testConfig() + + // Test with a bad boot_wait + config["boot_wait"] = "this is not good" + err := b.Prepare(config) + if err == nil { + t.Fatal("should have error") + } + + // Test with a good one + config["boot_wait"] = "5s" + err = b.Prepare(config) + if err != nil { + t.Fatalf("should not have error: %s", err) + } +} + func TestBuilderPrepare_ISOMD5(t *testing.T) { var b Builder config := testConfig() diff --git a/builder/virtualbox/step_run.go b/builder/virtualbox/step_run.go index cfca9eebf..673e853f4 100644 --- a/builder/virtualbox/step_run.go +++ b/builder/virtualbox/step_run.go @@ -17,6 +17,7 @@ type stepRun struct{ } func (s *stepRun) Run(state map[string]interface{}) multistep.StepAction { + config := state["config"].(*config) driver := state["driver"].(Driver) ui := state["ui"].(packer.Ui) vmName := state["vmName"].(string) @@ -30,7 +31,11 @@ func (s *stepRun) Run(state map[string]interface{}) multistep.StepAction { s.vmName = vmName - time.Sleep(15 * time.Second) + if int64(config.BootWait) > 0 { + ui.Say(fmt.Sprintf("Waiting %s for boot...", config.BootWait)) + time.Sleep(config.BootWait) + } + return multistep.ActionContinue }