“rdmsr” implemented in perl

Today I needed the “rdmsr” tool to determine if machines are configured correctly for Hypervisor usage (VT-X enabled). Just after learning how to detect this (by looking at the cpu-checker ubuntu package), I found out that msr-tools is not available on SLES11. Instead of building the package, I just implemented a minimal version in perl (it will be integrated into a perl tool checking other aspects of the system anyway):

#!/usr/bin/perl
# License: WTFPLv2
# minimal "rdmsr" implementation
$msr = shift
        or die "need msr as parameter";
$msr = hex($msr) if ($msr =~ m/^0x/i);
open(FD, "/dev/cpu/0/msr")
        or die "open /dev/cpu/0/msr: $!";
sysseek(FD, $msr, SEEK_SET)
        or die "sysseek: $!";
sysread(FD, $reg, 8 ) == 8
        or die "sysread: $!";
$reg = reverse($reg);
$hex = unpack('H*', $reg);
$hex =~s/^0*//;
print "$hex\n";

Yes, i know, my perl is horrible :-) but maybe this is useful for someone else anyway.

2 Responses to ““rdmsr” implemented in perl”

  1. Michal says:

    lscpu also checks for VT extensions, not sure how exactly, though.

  2. seife says:

    lscpu seems to look in /proc/cpuinfo, but checking the flags there just shows if the CPU is capable of VT-x / svm, but not if it has been enabled in the BIOS.
    Another method would have been to just try to load the “kvm” module (which fails if the BIOS has the feature disabled), but my solution needed to work in a SLES11 GM installation initrd environment — where the kvm module is not present. Thus I resorted to rdmsr, which I also had to implement by myself :-)