github.com/rothwerx/packer@v0.9.0/builder/qemu/step_run.go (about) 1 package qemu 2 3 import ( 4 "fmt" 5 "log" 6 "path/filepath" 7 "strconv" 8 "strings" 9 10 "github.com/mitchellh/multistep" 11 "github.com/mitchellh/packer/packer" 12 "github.com/mitchellh/packer/template/interpolate" 13 ) 14 15 // stepRun runs the virtual machine 16 type stepRun struct { 17 BootDrive string 18 Message string 19 } 20 21 type qemuArgsTemplateData struct { 22 HTTPIP string 23 HTTPPort uint 24 HTTPDir string 25 OutputDir string 26 Name string 27 SSHHostPort uint 28 } 29 30 func (s *stepRun) Run(state multistep.StateBag) multistep.StepAction { 31 driver := state.Get("driver").(Driver) 32 ui := state.Get("ui").(packer.Ui) 33 34 ui.Say(s.Message) 35 36 command, err := getCommandArgs(s.BootDrive, state) 37 if err != nil { 38 err := fmt.Errorf("Error processing QemuArggs: %s", err) 39 ui.Error(err.Error()) 40 return multistep.ActionHalt 41 } 42 43 if err := driver.Qemu(command...); err != nil { 44 err := fmt.Errorf("Error launching VM: %s", err) 45 ui.Error(err.Error()) 46 return multistep.ActionHalt 47 } 48 49 return multistep.ActionContinue 50 } 51 52 func (s *stepRun) Cleanup(state multistep.StateBag) { 53 driver := state.Get("driver").(Driver) 54 ui := state.Get("ui").(packer.Ui) 55 56 if err := driver.Stop(); err != nil { 57 ui.Error(fmt.Sprintf("Error shutting down VM: %s", err)) 58 } 59 } 60 61 func getCommandArgs(bootDrive string, state multistep.StateBag) ([]string, error) { 62 config := state.Get("config").(*Config) 63 isoPath := state.Get("iso_path").(string) 64 vncPort := state.Get("vnc_port").(uint) 65 sshHostPort := state.Get("sshHostPort").(uint) 66 ui := state.Get("ui").(packer.Ui) 67 driver := state.Get("driver").(Driver) 68 69 vnc := fmt.Sprintf("0.0.0.0:%d", vncPort-5900) 70 vmName := config.VMName 71 imgPath := filepath.Join(config.OutputDir, vmName) 72 73 defaultArgs := make(map[string]interface{}) 74 var deviceArgs []string 75 var driveArgs []string 76 77 defaultArgs["-name"] = vmName 78 defaultArgs["-machine"] = fmt.Sprintf("type=%s", config.MachineType) 79 defaultArgs["-netdev"] = fmt.Sprintf("user,id=user.0,hostfwd=tcp::%v-:%d", sshHostPort, config.Comm.Port()) 80 81 qemuVersion, err := driver.Version() 82 if err != nil { 83 return nil, err 84 } 85 parts := strings.Split(qemuVersion, ".") 86 qemuMajor, err := strconv.Atoi(parts[0]) 87 if err != nil { 88 return nil, err 89 } 90 if qemuMajor >= 2 { 91 if config.DiskInterface == "virtio-scsi" { 92 deviceArgs = append(deviceArgs, "virtio-scsi-pci,id=scsi0", "scsi-hd,bus=scsi0.0,drive=drive0") 93 driveArgs = append(driveArgs, fmt.Sprintf("if=none,file=%s,id=drive0,cache=%s,discard=%s", imgPath, config.DiskCache, config.DiskDiscard)) 94 } else { 95 driveArgs = append(driveArgs, fmt.Sprintf("file=%s,if=%s,cache=%s,discard=%s", imgPath, config.DiskInterface, config.DiskCache, config.DiskDiscard)) 96 } 97 } else { 98 defaultArgs["-drive"] = fmt.Sprintf("file=%s,if=%s,cache=%s", imgPath, config.DiskInterface, config.DiskCache) 99 } 100 deviceArgs = append(deviceArgs, fmt.Sprintf("%s,netdev=user.0", config.NetDevice)) 101 102 if config.Headless == true { 103 ui.Message("WARNING: The VM will be started in headless mode, as configured.\n" + 104 "In headless mode, errors during the boot sequence or OS setup\n" + 105 "won't be easily visible. Use at your own discretion.") 106 } else { 107 if qemuMajor >= 2 { 108 defaultArgs["-display"] = "sdl" 109 } else { 110 ui.Message("WARNING: The version of qemu on your host doesn't support display mode.\n" + 111 "The display parameter will be ignored.") 112 } 113 } 114 115 defaultArgs["-device"] = deviceArgs 116 defaultArgs["-drive"] = driveArgs 117 118 if !config.DiskImage { 119 defaultArgs["-cdrom"] = isoPath 120 } 121 defaultArgs["-boot"] = bootDrive 122 defaultArgs["-m"] = "512M" 123 defaultArgs["-vnc"] = vnc 124 125 // Append the accelerator to the machine type if it is specified 126 if config.Accelerator != "none" { 127 defaultArgs["-machine"] = fmt.Sprintf("%s,accel=%s", defaultArgs["-machine"], config.Accelerator) 128 } else { 129 ui.Message("WARNING: The VM will be started with no hardware acceleration.\n" + 130 "The installation may take considerably longer to finish.\n") 131 } 132 133 // Determine if we have a floppy disk to attach 134 if floppyPathRaw, ok := state.GetOk("floppy_path"); ok { 135 defaultArgs["-fda"] = floppyPathRaw.(string) 136 } else { 137 log.Println("Qemu Builder has no floppy files, not attaching a floppy.") 138 } 139 140 inArgs := make(map[string][]string) 141 if len(config.QemuArgs) > 0 { 142 ui.Say("Overriding defaults Qemu arguments with QemuArgs...") 143 144 httpPort := state.Get("http_port").(uint) 145 ctx := config.ctx 146 ctx.Data = qemuArgsTemplateData{ 147 "10.0.2.2", 148 httpPort, 149 config.HTTPDir, 150 config.OutputDir, 151 config.VMName, 152 sshHostPort, 153 } 154 newQemuArgs, err := processArgs(config.QemuArgs, &ctx) 155 if err != nil { 156 return nil, err 157 } 158 159 // because qemu supports multiple appearances of the same 160 // switch, just different values, each key in the args hash 161 // will have an array of string values 162 for _, qemuArgs := range newQemuArgs { 163 key := qemuArgs[0] 164 val := strings.Join(qemuArgs[1:], "") 165 if _, ok := inArgs[key]; !ok { 166 inArgs[key] = make([]string, 0) 167 } 168 if len(val) > 0 { 169 inArgs[key] = append(inArgs[key], val) 170 } 171 } 172 } 173 174 // get any remaining missing default args from the default settings 175 for key := range defaultArgs { 176 if _, ok := inArgs[key]; !ok { 177 arg := make([]string, 1) 178 switch defaultArgs[key].(type) { 179 case string: 180 arg[0] = defaultArgs[key].(string) 181 case []string: 182 arg = defaultArgs[key].([]string) 183 } 184 inArgs[key] = arg 185 } 186 } 187 188 // Flatten to array of strings 189 outArgs := make([]string, 0) 190 for key, values := range inArgs { 191 if len(values) > 0 { 192 for idx := range values { 193 outArgs = append(outArgs, key, values[idx]) 194 } 195 } else { 196 outArgs = append(outArgs, key) 197 } 198 } 199 200 return outArgs, nil 201 } 202 203 func processArgs(args [][]string, ctx *interpolate.Context) ([][]string, error) { 204 var err error 205 206 if args == nil { 207 return make([][]string, 0), err 208 } 209 210 newArgs := make([][]string, len(args)) 211 for argsIdx, rowArgs := range args { 212 parms := make([]string, len(rowArgs)) 213 newArgs[argsIdx] = parms 214 for i, parm := range rowArgs { 215 parms[i], err = interpolate.Render(parm, ctx) 216 if err != nil { 217 return nil, err 218 } 219 } 220 } 221 222 return newArgs, err 223 }