1. 前言

    在openstack中,glance负责p_w_picpath,即镜像相关的服务,镜像是一个已经打包好的文件,内置有操作系统和预先部署好的软件。基于p_w_picpath创建虚拟机,在openstack中是以backing file的形式创建的,即新建的虚拟机和镜像文件之间建立一个连接,相关的内容,后续以博客的形式呈现。本文以最简单的镜像上传为例,演示在openstack中制作完镜像之后,如何上传到openstack云环境中。


2. 通过glance操作

  1. 获取镜像,查看镜像信息

[root@controller upload]# qemu-img info CentOS-7-x86_64-GenericCloud.qcow2c 
p_w_picpath: CentOS-7-x86_64-GenericCloud.qcow2c
file format: qcow2                                #格式为qcow2
virtual size: 8.0G (8589934592 bytes)             #空间大小
disk size: 410M
cluster_size: 65536

2. 通过glance命令上传至openstack

[root@controller upload]# glance p_w_picpath-create --name centos-7 --disk-format qcow2 --container-format bare --min-disk 0 --min-ram 512 --is-public True --is-protected True --human-readable  --progress   --file CentOS-7-x86_64-GenericCloud.qcow2c 
[=================>            ] 59%
+------------------+--------------------------------------+
| Property         | Value                                |
+------------------+--------------------------------------+
| checksum         | 8e27a90b433e7c1a4db3fe8b8af6d1ad     |
| container_format | bare                                 |
| created_at       | 2016-01-21T01:58:13                  |
| deleted          | False                                |
| deleted_at       | None                                 |
| disk_format      | qcow2                                |
| id               | 4cccb602-0222-4c47-857a-5079ec571976 |
| is_public        | True                                 |
| min_disk         | 0                                    |
| min_ram          | 512                                  |
| name             | centos-7                             |
| owner            | 842ab3268a2c47e6a4b0d8774de805ae     |
| protected        | True                                 |
| size             | 409.2MB                              |
| status           | active                               |
| updated_at       | 2016-01-21T01:58:21                  |
| virtual_size     | None                                 |
+------------------+--------------------------------------+

3. 查看glance镜像列表

[root@controller upload]# glance p_w_picpath-list
+--------------------------------------+---------------+-------------+------------------+-------------+--------+
| ID                                   | Name          | Disk Format | Container Format | Size        | Status |
+--------------------------------------+---------------+-------------+------------------+-------------+--------+
| 6637990f-bcff-4ea7-a978-d3edeeb636d7 | centos-5.9-kg | qcow2       | bare             | 2187460608  | active |
| 4cccb602-0222-4c47-857a-5079ec571976 | centos-7      | qcow2       | bare             | 429056512   | active |  #上传成功
| b6c049c5-c4af-4f50-bba5-38b6850d7f41 | centos-7.0    | raw         | bare             | 10737418240 | active |
| 73379d54-10f8-42b9-94d3-16ca6640c4b0 | cfme          | qcow2       | bare             | 2052325376  | active |
| 37aaedc7-6fe6-4fc8-b110-408d166b8e51 | cirrors       | qcow2       | bare             | 13200896    | active |
| ff0f7d03-a553-4357-a819-c74e913d649f | win2k8        | qcow2       | bare             | 3391881216  | active |
+--------------------------------------+---------------+-------------+------------------+-------------+--------+

4. 查看p_w_picpath的详细信息

[root@controller upload]# glance p_w_picpath-show 4cccb602-0222-4c47-857a-5079ec571976
+------------------+--------------------------------------+
| Property         | Value                                |
+------------------+--------------------------------------+
| checksum         | 8e27a90b433e7c1a4db3fe8b8af6d1ad     |
| container_format | bare                                 |      container个数
| created_at       | 2016-01-21T01:58:13                  |
| deleted          | False                                |
| disk_format      | qcow2                                |      磁盘格式
| id               | 4cccb602-0222-4c47-857a-5079ec571976 |
| is_public        | True                                 |      是否共有
| min_disk         | 0                                    |      磁盘最小大小
| min_ram          | 512                                  |      内存最小大小
| name             | centos-7                             |      名字
| owner            | 842ab3268a2c47e6a4b0d8774de805ae     |
| protected        | True                                 |      是否保护
| size             | 429056512                            |      镜像大小
| status           | active                               |
| updated_at       | 2016-01-21T01:58:21                  |
+------------------+--------------------------------------+

5. p_w_picpath的更新

[root@controller upload]# glance p_w_picpath-update  --is-protected False 4cccb602-0222-4c47-857a-5079ec571976
+------------------+--------------------------------------+
| Property         | Value                                |
+------------------+--------------------------------------+
| checksum         | 8e27a90b433e7c1a4db3fe8b8af6d1ad     |
| container_format | bare                                 |
| created_at       | 2016-01-21T01:58:13                  |
| deleted          | False                                |
| deleted_at       | None                                 |
| disk_format      | qcow2                                |
| id               | 4cccb602-0222-4c47-857a-5079ec571976 |
| is_public        | True                                 |
| min_disk         | 0                                    |
| min_ram          | 512                                  |
| name             | centos-7                             |
| owner            | 842ab3268a2c47e6a4b0d8774de805ae     |
| protected        | False                                |        #设置不保护,有保护不可以删除
| size             | 429056512                            |
| status           | active                               |
| updated_at       | 2016-01-21T02:02:20                  |
| virtual_size     | None                                 |
+------------------+--------------------------------------+

6. p_w_picpath的删除

[root@controller upload]# glance p_w_picpath-delete 4cccb602-0222-4c47-857a-5079ec571976

3. 通过nova操作

    除了通过glance操作openstack的p_w_picpath服务之外,还可以通过nova命令操作,主要基于两方面的考虑:1. 最初glance集成在nova的代码中,到了E版才脱离出来,成为一个单独的项目,2. nova创建虚拟机的时候,需要通过API的方式调用glance,以获取instance的镜像,从而实现instance的创建。所以,也可以通过nova的方式操作glance,实际上都是调用glance的底层实现镜像的相关操作。与glance不同的是,nova提供了对镜像的快照功能。

  1. 查看p_w_picpath

[root@controller upload]# nova p_w_picpath-list
+--------------------------------------+---------------+--------+--------+
| ID                                   | Name          | Status | Server |
+--------------------------------------+---------------+--------+--------+
| 6637990f-bcff-4ea7-a978-d3edeeb636d7 | centos-5.9-kg | ACTIVE |        |
| b6c049c5-c4af-4f50-bba5-38b6850d7f41 | centos-7.0    | ACTIVE |        |
| 73379d54-10f8-42b9-94d3-16ca6640c4b0 | cfme          | ACTIVE |        |
| 37aaedc7-6fe6-4fc8-b110-408d166b8e51 | cirrors       | ACTIVE |        |
| ff0f7d03-a553-4357-a819-c74e913d649f | win2k8        | ACTIVE |        |
+--------------------------------------+---------------+--------+--------+

2. 将instance转换为快照

root@controller upload]# nova p_w_picpath-create --show --poll  c3e573aa-ea68-42fc-97be-609cf0907298 nova_p_w_picpath_test_by_happy
Server snapshotting... 100% complete
Finished
+-------------------------------------+--------------------------------------+
| Property                            | Value                                |
+-------------------------------------+--------------------------------------+
| OS-EXT-IMG-SIZE:size                | 965541888                            |     #快照记录元数据metadata的信息
| created                             | 2016-01-21T02:11:38Z                 |
| id                                  | 81c20616-fabd-419e-a286-37fd9b8cdb76 |
| metadata base_p_w_picpath_ref             | 5ce414b0-660a-46e1-ad0a-b842b2afc0b7 |
| metadata p_w_picpath_location             | snapshot                             |
| metadata p_w_picpath_state                | available                            |
| metadata p_w_picpath_type                 | snapshot                             |
| metadata instance_type_ephemeral_gb | 10                                   |
| metadata instance_type_flavorid     | 63a239ff-d557-4882-a9e4-1ebe24d98f3c |
| metadata instance_type_id           | 15                                   |
| metadata instance_type_memory_mb    | 2048                                 |
| metadata instance_type_name         | vmDev.F1                             |
| metadata instance_type_root_gb      | 10                                   |
| metadata instance_type_rxtx_factor  | 1.0                                  |
| metadata instance_type_swap         | 0                                    |
| metadata instance_type_vcpus        | 1                                    |
| metadata instance_uuid              | c3e573aa-ea68-42fc-97be-609cf0907298 |
| metadata network_allocated          | True                                 |
| metadata owner_id                   | 842ab3268a2c47e6a4b0d8774de805ae     |
| metadata user_id                    | bc5e46fc4204497185ae3ca6f8b7affb     |
| minDisk                             | 10                                   |
| minRam                              | 0                                    |
| name                                | nova_p_w_picpath_test_by_happy             |
| progress                            | 100                                  |
| server                              | c3e573aa-ea68-42fc-97be-609cf0907298 |
| status                              | ACTIVE                               |
| updated                             | 2016-01-21T02:14:20Z                 |
+-------------------------------------+--------------------------------------+

3. 查看镜像快照的详细信息

[root@controller upload]# nova p_w_picpath-show 81c20616-fabd-419e-a286-37fd9b8cdb76
+-------------------------------------+--------------------------------------+
| Property                            | Value                                |
+-------------------------------------+--------------------------------------+
| OS-EXT-IMG-SIZE:size                | 965541888                            |
| created                             | 2016-01-21T02:11:38Z                 |
| id                                  | 81c20616-fabd-419e-a286-37fd9b8cdb76 |
| metadata base_p_w_picpath_ref             | 5ce414b0-660a-46e1-ad0a-b842b2afc0b7 |
| metadata p_w_picpath_location             | snapshot                             |
| metadata p_w_picpath_state                | available                            |
| metadata p_w_picpath_type                 | snapshot                             |
| metadata instance_type_ephemeral_gb | 10                                   |
| metadata instance_type_flavorid     | 63a239ff-d557-4882-a9e4-1ebe24d98f3c |
| metadata instance_type_id           | 15                                   |
| metadata instance_type_memory_mb    | 2048                                 |
| metadata instance_type_name         | vmDev.F1                             |
| metadata instance_type_root_gb      | 10                                   |
| metadata instance_type_rxtx_factor  | 1.0                                  |
| metadata instance_type_swap         | 0                                    |
| metadata instance_type_vcpus        | 1                                    |
| metadata instance_uuid              | c3e573aa-ea68-42fc-97be-609cf0907298 |
| metadata network_allocated          | True                                 |
| metadata owner_id                   | 842ab3268a2c47e6a4b0d8774de805ae     |
| metadata user_id                    | bc5e46fc4204497185ae3ca6f8b7affb     |
| minDisk                             | 10                                   |
| minRam                              | 0                                    |
| name                                | nova_p_w_picpath_test_by_happy             |
| progress                            | 100                                  |
| server                              | c3e573aa-ea68-42fc-97be-609cf0907298 |
| status                              | ACTIVE                               |
| updated                             | 2016-01-21T02:14:20Z                 |
+-------------------------------------+--------------------------------------+

4. 删除p_w_picpath

[root@controller upload]# nova p_w_picpath-delete 81c20616-fabd-419e-a286-37fd9b8cdb76

4. 总结

    glance负责openstack中p_w_picpath的服务,p_w_picpath的制作可以通过两种方式:1. 新制作镜像,2. 从现有的instance做snapshot,前者的功能由glance实现,后者的功能由nova实现。制作完镜像之后,即可从镜像中启动虚拟机实例了。


5. 附录

  1. glance p_w_picpath-create的命令帮助信息

[root@controller upload]# glance help p_w_picpath-create
usage: glance p_w_picpath-create [--id ] [--name ] [--store ]
                           [--disk-format ]
                           [--container-format ]
                           [--owner ] [--size ]
                           [--min-disk ] [--min-ram ]
                           [--location ] [--file ]
                           [--checksum ] [--copy-from ]
                           [--is-public {True,False}]
                           [--is-protected {True,False}]
                           [--property ] [--human-readable]
                           [--progress]
Create a new p_w_picpath.
Optional arguments:
  --id        ID of p_w_picpath to reserve.
  --name          Name of p_w_picpath.                                            #名字
  --store        Store to upload p_w_picpath to.
  --disk-format 
                        Disk format of p_w_picpath. Acceptable formats: ami, ari,       #磁盘格式,支持aki, vhd, vmdk, raw, qcow2, vdi, and iso格式      
                        aki, vhd, vmdk, raw, qcow2, vdi, and iso
  --container-format                                            #容器格式,支持ami,ari, aki, bare, and ovf格式
                        Container format of p_w_picpath. Acceptable formats: ami,
                        ari, aki, bare, and ovf.
  --owner    Tenant who should own p_w_picpath.
  --size          Size of p_w_picpath data (in bytes). Only used with '--
                        location' and '--copy_from'.
  --min-disk   Minimum size of disk needed to boot p_w_picpath (in            #最小的磁盘,表示镜像最小需要的磁盘空间,和flavor有关,如果太小,则创建vm失败
                        gigabytes).
  --min-ram   Minimum amount of ram needed to boot p_w_picpath (in
                        megabytes).
  --location 
                        URL where the data for this p_w_picpath already resides. For   #位置,支持三种location,file和copy-from,location是一个url路径
                        example, if the p_w_picpath data is stored in swift, you
                        could specify
                        'swift://account:[email protected]/container/obj'.         #本地文件
  --file          Local file that contains disk p_w_picpath to be uploaded
                        during creation. Alternatively, p_w_picpaths can be passed
                        to the client via stdin.
  --checksum 
                        Hash of p_w_picpath data used Glance can use for
                        verification. Provide a md5 checksum here.
  --copy-from                                                         #远程拷贝,相比于location,copy-from会直接拷贝,而location则指定定义url路径
                        Similar to '--location' in usage, but this indicates
                        that the Glance server should immediately copy the
                        data and store it in its configured p_w_picpath store.
  --is-public {True,False}                                                       #设置是否共有
                        Make p_w_picpath accessible to the public.
  --is-protected {True,False}                                                    #是否保护标志位,房子误删除
                        Prevent p_w_picpath from being deleted.
  --property 
                        Arbitrary property to associate with p_w_picpath. May be
                        used multiple times.
  --human-readable      Print p_w_picpath size in a human-friendly format.             #镜像大小可读,即MB,GB格式显示
  --progress            Show upload progress bar.                                #显示上传的进度条


2. glance相关命令

[root@controller upload]# glance help 
usage: glance [--version] [-d] [-v] [--get-schema] [-k]
              [--cert-file CERT_FILE] [--key-file KEY_FILE]
              [--os-cacert ] [--ca-file OS_CACERT]
              [--timeout TIMEOUT] [--no-ssl-compression] [-f] [--dry-run]
              [--ssl] [-H ADDRESS] [-p PORT] [--os-username OS_USERNAME]
              [-I OS_USERNAME] [--os-password OS_PASSWORD] [-K OS_PASSWORD]
              [--os-tenant-id OS_TENANT_ID] [--os-tenant-name OS_TENANT_NAME]
              [-T OS_TENANT_NAME] [--os-auth-url OS_AUTH_URL] [-N OS_AUTH_URL]
              [--os-region-name OS_REGION_NAME] [-R OS_REGION_NAME]
              [--os-auth-token OS_AUTH_TOKEN] [-A OS_AUTH_TOKEN]
              [--os-p_w_picpath-url OS_IMAGE_URL] [-U OS_IMAGE_URL]
              [--os-p_w_picpath-api-version OS_IMAGE_API_VERSION]
              [--os-service-type OS_SERVICE_TYPE]
              [--os-endpoint-type OS_ENDPOINT_TYPE] [-S OS_AUTH_STRATEGY]
               ...
Command-line interface to the OpenStack Images API.
Positional arguments:
  
    add                 DEPRECATED! Use p_w_picpath-create instead.        #旧版的增删改查,即add,delete,update,list,show,details
    clear               DEPRECATED!
    delete              DEPRECATED! Use p_w_picpath-delete instead.
    details             DEPRECATED! Use p_w_picpath-list instead.
    p_w_picpath-create        Create a new p_w_picpath.                          #新版的增删改查,即p_w_picpath-create,p_w_picpath-delete,p_w_picpath-update,p_w_picpath-list,p_w_picpath-show
    p_w_picpath-delete        Delete specified p_w_picpath(s).
    p_w_picpath-download      Download a specific p_w_picpath.
    p_w_picpath-list          List p_w_picpaths you can access.
    p_w_picpath-members       DEPRECATED! Use member-list instead.
    p_w_picpath-show          Describe a specific p_w_picpath.
    p_w_picpath-update        Update a specific p_w_picpath.
    index               DEPRECATED! Use p_w_picpath-list instead.
    member-add          DEPRECATED! Use member-create instead.      #如果p_w_picpath设置为private,则可以通过member的形式共享给其他的tenant使用
    member-create       Share a specific p_w_picpath with a tenant.       #包含member共享的相关操作,即member-create新建共享,member-delete删除共享,member-list查看共享
    member-delete       Remove a shared p_w_picpath from a tenant.
    member-p_w_picpaths       DEPRECATED! Use member-list instead.
    member-list         Describe sharing permissions by p_w_picpath or tenant.
    members-replace     DEPRECATED!
    show                DEPRECATED! Use p_w_picpath-show instead.
    update              DEPRECATED! Use p_w_picpath-update instead.
    help                Display help about this program or one of its
                        subcommands.