From ac2c4203a2aa6e907cf3b019c10c54afa9f22f31 Mon Sep 17 00:00:00 2001 From: Brian Johnson Date: Wed, 12 Feb 2014 15:29:13 -0800 Subject: [PATCH] Add import_opts to the virtualbox-ovf builder. My use case is that I have existing virtualbox virtual machines where I need to keep the mac addresses so that the network interfaces come up when packer boots them. Otherwise I just get SSH timeout and I'm unable to configure the machines. --- builder/virtualbox/common/driver.go | 2 +- builder/virtualbox/common/driver_4_2.go | 3 ++- builder/virtualbox/common/driver_mock.go | 4 +++- builder/virtualbox/ovf/builder.go | 1 + builder/virtualbox/ovf/config.go | 2 ++ builder/virtualbox/ovf/step_import.go | 3 ++- website/source/docs/builders/virtualbox-ovf.html.markdown | 4 ++++ 7 files changed, 15 insertions(+), 4 deletions(-) diff --git a/builder/virtualbox/common/driver.go b/builder/virtualbox/common/driver.go index 3fd8c89ab..8c389d419 100644 --- a/builder/virtualbox/common/driver.go +++ b/builder/virtualbox/common/driver.go @@ -23,7 +23,7 @@ type Driver interface { Delete(string) error // Import a VM - Import(string, string) error + Import(string, string, string) error // Checks if the VM with the given name is running. IsRunning(string) (bool, error) diff --git a/builder/virtualbox/common/driver_4_2.go b/builder/virtualbox/common/driver_4_2.go index 78c91be5d..49609d059 100644 --- a/builder/virtualbox/common/driver_4_2.go +++ b/builder/virtualbox/common/driver_4_2.go @@ -40,11 +40,12 @@ func (d *VBox42Driver) Delete(name string) error { return d.VBoxManage("unregistervm", name, "--delete") } -func (d *VBox42Driver) Import(name, path string) error { +func (d *VBox42Driver) Import(name, path, opts string) error { args := []string{ "import", path, "--vsys", "0", "--vmname", name, + "--options", opts, } return d.VBoxManage(args...) diff --git a/builder/virtualbox/common/driver_mock.go b/builder/virtualbox/common/driver_mock.go index b73fb5c5f..39d0ebb5d 100644 --- a/builder/virtualbox/common/driver_mock.go +++ b/builder/virtualbox/common/driver_mock.go @@ -16,6 +16,7 @@ type DriverMock struct { ImportCalled bool ImportName string ImportPath string + ImportOpts string ImportErr error IsRunningName string @@ -51,10 +52,11 @@ func (d *DriverMock) Delete(name string) error { return d.DeleteErr } -func (d *DriverMock) Import(name, path string) error { +func (d *DriverMock) Import(name, path, opts string) error { d.ImportCalled = true d.ImportName = name d.ImportPath = path + d.ImportOpts = opts return d.ImportErr } diff --git a/builder/virtualbox/ovf/builder.go b/builder/virtualbox/ovf/builder.go index 5a80689aa..daf1f0052 100644 --- a/builder/virtualbox/ovf/builder.go +++ b/builder/virtualbox/ovf/builder.go @@ -58,6 +58,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe &StepImport{ Name: b.config.VMName, SourcePath: b.config.SourcePath, + ImportOpts: b.config.ImportOpts, }, /* new(stepAttachGuestAdditions), diff --git a/builder/virtualbox/ovf/config.go b/builder/virtualbox/ovf/config.go index 51918fdb2..56f37f43f 100644 --- a/builder/virtualbox/ovf/config.go +++ b/builder/virtualbox/ovf/config.go @@ -23,6 +23,7 @@ type Config struct { SourcePath string `mapstructure:"source_path"` VMName string `mapstructure:"vm_name"` + ImportOpts string `mapstructure:"import_opts"` tpl *packer.ConfigTemplate } @@ -59,6 +60,7 @@ func NewConfig(raws ...interface{}) (*Config, []string, error) { templates := map[string]*string{ "source_path": &c.SourcePath, "vm_name": &c.VMName, + "import_opts": &c.ImportOpts, } for n, ptr := range templates { diff --git a/builder/virtualbox/ovf/step_import.go b/builder/virtualbox/ovf/step_import.go index 58d3983c1..3dc72c174 100644 --- a/builder/virtualbox/ovf/step_import.go +++ b/builder/virtualbox/ovf/step_import.go @@ -11,6 +11,7 @@ import ( type StepImport struct { Name string SourcePath string + ImportOpts string vmName string } @@ -20,7 +21,7 @@ func (s *StepImport) Run(state multistep.StateBag) multistep.StepAction { ui := state.Get("ui").(packer.Ui) ui.Say(fmt.Sprintf("Importing VM: %s", s.SourcePath)) - if err := driver.Import(s.Name, s.SourcePath); err != nil { + if err := driver.Import(s.Name, s.SourcePath, s.ImportOpts); err != nil { err := fmt.Errorf("Error importing VM: %s", err) state.Put("error", err) ui.Error(err.Error()) diff --git a/website/source/docs/builders/virtualbox-ovf.html.markdown b/website/source/docs/builders/virtualbox-ovf.html.markdown index 36299e368..2112e276d 100644 --- a/website/source/docs/builders/virtualbox-ovf.html.markdown +++ b/website/source/docs/builders/virtualbox-ovf.html.markdown @@ -147,6 +147,10 @@ Optional: exported. By default this is "packer-BUILDNAME", where "BUILDNAME" is the name of the build. +* `import_opts` (string) - Additional options to pass to the `VBoxManage import`. + This can be useful for passing "keepallmacs" or "keepnatmacs" options for existing + ovf images. + ## Guest Additions Packer will automatically download the proper guest additions for the