欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

QEMU常用命令

程序员文章站 2022-06-04 08:52:28
...

QEMU常用命令:

1. 创建image,qcow2格式与raw格式的对比

[[email protected] qemu]# qemu-img create -f raw test.img 5G
Formatting 'test.img', fmt=raw size=5368709120 
[[email protected] qemu]# file test.img
test.img: data
[[email protected] qemu]# qemu-img info test.img
image: test.img
file format: raw
virtual size: 5.0G (5368709120 bytes)
disk size: 0
[[email protected] qemu]# qemu-img create -f qcow2 test2.img 10G
Formatting 'test2.img', fmt=qcow2 size=10737418240 encryption=off cluster_size=65536 lazy_refcounts=off 
[[email protected] qemu]# file test2.img
test2.img: QEMU QCOW Image (v3), 10737418240 bytes
[[email protected] qemu]# qemu-img info test2.img
image: test2.img
file format: qcow2
virtual size: 10G (10737418240 bytes)
disk size: 196K
cluster_size: 65536
Format specific information:
    compat: 1.1
    lazy refcounts: false
[[email protected] qemu]# ll -h test*
-rw-r--r-- 1 root root 193K Aug 30 14:53 test2.img
-rw-r--r-- 1 root root 5.0G Aug 30 14:53 test.img
[[email protected] qemu]# du -sh test*
196K	test2.img
0	test.img

2. 检查image文件:raw不支持check

[[email protected] qemu]# qemu-img check test.img 
qemu-img: This image format does not support checks
[[email protected] qemu]# qemu-img check test2.img
No errors were found on the image.
Image end offset: 262144

3. 对image文件的可操作性选项

[[email protected] qemu]# qemu-img create -f qcow2 -o ? test2.img
Supported options:
size             Virtual disk size
compat           Compatibility level (0.10 or 1.1)
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, falloc, full)
lazy_refcounts   Postpone refcount updates

4. 提交filename文件中的更改到后端支持镜像文件(创建时通过backing_file指定的)中去。

[[email protected] qemu]# qemu-img commit test2.img

5. 转换格式

[[email protected] qemu]# qemu-img convert -f qcow2 -O raw test2.img raw.img      #qcow2格式转换为raw格式
[[email protected] qemu]# qemu-img info raw.img 
image: raw.img
file format: raw
virtual size: 10G (10737418240 bytes)
disk size: 0
[[email protected] qemu]# qemu-img convert -f raw -O qcow2 raw.img qcow2.img      #raw格式转换为qcow2格式
[[email protected] qemu]# qemu-img info qcow2.img 
image: qcow2.img
file format: qcow2
virtual size: 10G (10737418240 bytes)
disk size: 196K
cluster_size: 65536
Format specific information:
    compat: 1.1
    lazy refcounts: false

6. 镜像扩容:只支持raw扩容

[[email protected] qemu]# qemu-img resize raw.img -/+1G
[[email protected] qemu]# qemu-img resize raw.img 11G
Image resized.
[[email protected] qemu]# qemu-img info raw.img 
image: raw.img
file format: raw
virtual size: 11G (11811160064 bytes)
disk size: 0
[[email protected] qemu]# qemu-img resize raw.img -1G
Image resized.
[[email protected] qemu]# qemu-img info raw.img 
image: raw.img
file format: raw
virtual size: 10G (10737418240 bytes)
disk size: 0

7. 快照操作

“-l” 选项是查询并列出镜像文件中的所有快照,“-a snapshot”是让镜像文件使用某个快照,“-c snapshot”是创建一个快照,“-d”是删除一个快照。
[[email protected] qemu]# qemu-img snapshot -l qcow2.img 
[[email protected] qemu]# qemu-img snapshot -c snapshot qcow2.img 
[[email protected] qemu]# ls
qcow2.img  raw.img  test2.img  test.img
[[email protected] qemu]# qemu-img snapshot -l qcow2.img 
Snapshot list:
ID        TAG                 VM SIZE                DATE       VM CLOCK
1         snapshot                  0 2017-08-30 15:18:25   00:00:00.000
qemu-img snapshot -a 1 qcow2.img
[[email protected] qemu]# qemu-img snapshot -d snapshot qcow2.img

8. 使用派生镜像

[[email protected] qemu]# qemu-img create -f qcow2 vm.qcow2 -o backing_file=qcow2.img
相关标签: QEMU