github.com/rahart/packer@v0.12.2-0.20161229105310-282bb6ad370f/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 } 35 36 if config.DiskCompression { 37 command = append(command, "-c") 38 } 39 40 command = append(command, []string{ 41 "-f", config.Format, 42 "-O", config.Format, 43 sourcePath, 44 targetPath, 45 }..., 46 ) 47 48 ui.Say("Converting hard drive...") 49 if err := driver.QemuImg(command...); err != nil { 50 err := fmt.Errorf("Error converting hard drive: %s", err) 51 state.Put("error", err) 52 ui.Error(err.Error()) 53 return multistep.ActionHalt 54 } 55 56 if err := os.Rename(targetPath, sourcePath); err != nil { 57 err := fmt.Errorf("Error moving converted hard drive: %s", err) 58 state.Put("error", err) 59 ui.Error(err.Error()) 60 return multistep.ActionHalt 61 } 62 63 return multistep.ActionContinue 64 } 65 66 func (s *stepConvertDisk) Cleanup(state multistep.StateBag) {}