2015-06-21 06:35:52 -04:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
2015-06-21 06:35:52 -04:00
|
|
|
"fmt"
|
2016-12-11 14:13:37 -05:00
|
|
|
|
2020-12-17 16:29:25 -05:00
|
|
|
"github.com/hashicorp/packer-plugin-sdk/multistep"
|
|
|
|
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
|
2015-06-21 06:35:52 -04:00
|
|
|
)
|
|
|
|
|
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
|
2020-11-19 14:54:31 -05:00
|
|
|
// ui packersdk.Ui
|
2015-06-21 06:35:52 -04:00
|
|
|
//
|
|
|
|
// 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.
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *StepCompactDisk) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2015-06-21 06:35:52 -04:00
|
|
|
driver := state.Get("driver").(Driver)
|
|
|
|
vmName := state.Get("vmName").(string)
|
2020-11-19 14:54:31 -05:00
|
|
|
ui := state.Get("ui").(packersdk.Ui)
|
2015-06-21 06:35:52 -04:00
|
|
|
|
|
|
|
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) {}
|