Introduction
KVM1 is part of linux kernel, and QEMU2 (Quick EMUlator) is an emulator. KVM by itself cannot provide the complete virtualization solution, it needs QEMU to provide full hypervisor functionality. QEMU can emulate cpu on its own, but with KVM, QEMU can achieve near native performance by executing the guest code directly on the host CPU.
So it’s best for them to work together.
tools you need
Use your own package manager to install these tools:
|
|
- kvm (Kernel-based Virtual Machine): Kernel module that handles CPU and memory communication
- qemu (Quick EMUlator): emulates many hardware resources – dick, network, usb…
- libvirt: an open-source API, daemon and management tool for managing platform virtualization. It can be used to manage KVM, Xen, VMware ESXi, QEMU and other virtualization technologies.
- virsh: comes with libvirt, command-line tools for communicating with libvirt
- virt-manager: GUI alternative to virsh, albeit less capable.
- virt-install: part of virt-manager project, create new VM guests
- virt-viewer: part of virt-manager project, UI for interacting with VMs via VNC/SPICE
- dnsmasq: light-weight DNS/DHCP server. Primarily used for allocating IPs to VMs.
- ebtables: used for setting up NAT networking the host
some setup
two problems
- by default, virt-manager talks to
qemu:///system
, and virsh talks toqemu:///session
(unless run as sudo). - when talking to qemu:///system, we need to input password every time, especially unpleasant experience when a cli tool like virsh.
for the first problem, we can tell virsh to use qemu:///system
by default
|
|
To solve the second problem, we can add a rule to polkit to allow our group (wheel
– administrator group) to use virt-manager or vish without being asked for password.
edit /etc/polkit-1/rules.d/xxx.rules
, your path may be different, put this in.
|
|
start services
|
|
add shrarefolders
Inside virtual machine manager, double click on one of your machine, then select view->details->Add Hardware
, set something like below:
The above setting will add a new device /ctf
in the virtual machine
if in ubuntu
sudo vim /etc/rc.local
|
|
After restarting this server, /ctf
will automatically be mounted on /home/sky/ctf
To make the user (actually kvm) writing the share folder same as the user at host (vm host):sudo vim /etc/libvirt/qemu.conf
, find two lines with user=xxx
and group=xxx
, change them to yourself (by default, xxx should be root
), then uncomment the two lines. for me, they are:
|
|
You may need to restart the libvirtd.service for this to take effect.
Also, you need to chown
the disk to the above user:group
: sudo chown sky:sky /var/lib/libvirt/images/ubt16-server.qcow2
create a VM
You can create VMs through virt-manager (gui) or virt-install.
clone a VM
- gui: virt-manager
- cli: virt-clone
useful virsh commands
|
|
rofi-vmrun.sh
rofi-vmrun.sh is a script that I use to quickly manage my virtual machines.
- alt+o
- open selected vm in virt-manager
- alt+r
- run/stop (toggle) selected vm
- enter
- run selected vm and open it in virt-manager