github.com/mmcquillan/packer@v1.1.1-0.20171009221028-c85cf0483a5d/builder/qemu/step_convert_disk.go (about) 1 package qemu 2 3 import ( 4 "fmt" 5 "path/filepath" 6 7 "github.com/hashicorp/packer/packer" 8 "github.com/mitchellh/multistep" 9 10 "os" 11 ) 12 13 // This step converts the virtual disk that was used as the 14 // hard drive for the virtual machine. 15 type stepConvertDisk struct{} 16 17 func (s *stepConvertDisk) Run(state multistep.StateBag) multistep.StepAction { 18 config := state.Get("config").(*Config) 19 driver := state.Get("driver").(Driver) 20 diskName := state.Get("disk_filename").(string) 21 ui := state.Get("ui").(packer.Ui) 22 23 if config.SkipCompaction && !config.DiskCompression { 24 return multistep.ActionContinue 25 } 26 27 name := diskName + ".convert" 28 29 sourcePath := filepath.Join(config.OutputDir, diskName) 30 targetPath := filepath.Join(config.OutputDir, name) 31 32 command := []string{ 33 "convert", 34 } 35 36 if config.DiskCompression { 37 command = append(command, "-c") 38 } 39 40 command = append(command, []string{ 41 "-O", config.Format, 42 sourcePath, 43 targetPath, 44 }..., 45 ) 46 47 ui.Say("Converting hard drive...") 48 if err := driver.QemuImg(command...); err != nil { 49 err := fmt.Errorf("Error converting hard drive: %s", err) 50 state.Put("error", err) 51 ui.Error(err.Error()) 52 return multistep.ActionHalt 53 } 54 55 if err := os.Rename(targetPath, sourcePath); err != nil { 56 err := fmt.Errorf("Error moving converted hard drive: %s", err) 57 state.Put("error", err) 58 ui.Error(err.Error()) 59 return multistep.ActionHalt 60 } 61 62 return multistep.ActionContinue 63 } 64 65 func (s *stepConvertDisk) Cleanup(state multistep.StateBag) {}