KVM2-使用qcow2管理KVM虛拟機磁盤
1、建立一個基礎鏡像檔案
[root@server1 images]# pwd
/var/lib/libvirt/images //虛拟機預設建立的位置
[root@server1 images]# qemu-img create -f qcow2 rhel6test.img 80G
Formatting 'rhel6base.img', fmt=qcow2 size=85899345920 encryption=off cluster_size=65536
[root@server1 images]# vim ~/bin/install-rhel6
[root@server1 images]# cat ~/bin/install-rhel6
#!/bin/bash
/usr/sbin/virt-install \
--vnc \
--noautoconsole \
--name=rhel6test \
--ram=1024 \
--arch=x86_64 \
--vcpus=1 \
--os-type=linux \
--os-variant=rhel6 \
--hvm \
--accelerate \
--disk=/var/lib/libvirt/images/rhel6test.img \
-m 52:54:00:00:01:02 \
-w bridge=br0 \
--location=ftp://192.168.3.1/rhel6 \
--extra-args="ks=ftp://192.168.3.1/ks.cfg"
[root@server1 images]#
2、安裝系統到鏡像檔案
[root@server1images]# install-rhel6
Startinginstall...
Retrievingfile vmlinuz... | 7.7 MB 00:00 ...
Retrievingfile initrd.img... | 60 MB 00:00 ...
Creatingdomain... | 0 B 00:00
Domaininstallation still in progress. You can reconnect to
theconsole to complete the installation process.
[root@server1images]#
3、基于基礎鏡像檔案建立增量鏡像檔案---克隆機器
[root@server1images]# qemu-img create -b rhel6test.img -f qcow2 rhel6testnode1.img
Formatting'rhel6testnode1.img', fmt=qcow2 size=85899345920 backing_file='rhel6test.img'encryption=off cluster_size=65536
4、導出原始虛拟機的配置檔案說明
檢視虛拟機rhel6test的配置資訊:[root@server1 images]# virsh dumpxml rhel6test
将配置資訊作為模闆導出:
[root@server1images]# virsh dumpxml rhel6test > /tmp/rhel6testnode1.xml
5、修改配置檔案,以滿足克隆虛拟機的要求
[root@server1images]# uuidgen
3b69e07e-95a9-48eb-b95d-52507ad864e0
[root@server1images]# vim /tmp/rhel6testnode1.xml //一共修改四處
<name>rhel6testnode1</name>
<uuid>3b69e07e-95a9-48eb-b95d-52507ad864e0</uuid>
<source file='/var/lib/libvirt/images/rhel6testnode1.img'/>
<mac address='52:54:00:00:01:03'/>
6、根據配置檔案定義新的克隆虛拟機
[root@server1images]# virsh define /tmp/rhel6testnode1.xml
附:實作自動建立克隆虛拟機的腳本:等同步驟3-6
[root@server1 ~]# vim bin/clone-rhel6
[root@server1 ~]# cat bin/clone-rhel6
qemu-img create -b /var/lib/libvirt/images/rhel6test.img -f qcow2 /var/lib/libvirt/images/rhel6testnode1.img
virsh dumpxml rhel6test > /tmp/rhel6test.xml
sed -i '/<name>rhel6test/s/rhel6test/rhel6testnode1/' /tmp/rhel6test.xml
sed -i "/<uuid>/s/<uuid>.\{36\}/<uuid>$(uuidgen)/" /tmp/rhel6test.xml
sed -i "/rhel6test\.img/s/rhel6test\.img/rhel6testnode1\.img/" /tmp/rhel6test.xml
sed -i "/mac address/s/00'/03'/" /tmp/rhel6test.xml
virsh define /tmp/rhel6test.xml
經過優化後的腳本
實作功能:
1,支援判斷使用者輸入的字元判斷,可用範圍為(1-254)
2,多餘輸出資訊導入到/dev/null中
3,支援輸出錯誤編碼$?
4,如果需要修改原虛拟機名,直接修改$BASE_VM的值即可
5,支出輸出資訊[OK]高亮顯示
[root@server1 ~]# cat bin/clone-rhel6.sh
IMG_DIR=/var/lib/libvirt/images //定義變量,虛拟機檔案存放的位置
BASE_VM=rhel6test //定義變量,基礎虛拟機的名字
E_NOINPUT=65 //定義輸出錯誤編碼
E_NOTNUM=66
E_OUT_OF_RANGE=67
E_VM_EXISTS=68
read -p "please input a vm number(0-254): " NEW_VMNU //互動式輸入增量虛拟機的編号
if [ -z $NEW_VMNU ]; then //判斷編号是否為空
echo "You must input a number."
exit $E_NOINPUT
fi
if [ $NEW_VMNU != $(echo "$NEW_VMNU*1" | bc) ]; then //判斷編号是否為字元
exit $E_NOTNUM
if [ $NEW_VMNU -lt 1 -o $NEW_VMNU -gt 254 ]; then //判斷編号是否超出了1-254範圍
echo "You must input a number between 1 and 254"
exit $E_OUT_OF_RANGE
NEW_VM=${BASE_VM}node${NEW_VMNU} //定義增量虛拟機的名字
if [ -e $IMG_DIR/$NEW_VM.img ]; then //判斷增量虛拟機編号是否重複
echo "$NEW_VM already exists"
exit $E_VM_EXISTS
echo -en "creating disk image......\t\t" //-en選項,不換行輸出,為了連接配接下面的ok
qemu-img create -b $IMG_DIR/$BASE_VM.img -f qcow2 $IMG_DIR/$NEW_VM.img &> /dev/null
echo -e "\e[32m[OK]\e[0m" //高亮顯示輸出内容:[ok]
echo //echo後面不接選項和參數,輸出空行
virsh dumpxml $BASE_VM > /tmp/$BASE_VM.xml //生成克隆虛拟機XML檔案
MAC_ADDR=$(echo "obase=16;$NEW_VMNU" | bc) //定義mac位址後兩位數字
sed -i "/<name>$BASE_VM/s/$BASE_VM/$NEW_VM/" /tmp/$BASE_VM.xml //XML檔案需要修改的四處
sed -i "/<uuid>/s/<uuid>.\{36\}/<uuid>$(uuidgen)/" /tmp/$BASE_VM.xml
sed -i "/$BASE_VM\.img/s/$BASE_VM\.img/$NEW_VM\.img/" /tmp/$BASE_VM.xml
sed -i "/mac address/s/00'/$MAC_ADDR'/" /tmp/$BASE_VM.xml
virsh define /tmp/$BASE_VM.xml &> /dev/null //定義新的虛拟機
echo "$NEW_VM created successful !!!"
[root@server1 ~]#
注:其實核心句子就是綠色字型的四個步驟
本文轉自 murongqingqqq 51CTO部落格,原文連結:http://blog.51cto.com/murongqingqqq/1396739