VGA GPU passthrough qemu虚拟桌面pci穿透--Shell脚本检测与关机

转载注明:http://blog.csdn.net/hubbybob1/article/details/77199162
本文主要讲解在VGA GPU passthrough成功后,如果关闭虚拟机windows,虚拟机的进程qemu-system-x86_64结束,然而宿主机linux(ubuntu,centos7)并没有因为虚拟机的关闭而关机,而此时屏幕不再显示,鼠标键盘而无法使用,这样很不科学,因此,有两种方案,
一是将解绑的VGA和USB重新解绑回来,然后咱绑定到相应的PCI上去,这个方法比较难实现,解绑回来好可以,但是再绑定本身的驱动,是不立即生效的,原因还不知道;
二是编写脚本在虚拟机关机后,宿主机linux也关机,这个方法很容易实现,也很科学,对于用户而言,根本无需关机两次。
学习VGA GPU 和USB穿透,查看下面两个博客:
http://blog.csdn.net/hubbybob1/article/details/73920296
http://blog.csdn.net/hubbybob1/article/details/77101913

思路就是写一个循环检测的shell脚本,那么就直接代码了

#!/bin/bash

#调用qemu进程脚本,启动虚拟机,当前目录下,也可以家绝对路径
./qemu.sh

#打印出当前的qemu进程:grep qemu-system-x86_64查询该进程,grep -v "grep" 去掉grep进程
QemuThread=`ps -ef | grep qemu-system-x86_64 | grep -v "grep"`
echo $QemuThread

#查qemu-system-x86_64进程个数:wc -l 返回行数
count=`ps -ef | grep qemu-system-x86_64 | grep -v "grep" | wc -l`
echo $count

sec=2 #2秒检测一次
#开始一个循环,以判断进程是否关闭

for((var=0;var<1000;))
do
#每次都检测是否进程退出
 count=`ps -ef | grep qemu-system-x86_64 | grep -v "grep" | wc -l`
 if [ $count -gt 0 ]; then
  #若进程还未关闭,则脚本sleep 2秒
  echo sleep $sec second the $var time, the QEMU thread is still alive
  sleep $sec
 else
  #若进程已经关闭,则跳出循环
  echo "break 退出循环"
  break
 fi
done

#if [ $count -eq 0 ]; then
# echo "nohup startMethodServer.sh &"
# nohup startMethodServer.sh &
#else
# echo "It's better to check the thread!!!"
#fi

#调用关机脚本
#nohup startMethodServer.sh &
shutdown -h now
#ls
#reboot

真正在写脚本的时候可以把那些打印信息全部去掉

猜你喜欢

转载自blog.csdn.net/hubbybob1/article/details/77199162
VGA
今日推荐