51 lines
1.4 KiB
Go
51 lines
1.4 KiB
Go
package common
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
|
"github.com/hashicorp/packer/packer"
|
|
)
|
|
|
|
type StepCompactDisk struct {
|
|
SkipCompaction bool
|
|
}
|
|
|
|
// Run runs a compaction/optimisation process on attached VHD/VHDX disks
|
|
func (s *StepCompactDisk) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
|
|
driver := state.Get("driver").(Driver)
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
if s.SkipCompaction {
|
|
ui.Say("Skipping disk compaction...")
|
|
return multistep.ActionContinue
|
|
}
|
|
|
|
// Get the tmp dir used to store the VMs files during the build process
|
|
var tmpPath string
|
|
if v, ok := state.GetOk("packerTempDir"); ok {
|
|
tmpPath = v.(string)
|
|
}
|
|
|
|
ui.Say("Compacting disks...")
|
|
// CompactDisks searches for all VHD/VHDX files under the supplied
|
|
// path and runs the compacting process on each of them. If no disks
|
|
// are found under the supplied path this is treated as a 'soft' error
|
|
// and a warning message is printed. All other errors halt the build.
|
|
result, err := driver.CompactDisks(tmpPath)
|
|
if err != nil {
|
|
err := fmt.Errorf("Error compacting disks: %s", err)
|
|
state.Put("error", err)
|
|
ui.Error(err.Error())
|
|
return multistep.ActionHalt
|
|
}
|
|
// Report disk compaction results/warn if no disks were found
|
|
ui.Message(result)
|
|
|
|
return multistep.ActionContinue
|
|
}
|
|
|
|
// Cleanup does nothing
|
|
func (s *StepCompactDisk) Cleanup(state multistep.StateBag) {}
|