github.com/rothwerx/packer@v0.9.0/builder/qemu/step_convert_disk.go (about) 1 package qemu 2 3 import ( 4 "fmt" 5 "path/filepath" 6 7 "github.com/mitchellh/multistep" 8 "github.com/mitchellh/packer/packer" 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 "-q", 35 } 36 37 if config.DiskCompression { 38 command = append(command, "-c") 39 } 40 41 command = append(command, []string{ 42 "-f", config.Format, 43 "-O", config.Format, 44 sourcePath, 45 targetPath, 46 }..., 47 ) 48 49 ui.Say("Converting hard drive...") 50 if err := driver.QemuImg(command...); err != nil { 51 err := fmt.Errorf("Error converting hard drive: %s", err) 52 state.Put("error", err) 53 ui.Error(err.Error()) 54 return multistep.ActionHalt 55 } 56 57 if err := os.Rename(targetPath, sourcePath); err != nil { 58 err := fmt.Errorf("Error moving converted hard drive: %s", err) 59 state.Put("error", err) 60 ui.Error(err.Error()) 61 return multistep.ActionHalt 62 } 63 64 return multistep.ActionContinue 65 } 66 67 func (s *stepConvertDisk) Cleanup(state multistep.StateBag) {}