This short tutorial will help you setup your CentOS 7 as a host for KVM virtualization via NAT IP. Using this method, we can install many VMs on CentOS 7 using only 1 IP as we did the same thing with Proxmox before.
Preparation for Prerequisite softwares
- First, check if the current machine support hardware virtualization:
[bash]egrep -c ‘(vmx|svm)’ /proc/cpuinfo[/bash]
. The result should return >1 this case.
- Next, install required softwares for kvm:
[bash]sudo yum install kvm virt-manager libvirt virt-install qemu-kvm xauth dejavu-lgc-sans-fonts[/bash]
- Allow IP forwarding for networking:
[bash][root@germany ~]# echo "net.ipv4.ip_forward = 1"|sudo tee /etc/sysctl.d/99-ipforward.conf
net.ipv4.ip_forward = 1
[root@germany ~]# sysctl -p /etc/sysctl.d/99-ipforward.conf
net.ipv4.ip_forward = 1[/bash] - Setting up bridge networking between the host and guest VMs
- In the host machine, there is one real network interface (enp3s0 or eth0 or sth like that), we will need to edit this interface (/etc/sysconfig/network-scripts/ifcfg-enp3s0 or /etc/sysconfig/network-scripts/ifcfg-eth0) and comment out IP-related information as follows:
[bash]DEVICE=eth0
BOOTPROTO=none
ONBOOT=yes
HWADDR=C8:60:00:BE:3F:E5
#IPADDR=XXX.XXX.XXX.35
#NETMASK=255.255.255.255
#SCOPE="peer XXX.XXX.XXX.33"
IPV6INIT=yes
IPV6ADDR=aaaa:aaaa:aaaa:aaaa::2/64
IPV6_DEFAULTGW=aaaa::1
IPV6_DEFAULTDEV=eth0[/bash] - Then, we will need to create a new bridge network interface (/etc/sysconfig/network-scripts/ifcfg-virbr0) and put IP information with some additional information as follows:
[bash]DEVICE="virbr0"
TYPE=BRIDGE
ONBOOT=yes
BOOTPROTO=static
IPADDR=XXX.XXX.XXX.35
NETMASK=255.255.255.255
SCOPE="peer XXX.XXX.XXX.33"[/bash] - Then, restart the network service.
- In the host machine, there is one real network interface (enp3s0 or eth0 or sth like that), we will need to edit this interface (/etc/sysconfig/network-scripts/ifcfg-enp3s0 or /etc/sysconfig/network-scripts/ifcfg-eth0) and comment out IP-related information as follows:
- Finally, check the setup environment:
[bash]ENV[/bash]
Manage VMs using CLI
After completing the KVM installation, it’s time to start using the host. First thing we need to do is to create a new domain or VM.
Adding a new VM
To create a new virtual machine using the CLI, we need to know which template we will use to install the system. To get a list of templates that are known in our KVM installation, you can do the following:
[bash]root@germany ~ # virt-install –os-variant=list
win7: Microsoft Windows 7
vista: Microsoft Windows Vista
winxp64: Microsoft Windows XP (x86_64)
winxp: Microsoft Windows XP
win2k: Microsoft Windows 2000
win2k8: Microsoft Windows Server 2008
win2k3: Microsoft Windows Server 2003
openbsd4: OpenBSD 4.x
freebsd8: FreeBSD 8.x
freebsd7: FreeBSD 7.x
freebsd6: FreeBSD 6.x
solaris9: Sun Solaris 9
solaris10: Sun Solaris 10
opensolaris: Sun OpenSolaris
netware6: Novell Netware 6
netware5: Novell Netware 5
netware4: Novell Netware 4
msdos: MS-DOS
generic: Generic
debianwheezy: Debian Wheezy
debiansqueeze: Debian Squeeze
debianlenny: Debian Lenny
debianetch: Debian Etch
fedora19: Fedora 19
fedora18: Fedora 18
fedora17: Fedora 17
fedora16: Fedora 16
fedora15: Fedora 15
fedora14: Fedora 14
fedora13: Fedora 13
fedora12: Fedora 12
fedora11: Fedora 11
fedora10: Fedora 10
fedora9: Fedora 9
fedora8: Fedora 8
fedora7: Fedora 7
fedora6: Fedora Core 6
fedora5: Fedora Core 5
mageia1: Mageia 1 and later
mes5.1 : Mandriva Enterprise Server 5.1 and later
mes5: Mandriva Enterprise Server 5.0
mandriva2010: Mandriva Linux 2010 and later
mandriva2009: Mandriva Linux 2009 and earlier
rhel7: Red Hat Enterprise Linux 7
rhel6: Red Hat Enterprise Linux 6
rhel5.4 : Red Hat Enterprise Linux 5.4 or later
rhel5: Red Hat Enterprise Linux 5
rhel4: Red Hat Enterprise Linux 4
rhel3: Red Hat Enterprise Linux 3
rhel2.1 : Red Hat Enterprise Linux 2.1
sles11: Suse Linux Enterprise Server 11
sles10: Suse Linux Enterprise Server
opensuse12: openSuse 12
opensuse11: openSuse 11
ubuntusaucy: Ubuntu 13.10 (Saucy Salamander)
ubunturaring: Ubuntu 13.04 (Raring Ringtail)
ubuntuquantal: Ubuntu 12.10 (Quantal Quetzal)
ubuntuprecise: Ubuntu 12.04 LTS (Precise Pangolin)
ubuntuoneiric: Ubuntu 11.10 (Oneiric Ocelot)
ubuntunatty: Ubuntu 11.04 (Natty Narwhal)
ubuntumaverick: Ubuntu 10.10 (Maverick Meerkat)
ubuntulucid: Ubuntu 10.04 LTS (Lucid Lynx)
ubuntukarmic: Ubuntu 9.10 (Karmic Koala)
ubuntujaunty: Ubuntu 9.04 (Jaunty Jackalope)
ubuntuintrepid: Ubuntu 8.10 (Intrepid Ibex)
ubuntuhardy: Ubuntu 8.04 LTS (Hardy Heron)
virtio26: Generic 2.6.25 or later kernel with virtio
generic26: Generic 2.6.x kernel
generic24: Generic 2.4.x kernel[/bash]
[bash][root@germany ~]$ sudo semanage fcontext -a -t virt_image_t "/vm(/.*)?"
[root@germany ~]$ sudo restorecon -R /vm[/bash]
Example to add a windows-guest
[bash][root@germany ~]$ virt-install -n VMWin2k8 –description "Win 2k8R2 VM" –ram=2048 –vcpus=2 –os-variant=win2k8 –accelerate –hvm -c /home/virtualization/isoes/Win2k8.iso –network bridge:br0 –graphics vnc,listen=XXX.XXX.XXX.XXX,port=5900,password=abc123 –disk path=/home/virtualization/images/vmwin2k8.img,size=30
Starting install…
Creating storage file vmwin2k8.img | 30 GB 00:00:00
Creating domain… | 0 B 00:00:01
Domain installation still in progress. Waiting for installation to complete.[/bash]
After launching the above command, you should be able to connect with VNC to the host and get on the console-display of the VM. The console displays what would normally, on a physical machine, appear on the attache monitor.
By default, VNC will use the first available screen on port 5900. To be sure which screen is used, we can use virsh to show the attached console-screens for VNC:
[bash][root@germany ~]$ virsh vncdisplay VMWin2k8
:0[/bash]
[bash][root@germany ~]$ netstat -tln|grep :59
tcp 0 0 0.0.0.0:5900 0.0.0.0:* LISTEN[/bash]
From this point, we can complete the windows installation as if it would be a normal physical system:
After completing the installation with VNC, we end up with a Windows-VM that is running on our KVM-host:
As for the networking part, we use the earlier created bridge (virbr0) to do NAT. This means that the KVM-host NAT’s all our connections to the real network connected to the KVM-host. If DHCP is active on that network, it can be used in the VM. Otherwise you will have to configure a static IP in the same subnet.
Example to add a Linux-guest
To add a Linux guest, next to the already added Windows-guest is quite similar:
[bash][root@germany ~]$ sudo virt-install –connect qemu:///system -n vmdeb7 -r 512 –vcpus=1 –disk path=/var/lib/libvirt/images/vmdeb7.img,size=2 –graphics vnc,listen=0.0.0.0 –noautoconsole –os-type linux –os-variant debianwheezy –accelerate –network=bridge:virbr0 –hvm –cdrom /tmp/debian-7.5.0-amd64-netinst.iso
Startinginstall…
Allocating ‘vmdeb7.img’ | 2.0 GB 00:00:00
Creating domain… | 0 B 00:00:02
Domain installation still in progress. You can reconnect to the console to complete the installation process.[/bash]
To know which VNC-display number (and port) is used for a certain VM, the same command as used earlier should do:
[bash][root@germany ~]$ sudo virsh vncdisplay vmdeb7
:1[/bash]
Above command gives :1 as result, meaning that the guest vmdeb7 can be contacted with VNC on port 5901:
After finishing the installation, we end up with a Linux guest running on top of our KVM-host. Which Linux distro we are using doesn’t matter since we’re doing full virtualization.
[bash]root@deb:~# uname -a
Linux deb 3.2.0-4-amd64 #1 SMP Debian 3.2.60-1+deb7u3 x86_64 GNU/Linux
root@deb:~# cat /proc/cpuinfo |grep model
model : 13
model name : QEMU Virtual CPU version 1.5.3[/bash]
[bash]root@deb:~# ping -c1 8.8.8.8
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_req=1 ttl=128 time=23.8 ms
— 8.8.8.8 ping statistics —
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 23.855/23.855/23.855/0.000 ms
root@deb:~# ip a show eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 52:54:00:33:65:75 brd ff:ff:ff:ff:ff:ff
inet 192.168.202.140/24 brd 192.168.202.255 scope global eth0
inet6 fe80::5054:ff:fe33:6575/64 scope link
valid_lft forever preferred_lft forever[/bash]
More KVM actions
Besides creating VM’s, it’s a good thing to know some basic operations regarding VM-managent.
List the active virtual machines:
[bash][root@germany ~]$ sudo virsh –connect qemu:///system list
IdNameState
—————————————————-
7vmwin7running
8vmdeb7running[/bash]
Get more information about a guest:
[bash][root@germany ~]$ sudo virsh dominfo vmwin7
Id: 7
Name: vmwin7
UUID: f913c6fa-b597-437d-b6f5-797314e34847
OS Type: hvm
State: running
CPU(s): 2
CPU time: 20955.1s
Max memory: 1048576 KiB
Used memory: 1048576 KiB
Persistent: yes
Autostart: disable
Managed save: no
Security model: selinux
Security DOI: 0
Security label: system_u:system_r:svirt_t:s0:c638,c926 (enforcing)[/bash]
Stop a running guest:
To stop a running VM in a clean way (as you would press the power button to start the shutdown sequence):
[bash][root@germany ~]$ sudo virsh –connect qemu:///system shutdown vmdeb7
Domain vmdeb7 is being shutdown[/bash]
[bash]root@deb:~#
Broadcast message from root@deb (Wed Aug 27 09:09:16 2014):
Power button pressed
The system is going down for system halt NOW![/bash]
[bash][root@germany ~]$ sudo virsh –connect qemu:///system destroy vmdeb7
Domain vmdeb7 destroyed[/bash]
Start a guest
[bash][root@germany ~]$ sudo virsh –connect qemu:///system start vmdeb7
Domain vmdeb7 started[/bash]
Delete a guest
First we need to make sure that the guest is stopped before it can be deleted. In case you don’t want the virtual disk image anymore either, you’ll have to delete it manually after undefining the guest.
[bash][root@germany ~]$ sudo virsh –connect qemu:///system destroy vmcen6
Domain vmcen6 destroyed
[root@germany ~]$ sudo virsh –connect qemu:///system undefine vmcen6
Domain vmcen6 has been undefined
[root@germany ~]$ sudo rm -f /var/lib/libvirt/images/vmcen6.img[/bash]
[bash][root@germany ~]$ sudo virsh pool-refresh default
Pooldefaultrefreshed[/bash]
Automatically let a guest start when the host starts
When rebooting your host, you probably want some or all the guests that are defined on that host to start at the same time. By default, the guest are not automatically started.
[bash]sudo virsh –connect qemu:///system autostart vmdeb7
sudo virsh –connect qemu:///system dominfo vmdeb7|grep Auto[/bash]
Using KVM with the virt-manager GUI
Starting the GUI
Managing KVM with the CLI is not so difficult and it can be ver handy to script certain day-to-day tasks. Sometimes, you just need to keep an overview and require a little more user-friendliness. For that, you can use virt-manager, which is a graphical interface for libvirt and is mainly built for KVM. When you want to manage your guest with virt-manager, you can either do it on the host itself, by starting an X-server locally or use X11 forwarding on a headless server (more information here).
Make sure that you have enough permissions to use virt-manager and simply execute virt-manager from the command line:
[bash]virt-manager[/bash]
Basic actions
From the initial start-up screen, you can immediately see a list of configured guests on this host and take actions on them like: Run, Pause, Shutdown, Reboot, Force off,…
When selecting a guest, you can also click on Open to display the console as we did earlier using VNC:
Advanced actions
Other possibilities using the virt-manager interface:
Connect to another host-system to manage the VM’s running there, using File -> Add connection (like using the –connect on the CLI)
Migrate a VM to another KVM-host: right click on the VM and choose Migrate…
Clone a VM to the same or another KVM-host: right click on the VM and choose Clone…
As you can see, the virt-manager interface is not very complicated and most of the basic tasks don’t need any explanation.
After completing all of the above steps, basic installation and using KVM shouldn’t have any secrets anymore for you. The next thing to do is experiment and test a little more with KVM and hopefully start to use it in your production environment.