Search code examples
linuxcentosvirtualboxvirtualizationcentos7

Check if virtualization is enabled without sudo on Centos 7?


I need to check if virtualization is enabled without sudo on Centos 7.

I found commands to check if virtualization is enabled, but it needs sudo.

sudo modprobe msr
sudo rdmsr 0x3a

For Ubuntu exists utility cpu-checker which can check if virtualization is enabled without sudo.

kvm-ok

Does exist similar utility to cpu-checker on Centos 7?


Solution

  • First:

    kvm-ok and rdmsr 0x3a do completely different things.

    kvm-ok, according to it's man page just parses /proc/cpuinfo for the CPU flags that indicate Virtualization technology in the CPU (and then checks whether the necessary module was loaded, whereas rdmsr reads special model-specific registers of the CPU.

    Hence, kvm-ok doesn't do anything you'd need root privileges for, whereas rdmsr can't work because your process doesn't have to context to query these registers.

    rdmsr 0x3a can be used, with a few tricks, to figure out whether on a intel Core iX or later XEON processor the VT technology was enabled; for performant virtualization, that is necessary, but not sufficient.

    Hence, I'd say just go ahead and do what kvm-ok does manually.

    Check for the first CPU's vmx or svm flags:

    grep flags /proc/cpuinfo|head -n1|grep -Eo '(vmx|svm)'
    

    and verify the kvm model was loaded:

    lsmod | grep '^kvm'
    

    If you, in fact, actually need to query that model-specific register (for example, because for some reason, even with VT disabled in the UEFI settings, the vmx flag is present), the easiest way would certainly be just using the powers of sudo to specify the program rdmsr to be run exactly with the arguments 0x3a by the user of your choice (let's call it daemonuser)without password (see man sudoers, or your /usr/share/doc/sudo/examples/sudoers):

    daemonuser ALL = NOPASSWD: /path/to/rdmsr 0x3a