Merge pull request #107 from smerrill/compact-disk

builder/vmware: Compact disk at end of the run
This commit is contained in:
Mitchell Hashimoto 2013-07-02 09:09:15 -07:00
commit c23cd4310b
4 changed files with 56 additions and 2 deletions

View File

@ -239,6 +239,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
&stepProvision{},
&stepShutdown{},
&stepCleanFiles{},
&stepCompactDisk{},
}
// Setup the state bag

View File

@ -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 {
defragCmd := exec.Command(d.vdiskManagerPath(), "-d", diskPath)
if _, _, err := d.runAndLog(defragCmd); err != nil {
return err
}
shrinkCmd := exec.Command(d.vdiskManagerPath(), "-k", diskPath)
if _, _, err := d.runAndLog(shrinkCmd); 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 {

View File

@ -0,0 +1,35 @@
package vmware
import (
"fmt"
"github.com/mitchellh/multistep"
"github.com/mitchellh/packer/packer"
)
// This step compacts the virtual disk for the VM.
//
// Uses:
// driver Driver
// ui packer.Ui
//
// Produces:
// <nothing>
type stepCompactDisk struct{}
func (stepCompactDisk) Run(state map[string]interface{}) multistep.StepAction {
driver := state["driver"].(Driver)
ui := state["ui"].(packer.Ui)
full_disk_path := state["full_disk_path"].(string)
ui.Say("Compacting the disk image")
if err := driver.CompactDisk(full_disk_path); 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{}) {}

View File

@ -24,8 +24,9 @@ func (stepCreateDisk) Run(state map[string]interface{}) multistep.StepAction {
ui := state["ui"].(packer.Ui)
ui.Say("Creating virtual machine disk")
output := filepath.Join(config.OutputDir, config.DiskName+".vmdk")
if err := driver.CreateDisk(output, fmt.Sprintf("%dM", config.DiskSize)); err != nil {
full_disk_path := filepath.Join(config.OutputDir, config.DiskName+".vmdk")
state["full_disk_path"] = full_disk_path
if err := driver.CreateDisk(full_disk_path, fmt.Sprintf("%dM", config.DiskSize)); err != nil {
err := fmt.Errorf("Error creating disk: %s", err)
state["error"] = err
ui.Error(err.Error())