This article is a compilation of information accumulated during the use of the Qemu-KVM hypervisor. I want to share the knowledge that I have at the moment. I hope that my article will benefit those who are only going to use the Qemu-KVM hypervisor or are already using it. And another thing: the article is not for linux beginners (elementary things will not be considered here).
Much has been written about this virtualization system in the network. But when you really start working with it, you encounter a lack of information and practical examples of application. So let's get started.
Incoming task. The computer was allocated as a test station - to check the performance of backup copies of databases, installed software, build msi packages and other very diverse tasks. Computer Configuration:
- Atlon X2 245 processor
- RAM 4 gigabytes
- 500 gigabyte hard drive
- motherboard ASUS M4N68T-M LE.
After a brief reflection, it was decided to use the computer as a platform for working with virtual machines. CPU hardware virtualization supported. In this regard, it was decided to install another 1 hard drive. Rummaging in the "bins of the motherland" was found a disk of 80 gigabytes. It was added to the configuration.
')
With the computer sorted out. Now in line is the hypervisor. I wanted to work with such a platform, which can then be used in a corporate environment. Therefore, virtualbox, microsoft virtual pc, vmware workstations were not suitable. The following question arose - which system to choose:
- Microsoft hyper-v does not fit - paid. The company in which I work uses only licensed software. Therefore, no one will allocate a server license for my purposes.
- VMWARE ESXi does not know the SATA controller located on the motherboard (since it was developed for server systems).
- Qemu-kvm is a freely developed hypervisor that supports hardware virtualization. It can be installed in any modern Linux distribution. This is for me, and we take it.
Choosing a Linux distribution. I use the default Debian. You can choose any distribution you like (ubuntu, Fedora, Opensuse, Archlinux). The qemu-kvm hypervisor is in any of the above distributions, and Google is replete with articles on how to install it.
Get down to business. I will not describe the installation of the operating environment. I will only make a reservation that during the installation of the operating environment, the larger hard disk did not touch. His time will come. How to install a hypervisor on Debian is very well described
here . Personally, I put qemu-kvm libvirt-bin.
Hypervisor set, now a little about the internal structure. There are two directories that are worth a look:
/ etc / libvirt / - configuration files are mainly stored here
/ var / lib / libvirt / - images of hard drives, instant snapshots of the system and much more will be stored here.
Our hypervisor is installed.
Now a little about the settings. Qemu-kvm does not work directly with a network card on a physical computer. Therefore you need to configure the bridge. What we do: open the file / etc / network / interfaces and bring it to the following form:
# This file is the network interfaces available on your system
# and how to activate them. For more information, see interfaces (5).
# The loopback network interface
auto lo br0
iface lo inet loopback
# Set up interfaces manually, avoiding conflicts with eg, network manager
iface eth0 inet manual
# Bridge setup
iface br0 inet static
bridge_ports eth0
address xxxx.xxx.xxx.xxx
broadcast xxxx.xxx.xxx.xxx
netmask xxxx.xxx.xxx.xxx
gateway xxxx.xxx.xxx.xxx
bridge_stp off
bridge_fd 0
bridge_maxwait 0
More information
here .
Next, save the file and reboot the computer.
Oh, miracle! Hypervisor installed!
Then the question arises: how to manage the server? You can manage Qemu-kvm with two programs: virt-manager and virtinst.
Virt-manager.This program is designed for a graphical interface. It supports both remote control of virtual machines and local. But she has a huge minus - analogs for windows simply do not.
How I personally got out of the situation. I installed the LXDE GUI and the xrdp server, thanks to such a simple set of programs I did not have to physically go to the computer (it hurts him a lot). I just connected through a standard RDP client which is in windows. But this is an additional waste of computer resources.
If you installed virt-manager, it automatically creates:
storage for images of virtual machines on the path / var / lib / libvirt / images
virtual network interface default.
Therefore, you need to mount a hard disk with a large amount into the / var / lib / libvirt / images directory.
Virtinst.This is a console utility. No graphics, solid command line and saving system resources.
If you decide to use console management, you will need to manually create a repository of virtual machine images. This is done as follows. By ssh connect to the server. Log in as root.
You can choose any place for storage:
- you can mount the hard disk in a directory and specify it as a storage
- You can simply select the device storage.
I was more attracted by the directory option (it's easier to copy virtual machine disks). So what I did was: formatted a 500 gigabyte disk into ext4 format (when btrfs is completed, it is better to use it). Created the / etc / libvirt / images directory and mounted it there. Do not forget to add a line for automatic mounting in fstab. Then in the console type vitsh and press Enter. It looks like this:
root @ kvm: / home / firsachi # virsh
Welcome to virsh, the virtualization interactive terminal.
Type: 'help' for help with commands
'quit' to quit
virsh #
Now, if we give the help command, we can see the list of shell commands. We are interested in whether there is ready storage. To do this, enter the command pool-list --all
virsh # pool-list - all
- Name State Autostart
If you just enter the pool-list, then you will be shown only active storage, and you need to see them all.
Create a pool
virsh # pool-define-as storage dir --target / etc / libvirt / images /We say that the pool starts automatically
virsh # pool-autostart storageWe start the pool
virsh # pool-start storageNow check
virsh # pool-list –allThe conclusion should be such
virsh # pool-list - all
Name State Autostart
- storage active yes
A little about the teams recommend reading
here .
If you do not create a repository for virtual machine disk images, then in order for the disks to lie in one place, you will need to specify the full path to the image when you create it (write a lot), and so the image will be created in the specified pool or if you have one, his name is optional. The pool configuration file will be located in the / etc / libvirt / storage / directory. This is the .xml file. Exit the virsh by typing the exit command.
In principle, our hypervisor is installed and can be used. But there is still a small trifle, which I would like to mention. Namely, how Qemu-kvm works.
A virtual machine running on it sends commands to the physical processor directly via a loadable module (kvm-amd or kvm-intel). It must be one of the modules that corresponds to the manufacturer of the processor (Intel or AMD).
This module is loaded during system startup. However, this is not my method. I rebuild the system kernel in order to embed the necessary module (as an option when rebuilding) directly into the kernel.
But this is not the only modernization that I spend. In addition, I do the following:
- turn off sound support (this is a server, not a workstation);
- I do not use the IPv6 protocol (it is not used in my network);
- I disable support for wifi, wmax network cards and all that is connected.
I do not like when the extra functions burden the core with an extra load.
In building my own kernel, these
first and
second articles helped me.
I'll run a little forward. Many people on the Internet complained that the virtio network card model does not work correctly. This is an explanation, and quite simple. Drivers for this device are in the experimental stage. But virtio storage works fine.
Now we start working with virtual machines. Create our first virtual machine:
virt-install --connect qemu: /// system \
--name comp1 \
--ram 512 \
--vcpus = 1 \
--disk pool = storage, cache = none, size = 20, format = qcow2 \
--disk /home/firsachi/Winxp.iso,device=cdrom \
--bridge = br0, model = e1000 \
--os-type = windows
--os-variant = winxp \
--graphics vnc, port = 5901, listen = 0.0.0.0Some details I want to clarify:
pool = storage we specify the pool in which you want to create a disk;
cache = none, this means that disk write caching is disabled. In my case, this is an img image. When write caching is enabled, the time for accessing the virtual machine disk is doubled;
size = 20 disk size in gigabytes;
format = qcow2 is the format of the disk image. As I understand it, only it supports system snapshots;
model = e1000 model of the Intel gigabit network card (the default is the hundred-megabyte rtl8139);
port = 5901 port, which can be accessed using Ultra VNC Viewer;
listen = 0.0.0.0 permission to connect from all IP (by default, only localhost listens).
Installation can be made directly to the device. It will look like this:
virt-install --connect qemu: /// system \
--name comp1 \
--ram 512 \
--vcpus = 1 \
--disk / dev / sdb, format = qcow2 \
--disk /home/firsachi/Winxp.iso,device=cdrom \
--bridge = br0, model = e1000 \
--os-type = windows
--os-variant = winxp \
--graphics vnc, port = 5901, listen = 0.0.0.0Where
sdb should be replaced with your device.
If everything went well, then to connect to the console of our virtual machine, you need to install Ultra VNC Viewer on your computer. In the connection, you need to specify the IP address of the server and the port, or the domain name of the server and the port.
How is the installation of Windows, I hope everyone knows.
Now about the virtual machine drivers. Driver installation requires the following disk images: virtio-win-0.1-30.iso and viostor-0.1-30-floppy.img
The last disk is not necessary, it is needed only if you are going to install windows xp or windows 2003 server on virtio storage (by the way, if you do this, the operating system is faster).
Here it is written how to connect and disconnect
cdrom .
There is one more bonus: external devices can be connected to the virtual machine (for example, a physical hard disk or usb drive). We go in virsh. The command will look like this:
virsh # attach-disk comp1 / dev / sdc vdv --type diskWhere:
- comp1 is the name of the virtual computer to which we connect the disk.
- / dev / sdc - path to the device on the physical computer.
- Vdv - where we connect to the virtual machine.
- --type - type of disk.
Unmount disk:
virsh # deatach-disk comp1 vdvThis
article will help a lot. I also recommend looking
here .
Where do I apply this hypervisor? I constantly have a domain controller on it. And it works stably, without failures. Authorization in the domain occurs without problems. In parallel, I can run a maximum of 2 virtual machines.
Restore a virtual machine on another server.One sunny and cloudless day I thought: what will happen if I have to transfer it to another server. There is a virtual machine migration mode in the hypervisor, but there is no place to check it - there is no such second computer.
Therefore it was necessary to proceed from the worst option. Suppose:
- The computer on which the virtual machine was running burned out (Atlon X2 245 processor).
- Once a week, the virtual machine shuts down and a backup copy of the configuration file and disk image is made.
Hence my actions in this case. For the experiment, I brought my laptop from home. It has an i5-3210M processor, linux distribution OpenSuse (for my laptop is more compatible with the "iron").
I installed Qemu-KVM on it, moved the virtual machine configuration file and disk image to it. In the configuration file, edited the path to the virtual machine disk, rebooted the laptop. And, lo and behold! The hypervisor not only saw my virtual machine, but also launched it.
Create snapshots of virtual machines.Qemu-KVM supports the creation of snapshots of virtual machines. I will give the simplest example. From the superuser, go to virsh and execute the following command:
virsh # snapshot-create-as namename is the name of the virtual machine.
After the snapshot of the virtual machine is taken, the backup copies of the configuration files will be stored in the / var / lib / libvirt / qemu / snapshot / directory. But that's where the data of the virtual machine disk is located - I do not know yet.
You can view the pictures with the following command:
virsh # snapshot-list name
Name Creation Time State
- 1360593244 2013-02-11 16:34:04 +0200 running
1360594479 2013-02-11 16:54:39 +0200 running
You can restore from a photo like this:
virsh # snapshot-revert name 1360593244You can delete an unnecessary image as follows:
virsh # snapshot-delete name 1360593244That's how we live now: the Qemu-KVM hypervisor, the virtual domain controller, and I am pleased with the work done.
Thanks to everyone who read to the end. I hope my thoughts were helpful.