#!/bin/bash

# Only root can run this script
if [ "$(id -u)" != "0" ]; then
   echo "This script must be run as root" 1>&2
   exit 1
fi

let NRNODES=(`grep processor /proc/cpuinfo | wc -l`)-1

loadCpuid=0
loadMsr=0

if [ ! -d /dev/cpu ] ; then
	mkdir /dev/cpu
fi

for i in `seq 0 $NRNODES`
do
	if [ ! -d /dev/cpu/$i ] ; then
		mkdir /dev/cpu/$i
	fi
	if [ ! -c /dev/cpu/$i/cpuid ] ; then
		(cd /dev/cpu/$i ; mknod cpuid c 203 $i)
		loadCpuid=1
	fi
	if [ ! -c /dev/cpu/$i/msr ] ; then
		(cd /dev/cpu/$i ; mknod msr c 202 $i)
		loadMsr=1
	fi
done

if test "$loadCpuid" -eq 1 ; then
	modprobe cpuid
fi
if test "$loadMsr" -eq 1 ; then
	modprobe msr
fi

echo "All cpuid & msr /dev nodes present."
