快照: 一。确認虛拟機鏡像檔案格式
[[email protected] vps]# qemu-img info centos1.img
image: centos1.img
file format: raw
virtual size: 200G (214748364800 bytes)
disk size: 6.5G
RAW格式是最原始的鏡像格式,好處是速度快。但不支援很多新的功能。現在qcow2格式效率有很大提升了,而且還支援一些新的功能
1 更小的存儲空間,即使是不支援holes的檔案系統也可以(這下du -h和ls -lh看到的就一樣了)
2 Copy-on-write support, where the image only represents changes made to an underlying disk image(這個特性SUN ZFS表現的淋漓盡緻)
3 支援多個snapshot,對曆史snapshot進行管理
4 支援zlib的磁盤壓縮
5 支援AES的加密
見《qcow2、raw、vmdk等鏡像格式》對比。
二。想要啟用快照功能,需要先轉換鏡像檔案格式為qcow2。
[[email protected] vps]# virsh shutdown esojourn.org
先關掉VM
[[email protected] vps]# qemu-img convert -f raw -O qcow2 centos1.img centos1qcow2.img
轉換格式
三。常用快照指令
這裡有一份libvirt官方的指令文檔。完整,但說明不詳細:http://wiki.libvirt.org/page/VM_lifecycle
具體示例:
1. 列出快照:
[[email protected] vps]# virsh snapshot-list CentOS1
Name Creation Time State
------------------------------------------------------------
centos1.snap1 2012-10-08 17:25:11 +0800 running
snap2 2012-10-08 17:33:14 +0800 running
snap3 2012-10-08 17:57:21 +0800 running
2. 建立快照
virsh snapshot-create-as CentOS1 snap2
virsh snapshot-create-as --domain CentOS1 --name snap2 --description "URL: www.esojourn.org"
3. 檢視快照配置
virsh snapshot-current CentOS1
4. 恢複快照
virsh snapshot-revert CentOS1 snap2
5. 删除快照
birsh snapshot-delete CentOS1 snap2
6. 擷取幫助
virsh help snapshot
四。關于qemu-img snapshot -c和savevm
很多互相抄襲的教程裡,都提到了使用qemu-img snapshot -c的指令來建立快照。但我自己測試的結果 ,不管虛拟機是運作中,還是關閉狀态,這個指令建立的快照位元組都是0。也就是說什麼也沒儲存下來。對此,我還沒有找到原因。但找到Red hat員工Kashyap Chamarthy的一篇文章。文章裡提到virsh在不同情況下,會調用不同方式來儲存快照。其中至少包括‘qemu-img snapshot -c‘,qemu的 ‘savevm‘和qemu的 ‘snapshot_blkdev‘這三種方式。是以看起來快照儲存,還是使用virsh snapshot-create的方式比較好。
原文引用
Also, discussed with Eric, in what cases does virsh invoke Qemu’s ‘savevm‘ and ‘qemu-img snapshot -c‘ commands while creating different types of snapshots discussed earlier above. Here is the outline:
- it uses ‘qemu-img snapshot -c‘ if the domain is offline and –disk-on ly was not specified
- it uses qemu’s ‘savevm‘ if the domain is on line and –disk-on ly was not specified
- it uses qemu’s ‘snapshot_blkdev‘ if the domain is on line and –disk-on ly is specified
http://kashyapc.wordpress.com/2011/10/04/snapshotting-with-libvirt-for-qcow2-images/
qemu-img snapshot相關指令格式:
qemu-img snapshot -c snap1 centos1-qcow2.img
qemu-img snapshot -l centos1-qcow2.img
Snapshot list:
ID TAG VM SIZE DATE VM CLOCK
1 snap1 0 2011-07-21 23:17:38 00:00:00.000
恢複快照:
qemu-img snapshot -a CentOS5.5_64bit_Qcow2_basesys.img CentOS5.5_64bit_Qcow2.img
其他操作:
'snapshot' is the name of the snapshot to create, apply or delete
'-a' applies a snapshot (revert disk to saved state)
'-c' creates a snapshot
'-d' deletes a snapshot
'-l' lists all snapshots in the given image
CentOS 6 KVM Snapshot
確認 VM Image 格式
- 執行 snapshot 的語法
範例:virsh snapshot-create vmname
要能執行 snapshot 的 VM image 必須是 qcow2 的格式, 出現這樣的訊息, 就要去確認與轉換.[[email protected] ~]# virsh snapshot-create e-plast-mail 錯誤:Requested operation is not valid: Disk '/var/lib/libvirt/images/e-plast-mail.img' does not support snapshotting
- 確認 image 的格式語法
範例:qemu-img info yourdisk.img
[[email protected] ~]# qemu-img info /var/lib/libvirt/images/e-plast-mail.img image: /var/lib/libvirt/images/e-plast-mail.img file format: raw virtual size: 9.8G (10485760000 bytes) disk size: 9.8G
- 如果是 raw 要先轉成 qcow2 格式, 語法
範例:qemu-img convert -f raw -O qcow2 yourdisk.img newdisk.qcow2
[[email protected] ~]# qemu-img convert -f raw -O qcow2 /var/lib/libvirt/images/e-plast-mail.img /var/lib/libvirt/images/e-plast-mail.qcow2 [[email protected] ~]# qemu-img info /var/lib/libvirt/images/e-plast-mail.qcow2 image: /var/lib/libvirt/images/e-plast-mail.qcow2 file format: qcow2 virtual size: 9.8G (10485760000 bytes) disk size: 3.0G cluster_size: 65536
- 更改 vm config file 範例:
virsh edit e-plast-mail
: <disk type='file' device='disk'> <driver name='qemu' type='qcow2' cache='none'/> <source file='/var/lib/libvirt/images/e-plast-mail.qcow2'/> <target dev='vda' bus='virtio'/> </disk> :
- 重新啟動 vm 範例:
virsh restart e-plast-mail
建立 snapshot
- 目前版本進行 snapshot 過程 vm 會無法運作
- 執行 snapshot 的語法
範例:virsh snapshot-create vmname
[[email protected] ~]# virsh snapshot-create e-plast-mail Domain snapshot 1349058343 created
- 這時會在 /var/lib/libvirt/qemu/snapshot/e-plast-mail 產生 1349058343.xml, 內容如下
<domainsnapshot> <name>1349058343</name> <state>running</state> <creationTime>1349058343</creationTime> <domain> <uuid>8dd0c9a8-c3d3-b6c2-1112-c7876db57444</uuid> </domain> <active>0</active> </domainsnapshot>
查詢目前 snapshot
- 可以檢視目前已經存在多少份 snapshot
virsh snapshot-list e-plast-mail
[[email protected] images]# virsh snapshot-list e-plast-mail 名稱 Creation Time 狀態 --------------------------------------------------- 1349058343 2012-10-01 10:25:43 +0800 running 1349059256 2012-10-01 10:40:56 +0800 running
- 目前是使用哪個 snapshot 版本
virsh snapshot-current e-plast-mail
[[email protected] images]# virsh snapshot-current e-plast-mail <domainsnapshot> <name>1349059256</name> <state>running</state> <parent> <name>1349058343</name> </parent> <creationTime>1349059256</creationTime> <domain> <uuid>8dd0c9a8-c3d3-b6c2-1112-c7876db57444</uuid> </domain> </domainsnapshot>
復原到特定版本 snapshot
- 經過驗證, libvirt 0.8.2-25.el5 在 VM 運行中執行 revert 後, VM 會當掉無法運作, 是以需要先關閉 VM 後再進行 revert
- 確認 VM 目前運作狀態
virsh domstate e-plast-mail
[[email protected] libvirt]# virsh domstate e-plast-mail 執行中
- 執行關閉 VM 指令
virsh shutdown e-plast-mail
[[email protected] libvirt]# virsh shutdown e-plast-mail 區域 e-plast-mail 正在執行關機
- 確認 VM 目前已經是關機狀態
virsh domstate e-plast-mail
[[email protected] save]# virsh domstate e-plast-mail 關機
- 確定要回覆哪份 snapshot 版本
virsh snapshot-list e-plast-mail
[[email protected] save]# virsh snapshot-list e-plast-mail 名稱 Creation Time 狀態 --------------------------------------------------- 1349058343 2012-10-01 10:25:43 +0800 running 1349059256 2012-10-01 10:40:56 +0800 running 1349071788 2012-10-01 14:09:48 +0800 running
- 執行 snapshot-revert 指令
virsh snapshot-revert e-plast-mail 1349071788
- 確認目前執行的 snapshot 版本
virsh snapshot-current e-plast-mail
[[email protected] save]# virsh snapshot-current e-plast-mail <domainsnapshot> <name>1349071788</name> <state>running</state> <parent> <name>1349059256</name> </parent> <creationTime>1349071788</creationTime> <domain> <uuid>8dd0c9a8-c3d3-b6c2-1112-c7876db57444</uuid> </domain> </domainsnapshot>
- revirt 後 VM 會自動啟動在當時 snapshot-create 的狀態
- 原本在 1349071788 (2012-10-01 14:09:48) 時所執行的程式還會繼續運作
- VM 內的系統時間還是在 1349071788 (2012-10-01 14:09:48) 是以要考慮時間矯正議題
刪除不需要的 snapshot
- 原有的 snapshot 清單
virsh snapshot-list e-plast-mail
[[email protected] save]# virsh snapshot-list e-plast-mail 名稱 Creation Time 狀態 --------------------------------------------------- 1349058343 2012-10-01 10:25:43 +0800 running 1349059256 2012-10-01 10:40:56 +0800 running 1349071788 2012-10-01 14:09:48 +0800 running
- 打算移除掉 1349059256 這份版本
virsh snapshot-delete e-plast-mail 1349059256
[[email protected] save]# virsh snapshot-list e-plast-mail 名稱 Creation Time 狀態 --------------------------------------------------- 1349058343 2012-10-01 10:25:43 +0800 running 1349071788 2012-10-01 14:09:48 +0800 running
- snapshot 主要在 image file 內增加 tag, 是以可以透過 qemu-img info 指令來瞭解
qemu-img info /var/lib/libvirt/images/e-plast-mail.qcow2
[[email protected] images]# qemu-img info /var/lib/libvirt/images/e-plast-mail.qcow2 image: /var/lib/libvirt/images/e-plast-mail.qcow2 file format: qcow2 virtual size: 9.8G (10485760000 bytes) disk size: 7.0G cluster_size: 65536 Snapshot list: ID TAG VM SIZE DATE VM CLOCK 1 1349058343 977M 2012-10-01 10:25:43 1290:29:38.005 3 1349071788 965M 2012-10-01 14:09:48 1291:18:26.283
參考網址
- http://forums.fedoraforum.org/showthread.php?t=260126
- http://blog.yam.com/keynes0918/article/45173318
KVM虛拟機的基本鏡像和增量鏡像
KVM虛拟機的基本鏡像和增量鏡像 1、概述 實驗目的:通過一個基礎鏡像(node.img),裡面把各個虛拟機都需要的環境都搭建好,然後基于這個鏡像建立起一個個增量鏡像,每個增量鏡像對應一個虛拟機,虛拟機對鏡像中所有的改變都記錄在增量鏡像裡面,基礎鏡像始終保持不變。 功能:節省磁盤空間,快速複制虛拟機。
環境: 基本鏡像檔案:node.img 虛拟機ID:node 增量鏡像檔案:node4.img 虛拟機ID:node4 要求:以基本鏡像檔案node.omg為基礎,建立一個鏡像檔案node4.img,以此建立一個虛拟機機node4,虛拟機node4的改變将存儲于node4.img中。
2、建立增量鏡像檔案 [[email protected] kvm_node]#qemu-img create -b node.img -f qcow2 node4.img [[email protected] kvm_node]# qemu-img info node4.img image: node4.img file format: qcow2 virtual size: 20G (21495808000 bytes) disk size: 33M cluster_size: 65536 backing file: node.img (actual path: node.img) #注:該實驗隻是針對qcow2格式的鏡像檔案,未測試raw格式的鏡像檔案是否可行。
3、建立虛拟機node4的XML配置檔案 [[email protected] kvm_node]# cp /etc/libvirt/qemu/node.xml /etc/libvirt/qemu/node4.xml [[email protected] kvm_node]# vim /etc/libvirt/qemu/node4.xml <domain type='kvm'> <name>node4</name> #node4的虛拟機名,須修改,否則與基本虛拟機沖突 <uuid>4b7e91eb-6521-c2c6-cc64-c1ba72707fe4</uuid> #node4的UUID,必須修改,否則與基本虛拟機沖突 <memory>524288</memory> <currentMemory>524288</currentMemory> <vcpu cpuset='0-1'>2</vcpu> <os> <type arch='x86_64' machine='rhel5.4.0'>hvm</type> <boot dev='hd'/> </os> <features> <acpi/> <apic/> <pae/> </features> <clock offset='localtime'/> <on_poweroff>destroy</on_poweroff> <on_reboot>restart</on_reboot> <on_crash>restart</on_crash> <devices> <emulator>/usr/libexec/qemu-kvm</emulator> <disk type='file' device='disk'> <driver name='qemu' type='qcow2'/> <source file='/virhost/kvm_node/node4.img'/> #将原指向/virhost/kvm_node/node.img改為node4.img <target dev='vda' bus='virtio'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/> </disk> <interface type='bridge'> <mac address='54:52:00:69:d5:f4'/> #修改網卡MAC,防止沖突 <source bridge='br0'/> <model type='virtio'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/> </interface> <interface type='bridge'> <mac address='54:52:00:69:d5:e4'/> #修改網卡MAC,防止沖突 <source bridge='br0'/> <model type='virtio'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/> </interface> <serial type='pty'> <target port='0'/> </serial> <console type='pty'> <target type='serial' port='0'/> </console> <input type='mouse' bus='ps2'/> <graphics type='vnc' port='5904' autoport='no' listen='0.0.0.0' passwd='xiaobai'> <listen type='address' address='0.0.0.0'/> </graphics> <video> <model type='cirrus' vram='9216' heads='1'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/> </video> <memballoon model='virtio'> <address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/> </memballoon> </devices> </domain>
4、根據xml配置定義虛拟機node4 [[email protected] kvm_node]#virsh define /etc/libvirt/qemu/node4.xml [[email protected] kvm_node]#virsh start node4
5、測試 [[email protected] kvm_node]# du -h node.img 6.3G node.img [[email protected] kvm_node]# du -h node4.img 33M node4.img [[email protected] ~]# dd if=/dev/zero of=test bs=1M count=200 #在虛拟機node4上增量200M大小檔案 200+0 records in 200+0 records out 209715200 bytes (210 MB) copied, 1.00361 seconds, 209 MB/s [[email protected] kvm_node]# du -h node.img #基本鏡像檔案node.img大小未變 6.3G node.img [[email protected] kvm_node]# du -h node.img #增量鏡像檔案node4.img增加200M了 234M node4.img
複制 克隆:
首先把需要克隆的源虛拟機先關閉,然後使用以下指令來進行克隆,注意我這裡使用的是相對路徑。
virsh shutdown VM02
virt-clone -o VM02 -n VM05 -f VM05.img --connect=qemu:///system
chown qemu.qemu VM05.img
需要修改一些東西,把 vnc 的端口号修改一下,避免兩個産生沖突,并記錄一下這裡面的 MAC 位址備用。
virsh edit VM05
先啟動 VM05,目前兩個虛拟機還不能同時啟動。
virsh start VM05
rm /etc/udev/rules.d/70-persistent-net.rules
vi /etc/sysconfig/network-scripts/ifcfg-eth0
修改 eth0 的 MAC 位址與剛才 VM05 配置檔案中的 MAC 一緻,并重新開機計算機。
這時再啟動 VM02(源虛拟機)時會報以下錯誤:
error: Failed to start domain VM02
error: Unable to read from monitor: Connection reset by peer
原因在于 IDE 的光驅裝置不可共享産生了沖突所緻,删除 IDE 光驅即可。
virsh shutdown VM05
virsh edit VM05
删除其中關于 ide cdrom 相關的一段裝置描述,同時需注意記憶體是否自己期望的大小。
virsh start VM05
virsh start VM02
源虛拟機與目标虛拟機都沒有報錯,正常啟動,則本次克隆完成。
當我們需要批量的部署我們的XEN 或者KVM的時候,LVM的snap功能是個不錯的選擇. #lvcreat -L 2G -s -n lv-virt2 /dev/vg01/lv-virt1 為/dev/vg01/lv-virt1建立一個大小為2G的快照lv-virt2 修改Domain-U的配置檔案,uuid得用uuidgen生成.修改内容很簡單. 注意:依據寫時複制的原理,當快照邏輯卷不能容納父卷改變的塊時,快照将不可用.避免這種情況的發生,注意用lvdisplay檢視及時用lvextend擴充尺寸或者建立塊大小與父卷塊一緻的不死快照.
![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsIiZpdmL1EDM1IDM4AjNxcjMxkTM0ADMxAjMvwFNwATMwIzLcRnbl1GajFGd0F2Lc12bj5yb0NWM14SMn1Wavw1LcpDc0RHaiojIsJye.gif)
系統版本
[[email protected] ~]# cat /proc/version
Linux version 2.6.32-220.el6.x86_64 ([email protected]) (gcc version 4.4.5 20110214 (Red Hat 4.4.5-6) (GCC) ) #1 SMP Wed Nov 9 08:03:13 EST 2011
[[email protected] ~]#
為虛拟機建立快照
[[email protected] ~]# lvs
LV VG Attr LSize Origin Snap% Move Log Copy% Convert
home vol0 -wi-ao 512.00m
root vol0 -wi-ao 8.00g
vserver vol0 owi-a- 10.00g
vserver-snap vol0 swi-a- 4.00g vserver 0.89
[r[email protected] ~]# lvcreate -L 1G -s -n vserver2 /dev/vol0/vserver
Logical volume "vserver2" created
[[email protected] ~]# lvs
LV VG Attr LSize Origin Snap% Move Log Copy% Convert
home vol0 -wi-ao 512.00m
root vol0 -wi-ao 8.00g
vserver vol0 owi-a- 10.00g
vserver-snap vol0 swi-a- 4.00g vserver 0.89
vserver2 vol0 swi-a- 1.00g vserver 0.00
生成新的uuid
[[email protected] ~]# uuidgen
1440f78a-3f93-4a84-be09-bef93c3188e3
導出vserver虛拟機的配置資訊,并修改4處客戶化資訊
[[email protected] ~]# virsh dumpxml vserver > /tmp/vserver2.xml
[[email protected] ~]# vim /tmp/vserver2.xml
<!--
WARNING: THIS IS AN AUTO-GENERATED FILE. CHANGES TO IT ARE LIKELY TO BE
OVERWRITTEN AND LOST. Changes to this xml configuration should be made using:
virsh edit vserver2
or other application using the libvirt API.
-->
<domain type='kvm'>
<name>vserver2</name>
<uuid>1440f78a-3f93-4a84-be09-bef93c3188e3</uuid>
<memory>2097152</memory>
<currentMemory>1048576</currentMemory>
<vcpu>2</vcpu>
<os>
<type arch='x86_64' machine='rhel6.2.0'>hvm</type>
<boot dev='hd'/>
</os>
<features>
<acpi/>
<apic/>
<pae/>
</features>
<clock offset='utc'/>
<on_poweroff>destroy</on_poweroff>
<on_reboot>restart</on_reboot>
<on_crash>restart</on_crash>
<devices>
<emulator>/usr/libexec/qemu-kvm</emulator>
<disk type='block' device='disk'>
<driver name='qemu' type='raw' cache='none' io='native'/>
<source dev='/dev/vol0/vserver2'/>
<target dev='vda' bus='virtio'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>
</disk>
<interface type='bridge'>
<mac address='52:54:00:00:00:02'/>
<source bridge='br0'/>
<model type='virtio'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
</interface>
<serial type='pty'>
<target port='0'/>
</serial>
<console type='pty'>
<target type='serial' port='0'/>
</console>
<input type='tablet' bus='usb'/>
<input type='mouse' bus='ps2'/>
<graphics type='vnc' port='-1' autoport='yes'/>
<video>
<model type='cirrus' vram='9216' heads='1'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>
</video>
<memballoon model='virtio'>
<address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/>
</memballoon>
</devices>
</domain>
[[email protected] ~]#
生成克隆虛拟機
[[email protected] ~]# virsh define /tmp/vserver2.xml
Domain vserver2 defined from /tmp/vserver2.xml
啟動克隆虛拟機
[[email protected] ~]# virsh start vserver2
Domain vserver2 started
[[email protected] ~]# virsh list
Id Name State
----------------------------------
3 vserver2 running
[[email protected] ~]# virt-install --debug --hvm --vnc --name virt1.example.com --os-type=linux --os-variant=rhel6 --pxe --network network=default,model=e1000,mac=02:54:00:13:be:e4 --disk pool=pool0,size=20 --ram 1024 --vcpus=1
virt-install --name=kvm3-rhel7 --disk path=/vm/kvm3/kvm1.img,size=12,sparse=true \
--graphics=vnc --vcpus=1 --ram=800 --network bridge=br0 --os-type=linux \
--os-variant=rhel6 --location=nfs:10.1.1.1:/share/rhel6.1 --extra-args "ks=ftp://cs:[email protected]/my.cfg"
為了是虛拟機可被外部網絡通路,有時後nat不能滿足生産和實驗要求,這時候就需要網卡的橋接設定了。 實驗環境:
centos 5.5 64位 , DELL R510 32G RAM 2.4*8 XEON, KVM, 1、首先配置bridge,
#cd /etc/sysconfig/network-scripts/ # vim ifcfg-eth0
DEVICE=eth0
ONBOOT=yes
BRIDGE=br0
HWADDR=b8:ac:6f:65:31:e5
#vim ifcfg-br0
DEVICE=br0
TYPE=Bridge
BOOTPROTO=static
ONBOOT=yes
IPADDR=10.10.21.70
NETMASK=255.255.255.192
GATEWAY=10.10.21.1
重新開機network,在建立虛拟機的時候就可以選擇eth0 br0了,
3、開啟防火牆允許通過這塊bridge裝置轉發
# iptables -I FORWARD -m physdev --physdev-is-bridged -j ACCEPT
# service iptables save
4、為linux kvm做的網絡bridge就好了,so easy!
5、既然做好了bridge,就來進行pxe安裝:
#virt-install --name centos5 --vcpus=2 --ram=2048 --accelerate --vnc --network bridge:br0 --disk path=/var/lib/libvirt/images/centos5.img,size=10 --network bridge:br0 --pxe
巧妙的利用ks.cfg自動安裝的配置檔案,可以省區平時自己安裝個系統的大量時間,當然在進行幾十台上百台的批量安裝時候,還是直接pxe-autoinstall好。
#virt-install --name centos5 --vcpus=2 --ram=2048 --accelerate --vnc --network bridge:br0 --disk path=/var/lib/libvirt/images/centos5.img,size=10 --network bridge:br0 --cdrom /iso/centos-5.5-dvd.iso -x "ks=ftp://10.10.21.1/pub/ks.cfg"
一、概念
Kvm:完全虛拟化,核心虛拟機,為什麼很快?
1.記憶體的排程直接交給核心空間;
2.客戶機與客戶機是程序與程序之間的關系,減少了I/O;
3.cpu由硬體直接支援;
安裝kvm的條件:
1.RHEL6以上版本;
2.64位作業系統;
3.最少2個GB的記憶體;
4.CPU支援虛拟化,Inter vmx,AMD svm;
5.CPU支援實體位址擴充,pae;
4、5項通過檢視/proc/cpuinfo可知;
二、安裝
安裝核心子產品:
#yum -y install kvm
安裝虛拟機庫,已經管理工具:
#yum -y install libvirt libvirt-python python-virtinst virt-viewer libvirt-client virt-manager
加載子產品:
#modprobe kvm
永久生效建議寫入/etc/rc.local:
#echo "modprobe kvm" >> /etc/rc.local
三、配置虛拟網絡
因預設沒有配置虛拟網絡;
建立br0跟實體網卡eth0橋接:br0 —橋接到—> eth0
1、關閉rhel6 NetworkManager服務
至少要關閉橋接網絡相關的裝置(如eth0 bro<準備建立的網絡>)
# chkconfig NetworkManager off
# service NetworkManager stop
2、建立橋接裝置br0
# vim /etc/sysconfig/network-scripts/ifcfg-br0
DEVICE=br0
ONBOOT=yes
BOOTPROTO=static
IPADDR=192.168.0.4
NETMASK=255.255.255.0
TYPE=Bridge #注意字元大小寫,第一個字母大寫,其他小寫
NM_CONTROLLED="no" #明确指定不用NetworkManager服務管理
3、修改實體網卡配置檔案:
# vim /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE="eth0"
BOOTPROTO="none"
HWADDR="48:5B:39:B9:41:31"
NM_CONTROLLED="no"
ONBOOT="yes"
BRIDGE=br0
# service network restart –>ifconfig檢查是否有br0産生,如沒有,把libvirtd服務也重新開機下
# service libvirtd start
四、客戶機安裝
例子:圖形界面安裝,同xen
安裝方式支援CD光牒,網絡,PXE等
# virt-manager & –>打開圖形管理視窗
例子:指令行安裝
# virt-install –help
# virt-install -v -n node2 –pxe –disk path=/var/lib/libvirt/images/node2.img,size=8 –network bridge=br0 –vnc –os-type=linux –os-variant=rhel5 -r 512 –vcpus=1
五、線上遷移
現在線上遷移一般都有錯誤,還沒有解決,官方也沒有文檔。待續…
使用qemu-img管理虛拟機磁盤鏡像(建立虛拟機,虛拟機快照)
分類: LINUX 2013-01-03 15:40 364人閱讀 評論(0) 收藏 舉報
目錄(?)[+]
一台虛拟機的核心就是一個磁盤鏡像,這個鏡像可以了解成虛拟機的磁盤,裡面有虛拟機的作業系統和驅動等重要檔案。本文主要介紹建立虛拟機的一般過程。
建立虛拟機鏡像
要在一台host上跑起一個虛拟機一般需要兩個步驟:
第一步:建立虛拟機鏡像
qemu-img create -f raw /images/vm1.raw 8G
qmeu-img建立的鏡像是一個稀疏檔案,也就是說剛建立出來的檔案并沒有8G,它會随着資料的增多慢慢增加,直到8G
第二步:啟動虛拟機
kvm /imges/vm1.raw
運作結果: 因為鏡像裡面沒有任何内容,是以提示找不到可引導裝置。
使用qemu-img管理鏡像
qemu-img基本指令
上節介紹了使用qemu-img建立鏡像,這一節将會介紹qemu-img在鏡像管理上的強大功能。
qemu-img有很多指令,包括下面常用的,當然qemu-img -h你懂得。
- 檢視鏡像的資訊
info
- 建立鏡像
- 檢查鏡像
- 轉化鏡像的格式,(raw,qcow ……)
- 管理鏡像的快照
- 在已有的鏡像的基礎上建立新的鏡像
- 增加或減小鏡像大小
create
check
convert
snapshot
rebase
resize
建立鏡像
qemu-img create -f <fmt> -o <options> <fname> <size>
舉例:
qemu-img create -f raw -o size=4G /images/vm2.raw
[email protected]:~/images$ ll
total 0-rw-r--r-- 1 hzgatt hzgatt 4.0G 6月 29 14:11 vm2.raw
[email protected]:~/images$ ll -s
total 00 -rw-r--r-- 1 hzgatt hzgatt 4.0G 6月 29 14:11 vm2.raw
[email protected]:~/images$ qemu-img info vm2.raw
image: vm2.raw
file format: raw
virtual size: 4.0G (4294967296 bytes)
disk size: 0
雖然ls中看到檔案的大小是4G,但是實際上磁盤大小是0。這就是稀疏檔案
轉化
将一個鏡像檔案轉化為另外一種格式,qemu-img支援的格式可以看qemu-img -h最後一行。
Supported formats: vvfat vpc vmdk vdi sheepdog rbd raw host_cdrom host_floppy host_device file qed qcow2 qcow parallels nbd dmg tftp ftps ftp https http cow cloop bochs blkverify blkdebug
轉化指令:
qemu-img convert -c -f fmt -O out_fmt -o options fname out_fname
-c:采用壓縮,隻有qcow和qcow2才支援
-f:源鏡像的格式,它會自動檢測,是以省略之
-O 目标鏡像的格式
-o 其他選先
fname:源檔案
out_fname:轉化後的檔案
看例子:
[email protected]:~/images$ qemu-img convert -c -O qcow2 vm2.raw vm2.qcow2
[email protected]:~/images$ ll -s
total 136K
0 -rw-r--r-- 1 hzgatt hzgatt 5.0G 6月 29 13:55 vm1.raw
136K -rw-r--r-- 1 hzgatt hzgatt 193K 6月 29 14:22 vm2.qcow2
0 -rw-r--r-- 1 hzgatt hzgatt 4.0G 6月 29 14:11 vm2.raw
[email protected]:~/images$ qemu-img info vm2.qcow2
image: vm2.qcow2
file format: qcow2
virtual size: 4.0G (4294967296 bytes)
disk size: 136K
cluster_size: 65536
如果想看要轉化的格式支援的-o選項有哪些,可以在指令末尾加上 -o ?
[email protected]:~/images$ qemu-img convert -c -O qcow2 vm2.raw vm2.qcow2 -o ?
Supported options:
size Virtual disk size
backing_file File name of a base image
backing_fmt Image format of the base image
encryption Encrypt the image
cluster_size qcow2 cluster size
preallocation Preallocation mode (allowed values: off, metadata)
增加減少鏡像大小
注意:隻有raw格式的鏡像才可以改變大小
[email protected]:~/images$ qemu-img resize vm2.raw +2GB
[email protected]:~/images$ ll -s
total 136K
0 -rw-r--r-- 1 hzgatt hzgatt 5.0G 6月 29 13:55 vm1.raw
136K -rw-r--r-- 1 hzgatt hzgatt 193K 6月 29 14:22 vm2.qcow2
0 -rw-r--r-- 1 hzgatt hzgatt 6.0G 6月 29 14:28 vm2.raw
[email protected]:~/images$ qemu-img info vm2.raw
image: vm2.raw
file format: raw
virtual size: 6.0G (6442450944 bytes)
disk size: 0
快照
檢視快照
qemu-img snapshot -l /images/vm2.qcow2
注意:隻有qcow2才支援快照
打快照
qemu-img snapshot -c booting vm2.qcow2
舉例:
[email protected]:~/images$ qemu-img snapshot -c booting vm2.qcow2
[email protected]:~/images$ qemu-img snapshot -l vm2.qcow2
Snapshot list:
ID TAG VM SIZE DATE VM CLOCK
1 booting 0 2012-06-29 14:35:04 00:00:00.000
從快照恢複:
qemu-img snapshot -a 1 /images/vm2.qcow2
然後從kvm啟動這個虛拟機,會發現虛拟機又在打快照時的狀态了
删除快照:
qemu-img snapshot -d 2 /images/vm2.qcow
使用派生鏡像(qcow2)
當建立的虛拟機越來越多,并且你發現好多虛拟機都是同一個作業系統,它們的差別就是安裝的軟體不大一樣,那麼你肯定會希望把他們公共的部分提取出來,隻儲存那些與公共部分不同的東西,這樣鏡像大小下去了,空間變多了,管理也友善了。派生鏡像就是用來幹這事的!
首先看一個原始鏡像
[email protected]:~/images$ qemu-img info vm3_base.raw
image: vm3_base.raw
file format: raw
virtual size: 2.0G (2147483648 bytes)
disk size: 2.0G
現在我們建立一個鏡像,但是派生自它
[email protected]:~/images$ qemu-img create -f qcow2 vm3_5.qcow2 -o backing_file=vm3_base.raw 5G
Formatting 'vm3_5.qcow2', fmt=qcow2 size=5368709120 backing_file='vm3_base.raw' encryption=off cluster_size=65536
[email protected]:~/images$ ll-rw-r--r-- 1 hzgatt hzgatt 193K 6月 29 15:00 vm3_5.qcow2
-rw-r--r-- 1 hzgatt hzgatt 2.0G 6月 29 14:51 vm3_base.raw
[email protected]:~/images$ qemu-img info vm3_5.qcow2
image: vm3_5.qcow2
file format: qcow2
virtual size: 5.0G (5368709120 bytes)
disk size: 136K
cluster_size: 65536
backing file: vm3_base.raw (actual path: vm3_base.raw)
^_^,這個鏡像才136K,夠省了吧。DRY永遠的真理啊!
現在我們在vm3_5.qcow2上打了很多安全更新檔,然後發現我又想在vm3_5.qcow2上派生新的虛拟機,o(∩∩)o...哈哈,這下怎麼辦呢?
[email protected]:~/images$ qemu-img convert -O raw vm3_5.qcow2 vm3_base2.raw
[email protected]:~/images$ qemu-img info vm3_base2.raw
image: vm3_base2.raw
file format: raw
virtual size: 5.0G (5368709120 bytes)
disk size: 592M
這個轉化将會将vm3_5和base合并,生成新的vm3_base2.raw,然後你就可以繼續無窮無盡的派生之旅了!
------------------------------------------------------------------------------------------------------
kvm快照應用 kvm也具有快速恢複的方法,前提是必須 處于關機狀态才可以執行,否則會出現各種莫名其妙的問題 建立鏡像: qemu-img snapshot -c initial smokeping_falcon_test0917.qcow2 恢複鏡像: qemu-img snapshot -a initial smokeping_falcon_test0917.qcow2 删除鏡像 qemu-img snapshot -d initial smokeping_falcon_test0917.qcow2 狀态檢視 qemu-img snapshot -l smokeping_falcon_test0917.qcow2 建立前的大小 [[email protected]]# ll total 7578488 -rw-r--r-- 1 root root 171825168384 Sep 17 17:01 smokeping_falcon_test0917.xml -rw-r--r-- 1 root root 171825168384 Sep 17 16:01 smokeping_falcon_test_187.qcow2 -rw-r--r-- 1 root root 171825168384 Sep 6 11:14 smokeping_falcon_test.qcow2 以下是建立後的檔案 [[email protected]]# ll total 7603284 -rw-r--r-- 1 qemu qemu 171850530816 Sep 17 17:22 smokeping_falcon_test0917.qcow2 -rw-r--r-- 1 root root 171825168384 Sep 17 16:01 smokeping_falcon_test_187.qcow2 -rw-r--r-- 1 root root 171825168384 Sep 6 11:14 smokeping_falcon_test.qcow2 檢視鏡像方法1 [[email protected]]# qemu-img info smokeping_falcon_test0917.qcow2 image: smokeping_falcon_test0917.qcow2 file format: qcow2 virtual size: 160G (171798691840 bytes) disk size: 2.2G cluster_size: 65536 Snapshot list: ID TAG VM SIZE DATE VM CLOCK 1 initial 0 2012-09-17 17:08:49 00:00:00.000 檢視鏡像方法2 [[email protected]]# qemu-img snapshot -l smokeping_falcon_test0917.qcow2 Snapshot list: ID TAG VM SIZE DATE VM CLOCK 1 initial 0 2012-09-17 17:08:49 00:00:00.000 利用這個鏡像,可以迅速還原伺服器狀态,使用空間也不大。 實測中,若虛拟機為啟動狀态制作快照,恢複後會無法載入系統,關閉伺服器,再次啟動,伺服器直接崩潰。
CentOS 6 KVM Snapshot
確認 VM Image 格式
- 執行 snapshot 的語法
範例:virsh snapshot-create vmname
要能執行 snapshot 的 VM image 必須是 qcow2 的格式, 出現這樣的訊息, 就要去確認與轉換.[[email protected] ~]# virsh snapshot-create e-plast-mail 錯誤:Requested operation is not valid: Disk '/var/lib/libvirt/images/e-plast-mail.img' does not support snapshotting
- 確認 image 的格式語法
範例:qemu-img info yourdisk.img
[[email protected] ~]# qemu-img info /var/lib/libvirt/images/e-plast-mail.img image: /var/lib/libvirt/images/e-plast-mail.img file format: raw virtual size: 9.8G (10485760000 bytes) disk size: 9.8G
- 如果是 raw 要先轉成 qcow2 格式, 語法
範例:qemu-img convert -f raw -O qcow2 yourdisk.img newdisk.qcow2
[[email protected] ~]# qemu-img convert -f raw -O qcow2 /var/lib/libvirt/images/e-plast-mail.img /var/lib/libvirt/images/e-plast-mail.qcow2 [[email protected] ~]# qemu-img info /var/lib/libvirt/images/e-plast-mail.qcow2 image: /var/lib/libvirt/images/e-plast-mail.qcow2 file format: qcow2 virtual size: 9.8G (10485760000 bytes) disk size: 3.0G cluster_size: 65536
- 更改 vm config file 範例:
virsh edit e-plast-mail
: <disk type='file' device='disk'> <driver name='qemu' type='qcow2' cache='none'/> <source file='/var/lib/libvirt/images/e-plast-mail.qcow2'/> <target dev='vda' bus='virtio'/> </disk> :
- 重新啟動 vm 範例:
virsh restart e-plast-mail
建立 snapshot
- 目前版本進行 snapshot 過程 vm 會無法運作
- 執行 snapshot 的語法
範例:virsh snapshot-create vmname
[[email protected] ~]# virsh snapshot-create e-plast-mail Domain snapshot 1349058343 created
- 這時會在 / var/ lib/ libvirt/ qemu/ snapshot/e-plast-mail 產生 1349058343.xml, 內容如下
<domainsnapshot> <name>1349058343</name> <state>running</state> <creationTime>1349058343</creationTime> <domain> <uuid>8dd0c9a8-c3d3-b6c2-1112-c7876db57444</uuid> </domain> <active>0</active> </domainsnapshot>
查詢目前 snapshot
- 可以檢視目前已經存在多少份 snapshot
virsh snapshot-list e-plast-mail
[[email protected] images]# virsh snapshot-list e-plast-mail 名稱 Creation Time 狀態 --------------------------------------------------- 1349058343 2012-10-01 10:25:43 +0800 running 1349059256 2012-10-01 10:40:56 +0800 running
- 目前是使用哪個 snapshot 版本
virsh snapshot-current e-plast-mail
[[email protected] images]# virsh snapshot-current e-plast-mail <domainsnapshot> <name>1349059256</name> <state>running</state> <parent> <name>1349058343</name> </parent> <creationTime>1349059256</creationTime> <domain> <uuid>8dd0c9a8-c3d3-b6c2-1112-c7876db57444</uuid> </domain> </domainsnapshot>
復原到特定版本 snapshot
- 經過驗證, libvirt 0.8.2-25.el5 在 VM 運行中執行 revert 後, VM 會當掉無法運作, 是以需要先關閉 VM 後再進行 revert
- 確認 VM 目前運作狀態
virsh domstate e-plast-mail
[[email protected] libvirt]# virsh domstate e-plast-mail 執行中
- 執行關閉 VM 指令
virsh shutdown e-plast-mail
[[email protected] libvirt]# virsh shutdown e-plast-mail 區域 e-plast-mail 正在執行關機
- 確認 VM 目前已經是關機狀態
virsh domstate e-plast-mail
[[email protected] save]# virsh domstate e-plast-mail 關機
- 確定要回覆哪份 snapshot 版本
virsh snapshot-list e-plast-mail
[[email protected] save]# virsh snapshot-list e-plast-mail 名稱 Creation Time 狀態 --------------------------------------------------- 1349058343 2012-10-01 10:25:43 +0800 running 1349059256 2012-10-01 10:40:56 +0800 running 1349071788 2012-10-01 14:09:48 +0800 running
- 執行 snapshot-revert 指令
virsh snapshot-revert e-plast-mail 1349071788
- 確認目前執行的 snapshot 版本
virsh snapshot-current e-plast-mail
[[email protected] save]# virsh snapshot-current e-plast-mail <domainsnapshot> <name>1349071788</name> <state>running</state> <parent> <name>1349059256</name> </parent> <creationTime>1349071788</creationTime> <domain> <uuid>8dd0c9a8-c3d3-b6c2-1112-c7876db57444</uuid> </domain> </domainsnapshot>
- revirt 後 VM 會自動啟動在當時 snapshot-create 的狀態
- 原本在 1349071788 (2012-10-01 14:09:48) 時所執行的程式還會繼續運作
- VM 內的系統時間還是在 1349071788 (2012-10-01 14:09:48) 是以要考慮時間矯正議題
刪除不需要的 snapshot
- 原有的 snapshot 清單
virsh snapshot-list e-plast-mail
[[email protected] save]# virsh snapshot-list e-plast-mail 名稱 Creation Time 狀態 --------------------------------------------------- 1349058343 2012-10-01 10:25:43 +0800 running 1349059256 2012-10-01 10:40:56 +0800 running 1349071788 2012-10-01 14:09:48 +0800 running
- 打算移除掉 1349059256 這份版本
virsh snapshot-delete e-plast-mail 1349059256
[[email protected] save]# virsh snapshot-list e-plast-mail 名稱 Creation Time 狀態 --------------------------------------------------- 1349058343 2012-10-01 10:25:43 +0800 running 1349071788 2012-10-01 14:09:48 +0800 running
- snapshot 主要在 image file 內增加 tag, 是以可以透過 qemu-img info 指令來瞭解
qemu-img info /var/lib/libvirt/images/e-plast-mail.qcow2
[[email protected] images]# qemu-img info /var/lib/libvirt/images/e-plast-mail.qcow2 image: /var/lib/libvirt/images/e-plast-mail.qcow2 file format: qcow2 virtual size: 9.8G (10485760000 bytes) disk size: 7.0G cluster_size: 65536 Snapshot list: ID TAG VM SIZE DATE VM CLOCK 1 1349058343 977M 2012-10-01 10:25:43 1290:29:38.005 3 1349071788 965M 2012-10-01 14:09:48 1291:18:26.283