2014-07-17 10:01:19 -04:00
|
|
|
package qemu
|
|
|
|
|
|
|
|
import (
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
2014-07-17 10:01:19 -04:00
|
|
|
"fmt"
|
2015-05-17 10:35:39 -04:00
|
|
|
"path/filepath"
|
|
|
|
|
2020-12-17 16:29:25 -05:00
|
|
|
"github.com/hashicorp/packer-plugin-sdk/multistep"
|
|
|
|
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
|
2014-07-17 10:01:19 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// This step resizes the virtual disk that will be used as the
|
|
|
|
// hard drive for the virtual machine.
|
2020-09-16 16:40:23 -04:00
|
|
|
type stepResizeDisk struct {
|
|
|
|
DiskCompression bool
|
|
|
|
DiskImage bool
|
|
|
|
Format string
|
|
|
|
OutputDir string
|
|
|
|
SkipResizeDisk bool
|
|
|
|
VMName string
|
|
|
|
DiskSize string
|
|
|
|
|
|
|
|
QemuImgArgs QemuImgArgs
|
|
|
|
}
|
2014-07-17 10:01:19 -04:00
|
|
|
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *stepResizeDisk) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2014-07-17 10:01:19 -04:00
|
|
|
driver := state.Get("driver").(Driver)
|
2020-11-19 14:54:31 -05:00
|
|
|
ui := state.Get("ui").(packersdk.Ui)
|
2020-09-16 16:40:23 -04:00
|
|
|
path := filepath.Join(s.OutputDir, s.VMName)
|
2014-07-17 10:01:19 -04:00
|
|
|
|
2020-09-16 16:40:23 -04:00
|
|
|
command := s.buildResizeCommand(path)
|
2020-09-08 14:32:08 -04:00
|
|
|
|
2020-09-16 16:40:23 -04:00
|
|
|
if s.DiskImage == false || s.SkipResizeDisk == true {
|
2014-10-28 11:43:19 -04:00
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
2014-07-17 10:01:19 -04:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-09-16 16:40:23 -04:00
|
|
|
func (s *stepResizeDisk) buildResizeCommand(path string) []string {
|
|
|
|
command := []string{"resize", "-f", s.Format}
|
|
|
|
|
|
|
|
// add user-provided convert args
|
|
|
|
command = append(command, s.QemuImgArgs.Resize...)
|
|
|
|
|
|
|
|
// Add file and size
|
|
|
|
command = append(command, path, s.DiskSize)
|
|
|
|
|
|
|
|
return command
|
|
|
|
}
|
|
|
|
|
2014-07-17 10:01:19 -04:00
|
|
|
func (s *stepResizeDisk) Cleanup(state multistep.StateBag) {}
|