Commit c98b8d96 authored by Brian Johnson's avatar Brian Johnson

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.
parent 1a57e389
...@@ -23,7 +23,7 @@ type Driver interface { ...@@ -23,7 +23,7 @@ type Driver interface {
Delete(string) error Delete(string) error
// Import a VM // Import a VM
Import(string, string) error Import(string, string, string) error
// Checks if the VM with the given name is running. // Checks if the VM with the given name is running.
IsRunning(string) (bool, error) IsRunning(string) (bool, error)
......
...@@ -40,11 +40,12 @@ func (d *VBox42Driver) Delete(name string) error { ...@@ -40,11 +40,12 @@ func (d *VBox42Driver) Delete(name string) error {
return d.VBoxManage("unregistervm", name, "--delete") 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{ args := []string{
"import", path, "import", path,
"--vsys", "0", "--vsys", "0",
"--vmname", name, "--vmname", name,
"--options", opts,
} }
return d.VBoxManage(args...) return d.VBoxManage(args...)
......
...@@ -16,6 +16,7 @@ type DriverMock struct { ...@@ -16,6 +16,7 @@ type DriverMock struct {
ImportCalled bool ImportCalled bool
ImportName string ImportName string
ImportPath string ImportPath string
ImportOpts string
ImportErr error ImportErr error
IsRunningName string IsRunningName string
...@@ -51,10 +52,11 @@ func (d *DriverMock) Delete(name string) error { ...@@ -51,10 +52,11 @@ func (d *DriverMock) Delete(name string) error {
return d.DeleteErr return d.DeleteErr
} }
func (d *DriverMock) Import(name, path string) error { func (d *DriverMock) Import(name, path, opts string) error {
d.ImportCalled = true d.ImportCalled = true
d.ImportName = name d.ImportName = name
d.ImportPath = path d.ImportPath = path
d.ImportOpts = opts
return d.ImportErr return d.ImportErr
} }
......
...@@ -58,6 +58,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe ...@@ -58,6 +58,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
&StepImport{ &StepImport{
Name: b.config.VMName, Name: b.config.VMName,
SourcePath: b.config.SourcePath, SourcePath: b.config.SourcePath,
ImportOpts: b.config.ImportOpts,
}, },
/* /*
new(stepAttachGuestAdditions), new(stepAttachGuestAdditions),
......
...@@ -23,6 +23,7 @@ type Config struct { ...@@ -23,6 +23,7 @@ type Config struct {
SourcePath string `mapstructure:"source_path"` SourcePath string `mapstructure:"source_path"`
VMName string `mapstructure:"vm_name"` VMName string `mapstructure:"vm_name"`
ImportOpts string `mapstructure:"import_opts"`
tpl *packer.ConfigTemplate tpl *packer.ConfigTemplate
} }
...@@ -59,6 +60,7 @@ func NewConfig(raws ...interface{}) (*Config, []string, error) { ...@@ -59,6 +60,7 @@ func NewConfig(raws ...interface{}) (*Config, []string, error) {
templates := map[string]*string{ templates := map[string]*string{
"source_path": &c.SourcePath, "source_path": &c.SourcePath,
"vm_name": &c.VMName, "vm_name": &c.VMName,
"import_opts": &c.ImportOpts,
} }
for n, ptr := range templates { for n, ptr := range templates {
......
...@@ -11,6 +11,7 @@ import ( ...@@ -11,6 +11,7 @@ import (
type StepImport struct { type StepImport struct {
Name string Name string
SourcePath string SourcePath string
ImportOpts string
vmName string vmName string
} }
...@@ -20,7 +21,7 @@ func (s *StepImport) Run(state multistep.StateBag) multistep.StepAction { ...@@ -20,7 +21,7 @@ func (s *StepImport) Run(state multistep.StateBag) multistep.StepAction {
ui := state.Get("ui").(packer.Ui) ui := state.Get("ui").(packer.Ui)
ui.Say(fmt.Sprintf("Importing VM: %s", s.SourcePath)) 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) err := fmt.Errorf("Error importing VM: %s", err)
state.Put("error", err) state.Put("error", err)
ui.Error(err.Error()) ui.Error(err.Error())
......
...@@ -147,6 +147,10 @@ Optional: ...@@ -147,6 +147,10 @@ Optional:
exported. By default this is "packer-BUILDNAME", where "BUILDNAME" is exported. By default this is "packer-BUILDNAME", where "BUILDNAME" is
the name of the build. 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 ## Guest Additions
Packer will automatically download the proper guest additions for the Packer will automatically download the proper guest additions for the
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment