How to set up Linux KVM

Posted by paul on 2014.05.12

How to set up Linux KVM (Kernel-based Virtual Machine)

2014.05.12 Mon

I used to have CentOS 5 running on a HP workstation box at home to host a few virtual Linux installations. I use them for dev and just messing around. When CentOS 6 came around, I discovered Xen support was dropped and KVM was the only open source option. Here's how I set it up.

Assumptions

  1. I'm CentOS 6.x on an HP Proliant MicroServer. The hostname is server01. Its IP is 192.168.11.222.
  2. You have .iso image file of CentOS 6.x, NOT just the DVD. If not, start downloading it now on your PC.
  3. Managing my CentOS servers and KVM host from a Mac.
  4. The CentOS server you are working on has no data you need and can be rebooted/reinstalled without causing any issue on your LAN. Once the KVM host is up and running, you will of course want to keep it running.

Initial install on the CentOS server

  1. On the CentOS server you will use as KVM host, edit /etc/motd and add following so that you are clearly reminded where you are when jumping between servers
  2. #################
    Welcome to server01.
    #################
    
  3. Run ifconfig to view current network setting
  4. [[email protected] ~]# ifconfig
    eth0      Link encap:Ethernet  HWaddr 00:11:22:33:44:55
              inet addr:192.168.12.222  Bcast:192.168.12.255  Mask:255.255.255.0
              inet6 addr: fe80::29c:2ff:fea0:4a8a/64 Scope:Link
              UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
              RX packets:159 errors:0 dropped:0 overruns:0 frame:0
              TX packets:50 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:1000
              RX bytes:29252 (28.5 KiB)  TX bytes:7780 (7.5 KiB)
              Interrupt:18
    
    lo        Link encap:Local Loopback
              inet addr:127.0.0.1  Mask:255.0.0.0
              inet6 addr: ::1/128 Scope:Host
              UP LOOPBACK RUNNING  MTU:16436  Metric:1
              RX packets:0 errors:0 dropped:0 overruns:0 frame:0
              TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:0
              RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)
    
  5. Install RPMs for KVM
  6. [[email protected] ~]# yum groupinstall "Virtualization"
    ...
    [[email protected] ~]# yum groupinstall "Virtualization Client"
    ...
    [[email protected] ~]# yum groupinstall "Virtualization Platform"
    ...
    [[email protected] ~]# yum install openssh-askpass
    ...
    
  7. Install RPMs for virt-manager
  8. [[email protected] ~]# yum install xorg-x11-xauth
    ...
    [[email protected] ~]# yum install dejavu-lgc-sans-fonts
    ...
    
  9. Start libvirtd service to auto start and and start it
  10. [[email protected] ~]# /etc/init.d/libvirtd start
    ...
    [[email protected] ~]# chkconfig libvirtd on
    ...
    
  11. Run ifconfig to view the updated network setting. Note the new network interface 'virbr0', starting at line 21..
  12. [[email protected] ~]# ifconfig
    eth0      Link encap:Ethernet  HWaddr 00:11:22:33:44:55
              inet addr: 192.168.12.222  Bcast:192.168.12.255  Mask:255.255.255.0
              inet6 addr: fe80::29c:2ff:fea0:4a8a/64 Scope:Link
              UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
              RX packets:23208 errors:0 dropped:0 overruns:0 frame:0
              TX packets:14939 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:1000
              RX bytes:28768875 (27.4 MiB)  TX bytes:2335569 (2.2 MiB)
              Interrupt:18
    
    lo        Link encap:Local Loopback
              inet addr:127.0.0.1  Mask:255.0.0.0
              inet6 addr: ::1/128 Scope:Host
              UP LOOPBACK RUNNING  MTU:16436  Metric:1
              RX packets:938 errors:0 dropped:0 overruns:0 frame:0
              TX packets:938 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:0
              RX bytes:1110736 (1.0 MiB)  TX bytes:1110736 (1.0 MiB)
    
    virbr0    Link encap:Ethernet  HWaddr AA:11:BB:33:CC:55
              inet addr:192.168.122.1  Bcast:192.168.122.255  Mask:255.255.255.0
              UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
              RX packets:0 errors:0 dropped:0 overruns:0 frame:0
              TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:0
              RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)
    
  13. Copy CentOS 6 ISO image file (ie: CentOS-6.5-x86_64-bin-DVD1.iso) to /var/lib/libvirt/images/. When you install CentOS KVM guest, it will be installed off of /var/lib/libvirt/images/CentOS-6.5-x86_64-bin-DVD1.iso.

Add Bridged network in KVM

  1. Source: http://thepoch.com/post/24055790979/so-you-want-a-centos-6-kvm-host-server
  2. You will dupe ifcfg-eth0 into ifcfg-bro0. Next you will edit both files. This enables Bridge network for KVM, allowing the KVM guests to be able to get actual IP. Without Bridged network, the KVM guests cannot access the network.
  3. Copy ifcfg-eth0 to ifcfg-br0.
  4. [[email protected] ~]# cp /etc/sysconfig/network-scripts/ifcfg-eth0 /etc/sysconfig/network-scripts/ifcfg-br0
    
  5. BEFORE editing ifcfg-eth0, it looks like this.
  6. [[email protected] ~]# more /etc/sysconfig/network-scripts/ifcfg-eth0
    DEVICE=eth0
    HWADDR=00:11:22:33:44:55
    NM_CONTROLLED=no
    ONBOOT=yes
    IPADDR=192.168.12.222
    BOOTPROTO=none
    NETMASK=255.255.255.0
    TYPE=Ethernet
    GATEWAY=192.168.12.1
    IPV6INIT=no
    USERCTL=no
    
  7. AFTER editing ifcfg-eth0, it looks like below. Lines 6, 8, & 10 are commented out. Line 14 is added.
  8. [[email protected] ~]# more /etc/sysconfig/network-scripts/ifcfg-eth0
    DEVICE=eth0
    HWADDR=00:11:22:33:44:55
    NM_CONTROLLED=no
    ONBOOT=yes
    #IPADDR=192.168.12.222
    BOOTPROTO=none
    #NETMASK=255.255.255.0
    TYPE=Ethernet
    #GATEWAY=192.168.12.1
    IPV6INIT=no
    USERCTL=no
    BRIDGE=br0
    
  9. Now edit ifcfg-br0 so that it looks like below. Lines 9 & 13 are added.
  10. [[email protected] ~]# more /etc/sysconfig/network-scripts/ifcfg-br0
    DEVICE=br0
    HWADDR=00:11:22:33:44:55
    NM_CONTROLLED=no
    ONBOOT=yes
    IPADDR=192.168.12.222
    BOOTPROTO=none
    NETMASK=255.255.255.0
    TYPE=Bridge
    GATEWAY=192.168.12.1
    IPV6INIT=no
    USERCTL=no
    DEFROUTE=yes
    
  11. Restart network service with command: service network restart
  12. Run ifconfig and you should see something like this below. Note the network interface "br0".
  13. [[email protected] ~]# ifconfig
    br0       Link encap:Ethernet  HWaddr 00:11:22:33:44:55
              inet addr:192.168.12.222  Bcast:192.168.12.255  Mask:255.255.255.0
              inet6 addr: fe80::29c:2ff:fea0:4a8a/64 Scope:Link
              UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
              RX packets:51 errors:0 dropped:0 overruns:0 frame:0
              TX packets:46 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:0
              RX bytes:4461 (4.3 KiB)  TX bytes:4212 (4.1 KiB)
    
    eth0      Link encap:Ethernet  HWaddr 00:11:22:33:44:55
              inet6 addr: fe80::29c:2ff:fea0:4a8a/64 Scope:Link
              UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
              RX packets:65 errors:0 dropped:0 overruns:0 frame:0
              TX packets:16 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:1000
              RX bytes:8869 (8.6 KiB)  TX bytes:2600 (2.5 KiB)
              Interrupt:18
    
    lo        Link encap:Local Loopback
              inet addr:127.0.0.1  Mask:255.0.0.0
              inet6 addr: ::1/128 Scope:Host
              UP LOOPBACK RUNNING  MTU:16436  Metric:1
              RX packets:1172 errors:0 dropped:0 overruns:0 frame:0
              TX packets:1172 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:0
              RX bytes:1154672 (1.1 MiB)  TX bytes:1154672 (1.1 MiB)
    
    virbr0    Link encap:Ethernet  HWaddr AA:11:BB:33:CC:55
              inet addr:192.168.122.1  Bcast:192.168.122.255  Mask:255.255.255.0
              UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
              RX packets:0 errors:0 dropped:0 overruns:0 frame:0
              TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:0
              RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)
    ...
    

Install XQuartz on your Mac

My linux servers are headless, meaning no monitor is attached, and I manage them from my Mac via ssh connection. Running virt-manager to manage KVM however requires X window interface. In order to do this I needed to install XQuartz which is an open source tool.

  1. Download and install XQuartz on your Mac.
  2. Open Terminal window on your Mac.
  3. ssh -Y [email protected]
  4. Run virt-manager in the Terminal window.
  5. Here's what the Terminal screen would look like. Note the command issued in line 7.
  6. paul-Mac:~ paul$ ssh -X [email protected]
    [email protected]'s password:
    Last login: Fri May  9 16:44:30 2014 from 192.168.11.23
    #################
    Welcome to server01
    #################
    [[email protected] ~]# virt-manager
    [[email protected] ~]#
    
  7. XQuartz starts up and virt-manager window opens up. Here's what it looks like. Your window won't show any KVM guests at this point though.
  8. Next I will explain how you can install a virtual CentOS server in the KVM.

Creating a new KVM guest running CentOS 6.x via virt-manager

  1. Before creating any KVM guests, create a folder on your KVM Host's storage to keep virtual HDs to be used by the KVM guests. I prefer something like /vm/. KVM Host by default recommends /var/lib/libvirt/images/.
  2. Try creating a new KVM guest and power it on.
  3. Click on 'New'
  4. Give it a name. I like to use the hostname the CentOS will get. I will call this test100
  5. Since it will be installed off of a .iso file stored on the server01 itself, I can leave 'Local install media (ISO image or CDROM)' selected as it is.
  6. Forward
  7. Click on Browse to look for CentOS-6.5-x86_64-bin-DVD1.iso.
  8. In 'Default' Storage Pool, you should see CentOS-6.5-x86_64-bin-DVD1.iso.
  9. Click on CentOS-6.5-x86_64-bin-DVD1.iso and then 'Choose Volume'.
  10. For OS type and Version, I just leave it as 'Generic'. You can certainly choose 'Linux' and 'Red hat Enterprise Linux 6'.
  11. Adjust RAM and CPUs to your preference.
  12. Forward
  13. UNcheck 'Allocate entire disk now'.
  14. Change disk image size from 8.0GB to your preference.
  15. Click on Browse to select /vm/ and assign a file name (ex.: test100.img). More simply, you can simply type in /vm/test100.img. I use .img extension to indicate it's a disk image.
  16. Forward
  17. Check 'Customize configuration before install'
  18. Finish
  19. In this next window, you are seeing parameters of the virtual machine you are about to install CentOS on.
  20. Click on 'Display VNC'
  21. Change value of Keymap to en-us. If you don't set this value, keyboard will not work within the KVM guest.
  22. Apply
  23. Begin Installation
  24. Next screen is virtual screen of the KVM guest.
  25. BEFORE clicking within the virtual screen, change viewing preference by going to View | Scale Display | Always. Without this change, the virtual screen will cut off some areas and render the screen unusable.
  26. Note some idiosyncracies when going in and out of the virtual screen. You may end up with mouse pointer that is off. Or when the mouse is captured by the virtual screen, the virtual screen may not release the control of keyboard/mouse back to your Mac OS. To fix this, use Cmd+tab key combination on your Mac keyboard to switch in and out of the virtual screen. You may need to switch in and out twice to get this working. You will use this virtual screen/keyboard/mouse only while installing the OS and initial network configuration. Your keyboard's tab, arrow keys, and spacebar are all you need to navigate CentOS installation GUI. After this, you will access it via CLI over ssh connection. Best way to avoid these idiosyncracies is just staying in the virtual screen until the KVM guest is installed.
  27. Now click within the virtual screen.
  28. Under 'Disk Found', select 'Skip' using keyboard only (Tab and Enter).
  29. Now you can use keyboard and mouse to proceed with installing the virtual CentOS 6.x.
  30. From here, the install process is just the same as when you are installing CentOS on a physical computer.
  31. Under 'Storage Device Warning', select 'Yes, discard any data'.
  32. For the type of installation, make sure to pick 'Basic Server'. You could pick 'Database Server' or 'Web Server'. However you should not pick anything that has GUI interface, meaning do not pick 'Desktop' or 'Minimal Desktop'. Using GUI interface on a virtual KVM guest will be slow. Best way to interface with a KVM guest is via CLI (ssh connection from Terminal window).
  33. Keeping clicking on 'Next' to proceed with the installation.
  34. After a while, the KVM guest 'test100' will be ready.

Let's say you had to close XQuartz app before the installation/initial-configuration was done. Here's how you can reconnect with virtual screen of 'test100'.

  1. From Mac run 'ssh -X [email protected]' into the KVM Host and run 'virt-manager'. You will see 'Virtual Machine Manager'.
  2. Click on the icon 'test100' and click on 'Open'. Or double click on 'test100'.
  3. A new window will open, showing the console of the test100 computer.
  4. Click in the virtual screen to start interfacing with the 'test100'.

In case you are not familiar with CentOS 6, I will explain how to set up static IP on it.

  1. At this point, you probably want to interact with the newly installed KVM guest via the virt-manager (aka Virtual Machine Manager). As soon as you configure static IP on the KVM guest, ssh login from your Mac's Terminal.
  2. On the KVM guest, open /etc/sysconfig/network-scripts/ifcfg-eth0 to edit.
  3. Change ONBOOT value from no to yes
  4. Save and close file.
  5. To configure IP, run 'setup'.
  6. Using keyboard's tab and arrow keys, pick 'Network configuration'
  7. Go to 'Device configuration'.
  8. You should see 'eth0 (eth0) - Realtek ...' which is selected already.
  9. Enter to configure it.
  10. Uncheck 'Use DHCP'.
  11. Enter static IP, netmask and Default gateway IP.
  12. No need to enter any for DNS server yet.
  13. Ok
  14. Save
  15. Save&Quit
  16. Quit
  17. Back at terminal, edit /etc/resolv.conf and enter at least 1 DNS server as shown below. Make sure to use the IP of your own DNS server.
  18. nameserver 192.168.11.1
    
  19. Back at terminal, restart network service (service network restart)
  20. Now you can ping a network host. The KVM guest works just like a regular, virtualized OS with regular network access.
  21. To continue configuring the new KVM guest, ssh log into from your Mac's Terminal.

That's it!