From 4de76ccd3f093d9437985f75ebeb86dc476deceb Mon Sep 17 00:00:00 2001 From: Steven Merrill Date: Mon, 1 Jul 2013 22:22:00 -0400 Subject: [PATCH 1/6] Add a computed FullDiskPath config option. --- builder/vmware/builder.go | 4 ++++ builder/vmware/step_create_disk.go | 3 +-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/builder/vmware/builder.go b/builder/vmware/builder.go index d8a8f27b6..e064d5d4a 100644 --- a/builder/vmware/builder.go +++ b/builder/vmware/builder.go @@ -52,6 +52,7 @@ type config struct { PackerBuildName string `mapstructure:"packer_build_name"` PackerDebug bool `mapstructure:"packer_debug"` + FullDiskPath string `` RawBootWait string `mapstructure:"boot_wait"` RawShutdownTimeout string `mapstructure:"shutdown_timeout"` @@ -114,6 +115,9 @@ func (b *Builder) Prepare(raws ...interface{}) error { b.config.ToolsUploadPath = "{{ .Flavor }}.iso" } + // Store the full path to the disk file. + b.config.FullDiskPath = filepath.Join(b.config.OutputDir, b.config.DiskName+".vmdk") + // Accumulate any errors var err error errs := make([]error, 0) diff --git a/builder/vmware/step_create_disk.go b/builder/vmware/step_create_disk.go index 7624224d0..5997bf998 100644 --- a/builder/vmware/step_create_disk.go +++ b/builder/vmware/step_create_disk.go @@ -24,8 +24,7 @@ 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 { + if err := driver.CreateDisk(config.FullDiskPath, fmt.Sprintf("%dM", config.DiskSize)); err != nil { err := fmt.Errorf("Error creating disk: %s", err) state["error"] = err ui.Error(err.Error()) From d4cd9352d6a629db2876f7fc62c4b89e41bb55a4 Mon Sep 17 00:00:00 2001 From: Steven Merrill Date: Mon, 1 Jul 2013 22:25:33 -0400 Subject: [PATCH 2/6] First try at a compaction step. --- builder/vmware/builder.go | 1 + builder/vmware/driver.go | 17 +++++++++++++ builder/vmware/step_compact_disk.go | 37 +++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+) create mode 100644 builder/vmware/step_compact_disk.go 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{}) {} From 29fa6219075e5b046bf0a985c78d88210fbf8d1e Mon Sep 17 00:00:00 2001 From: Steven Merrill Date: Mon, 1 Jul 2013 22:45:46 -0400 Subject: [PATCH 3/6] Finish initial compaction feature. --- builder/vmware/driver.go | 8 ++++---- builder/vmware/step_compact_disk.go | 3 +-- builder/vmware/step_create_disk.go | 1 - 3 files changed, 5 insertions(+), 7 deletions(-) diff --git a/builder/vmware/driver.go b/builder/vmware/driver.go index 94a3551e7..2acf20adf 100644 --- a/builder/vmware/driver.go +++ b/builder/vmware/driver.go @@ -44,13 +44,13 @@ type Fusion5Driver struct { } func (d *Fusion5Driver) CompactDisk(diskPath string) error { - cmd := exec.Command(d.vdiskManagerPath(), "-d", diskPath) - if _, _, err := d.runAndLog(cmd); err != nil { + defragCmd := exec.Command(d.vdiskManagerPath(), "-d", diskPath) + if _, _, err := d.runAndLog(defragCmd); err != nil { return err } - cmd := exec.Command(d.vdiskManagerPath(), "-k", diskPath) - if _, _, err := d.runAndLog(cmd); err != nil { + shrinkCmd := exec.Command(d.vdiskManagerPath(), "-k", diskPath) + if _, _, err := d.runAndLog(shrinkCmd); err != nil { return err } diff --git a/builder/vmware/step_compact_disk.go b/builder/vmware/step_compact_disk.go index c4f67fa77..18034bb87 100644 --- a/builder/vmware/step_compact_disk.go +++ b/builder/vmware/step_compact_disk.go @@ -4,7 +4,6 @@ import ( "fmt" "github.com/mitchellh/multistep" "github.com/mitchellh/packer/packer" - "path/filepath" ) // This step compacts the virtual disks for the VM. @@ -24,7 +23,7 @@ func (stepCompactDisk) Run(state map[string]interface{}) multistep.StepAction { ui := state["ui"].(packer.Ui) ui.Say("Compacting the disk image") - if err := driver.CompactDisk(config.FullDiskPath, fmt.Sprintf("%dM", config.DiskSize)); err != nil { + if err := driver.CompactDisk(config.FullDiskPath); err != nil { err := fmt.Errorf("Error compacting disk: %s", err) state["error"] = err ui.Error(err.Error()) diff --git a/builder/vmware/step_create_disk.go b/builder/vmware/step_create_disk.go index 5997bf998..7a6ce21b2 100644 --- a/builder/vmware/step_create_disk.go +++ b/builder/vmware/step_create_disk.go @@ -4,7 +4,6 @@ import ( "fmt" "github.com/mitchellh/multistep" "github.com/mitchellh/packer/packer" - "path/filepath" ) // This step creates the virtual disks for the VM. From 359ba01c6a1c361fdac5225647f443bfe1bd301d Mon Sep 17 00:00:00 2001 From: Steven Merrill Date: Tue, 2 Jul 2013 01:12:57 -0400 Subject: [PATCH 4/6] Integrate code review comments. --- builder/vmware/builder.go | 4 ---- builder/vmware/step_compact_disk.go | 7 ++++--- builder/vmware/step_create_disk.go | 5 ++++- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/builder/vmware/builder.go b/builder/vmware/builder.go index dab0880b6..413619f1e 100644 --- a/builder/vmware/builder.go +++ b/builder/vmware/builder.go @@ -52,7 +52,6 @@ type config struct { PackerBuildName string `mapstructure:"packer_build_name"` PackerDebug bool `mapstructure:"packer_debug"` - FullDiskPath string `` RawBootWait string `mapstructure:"boot_wait"` RawShutdownTimeout string `mapstructure:"shutdown_timeout"` @@ -115,9 +114,6 @@ func (b *Builder) Prepare(raws ...interface{}) error { b.config.ToolsUploadPath = "{{ .Flavor }}.iso" } - // Store the full path to the disk file. - b.config.FullDiskPath = filepath.Join(b.config.OutputDir, b.config.DiskName+".vmdk") - // Accumulate any errors var err error errs := make([]error, 0) diff --git a/builder/vmware/step_compact_disk.go b/builder/vmware/step_compact_disk.go index 18034bb87..34da410b8 100644 --- a/builder/vmware/step_compact_disk.go +++ b/builder/vmware/step_compact_disk.go @@ -6,7 +6,8 @@ import ( "github.com/mitchellh/packer/packer" ) -// This step compacts the virtual disks for the VM. +// This step compacts the virtual disk for the VM. If "compact_disk" is not +// true, it will immediately return. // // Uses: // config *config @@ -18,12 +19,12 @@ import ( 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) + full_disk_path := state["full_disk_path"].(string) ui.Say("Compacting the disk image") - if err := driver.CompactDisk(config.FullDiskPath); err != nil { + if err := driver.CompactDisk(full_disk_path); err != nil { err := fmt.Errorf("Error compacting disk: %s", err) state["error"] = err ui.Error(err.Error()) diff --git a/builder/vmware/step_create_disk.go b/builder/vmware/step_create_disk.go index 7a6ce21b2..56ae154a8 100644 --- a/builder/vmware/step_create_disk.go +++ b/builder/vmware/step_create_disk.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/mitchellh/multistep" "github.com/mitchellh/packer/packer" + "path/filepath" ) // This step creates the virtual disks for the VM. @@ -23,12 +24,14 @@ func (stepCreateDisk) Run(state map[string]interface{}) multistep.StepAction { ui := state["ui"].(packer.Ui) ui.Say("Creating virtual machine disk") - if err := driver.CreateDisk(config.FullDiskPath, fmt.Sprintf("%dM", config.DiskSize)); err != nil { + full_disk_path := filepath.Join(config.OutputDir, config.DiskName+".vmdk") + 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()) return multistep.ActionHalt } + state["full_disk_path"] = full_disk_path return multistep.ActionContinue } From 586a9a7c32c444562c5079fbfde002a4e2eaf69c Mon Sep 17 00:00:00 2001 From: Steven Merrill Date: Tue, 2 Jul 2013 01:15:03 -0400 Subject: [PATCH 5/6] Small doc fixes. --- builder/vmware/step_compact_disk.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/builder/vmware/step_compact_disk.go b/builder/vmware/step_compact_disk.go index 34da410b8..1f0f49c76 100644 --- a/builder/vmware/step_compact_disk.go +++ b/builder/vmware/step_compact_disk.go @@ -6,11 +6,9 @@ import ( "github.com/mitchellh/packer/packer" ) -// This step compacts the virtual disk for the VM. If "compact_disk" is not -// true, it will immediately return. +// This step compacts the virtual disk for the VM. // // Uses: -// config *config // driver Driver // ui packer.Ui // From 432cab3fcf29fd28a4edaa18ce7b402ba6393e64 Mon Sep 17 00:00:00 2001 From: Steven Merrill Date: Tue, 2 Jul 2013 01:32:50 -0400 Subject: [PATCH 6/6] Small fix to be sure "full_disk_path" is put into state. --- builder/vmware/step_create_disk.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/builder/vmware/step_create_disk.go b/builder/vmware/step_create_disk.go index 56ae154a8..e3fdc9272 100644 --- a/builder/vmware/step_create_disk.go +++ b/builder/vmware/step_create_disk.go @@ -25,13 +25,13 @@ func (stepCreateDisk) Run(state map[string]interface{}) multistep.StepAction { ui.Say("Creating virtual machine disk") 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()) return multistep.ActionHalt } - state["full_disk_path"] = full_disk_path return multistep.ActionContinue }