linux_wiki:kernel-based_virtual_machine_kvm

This is an old revision of the document!


Kernel-based Virtual Machine (KVM)

Description: KVM is “a full virtualization solution for Linux on x86 hardware containing virtualization extensions.” (KVM Main Page) It can be run on the desktop with GUI managers or on a headless server.

Assumptions:

  • Some Red Hat based distro is already installed. (CentOS, Scientific, RHEL, etc)
  • This page was written using CentOS 6.6 with GNOME desktop (headless KVM is on the to do list!)

2015-02-13: These are rough notes and in progress as I learn KVM.

  • Install: Virtualization Groups
yum groupinstall "Virtualization*"

Group Explanations This will install the following package groups:

  • Virtualization Tools ⇒ Tools for offline virtual image management.
  • Virtualization Platform ⇒ Provides an interface for accessing and controlling virtualized guests and containers.
  • Virtualization Client ⇒ Clients for installing and managing virtualization instances.
  • Virtualization ⇒ Provides an environment for hosting virtualized guests.

For detailed info and a list of packages that will be installed:

yum groupinfo "Virtualization*"
  • Install: Additional Useful Packages
yum install bridge-utils dejavu-lgc-sans-fonts tigervnc xorg-x11-xauth

Packages Descriptions

  • bridge-utils ⇒ network bridging to allow the VMs to use the host machine's physical interfaces as a bridge.
  • dejavu-lgc-sans-fonts ⇒ Fonts for virt-manager (GUI VM manager)
  • tigervnc ⇒ VNC to connect locally to VM console
  • xorg-x11-xauth ⇒ X11 forwarding through ssh. (Remote virt-manager for headless servers)

  • Ensure that the KVM kernel module is loaded
lsmod | grep kvm
  • Start the libvirtd service
service libvirtd start
  • Enable libvirtd on system boot
chkconfig libvirtd on
  • Verify libvirtd is running successfully
service libvirtd status
virsh -c qemu://system list

By default, VMs will be on a private network with no access to the outside world unless they use a physical host network interface as a bridge.

  • Turn off NetworkManager, which does not play nice with bridging.
chkconfig NetworkManager off
service NetworkManager stop
  • Ensure the network service is enabled and running.
chkconfig network on
service network start
  • Create a bridge interface called “br0” that will use the physical interface “eth0” the easy way.
virsh iface-bridge eth0 br0

This will create the new file “ifcfg-br0” and modify “ifcfg-eth0” to look as follows:

/etc/sysconfig/network-scripts/ifcfg-eth0

DEVICE="eth0"
HWADDR="00:04:4B:17:3C:FA"
ONBOOT="yes"
BRIDGE="br0"

/etc/sysconfig/network-scripts/ifcfg-br0

DEVICE="br0"
ONBOOT="yes"
TYPE="Bridge"
BOOTPROTO="dhcp"
STP="on"
DELAY="0"

br0 will be the interface with an IP address. To make it static, simply edit ifcfg-br0 and change it:

DEVICE="br0"
ONBOOT="yes"
TYPE="Bridge"
BOOTPROTO="none"
STP="on"
DELAY="0"
IPADDR="192.168.0.100"
NETMASK="255.255.255.0"
GATEWAY="192.168.0.1"
  • Enable ip forwarding

Edit /etc/sysctl.conf

inet.ipv4.ip_forward = 1
  • Apply settings
sysctl -p /etc/sysctl.conf
  • Restart the network service
service network restart

If SELinux is in Enforcing mode, there is additional configuration if you change the default directory for VM images.

Default VM Image directory: /var/lib/libvirt/images/

  • Create the directory
mkdir /vm-images
  • Install the following package to enable use of the “semanage” utility.
yum install policycoreutils-python
  • Set security context for the vm-images directory and everything that is created under it.
semanage fcontext -a -t virt_image_t "/vm-images(/.*)?"
  • Restore the security context (I don't know why you have to do this yet…but you do)
restorecon -R /vm-images
  • Verify
ls -Z /vm-images

VMs can be installed via a command line tool (virt-install) or a GUI (virt-manager).

virt-install has many options, see them with “virt-install –help”.

Common options:

General Options
--name=NAME  (Name of the guest)
--ram=MEMORY  (Memory in megabytes)
--vcpus=VCPUS  (Number of vcpus)
 
Installation Method
--cdrom=CDROM  (CD-ROM installation media, can be ISO or physical cd-rom drive)
--location=LOCATION  (Installation source via http, ftp, or nfs.)
--pxe  (Boot from network using PXE)
--extra-args "ks=http://myserver/mykickstartfile.ks"  (Path to kickstart file for automated installs)
 
Storage Configuration
--disk=DISKOPTS  (Storage with different options)
  --disk path=/my/existing/disk 
  --disk path=/my/new/disk,size=10 (in GB)
 
Network
--network bridge=br0  (Guest network interface)
 
Graphics
--graphics=GRAPHICS  (Guest display settings)
  --graphics vnc  (Default if DISPLAY environment variable is set)
  --graphics none  (Default if DISPLAY environment variable is not set)

Example: Interactive Install with VNC

Install a VM called “centos1”, create a 20GB hard drive, 1 CPU, 1GB ram, point it to the iso, have it use the network interface “br0”

virt-install \
--name=centos1 \
--disk path=/vm-images/centos1.img,size=20 \
--vcpus=1 --ram=1024 \
--cdrom=/vm-images/CentOS-6.6-x86_64-netinstall.iso \
--network bridge=br0

Connect to Guest for Installation

After the VM is created, if you are using the “–graphics vnc” option (either by default or explicitly), a console is opened up for interactive install.

If you close it, this is how you can connect back to the guest:

  • Method 1: Connect directly to the guest console

Example connect to the guest named “centos1” (Yes, that is 3 slashes)

virt-viewer --connect qemu:///system centos1
  • Method 2: Open virt-manager

Example open virt-manager and then the VM “centos1”

virt-manager
  • Select the VM
  • Click “Open”

Proceed with installation as normal.


Installing locally via a GUI.

  • Open a terminal and type:
virt-manager
  • The virt-manager GUI will open.
  • Select the name of the host (probably “localhost (QEMU)”)
  • Click the “Create a New Virtual Machine” button (the monitor with a play button on it)
    • Step 1 ⇒
      • Name: Name the virtual machine
      • Choose how to install: Select the install method
    • Step 2 ⇒
      • Browse for the media
      • OS Type/Version: Optionally, select the OS type and version
    • Step 3 ⇒
      • Memory/CPU: Select memory and CPU.
    • Step 4 ⇒
      • Storage: Create or use existing storage
    • Step 5 ⇒
      • Ready to install: Confirm settings, set advanced options such as Host network device.
  • linux_wiki/kernel-based_virtual_machine_kvm.1423882909.txt.gz
  • Last modified: 2019/05/25 23:50
  • (external edit)