Monday, December 23, 2013

"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.