1. 前言

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

2. 通过glance操作

  1. 获取镜像,查看镜像信息
[root@controller upload]         # qemu-img info CentOS-7-x86_64-GenericCloud.qcow2c         


         image: 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 image-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 image-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. 查看image的详细信息


[root@controller upload]         # glance image-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. image的更新

[root@controller upload]         # glance image-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. image的删除


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



3. 通过nova操作

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

  1. 查看image
[root@controller upload]         # nova image-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 image-create --show --poll  c3e573aa-ea68-42fc-97be-609cf0907298 nova_image_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_image_ref             | 5ce414b0-660a-46e1-ad0a-b842b2afc0b7 |        


         | metadata image_location             | snapshot                             |        


         | metadata image_state                | available                            |        


         | metadata image_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_image_test_by_happy             |        


         | progress                            | 100                                  |        


         | server                              | c3e573aa-ea68-42fc-97be-609cf0907298 |        


         | status                              | ACTIVE                               |        


         | updated                             | 2016-01-21T02:14:20Z                 |        


         +-------------------------------------+--------------------------------------+



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


[root@controller upload]         # nova image-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_image_ref             | 5ce414b0-660a-46e1-ad0a-b842b2afc0b7 |        


         | metadata image_location             | snapshot                             |        


         | metadata image_state                | available                            |        


         | metadata image_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_image_test_by_happy             |        


         | progress                            | 100                                  |        


         | server                              | c3e573aa-ea68-42fc-97be-609cf0907298 |        


         | status                              | ACTIVE                               |        


         | updated                             | 2016-01-21T02:14:20Z                 |        


         +-------------------------------------+--------------------------------------+


4. 删除image


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



4. 总结

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

5. 附录

  1. glance image-create的命令帮助信息
[root@controller upload]         # glance help image-create        


         usage: glance image-create [--id <IMAGE_ID>] [--name <NAME>] [--store <STORE>]        


                  [--disk-format <DISK_FORMAT>]        


                  [--container-format <CONTAINER_FORMAT>]        


                  [--owner <TENANT_ID>] [--size <SIZE>]        


                  [--min-disk <DISK_GB>] [--min-ram <DISK_RAM>]        


                  [--location <IMAGE_URL>] [--file <FILE>]        


                  [--checksum <CHECKSUM>] [--copy-from <IMAGE_URL>]        


                  [--is-public {True,False}]        


                  [--is-protected {True,False}]        


                  [--property <key=value>] [--human-readable]        


                  [--progress]        


         Create a          new          image.        


         Optional arguments:        


                  --id <IMAGE_ID>       ID of image to reserve.        


                  --name <NAME>         Name of image.                                                     #名字        


                  --store <STORE>       Store to upload image to.        


                  --disk-format <DISK_FORMAT>        


                  Disk format of image. Acceptable formats: ami, ari,                #磁盘格式,支持aki, vhd, vmdk, raw, qcow2, vdi, and iso格式              


                  aki, vhd, vmdk, raw, qcow2, vdi, and iso        


                  --container-format <CONTAINER_FORMAT>                                                    #容器格式,支持ami,ari, aki, bare, and ovf格式        


                  Container format of image. Acceptable formats: ami,        


                  ari, aki, bare, and ovf.        


                  --owner <TENANT_ID>   Tenant who should own image.        


                  --size <SIZE>         Size of image data (         in          bytes). Only used          with          '--        


                  location'          and          '--copy_from'         .        


                  --min-disk <DISK_GB>  Minimum size of disk needed to boot image (         in                     #最小的磁盘,表示镜像最小需要的磁盘空间,和flavor有关,如果太小,则创建vm失败        


                  gigabytes).        


                  --min-ram <DISK_RAM>  Minimum amount of ram needed to boot image (         in        


                  megabytes).        


                  --location <IMAGE_URL>        


                  URL where the data          for          this          image already resides. For            #位置,支持三种location,file和copy-from,location是一个url路径        


                  example,          if          the image data is stored          in          swift, you        


                  could specify        


                  'swift://account:key@example.com/container/obj'         .                  #本地文件        


                  --file <FILE>         Local file that contains disk image to be uploaded        


                  during creation. Alternatively, images can be passed        


                  to the client via stdin.        


                  --checksum <CHECKSUM>        


                  Hash of image data used Glance can use          for        


                  verification. Provide a md5 checksum here.        


                  --copy-from <IMAGE_URL>                                                                 #远程拷贝,相比于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 image store.        


                  --is-public {True,False}                                                                #设置是否共有        


                  Make image accessible to the public.        


                  --is-protected {True,False}                                                             #是否保护标志位,房子误删除        


                  Prevent image from being deleted.        


                  --property <key=value>        


                  Arbitrary property to associate          with          image. May be        


                  used multiple times.        


                  --human-readable      Print image 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-certificate-file>] [--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-image-url OS_IMAGE_URL] [-U OS_IMAGE_URL]        


                  [--os-image-api-version OS_IMAGE_API_VERSION]        


                  [--os-service-type OS_SERVICE_TYPE]        


                  [--os-endpoint-type OS_ENDPOINT_TYPE] [-S OS_AUTH_STRATEGY]        


                  <subcommand> ...        


         Command-line interface to the OpenStack Images API.        


         Positional arguments:        


                  <subcommand>        


                  add                 DEPRECATED! Use image-create instead.                 #旧版的增删改查,即add,delete,update,list,show,details        


                  clear               DEPRECATED!        


                  delete                       DEPRECATED! Use image-         delete          instead.        


                  details             DEPRECATED! Use image-list instead.        


                  image-create        Create a          new          image.                                   #新版的增删改查,即image-create,image-delete,image-update,image-list,image-show        


                  image-         delete                 Delete specified image(s).        


                  image-download      Download a specific image.        


                  image-list          List images you can access.        


                  image-members       DEPRECATED! Use member-list instead.        


                  image-show          Describe a specific image.        


                  image-update        Update a specific image.        


                  index               DEPRECATED! Use image-list instead.        


                  member-add          DEPRECATED! Use member-create instead.               #如果image设置为private,则可以通过member的形式共享给其他的tenant使用        


                  member-create       Share a specific image          with          a tenant.                #包含member共享的相关操作,即member-create新建共享,member-delete删除共享,member-list查看共享        


                  member-         delete                Remove a shared image from a tenant.        


                  member-images       DEPRECATED! Use member-list instead.        


                  member-list         Describe sharing permissions by image or tenant.        


                  members-replace     DEPRECATED!        


                  show                DEPRECATED! Use image-show instead.        


                  update              DEPRECATED! Use image-update instead.        


                  help                Display help about          this          program or one of its        


                  subcommands.