{"id":658,"date":"2011-12-09T07:37:23","date_gmt":"2011-12-09T00:37:23","guid":{"rendered":"http:\/\/blog.iwayvietnam.com\/tuanta\/?p=658"},"modified":"2011-12-13T17:29:18","modified_gmt":"2011-12-13T10:29:18","slug":"startup-guide-for-kvm-on-centos-6","status":"publish","type":"post","link":"https:\/\/blog.iwayvietnam.com\/tuanta\/2011\/12\/startup-guide-for-kvm-on-centos-6\/","title":{"rendered":"Startup Guide for KVM on CentOS 6"},"content":{"rendered":"<p>I recently made the leap from CentOS 5.6 to CentOS 6 on my primary KVM host, and had to modify how I setup the KVM host to begin hosting virtual machines. Below is a start to finish guide to get you hosting VMs using KVM. These instructions are very specific to CentOS 6.<\/p>\n<p>For this I assume you have setup your server using the \u201cMinimal\u201d option when installing CentOS 6. You must also have the Virtualization features enabled for your CPU. This is done in your host\u2019s BIOS.<\/p>\n<p>Optionally you can skip the first section,\u00a0<strong>Installing KVM<\/strong>, if you check all 4 \u201cVirtualization\u201d software categories during the install.<\/p>\n<h1 id=\"installing_kvm\">Installing KVM<\/h1>\n<p>If you choose the \u201cMinimal\u201d option during CentOS 6 then this step is necessary. To get the full set of tools there are 4 software groups to install\u2026<\/p>\n<ul>\n<li>Virtualization<\/li>\n<li>Virtualization Client<\/li>\n<li>Virtualization Platform<\/li>\n<li>Virtualization Tools<\/li>\n<\/ul>\n<p>To install run<\/p>\n<p><code>yum groupinstall \"Virtualization*\"<\/code><\/p>\n<p><strong>dejavu-lgc-sans-fonts<\/strong>\u00a0is necessary or all the fonts in virt-manager will show as squares<\/p>\n<p><code>yum install dejavu-lgc-sans-fonts<\/code><\/p>\n<p>Once the install is finished verify that the KVM kernel module is loaded.<\/p>\n<p><code>lsmod | grep kvm<\/code><\/p>\n<p>You should see either\u00a0<strong>kvm_intel<\/strong>\u00a0or\u00a0<strong>kvm_amd<\/strong>\u00a0depending on your host\u2019s CPU manufacturer.<\/p>\n<p>At this point I chose to reboot the server. This allows services to be started and udev rules for KVM to be applied. This will also allow dbus to create the machine-id file, otherwise you would see something like the below when running\u00a0<strong>virt-manager<\/strong><\/p>\n<p><code># virt-manager<br \/>\nXlib: \u00a0extension \"RANDR\" missing on display \"localhost:10.0\". process 1869: D-Bus library appears to be incorrectly set up; failed to read machine uuid: Failed to open \/var\/lib\/dbus\/machine-id\": No such file or directory See the manual page for dbus-uuidgen to correct this issue. D-Bus not built with -rdynamic so unable to print a backtrace Aborted<\/code><\/p>\n<p>If you receive that D-Bus error and would prefer not to restart then run this command to generate the necessary machine-id file<\/p>\n<p><code>dbus-uuidgen &gt; \/var\/lib\/dbus\/machine-id<\/code><\/p>\n<h1 id=\"final_configuration_steps\">Final configuration steps<\/h1>\n<p>The server I run KVM on is headless, but I still like using virt-manager. So we must install the necessary tools to do X11 forwarding through SSH.<\/p>\n<p><code>yum install xorg-x11-xauth<\/code><\/p>\n<p><code># If you plan to use VNC to connect to the virtual machine's console locally<br \/>\nyum install tigervnc<\/code><\/p>\n<p>Now when you connect through SSH be sure to pass the\u00a0<strong>-X<\/strong>\u00a0flag to enable X11 forwarding.<\/p>\n<h2 id=\"optional.3A_using_an_alternate_location_for_vm_images_with_selinux\">Optional: Using an alternate location for VM images with SELinux<\/h2>\n<p>With SELinux enabled, special steps must be taken to change the default VM store from<strong>\/var\/lib\/libvirt\/images<\/strong>. My particular server I choose to keep all images and ISOs for VMs under\u00a0<strong>\/vmstore<\/strong>. The steps below give your new store the correct security context for SELinux.<\/p>\n<p><code># this package is necessary to run semanage<br \/>\nyum install policycoreutils-pythonsemanage fcontext -a -t virt_image_t \"\/vmstore(\/.*)?\"<br \/>\nrestorecon -R \/vmstore<\/code><\/p>\n<p>To activate this store you must open\u00a0<strong>virt-manager<\/strong>, select your host, then do Edit-&gt; Host Details. Under the\u00a0<strong>Storage<\/strong>\u00a0tab you can add your new storage volume.<\/p>\n<h2 id=\"optional_.3A_network_bridging_for_virtual_machines\">Optional : Network Bridging for Virtual Machines<\/h2>\n<p>If you wish for your virtual machines to be accessible remotely then you must use network bridging to share your host\u2019s network interface with the virtual machines. The setup requires linking one of your host\u2019s physical interfaces with a bridge device. First copy your physical interface\u2019s ifcfg file to create the new bridge device, named\u00a0<strong>br0<\/strong>.<\/p>\n<p><code>cp \/etc\/sysconfig\/networking-scripts\/ifcfg-eth0 \/etc\/sysconfig\/networking-scripts\/ifcfg-br0<\/code><\/p>\n<p>Modify ifcfg-br0 to have the IP information in ifcfg-eth0 and remove, or comment out, that information in ifcfg-eth0. Below are examples of ifcfg-eth0 and ifcfg-br0. The highlighted lines are important.<\/p>\n<p><strong>\/etc\/sysconfig\/networking-scripts\/ifcfg-eth0<\/strong><\/p>\n<p><code>DEVICE=eth0<br \/>\nHWADDR=00:18:8B:58:07:3B<br \/>\nONBOOT=yes<br \/>\nBRIDGE=br0<\/code><\/p>\n<p><strong>\/etc\/sysconfig\/networking-scripts\/ifcfg-br0<\/strong><\/p>\n<p><code>DEVICE=br0<br \/>\nTYPE=Bridge<br \/>\nBOOTPROTO=static<br \/>\nONBOOT=yes<br \/>\nIPADDR=10.1.0.3<br \/>\nNETMASK=255.255.255.0<\/code><\/p>\n<p>Once those two files are configured restart the network service<\/p>\n<p><code>service network restart<\/code><\/p>\n<h2 id=\"optional.3A_managing_libvirt_with_standard_user_account\">Optional: Managing libvirt with standard user account<\/h2>\n<p>Beginning in CentOS 6 access to managing libvirt is handled by\u00a0<a href=\"http:\/\/wiki.libvirt.org\/page\/SSHPolicyKitSetup\" target=\"_blank\">PolicyKit<\/a>. It\u2019s always a good practice to do your daily administration tasks as some user besides root, and using PolicyKit you can give access to libvirt functions to a standard account.<\/p>\n<p>First we create the necessary config file to define the access controls. The file must begin with a numeric value and have the .pkla extension.<\/p>\n<p><code>vim \/etc\/polkit-1\/localauthority\/50-local.d\/50-libvirt-remote-access.pkla<\/code><\/p>\n<p>Here\u2019s an example of the file I used to give access to a single user. Be sure to put your desired username in place of\u00a0<strong>username<\/strong>\u00a0on the highlighted line.<\/p>\n<p><code>[libvirt Management Access]<br \/>\nIdentity=unix-user:username<br \/>\nAction=org.libvirt.unix.manage<br \/>\nResultAny=yes<br \/>\nResultInactive=yes<br \/>\nResultActive=yes<\/code><\/p>\n<ul>\n<li>You can optionally replace\u00a0<strong>Identity=unix-user:username<\/strong>\u00a0with\u00a0<strong>Identity=unix-group:groupname<\/strong>\u00a0to allow access to a group of users.<\/li>\n<\/ul>\n<p>Finally restart the libvirtd daemon to apply your changes.<\/p>\n<p><code>\/etc\/init.d\/libvirtd restart<\/code><\/p>\n<h1 id=\"creating_the_first_virtual_machine\">Creating the first virtual machine<\/h1>\n<p>You are now ready to create your virtual machines.<\/p>\n<h2 id=\"create_the_virtual_disk\">Create the virtual disk<\/h2>\n<p>With the version of virt-manager shipped with CentOS 6 you cannot create\u00a0<strong>qcow2<\/strong>\u00a0images from within the GUI. If you wish to create your new VM with a qcow2 format virtual disk you must do so from the command line, or see the next section for RPMs to upgrade virt-manager.<\/p>\n<p><strong>Update<\/strong>: Through some testing I\u2019ve found that performance can be greatly improved if the preallocation is set when creating a qcow2 image.<\/p>\n<p><code># With preallocation<br \/>\nqemu-img create -f qcow2 -o preallocation=metadata CentOS-6.0-x86_64-Template.qcow2 20G<\/code><\/p>\n<p><code># Without preallocation<br \/>\nqemu-img create -f qcow2 CentOS-6.0-x86_64-Template.qcow2 20G<\/code><\/p>\n<ul>\n<li><strong>NOTE:<\/strong>\u00a0Replace the filename \u201cCentOS-6.0-x86_64-Template\u201d with your desired name, and also replace \u201c20G\u201d with the desired max size of the virtual disk.<\/li>\n<\/ul>\n<p>Now when creating your virtual machine select to use an existing virtual disk.<\/p>\n<p style=\"text-align: right;\"><em>Source: itscblog.tamu.edu<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I recently made the leap from CentOS 5.6 to CentOS 6 on my primary KVM host, and had to modify how I setup the KVM host to begin hosting virtual machines. Below is a start to finish guide to get you hosting VMs using KVM. These instructions are very specific to CentOS 6. For this<a class=\"more-link\" href=\"https:\/\/blog.iwayvietnam.com\/tuanta\/2011\/12\/startup-guide-for-kvm-on-centos-6\/\">Continue reading <span class=\"screen-reader-text\">&#8220;Startup Guide for KVM on CentOS 6&#8221;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,61],"tags":[],"class_list":["post-658","post","type-post","status-publish","format-standard","hentry","category-tips","category-virtualization","entry"],"_links":{"self":[{"href":"https:\/\/blog.iwayvietnam.com\/tuanta\/wp-json\/wp\/v2\/posts\/658"}],"collection":[{"href":"https:\/\/blog.iwayvietnam.com\/tuanta\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.iwayvietnam.com\/tuanta\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.iwayvietnam.com\/tuanta\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.iwayvietnam.com\/tuanta\/wp-json\/wp\/v2\/comments?post=658"}],"version-history":[{"count":5,"href":"https:\/\/blog.iwayvietnam.com\/tuanta\/wp-json\/wp\/v2\/posts\/658\/revisions"}],"predecessor-version":[{"id":663,"href":"https:\/\/blog.iwayvietnam.com\/tuanta\/wp-json\/wp\/v2\/posts\/658\/revisions\/663"}],"wp:attachment":[{"href":"https:\/\/blog.iwayvietnam.com\/tuanta\/wp-json\/wp\/v2\/media?parent=658"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.iwayvietnam.com\/tuanta\/wp-json\/wp\/v2\/categories?post=658"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.iwayvietnam.com\/tuanta\/wp-json\/wp\/v2\/tags?post=658"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}