使用qmeu-img管理虛擬機磁盤鏡像(創建虛擬機,虛擬機快照)
一台虛擬機的核心就是一個磁盤鏡像,這個鏡像可以理解成虛擬機的磁盤,里面有虛擬機的操作系統和驅動等重要文件。本文主要介紹創建虛擬機的一般過程。
創建虛擬機鏡像
要在一台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
-
查看鏡像的信息
-
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
hzgatt@hzgatt:~/images$ ll total 0-rw-r--r-- 1 hzgatt hzgatt 4.0G 6月 29 14:11 vm2.raw hzgatt@hzgatt:~/images$ ll -s total 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.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:轉化后的文件
看例子:
hzgatt@hzgatt:~/images$ qemu-img convert -c -O qcow2 vm2.raw vm2.qcow2
hzgatt@hzgatt:~/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
hzgatt@hzgatt:~/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 ?
hzgatt@hzgatt:~/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格式的鏡像才可以改變大小
hzgatt@hzgatt:~/images$ qemu-img resize vm2.raw +2GB
hzgatt@hzgatt:~/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 hzgatt@hzgatt:~/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
舉例:
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 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)
當創建的虛擬機越來越多,並且你發現好多虛擬機都是同一個操作系統,它們的區別就是安裝的軟件不大一樣,那么你肯定會希望把他們公共的部分提取出來,只保存那些與公共部分不同的東西,這樣鏡像大小下去了,空間變多了,管理也方便了。派生鏡像就是用來干這事的!
首先看一個原始鏡像
hzgatt@hzgatt:~/images$ qemu-img info vm3_base.raw image: vm3_base.raw file format: raw virtual size: 2.0G (2147483648 bytes) disk size: 2.0G
現在我們新建一個鏡像,但是派生自它
hzgatt@hzgatt:~/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
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.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...哈哈,這下怎么辦呢?
hzgatt@hzgatt:~/images$ qemu-img convert -O raw vm3_5.qcow2 vm3_base2.raw
hzgatt@hzgatt:~/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,然后你就可以繼續無窮無盡的派生之旅了!