Bug 599

Summary: [poky] qemu running with KVM needs root permission
Product: [Build System, Metadata & Runtime] Meta-yocto Reporter: Jiajun Xu <jiajun.xu>
Component: meta-yoctoAssignee: Edwin Zhai <edwin.zhai>
Status: VERIFIED FIXED QA Contact:
Severity: normal    
Priority: High CC: edwin.zhai, kevin.tian, kunwei.tian, poky.bs.watcher, poky.watcher, richard.purdie, sgw
Version: 1.0   
Target Milestone: 1.0 M2   
Hardware: x86   
OS: Multiple   
Whiteboard:
OS type for building Yocto: --- Type of Regression: ---
Verified: Program Management Documentation change: ---

Description Jiajun Xu 2010-12-15 06:47:44 UTC
Testing Env:    
Poky gitinfo: 3a78838d4f1db75d06b5d3df042638f34cf328f5

After KVM feature is enabled in poky, now qemu can run with KVM enabled. But it needs root permission currently. Pls. check following log:

#########
jxu49@vt-nhm4:~/osel/poky-m2/build$ poky-qemu qemux86 kvm images/qemux86/bzImage-2.6.37+git0+ee0a10ab687b29c4d22d47e5b28bc8b3ebb7a8d9_0+87aacc373557f8849dde8618fbe1f7f8f2af6038-r14-qemux86-20101211200958.bin images/qemux86/poky-image-
poky-image-minimal-qemux86-20101211235049.rootfs.ext3  poky-image-sato-qemux86-20101211224433.rootfs.ext3     poky-image-sdk-qemux86-20101211220547.rootfs.ext3
jxu49@vt-nhm4:~/osel/poky-m2/build$ poky-qemu qemux86 kvm images/qemux86/bzImage-2.6.37+git0+ee0a10ab687b29c4d22d47e5b28bc8b3ebb7a8d9_0+87aacc373557f8849dde8618fbe1f7f8f2af6038-
r14-qemux86-20101211200958.bin images/qemux86/poky-image-sato-qemux86-20101211224433.rootfs.ext3

Continuing with the following parameters:
KERNEL: [images/qemux86/bzImage-2.6.37+git0+ee0a10ab687b29c4d22d47e5b28bc8b3ebb7a8d9_0+87aacc373557f8849dde8618fbe1f7f8f2af6038-r14-qemux86-20101211200958.bin]
ROOTFS: [images/qemux86/poky-image-sato-qemux86-20101211224433.rootfs.ext3]
FSTYPE: [ext3]
Acquiring lockfile for tap0...
Setting up tap interface under sudo
[sudo] password for jxu49:
Acquiring lockfile for tap1...
Warning: distccd not present, no distcc support loaded.
Running qemu...
/home/jxu49/osel/poky-m2/build/tmp/sysroots/i686-linux/usr/bin/qemu -kernel images/qemux86/bzImage-2.6.37+git0+ee0a10ab687b29c4d22d47e5b28bc8b3ebb7a8d9_0+87aacc373557f8849dde8618fbe1f7f8f2af6038-r14-qemux86-20101211200958.bin -net nic,vlan=0 -net tap,vlan=0,ifname=tap1,script=no,downscript=no -hda images/qemux86/poky-image-sato-qemux86-20101211224433.rootfs.ext3 -show-cursor -usb -usbdevice wacom-tablet -vga vmware -enable-gl -k en-us -no-reboot --append "vga=0 root=/dev/hda mem=128M ip=192.168.7.4::192.168.7.3:255.255.255.0 "
Enabling opengl
Could not access KVM kernel module: Permission denied
failed to initialize KVM
Set 'tap1' nonpersistent
Releasing lockfile of preconfigured tap device 'tap1'
###########
Comment 1 Edwin Zhai 2010-12-17 01:11:02 UTC
ubuntu required root privilege to access kvm module for security reason. One fix is to use "sudo" to run qemu. Another one is change the ownership of "/dev/kvm", which is more reasonable.

Other distribution like RH and Debian seems has something similar, so step 1~3 are probably not needed.

Jiajun,
Can you have a try on following steps? I'll make documents for those in our wiki

1. As root to insert all modules
# modprobe kvm-intel

2. Add a new group named "kvm", and add current user to it
$sudo addgroup --system kvm
$sudo adduser $USER kvm

3. Change the ownership and mod
$ sudo chown root:kvm /dev/kvm
$ sudo chmod 0660 /dev/kvm 

The output of "ls -l /dev/kvm" should be like this:
crw-rw----+ 1 root kvm 10, 232 2010-07-02 09:27 /dev/kvm

4. to make it works, need logout then login with current user, now you can access kvm module as non-root user.
Comment 2 Edwin Zhai 2010-12-20 05:10:39 UTC
I have created a howto @
https://oldwiki.pokylinux.org/wiki2/index.php/How_to_enable_KVM_for_Poky_qemu

I have no edit right on the public wiki. Once I get it, I can move it to public wiki.

Jiajun,
It works on my part. Can you have a try with the ownership changing part. If it works, I can mark it as fix.

Thanks,
Comment 3 Richard Purdie 2010-12-21 06:39:01 UTC
Edwin:

Firstly, can we detect whether we have permission to access the kvm device and only use it if we have permission? If we don't have permission can we print a warning to the user about how to enable the speedup?

Secondly, you can get edit permissions on the wiki by creating an account and confirming your email address.
Comment 4 Edwin Zhai 2010-12-21 17:15:16 UTC
(In reply to comment #3)
> Edwin:
> 
> Firstly, can we detect whether we have permission to access the kvm device and
> only use it if we have permission? If we don't have permission can we print a
> warning to the user about how to enable the speedup?

This was also what Ke suggested and I'm doing. The issue here is I have no idea of how to open the "/dev/kvm" with rw mode in bash to check if we have the right. I don't want to write a C program using "open" system call, or check the ownership and rw permission of "/dev/kvm" against current user in a complicated way.

> 
> Secondly, you can get edit permissions on the wiki by creating an account and
> confirming your email address.

Done.
Thanks
Comment 5 Edwin Zhai 2010-12-21 17:29:57 UTC
Move bkm to https://wiki.yoctoproject.org/wiki/How_to_enable_KVM_for_Poky_qemu
Comment 6 Kevin Tian 2010-12-21 17:38:28 UTC
(In reply to comment #4)
> (In reply to comment #3)
> > Edwin:
> > 
> > Firstly, can we detect whether we have permission to access the kvm device and
> > only use it if we have permission? If we don't have permission can we print a
> > warning to the user about how to enable the speedup?
> This was also what Ke suggested and I'm doing. The issue here is I have no idea
> of how to open the "/dev/kvm" with rw mode in bash to check if we have the
> right. I don't want to write a C program using "open" system call, or check the
> ownership and rw permission of "/dev/kvm" against current user in a complicated
> way.

how about using "9<>/dev/kvm"?
Comment 7 Edwin Zhai 2010-12-26 21:23:05 UTC
got fixed in poky/master @ 983bd9a77e4afdb2890245653f39d6c6d093a655
Comment 8 Jiajun Xu 2010-12-27 07:17:25 UTC
Kunwei,
Could you help to verify the bug by following Edwin's BKM, https://wiki.yoctoproject.org/wiki/How_to_enable_KVM_for_Poky_qemu?
Comment 9 Kunwei Tian 2010-12-30 21:45:34 UTC
According to Edwin's methods,this problem was resolved.So verified it as fixed.