github.com/raghuse92/packer@v1.3.2/builder/qemu/step_copy_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 copies the virtual disk that will be used as the
    13  // hard drive for the virtual machine.
    14  type stepCopyDisk struct{}
    15  
    16  func (s *stepCopyDisk) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
    17  	config := state.Get("config").(*Config)
    18  	driver := state.Get("driver").(Driver)
    19  	isoPath := state.Get("iso_path").(string)
    20  	ui := state.Get("ui").(packer.Ui)
    21  	path := filepath.Join(config.OutputDir, fmt.Sprintf("%s", config.VMName))
    22  	name := config.VMName
    23  
    24  	command := []string{
    25  		"convert",
    26  		"-O", config.Format,
    27  		isoPath,
    28  		path,
    29  	}
    30  
    31  	if !config.DiskImage || config.UseBackingFile {
    32  		return multistep.ActionContinue
    33  	}
    34  
    35  	ui.Say("Copying hard drive...")
    36  	if err := driver.QemuImg(command...); err != nil {
    37  		err := fmt.Errorf("Error creating hard drive: %s", err)
    38  		state.Put("error", err)
    39  		ui.Error(err.Error())
    40  		return multistep.ActionHalt
    41  	}
    42  
    43  	state.Put("disk_filename", name)
    44  
    45  	return multistep.ActionContinue
    46  }
    47  
    48  func (s *stepCopyDisk) Cleanup(state multistep.StateBag) {}