KVM Hypervior Cheatsheets

1. Install Packages

  • Check system is capable of running KVM: kvm-ok
sudo apt-get install qemu-kvm qemu-system libvirt-bin bridge-utils virt-manager -y

2. Create KVM/Qemu Hard Disk File

    qemu-img create -f raw <name>.img <Size>
        qemu-img create -f raw ubuntu14-HD.img 10G
  • Then copy the HD file to /var/lib/libvirt/images/

3. Launch VM with virt-install

    virt-install --name spinnaker \
    --ram 11096 \
    --vcpus=4 \
    --os-type linux \
    --os-variant=ubuntutrusty \
    --accelerate \
    --nographics -v  \
    --disk path=/var/lib/libvirt/images/ubuntu14-HD.img,size=8 \
    --extra-args "console=ttyS0" \
    --location /opt/ubuntu14.iso --force \
    --network bridge:virbr0
  • Create bridge virbr0 if it is necessary
  • To know what are --os-variant available, run virt-install --os-variant list
  • Specify --location and --disk locations
  • Specify --ram (By default in MBs)
  • Other Options
    • --boot hd Boot from HD file
    • --force Force to use existing HD that is used by another VM
    • --debug verbose
    • --description Description of VM

4. Connect to console

  • virsh list --all - : List VMs
  • virsh console <name> - : Connect to tty of the VM
    • Note down the IP of the VM once you connect to tty. we can ssh

      NOTE: If console/tty is already being used or active, you can reconnect to that tty by using --extra-args='console=ttyS0' option

5. Export VM as .qcow2

$ qemu-img convert -f raw -O qcow2 <souruce .img file> <destination .qcow2 file>
$ #Example:
$ qemu-img convert -f raw -O qcow2 /var/lib/libvirt/images/ubuntu14-HD.img /home/opsmx/spinnaker.qcow2

Commands CheatSheet

  • virsh list --all - Shows all VMs
  • virsh console <VM name> - Connect to tty of the VM (If tty is enables)
  • virsh shutdown <VM name> - Shutdown the VM
  • vish destroy <VM name> - Destroys VM (Won’t deletes the VM/ Similar to shutdown)
  • vish undefine <VM name> - Deletes the VM (Run after destroy)
  • virsh dumpxml <VM name> - Dumps configuration of VM in xml format
  • virsh net-list - List the available networks
  • virsh net-edit <net name> - Edit the network
  • virt-install --os-variant list - Lists OS variants
Written on January 27, 2018