github.com/daniellockard/packer@v0.7.6-0.20141210173435-5a9390934716/builder/qemu/step_copy_disk.go (about)

     1  package qemu
     2  
     3  import (
     4  	"fmt"
     5  	"path/filepath"
     6  	"strings"
     7  
     8  	"github.com/mitchellh/multistep"
     9  	"github.com/mitchellh/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(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.%s", config.VMName,
    22  		strings.ToLower(config.Format)))
    23  	name := config.VMName + "." + strings.ToLower(config.Format)
    24  
    25  	command := []string{
    26  		"convert",
    27  		"-f", config.Format,
    28  		isoPath,
    29  		path,
    30  	}
    31  
    32  	if config.DiskImage == false {
    33  		return multistep.ActionContinue
    34  	}
    35  
    36  	ui.Say("Copying hard drive...")
    37  	if err := driver.QemuImg(command...); err != nil {
    38  		err := fmt.Errorf("Error creating hard drive: %s", err)
    39  		state.Put("error", err)
    40  		ui.Error(err.Error())
    41  		return multistep.ActionHalt
    42  	}
    43  
    44  	state.Put("disk_filename", name)
    45  
    46  	return multistep.ActionContinue
    47  }
    48  
    49  func (s *stepCopyDisk) Cleanup(state multistep.StateBag) {}