KVM Guests: Using Virt-Install to Import an Existing Disk Image

March 8th, 2011 | Tags: , , , , ,

In my previous article KVM Guests: Using Virt-Install to Install Debian and Ubuntu Guests we identified how we can use the virt-install command to create a KVM VM and instead of using an iso or physical cd for the install media we can instead use a boot image from an apt mirror via http.  Now obviously this does not harness the true power of the virt-install command.  Today we will discuss how we can take an existing disk image and import it as a VM with the appropriate devices, this would most commonly be done with sysprepped Windows images, to speed up deployment. Though these instructions will easily work with any OS.

Assumptions I Have Made

  • My KVM host is Ubuntu 10.10 and I am assuming that yours is as well.  If it is not then the syntax might be slightly different or may not include the same features.
  • That you have kvm installed on the host and you can manually create VMs using virt-manager and they work perfectly.
  • That you have a bridge configured and working on other guests.
  • That you have virt-install and libvirt-bin installed as well as virt-manager or virt-viewer so that you can complete the install after the virt-install command has completed.
  • That you are trying to import disk images that support VirtIO devices (most recent Linux distributions, Windows does not natively support the VirtIO interface, so you will had to have manually installed the VirtIO drivers into your disk image).

The Basic Command

# virt-install -n vmname -r 2048 --os-type=windows   --os-variant=win7 --disk   /kvm/images/disk/vmname_boot.img,device=disk,bus=virtio -w   bridge=br0,model=virtio --vnc --noautoconsole --import

Parameters Detailed

  • -n vmname [the name of your VM]
  • -r 2048 [the amount of RAM in MB for your VM]
  • –os-type=windows [the type of OS linux or windows]
  • –os-variant=win7 [the distribution or version of Windows for a full list see man virt-install]
  • –disk /kvm/images/disk/vmname_boot.img,device=disk,bus=virtio [this is a long one you define the path, then comma delimited options, device is the type of storage cdrom, disk, floppy, bus is the interface ide, scsi, usb, virtio - virtio is the fastest but you need to install the drivers for Windows and older versions of Linux don't have support]
  • -w bridge=br0,model=virtio [the network configuration, in this case we are connecting to a bridge named br0, and using the virtio drivers which perform much better if you are using an OS which doesn't support virtio you can use e1000 or rtl8139.  You could alternatively use --nonetworks if you do not need networking]
  • –vnc [configures the graphics card to use VNC allowing you to use virt-viewer or virt-manager to see the desktop as if you were at the a monitor of a physical machine]
  • –noautoconsole [configures the installer to NOT automatically try to open virt-viewer to view the console to complete the installation - this is helpful if you are working on a remote system through SSH]

Multiple Disk Variation

# virt-install -n vmname -r 2048 --os-type=windows    --os-variant=win7 --disk    /kvm/images/disk/vmname_boot.img,device=disk,bus=virtio --disk    /kvm/images/disk/vmname_data_1.img,device=disk,bus=virtio -w    bridge=br0,model=virtio --vnc --noautoconsole --import

LVM Disk Image Variation

# virt-install -n vmname -r 2048 --os-type=windows    --os-variant=win7 --disk    /dev/vg_name/lv_name,device=disk,bus=virtio -w    bridge=br0,model=virtio --vnc --noautoconsole --import

No VirtIO Variation (uses IDE and e1000 NIC emulation)

# virt-install -n vmname -r 2048 --os-type=windows    --os-variant=win7 --disk    /kvm/images/disk/vmname_boot.img,device=disk,bus=ide -w    bridge=br0,model=e1000 --vnc --noautoconsole --import

Now as I said before my examples are all defined as Windows 7 VMs but they could just as easily have been a Linux distribution as long as it has already been installed onto a hard disk image or a LV.  Though Linux doesn’t really have a sysprep equivalent you really have to manually update the configuration files to change the hostname, ip address or write a script to make those changes for you.  The value here is really for your Windows guests since they can be so cumbersome to spin up without some sort of imaging process.

  1. root
    February 17th, 2012 at 01:33
    Quote | #1

    Nice article. Keep ‘em coming! :)

Comments are closed.