Show pageOld revisionsBacklinksBack to top This page is read only. You can view the source, but not change it. Ask your administrator if you think this is wrong. ====== Kernel-based Virtual Machine (KVM) ====== **General Information** KVM is "a full virtualization solution for Linux on x86 hardware containing virtualization extensions." ([[http://www.linux-kvm.org/page/Main_Page|KVM Main Page]]) It can be run on the desktop with GUI managers or on a headless server. **Checklist** * Distro(s): Enterprise Linux 6 * Other: 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. ---- ====== Installation ====== ==== Install: Required Virtualization Packages (CLI) ==== <code bash> yum install qemu-kvm python-virtinst virt-top libvirt libvirt-client </code> **CLI Package Explanations** * qemu-kvm => The main KVM package. * python-virtinst => CLI tools and libraries for creating VMs. * virt-top => CLI VM stats. * libvirt => Toolkit for libvirtd service. * libvirt-client => Toolkit for VM clients. ==== Install: Optional Virtualization Packages (GUI) ==== <code bash> yum install virt-manager virt-viewer </code> **GUI Package Explanations** * virt-manager => GUI VM administration tool. * virt-viewer => GUI local console connection to VMs. ==== Install: Optional Useful Packages ==== <code bash> yum install bridge-utils dejavu-lgc-sans-fonts tigervnc xorg-x11-xauth </code> **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 remotely to VM's GUI. * xorg-x11-xauth => X11 forwarding through ssh. (Remote virt-manager for headless servers) ---- ====== Verify Installation and Start Services ====== * Ensure that the KVM kernel module is loaded <code bash> [root@bash ~]# lsmod | grep kvm kvm_intel 55496 0 kvm 337900 1 kvm_intel </code> * If it is not loaded, attempt to load the module: <code bash> modprobe -v kvm </code> * Start the libvirtd service <code bash> service libvirtd start </code> * Enable libvirtd on system boot <code bash> chkconfig libvirtd on </code> * Verify libvirtd is running successfully (Yes, 3 forward slashes) <code bash> service libvirtd status virsh -c qemu:///system list </code> ---- ====== Setup Networking ====== 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. ==== NetworkManager vs network ==== * Turn off NetworkManager, which does not play nice with bridging. <code bash> chkconfig NetworkManager off service NetworkManager stop </code> * Ensure the network service is enabled and running. <code bash> chkconfig network on service network start </code> ==== Create a Bridge ==== * Create a bridge interface called "br0" that will use the physical interface "eth0" the easy way. <code bash> virsh iface-bridge eth0 br0 </code> This will create the new file "ifcfg-br0" and modify "ifcfg-eth0" to look as follows: **/etc/sysconfig/network-scripts/ifcfg-eth0** <code bash> DEVICE="eth0" HWADDR="00:04:4B:17:3C:FA" ONBOOT="yes" BRIDGE="br0" </code> **/etc/sysconfig/network-scripts/ifcfg-br0** <code bash> DEVICE="br0" ONBOOT="yes" TYPE="Bridge" BOOTPROTO="dhcp" STP="on" DELAY="0" </code> br0 will be the interface with an IP address. To make it static, simply edit ifcfg-br0 and change it: <code bash> 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" DNS1="208.67.222.222" DNS2="208.67.220.220" </code> Note: Those DNS entries are for OpenDNS. ==== IP Forwarding ==== * Enable ip forwarding **Edit /etc/sysctl.conf** <code> inet.ipv4.ip_forward = 1 </code> * Apply settings <code bash> sysctl -p /etc/sysctl.conf </code> * Restart the network service <code bash> service network restart </code> ---- ====== SELinux ====== If SELinux is on and in Enforcing mode, there is additional configuration if you change the default directory for VM images. **Check if SELinux is on/enabled** <code bash> [bash]# sestatus SELinux status: enabled SELinuxfs mount: /selinux Current mode: enforcing </code> **Default VM Image directory:** /var/lib/libvirt/images/ ==== Changing the Default VM Image Path ==== * Create the directory <code bash> mkdir /vm-images </code> * Set the context of the new directory, referencing the default <code bash> chcon -R --reference /var/lib/libvirt/images /vm-images </code> * Verify <code bash> ls -Z /vm-images </code> * Create a symlink to the new location to ensure that is survives a SELinux relabel. <code bash> rmdir /var/lib/libvirt/images ln -s /vm-images /var/lib/libvirt/images </code> ---- ====== Creating VMs ====== VMs can be installed via a command line tool (virt-install) or a GUI (virt-manager). ==== CLI: virt-install ==== virt-install has many options, see them with "virt-install --help". Common options: <code bash> 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) </code> === 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" <code bash> 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 </code> === 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) <code bash> virt-viewer --connect qemu:///system centos1 </code> * **Method 2: Open virt-manager** Example open virt-manager and then the VM "centos1" <code bash> virt-manager </code> * Select the VM * Click "Open" Proceed with installation as normal. ---- ==== GUI: virt-manager ==== Installing locally via a GUI. * Open a terminal and type: <code bash> virt-manager </code> * 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. ---- ====== Disabling virbr0 NAT Interface ====== On some desktop installations, a NAT interface "virbr0" is auto created. To disable this interface: * View the interface<code bash>virsh net-list</code> * Turn off the interface<code bash>virsh net-destroy default</code> * Disable auto start on the interface<code bash>virsh net-autostart default –disable</code> * Verify State is "inactive" and Autostart is "no", then ensure virbr0 is not in the interface list<code bash>virsh net-list --all ip addr show</code> * Restart the network service * EL6<code bash>service network restart</code> * EL7<code bash>systemctl restart network</code> ---- linux_wiki/kernel-based_virtual_machine_kvm.txt Last modified: 2019/05/25 23:50(external edit)