1. 程式人生 > >虛擬機器管理、虛擬機器傳輸、虛擬機器快照的製作

虛擬機器管理、虛擬機器傳輸、虛擬機器快照的製作

1.虛擬機器的管理

shut down      #正常關機
forceoff       #強制關機
virsh list               #列出正在執行的虛擬機器
virsh list --all         #列出所有的虛擬機器
virsh start desktop      #開啟desktop 
virt-viewer desktop      #顯示desktop
virsh shutdown desktop   #正常關閉desktop
virsh destroy desktop    #強制關閉desktop

virsh create rhel7.2.xml        #一次性恢復
virsh define rhel7.2.xml        #永久性恢復
virsh undefine rhel7.2          #永久性刪除

實驗:

##列出正在執行的虛擬機器
[[email protected] ~]# virsh list

在這裡插入圖片描述

##列出所有的虛擬機器
[[email protected] ~]# virsh list --all

在這裡插入圖片描述

##正常關閉虛擬機器(不會關閉圖形)
[[email protected] ~]# virsh shutdown rhel7.2
Domain rhel7.2 is being shutdown
##開啟虛擬機器
[email protected] ~]# virsh start rhel7.2
Domain rhel7.2 started
##強制退出虛擬機器(會關閉圖形)
[
[email protected]
~]# virsh destroy rhel7.2 Domain rhel7.2 destroyed ##開啟虛擬機器 [email protected] ~]# virsh start rhel7.2 Domain rhel7.2 started ##顯示虛擬機器 [[email protected] ~]# virt-viewer rhel7.2

在這裡插入圖片描述
2.虛擬機器的傳輸

html  #超文字編輯檔案
xml   #可擴充套件性編輯語言   
/etc/libvirt/qemu/westos1.xml            包含虛擬機器硬碟資訊
/var/lib/libvirt/images/westos1.qcow2    包含虛擬機器系統資訊(在虛擬機器圖形中可以檢視到)

實驗:將42主機的虛擬機器rhel7.2拷貝到本機

@1檢視42主機的硬碟資訊與系統資訊位置

[[email protected] mnt]# ssh [email protected] -X
[email protected]'s password: 
Last login: Sun Oct 21 13:56:52 2018 from foundation34.ilt.example.com
[[email protected] ~]# virt-manager
[[email protected] ~]# 
** (virt-manager:17892): WARNING **: Couldn't connect to accessibility bus: Failed to connect to socket /tmp/dbus-WF050MidiS: Connection refused
^C

在這裡插入圖片描述

[[email protected] ~]# cd /etc/libvirt/qemu/
[[email protected] qemu]# ls
desktop.xml  networks  rhel7.2.xml  server.xml
                        硬體資訊
[[email protected] qemu]# pwd
/etc/libvirt/qemu

@2拷貝42主機的硬碟資訊與系統資訊到本機

[[email protected] ~]# cd /mnt
##拷貝虛擬機器硬碟資訊
[[email protected] mnt]# scp [email protected]:/etc/libvirt/qemu/rhel7.2.xml .
[email protected]'s password: 
rhel7.2.xml                                   100% 4265     4.2KB/s   00:00 
##拷貝虛擬機器系統資訊
[[email protected] mnt]# scp [email protected]:/var/lib/libvirt/images/rhel7.2-2.qcow2 .
[email protected]'s password: 
rhel7.2-2.qcow2                               100% 9218MB  29.5MB/s   05:12 
[[email protected] mnt]# ls
rhel7.2-2.qcow2  rhel7.2.xml  vm_create.sh

@3管理剛拷貝過來的虛擬機器

[[email protected] mnt]# virsh create rhel7.2.xml 
error: Failed to create domain from rhel7.2.xml
error: Cannot access storage file '/var/lib/libvirt/images/rhel7.2-2.qcow2' (as uid:107, gid:107): No such file or directory
[[email protected] mnt]# mv rhel7.2-2.qcow2 /var/lib/libvirt/images/
##開啟虛擬機器
[[email protected] mnt]# virsh create rhel7.2.xml 
Domain rhel7.2 created from rhel7.2.xml

將虛擬機器關機(force off)後;會發現虛擬機器消失了

##恢復虛擬機器
[[email protected] mnt]# virsh define rhel7.2.xml 
Domain rhel7.2 defined from rhel7.2.xml
##永久性刪除硬體資訊;不是刪除虛擬機器
[[email protected] mnt]# virsh undefine rhel7.2
[[email protected] mnt]# cd /var/lib/libvirt/images/
[[email protected] images]# ls
rh124-desktop-vda.ovl    rh124-server-vda.ovl    rhel7.2-2.qcow2  #
rh124-desktop-vdb.ovl    rh124-server-vdb.ovl    westos2.qcow2
rh124-desktop-vdb.qcow2  rh124-server-vdb.qcow2
rh124-desktop.xml        rh124-server.xml
##一次性恢復
rh[[email protected] mnt]# virsh create rhel7.2.xml
Domain rhel7.2 created from rhel7.2.xml
##永久性恢復
[[email protected] mnt]# virsh define rhel7.2.xml
Domain rhel7.2 defined from rhel7.2.xml124-desktop-vda.qcow2  rh124-server-vda.qcow2  westos1.qcow2
##刪除虛擬機器系統資訊(磁碟檔案),即刪除虛擬機器;此時虛擬機器便無法再恢復
[[email protected] images]# rm -rf /var/lib/libvirt/images/rhel7.2-2.qcow2 
注意:你所拷貝的虛擬機器不能和你已有的虛擬機器名稱重名,我這裡是因為前面的實驗主機與此實現主機不同,所以沒有影響。

3.虛擬機器快照的製作

qemu-img create -f qcow2 -b rhel7.2.qcow2 node1.qcow2

具體引數含義:
   create  #建立
   -f      #型別
   -b      #備份

方法1:命令的方式

@1製作快照

[[email protected] ~]# cd /var/lib/libvirt/images/
[[email protected] images]# ls

在這裡插入圖片描述

##製作快照
[[email protected] images]# qemu-img create -f qcow2 -b rhel7.2.qcow2 node1.qcow2
##檢視快照所佔磁碟空間大小
[[email protected] images]# du -sh node1.qcow2 

在這裡插入圖片描述

[[email protected] images]# ls

在這裡插入圖片描述

@2匯入快照

[[email protected] images]# virt-manager

在這裡插入圖片描述
在這裡插入圖片描述
在這裡插入圖片描述

在這裡插入圖片描述
在這裡插入圖片描述

如果虛擬機器快照node1誤刪了/ 此時虛擬機器便會起不來
在這裡插入圖片描述
刪除/之後,reboot命令都無法使用;此時用圖形強制關機再開機發現系統仍然無法使用
在這裡插入圖片描述

解決方案:

@1刪除壞掉的虛擬機器快照node1

##關閉虛擬機器
[[email protected] ~]# virsh destroy node1
##永久性刪除虛擬機器
[[email protected] ~]# virsh undefine node1
##刪除虛擬機器系統資訊
[[email protected] ~]# rm -rf /var/lib/libvirt/images/node1.qcow2

在這裡插入圖片描述

@2重新建立快照,再匯入即可

[[email protected] ~]# cd /var/lib/libvirt/images/
##建立快照
[[email protected] images]# qemu-img create -f qcow2 -b rhel7.2.qcow2 

在這裡插入圖片描述
因為壞掉的是快照,而它的母盤完好無損,所以刪除壞掉的快照,重新建立即可

(2).方法2:指令碼的方式

[[email protected] ~]# vim vm_snapshoot.sh
######################
#!/bin/bash           #幻數
qemu-img create \               #1製作快照(同命令方式)
-f qcow2 \
-b /var/lib/libvirt/images/$1.qcow2 \
/var/lib/libvirt/images/$2.qcow2 &> /dev/null \  

virt-install \                   #2匯入快照(同圖形介面時設定硬體資訊)
--name $2 \
--ram 1024 \
--vcpus 1 \
--disk /var/lib/libvirt/images/$2.qcow2,bus=virtio \
--network source=br0,model=virtio \
--import &> /dev/null &     # --import 表示匯入

在這裡插入圖片描述

[[email protected] ~]# chmod +x vm_snapshoot.sh
##執行指令碼
[[email protected] ~]# ./vm_snapshoot.sh rhel7.2 node1

在這裡插入圖片描述
排錯思想:

1.先將重定向 &>/dev/null 刪掉
2.執行指令碼,檢視報錯資訊
3.根據報錯資訊,更正指令碼內容