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

真正在寫腳本的時候可以把那些打印信息全部去掉

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章