Some minor fixes for KVM SRAO test cases

I give a quick overview and find some defects. Here is the list:

1) EARLYKILL is defined but not used
2) some echo info outside functions include "!", which will
   make shell confused and give wrong output
3) add page-types check on the host side
4) some $mnt usages are dangerous. Such as $mnt$get_tmp
   will return wrong path
5) fix a spell error for variable QEMU_PID
6) update p2v -> x-gpa2hva according to Ying's latest QEMU patch
7) in the usage host_run.sh can be executed directly but in fact
   it doesn't. Add execution permission for it.
8) add "-h" description and option "h" should not be given a ":"
9) make "-m" option a consistent action as other options
10) add more conditions check before tests
11) simplify some statements
12) auto mount mce_inject module

All of these fixes don't touch actual functions.

Signed-off-by: Chen Gong <gong.chen@linux.intel.com>
diff --git a/kvm/host/guest_init b/kvm/host/guest_init
index b16d2b4..df34ad6 100644
--- a/kvm/host/guest_init
+++ b/kvm/host/guest_init
@@ -11,7 +11,7 @@
     touch /root/jobdone
     mount -t debugfs null /sys/kernel/debug
     echo 1 > /sys/kernel/debug/mce/fake_ser_p
-    echo 1 > /proc/sys/vm/memory_failure_early_kill
+    echo EARLYKILL > /proc/sys/vm/memory_failure_early_kill
     sh GUESTRUN &
     return "$RETVAL"
 }
diff --git a/kvm/host/host_run.sh b/kvm/host/host_run.sh
old mode 100644
new mode 100755
index a72f0d5..c83c241
--- a/kvm/host/host_run.sh
+++ b/kvm/host/host_run.sh
@@ -49,12 +49,13 @@
 	echo -e "\t\t\tBy default, host public key is $host_key_pub"
 	echo -e "\t-p privkey\t: host privite key"
 	echo -e "\t\t\tBy default, host privite key is $host_key_priv"
-	echo -e "\t-o offset\t: guest image offset (optional) "
+	echo -e "\t-o offset\t: guest image offset"
 	echo -e "\t\t\tBy default, offset is calculated by kpartx "
         echo -e "\t-l\t\t: late kill, disable early kill in guest system"
         echo -e "\t\t\tBy default, earlykill is enabled "
         echo -e "\t-m ramsize\t: virtual RAM size of guest system"
         echo -e "\t\t\tBy default, qemu-kvm defaults to 128M bytes"
+        echo -e "\t-h\t\t: show this help"
 	echo "============If you want to specify the guest kernel==========="
 	echo "============please set below options all together============="
 	echo -e "\t-k kernel\t: guest kernel"
@@ -63,12 +64,14 @@
 	exit 0
 }
 
-while getopts ":i:f:d:g:o:b:p:k:n:r:h:lm:" option
+while getopts "i:f:d:g:o:b:p:k:n:r:hlm:" option
 do
         case $option in
-		i) image=$OPTARG; offset=`kpartx -l $image | awk '/loop deleted/ {next}; {offset=$NF*512}; END {print offset}'`;;
+		i) image=$OPTARG;;
 		f) mce_inject_file=$OPTARG;;
-		d) HOST_DIR=$OPTARG; host_key_pub=$HOST_DIR/id_rsa.pub; host_key_priv=$HOST_DIR/id_rsa;;
+		d) HOST_DIR=$OPTARG
+		   host_key_pub=$HOST_DIR/id_rsa.pub
+		   host_key_priv=$HOST_DIR/id_rsa ;;
 		g) GUEST_DIR=$OPTARG;;
 		b) host_key_pub=$OPTARG;;
 		p) host_key_priv=$OPTARG;;
@@ -77,9 +80,9 @@
 		k) kernel=$OPTARG;;
 		n) initrd=$OPTARG;;
 		r) root=$OPTARG;;
-		m) RAM_size="-m $OPTARG";;
+		m) RAM_size=$OPTARG;;
 		h) usage;;
-		*) echo "invalid option!"; usage;;
+		*) echo 'invalid option!'; usage;;
         esac
 done
 
@@ -103,12 +106,32 @@
 invalid()
 {
 	echo $1
-	echo "Try \`./host_run.sh -h\` for more information."
+	echo "Try ./host_run.sh -h for more information."
 	exit 0
 }
 
 check_env()
 {
+	if [ "`whoami`" != "root" ]; then
+		echo "Must run as root"
+		exit 1
+	fi
+
+	if modinfo mce_inject &> /dev/null; then
+		if ! lsmod | grep -q mce_inject; then
+			if ! modprobe mce_inject; then
+				invalid "module mce_inject isn't supported ?"
+			fi
+		fi
+	fi
+
+	which page-types &>/dev/null
+	[ ! $? -eq 0 ] && invalid "please install page-types tool!"
+	which kpartx &>/dev/null
+	[ ! $? -eq 0 ] && invalid "please install kpartx tool!"
+	which mce-inject &>/dev/null
+	[ ! $? -eq 0 ] && invalid "please install mce-inject tool!"
+
 	[ -z $image ] && invalid "please input the guest image!"
 	[ ! -e $image ] && invalid "guest image $image does not exist!"
 	[ -z $mce_inject_file ] && invalid "please input the mce data file!"
@@ -123,6 +146,8 @@
 mount_image()
 {
 	mnt=`mktemp -d`
+	offset=`kpartx -l $image | awk '/loop deleted/ {next}; \
+	{offset=$NF*512}; END {print offset}'`
 	mount_err=`mount -oloop,offset=$offset $image $mnt 2>&1`
 	if [ $? -eq 0 ]; then
 	    fs_type=unset
@@ -138,6 +163,13 @@
 	    return 1
 	fi
 
+	which losetup &>/dev/null
+	[ ! $? -eq 0 ] && invalid "please install losetup tool!"
+	which pvdisplay &>/dev/null
+	[ ! $? -eq 0 ] && invalid "please install pvdisplay tool!"
+	which vgchange &>/dev/null
+	[ ! $? -eq 0 ] && invalid "please install vgchange tool!"
+
 	#Try mounting the LVM image
 	loop_dev=`losetup -o ${offset} -f --show ${image}`
 	if [ -z ${loop_dev} ]; then
@@ -149,7 +181,7 @@
 	lv=lv_root
 	vgchange -a ey ${vg}
 	if [ ! -b /dev/mapper/${vg}-${lv} ]; then
-	    echo ! block special
+	    echo '! block special'
 	    losetup -d ${loop_dev}
 	    rm -rf $mnt
 	    return 1
@@ -181,15 +213,15 @@
 {
 	mount_image
 	if [ $? -ne 0 ]; then
-	    echo "mount of image failed!"
+	    echo 'mount of image failed!'
 	    return 1
 	fi
-	if [ ! -e $mnt$guest_script ]; then
+	if [ ! -e $mnt/$guest_script ]; then
 	    umount_image
 	    invalid "Invalid guest directory!"
 	fi
 	rm -f $mnt/etc/rc3.d/S99kvm_ras
-	rm -f $mnt$guest_tmp $mnt$guest_page
+	rm -f $mnt/$guest_tmp $mnt/$guest_page
 
 	if [ ! -d $mnt/root/.ssh ]; then
 	    mkdir $mnt/root/.ssh
@@ -197,8 +229,9 @@
 	fi
 	cat $host_key_pub >> $mnt/root/.ssh/authorized_keys
         kvm_ras=/etc/init.d/kvm_ras
-	sed "s#EARLYKILL#$early_kill#g" $guest_init | sed "s#GUESTRUN#$guest_script#g" > $mnt$kvm_ras
-	chmod a+x $mnt$kvm_ras
+	sed -e "s#EARLYKILL#$early_kill#g" \
+	-e "s#GUESTRUN#$guest_script#g" $guest_init > $mnt/$kvm_ras
+	chmod a+x $mnt/$kvm_ras
 	ln -s $kvm_ras $mnt/etc/rc3.d/S99kvm_ras
 	sleep 2
 	umount_image
@@ -214,7 +247,7 @@
 		if [ ! -z $root ]; then
 		    append="root=$root ro loglevel=8 mce=3 console=ttyS0,115200n8 console=tty0"
 	            qemu-system-x86_64 -hda $image -kernel $kernel -initrd $initrd --append "$append" \
-		    $RAM_size -net nic,model=rtl8139 -net user,hostfwd=tcp::5555-:22 \
+		    -m $RAM_size -net nic,model=rtl8139 -net user,hostfwd=tcp::5555-:22 \
 		    -monitor pty -serial pty -pidfile $pid_file > $host_start 2>&1 &
 		    sleep 5
 		else
@@ -226,13 +259,13 @@
 	else
 	    echo "Start the default kernel on guest system"
 	    qemu-system-x86_64 -hda $image \
-	    $RAM_size -net nic,model=rtl8139 -net user,hostfwd=tcp::5555-:22 \
+	    -m $RAM_size -net nic,model=rtl8139 -net user,hostfwd=tcp::5555-:22 \
 	    -monitor pty -serial pty -pidfile $pid_file > $host_start 2>&1 &
 	    sleep 5
 	fi
 	monitor_console=`awk '{print $NF}' $host_start | sed -n -e '1p'`
 	serial_console=`awk '{print $NF}' $host_start | sed -n -e '2p'`
-	QUME_PID=`cat $pid_file`
+	QEMU_PID=`cat $pid_file`
 	echo "monitor console is $monitor_console"
 	echo "serial console is $serial_console"
 	echo "Waiting for guest system start up..."
@@ -267,14 +300,14 @@
 	sleep 2
 
 	#Get Host virtual address
-	echo p2v $GUEST_PHY > $monitor_console
+	echo x-gpa2hva $GUEST_PHY > $monitor_console
 	cat $monitor_console > $monitor_console_output &
 	sleep 5
 	HOST_VIRT=`awk '/address/{print $NF}' $monitor_console_output |cut -b 3-11`
 	echo "Host virtual address is $HOST_VIRT"
 
 	#Get Host physical address
-	page-types/page-types -p $QUME_PID -LN -b anon | grep $HOST_VIRT > $host_tmp
+	page-types -p $QEMU_PID -LN -b anon | grep $HOST_VIRT > $host_tmp
 	sleep 5
 	ADDR=`cat $host_tmp | awk '{print "0x"$2"000"}' `
 	echo "Host physical address is $ADDR"
@@ -311,13 +344,13 @@
 check_env
 image_prepare
 if [ $? -ne 0 ]; then
-    echo "Mount Guest image failed, quit testing!"
+    echo 'Mount Guest image failed, quit testing!'
 else
     start_guest
     get_guest_klog
     check_guest_alive
     if [ $? -ne 0 ]; then
-        echo "Start Guest system failed, quit testing!"
+        echo 'Start Guest system failed, quit testing!'
     else
 	sleep 5
         addr_translate
@@ -325,17 +358,17 @@
 	sleep 5
 	check_guest_klog
 	if [ $? -ne 0 ]; then
-            echo "FAIL: Did not get expected log!"
-	    exit 0
+            echo 'FAIL: Did not get expected log!'
+	    exit 1
 	else
-	    echo "PASS: Inject error into guest!"
+	    echo 'PASS: Inject error into guest!'
 	fi
 	sleep 10
 	check_guest_alive
 	if [ $? -ne 0 ]; then
-            echo "FAIL: Guest System could have died!"
+            echo 'FAIL: Guest System could have died!'
 	else
-	    echo "PASS: Guest System alive!"
+	    echo 'PASS: Guest System alive!'
 	fi
     fi
 fi