github.com/kaixiang/packer@v0.5.2-0.20140114230416-1f5786b0d7f1/website/source/docs/builders/qemu.html.markdown (about) 1 --- 2 layout: "docs" 3 --- 4 5 # QEMU Builder 6 7 Type: `qemu` 8 9 The Qemu builder is able to create [KVM](http://www.linux-kvm.org) 10 and [Xen](http://www.xenproject.org) virtual machine images. Support 11 for Xen is experimanetal at this time. 12 13 The builder builds a virtual machine by creating a new virtual machine 14 from scratch, booting it, installing an OS, rebooting the machine with the 15 boot media as the virtual hard drive, provisioning software within 16 the OS, then shutting it down. The result of the Qemu builder is a directory 17 containing the image file necessary to run the virtual machine on KVM or Xen. 18 19 ## Basic Example 20 21 Here is a basic example. This example is functional so long as you fixup 22 paths to files, URLS for ISOs and checksums. 23 24 <pre class="prettyprint"> 25 { 26 "builders": 27 [ 28 { 29 "type": "qemu", 30 "iso_url": "http://mirror.raystedman.net/centos/6/isos/x86_64/CentOS-6.4-x86_64-minimal.iso", 31 "iso_checksum": "4a5fa01c81cc300f4729136e28ebe600", 32 "iso_checksum_type": "md5", 33 "output_directory": "output_centos_tdhtest", 34 "ssh_wait_timeout": "30s", 35 "shutdown_command": "shutdown -P now", 36 "disk_size": 5000, 37 "format": "qcow2", 38 "headless": false, 39 "accelerator": "kvm", 40 "http_directory": "httpdir", 41 "http_port_min": 10082, 42 "http_port_max": 10089, 43 "ssh_host_port_min": 2222, 44 "ssh_host_port_max": 2229, 45 "ssh_username": "root", 46 "ssh_password": "s0m3password", 47 "ssh_port": 22, 48 "ssh_wait_timeout": "90m", 49 "vm_name": "tdhtest", 50 "net_device": "virtio-net", 51 "disk_interface": "virtio", 52 "boot_command": 53 [ 54 "<tab><wait>", 55 " ks=http://10.0.2.2:{{ .HTTPPort }}/centos6-ks.cfg<enter>" 56 ] 57 } 58 ] 59 } 60 </pre> 61 62 A working CentOS 6.x kickstart file can be found 63 [at this URL](https://gist.github.com/mitchellh/7328271/raw/c91e0c4fa19c171a40b016c6c8f251f90d2ad0ba/centos6-ks.cfg), adapted from an unknown source. 64 Place this file in the http directory with the proper name. For the 65 example above, it should go into "httpdir" with a name of "centos6-ks.cfg". 66 67 ## Configuration Reference 68 69 There are many configuration options available for the Qemu builder. 70 They are organized below into two categories: required and optional. Within 71 each category, the available options are alphabetized and described. 72 73 Required: 74 75 * `iso_checksum` (string) - The checksum for the OS ISO file. Because ISO 76 files are so large, this is required and Packer will verify it prior 77 to booting a virtual machine with the ISO attached. The type of the 78 checksum is specified with `iso_checksum_type`, documented below. 79 80 * `iso_checksum_type` (string) - The type of the checksum specified in 81 `iso_checksum`. Valid values are "md5", "sha1", "sha256", or "sha512" currently. 82 83 * `iso_url` (string) - A URL to the ISO containing the installation image. 84 This URL can be either an HTTP URL or a file URL (or path to a file). 85 If this is an HTTP URL, Packer will download it and cache it between 86 runs. 87 88 * `ssh_username` (string) - The username to use to SSH into the machine 89 once the OS is installed. 90 91 Optional: 92 93 * `accelerator` (string) - The accelerator type to use when running the VM. 94 This may have a value of either "kvm" or "xen" and you must have that 95 support in on the machine on which you run the builder. 96 97 * `boot_command` (array of strings) - This is an array of commands to type 98 when the virtual machine is first booted. The goal of these commands should 99 be to type just enough to initialize the operating system installer. Special 100 keys can be typed as well, and are covered in the section below on the boot 101 command. If this is not specified, it is assumed the installer will start 102 itself. 103 104 * `boot_wait` (string) - The time to wait after booting the initial virtual 105 machine before typing the `boot_command`. The value of this should be 106 a duration. Examples are "5s" and "1m30s" which will cause Packer to wait 107 five seconds and one minute 30 seconds, respectively. If this isn't specified, 108 the default is 10 seconds. 109 110 * `disk_size` (int) - The size, in megabytes, of the hard disk to create 111 for the VM. By default, this is 40000 (40 GB). 112 113 * `disk_interface` (string) - The interface to use for the disk. Allowed 114 values include any of "ide," "scsi" or "virtio." Note also that any boot 115 commands or kickstart type scripts must have proper adjustments for 116 resulting device names. The Qemu builder uses "virtio" by default. 117 118 * `format` (string) - Either "qcow2" or "raw", this specifies the output 119 format of the virtual machine image. This defaults to "qcow2". 120 121 * `floppy_files` (array of strings) - A list of files to place onto a floppy 122 disk that gets attached when Packer powers up the VM. This is most useful 123 for unattended Windows installs, which look for an `Autounattend.xml` file 124 on removable media. By default no floppy will be attached. All files 125 listed in this setting get placed into the root directory of the floppy 126 and teh floppy is attached as the first floppy device. Currently, no 127 support exists for sub-directories. 128 129 * `headless` (bool) - Packer defaults to building virtual machines by 130 launching a GUI that shows the console of the machine being built. 131 When this value is set to true, the machine will start without a console. 132 133 * `http_directory` (string) - Path to a directory to serve using an HTTP 134 server. The files in this directory will be available over HTTP that will 135 be requestable from the virtual machine. This is useful for hosting 136 kickstart files and so on. By default this is "", which means no HTTP 137 server will be started. The address and port of the HTTP server will be 138 available as variables in `boot_command`. This is covered in more detail 139 below. 140 141 * `http_port_min` and `http_port_max` (int) - These are the minimum and 142 maximum port to use for the HTTP server started to serve the `http_directory`. 143 Because Packer often runs in parallel, Packer will choose a randomly available 144 port in this range to run the HTTP server. If you want to force the HTTP 145 server to be on one port, make this minimum and maximum port the same. 146 By default the values are 8000 and 9000, respectively. 147 148 * `iso_urls` (array of strings) - Multiple URLs for the ISO to download. 149 Packer will try these in order. If anything goes wrong attempting to download 150 or while downloading a single URL, it will move on to the next. All URLs 151 must point to the same file (same checksum). By default this is empty 152 and `iso_url` is used. Only one of `iso_url` or `iso_urls` can be specified. 153 154 * `net_device` (string) - The driver to use for the network interface. Allowed 155 values "ne2k_pci," "i82551," "i82557b," "i82559er," "rtl8139," "e1000," 156 "pcnet" or "virtio." The Qemu builder uses "virtio" by default. 157 158 * `qemuargs` (array of array of strings) - Allows complete control over 159 the qemu command line (though not, at this time, qemu-img). Each array 160 of strings makes up a command line switch that overrides matching default 161 switch/value pairs. Any value specified as an empty string is ignored. 162 All values after the switch are concatenated with no separater. 163 164 WARNING: The qemu command line allows extreme flexibility, so beware of 165 conflicting arguments causing failures of your run. For instance, using 166 --no-acpi could break the ability to send power signal type commands (e.g., 167 shutdown -P now) to the virtual machine, thus preventing proper shutdown. To 168 see the defaults, look in the packer.log file and search for the 169 qemu-system-x86 command. The arguments are all printed for review. 170 171 The following shows a sample usage: 172 173 <pre class="prettyprint"> 174 . . . 175 "qemuargs": [ 176 [ "-m", "1024m" ], 177 [ "--no-acpi", "" ], 178 [ 179 "-netdev", 180 "user,id=mynet0,", 181 "hostfwd=hostip:hostport-guestip:guestport", 182 "" 183 ], 184 [ "-device", "virtio-net,netdev=mynet0" ] 185 ] 186 . . . 187 </pre> 188 189 would produce the following (not including other defaults supplied by the builder and not otherwise conflicting with the qemuargs): 190 191 <pre class="prettyprint"> 192 qemu-system-x86 -m 1024m --no-acpi -netdev user,id=mynet0,hostfwd=hostip:hostport-guestip:guestport -device virtio-net,netdev=mynet0" 193 </pre> 194 195 * `output_directory` (string) - This is the path to the directory where the 196 resulting virtual machine will be created. This may be relative or absolute. 197 If relative, the path is relative to the working directory when `packer` 198 is executed. This directory must not exist or be empty prior to running the builder. 199 By default this is "output-BUILDNAME" where "BUILDNAME" is the name 200 of the build. 201 202 * `run_once` (boolean) - When set to true, run_once causes the builder to run 203 Qemu only once, rather than twice. Normally (default false) the builder 204 will run Qemu once for an initial OS install, then switch the CDROM device 205 and boot device for a second run. In that case, Packer does not wait for 206 SSH connections until the second power up of the VM. This approach is often 207 necessary in Linux distribution installs. However, in many Windows unattended 208 installs, the setup handles reboots and dealing with the CDROM as the boot 209 device. With care, a power-down/power-up setting (run_once is set to false) 210 could work if the unattended install is set to restart into audit mode. 211 212 * `shutdown_command` (string) - The command to use to gracefully shut down 213 the machine once all the provisioning is done. By default this is an empty 214 string, which tells Packer to just forcefully shut down the machine. 215 216 * `shutdown_timeout` (string) - The amount of time to wait after executing 217 the `shutdown_command` for the virtual machine to actually shut down. 218 If it doesn't shut down in this time, it is an error. By default, the timeout 219 is "5m", or five minutes. 220 221 * `ssh_host_port_min` and `ssh_host_port_max` (uint) - The minimum and 222 maximum port to use for the SSH port on the host machine which is forwarded 223 to the SSH port on the guest machine. Because Packer often runs in parallel, 224 Packer will choose a randomly available port in this range to use as the 225 host port. 226 227 * `ssh_key_path` (string) - Path to a private key to use for authenticating 228 with SSH. By default this is not set (key-based auth won't be used). 229 The associated public key is expected to already be configured on the 230 VM being prepared by some other process (kickstart, etc.). 231 232 * `ssh_password` (string) - The password for `ssh_username` to use to 233 authenticate with SSH. By default this is the empty string. 234 235 * `ssh_port` (int) - The port that SSH will be listening on in the guest 236 virtual machine. By default this is 22. The Qemu builder will map, via 237 port forward, a port on the host machine to the port listed here so 238 machines outside the installing VM can access the VM. 239 240 * `ssh_wait_timeout` (string) - The duration to wait for SSH to become 241 available. By default this is "20m", or 20 minutes. Note that this should 242 be quite long since the timer begins as soon as the virtual machine is booted. 243 244 * `vm_name` (string) - This is the name of the image (QCOW2 or IMG) file for 245 the new virtual machine, without the file extension. By default this is 246 "packer-BUILDNAME", where "BUILDNAME" is the name of the build. 247 248 ## Boot Command 249 250 The `boot_command` configuration is very important: it specifies the keys 251 to type when the virtual machine is first booted in order to start the 252 OS installer. This command is typed after `boot_wait`, which gives the 253 virtual machine some time to actually load the ISO. 254 255 As documented above, the `boot_command` is an array of strings. The 256 strings are all typed in sequence. It is an array only to improve readability 257 within the template. 258 259 The boot command is "typed" character for character over a VNC connection 260 to the machine, simulating a human actually typing the keyboard. There are 261 a set of special keys available. If these are in your boot command, they 262 will be replaced by the proper key: 263 264 * `<enter>` and `<return>` - Simulates an actual "enter" or "return" keypress. 265 266 * `<esc>` - Simulates pressing the escape key. 267 268 * `<tab>` - Simulates pressing the tab key. 269 270 * `<wait>` `<wait5>` `<wait10>` - Adds a 1, 5 or 10 second pause before sending any additional keys. This 271 is useful if you have to generally wait for the UI to update before typing more. 272 273 In addition to the special keys, each command to type is treated as a 274 [configuration template](/docs/templates/configuration-templates.html). 275 The available variables are: 276 277 * `HTTPIP` and `HTTPPort` - The IP and port, respectively of an HTTP server 278 that is started serving the directory specified by the `http_directory` 279 configuration parameter. If `http_directory` isn't specified, these will 280 be blank! 281 282 Example boot command. This is actually a working boot command used to start 283 an CentOS 6.4 installer: 284 285 <pre class="prettyprint"> 286 "boot_command": 287 [ 288 "<tab><wait>", 289 " ks=http://10.0.2.2:{{ .HTTPPort }}/centos6-ks.cfg<enter>" 290 ] 291 </pre>