kvm常見故障及解決


一、啟動虛擬機Connection reset by peer

 

# virsh start vmhost1error: Failed to start domain vmhost1error: Unable to read from monitor: Connection reset by peer

在虛擬機運行過程中關閉宿主服務器就有可能導致這種情況出現,由於宿主服務器中的kvm虛擬機控制器與安裝在kvm中的虛擬機會話被異常重置,所以我們可以如下解決:

# virsh managedsave-remove vmhost1# virsh start vmhost1

如果啟動查看/var/log/libvirt/qemu/vmhost1.log下log還報如下錯誤:

Cannot set up guest memory 'pc.ram': Cannot allocate memory

這個問題可能是分配給vmhost1分配的內存過大(甚至超過的物理主機的內存大小),或者可能是宿主機沒有足夠的內存分配給此虛擬機,導致無法啟動!


二、重Define虛擬機時無/usr/bin/kvm

 

error: Failed to define domain from hostname.xmlerror: Cannot find QEMU binary /usr/bin/kvm: No such file or directory

解決方法:

# ln -s /usr/libexec/qemu-kvm /usr/bin/kvm

 


三、error: internal error process exited while connecting to monitor

 

# virsh start vmhost1  
error: Failed to start domain vmhost1error: internal error process exited while connecting to monitor: kvm: -drive file=/dev/sp1368155439693/v1368544020461,if=none,id=drive-virtio-disk0,format=qcow2: could not open disk image /dev/sp1368155439693/v1368544020461: Invalid argument

分析:鏡像格式錯誤,用qemu-img info 檢查鏡像和xml配置文件中指定的type是否一致!


四、Unable to load library 'virt': libvirt.so

 

Unable to load library 'virt': libvirt.so: cannot open shared object file: No such file or directoryLinux下解決:      ln -s /usr/lib/libvirt.so.0   /usr/lib/libvirt.so windows下解決:      將libvirt-0.dll改名為virt.dll

 


五、error: Refusing to undefine while domain managed save image exists

 

# virsh undefine vmhost1error: Refusing to undefine while domain managed save image existshttp://www.redhat.com/archives/libvir-list/2011-July/msg01219.html

解決方法:virsh undefine $domain  --managed-save


六、啟動libvirtd進程出錯

 

# /usr/local/sbin/libvirtd -d -l --config /usr/local/etc/libvirt/libvirtd.conf (編譯安裝的啟動方式)
error:/usr/local/sbin/libvirtd: initialization failed
try to install libpcap-devel RPM and rebuild libvirt  http://comments.gmane.org/gmane.comp.emulators.libvirt/58218apt-get install libpcap-dev 上面的方法好像都沒有效果,但是嘗試了http://wiki.libvirt.org/page/The_daemon_cannot_be_started說的,把配置文件里的 listen_tls = 0注釋取消(更奇怪的問題,在我的客戶端鏈接不對)

 


七、啟動虛擬機報錯

 

# virsh start vmhost1error: Failed to start domain vmhost1error: internal error process exited while connecting to monitor: Could not access KVM kernel module: No such file or directoryfailed to initialize KVM: No such file or directoryNo accelerator found!

上面的提示信息就是因為QEMU在初始化階段因為無法找到kvm內核模塊。

# modprobe kvm   #載入指定的模塊

重啟電腦,進入bios界面,設置advance選項里面的virtualization標簽為Enabled

通過命令 lsmod | grep kvm    #顯示已載入的模塊

 


八、虛擬機遷移

 

# virsh migrate --live 1 qemu+tcp://192.168.0.121 --p2p --tunnelled --unsafe error: operation failed: Failed to connect to remote libvirt URI qemu+tcp://192.168.0.121(在URI后面加上/system,‘system’相當於root用戶的訪問權限) #virsh migrate --live 2 qemu+tcp://192.168.0.121/system --p2p --tunnelled error: Unsafe migration: Migration may lead to data corruption if disks use cache != none(加上--unsafe參數)#virsh migrate --live 2 qemu+tcp://192.168.0.121/system --p2p --tunnelled --unsafe error: Timed out during operation: cannot acquire state change lock (啟動虛擬機有時也會遇此錯誤),需要重啟libvirtd進程

 


九、virsh

 

error: Failed to connect socket to '/var/run/libvirt/libvirt-sock': Connection refused(libvirtd 進程沒有啟動,libvirtd是一個監聽客戶端請求的進程)# virsh -c qemu:///system listerror: Failed to connect socket to '/var/run/libvirt/libvirt-sock': Permission denied error: failed to connect to the hypervisor (當前用戶沒有權限,修改/etc/libvirt/libvirtd.conf,unix_sock_rw_perms = 0777,使所有用戶都有權限讀寫)


注意:如果查詢的結果中包含有vmx,那么就可以證明服務器是支持虛擬化的

 

安裝包

yum -y groupinstall "Virtualization" "Virtualization Client" "Virtualization Platform"

 

modprobe kvm

lsmod |grep kvm

 

ln -sv /usr/libexec/qemu-kvm /usr/bin/ 

 

service libvirtd start 

chkconfig --level 2345 libvirtd on 

chkconfig libvirtd  --list

 

配置網卡橋接

virsh iface-bridge eth0 br0

 

安裝

virt-install --connect  qemu:///system --virt-type kvm --name rhel6 --ram=1024  --disk path=/var/lib/libvirt/images/rhel6.img,size=8,sparse --os-type=linux --accelerate -c /sdb1/CentOS-6.6-x86_64-bin-DVD.iso --network bridge=br0,model=virtio --vnc  --vncport=5991 --vnclisten=0.0.0.0 --force

 

--vncport 端口可以自己改,不指定的話自動獲取

--boot network,cdrom,menu=on  這個是啟動順序可以不用加

當出現下面提示的時候用vnc客戶端連接即可(端口:5991)

netstat -tnlp |grep kvm

tcp        0      0 0.0.0.0:5991                0.0.0.0:*                   LISTEN      3006/qemu-kvm

開始安裝......

創建域......                                                                     |    0 B     00:00     

無法打開顯示:

運行 'virt-viewer --help' 來查看可用命令行選項的完整列表

域安裝仍在進行。您可以重新連接

到控制台以便完成安裝進程。

 

注意: 如果安裝完成后,ifconfig只能看到本地回環網卡,到/etc/sysconfig/network-scripts/目錄下將ifcfg-eth0的ONBOOT改為yes,然后啟動網卡即可。

 

virsh uri: 查看當前主機上hypervisor的連接路徑;

virsh connect:

virsh define: 創建一個虛擬機,根據事先定義的xml格式的配置文件;創建以后不會自動啟動;

virsh create: 創建,創建完成后會自動啟動;

virsh undefine: 刪除

virsh list  --all 查看

virsh destroy  rhel6  刪除rhel6

virt-install --os-variant list

virsh start rhel66  開啟rhel66 或 virsh create /etc/libvirt/qemu/rhel66.xml

virsh shutdown rhel66

virsh reboot rhel66  

virsh console rhel66

virsh suspend rhel66  掛起

virsh resume  rhel66 恢復掛起

 

/etc/libvirt/qemu/  虛擬機xml配置文件目錄

virsh edit rhel66   編輯xml配置文件,vi編輯的不會立即生效

 

解決virsh shutdown無法關閉KVM虛擬機,以下操作都在客戶機,不是宿主機

yum -y install  acpid

service acpid restart

chkconfig --level 2345 acpid on 

chkconfig acpid --list

 

解決KVM中宿主機通過console無法連接客戶機,以下操作都在客戶機,不是宿主機

 

1、添加ttyS0的安全許可,允許root登錄:

  echo "ttyS0" >> /etc/securetty

 

2、在/etc/grub.conf文件中為內核添加參數:

   console=ttyS0

  這步要注意:

  console=ttyS0一定要放在kernel這行中(大約在第16行),不能單獨一行,即console=ttyS0是kernel的一個參數,不是單獨的

 

3、在/etc/inittab中添加agetty:

S0:12345:respawn:/sbin/agetty ttyS0 115200

 

4、重啟客戶機:

 reboot

 

kvm虛擬機的重命名 

 

1、查看所有的kvm虛擬機

virsh list --all

 Id    名稱                         狀態

----------------------------------------------------

 -     rhel6                        關閉

2、重命名kvm虛擬機最好是將虛擬機先關機,然后再導出其xml文件

cd /etc/libvirt/qemu

virsh dumpxml rhel6 > CentOS02.xml

 

3、為了讓重命名后的虛擬機名字與磁盤文件名一致、我們可以先將之前的磁盤文件命名成CentOS02.img

cd /var/lib/libvirt/images

mv rhel6.1.img CentOS02.img

 

4、編輯CentOS02.xml文件,修改<name>標簽對以及磁盤文件,這里我們將磁盤文件修改成CentOS02.img

<domain type='kvm'>

  <name>CentOS02</name>

      ......  

   ...省略......

      ......

  <disk type='file' device='disk'>

      <driver name='qemu' type='raw' cache='none'/>

  <source file='/var/lib/libvirt/images/CentOS02.img'/>

 

5、使用virsh undefine命令刪除之前的虛擬機,根據新的xml文件定義新的kvm虛擬機

virsh undefine  rhel6 ////刪除rhel6虛擬機

virsh define /etc/libvirt/qemu/CentOS02.xml    //重新定義新的kvm虛擬機

 

6、最后我們可以再次使用virsh list --all命令查看,結果如下所示

Id    名稱                         狀態

----------------------------------------------------

 -     CentOS02                       關閉

經過如上方法重命名的kvm機直接啟動使用即可,不會影響udev動態管理設備的文件。和之前的虛擬機相比、僅僅就是名字變了而已。

 

設置步驟如下:

  1、以聯想t420為例,開機后按F2進入bios設置;

  2、選擇Security選項;

  3、選擇Virtualization,回車進入;

  4、按+鍵修改倆個選項為enabled;

  5、按F10,輸入y保存退出即可。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM