KVM: Difference between revisions

From Alpine Linux
(few correction, try to make it more clear, toc + bridge for KVM)
(3 intermediate revisions by 3 users not shown)
Line 1: Line 1:
[https://www.linux-kvm.org/page/Main_Page KVM] is an open source virtualization solution in a kernel module. KVM can virtualize x86, PowerPC, and S390 guests.
[https://www.linux-kvm.org/page/Main_Page KVM] is an free and open source virtualization solution in a kernel module. Although it is often simply referred to as KVM, the actual hypervisor is [https://www.qemu.org QEMU]. QEMU runs from user-space, but can integrate with KVM, providing better performance by leveraging the hardware from kernel-space. QEMU can virtualize x86, PowerPC, and S390 guests, amongst others. [https://libvirt.org Libvirt] is a management framework that integrates with QEMU/KVM, [https://wiki.alpinelinux.org/wiki/LXC LXC], [https://wiki.alpinelinux.org/wiki/Xen_Dom0 Xen] and others.


== Installing KVM ==
== Installation ==
It is possible to install just qemu and libvirt for local management
The following commands provide '''libvirt''' as well as '''QEMU with emulation for x86_64''' and '''qemu-img''', a necessary component for using various disk formats such as qcow2. Without qemu-img, only raw disks are available. It can also convert images between several formats like vhdx and vmdk.
{{Cmd|sudo apk add qemu-system-x86_64 libvirt}}
{{Cmd|<nowiki># apk add libvirt-daemon qemu-img qemu-system-x86_64
but most users will probably want to manage it via a GUI with virt-manager (remotely or not).
# rc-update add libvirtd</nowiki>}}
{{Cmd|sudo apk add libvirt-daemon dbus polkit}}
If you want to use other disk image formats than raw, you need qemu-img
{{Cmd|sudo apk add qemu-img}}


Enable the KVM kernel module
== Networking ==
{{Cmd|sudo modprobe kvm-intel}}
By default, libvirt uses NAT for VM connectivity. If you want to use the default configuration, you need to load the tun module.
or
{{Cmd|# modprobe tun}}
{{Cmd|sudo modprobe kvm-amd}}
 
If you prefer bridging a guest over your Ethernet interface, you need to make a [https://wiki.alpinelinux.org/wiki/Bridge#Configuration_file bridge].
 
== Management ==
For (non-root) management, you will need to add your user to the libvirt group.
{{Cmd|# addgroup user libvirt}}


=== Service start on boot ===
You can use libvirt's virsh on the CLI. It can execute commands as well as run as an interactive shell. Read its manual page and/or use the "help" command for more info. Some basic commands are:
Set libvirtd and dbus to start on startup
{{Cmd|sudo rc-update add libvirtd
sudo rc-update add dbus}}


== Networking ==
{{Cmd|<nowiki>virsh help
by defaults kvm use bridge to nat vm, if you want network with the default configuration, you need to load the tun module
virsh list --all
{{Cmd|sudo modprobe tun}}
virsh start $domain
but if you prefer bridging kvm over your ethernet interface, you need to make a [https://wiki.alpinelinux.org/wiki/Bridge#Configuration_file bridge]
virsh shutdown $domain</nowiki>
}}


== Groups and user ==
The libvirt project provides a GUI for managing hosts, called virt-manager. It handles local systems as well as remote ones via SSH.
Add your user to the follwing groups so you can mange the vms
{{Cmd|<nowiki># apk add dbus polkit virt-manager
{{Cmd|sudo addgroup user kvm
# rc-update add dbus</nowiki>}}
sudo addgroup user qemu
sudo addgroup user libvirt}}


=== PolicyKit for Remote User ===
In order to use libvirtd to remotely control KVM over ssh PolicyKit needs a .pkla informing it that this is allowed.
In order to use libvirtd to remotely control KVM over ssh PolicyKit needs a .pkla informing it that this is allowed.
Write the following file to /etc/polkit-1/localauthority/50-local.d/50-libvirt-ssh-remote-access-policy.pkla
Write the following file to /etc/polkit-1/localauthority/50-local.d/50-libvirt-ssh-remote-access-policy.pkla

Revision as of 06:34, 28 April 2019

KVM is an free and open source virtualization solution in a kernel module. Although it is often simply referred to as KVM, the actual hypervisor is QEMU. QEMU runs from user-space, but can integrate with KVM, providing better performance by leveraging the hardware from kernel-space. QEMU can virtualize x86, PowerPC, and S390 guests, amongst others. Libvirt is a management framework that integrates with QEMU/KVM, LXC, Xen and others.

Installation

The following commands provide libvirt as well as QEMU with emulation for x86_64 and qemu-img, a necessary component for using various disk formats such as qcow2. Without qemu-img, only raw disks are available. It can also convert images between several formats like vhdx and vmdk.

# apk add libvirt-daemon qemu-img qemu-system-x86_64 # rc-update add libvirtd

Networking

By default, libvirt uses NAT for VM connectivity. If you want to use the default configuration, you need to load the tun module.

# modprobe tun

If you prefer bridging a guest over your Ethernet interface, you need to make a bridge.

Management

For (non-root) management, you will need to add your user to the libvirt group.

# addgroup user libvirt

You can use libvirt's virsh on the CLI. It can execute commands as well as run as an interactive shell. Read its manual page and/or use the "help" command for more info. Some basic commands are:

virsh help virsh list --all virsh start $domain virsh shutdown $domain

The libvirt project provides a GUI for managing hosts, called virt-manager. It handles local systems as well as remote ones via SSH.

# apk add dbus polkit virt-manager # rc-update add dbus

In order to use libvirtd to remotely control KVM over ssh PolicyKit needs a .pkla informing it that this is allowed. Write the following file to /etc/polkit-1/localauthority/50-local.d/50-libvirt-ssh-remote-access-policy.pkla

[Remote libvirt SSH access] Identity=unix-group:libvirt Action=org.libvirt.unix.manage ResultAny=yes ResultInactive=yes ResultActive=yes