github.com/raghuse92/packer@v1.3.2/builder/qemu/step_create_disk.go (about)

     1  package qemu
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"path/filepath"
     7  
     8  	"github.com/hashicorp/packer/helper/multistep"
     9  	"github.com/hashicorp/packer/packer"
    10  )
    11  
    12  // This step creates the virtual disk that will be used as the
    13  // hard drive for the virtual machine.
    14  type stepCreateDisk struct{}
    15  
    16  func (s *stepCreateDisk) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
    17  	config := state.Get("config").(*Config)
    18  	driver := state.Get("driver").(Driver)
    19  	ui := state.Get("ui").(packer.Ui)
    20  	name := config.VMName
    21  	path := filepath.Join(config.OutputDir, name)
    22  
    23  	command := []string{
    24  		"create",
    25  		"-f", config.Format,
    26  	}
    27  
    28  	if config.UseBackingFile {
    29  		isoPath := state.Get("iso_path").(string)
    30  		command = append(command, "-b", isoPath)
    31  	}
    32  
    33  	command = append(command,
    34  		path,
    35  		fmt.Sprintf("%vM", config.DiskSize),
    36  	)
    37  
    38  	if config.DiskImage && !config.UseBackingFile {
    39  		return multistep.ActionContinue
    40  	}
    41  
    42  	ui.Say("Creating hard drive...")
    43  	if err := driver.QemuImg(command...); err != nil {
    44  		err := fmt.Errorf("Error creating hard drive: %s", err)
    45  		state.Put("error", err)
    46  		ui.Error(err.Error())
    47  		return multistep.ActionHalt
    48  	}
    49  
    50  	state.Put("disk_filename", name)
    51  
    52  	return multistep.ActionContinue
    53  }
    54  
    55  func (s *stepCreateDisk) Cleanup(state multistep.StateBag) {}