Differences
This shows you the differences between two versions of the page.
linux_wiki:kernel-based_virtual_machine_kvm [2015/02/25 22:44] billdozor |
linux_wiki:kernel-based_virtual_machine_kvm [2019/05/25 23:50] |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Kernel-based Virtual Machine (KVM) ====== | ||
- | Description: | ||
- | |||
- | 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. | ||
- | |||
- | ===== Installation ===== | ||
- | |||
- | ==== Install: Required Virtualization Packages (CLI) ==== | ||
- | |||
- | <code bash> | ||
- | yum install qemu-kvm python-virtinst virt-top libvirt libvirt-client | ||
- | </ | ||
- | |||
- | **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 | ||
- | </ | ||
- | |||
- | **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 | ||
- | </ | ||
- | |||
- | **Packages Descriptions** | ||
- | * bridge-utils => network bridging to allow the VMs to use the host machine' | ||
- | * 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 | ||
- | kvm | ||
- | </ | ||
- | |||
- | * If it is not loaded, attempt to load the module: | ||
- | <code bash> | ||
- | modprobe -v kvm | ||
- | </ | ||
- | |||
- | * Start the libvirtd service | ||
- | |||
- | <code bash> | ||
- | service libvirtd start | ||
- | </ | ||
- | |||
- | * Enable libvirtd on system boot | ||
- | |||
- | <code bash> | ||
- | chkconfig libvirtd on | ||
- | </ | ||
- | |||
- | * Verify libvirtd is running successfully (Yes, 3 forward slashes) | ||
- | |||
- | <code bash> | ||
- | service libvirtd status | ||
- | virsh -c qemu:/// | ||
- | </ | ||
- | |||
- | ---- | ||
- | |||
- | ===== 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, | ||
- | |||
- | <code bash> | ||
- | chkconfig NetworkManager off | ||
- | service NetworkManager stop | ||
- | </ | ||
- | |||
- | * Ensure the network service is enabled and running. | ||
- | |||
- | <code bash> | ||
- | chkconfig network on | ||
- | service network start | ||
- | </ | ||
- | |||
- | ==== Create a Bridge ==== | ||
- | * Create a bridge interface called " | ||
- | |||
- | <code bash> | ||
- | virsh iface-bridge eth0 br0 | ||
- | </ | ||
- | |||
- | This will create the new file " | ||
- | |||
- | **/ | ||
- | <code bash> | ||
- | DEVICE=" | ||
- | HWADDR=" | ||
- | ONBOOT=" | ||
- | BRIDGE=" | ||
- | </ | ||
- | |||
- | **/ | ||
- | <code bash> | ||
- | DEVICE=" | ||
- | ONBOOT=" | ||
- | TYPE=" | ||
- | BOOTPROTO=" | ||
- | STP=" | ||
- | DELAY=" | ||
- | </ | ||
- | |||
- | br0 will be the interface with an IP address. To make it static, simply edit ifcfg-br0 and change it: | ||
- | <code bash> | ||
- | DEVICE=" | ||
- | ONBOOT=" | ||
- | TYPE=" | ||
- | BOOTPROTO=" | ||
- | STP=" | ||
- | DELAY=" | ||
- | IPADDR=" | ||
- | NETMASK=" | ||
- | GATEWAY=" | ||
- | </ | ||
- | |||
- | ==== IP Forwarding ==== | ||
- | * Enable ip forwarding | ||
- | |||
- | **Edit / | ||
- | < | ||
- | inet.ipv4.ip_forward = 1 | ||
- | </ | ||
- | |||
- | * Apply settings | ||
- | |||
- | <code bash> | ||
- | sysctl -p / | ||
- | </ | ||
- | |||
- | * Restart the network service | ||
- | |||
- | <code bash> | ||
- | service network restart | ||
- | </ | ||
- | ---- | ||
- | |||
- | ===== 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/ | ||
- | <code bash> | ||
- | [bash]# sestatus | ||
- | SELinux status: | ||
- | SELinuxfs mount: | ||
- | Current mode: | ||
- | </ | ||
- | |||
- | **Default VM Image directory: | ||
- | |||
- | ==== Changing the Default VM Image Path ==== | ||
- | * Create the directory | ||
- | <code bash> | ||
- | mkdir /vm-images | ||
- | </ | ||
- | |||
- | * Install the following package to enable use of the " | ||
- | |||
- | <code bash> | ||
- | yum install policycoreutils-python | ||
- | </ | ||
- | |||
- | * Set security context for the vm-images directory and everything that is created under it. | ||
- | |||
- | <code bash> | ||
- | semanage fcontext -a -t virt_image_t "/ | ||
- | </ | ||
- | |||
- | * Restore the security context (I don't know why you have to do this yet...but you do) | ||
- | |||
- | <code bash> | ||
- | restorecon -R /vm-images | ||
- | </ | ||
- | |||
- | * Verify | ||
- | |||
- | <code bash> | ||
- | ls -Z /vm-images | ||
- | </ | ||
- | |||
- | ---- | ||
- | |||
- | ===== 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 " | ||
- | |||
- | Common options: | ||
- | <code bash> | ||
- | General Options | ||
- | --name=NAME | ||
- | --ram=MEMORY | ||
- | --vcpus=VCPUS | ||
- | |||
- | Installation Method | ||
- | --cdrom=CDROM | ||
- | --location=LOCATION | ||
- | --pxe (Boot from network using PXE) | ||
- | --extra-args " | ||
- | |||
- | Storage Configuration | ||
- | --disk=DISKOPTS | ||
- | --disk path=/ | ||
- | --disk path=/ | ||
- | |||
- | Network | ||
- | --network bridge=br0 | ||
- | |||
- | Graphics | ||
- | --graphics=GRAPHICS | ||
- | --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 " | ||
- | <code bash> | ||
- | virt-install \ | ||
- | --name=centos1 \ | ||
- | --disk path=/ | ||
- | --vcpus=1 --ram=1024 \ | ||
- | --cdrom=/ | ||
- | --network bridge=br0 | ||
- | </ | ||
- | |||
- | === Connect to Guest for Installation === | ||
- | After the VM is created, if you are using the " | ||
- | |||
- | 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 " | ||
- | <code bash> | ||
- | virt-viewer --connect qemu:/// | ||
- | </ | ||
- | |||
- | * **Method 2: Open virt-manager** | ||
- | |||
- | Example open virt-manager and then the VM " | ||
- | <code bash> | ||
- | virt-manager | ||
- | </ | ||
- | * Select the VM | ||
- | * Click " | ||
- | |||
- | |||
- | Proceed with installation as normal. | ||
- | |||
- | ---- | ||
- | |||
- | ==== GUI: virt-manager ==== | ||
- | Installing locally via a GUI. | ||
- | |||
- | * Open a terminal and type: | ||
- | <code bash> | ||
- | virt-manager | ||
- | </ | ||
- | * The virt-manager GUI will open. | ||
- | * Select the name of the host (probably " | ||
- | * Click the " | ||
- | * Step 1 => | ||
- | * Name: Name the virtual machine | ||
- | * Choose how to install: Select the install method | ||
- | * Step 2 => | ||
- | * Browse for the media | ||
- | * OS Type/ | ||
- | * 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. |