KVM

From Indie IT Wiki

Introduction

KVM (for Kernel-based Virtual Machine) is a full virtualization solution for Linux on x86 hardware containing virtualization extensions (Intel VT or AMD-V). It consists of a loadable kernel module, kvm.ko, that provides the core virtualization infrastructure and a processor specific module, kvm-intel.ko or kvm-amd.ko.

Using KVM, one can run multiple virtual machines running unmodified Linux or Windows images. Each virtual machine has private virtualized hardware: a network card, disk, graphics adapter, etc.

KVM is open source software. The kernel component of KVM is included in mainline Linux, as of 2.6.20. The userspace component of KVM is included in mainline QEMU, as of 1.3.

HOWTOs

Install

sudo apt-get install qemu-kvm libvirt-daemon-system libvirt-clients bridge-utils virt-manager

Guest Agent

ssh user@guest
sudo apt-get install qemu-guest-agent
sudo reboot

List

virsh --connect qemu:///system list --all

Start

virsh --connect qemu:///system start '<Name>'

Stop

virsh --connect qemu:///system stop '<Name>'

Reboot

virsh --connect qemu:///system reboot '<Name>'

Connect Remotely

virsh --connect qemu+ssh://username@servername/system

https://libvirt.org/remote.html#Remote_URI_reference

https://libvirt.org/uri.html

http://manpages.ubuntu.com/manpages/bionic/en/man1/virsh.1.html

Management

Ubuntu KVM Page

VirSH (Command Line)

Virtual Machine Manager (X Based GUI)

WebVirtMgr (Web Based)

Upgrade Virtual Disk Size

SSH in the host...

ssh login@host

Switch to root user...

sudo -i

Change to the Images directory...

cd /var/lib/libvirt/images

Check disk file...

qemu-img info disk1.qcow2

Increase disk size...

qemu-img resize disk1.qcow2 +10G

Check disk file again...

qemu-img info disk1.qcow2

Log out of host...

logout

Log in to guest...

ssh login@guest

Switch to root...

sudo -i

Install Cloud Utils for growpart software...

sudo apt-get -y install cloud-guest-utils

List current blocks...

lsblk

Extend partition to fill remaining space...

 growpart /dev/vda 2

Extend filesystem to fill remaining space...

resize2fs /dev/vda2

Reboot...

reboot

Convert OVA File Based VM To KVM

https://www.sbarjatiya.com/notes_wiki/index.php/CentOS_7.x_import_ova_file_based_VMs_to_KVM

Disable Virtual Network Interfaces

sudo -i
virsh net-list
virsh net-destroy default
virsh net-undefine default
virsh net-destroy private
virsh net-undefine private
service libvirtd restart
ifconfig
exit

Enable Virtual Network Interface

Save the following as a file called virsh_net_default.xml

<network>
  <name>default</name>
  <bridge name="virbr0"/>
  <forward mode="nat"/>
  <ip address="192.168.122.1" netmask="255.255.255.0">
    <dhcp>
      <range start="192.168.122.2" end="192.168.122.254"/>
    </dhcp>
  </ip>
</network>
sudo -i
virsh net-define /tmp/virsh_net_default.xml
virsh net-start default
virsh net-autostart default
service libvirtd restart
ifconfig
exit

View Network Information

virsh --connect qemu:///system net-list
virsh --connect qemu:///system net-info default
virsh --connect qemu:///system net-dhcp-leases default

Setup

Virtual Networking

Thanks

Information

Virsh Commands Cheat Sheet

Offical Web Site

Ubuntu Official Documentation

Nice Command Line examples