github.phpd.cn/hashicorp/packer@v1.3.2/builder/hyperv/common/step_compact_disk.go (about)

     1  package common
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	"github.com/hashicorp/packer/helper/multistep"
     8  	"github.com/hashicorp/packer/packer"
     9  )
    10  
    11  type StepCompactDisk struct {
    12  	SkipCompaction bool
    13  }
    14  
    15  // Run runs a compaction/optimisation process on attached VHD/VHDX disks
    16  func (s *StepCompactDisk) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
    17  	driver := state.Get("driver").(Driver)
    18  	ui := state.Get("ui").(packer.Ui)
    19  
    20  	if s.SkipCompaction {
    21  		ui.Say("Skipping disk compaction...")
    22  		return multistep.ActionContinue
    23  	}
    24  
    25  	// Get the dir used to store the VMs files during the build process
    26  	var buildDir string
    27  	if v, ok := state.GetOk("build_dir"); ok {
    28  		buildDir = v.(string)
    29  	}
    30  
    31  	ui.Say("Compacting disks...")
    32  	// CompactDisks searches for all VHD/VHDX files under the supplied
    33  	// path and runs the compacting process on each of them. If no disks
    34  	// are found under the supplied path this is treated as a 'soft' error
    35  	// and a warning message is printed. All other errors halt the build.
    36  	result, err := driver.CompactDisks(buildDir)
    37  	if err != nil {
    38  		err := fmt.Errorf("Error compacting disks: %s", err)
    39  		state.Put("error", err)
    40  		ui.Error(err.Error())
    41  		return multistep.ActionHalt
    42  	}
    43  	// Report disk compaction results/warn if no disks were found
    44  	ui.Message(result)
    45  
    46  	return multistep.ActionContinue
    47  }
    48  
    49  // Cleanup does nothing
    50  func (s *StepCompactDisk) Cleanup(state multistep.StateBag) {}