title: How to fully automate unattended virt-install?
body: <p>Let me start by saying what I want to do. I'd like to fully automate, in an unattended way, the building of QEMU/KVM VM images using <code>virt-install</code>. I know that some folks use the GUI tool to do this, or they edit a pre-existing image's XML description, but I want to start from scratch.</p>
  
  <p>I've Googled around and examples of doing this are hard to find. What I have found is that <code>virt-install</code> is the command to use, and that it can be used interactively with a TTY console attached (you manually answer configuration questions during the install). For a fully automated solution, you can specify a <em>kickstart</em> file (typically <code>preseed.cfg</code>) to provide answers to questions that you'd normally enter manually. The kickstart file can also specify additional software to install, disk and network configuration, etc.).</p>
  
  <p>I think I've got this mostly working except that the installation hangs shortly after install begins. I <em>think</em> it has something to do with the need (or not) to have a console attached to the install. Here is the <code>virt-install</code> command I am using:</p>
  
  <pre><code>virt-install --connect qemu:///system \
    --name vm --ram 128 \
    --disk path=./vm.qcow2,size=8,format=qcow2 \
    --location 'http://archive.ubuntu.com/ubuntu/dists/trusty/main/installer-amd64/' \
    --network user,model=virtio \
    --initrd-inject preseed.cfg \
    --extra-args="console=tty0 console=ttyS0,115200"
  </code></pre>
  
  <p>This is the <code>preseed.cfg</code> file (which I cribbed from many examples on the web and in the Ubuntu documentation):</p>
  
  <pre><code>### Localization
  # Locale sets language and country.
  d-i debian-installer/locale string en_US
  # Keyboard selection.
  d-i keyboard-configuration/layoutcode string us
  d-i keyboard-configuration/modelcode string pc105
  d-i keyboard-configuration/variantcode string
  
  ### Network configuration
  # netcfg will choose an interface that has link if possible. This makes it
  # skip displaying a list if there is more than one interface.
  d-i netcfg/choose_interface select auto
  # Any hostname and domain names assigned from dhcp take precedence over
  # values set here. However, setting the values still prevents the questions
  # from being shown, even if values come from dhcp.
  d-i netcfg/get_hostname string vm
  d-i netcfg/get_domain string foobar.net
  # Disable that annoying WEP key dialog.
  d-i netcfg/wireless_wep string
  
  ### Mirror settings
  d-i mirror/country string manual
  d-i mirror/http/hostname string us.archive.ubuntu.com
  d-i mirror/http/directory string /ubuntu
  d-i mirror/http/proxy string
  
  ### Partitioning
  # Encrypt your home directory?
  d-i user-setup/encrypt-home boolean false
  # Alternatively, you can specify a disk to partition. The device name
  # can be given in either devfs or traditional non-devfs format.
  d-i partman-auto/disk string /dev/vda
  # In addition, you'll need to specify the method to use.
  # The presently available methods are: "regular", "lvm" and "crypto"
  d-i partman-auto/method string regular
  # You can choose from any of the predefined partitioning recipes.
  d-i partman-auto/choose_recipe select atomic
  # This makes partman automatically partition without confirmation, provided
  # that you told it what to do using one of the methods above.
  d-i partman-partitioning/confirm_write_new_label boolean true
  d-i partman/choose_partition select finish
  d-i partman/confirm boolean true
  d-i partman/confirm_nooverwrite boolean true
  
  ### Clock and time zone setup
  # Controls whether or not the hardware clock is set to UTC.
  d-i clock-setup/utc boolean true
  # You may set this to any valid setting for $TZ; see the contents of
  # /usr/share/zoneinfo/ for valid values.
  d-i time/zone string UTC
  
  ### Account setup
  # Skip creation of a root account (normal user account will be able to
  # use sudo).
  d-i passwd/root-login boolean false
  
  # To create a normal user account.
  d-i passwd/user-fullname string VMuser
  d-i passwd/username string vmuser
  # Normal user's password, either in clear text
  # or encrypted using an MD5 hash.
  d-i passwd/user-password-crypted password CRACKMECRACKM
  
  # This is fairly safe to set, it makes grub install automatically to the MBR
  # if no other operating system is detected on the machine.
  d-i grub-installer/only_debian boolean true
  
  ### Package selection
  d-i tasksel/first multiselect standard
  # Individual additional packages to install
  d-i pkgsel/include string openssh-server
  
  ### Finishing up the first stage install
  # Avoid that last message about the install being complete.
  d-i finish-install/reboot_in_progress note
  # How do you want to manage upgrades on this system?
  d-i pkgsel/update-policy select none
  </code></pre>
  
  <p>After all that, when I execute the <code>virt-install</code> command I see:</p>
  
  <pre><code>WARNING  Unable to connect to graphical console: virt-viewer not installed. Please install the 'virt-viewer' package.
  WARNING  No console to launch for the guest, defaulting to --wait -1
  
  Starting install...
  Retrieving file linux...                                                                                
  Retrieving file initrd.gz...                                                                            
  Allocating 'virtinst-linux.rCdX0h'                                                                      
  Transferring virtinst-linux.rCdX0h                                                                      
  Allocating 'virtinst-initrd.gz.BbRBMv'                                                                  
  Transferring virtinst-initrd.gz.BbRBMv                                                                  
  Creating domain...                                                                                      
  Domain installation still in progress. Waiting for installation to complete.
  </code></pre>
  
  <p>and it just hangs. If I <code>^Z</code> into the background and start <code>virsh</code> I see the vm in a running state.</p>
  
  <p>I think I'm close, but need to fix it so that:</p>
  
  <ol>
  <li>Install shows complete and <code>virt-install</code> returns to shell.</li>
  <li>The new VM is shutdown and I'm left with the image file ready to go.</li>
  </ol>
  
  <p>I think #2 can be accomplished in the <code>preseed.cfg</code> file with some kind of cleanup instructions (still researching this), but any help of fixing #1 would be greatly appreciated.</p>