通过KVM创建虚拟机,用到的命令不多,而且可以通过qemu-img -help查看到非常详细的解释,常用的主要有以下几种:

1、qemu-img info  查看磁盘信息     #info [-f fmt] [--output=ofmt] [--backing-chain] filename

1 [root@KVM03-10 opt]# qemu-img info  centos2.1.raw
2 image: centos2.1.raw
3 file format: raw
4 virtual size: 10G (10737418240 bytes)
5 disk size: 1.2G

磁盘文件类型:

RAW:裸格式,连续,不支持快照等高级功能,但性能优异;

qcow2:支持快照,非连续,性能较好,qcow2,q=qemu,cow=copy on write 写时复制,写多少内容,占用多少磁盘空间

2、qemu-img create 创建磁盘         #create [-q] [-f fmt] [-o options] filename [size]

 1 [root@KVM03-10 opt]# qemu-img --help
2 qemu-img version 1.5.3, Copyright (c) 2004-2008 Fabrice Bellard
3 usage: qemu-img command [command options]
4 QEMU disk image utility
5
6 Command syntax:
7 check [-q] [-f fmt] [--output=ofmt] [-r [leaks | all]] [-T src_cache] filename
8 create [-q] [-f fmt] [-o options] filename [size]
9 commit [-q] [-f fmt] [-t cache] filename
10 compare [-f fmt] [-F fmt] [-T src_cache] [-p] [-q] [-s] filename1 filename2
11 convert [-c] [-p] [-q] [-n] [-f fmt] [-t cache] [-T src_cache] [-O output_fmt] [-o options] [-s snapshot_name] [-S sparse_size] filename [filename2 [...]] output_filename
12 info [-f fmt] [--output=ofmt] [--backing-chain] filename
13 map [-f fmt] [--output=ofmt] filename
14 snapshot [-q] [-l | -a snapshot | -c snapshot | -d snapshot] filename
15 rebase [-q] [-f fmt] [-t cache] [-T src_cache] [-p] [-u] -b backing_file [-F backing_fmt] filename
16 resize [-q] filename [+ | -]size
17 amend [-q] [-f fmt] [-t cache] -o options filename
18
19 Command parameters:
20 'filename' is a disk image filename
21 'fmt' is the disk image format. It is guessed automatically in most cases
22 'cache' is the cache mode used to write the output disk image, the valid
23 options are: 'none', 'writeback' (default, except for convert), 'writethrough',
24 'directsync' and 'unsafe' (default for convert)
25 'src_cache' is the cache mode used to read input disk images, the valid
26 options are the same as for the 'cache' option
27 'size' is the disk image size in bytes. Optional suffixes
28 'k' or 'K' (kilobyte, 1024), 'M' (megabyte, 1024k), 'G' (gigabyte, 1024M),
29 'T' (terabyte, 1024G), 'P' (petabyte, 1024T) and 'E' (exabyte, 1024P) are
30 supported. 'b' is ignored.
31 'output_filename' is the destination disk image filename
32 'output_fmt' is the destination format
33 'options' is a comma separated list of format specific options in a
34 name=value format. Use -o ? for an overview of the options supported by the
35 used format
36 '-c' indicates that target image must be compressed (qcow format only)
37 '-u' enables unsafe rebasing. It is assumed that old and new backing file
38 match exactly. The image doesn't need a working backing file before
39 rebasing in this case (useful for renaming the backing file)
40 '-h' with or without a command shows this help and lists the supported formats
41 '-p' show progress of command (only certain commands)
42 '-q' use Quiet mode - do not print any output (except errors)
43 '-S' indicates the consecutive number of bytes (defaults to 4k) that must
44 contain only zeros for qemu-img to create a sparse image during
45 conversion. If the number of bytes is 0, the source will not be scanned for
46 unallocated or zero sectors, and the destination image will always be
47 fully allocated
48 '--output' takes the format in which the output must be done (human or json)
49 '-n' skips the target volume creation (useful if the volume is created
50 prior to running qemu-img)
51
52 Parameters to check subcommand:
53 '-r' tries to repair any inconsistencies that are found during the check.
54 '-r leaks' repairs only cluster leaks, whereas '-r all' fixes all
55 kinds of errors, with a higher risk of choosing the wrong fix or
56 hiding corruption that has already occurred.
57
58 Parameters to snapshot subcommand:
59 'snapshot' is the name of the snapshot to create, apply or delete
60 '-a' applies a snapshot (revert disk to saved state)
61 '-c' creates a snapshot
62 '-d' deletes a snapshot
63 '-l' lists all snapshots in the given image
64
65 Parameters to compare subcommand:
66 '-f' first image format
67 '-F' second image format
68 '-s' run in Strict mode - fail on different image size or sector allocation
69
70 Supported formats: vvfat vpc vmdk vhdx vdi ssh sheepdog rbd raw host_cdrom host_floppy host_device file qed qcow2 qcow parallels nbd iscsi gluster dmg tftp ftps ftp https http cloop bochs blkverify blkdebug

[root@KVM03-10 ~]# qemu-img create /opt/test.raw 5G

#/opt/test.raw   虚拟磁盘存放的位置和文件名;

# 5G 虚拟磁盘大小;

[root@KVM03-10 ~]# qemu-img create /opt/test.raw
qemu-img: /opt/test.raw: Image creation needs a size parameter
[root@KVM03-10 ~]# qemu-img create /opt/test.raw 5G
Formatting '/opt/test.raw', fmt=raw size=5368709120
[root@KVM03-10 opt]#
[root@KVM03-10 opt]# ll -h
total 5.3G
-rw-------. 1 qemu qemu 10G Oct 11 10:32 centos2.1.raw
-rw-r--r--. 1 qemu qemu 4.1G Apr 27 20:56 CentOS-7.3-x86_64-DVD-1611.iso
-rw-r--r--. 1 root root 5.0G Oct 11 10:37 test.raw
[root@KVM03-10 opt]#

[root@KVM03-10 opt]# qemu-img info test.raw
image: test.raw
file format: raw
virtual size: 5.0G (5368709120 bytes)
disk size: 0
[root@KVM03-10 opt]

其实以前Linux的dd命令同样可用于创建和负责磁盘并实现转换,具体可以man dd阅读或参照前辈的博客:https://www.cnblogs.com/ginvip/p/6370836.html

[root@KVM03-10 dev]# dd if=/dev/zero of=/opt/test-dd.raw bs=100M count=4
4+0 records in
4+0 records out
419430400 bytes (419 MB) copied, 1.93646 s, 217 MB/s
[root@KVM03-10 dev]# cd /opt/
[root@KVM03-10 opt]# ll -h
total 5.7G
-rw-------. 1 qemu qemu 10G Oct 11 10:46 centos2.1.raw
-rw-r--r--. 1 qemu qemu 4.1G Apr 27 20:56 CentOS-7.3-x86_64-DVD-1611.iso
-rw-r--r--. 1 root root 400M Oct 11 10:58 test-dd.raw
-rw-r--r--. 1 root root 5.0G Oct 11 10:50 test.raw
[root@KVM03-10 opt]# qemu-img info test-dd.raw
image: test-dd.raw
file format: raw
virtual size: 400M (419430400 bytes)
disk size: 400M
[root@KVM03-10 opt]#

发现在不加磁盘类型参数的时候,默认创建的是raw类型磁盘,raw和qcow2格式各有优缺点,在创建的过程中可以指定磁盘格式,也可以创建完成后进行磁盘格式装换。下面就学习一下磁盘转换的命令。

create [-q] [-f fmt] [-o options] filename [size]

'fmt' is the disk image format. It is guessed automatically in most cases

[root@KVM03-10 opt]# qemu-img  create -f qcow2 /opt/test.qcow2 2G
Formatting '/opt/test.qcow2', fmt=qcow2 size=2147483648 encryption=off cluster_size=65536 lazy_refcounts=off
[root@KVM03-10 opt]# ll
total 5520240
-rw-------. 1 qemu qemu 10737418240 Oct 11 11:21 centos2.1.raw
-rw-r--r--. 1 qemu qemu 4379901952 Apr 27 20:56 CentOS-7.3-x86_64-DVD-1611.iso
-rw-r--r--. 1 root root 5368709120 Oct 11 11:19 test-1.raw
-rw-r--r--. 1 root root 5368709120 Oct 11 11:15 test-2.raw
-rw-r--r--. 1 root root 197120 Oct 11 11:24 test.qcow2
-rw-r--r--. 1 root root 5368709120 Oct 11 10:50 test.raw
[root@KVM03-10 opt]# qemu-img info test.qcow2
image: test.qcow2
file format: qcow2 #文件类型:qcow2
virtual size: 2.0G (2147483648 bytes)      #虚拟磁盘大小:2G
disk size: 196K
cluster_size: 65536
Format specific information:
compat: 1.1
lazy refcounts: false

3、磁盘格式转换

  convert [-c] [-p] [-q] [-n] [-f fmt] [-t cache] [-T src_cache] [-O output_fmt] [-o options] [-s snapshot_name] [-S sparse_size] filename [filename2 [...]] output_filename

# -f 原磁盘格式

# -O 目标磁盘格式

[root@KVM03-10 opt]# qemu-img convert -f raw -O qcow2 test.raw raw-to-qcow2.qcow2
[root@KVM03-10 opt]# ll
total 5509896
-rw-------. 1 qemu qemu 10737418240 Oct 11 11:51 centos2.1.raw
-rw-r--r--. 1 qemu qemu 4379901952 Apr 27 20:56 CentOS-7.3-x86_64-DVD-1611.iso
-rw-r--r--. 1 root root 197120 Oct 11 11:55 raw-to-qcow2.qcow2
-rw-r--r--. 1 root root 1073741824 Oct 11 11:35 resize.raw
-rw-r--r--. 1 root root 262656 Oct 11 11:36 test.qcow2
-rw-r--r--. 1 root root 5368709120 Oct 11 10:50 test.raw
[root@KVM03-10 opt]#
[root@KVM03-10 opt]# qemu-img info raw-to-qcow2.qcow2
image: raw-to-qcow2.qcow2
file format: qcow2
virtual size: 5.0G (5368709120 bytes)
disk size: 196K
cluster_size: 65536
Format specific information:
compat: 1.1
lazy refcounts: false
[root@KVM03-10 opt]#

转换以后,不会对原文件有任何修改,而是新增一个文件目标磁盘格式的文件出来。这与电脑视频转换软件类似,转换以后原文件还在。

对文件进行转换后,修改虚机的xml文件中使用的磁盘文件,即可实现使用qcow2类型的磁盘文件启动。

  <devices>
<emulator>/usr/libexec/qemu-kvm</emulator>
<disk type='file' device='disk'>
<driver name='qemu' type='raw'/>
<source file='/opt/centos2.1.raw.qcow2'/> #修改虚机的磁盘文件
<target dev='vda' bus='virtio'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>

[root@KVM03-10 ~]# virsh list --all
Id Name State
----------------------------------------------------
1 test-kvm running

好像不太对,虽然通过virsh list --all,可以看到正处于运行状态,但是通过vnc连接发现,启动异常。检查发现,还需要修改磁盘类型的参数!

  <devices>
<emulator>/usr/libexec/qemu-kvm</emulator>
<disk type='file' device='disk'>
<driver name='qemu' type='qcow2'/>
<source file='/opt/centos2.1.raw.qcow2'/>
<target dev='vda' bus='virtio'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>
</disk>

修改以后重新启用正常。

4、调整虚拟磁盘容量大小   resize [-q] filename [+ | -]size

创建完磁盘以后 ,可能需要对磁盘进行扩容操作,这个时候可以用过 qemu-img resize命令实现

测试发现,qcow2和RAW格式的磁盘文件均支持扩容,但是仅raw格式文件支持缩容,qcow2不支持,这与两种磁盘文件类型有关,raw为连续的。另外,需要注意,在生产环境中,对磁盘进行缩容是危险操作,不建议缩容。

[root@KVM03-10 opt]# ll -h
total 5.3G
-rw-------. 1 qemu qemu 10G Oct 11 11:27 centos2.1.raw
-rw-r--r--. 1 qemu qemu 4.1G Apr 27 20:56 CentOS-7.3-x86_64-DVD-1611.iso
-rw-r--r--. 1 root root 1.0G Oct 11 11:30 resize.raw
-rw-r--r--. 1 root root 193K Oct 11 11:24 test.qcow2
-rw-r--r--. 1 root root 5.0G Oct 11 10:50 test.raw
[root@KVM03-10 opt]# qemu-img resize resize.raw +1G
Image resized.

[root@KVM03-10 opt]# ll -h
total 5.3G
-rw-------. 1 qemu qemu 10G Oct 11 11:27 centos2.1.raw
-rw-r--r--. 1 qemu qemu 4.1G Apr 27 20:56 CentOS-7.3-x86_64-DVD-1611.iso
-rw-r--r--. 1 root root 2.0G Oct 11 11:31 resize.raw
-rw-r--r--. 1 root root 193K Oct 11 11:24 test.qcow2
-rw-r--r--. 1 root root 5.0G Oct 11 10:50 test.raw
[root@KVM03-10 opt]#

抽空学学KVM(六)qemu-img命令使用的更多相关文章

  1. 抽空学学KVM(七):虚拟机快照和克隆

    前几天学写了KVM中qume-info命令的使用,今天学学在虚拟化里面用处广泛的快照和克隆功能,snapshot和virt-clone.对于snapshot命令的使用其实很简单.进入virsh界面以后 ...

  2. KVM和QEMU简介

    KVM/QEMU简介 KVM虚拟机是基于linux内核虚拟化,自linux2.6.20之后就集成在linux的各个主要发行版本中.它使用linux自身的调度器进行管理,所以相对于xen,其核心源码很少 ...

  3. Linux虚拟化技术KVM、QEMU与libvirt的关系(转)

    说明:个人理解,KVM是内核虚拟化技术,而内核是不能使用在界面上使用的,那么此时QEMU提供了用户级别的使用界面,相互辅助.当然,单独使用QEMU也是可以实现一整套虚拟机,不过QEMU+KVM基本是标 ...

  4. 虚拟化技术xen,kvm,qemu区别

    虚拟化类型 全虚拟化(Full Virtualization) 全虚拟化也成为原始虚拟化技术,该模型使用虚拟机协调guest操作系统和原始硬件,VMM在guest操作系统和裸硬件之间用于工作协调,一些 ...

  5. kvm之 virt-install工具命令详解

    一.virt-install是一个命令行工具,它能够为KVM.Xen或其它支持libvrit API的hypervisor创建虚拟机并完成GuestOS安装:此外,它能够基于串行控制台.VNC或SDL ...

  6. 没事学学KVM(一)

    学习KVM肯定要找来一台虚机来学习呀,通过VMware workstation创建虚机,现在的电脑CPU,包括INTER,AMD都支持,公司发的电脑CPU为inter,通过开启inter VT-X可在 ...

  7. kvm与qemu

    载请注明出处: http://www.openext.org/2014/04/kvmqemu/ http://blog.csdn.net/muge0913/article/details/245577 ...

  8. kvm和qemu交互处理io流程

    1.IO虚拟化的分类 (1)全虚拟化:宿主机截获客户机对I/O设备的访问请求,然后通过软件模拟真实的硬件.这种方式对客户机而言非常透明,无需考虑底层硬件的情况,不需要修改操作系统. QEMU模拟I/O ...

  9. kvm和qemu的关系

    KVM (Kernel Virtual Machine) is a Linux kernel module that allows a user space program to utilize th ...

随机推荐

  1. 常见消息中间件之RocketMQ

    前言 RocketMQ是一款分布式.队列模型的消息中间件,由阿里巴巴自主研发的一款适用于高并发.高可靠性.海量数据场景的消息中间件.早期开源2.X版本名为MetaQ:2015年迭代3.X版本,更名为R ...

  2. PostgreSQL数组类型应用

    在使用 awk 脚本:数组是一大利器:在很多场景是用数组能处理. 在 python 中,数据类型list:相当于array类型. 在 Oracle 中,对 array 不够友好,感觉像是鸡肋.但是在 ...

  3. Android Handler 分析学习

    一.Handler简介 Handler 是 Android 中用于线程间交互的机制.与其相关的概念有 Thread.Looper.Runnable.Message.MessageQueue 等. Go ...

  4. Centos-内核核心组成

    linux内核,相当于linux大脑,高可靠和高稳定都是针对内核来说 完整linux核心组成部分 1. 内存管理 合理有效的管理整个系统的物理内存,同时快速响应内核各子系统对内存分配的请求 2. 进程 ...

  5. kubernetes下jenkins实战maven项目编译构建

    关于kubernetes环境的jenkins集群 在kubernetes环境部署的jenkins集群,执行任务时会新建pod,任务完成后pod被销毁,架构如下所示: 在kubernetes搭建jenk ...

  6. spring-boot-route(六)整合JApiDocs生成接口文档

    上一篇文章中介绍了使用Swagger生成接口文档,非常方便,功能也十分强大.如果非要说Swaager有什么缺点,想必就是注解写起来比较麻烦.如果我说有一款不用写注解,就可以生成文档的工具,你心动了吗? ...

  7. tomcat:tomcat安装(在一台电脑上安装两个tomcat)

    1.安装前的说明 (1)在安装第二个tomcat之前,我们要知道安装一台tomcat的时候需要在电脑上添加两个系统变量 然后在path中配置: (2)这个时候我们就要思考了,当安装第二台服务器的时候首 ...

  8. 萌新学python

    python python安装 进入官网http://www.python.org/download/ 下载 我下的是3.6.6大家可以根据需要下载(3.x和2.x不兼容请小心) 之后安装就可以了 p ...

  9. 玩转控件:GDI+动态绘制流程图

       前言 今天,要跟大家一起分享是"GDI+动态生成流程图"的功能.别看名字高大上(也就那样儿--!),其实就是动态生成控件,然后GDI+绘制直线连接控件罢了.实际项目效果图如下 ...

  10. java性能分析之火焰图

    原由 最近因为kafka.zookeeper.ES和相关的Java应用的内存问题搞的头大,做运维将近4年,对Java调优.性能方面的知识了解的少之又少,是时候下定决心来对他多一个学习了.不能一口吃成一 ...