diff --git a/builder/vmware/builder.go b/builder/vmware/builder.go index e064d5d4a..dab0880b6 100644 --- a/builder/vmware/builder.go +++ b/builder/vmware/builder.go @@ -243,6 +243,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe &stepProvision{}, &stepShutdown{}, &stepCleanFiles{}, + &stepCompactDisk{}, } // Setup the state bag diff --git a/builder/vmware/driver.go b/builder/vmware/driver.go index d363aa001..94a3551e7 100644 --- a/builder/vmware/driver.go +++ b/builder/vmware/driver.go @@ -12,6 +12,9 @@ import ( // A driver is able to talk to VMware, control virtual machines, etc. type Driver interface { + // CompactDisk compacts a virtual disk. + CompactDisk(string) error + // CreateDisk creates a virtual disk with the given size. CreateDisk(string, string) error @@ -40,6 +43,20 @@ type Fusion5Driver struct { AppPath string } +func (d *Fusion5Driver) CompactDisk(diskPath string) error { + cmd := exec.Command(d.vdiskManagerPath(), "-d", diskPath) + if _, _, err := d.runAndLog(cmd); err != nil { + return err + } + + cmd := exec.Command(d.vdiskManagerPath(), "-k", diskPath) + if _, _, err := d.runAndLog(cmd); err != nil { + return err + } + + return nil +} + func (d *Fusion5Driver) CreateDisk(output string, size string) error { cmd := exec.Command(d.vdiskManagerPath(), "-c", "-s", size, "-a", "lsilogic", "-t", "1", output) if _, _, err := d.runAndLog(cmd); err != nil { diff --git a/builder/vmware/step_compact_disk.go b/builder/vmware/step_compact_disk.go new file mode 100644 index 000000000..c4f67fa77 --- /dev/null +++ b/builder/vmware/step_compact_disk.go @@ -0,0 +1,37 @@ +package vmware + +import ( + "fmt" + "github.com/mitchellh/multistep" + "github.com/mitchellh/packer/packer" + "path/filepath" +) + +// This step compacts the virtual disks for the VM. +// +// Uses: +// config *config +// driver Driver +// ui packer.Ui +// +// Produces: +// +type stepCompactDisk struct{} + +func (stepCompactDisk) Run(state map[string]interface{}) multistep.StepAction { + config := state["config"].(*config) + driver := state["driver"].(Driver) + ui := state["ui"].(packer.Ui) + + ui.Say("Compacting the disk image") + if err := driver.CompactDisk(config.FullDiskPath, fmt.Sprintf("%dM", config.DiskSize)); err != nil { + err := fmt.Errorf("Error compacting disk: %s", err) + state["error"] = err + ui.Error(err.Error()) + return multistep.ActionHalt + } + + return multistep.ActionContinue +} + +func (stepCompactDisk) Cleanup(map[string]interface{}) {}