快照:
一。確認(rèn)虛擬機(jī)鏡像文件格式
[root@WWW.ESOJOURN.ORG vps]# qemu-img info centos1.img
image: centos1.img
file format: raw
virtual size: 200G (214748364800 bytes)
disk size: 6.5G
RAW格式是最原始的鏡像格式,好處是速度快。但不支持很多新的功能?,F(xiàn)在qcow2格式效率有很大提升了,而且還支持一些新的功能
1 更小的存儲(chǔ)空間,即使是不支持holes的文件系統(tǒng)也可以(這下du -h和ls -lh看到的就一樣了)
2 Copy-on-write support, where the image only represents changes made to an underlying disk image(這個(gè)特性SUN ZFS表現(xiàn)的淋漓盡致)
3 支持多個(gè)snapshot,對(duì)歷史snapshot進(jìn)行管理
4 支持zlib的磁盤壓縮
5 支持AES的加密
見
《qcow2、raw、vmdk等鏡像格式》對(duì)比。
二。想要啟用快照功能,需要先轉(zhuǎn)換鏡像文件格式為qcow2。
[root@WWW.ESOJOURN.ORG vps]# virsh shutdown esojourn.org
先關(guān)掉VM
[root@WWW.ESOJOURN.ORG vps]# qemu-img convert -f raw -O qcow2 centos1.img centos1qcow2.img
轉(zhuǎn)換格式
三。常用快照命令
這里有一份libvirt官方的命令文檔。完整,但說明不詳細(xì):
http://wiki.libvirt.org/page/VM_lifecycle具體示例:
1. 列出快照:
[root@WWW.ESOJOURN.ORG 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. 創(chuàng)建快照
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. 恢復(fù)快照
virsh snapshot-revert CentOS1 snap2
5. 刪除快照
birsh snapshot-delete CentOS1 snap2
6. 獲取幫助
virsh help snapshot
四。關(guān)于qemu-img snapshot -c和savevm
很多互相抄襲的教程里,都提到了使用qemu-img snapshot -c的命令來創(chuàng)建快照。但我自己測(cè)試的結(jié)果 ,不管虛擬機(jī)是運(yùn)行中,還是關(guān)閉狀態(tài),這個(gè)命令創(chuàng)建的快照字節(jié)都是0。也就是說什么也沒保存下來。對(duì)此,我還沒有找到原因。但找到Red hat員工Kashyap Chamarthy的一篇文章。文章里提到virsh在不同情況下,會(huì)調(diào)用不同方式來保存快照。其中至少包括‘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-only was not specified
- it uses qemu’s ‘savevm‘ if the domain is online and –disk-only was not specified
- it uses qemu’s ‘snapshot_blkdev‘ if the domain is online and –disk-only is specified
http://kashyapc.wordpress.com/2011/10/04/snapshotting-with-libvirt-for-qcow2-images/
qemu-img snapshot相關(guān)命令格式:
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
恢復(fù)快照:
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
確認(rèn) VM Image 格式
執(zhí)行 snapshot 的語法virsh snapshot-create vmname範(fàn)例:
[root@asus-ts100e7 ~]# virsh snapshot-create e-plast-mail錯(cuò)誤:Requested operation is not valid: Disk '/var/lib/libvirt/images/e-plast-mail.img' does not support snapshotting要能執(zhí)行 snapshot 的 VM image 必須是 qcow2 的格式, 出現(xiàn)這樣的訊息, 就要去確認(rèn)與轉(zhuǎn)換.
確認(rèn) image 的格式語法qemu-img info yourdisk.img範(fàn)例:
[root@asus-ts100e7 ~]# qemu-img info /var/lib/libvirt/images/e-plast-mail.imgimage: /var/lib/libvirt/images/e-plast-mail.imgfile format: rawvirtual size: 9.8G (10485760000 bytes)disk size: 9.8G
如果是 raw 要先轉(zhuǎn)成 qcow2 格式, 語法qemu-img convert -f raw -O qcow2 yourdisk.img newdisk.qcow2範(fàn)例:
[root@asus-ts100e7 ~]# qemu-img convert -f raw -O qcow2 /var/lib/libvirt/images/e-plast-mail.img /var/lib/libvirt/images/e-plast-mail.qcow2[root@asus-ts100e7 ~]# qemu-img info /var/lib/libvirt/images/e-plast-mail.qcow2image: /var/lib/libvirt/images/e-plast-mail.qcow2file format: qcow2virtual size: 9.8G (10485760000 bytes)disk size: 3.0Gcluster_size: 65536
更改 vm config file 範(fàn)例: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>:
重新啟動(dòng) vm 範(fàn)例:virsh restart e-plast-mail
建立 snapshot
目前版本進(jìn)行 snapshot 過程 vm 會(huì)無法運(yùn)作
執(zhí)行 snapshot 的語法virsh snapshot-create vmname範(fàn)例:
[root@asus-ts100e7 ~]# virsh snapshot-create e-plast-mailDomain snapshot 1349058343 created
這時(shí)會(huì)在 /var/lib/libvirt/qemu/snapshot/e-plast-mail 產(chǎn)生 1349058343.xml, 內(nèi)容如下<domainsnapshot> <name>1349058343</name> <state>running</state> <creationTime>1349058343</creationTime> <domain> <uuid>8dd0c9a8-c3d3-b6c2-1112-c7876db57444</uuid> </domain> <active>0</active></domainsnapshot>
查詢目前 snapshot
可以查看目前已經(jīng)存在多少份 snapshotvirsh snapshot-list e-plast-mail[root@asus-ts100e7 images]# virsh snapshot-list e-plast-mail 名稱 Creation Time 狀態(tài)--------------------------------------------------- 1349058343 2012-10-01 10:25:43 +0800 running 1349059256 2012-10-01 10:40:56 +0800 running
目前是使用哪個(gè) snapshot 版本virsh snapshot-current e-plast-mail[root@asus-ts100e7 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>
復(fù)原到特定版本 snapshot
經(jīng)過驗(yàn)證, libvirt 0.8.2-25.el5 在 VM 運(yùn)行中執(zhí)行 revert 後, VM 會(huì)當(dāng)?shù)魺o法運(yùn)作, 因此需要先關(guān)閉 VM 後再進(jìn)行 revert
確認(rèn) VM 目前運(yùn)作狀態(tài)virsh domstate e-plast-mail[root@asus-ts100e7 libvirt]# virsh domstate e-plast-mail執(zhí)行中
執(zhí)行關(guān)閉 VM 指令virsh shutdown e-plast-mail[root@asus-ts100e7 libvirt]# virsh shutdown e-plast-mail區(qū)域 e-plast-mail 正在執(zhí)行關(guān)機(jī)
確認(rèn) VM 目前已經(jīng)是關(guān)機(jī)狀態(tài)virsh domstate e-plast-mail[root@asus-ts100e7 save]# virsh domstate e-plast-mail關(guān)機(jī)
確定要回覆哪份 snapshot 版本virsh snapshot-list e-plast-mail[root@asus-ts100e7 save]# virsh snapshot-list e-plast-mail 名稱 Creation Time 狀態(tài)--------------------------------------------------- 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
執(zhí)行 snapshot-revert 指令virsh snapshot-revert e-plast-mail 1349071788
確認(rèn)目前執(zhí)行的 snapshot 版本virsh snapshot-current e-plast-mail[root@asus-ts100e7 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 會(huì)自動(dòng)啟動(dòng)在當(dāng)時(shí) snapshot-create 的狀態(tài)
原本在 1349071788 (2012-10-01 14:09:48) 時(shí)所執(zhí)行的程式還會(huì)繼續(xù)運(yùn)作
VM 內(nèi)的系統(tǒng)時(shí)間還是在 1349071788 (2012-10-01 14:09:48) 所以要考慮時(shí)間矯正議題
刪除不需要的 snapshot
原有的 snapshot 清單virsh snapshot-list e-plast-mail[root@asus-ts100e7 save]# virsh snapshot-list e-plast-mail 名稱 Creation Time 狀態(tài)--------------------------------------------------- 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[root@asus-ts100e7 save]# virsh snapshot-list e-plast-mail 名稱 Creation Time 狀態(tài)--------------------------------------------------- 1349058343 2012-10-01 10:25:43 +0800 running 1349071788 2012-10-01 14:09:48 +0800 running
snapshot 主要在 image file 內(nèi)增加 tag, 因此可以透過 qemu-img info 指令來瞭解qemu-img info /var/lib/libvirt/images/e-plast-mail.qcow2[root@asus-ts100e7 images]# qemu-img info /var/lib/libvirt/images/e-plast-mail.qcow2image: /var/lib/libvirt/images/e-plast-mail.qcow2file format: qcow2virtual size: 9.8G (10485760000 bytes)disk size: 7.0Gcluster_size: 65536Snapshot list:ID TAG VM SIZE DATE VM CLOCK1 1349058343 977M 2012-10-01 10:25:43 1290:29:38.0053 1349071788 965M 2012-10-01 14:09:48 1291:18:26.283
參考網(wǎng)址
http://forums.fedoraforum.org/showthread.php?t=260126http://blog.yam.com/keynes0918/article/45173318KVM虛擬機(jī)的基本鏡像和增量鏡像
KVM虛擬機(jī)的基本鏡像和增量鏡像
1、概述
實(shí)驗(yàn)?zāi)康模和ㄟ^一個(gè)基礎(chǔ)鏡像(node.img),里面把各個(gè)虛擬機(jī)都需要的環(huán)境都搭建好,然后基于這個(gè)鏡像建立起一個(gè)個(gè)增量鏡像,每個(gè)增量鏡像對(duì)應(yīng)一個(gè)虛擬機(jī),虛擬機(jī)對(duì)鏡像中所有的改變都記錄在增量鏡像里面,基礎(chǔ)鏡像始終保持不變。
功能:節(jié)省磁盤空間,快速復(fù)制虛擬機(jī)。
環(huán)境:
基本鏡像文件:node.img 虛擬機(jī)ID:node
增量鏡像文件:node4.img 虛擬機(jī)ID:node4
要求:以基本鏡像文件node.omg為基礎(chǔ),創(chuàng)建一個(gè)鏡像文件node4.img,以此創(chuàng)建一個(gè)虛擬機(jī)機(jī)node4,虛擬機(jī)node4的改變將存儲(chǔ)于node4.img中。
2、創(chuàng)建增量鏡像文件
[root@target kvm_node]#qemu-img create -b node.img -f qcow2 node4.img
[root@target 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)
#注:該實(shí)驗(yàn)只是針對(duì)qcow2格式的鏡像文件,未測(cè)試raw格式的鏡像文件是否可行。
3、創(chuàng)建虛擬機(jī)node4的XML配置文件
[root@target kvm_node]# cp /etc/libvirt/qemu/node.xml /etc/libvirt/qemu/node4.xml
[root@target kvm_node]# vim /etc/libvirt/qemu/node4.xml
<domain type='kvm'>
<name>node4</name> #node4的虛擬機(jī)名,須修改,否則與基本虛擬機(jī)沖突
<uuid>4b7e91eb-6521-c2c6-cc64-c1ba72707fe4</uuid> #node4的UUID,必須修改,否則與基本虛擬機(jī)沖突
<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'/> #修改網(wǎng)卡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'/> #修改網(wǎng)卡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、根據(jù)xml配置定義虛擬機(jī)node4
[root@target kvm_node]#virsh define /etc/libvirt/qemu/node4.xml
[root@target kvm_node]#virsh start node4
5、測(cè)試
[root@target kvm_node]# du -h node.img
6.3G node.img
[root@target kvm_node]# du -h node4.img
33M node4.img
[root@node4 ~]# dd if=/dev/zero of=test bs=1M count=200 #在虛擬機(jī)node4上增量200M大小文件
200+0 records in
200+0 records out
209715200 bytes (210 MB) copied, 1.00361 seconds, 209 MB/s
[root@target kvm_node]# du -h node.img #基本鏡像文件node.img大小未變
6.3G node.img
[root@target kvm_node]# du -h node.img #增量鏡像文件node4.img增加200M了
234M node4.img
復(fù)制 克?。?div style="height:15px;">
首先把需要克隆的源虛擬機(jī)先關(guān)閉,然后使用以下命令來進(jìn)行克隆,注意我這里使用的是相對(duì)路徑。
virsh shutdown VM02virt-clone -o VM02 -n VM05 -f VM05.img --connect=qemu:///systemchown qemu.qemu VM05.img需要修改一些東西,把 vnc 的端口號(hào)修改一下,避免兩個(gè)產(chǎn)生沖突,并記錄一下這里面的 MAC 地址備用。
virsh edit VM05先啟動(dòng) VM05,目前兩個(gè)虛擬機(jī)還不能同時(shí)啟動(dòng)。
virsh start VM05rm /etc/udev/rules.d/70-persistent-net.rulesvi /etc/sysconfig/network-scripts/ifcfg-eth0修改 eth0 的 MAC 地址與剛才 VM05 配置文件中的 MAC 一致,并重啟計(jì)算機(jī)。
這時(shí)再啟動(dòng) VM02(源虛擬機(jī))時(shí)會(huì)報(bào)以下錯(cuò)誤:
error: Failed to start domain VM02error: Unable to read from monitor: Connection reset by peer原因在于 IDE 的光驅(qū)設(shè)備不可共享產(chǎn)生了沖突所致,刪除 IDE 光驅(qū)即可。
virsh shutdown VM05virsh edit VM05刪除其中關(guān)于 ide cdrom 相關(guān)的一段設(shè)備描述,同時(shí)需注意內(nèi)存是否自己期望的大小。
virsh start VM05virsh start VM02源虛擬機(jī)與目標(biāo)虛擬機(jī)都沒有報(bào)錯(cuò),正常啟動(dòng),則本次克隆完成。
當(dāng)我們需要批量的部署我們的XEN 或者KVM的時(shí)候,LVM的snap功能是個(gè)不錯(cuò)的選擇.
#lvcreat -L 2G -s -n lv-virt2 /dev/vg01/lv-virt1
為/dev/vg01/lv-virt1創(chuàng)建一個(gè)大小為2G的快照lv-virt2
修改Domain-U的配置文件,uuid得用uuidgen生成.修改內(nèi)容很簡(jiǎn)單.
注意:依據(jù)寫時(shí)復(fù)制的原理,當(dāng)快照邏輯卷不能容納父卷改變的塊時(shí),快照將不可用.避免這種情況的發(fā)生,注意用lvdisplay查看及時(shí)用lvextend擴(kuò)展尺寸或者創(chuàng)建塊大小與父卷塊一致的不死快照.
系統(tǒng)版本
[root@desktop2 ~]# cat /proc/version
Linux version 2.6.32-220.el6.x86_64 (mockbuild@x86-004.build.bos.redhat.com) (gcc version 4.4.5 20110214 (Red Hat 4.4.5-6) (GCC) ) #1 SMP Wed Nov 9 08:03:13 EST 2011
[root@desktop2 ~]#
為虛擬機(jī)創(chuàng)建快照
[root@desktop2 ~]# 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
[root@desktop2 ~]# lvcreate -L 1G -s -n vserver2 /dev/vol0/vserver
Logical volume "vserver2" created
[root@desktop2 ~]# 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
[root@desktop2 ~]# uuidgen
1440f78a-3f93-4a84-be09-bef93c3188e3
導(dǎo)出vserver虛擬機(jī)的配置信息,并修改4處客戶化信息
[root@desktop2 ~]# virsh dumpxml vserver > /tmp/vserver2.xml
[root@desktop2 ~]# 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>
[root@desktop2 ~]#
生成克隆虛擬機(jī)
[root@desktop2 ~]# virsh define /tmp/vserver2.xml
Domain vserver2 defined from /tmp/vserver2.xml
啟動(dòng)克隆虛擬機(jī)
[root@desktop2 ~]# virsh start vserver2
Domain vserver2 started
[root@desktop2 ~]# virsh list
Id Name State
----------------------------------
3 vserver2 running
[root@server ~]# 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:cs@10.1.1.10/my.cfg"
為了是虛擬機(jī)可被外部網(wǎng)絡(luò)訪問,有時(shí)后nat不能滿足生產(chǎn)和實(shí)驗(yàn)要求,這時(shí)候就需要網(wǎng)卡的橋接設(shè)置了。
實(shí)驗(yàn)環(huán)境:
centos 5.5 64位 , DELL R510 32G RAM 2.4*8 XEON, KVM,
1、首先配置bridge,
#cd /etc/sysconfig/network-scripts/
# vim ifcfg-eth0
DEVICE=eth0ONBOOT=yesBRIDGE=br0HWADDR=b8:ac:6f:65:31:e5 #vim ifcfg-br0DEVICE=br0 TYPE=Bridge BOOTPROTO=staticONBOOT=yes IPADDR=10.10.21.70 NETMASK=255.255.255.192GATEWAY=10.10.21.1重啟network,在創(chuàng)建虛擬機(jī)的時(shí)候就可以選擇eth0 br0了,3、開啟防火墻允許通過這塊bridge設(shè)備轉(zhuǎn)發(fā)# iptables -I FORWARD -m physdev --physdev-is-bridged -j ACCEPT# service iptables save4、為linux kvm做的網(wǎng)絡(luò)bridge就好了,so easy!5、既然做好了bridge,就來進(jìn)行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自動(dòng)安裝的配置文件,可以省區(qū)平時(shí)自己安裝個(gè)系統(tǒng)的大量時(shí)間,當(dāng)然在進(jìn)行幾十臺(tái)上百臺(tái)的批量安裝時(shí)候,還是直接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:完全虛擬化,內(nèi)核虛擬機(jī),為什么很快?
1.內(nèi)存的調(diào)度直接交給內(nèi)核空間;
2.客戶機(jī)與客戶機(jī)是進(jìn)程與進(jìn)程之間的關(guān)系,減少了I/O;
3.cpu由硬件直接支持;
安裝kvm的條件:
1.RHEL6以上版本;
2.64位操作系統(tǒng);
3.最少2個(gè)GB的內(nèi)存;
4.CPU支持虛擬化,Inter vmx,AMD svm;
5.CPU支持物理地址擴(kuò)展,pae;
4、5項(xiàng)通過查看/proc/cpuinfo可知;
二、安裝
安裝內(nèi)核模塊:
#yum -y install kvm
安裝虛擬機(jī)庫,已經(jīng)管理工具:
#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
三、配置虛擬網(wǎng)絡(luò)
因默認(rèn)沒有配置虛擬網(wǎng)絡(luò);
新建br0跟物理網(wǎng)卡eth0橋接:br0 —橋接到—> eth0
1、關(guān)閉rhel6 NetworkManager服務(wù)
至少要關(guān)閉橋接網(wǎng)絡(luò)相關(guān)的設(shè)備(如eth0 bro<準(zhǔn)備建立的網(wǎng)絡(luò)>)
# chkconfig NetworkManager off
# service NetworkManager stop
2、建立橋接設(shè)備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 #注意字符大小寫,第一個(gè)字母大寫,其他小寫
NM_CONTROLLED="no" #明確指定不用NetworkManager服務(wù)管理
3、修改物理網(wǎng)卡配置文件:
# 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產(chǎn)生,如沒有,把libvirtd服務(wù)也重啟下
# service libvirtd start
四、客戶機(jī)安裝
例子:圖形界面安裝,同xen
安裝方式支持光盤,網(wǎng)絡(luò),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
五、在線遷移
現(xiàn)在在線遷移一般都有錯(cuò)誤,還沒有解決,官方也沒有文檔。待續(xù)…
使用qemu-img管理虛擬機(jī)磁盤鏡像(創(chuàng)建虛擬機(jī),虛擬機(jī)快照)分類:
LINUX 2013-01-03 15:40 364人閱讀
評(píng)論(0)
舉報(bào)目錄
(?)[+]一臺(tái)虛擬機(jī)的核心就是一個(gè)磁盤鏡像,這個(gè)鏡像可以理解成虛擬機(jī)的磁盤,里面有虛擬機(jī)的操作系統(tǒng)和驅(qū)動(dòng)等重要文件。本文主要介紹創(chuàng)建虛擬機(jī)的一般過程。
創(chuàng)建虛擬機(jī)鏡像
要在一臺(tái)host上跑起一個(gè)虛擬機(jī)一般需要兩個(gè)步驟:
第一步:創(chuàng)建虛擬機(jī)鏡像
qemu-img create -f raw /images/vm1.raw 8G
qmeu-img創(chuàng)建的鏡像是一個(gè)稀疏文件,也就是說剛創(chuàng)建出來的文件并沒有8G,它會(huì)隨著數(shù)據(jù)的增多慢慢增加,直到8G
第二步:?jiǎn)?dòng)虛擬機(jī)
kvm /imges/vm1.raw
運(yùn)行結(jié)果: 因?yàn)殓R像里面沒有任何內(nèi)容,所以提示找不到可引導(dǎo)設(shè)備。
使用qemu-img管理鏡像
qemu-img基本命令
上節(jié)介紹了使用qemu-img創(chuàng)建鏡像,這一節(jié)將會(huì)介紹qemu-img在鏡像管理上的強(qiáng)大功能。
qemu-img有很多命令,包括下面常用的,當(dāng)然qemu-img -h你懂得。
info
查看鏡像的信息
create
創(chuàng)建鏡像
check
檢查鏡像
convert
轉(zhuǎn)化鏡像的格式,(raw,qcow ……)
snapshot
管理鏡像的快照
rebase
在已有的鏡像的基礎(chǔ)上創(chuàng)建新的鏡像
resize
增加或減小鏡像大小
創(chuàng)建鏡像
qemu-img create -f <fmt> -o <options> <fname> <size>
舉例:
qemu-img create -f raw -o size=4G /images/vm2.raw
hzgatt@hzgatt:~/images$ lltotal 0-rw-r--r-- 1 hzgatt hzgatt 4.0G 6月 29 14:11 vm2.rawhzgatt@hzgatt:~/images$ ll -stotal 00 -rw-r--r-- 1 hzgatt hzgatt 4.0G 6月 29 14:11 vm2.raw
hzgatt@hzgatt:~/images$ qemu-img info vm2.raw image: vm2.rawfile format: rawvirtual size: 4.0G (4294967296 bytes)disk size: 0
雖然ls中看到文件的大小是4G,但是實(shí)際上磁盤大小是0。這就是稀疏文件
轉(zhuǎn)化
將一個(gè)鏡像文件轉(zhuǎn)化為另外一種格式,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
轉(zhuǎn)化命令:
qemu-img convert -c -f fmt -O out_fmt -o options fname out_fname
-c:采用壓縮,只有qcow和qcow2才支持
-f:源鏡像的格式,它會(huì)自動(dòng)檢測(cè),所以省略之
-O 目標(biāo)鏡像的格式
-o 其他選先
fname:源文件
out_fname:轉(zhuǎn)化后的文件
看例子:
hzgatt@hzgatt:~/images$ qemu-img convert -c -O qcow2 vm2.raw vm2.qcow2
hzgatt@hzgatt:~/images$ ll -stotal 136K 0 -rw-r--r-- 1 hzgatt hzgatt 5.0G 6月 29 13:55 vm1.raw136K -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
hzgatt@hzgatt:~/images$ qemu-img info vm2.qcow2 image: vm2.qcow2file format: qcow2virtual size: 4.0G (4294967296 bytes)disk size: 136Kcluster_size: 65536
如果想看要轉(zhuǎn)化的格式支持的-o選項(xiàng)有哪些,可以在命令末尾加上 -o ?
hzgatt@hzgatt:~/images$ qemu-img convert -c -O qcow2 vm2.raw vm2.qcow2 -o ?Supported options:size Virtual disk sizebacking_file File name of a base imagebacking_fmt Image format of the base imageencryption Encrypt the imagecluster_size qcow2 cluster sizepreallocation Preallocation mode (allowed values: off, metadata)
增加減少鏡像大小
注意:只有raw格式的鏡像才可以改變大小
hzgatt@hzgatt:~/images$ qemu-img resize vm2.raw +2GB
hzgatt@hzgatt:~/images$ ll -stotal 136K 0 -rw-r--r-- 1 hzgatt hzgatt 5.0G 6月 29 13:55 vm1.raw136K -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.rawhzgatt@hzgatt:~/images$ qemu-img info vm2.raw image: vm2.rawfile format: rawvirtual size: 6.0G (6442450944 bytes)disk size: 0
快照
查看快照
qemu-img snapshot -l /images/vm2.qcow2
注意:只有qcow2才支持快照
打快照
qemu-img snapshot -c booting vm2.qcow2
舉例:
hzgatt@hzgatt:~/images$ qemu-img snapshot -c booting vm2.qcow2 hzgatt@hzgatt:~/images$ qemu-img snapshot -l vm2.qcow2 Snapshot list:ID TAG VM SIZE DATE VM CLOCK1 booting 0 2012-06-29 14:35:04 00:00:00.000
從快照恢復(fù):
qemu-img snapshot -a 1 /images/vm2.qcow2
然后從kvm啟動(dòng)這個(gè)虛擬機(jī),會(huì)發(fā)現(xiàn)虛擬機(jī)又在打快照時(shí)的狀態(tài)了
刪除快照:
qemu-img snapshot -d 2 /images/vm2.qcow
使用派生鏡像(qcow2)
當(dāng)創(chuàng)建的虛擬機(jī)越來越多,并且你發(fā)現(xiàn)好多虛擬機(jī)都是同一個(gè)操作系統(tǒng),它們的區(qū)別就是安裝的軟件不大一樣,那么你肯定會(huì)希望把他們公共的部分提取出來,只保存那些與公共部分不同的東西,這樣鏡像大小下去了,空間變多了,管理也方便了。派生鏡像就是用來干這事的!
首先看一個(gè)原始鏡像
hzgatt@hzgatt:~/images$ qemu-img info vm3_base.raw image: vm3_base.rawfile format: rawvirtual size: 2.0G (2147483648 bytes)disk size: 2.0G
現(xiàn)在我們新建一個(gè)鏡像,但是派生自它
hzgatt@hzgatt:~/images$ qemu-img create -f qcow2 vm3_5.qcow2 -o backing_file=vm3_base.raw 5GFormatting 'vm3_5.qcow2', fmt=qcow2 size=5368709120 backing_file='vm3_base.raw' encryption=off cluster_size=65536
hzgatt@hzgatt:~/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
hzgatt@hzgatt:~/images$ qemu-img info vm3_5.qcow2 image: vm3_5.qcow2file format: qcow2virtual size: 5.0G (5368709120 bytes)disk size: 136Kcluster_size: 65536backing file: vm3_base.raw (actual path: vm3_base.raw)
^_^,這個(gè)鏡像才136K,夠省了吧。DRY永遠(yuǎn)的真理?。?div style="height:15px;">
現(xiàn)在我們?cè)趘m3_5.qcow2上打了很多安全補(bǔ)丁,然后發(fā)現(xiàn)我又想在vm3_5.qcow2上派生新的虛擬機(jī),o(∩∩)o...哈哈,這下怎么辦呢?
hzgatt@hzgatt:~/images$ qemu-img info vm3_base2.raw image: vm3_base2.rawfile format: rawvirtual size: 5.0G (5368709120 bytes)disk size: 592M
------------------------------------------------------------------------------------------------------
實(shí)測(cè)中,若虛擬機(jī)為啟動(dòng)狀態(tài)制作快照,恢復(fù)后會(huì)無法載入系統(tǒng),關(guān)閉服務(wù)器,再次啟動(dòng),服務(wù)器直接崩潰。
[root@asus-ts100e7 ~]# virsh snapshot-create e-plast-mail錯(cuò)誤:Requested operation is not valid: Disk '/var/lib/libvirt/images/e-plast-mail.img' does not support snapshotting
要能執(zhí)行 snapshot 的 VM image 必須是 qcow2 的格式, 出現(xiàn)這樣的訊息, 就要去確認(rèn)與轉(zhuǎn)換.
[root@asus-ts100e7 ~]# qemu-img info /var/lib/libvirt/images/e-plast-mail.imgimage: /var/lib/libvirt/images/e-plast-mail.imgfile format: rawvirtual size: 9.8G (10485760000 bytes)disk size: 9.8G
如果是 raw 要先轉(zhuǎn)成 qcow2 格式, 語法qemu-img convert -f raw -O qcow2 yourdisk.img newdisk.qcow2
[root@asus-ts100e7 ~]# qemu-img convert -f raw -O qcow2 /var/lib/libvirt/images/e-plast-mail.img /var/lib/libvirt/images/e-plast-mail.qcow2[root@asus-ts100e7 ~]# qemu-img info /var/lib/libvirt/images/e-plast-mail.qcow2image: /var/lib/libvirt/images/e-plast-mail.qcow2file format: qcow2virtual size: 9.8G (10485760000 bytes)disk size: 3.0Gcluster_size: 65536
更改 vm config file 範(fàn)例: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>:
[root@asus-ts100e7 ~]# virsh snapshot-create e-plast-mailDomain snapshot 1349058343 created
這時(shí)會(huì)在 /var/lib/libvirt/qemu/snapshot/e-plast-mail 產(chǎn)生 1349058343.xml, 內(nèi)容如下<domainsnapshot> <name>1349058343</name> <state>running</state> <creationTime>1349058343</creationTime> <domain> <uuid>8dd0c9a8-c3d3-b6c2-1112-c7876db57444</uuid> </domain> <active>0</active></domainsnapshot>
可以查看目前已經(jīng)存在多少份 snapshotvirsh snapshot-list e-plast-mail[root@asus-ts100e7 images]# virsh snapshot-list e-plast-mail 名稱 Creation Time 狀態(tài)--------------------------------------------------- 1349058343 2012-10-01 10:25:43 +0800 running 1349059256 2012-10-01 10:40:56 +0800 running
目前是使用哪個(gè) snapshot 版本virsh snapshot-current e-plast-mail[root@asus-ts100e7 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>
經(jīng)過驗(yàn)證, libvirt 0.8.2-25.el5 在 VM 運(yùn)行中執(zhí)行 revert 後, VM 會(huì)當(dāng)?shù)魺o法運(yùn)作, 因此需要先關(guān)閉 VM 後再進(jìn)行 revert
確認(rèn) VM 目前運(yùn)作狀態(tài)virsh domstate e-plast-mail[root@asus-ts100e7 libvirt]# virsh domstate e-plast-mail執(zhí)行中
執(zhí)行關(guān)閉 VM 指令virsh shutdown e-plast-mail[root@asus-ts100e7 libvirt]# virsh shutdown e-plast-mail區(qū)域 e-plast-mail 正在執(zhí)行關(guān)機(jī)
確認(rèn) VM 目前已經(jīng)是關(guān)機(jī)狀態(tài)virsh domstate e-plast-mail[root@asus-ts100e7 save]# virsh domstate e-plast-mail關(guān)機(jī)
確定要回覆哪份 snapshot 版本virsh snapshot-list e-plast-mail[root@asus-ts100e7 save]# virsh snapshot-list e-plast-mail 名稱 Creation Time 狀態(tài)--------------------------------------------------- 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
確認(rèn)目前執(zhí)行的 snapshot 版本virsh snapshot-current e-plast-mail[root@asus-ts100e7 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>
原有的 snapshot 清單virsh snapshot-list e-plast-mail[root@asus-ts100e7 save]# virsh snapshot-list e-plast-mail 名稱 Creation Time 狀態(tài)--------------------------------------------------- 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[root@asus-ts100e7 save]# virsh snapshot-list e-plast-mail 名稱 Creation Time 狀態(tài)--------------------------------------------------- 1349058343 2012-10-01 10:25:43 +0800 running 1349071788 2012-10-01 14:09:48 +0800 running
snapshot 主要在 image file 內(nèi)增加 tag, 因此可以透過 qemu-img info 指令來瞭解qemu-img info /var/lib/libvirt/images/e-plast-mail.qcow2[root@asus-ts100e7 images]# qemu-img info /var/lib/libvirt/images/e-plast-mail.qcow2image: /var/lib/libvirt/images/e-plast-mail.qcow2file format: qcow2virtual size: 9.8G (10485760000 bytes)disk size: 7.0Gcluster_size: 65536Snapshot list:ID TAG VM SIZE DATE VM CLOCK1 1349058343 977M 2012-10-01 10:25:43 1290:29:38.0053 1349071788 965M 2012-10-01 14:09:48 1291:18:26.283