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