- Enable kvm in linux
- Contents
- Checking support for KVM
- Hardware support
- Kernel support
- Para-virtualization with Virtio
- Kernel support
- List of para-virtualized devices
- How to use KVM
- Tips and tricks
- Nested virtualization
- Enabling huge pages
- Ubuntu Documentation
- Installation
- Pre-installation checklist
- Check that your CPU supports hardware virtualization
- Use a 64 bit kernel (if possible)
- Installation of KVM
- Install Necessary Packages
- Add Users to Groups
- Verify Installation
- Optional: Install virt-manager (graphical user interface)
- Version-specific Notes
- 8.10 (Intrepid) Notes
- 11.10 (Oneric) Notes
- How to enable KVM virsh console access for Ubuntu Linux VM
- Our sample setup
- How do I setup console access of Ubuntu 16.04 KVM Guest
- Use ssh login
- Use vnc login
- Configure a serial console in the Ubuntu guest
- How do I access console of Ubuntu 16.04 KVM Guest from KVM host
Enable kvm in linux
KVM, Kernel-based Virtual Machine, is a hypervisor built into the Linux kernel. It is similar to Xen in purpose but much simpler to get running. Unlike native QEMU, which uses emulation, KVM is a special operating mode of QEMU that uses CPU extensions (HVM) for virtualization via a kernel module.
Using KVM, one can run multiple virtual machines running unmodified GNU/Linux, Windows, or any other operating system. (See Guest Support Status for more information.) Each virtual machine has private virtualized hardware: a network card, disk, graphics card, etc.
Differences between KVM and Xen, VMware, or QEMU can be found at the KVM FAQ.
This article does not cover features common to multiple emulators using KVM as a backend. You should see related articles for such information.
Contents
Checking support for KVM
Hardware support
KVM requires that the virtual machine host’s processor has virtualization support (named VT-x for Intel processors and AMD-V for AMD processors). You can check whether your processor supports hardware virtualization with the following command:
If nothing is displayed after running either command, then your processor does not support hardware virtualization, and you will not be able to use KVM.
Kernel support
Arch Linux kernels provide the required kernel modules to support KVM.
- One can check if the necessary modules, kvm and either kvm_amd or kvm_intel , are available in the kernel with the following command:
The module is available only if it is set to either y or m .
- Then, ensure that the kernel modules are automatically loaded, with the command:
If the command returns nothing, the module needs to be loaded manually, see Kernel modules#Manual module handling.
Para-virtualization with Virtio
Para-virtualization provides a fast and efficient means of communication for guests to use devices on the host machine. KVM provides para-virtualized devices to virtual machines using the Virtio API as a layer between the hypervisor and guest.
All Virtio devices have two parts: the host device and the guest driver.
Kernel support
Use the following command to check if the VIRTIO modules are available in the kernel inside the virtual machine:
Then, check if the kernel modules are automatically loaded with the command:
In case the above commands return nothing, you need to load the kernel modules manually.
List of para-virtualized devices
- network device (virtio-net)
- block device (virtio-blk)
- controller device (virtio-scsi)
- serial device (virtio-serial)
- balloon device (virtio-balloon)
How to use KVM
See the main article: QEMU.
Tips and tricks
Nested virtualization
Nested virtualization enables existing virtual machines to be run on third-party hypervisors and on other clouds without any modifications to the original virtual machines or their networking.
On host, enable nested feature for kvm_intel :
Verify that feature is activated:
Enable the «host passthrough» mode to forward all CPU features to the guest system:
- If using QEMU, run the guest virtual machine with the following command: qemu-system-x86_64 -enable-kvm -cpu host .
- If using virt-manager, change the CPU model to host-passthrough .
- If using virsh, use virsh edit vm-name and change the CPU line to
Boot VM and check if vmx flag is present:
Enabling huge pages
This article or section is a candidate for merging with QEMU.
You may also want to enable hugepages to improve the performance of your virtual machine. With an up to date Arch Linux and a running KVM you probably already have everything you need. Check if you have the directory /dev/hugepages . If not, create it. Now we need the right permissions to use this directory. The default permission is root’s uid and gid with 0755, but we want anyone in the kvm group to have access to hugepages.
Add to your /etc/fstab :
Of course the gid must match that of the kvm group or specify the group name directly with gid=kvm . The mode of 1770 allows anyone in the group to create files but not unlink or rename each other’s files. Make sure /dev/hugepages is mounted properly:
Now you can calculate how many hugepages you need. Check how large your hugepages are:
Normally that should be 2048 kB ≙ 2 MB. Let us say you want to run your virtual machine with 1024 MB. 1024 / 2 = 512. Add a few extra so we can round this up to 550. Now tell your machine how many hugepages you want:
If you had enough free memory you should see:
If the number is smaller, close some applications or start your virtual machine with less memory (number_of_pages x 2):
Note the -mem-path parameter. This will make use of the hugepages.
Now you can check, while your virtual machine is running, how many pages are used:
Источник
Ubuntu Documentation
Content Cleanup Required
This article should be cleaned-up to follow the content standards in the Wiki Guide. More info.
Installation
Pre-installation checklist
Check that your CPU supports hardware virtualization
To run KVM, you need a processor that supports hardware virtualization. Intel and AMD both have developed extensions for their processors, deemed respectively Intel VT-x (code name Vanderpool) and AMD-V (code name Pacifica). To see if your processor supports one of these, you can review the output from this command:
If 0 it means that your CPU doesn’t support hardware virtualization.
If 1 or more it does — but you still need to make sure that virtualization is enabled in the BIOS.
By default, if you booted into XEN kernel it will not display svm or vmx flag using the grep command. To see if it is enabled or not from xen, enter:
You must see hvm flags in the output.
Alternatively, you may execute:
which may provide an output like this:
You can still run virtual machines, but it’ll be much slower without the KVM extensions.
NOTE: You may see a message like «KVM acceleration can/can NOT be used». This is misleading and only means if KVM is *currently* available (i.e. «turned on»), *not* if it is supported.
Use a 64 bit kernel (if possible)
Running a 64 bit kernel on the host operating system is recommended but not required.
To serve more than 2GB of RAM for your VMs, you must use a 64-bit kernel (see 32bit_and_64bit). On a 32-bit kernel install, you’ll be limited to 2GB RAM at maximum for a given VM.
To see if your processor is 64-bit, you can run this command:
If 0 is printed, it means that your CPU is not 64-bit.
If 1 or higher, it is. Note: lm stands for Long Mode which equates to a 64-bit CPU.
Now see if your running kernel is 64-bit, just issue the following command:
x86_64 indicates a running 64-bit kernel. If you use see i386, i486, i586 or i686, you’re running a 32-bit kernel.
Note: x86_64 is synonymous with amd64.
Installation of KVM
Install Necessary Packages
For the following setup, we will assume that you are deploying KVM on a server, and therefore do not have any X server on the machine.
You need to install a few packages first:
Cosmic (18.10) or later
Lucid (10.04) or later
Karmic (9.10) or earlier
libvirt-bin provides libvirtd which you need to administer qemu and kvm instances using libvirt
qemu-kvm (kvm in Karmic and earlier) is the backend
ubuntu-vm-builder powerful command line tool for building virtual machines
bridge-utils provides a bridge from your network to the virtual machines
You might also want to install virt-viewer, for viewing instances.
Add Users to Groups
Karmic (9.10) and later (but not 14.04 LTS and 18.10)
You need to ensure that your username is added to the group libvirtd:
After this, you need to relogin so that your user becomes an effective member of the libvirtd group. The members of this group can run virtual machines. (You can also ‘newgrp kvm’ in a terminal, but this will affect only that terminal.)
Bionic (18.04 LTS) and higher
The group name is changed to libvirt, and you also need to be a member of ‘kvm’:
Releases prior to Karmic (9.10)
You need to ensure that your username is added to the groups: kvm and libvirtd.
To add your to the groups:
After the installation, you need to relogin so that your user becomes an effective member of kvm and libvirtd user groups. The members of this group can run virtual machines.
Verify Installation
You can test if your install has been successful with the following command:
If on the other hand you get something like this:
Something is wrong (e.g. you did not relogin) and you probably want to fix this before you move on. The critical point here is whether or not you have write access to /var/run/libvirt/libvirt-sock.
The sock file should have permissions similar to:
Also, /dev/kvm needs to be in the right group. If you see:
You might experience problems when creating a virtual machine. Change the device’s group to kvm/libvirtd instead:
Now you need to either relogin or restart the kernel modules:
Optional: Install virt-manager (graphical user interface)
If you are working on a desktop computer you might want to install a GUI tool to manage virtual machines.
Virtual Machine Manager will appear in Applications -> System Tools menu. First create a new connection to local QEMU instance from File -> Add Connection menu. Localhost (QEMU) or QEMU/KVM should appear in the virtual machine list. Note: there already exist Localhost (QEMU Usermode) connection but this does not work at least on Ubuntu 10.04.
Create a new virtual machine by pressing the top left Create a new virtual machine toolbar button.
Version-specific Notes
8.10 (Intrepid) Notes
Two meta packages have been added: ubuntu-virt-server and ubuntu-virt-mgmt. Ubuntu-virt-server installs the packages needed to setup a base virtulization host (kvm, libvirt-bin and openssh-server) and ubuntu-virt-mgmt installs what you need to administer it from a management station (virt-manager, python-vm-builder and virt-viewer).
ubuntu-vm-builder has been replaced by python-vm-builder (tutorial).
Note: libdevmapper does not load its module when it is installed (bug 277648) , so you will either need to do a
or reboot your system before being able to use it.
11.10 (Oneric) Notes
Switching to the server kernel might be helpful if there are start problems with virtual machines (i.e. Windows XP freezes approximately once every 5 starts )
KVM/Installation (последним исправлял пользователь hamishmb 2020-03-23 20:00:34)
The material on this wiki is available under a free license, see Copyright / License for details
You can contribute to this wiki, see Wiki Guide for details
Источник
How to enable KVM virsh console access for Ubuntu Linux VM
Our sample setup
- KVM host IP: 192.168.2.15
- KVM guest OS: Ubuntu Linux 16.04 LTS
- KVM guest IP: 192.168.2.45
- KVM guest name: ubuntu-box1
- KVM guest user name: vivek
- KVM guest password: hidden-secrete
How do I setup console access of Ubuntu 16.04 KVM Guest
First, you need to login using ssh or VNC client to your Ubuntu guest.
Use ssh login
In this example, I am login using ssh client from my workstation (or type command on KVM host itself) to the Ubuntu Linux VM guest:
$ ssh vivek@192.168.2.45
After log in gain access to the root shell:
$ sudo -s
Use vnc login
Type the following command on your KVM host:
$ virsh dumpxml ubuntu-box1 | grep vnc
port=’5901′ autoport=’yes’ listen=’127.0.0.1’>
Please note down the port value (i.e. 5901). You need to use an SSH client to setup tunnel and a VNC client to access the remote vnc server. Type the following SSH port forwarding command from your Linux/Unix/MacOS client or desktop system:
$ ssh
$ ssh vivek@192.168.2.15 -L 5901:127.0.0.1:5901
Once you have ssh tunnel established, you can point your VNC client at your own IP 127.0.0.1 (localhost) address and port 5901 as follows in VNC client:
Fig.01: Graphical VNC client to connect to Ubuntu Linux 16.04 LTS server console
Fig.02: Login to my Ubuntu guest VM
Configure a serial console in the Ubuntu guest
Finally type the following two commands to enable a serial console in the guest to access a connection using ‘virsh console’ command:
$ sudo systemctl enable serial-getty@ttyS0.service
$ sudo systemctl start serial-getty@ttyS0.service
Sample outputs:
- No ads and tracking
- In-depth guides for developers and sysadmins at Opensourceflare✨
- Join my Patreon to support independent content creators and start reading latest guides:
- How to set up Redis sentinel cluster on Ubuntu or Debian Linux
- How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
- How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
- A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
- How to protect Linux against rogue USB devices using USBGuard
Join Patreon ➔
How do I access console of Ubuntu 16.04 KVM Guest from KVM host
To see a list of running VM, enter:
$ virsh list
Sample outputs:
Type the following command from KVM host to login to the guest named ubuntu-box1
$ virsh console ubuntu-box1
OR
$ virsh console 2
Sample session:
Animated gif 01: virsh console demo
🐧 Get the latest tutorials on Linux, Open Source & DevOps via
Источник