From cf191350cd3b17ec60e694f9c1e092dc0e8bc446 Mon Sep 17 00:00:00 2001 From: Brandon Liu Date: Thu, 4 Jul 2013 14:44:48 -0700 Subject: [PATCH] Virtualbox: clean up port forwarding before exporting OVF. --- builder/virtualbox/step_export.go | 16 +++++++++++++--- builder/virtualbox/step_forward_ssh.go | 2 +- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/builder/virtualbox/step_export.go b/builder/virtualbox/step_export.go index e29aabf0b..52472246a 100644 --- a/builder/virtualbox/step_export.go +++ b/builder/virtualbox/step_export.go @@ -7,8 +7,7 @@ import ( "path/filepath" ) -// This step creates the virtual disk that will be used as the -// hard drive for the virtual machine. +// This step cleans up forwarded ports and exports the VM to an OVF. // // Uses: // @@ -22,9 +21,20 @@ func (s *stepExport) Run(state map[string]interface{}) multistep.StepAction { ui := state["ui"].(packer.Ui) vmName := state["vmName"].(string) + // Clear out the Packer-created forwarding rule + ui.Say(fmt.Sprintf("Deleting forwarded port mapping for SSH (host port %d)", state["sshHostPort"])) + command := []string{"modifyvm", vmName, "--natpf1", "delete", "packerssh"} + if err := driver.VBoxManage(command...); err != nil { + err := fmt.Errorf("Error deleting port forwarding rule: %s", err) + state["error"] = err + ui.Error(err.Error()) + return multistep.ActionHalt + } + + // Export the VM to an OVF outputPath := filepath.Join(config.OutputDir, "packer.ovf") - command := []string{ + command = []string{ "export", vmName, "--output", diff --git a/builder/virtualbox/step_forward_ssh.go b/builder/virtualbox/step_forward_ssh.go index aee47bf1c..74a2354ec 100644 --- a/builder/virtualbox/step_forward_ssh.go +++ b/builder/virtualbox/step_forward_ssh.go @@ -36,7 +36,7 @@ func (s *stepForwardSSH) Run(state map[string]interface{}) multistep.StepAction } } - // Attach the disk to the controller + // Create a forwarded port mapping to the VM ui.Say(fmt.Sprintf("Creating forwarded port mapping for SSH (host port %d)", sshHostPort)) command := []string{ "modifyvm", vmName,