diff --git a/CHANGELOG.md b/CHANGELOG.md index 81375fbaf..e9b39c439 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ ## 0.1.2 (unreleased) +IMPROVEMENTS: + +* vmware: Delete any VMware files in the VM that aren't necessary for + it to function. + BUG FIXES: * amazon-ebs: Sleep between checking instance state to avoid diff --git a/builder/vmware/builder.go b/builder/vmware/builder.go index 9dfbc2da7..e49609ab0 100644 --- a/builder/vmware/builder.go +++ b/builder/vmware/builder.go @@ -224,6 +224,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe &stepWaitForSSH{}, &stepProvision{}, &stepShutdown{}, + &stepCleanFiles{}, } // Setup the state bag @@ -263,11 +264,15 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe // Compile the artifact list files := make([]string, 0, 10) visit := func(path string, info os.FileInfo, err error) error { + if err != nil { + return err + } + if !info.IsDir() { files = append(files, path) } - return err + return nil } if err := filepath.Walk(b.config.OutputDir, visit); err != nil { diff --git a/builder/vmware/step_clean_files.go b/builder/vmware/step_clean_files.go new file mode 100644 index 000000000..ba6f4b04e --- /dev/null +++ b/builder/vmware/step_clean_files.go @@ -0,0 +1,65 @@ +package vmware + +import ( + "fmt" + "github.com/mitchellh/multistep" + "github.com/mitchellh/packer/packer" + "os" + "path/filepath" +) + +// These are the extensions of files that are important for the function +// of a VMware virtual machine. Any other file is discarded as part of the +// build. +var KeepFileExtensions = []string{".nvram", ".vmdk", ".vmsd", ".vmx", ".vmxf"} + +// This step removes unnecessary files from the final result. +// +// Uses: +// config *config +// ui packer.Ui +// +// Produces: +// +type stepCleanFiles struct{} + +func (stepCleanFiles) Run(state map[string]interface{}) multistep.StepAction { + config := state["config"].(*config) + ui := state["ui"].(packer.Ui) + + ui.Say("Deleting unnecessary VMware files...") + visit := func(path string, info os.FileInfo, err error) error { + if err != nil { + return err + } + + if !info.IsDir() { + // If the file isn't critical to the function of the + // virtual machine, we get rid of it. + keep := false + ext := filepath.Ext(path) + for _, goodExt := range KeepFileExtensions { + if goodExt == ext { + keep = true + break + } + } + + if !keep { + ui.Message(fmt.Sprintf("Deleting: %s", path)) + return os.Remove(path) + } + } + + return nil + } + + if err := filepath.Walk(config.OutputDir, visit); err != nil { + state["error"] = err + return multistep.ActionHalt + } + + return multistep.ActionContinue +} + +func (stepCleanFiles) Cleanup(map[string]interface{}) {}