First try at a compaction step.

This commit is contained in:
Steven Merrill 2013-07-01 22:25:33 -04:00
parent c9d0de593e
commit 3b49f7bd20
3 changed files with 55 additions and 0 deletions

View File

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

View File

@ -12,6 +12,9 @@ import (
// A driver is able to talk to VMware, control virtual machines, etc. // A driver is able to talk to VMware, control virtual machines, etc.
type Driver interface { type Driver interface {
// CompactDisk compacts a virtual disk.
CompactDisk(string) error
// CreateDisk creates a virtual disk with the given size. // CreateDisk creates a virtual disk with the given size.
CreateDisk(string, string) error CreateDisk(string, string) error
@ -40,6 +43,20 @@ type Fusion5Driver struct {
AppPath string 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 { func (d *Fusion5Driver) CreateDisk(output string, size string) error {
cmd := exec.Command(d.vdiskManagerPath(), "-c", "-s", size, "-a", "lsilogic", "-t", "1", output) cmd := exec.Command(d.vdiskManagerPath(), "-c", "-s", size, "-a", "lsilogic", "-t", "1", output)
if _, _, err := d.runAndLog(cmd); err != nil { if _, _, err := d.runAndLog(cmd); err != nil {

View File

@ -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:
// <nothing>
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{}) {}