packer-cn/builder/qemu/step_resize_disk.go
Richard Turc b4ff0ea4bc
[builder/qemu] Skip resize step when skip_resize_disk is enable #9860 (#9896)
* [builder/qemu] Skip resize step when skip_resize_disk is enable #9860

* Update builder/qemu/builder_test.go

Improve the code quality

Co-authored-by: Wilken Rivera <dev@wilkenrivera.com>

* Update files for unit tests

Co-authored-by: Wilken Rivera <dev@wilkenrivera.com>
2020-09-08 14:32:08 -04:00

45 lines
1.0 KiB
Go

package qemu
import (
"context"
"fmt"
"path/filepath"
"github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer"
)
// This step resizes the virtual disk that will be used as the
// hard drive for the virtual machine.
type stepResizeDisk struct{}
func (s *stepResizeDisk) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
config := state.Get("config").(*Config)
driver := state.Get("driver").(Driver)
ui := state.Get("ui").(packer.Ui)
path := filepath.Join(config.OutputDir, config.VMName)
command := []string{
"resize",
"-f", config.Format,
path,
config.DiskSize,
}
if config.DiskImage == false || config.SkipResizeDisk == true {
return multistep.ActionContinue
}
ui.Say("Resizing hard drive...")
if err := driver.QemuImg(command...); err != nil {
err := fmt.Errorf("Error creating hard drive: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
return multistep.ActionContinue
}
func (s *stepResizeDisk) Cleanup(state multistep.StateBag) {}