github.com/victortrac/packer@v0.7.6-0.20160602180447-63c7fdb6e41f/website/source/docs/builders/qemu.html.md (about) 1 --- 2 description: | 3 The Qemu Packer builder is able to create KVM and Xen virtual machine images. 4 Support for Xen is experimental at this time. 5 layout: docs 6 page_title: QEMU Builder 7 ... 8 9 # QEMU Builder 10 11 Type: `qemu` 12 13 The Qemu Packer builder is able to create [KVM](http://www.linux-kvm.org) and 14 [Xen](http://www.xenproject.org) virtual machine images. Support for Xen is 15 experimental at this time. 16 17 The builder builds a virtual machine by creating a new virtual machine from 18 scratch, booting it, installing an OS, rebooting the machine with the boot media 19 as the virtual hard drive, provisioning software within the OS, then shutting it 20 down. The result of the Qemu builder is a directory containing the image file 21 necessary to run the virtual machine on KVM or Xen. 22 23 ## Basic Example 24 25 Here is a basic example. This example is functional so long as you fixup paths 26 to files, URLS for ISOs and checksums. 27 28 ``` {.javascript} 29 { 30 "builders": 31 [ 32 { 33 "type": "qemu", 34 "iso_url": "http://mirror.raystedman.net/centos/6/isos/x86_64/CentOS-6.5-x86_64-minimal.iso", 35 "iso_checksum": "0d9dc37b5dd4befa1c440d2174e88a87", 36 "iso_checksum_type": "md5", 37 "output_directory": "output_centos_tdhtest", 38 "shutdown_command": "shutdown -P now", 39 "disk_size": 5000, 40 "format": "qcow2", 41 "headless": false, 42 "accelerator": "kvm", 43 "http_directory": "httpdir", 44 "http_port_min": 10082, 45 "http_port_max": 10089, 46 "ssh_host_port_min": 2222, 47 "ssh_host_port_max": 2229, 48 "ssh_username": "root", 49 "ssh_password": "s0m3password", 50 "ssh_port": 22, 51 "ssh_wait_timeout": "30s", 52 "vm_name": "tdhtest", 53 "net_device": "virtio-net", 54 "disk_interface": "virtio", 55 "boot_wait": "5s", 56 "boot_command": 57 [ 58 "<tab> text ks=http://{{ .HTTPIP }}:{{ .HTTPPort }}/centos6-ks.cfg<enter><wait>" 59 ] 60 } 61 ] 62 } 63 ``` 64 65 A working CentOS 6.x kickstart file can be found [at this 66 URL](https://gist.github.com/mitchellh/7328271/#file-centos6-ks-cfg), adapted 67 from an unknown source. Place this file in the http directory with the proper 68 name. For the example above, it should go into "httpdir" with a name of 69 "centos6-ks.cfg". 70 71 ## Configuration Reference 72 73 There are many configuration options available for the Qemu builder. They are 74 organized below into two categories: required and optional. Within each 75 category, the available options are alphabetized and described. 76 77 In addition to the options listed here, a 78 [communicator](/docs/templates/communicator.html) can be configured for this 79 builder. 80 81 Note that you will need to set `"headless": true` if you are running Packer 82 on a Linux server without X11; or if you are connected via ssh to a remote 83 Linux server and have not enabled X11 forwarding (`ssh -X`). 84 85 ### Required: 86 87 - `iso_checksum` (string) - The checksum for the OS ISO file. Because ISO 88 files are so large, this is required and Packer will verify it prior to 89 booting a virtual machine with the ISO attached. The type of the checksum is 90 specified with `iso_checksum_type`, documented below. At least one of 91 `iso_checksum` and `iso_checksum_url` must be defined. This has precedence 92 over `iso_checksum_url` type. 93 94 - `iso_checksum_type` (string) - The type of the checksum specified in 95 `iso_checksum`. Valid values are "none", "md5", "sha1", "sha256", or 96 "sha512" currently. While "none" will skip checksumming, this is not 97 recommended since ISO files are generally large and corruption does happen 98 from time to time. 99 100 - `iso_checksum_url` (string) - A URL to a GNU or BSD style checksum file 101 containing a checksum for the OS ISO file. At least one of `iso_checksum` 102 and `iso_checksum_url` must be defined. This will be ignored if 103 `iso_checksum` is non empty. 104 105 - `iso_url` (string) - A URL to the ISO containing the installation image. 106 This URL can be either an HTTP URL or a file URL (or path to a file). If 107 this is an HTTP URL, Packer will download it and cache it between runs. 108 109 - `ssh_username` (string) - The username to use to SSH into the machine once 110 the OS is installed. 111 112 ### Optional: 113 114 - `accelerator` (string) - The accelerator type to use when running the VM. 115 This may be `none`, `kvm`, `tcg`, or `xen`. The appropriate software must 116 already been installed on your build machine to use the accelerator you 117 specified. When no accelerator is specified, Packer will try to use `kvm` 118 if it is available but will default to `tcg` otherwise. 119 120 - `boot_command` (array of strings) - This is an array of commands to type 121 when the virtual machine is first booted. The goal of these commands should 122 be to type just enough to initialize the operating system installer. Special 123 keys can be typed as well, and are covered in the section below on the 124 boot command. If this is not specified, it is assumed the installer will 125 start itself. 126 127 - `boot_wait` (string) - The time to wait after booting the initial virtual 128 machine before typing the `boot_command`. The value of this should be 129 a duration. Examples are "5s" and "1m30s" which will cause Packer to wait 130 five seconds and one minute 30 seconds, respectively. If this isn't 131 specified, the default is 10 seconds. 132 133 - `disk_cache` (string) - The cache mode to use for disk. Allowed values 134 include any of "writethrough", "writeback", "none", "unsafe" 135 or "directsync". By default, this is set to "writeback". 136 137 - `disk_compression` (boolean) - Apply compression to the QCOW2 disk file 138 using `qemu-img convert`. Defaults to `false`. 139 140 - `disk_discard` (string) - The discard mode to use for disk. Allowed values 141 include any of "unmap" or "ignore". By default, this is set to "ignore". 142 143 - `disk_image` (boolean) - Packer defaults to building from an ISO file, this 144 parameter controls whether the ISO URL supplied is actually a bootable 145 QEMU image. When this value is set to true, the machine will clone the 146 source, resize it according to `disk_size` and boot the image. 147 148 - `disk_interface` (string) - The interface to use for the disk. Allowed 149 values include any of "ide", "scsi", "virtio" or "virtio-scsi". Note also 150 that any boot commands or kickstart type scripts must have proper 151 adjustments for resulting device names. The Qemu builder uses "virtio" by 152 default. 153 154 - `disk_size` (integer) - The size, in megabytes, of the hard disk to create 155 for the VM. By default, this is 40000 (about 40 GB). 156 157 - `floppy_files` (array of strings) - A list of files to place onto a floppy 158 disk that is attached when the VM is booted. This is most useful for 159 unattended Windows installs, which look for an `Autounattend.xml` file on 160 removable media. By default, no floppy will be attached. All files listed in 161 this setting get placed into the root directory of the floppy and the floppy 162 is attached as the first floppy device. Currently, no support exists for 163 creating sub-directories on the floppy. Wildcard characters (\*, ?, 164 and \[\]) are allowed. Directory names are also allowed, which will add all 165 the files found in the directory to the floppy. 166 167 - `format` (string) - Either "qcow2" or "raw", this specifies the output 168 format of the virtual machine image. This defaults to `qcow2`. 169 170 - `headless` (boolean) - Packer defaults to building QEMU virtual machines by 171 launching a GUI that shows the console of the machine being built. When this 172 value is set to true, the machine will start without a console. 173 174 You can still see the console if you make a note of the VNC display 175 number chosen, and then connect using `vncviewer -Shared <host>:<display>` 176 177 - `http_directory` (string) - Path to a directory to serve using an 178 HTTP server. The files in this directory will be available over HTTP that 179 will be requestable from the virtual machine. This is useful for hosting 180 kickstart files and so on. By default this is "", which means no HTTP server 181 will be started. The address and port of the HTTP server will be available 182 as variables in `boot_command`. This is covered in more detail below. 183 184 - `http_port_min` and `http_port_max` (integer) - These are the minimum and 185 maximum port to use for the HTTP server started to serve the 186 `http_directory`. Because Packer often runs in parallel, Packer will choose 187 a randomly available port in this range to run the HTTP server. If you want 188 to force the HTTP server to be on one port, make this minimum and maximum 189 port the same. By default the values are 8000 and 9000, respectively. 190 191 - `iso_skip_cache` (boolean) - Use iso from provided url. Qemu must support 192 curl block device. This defaults to `false`. 193 194 - `iso_target_path` (string) - The path where the iso should be saved after 195 download. By default will go in the packer cache, with a hash of the 196 original filename as its name. 197 198 - `iso_urls` (array of strings) - Multiple URLs for the ISO to download. 199 Packer will try these in order. If anything goes wrong attempting to 200 download or while downloading a single URL, it will move on to the next. All 201 URLs must point to the same file (same checksum). By default this is empty 202 and `iso_url` is used. Only one of `iso_url` or `iso_urls` can be specified. 203 204 - `machine_type` (string) - The type of machine emulation to use. Run your 205 qemu binary with the flags `-machine help` to list available types for 206 your system. This defaults to "pc". 207 208 - `net_device` (string) - The driver to use for the network interface. Allowed 209 values "ne2k\_pci", "i82551", "i82557b", "i82559er", "rtl8139", "e1000", 210 "pcnet", "virtio", "virtio-net", "virtio-net-pci", "usb-net", "i82559a", 211 "i82559b", "i82559c", "i82550", "i82562", "i82557a", "i82557c", "i82801", 212 "vmxnet3", "i82558a" or "i82558b". The Qemu builder uses "virtio-net" by 213 default. 214 215 - `output_directory` (string) - This is the path to the directory where the 216 resulting virtual machine will be created. This may be relative or absolute. 217 If relative, the path is relative to the working directory when `packer` 218 is executed. This directory must not exist or be empty prior to running 219 the builder. By default this is "output-BUILDNAME" where "BUILDNAME" is the 220 name of the build. 221 222 - `qemu_binary` (string) - The name of the Qemu binary to look for. This 223 defaults to "qemu-system-x86\_64", but may need to be changed for 224 some platforms. For example "qemu-kvm", or "qemu-system-i386" may be a 225 better choice for some systems. 226 227 - `qemuargs` (array of array of strings) - Allows complete control over the 228 qemu command line (though not, at this time, qemu-img). Each array of 229 strings makes up a command line switch that overrides matching default 230 switch/value pairs. Any value specified as an empty string is ignored. All 231 values after the switch are concatenated with no separator. 232 233 \~> **Warning:** The qemu command line allows extreme flexibility, so beware 234 of conflicting arguments causing failures of your run. For instance, using 235 --no-acpi could break the ability to send power signal type commands (e.g., 236 shutdown -P now) to the virtual machine, thus preventing proper shutdown. To see 237 the defaults, look in the packer.log file and search for the qemu-system-x86 238 command. The arguments are all printed for review. 239 240 The following shows a sample usage: 241 242 ``` {.javascript} 243 // ... 244 "qemuargs": [ 245 [ "-m", "1024M" ], 246 [ "--no-acpi", "" ], 247 [ 248 "-netdev", 249 "user,id=mynet0,", 250 "hostfwd=hostip:hostport-guestip:guestport", 251 "" 252 ], 253 [ "-device", "virtio-net,netdev=mynet0" ] 254 ] 255 // ... 256 ``` 257 258 would produce the following (not including other defaults supplied by the 259 builder and not otherwise conflicting with the qemuargs): 260 261 <pre class="prettyprint"> 262 qemu-system-x86 -m 1024m --no-acpi -netdev user,id=mynet0,hostfwd=hostip:hostport-guestip:guestport -device virtio-net,netdev=mynet0" 263 </pre> 264 265 You can also use the `SSHHostPort` template variable to produce a packer 266 template that can be invoked by `make` in parallel: 267 268 ``` {.javascript} 269 // ... 270 "qemuargs": [ 271 [ "-netdev", "user,hostfwd=tcp::{{ .SSHHostPort }}-:22,id=forward"], 272 [ "-device", "virtio-net,netdev=forward,id=net0"], 273 ... 274 ] 275 // ... 276 ``` 277 `make -j 3 my-awesome-packer-templates` spawns 3 packer processes, each of which 278 will bind to their own SSH port as determined by each process. This will also 279 work with WinRM, just change the port forward in `qemuargs` to map to WinRM's 280 default port of `5985` or whatever value you have the service set to listen on. 281 282 - `shutdown_command` (string) - The command to use to gracefully shut down the 283 machine once all the provisioning is done. By default this is an empty 284 string, which tells Packer to just forcefully shut down the machine unless a 285 shutdown command takes place inside script so this may safely be omitted. If 286 one or more scripts require a reboot it is suggested to leave this blank 287 since reboots may fail and specify the final shutdown command in your 288 last script. 289 290 - `shutdown_timeout` (string) - The amount of time to wait after executing the 291 `shutdown_command` for the virtual machine to actually shut down. If it 292 doesn't shut down in this time, it is an error. By default, the timeout is 293 `5m`, or five minutes. 294 295 - `skip_compaction` (boolean) - Packer compacts the QCOW2 image using `qemu-img convert`. 296 Set this option to `true` to disable compacting. Defaults to `false`. 297 298 - `ssh_host_port_min` and `ssh_host_port_max` (integer) - The minimum and 299 maximum port to use for the SSH port on the host machine which is forwarded 300 to the SSH port on the guest machine. Because Packer often runs in parallel, 301 Packer will choose a randomly available port in this range to use as the 302 host port. By default this is 2222 to 4444. 303 304 - `vm_name` (string) - This is the name of the image (QCOW2 or IMG) file for 305 the new virtual machine. By default this is "packer-BUILDNAME", where 306 `BUILDNAME` is the name of the build. Currently, no file extension will be 307 used unless it is specified in this option. 308 309 - `vnc_bind_address` (string / IP address) - The IP address that should be binded 310 to for VNC. By default packer will use 127.0.0.1 for this. If you wish to bind 311 to all interfaces use 0.0.0.0 312 313 - `vnc_port_min` and `vnc_port_max` (integer) - The minimum and maximum port 314 to use for VNC access to the virtual machine. The builder uses VNC to type 315 the initial `boot_command`. Because Packer generally runs in parallel, 316 Packer uses a randomly chosen port in this range that appears available. By 317 default this is 5900 to 6000. The minimum and maximum ports are inclusive. 318 319 ## Boot Command 320 321 The `boot_command` configuration is very important: it specifies the keys to 322 type when the virtual machine is first booted in order to start the OS 323 installer. This command is typed after `boot_wait`, which gives the virtual 324 machine some time to actually load the ISO. 325 326 As documented above, the `boot_command` is an array of strings. The strings are 327 all typed in sequence. It is an array only to improve readability within the 328 template. 329 330 The boot command is "typed" character for character over a VNC connection to the 331 machine, simulating a human actually typing the keyboard. There are a set of 332 special keys available. If these are in your boot command, they will be replaced 333 by the proper key: 334 335 - `<bs>` - Backspace 336 337 - `<del>` - Delete 338 339 - `<enter>` and `<return>` - Simulates an actual "enter" or "return" keypress. 340 341 - `<esc>` - Simulates pressing the escape key. 342 343 - `<tab>` - Simulates pressing the tab key. 344 345 - `<f1>` - `<f12>` - Simulates pressing a function key. 346 347 - `<up>` `<down>` `<left>` `<right>` - Simulates pressing an arrow key. 348 349 - `<spacebar>` - Simulates pressing the spacebar. 350 351 - `<insert>` - Simulates pressing the insert key. 352 353 - `<home>` `<end>` - Simulates pressing the home and end keys. 354 355 - `<pageUp>` `<pageDown>` - Simulates pressing the page up and page down keys. 356 357 - `<wait>` `<wait5>` `<wait10>` - Adds a 1, 5 or 10 second pause before 358 sending any additional keys. This is useful if you have to generally wait 359 for the UI to update before typing more. 360 361 - `<waitXX> ` - Add user defined time.Duration pause before sending any 362 additional keys. For example `<wait10m>` or `<wait1m20s>` 363 364 In addition to the special keys, each command to type is treated as a 365 [configuration template](/docs/templates/configuration-templates.html). The 366 available variables are: 367 368 - `HTTPIP` and `HTTPPort` - The IP and port, respectively of an HTTP server 369 that is started serving the directory specified by the `http_directory` 370 configuration parameter. If `http_directory` isn't specified, these will be 371 blank! 372 373 Example boot command. This is actually a working boot command used to start an 374 CentOS 6.4 installer: 375 376 ``` {.javascript} 377 "boot_command": 378 [ 379 "<tab><wait>", 380 " ks=http://10.0.2.2:{{ .HTTPPort }}/centos6-ks.cfg<enter>" 381 ] 382 ```