使用qemu-img管理虛擬機器磁碟映象(建立虛擬機器,虛擬機器快照)
一臺虛擬機器的核心就是一個磁碟映象,這個映象可以理解成虛擬機器的磁碟,裡面有虛擬機器的作業系統和驅動等重要檔案。本文主要介紹建立虛擬機器的一般過程。
建立虛擬機器映象
要在一臺host上跑起一個虛擬機器一般需要兩個步驟:
第一步:建立虛擬機器映象
qemu-img create -f raw /images/vm1.raw 8G
qmeu-img建立的映象是一個稀疏檔案,也就是說剛創建出來的檔案並沒有8G,它會隨著資料的增多慢慢增加,直到8G
第二步:啟動虛擬機器
kvm
執行結果: 因為映象裡面沒有任何內容,所以提示找不到可引導裝置。
使用qemu-img管理映象
qemu-img基本命令
上節介紹了使用qemu-img建立映象,這一節將會介紹qemu-img在映象管理上的強大功能。
qemu-img有很多命令,包括下面常用的,當然qemu-img -h你懂得。
info
檢視映象的資訊
create
建立映象
check
檢查映象
convert
轉化映象的格式,(raw,qcow ……)
snapshot
管理映象的快照
rebase
在已有的映象的基礎上建立新的映象
resize
增加或減小映象大小
建立映象
qemu-img create -f <fmt> -o <options> <fname> <size>
舉例:
qemu-img create -f raw -o size=4G /images/vm2.raw
terminal:~/images$ ll total 0-rw-r--r-- 1 hzgatt hzgatt 4.0G 6月 29 14:11 vm2.raw terminal:~/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:轉化後的檔案
看例子:
terminal:~/images$ qemu-img convert -c -O qcow2 vm2.raw vm2.qcow2 terminal:~/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 terminal:~/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 ?
terminal:~/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格式的映象才可以改變大小
terminal:~/images$ qemu-img resize vm2.raw +2GB
terminal:~/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 terminal:~/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
舉例:
terminal:~/images$ qemu-img snapshot -c booting vm2.qcow2 terminal:~/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)
當建立的虛擬機器越來越多,並且你發現好多虛擬機器都是同一個作業系統,它們的區別就是安裝的軟體不大一樣,那麼你肯定會希望把他們公共的部分提取出來,只儲存那些與公共部分不同的東西,這樣映象大小下去了,空間變多了,管理也方便了。派生映象就是用來幹這事的!
首先看一個原始映象
terminal:~/images$ qemu-img info vm3_base.raw image: vm3_base.raw file format: raw virtual size: 2.0G (2147483648 bytes) disk size: 2.0G
現在我們新建一個映象,但是派生自它
terminal:~/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
terminal:~/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
terminal:~/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...哈哈,這下怎麼辦呢?
terminal:~/images$ qemu-img convert -O raw vm3_5.qcow2 vm3_base2.raw
terminal:~/images$ qemu-img info vm3_base2.raw image: vm3_base2.raw file format: raw virtual size: 5.0G (5368709120 bytes) disk size: 592M