QEMU命令练习

本文深入探讨QEMU命令,包括源码安装、关键概念如snapshot、redirect-on-write等,以及qemu-img、qemu-system-i386/x86_64等命令的使用。通过实例分析Openstack nova如何利用QEMU进行instance和image管理,特别是snapshot和backing file的作用。同时,文章指出在不同nova image_type下,如ceph,如何影响实例的backing_file和存储方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Openstack nova/glance涉及很多instance、image、snapshot的概念和操作,一直搞不清楚。其中很多跟qemu有关系,底层都是通过libvirt调用kvm/qemu接口实现的。

所以希望从qemu命令入手,对此有所了解。

官网:
http://wiki.qemu.org/Main_Page
http://wiki.qemu.org/Manual

源码安装

https://en.wikibooks.org/wiki/QEMU/Linux
http://my.oschina.net/kelvinxupt/blog/265108

[felix@centos65 ~]$ wget http://wiki.qemu-project.org/download/qemu-2.3.1.tar.bz2
[felix@centos65 ~]$ tar xjvf qemu-2.3.1.tar.bz2
[felix@centos65 ~]$ cd qemu-2.3.1
[felix@centos65 qemu-2.3.1]$ sudo yum install zlib-devel
[felix@centos65 qemu-2.3.1]$ sudo yum install glib2-devel -y
[felix@centos65 qemu-2.3.1]$ sudo yum install install autoconf automake libtool
[felix@centos65 qemu-2.3.1]$ ./configure --enable-kvm --enable-debug --enable-vnc  --target-list="x86_64-softmmu"
[felix@centos65 qemu-2.3.1]$ sudo make install
.....
install -d -m 0755 "/usr/local/bin"
install -c -m 0755 qemu-system-x86_64  "/usr/local/bin"
[felix@centos65 qemu-2.3.1]$ qemu-
qemu-ga             qemu-img            qemu-io             qemu-nbd            qemu-system-x86_64
[felix@centos65 qemu-2.3.1]$ which qemu-img
/usr/local/bin/qemu-img
[felix@centos65 qemu-2.3.1]$ which qemu-system-x86_64
/usr/local/bin/qemu-system-x86_64

几个概念

snapshot

参考1

Snapshots in QEMU are images that refer to an original image using Redirect-on-Write to avoid changing the original image

参考2
Copy-on-Write(写时拷贝)

  • A snapshot of a storage volume is created using the pre-designated space for the snapshot. (空间预留)
  • When the snapshot is first created, only the meta-data about where original data is stored is copied. No physical copy of the data is done at the time the snapshot is created.(刚创建时只记录原始数据的位置)
  • The snapshot copy then tracks the changing blocks on the original volume as writes to the original volume are performed.(记录原始数据的变化)
  • The original data that is being written to is copied into the designated storage pool that is set aside for the snapshot before original data is overwritten, hence the name “copy-on-write”.(当原始数据被覆盖之前,原始数据会被copy到snapshot预留的空间中)

因此:

  • This keeps the snapshot data consistent with the exact time the snapshot was taken.
  • Read requests to the snapshot volume of the unchanged data blocks are redirected to the “copied” blocks in the snapshot, while read requests to active data blocks that have been changed are directed to the original volume. (访问snapshot时:读取未改变的数据的请求会被重定向到原来的volume block中,读取改变了的数据的请求会被发送到snapshot所在的block中 - 原文有点不对啊)
  • Snapshot contains the meta-data that describes the data blocks that have changed since the snapshot was first created.(snapshot中的meta-data记录了数据的变化)
    enter image description here

优点:

  • 节省空间

缺点:

  • 写数据时,必须先copy原来的数据,影响性能
  • snapshot数据完备性依赖于原始数据

Redirect-on-write(写时重定向)
和copy-on-write相似,但没有两次写的动作:

  • New writes to the original volume are redirected to another location set aside for snapshot.
    (对原始数据执行写操作时,新的数据被写入到了snapshot所在的block中)

优点:

  • 没有两次写的动作

缺点:

  • 当删除snapshot时,其中的数据必须要reconciled back into the original volume
  • 做多次snapshot时,情况更复杂
  • snapshot数据完备性依赖于原始数据

(原文还有其他方式,这里不做记录)

思考:

  • nova boot instance用了qemu create with backing_file(?)即redirect-on-write方式,instance/_base/xxxxx(backing_file)就是original data,instance/yyyy/disk就相当于snapshot data。当nova存储后端为ceph时,没有backing_file和disk文件,那是怎么回事?

    Update:

    1. 当nova image_type不为ceph时(默认qcow2),instance基本都有backing_file,除了openstack自带的cirros镜像
    2. 当nova image_type=ceph时,instance/yyy/目录下没有disk,disk存储在ceph中,例如:
      
      # libvirt.xml
      
      <disk type="network" device="disk">
      <driver type="raw" cache="writeback"/>
      <source protocol="rbd" name="vms/eb511866-3b8f-40e0-9650-ea040b439a7f_disk">
       <host name="10.254.3.111" port="6789"/>
      </source>
      <auth username="cinder">
       <secret type="ceph" uuid="ea60f8ea-18d1-4dba-afa9-6528b1685100"/>
      </auth>
      <target bus="virtio" dev="vda"/>
      </disk>
      
      op@ubuntu-op:/home/openstack/workspace/data/nova/instances$ qemu-img info  rbd:vms/eb511866-3b8f-40e0-9650-ea040b439a7f_disk:id=cinder:conf=/etc/ceph/ceph.conf
      image: rbd:vms/eb511866-3b8f-40e0-9650-ea040b439a7f_disk:id=cinder:conf=/etc/ceph/ceph.conf
      file format: raw
      virtual size: 40G (42949672960 bytes)
      disk size: unavailable
      cluster_size: 4194304
  • 对于raw格式的image启动instance,也不是用的redirect-on-write方式吧(因为没有backing_file)?
    Update:用Openstack自带的cirros创建的虚机没有backing_file(因为它用kernel/ramdisk?)

    [admin@maqi-openstack instances]$ ls d8e20388-2ff0-4667-a9b3-29921bf415be/
    console.log  disk.config  disk.info  kernel  libvirt.xml  ramdisk

    但是自建的raw格式glance image,启动的虚机还是有backing_file的

  • nova image-create(Create a new image by taking a snapshot of a running server)是怎么回事?

    Update:这个就是nova snapshot功能,它和qemu-img snapshot不是一回事。

    lihao:(nova/virt/libvirt/driver.py _live_snapshot)

    1. 创建临时镜像,使用和instance相同的backing_file
      qemu-img create yyy.delta -b _base/xxxx -f qcow2
    2. blockRebase
    3. 去掉临时镜像的backing_file
      qemu-img convert -f qcow2 -O qcow2 yyy.delta
    4. 上传snapshot到glance
    5. 删除临时镜像
  • 如果image有backing_file,对这个image做snapshot,会怎样?(尝试nova snapshot)
    Update:见上。
base image/backing file

创建镜像的时候,可以使用-b指定backing_file,也叫base image。这样做的好处:

  • 创建的镜像文件可以很小
  • 用这个镜像启动虚拟机之后,对虚拟机所做的改动只写入镜像文件,不会(也不能)修改base image
  • base image可以重用
qcow2,raw

https://en.wikibooks.org/wiki/QEMU/Images
raw:

(default) the raw format is a plain binary image of the disc image, and is very portable. On filesystems that support sparse files, images in this format only use the space actually used by the data recorded in them.

qcow2:

QEMU Copy-On-Write format with a range of special features, including the ability to take multiple snapshots, smaller images on filesystems that don’t support sparse files, optional AES encryption, and optional zlib compression

https://people.gnome.org/~markmc/qcow-image-format.html
http://www.ibm.com/developerworks/cn/linux/1409_qiaoly_qemuimgages/

命令

qemu-ga

QEMU Guest Agent

以后再看。

qemu-io

http://manned.org/qemu-io/8ccaf7b2
qemu-io is a command line utility to exercise the QEMU I/O path.

以后再看。

qemu-nbd
[admin@maqi-centos7 ~]$ qemu-nbd -h
Usage: qemu-nbd [OPTIONS] FILE
QEMU Disk Network Block Device Server

NBD:Network Block Device
简单来说,qemu-nbd可以把一个qemu disk image通过nbd方式挂载到系统上:

[admin@maqi-centos7 ~]$ sudo qemu-nbd --connect=/dev/nbd0 bcec-centos-6.5-amd64-server-10G-v4-ovirt.compress.qcow2
Failed to open /dev/nbd0: No such file or directory[admin@maqi-centos7 ~]$ sudo touch /dev/nbd0
[admin@maqi-centos7 ~]$ sudo qemu-nbd --connect=/dev/nbd0 bcec-centos-6.5-amd64-server-10G-v4-ovirt.compress.qcow2
nbd.c:nbd_init():L571: Failed to set NBD socket

需要kernel支持nbd:http://www.aixchina.net/Question/123287?p=1

找另一个环境试验:

felix@ubuntu14-home:~|⇒  sudo qemu-nbd -c /dev/nbd0 bcec-centos-6.5-amd64-server-10G-v4-ovirt.compress.qcow2
felix@ubuntu14-home:~|⇒  ll /dev/nbd0
brw-rw---- 1 root disk 43, 0  921 03:05 /dev/nbd0
felix@ubuntu14-home:~|⇒  sudo fdisk /dev/nbd0

Command (m for help): p

Disk /dev/nbd0: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders, total 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00010f6d

     Device Boot      Start         End      Blocks   Id  System
/dev/nbd0p1   *        2048    20971519    10484736   83  Linux

Command (m for help): q

felix@ubuntu14-home:~|⇒  sudo mount /dev/nbd0p1 /mnt
felix@ubuntu14-home:~|⇒  ll /mnt/home
total 4.0K
drwx------ 4 500 500 4.0K  210  2015 admin
felix@ubuntu14-home:~|⇒  mount | grep nbd
/dev/nbd0p1 on /mnt type ext4 (rw)
felix@ubuntu14-home:~|⇒  sudo umount /mnt
felix&#
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值