15

I have installed Jenkins and configured a build job with emulator running during build. But when I execute the job, I get error message that jenkins user doesn't have permission to run kvm. How can I fix it.

$ android list target
[android] Using Android SDK: /opt/android/sdk
$ adb start-server
* daemon not running. starting it now on port 5973 *
* daemon started successfully *
$ adb start-server
[android] Starting Android emulator
[android] Erasing existing emulator data...
$ emulator -ports 5971,5972 -prop persist.sys.language=en -prop persist.sys.country=US -avd hudson_en-US_160_1024x768_android-15_x86 -no-snapshot-load -no-snapshot-save -wipe-data
emulator: ERROR: x86 emulation currently requires hardware acceleration!
Please ensure KVM is properly installed and usable.
CPU acceleration status: This user doesn't have permissions to use KVM (/dev/kvm).
[android] Emulator did not appear to start; giving up
$ adb disconnect localhost:5972
[android] Stopping Android emulator
$ adb kill-server

Permissions

$ ls -l /dev/kvm
crw-rw----+ 1 root root 10, 232 May  6 13:46 /dev/kvm

$ groups jenkins
jenkins : jenkins
Viktor K
  • 2,153
  • 3
  • 25
  • 40
  • Run `kvm-ok` and see the output. Is it `INFO: /dev/kvm exists KVM acceleration can be used` or `INFO: Your CPU does not support KVM extensions KVM acceleration can NOT be used` ? It also could be that `Enter your BIOS setup and enable Virtualization Technology`, so then you should do as said :) – Stan E May 06 '15 at 16:45
  • As you've shown yourself, only `root` has permission to access `/dev/kvm`, and `jenkins` isn't in the `root` group. – Christopher Orr May 06 '15 at 20:25

2 Answers2

32

I have managed to fix the problem.

  1. Install Qemu-KVM and cpu-checker:
    sudo apt install qemu-kvm cpu-checker
  2. Check if KVM is available (as jenkins user):
    $ kvm-ok
    INFO: /dev/kvm exists
    KVM acceleration can be used
  3. Create group kvm and add jenkins to this group:
    addgroup kvm
    usermod -a -G kvm jenkins
  4. Change group ownership for /dev/kvm:
    chgrp kvm /dev/kvm
  5. Create udev rule:
    $ sudo nano /etc/udev/rules.d/60-qemu-kvm.rules
    KERNEL=="kvm", GROUP="kvm", MODE="0660"
  6. Reboot
Viktor K
  • 2,153
  • 3
  • 25
  • 40
  • See http://stackoverflow.com/a/43731342/2482947. In ubuntu you should also add `jenkins` user to `libvirt` group. – hadilq May 02 '17 at 06:22
0

On my CI (gitlab) this was missing on Ubuntu

usermod -a -G kvm gitlab-runner

or with Jenkins

usermod -a -G kvm jenkins
hannes ach
  • 16,247
  • 7
  • 61
  • 84