OpenStack 虚拟机的磁盘文件类型与存储方式

目录

虚拟机的磁盘文件类型

Nova 虚拟机的虚拟磁盘主要包含有 Root Disk、Ephemeral Disk、Swap Disk、Block Storage 等几种类型。html

Root Disk – virtual root disk size in glgabytes. This is an ephemeral disk the base image is copied into. You don’t use it when you boot from a persistent volume. The “0” size is a special case that uses the native base image size as the size of the ephemeral root volume.web

Root Disk(根分区)系统盘,提供 Boot Loader。shell

Swap – Optional swap space allocation for the instancejson

Swap Disk(交换分区)交换盘。用于虚拟机内存耗尽时,协助承载内存压力。后端

Ephemeral – Specifies the size of a secondary ephemeral data disk. this is an empty unformatted disk and exists only for the life of the instance.安全

Ephemeral Disk 临时盘,非持久性存储空间,会随着虚拟机的生命周期被建立和回收。app

Block Storage – Volume from Cinderdom

Block Storage(块存储)数据盘,由 Cinder 提供的持久性存储空间。ide

OpenStack 提供了 Nova Flavor 和 nova boot 指令两种操做入口来指定虚拟机的磁盘类型:svg

[stack@undercloud (overcloudrc) ~]$ openstack flavor create
usage: openstack flavor create [-h]
                               [-f {html,json,json,shell,table,value,yaml,yaml}]
                               [-c COLUMN] [--max-width <integer>]
                               [--noindent] [--prefix PREFIX] [--id <id>]
                               [--ram <size-mb>] [--disk <size-gb>]
                               [--ephemeral <size-gb>] [--swap <size-gb>]
                               [--vcpus <vcpus>] [--rxtx-factor <factor>]
                               [--public | --private] [--property <key=value>]
                               [--project <project>]
                               [--project-domain <project-domain>]
                               <flavor-name>
  • [--disk <size-gb>]:系统盘
  • [--ephemeral <size-gb>]:临时盘
  • [--swap <size-gb>]:交换盘
[stack@undercloud (overcloudrc) ~]$ nova boot
usage: nova boot [--flavor <flavor>] [--image <image>]
                 [--image-with <key=value>] [--boot-volume <volume_id>]
                 [--snapshot <snapshot_id>] [--min-count <number>]
                 [--max-count <number>] [--meta <key=value>]
                 [--file <dst-path=src-path>] [--key-name <key-name>]
                 [--user-data <user-data>]
                 [--availability-zone <availability-zone>]
                 [--security-groups <security-groups>]
                 [--block-device-mapping <dev-name=mapping>]
                 [--block-device key1=value1[,key2=value2...]]
                 [--swap <swap_size>]
                 [--ephemeral size=<size>[,format=<format>]]
                 [--hint <key=value>]
                 [--nic <auto,none,net-id=net-uuid,net-name=network-name,port-id=port-uuid,v4-fixed-ip=ip-addr,v6-fixed-ip=ip-addr,tag=tag>]
                 [--config-drive <value>] [--poll] [--admin-pass <value>]
                 [--access-ip-v4 <value>] [--access-ip-v6 <value>]
                 [--description <description>]
                 <name>
  • [--image <image>]:Boot from image 的镜像文件
  • [--boot-volume <volume_id>]:Boot from volume 的块设备
  • [--snapshot <snapshot_id>]:Boot from snapshot(Snapshot 能够来自 Image,也可能来自 Volume)
  • [--ephemeral size=<size>[,format=<format>]]:临时盘
  • [--swap <swap_size>]:交换盘
  • [--block-device key1=value1[,key2=value2...]]:块设备隐射(可指定多种来源类型),key-value 选项以下
    • source=image|snapshot|volume|blank
    • id={uuid} (a volume|image|snapshot UUID if using source=volume|snapshot|image)
    • *dest=volume|local
    • *format=swap|ext4|…|none (to format the image/volume/ephemeral file; defaults to ‘none’ if omitted)
    • *bus=ide|usb|virtio|scsi (hypervisor driver chooses a suitable default if omitted)
    • *device=the desired device name (e.g. /dev/vda, /dev/xda, …)
    • *type=disk|cdrom|floppy|mmc (defaults to ‘disk’ if omitted)
    • *bootindex=N (where N is any number >= 0, controls the order in which disks are looked at for booting)
    • (size=NN (where NN is number of GB to create type=emphemeral image, or the size to re-size to for type=glance|cinder)
    • *shutdown=preserve|remove
    • NOTE:* 表示可选项
      在这里插入图片描述
  • [--block-device-mapping <dev-name=mapping>]:块设备隐射(只指定 Volume 类型),mapping 的格式为 <dev-name>=<id>:<type>:<size(GB)>:<delete-on-terminate>
    • id:UUID of Volume|Snapshot
    • *dev-name:Volume 在虚拟机中的设备名,e.g. /dev/sda
    • *type:snapshot|blank,snapshot 则表示 Volume 是从 Snapshot 上
    • *size (GB): Volume Size
    • *delete-on-terminate(Boolean):标识是否随虚拟机一块儿被删除

NOTE:在实际使用中,建议经过 Flavor 的方式来使用 Ephemeral 和 Swap 磁盘,由于 nova boot 指定的 ephemeral size 和 swap size 不能大于 Flavor 设定的值,很容易形成冲突。

经过对以上虚拟机磁盘类型的了解,在生产环境中使用 Boot from Volume 的虚拟机启动方式会是一种不错的选择。以 Volume 做为 Root Disk 的话,Volume 不会随着虚拟机删除而销毁。Boot from Volume 有着更高的数据安全性,是大多数 OpenStack 企业级产品提供的默认选项。

虚拟机磁盘文件的存放方式

Openstack 虚拟机磁盘文件的存放方式大体上能够归纳为 Local(本地)和 backend(后端存储)两种形式。

  • Local,顾名思义,虚拟机的磁盘文件存放在计算节点本地,由 nova.conf 配置项 instances_path 指定目录路径。e.g.
instances_path = /var/lib/nova/instances
  • backend,即 Nova 启用了后端存储,或者说 Nova 底层的 Hypervisor 启用了后端存储更为准确。e.g.
[libvirt]
# VM Images format.
# Allowed values: raw, flat, qcow2, lvm, rbd, ploop, default
images_type = rbd

# The RADOS pool in which rbd volumes are stored (string value)
images_rbd_pool = rbd

# Path to the ceph configuration file to use (string value)
images_rbd_ceph_conf = /etc/ceph/ceph.conf

这里以 Ceph RBD 为例,当 Libvirt 对接了 Ceph RBD Backend,那么虚拟机的磁盘文件再也不存放到计算节点本地,Ceph 经过精简配置的快照复制(snapshotted-cloned)卷(又称快照连接)来帮助 Libvirt 快速启动虚拟机,虚拟机的数据变动经过 rbd 协议写入 Ceph 块存储。此时的 instances_path 目录做为 console.log、disk.info、Ephemeral file、Swap file 的存放路径。可见 Ephemeral file、Swap file 老是 Local 的,会随着虚拟机的删除而彻底销毁,除非特殊需求,不然不建议使用。

[root@localhost ~]# ll /opt/stack/data/nova/instances/9b17609e-13b8-448d-a225-e69b797e83c4/
total 416
-rw-------. 1 root  root      18067 Mar 21 12:08 console.log
-rw-r--r--. 1 qemu  qemu     196624 Mar 21 12:07 disk.eph0
-rw-r--r--. 1 stack libvirtd    181 Mar 21 12:07 disk.info
-rw-r--r--. 1 qemu  qemu     196616 Mar 21 12:07 disk.swap

固然还有一种场景,Nova 使用 NFS 后端存储来存放虚拟机磁盘文件。与 Ceph RBD 不一样,NFS 以文件存储的方式 mount 到计算节点本地的(注:CephFS 也支持文件存储,这里以 NFS 为例)。e.g.

mount {nfs_server_ipaddr}:/instances /var/lib/nova/instances
chown -R nova:nova /var/lib/nova/instances
chmod 755 /var/lib/nova/instances

此时,虽然虚拟机磁盘文件存放到了后端存储,但对于 Libvirt 而言这些文件依旧是存放在本地文件系统上的,不须要经过特殊的协议访问,只是单纯的本地文件访问,因此我将这种存放形式概括到 Local 行列。

QCOW2 镜像格式与 qemu-img 指令

QCOW2 是 QEMU-KVM 经常使用的 Image 格式,也是常见的虚拟机 disk_format。当采用 Local 磁盘文件存放方式时,Nova 经过 qemu-img 指令对此格式的磁盘文件进行管理。关于 qemu-img 指令的详细使用方式在 《QCOW2/RAW/qemu-img 概念浅析》中已经有过记录,这里再也不赘述。

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.

QCOW2 的特性

  • COW
  • 支持多重快照,又称链式快照
  • 占用更小的存储空间
  • AES 加密
  • zlib 压缩

其中咱们关心 QCOW2 的 COW 特性,COW(Copy-On-Write,写时复制,或称即写即拷)是一种快照技术,更多的 COW 实现原理能够浏览《再谈 COW、ROW 快照技术》。简而言之,当使用 QCOW2 Image 建立虚拟机而且虚拟机的磁盘文件存放方式是 Local 的话,那么虚拟机 Root file 本质只是一个 QCOW2 Image 的快照连接,而非从 Glance download 到本地的 QCOW2 Image。与 Ceph RBD 相似的,这个快照连接一样用于记录 GuestOS 对磁盘的读写变动,Root file 会随着虚拟机的使用而膨胀,直到 Root file size 达到 Flavor Disk_GB 的上限为止。

EXAMPLE

[root@overcloud-ovscompute-0 instances]# qemu-img info 84603dbf-cf56-4c7d-b8a9-3ca83970682d/disk
image: 84603dbf-cf56-4c7d-b8a9-3ca83970682d/disk
file format: qcow2
virtual size: 10G (10737418240 bytes)
disk size: 30M
cluster_size: 65536
backing file: /var/lib/nova/instances/_base/309eee24778f158af7ae57d8843e698d48f43d4a
Format specific information:
    compat: 1.1
    lazy refcounts: false
    refcount bits: 16
    corrupt: false
    
[root@overcloud-ovscompute-0 instances]# qemu-img info 744a1c9f-705d-4294-89cf-dadbb1b6ca8c/disk
image: 744a1c9f-705d-4294-89cf-dadbb1b6ca8c/disk
file format: qcow2
virtual size: 10G (10737418240 bytes)
disk size: 30M
cluster_size: 65536
backing file: /var/lib/nova/instances/_base/309eee24778f158af7ae57d8843e698d48f43d4a
Format specific information:
    compat: 1.1
    lazy refcounts: false
    refcount bits: 16
    corrupt: false
    
[root@overcloud-ovscompute-0 instances]# qemu-img info /var/lib/nova/instances/_base/309eee24778f158af7ae57d8843e698d48f43d4a
image: /var/lib/nova/instances/_base/309eee24778f158af7ae57d8843e698d48f43d4a
file format: raw
virtual size: 8.0G (8589934592 bytes)
disk size: 881M

上述输出,两个虚拟机的根磁盘文件有着共同的 backing file /var/lib/nova/instances/_base/309eee24778f158af7ae57d8843e698d48f43d4a,这个文件才是从 Glance 拉到本地的 QCOW2 Image。因此,我更愿意将虚拟机磁盘文件称为 disk file,将 QCOW2 Image 称为 base file,二者的结合才是虚拟机在当前时刻的完整数据。这种利用了 QCOW2 特性的虚拟机磁盘文件处理方式的好处在于占用了更小的磁盘空间,但在虚拟机迁移场景中要引发注意,若是只是迁移 disk file 的话,将没法获得虚拟机的完整数据。
在这里插入图片描述

Initially the image is downloaded from glance and cached in libvirt base. We’ll consider the options for handling a qcow2 image stored in glance, as that format can be downloaded quite efficiently from glance as it supports compression, and image sparseness can be maintained. This article will focus on the flow and transformations in “libvirt base”, which is used to cache, preprocess and optionally back, VM disk images.

NOTE:Ephemeral file 和 Swap file 也同样,不作过多讨论。

随带一提,若是 Nova、Cinder、Glance 都使用了 Ceph RBD 后端存储。那么 Image 是以 RAW(裸数据)格式存储的,依赖 Ceph 块存储的 COW 特性来达到与 QCOW2 相似,甚至更完备的效果。

参考文章

http://www.javashuo.com/article/p-ubrtxteq-mq.html

相关文章
相关标签/搜索