####1.安裝####
#!/bin/bash ##指令運作環境的指定
virt-install \ ##安裝虛拟機
--name $1 \ ##虛拟機名稱指定,$1表示腳本後的第一串字元
--ram 1000 \ ##記憶體
--file /var/lib/libvirt/images/$1.img \ ##硬碟檔案
--file-size 8 \ ##硬碟檔案大小
--cdrom /var/ftp/pub/iso/rhel-server-7.1-x86_64-dvd.iso & ##安裝源指定
a.編寫腳本内容
<a href="https://s1.51cto.com/oss/201710/24/bd8eb1d724990903b1ccf6e9e73132fe.png-wh_500x0-wm_3-wmp_4-s_2222806633.png" target="_blank"></a>
<a href="https://s3.51cto.com/oss/201710/24/383c17a4f70b90ec20747b24004448df.png-wh_500x0-wm_3-wmp_4-s_910995489.png" target="_blank"></a>
b.記得寫完後要給權限! chmod +x Create_Vm.sh
<a href="https://s5.51cto.com/oss/201710/24/4f1e730438a328591a8c67ea4cca5efe.png-wh_500x0-wm_3-wmp_4-s_3585114120.png" target="_blank"></a>
<a href="https://s4.51cto.com/oss/201710/24/5f77944902d43301ae54bd487c1175cc.png-wh_500x0-wm_3-wmp_4-s_3819659717.png" target="_blank"></a>
####2.管理####
virt-manager ##開啟圖形管理工具
virt-viewer vmname ##顯示虛拟機,vmname表示虛拟機名稱
virsh list ##列出正在運作的vm
virsh list --all ##列出所有vm
virsh start vmname ##運作指定vm
virsh shutdown vmname ##正常關閉指定vm
virsh destroy vmname ##強行結束指定vm
virsh create vmname.xml ##臨時恢複指定vm,vmname表示前端管理檔案
virsh define vmname.xml ##永久恢複vm
virsh undefine vmname ##删除vm的前端管理,不會删除存儲
<a href="https://s4.51cto.com/oss/201710/24/fc460f2897246d26729326e816e3abc7.png-wh_500x0-wm_3-wmp_4-s_939004239.png" target="_blank"></a>
####3.虛拟機快照####
有了快照可以讓虛拟機“永葆青春”,随時恢複……
qemu-img create -f qcow2 -b vm2.qcow2 node1.qcow2
<a href="https://s3.51cto.com/oss/201710/24/9f2db60acbc7fd68fbd46c1ad43f36d0.png-wh_500x0-wm_3-wmp_4-s_926799391.png" target="_blank"></a>
<a href="https://s4.51cto.com/oss/201710/24/6b8caf2a562d3933f296b310998c1bcd.png-wh_500x0-wm_3-wmp_4-s_582050612.png" target="_blank"></a>
<a href="https://s4.51cto.com/oss/201710/24/21dde7602cb8ffeb974e0d2c13796f7b.png-wh_500x0-wm_3-wmp_4-s_713833865.png" target="_blank"></a>
!/bin/bash
virsh destroy $1 &> /dev/null
virsh undefine $1 &> /dev/null
qemu-img create -f qcow2 -b /var/lib/libvirt/images/$1.qcow2 /var/lib/libvirt/images/$2.qcow2 &> /dev/null
virt-install \
--name $2 \
--ram 1000 \
--disk /var/lib/libvirt/images/$2.qcow2 \
--import &> /dev/null &
<a href="https://s4.51cto.com/oss/201710/24/610efaf391b397e8da2f5419d91d6d66.png-wh_500x0-wm_3-wmp_4-s_1225701719.png" target="_blank"></a>
[root@foundation12 sbin]# vim Createvm.sh
[root@foundation12 sbin]# chmod +x Createvm.sh
[root@foundation12 sbin]# ./Createvm.sh node2
<a href="https://s2.51cto.com/oss/201710/24/55094ea1a9c31a5ee9b818df099b1a0b.png-wh_500x0-wm_3-wmp_4-s_2798572482.png" target="_blank"></a>
以上指令建立虛拟機!!!
實驗:用以上指令建立node2虛拟機,建立好後打開虛拟機,在裡面執行删根指令,再次打開後無法正常使用。此時要想重建該虛拟機,可如下操作。
[root@foundation12 mnt]# vim Resetvm.sh
[root@foundation12 mnt]# chmod +x Resetvm.sh
[root@foundation12 mnt]# ./Resetvm.sh node2
<a href="https://s2.51cto.com/oss/201710/24/7270849eb0e5f4a9df686eae8c703b78.png-wh_500x0-wm_3-wmp_4-s_637466801.png" target="_blank"></a>
小記:
echo
<a href="https://s4.51cto.com/oss/201710/24/544c983bca0400461045b98b64018d03.png-wh_500x0-wm_3-wmp_4-s_1934737342.png" target="_blank"></a>
.xml
可擴充标記語言,可對資料進行封裝
本文轉自 huanzi2017 51CTO部落格,原文連結:http://blog.51cto.com/13362895/1975792