2015-06-21 06:35:52 -04:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-12-11 14:13:37 -05:00
|
|
|
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2015-06-21 06:35:52 -04:00
|
|
|
"github.com/mitchellh/multistep"
|
|
|
|
)
|
|
|
|
|
2016-12-11 17:37:41 -05:00
|
|
|
// StepCompactDisk is a step that removes all empty blocks from expanding
|
|
|
|
// Parallels virtual disks and reduces the result disk size
|
2015-06-21 06:35:52 -04:00
|
|
|
//
|
|
|
|
// Uses:
|
|
|
|
// driver Driver
|
|
|
|
// vmName string
|
|
|
|
// ui packer.Ui
|
|
|
|
//
|
|
|
|
// Produces:
|
|
|
|
// <nothing>
|
|
|
|
type StepCompactDisk struct {
|
|
|
|
Skip bool
|
|
|
|
}
|
|
|
|
|
2016-12-16 14:51:55 -05:00
|
|
|
// Run runs the compaction of the virtual disk attached to the VM.
|
2015-06-21 06:35:52 -04:00
|
|
|
func (s *StepCompactDisk) Run(state multistep.StateBag) multistep.StepAction {
|
|
|
|
driver := state.Get("driver").(Driver)
|
|
|
|
vmName := state.Get("vmName").(string)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
|
|
|
|
if s.Skip {
|
|
|
|
ui.Say("Skipping disk compaction step...")
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
ui.Say("Compacting the disk image")
|
|
|
|
diskPath, err := driver.DiskPath(vmName)
|
|
|
|
if err != nil {
|
2016-12-11 16:12:55 -05:00
|
|
|
err = fmt.Errorf("Error detecting virtual disk path: %s", err)
|
2015-06-21 06:35:52 -04:00
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := driver.CompactDisk(diskPath); err != nil {
|
|
|
|
state.Put("error", fmt.Errorf("Error compacting disk: %s", err))
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2016-12-16 14:51:55 -05:00
|
|
|
// Cleanup does nothing.
|
2015-06-21 06:35:52 -04:00
|
|
|
func (*StepCompactDisk) Cleanup(multistep.StateBag) {}
|