Based on currently available code (nova: a77c0c50166aac04f0707af25946557fbd43ad44 2012-11-02/python-glanceclient: 16aafa728e4b8309b16bcc120b10bc20372883f4 2012-11-07/glance: 9dae32d60fc285d03fdb5586e3368d229485fdb4)
This is a deep dive into what happens (and where in the code) during image creation with a Nova/Glance configuration that is backed by XenServer/XenAPI. Hopefully the methods used in Glance/Nova’s code will not change over time, and this guide will remain good starting point.
Disclaimer: I am _not_ a developer, and these are really just best guesses. Corrections are welcome.
- nova-api receives an imaging request. The request is validated, checking for a name and making sure the request is within quotas. Instance data is retrieved, as well as block device mappings. If the instance is volume backed, a separate compute API call is made to snapshot (self.compute_api.snapshot_volume_backed). For this deep dive, we’ll assume there is no block device mapping. self.compute_api.snapshot is called. The newly created image UUID is returned.
- nova/api/openstack/compute/servers.py
- def _action_create_image
- The compute API gets the request and calls _create_image. The instance’s task state is set to IMAGE_SNAPSHOT. Notifications are created of the state change. Several properties are collected about the image, including the minimum RAM, customer, and base image ref.The non inheritable instance_system meta data is also collected. (2a, 2b, 2c) self.image_service.create and (3) self.compute_rpcapi.snapshot_instance are called.
- nova/compute/api.py
- def snapshot
- def _create_image
2a) The collected metadata from 2 is put into a glance-friendly format, and sent to glance. The glance client’s create is called.
- nova/image/glance.py
- def create
2b) Glance (client) sends a POST the glance server to /v1/images with the gathered image metadata from (3).
- glanceclient/v1/images.py
- def create
2c) Glance (server) receives the POST. Per the code comments:
Upon a successful save of the image data and metadata, a response containing metadata about the image is returned, including its opaque identifier.
- glance/api/v1
- def create
- def _handle_source
- Compute RPC API casts a message to the queue for the instance’s compute node.
- nova/compute/rpcapi.py
- def snapshot_instance
- The instance’s power state is read and updated. (4a) The XenAPI driver’s snapshot() is called. Notification is created for the snapshot’s start and end.
- nova/compute/manager.py
- def snapshot_instance
4a) The vmops snapshot is called (4a1).
- nova/virt/xenapi/driver.py
- def snapshot
4a1) The snapshot is created in XenServer via (4a1i) vm_utils, and (4a1ii) uploaded to glance. The code’s comments say this:
Steps involved in a XenServer snapshot:
1. XAPI-Snapshot: Snapshotting the instance using XenAPI. This creates: Snapshot (Template) VM, Snapshot VBD, Snapshot VDI, Snapshot VHD 2. Wait-for-coalesce: The Snapshot VDI and Instance VDI both point to a ‘base-copy’ VDI. The base_copy is immutable and may be chained with other base_copies. If chained, the base_copies coalesce together, so, we must wait for this coalescing to occur to get a stable representation of the data on disk. 3. Push-to-glance: Once coalesced, we call a plugin on the XenServer that will bundle the VHDs together and then push the bundle into Glance.
- nova/virt/xenapi/vmops.py
- def snapshot
4a1i) The instance’s root disk is recorded and its VHD parent is also recorded. The SR is recorded. The instance’s root VDI is snapshotted. Operations are blocked until a coalesce completes in _wait_for_vhd_coalesce (4a1i-1).
- nova/virt/xenapi/vm_utils.py
- def snapshot_attached_here
4a1i-1) The end result of this process is outlined in the code comments:
Before coalesce:
* original_parent_vhd * parent_vhd snapshot
After coalesce:
* parent_vhd snapshot
In (4a1i) the original vdi uuid was recorded. The SR is scanned. In a nutshell, the code is ensuring that the desired layout above is met before allowing the snapshot to continue. The code polls CONF.xenapi_vhd_coalesce_max_attempts times and sleeps CONF.xenapi_vhd_coalesce_poll_interval: the SR is scanned. The original_parent_uuid is compared to the parent_uuid… if they don’t match we wait a while and check again for the coalescing to complete.
- nova/virt/xenapi/vm_utils.py
- def _wait_for_vhd_coalesce
4a1ii) The glance API servers are retrieved from configuration. The glance upload_vhd XenAPI plugin is called.
- nova/virt/xenapi/vm_utils.py
- def upload_image
4a2) A staging area is created, prepared, and _upload_tarball is called.
- plugins/xenserver/xenapi/etc/xapi.d/plugins/glance
- def upload_vhd
4a3) The staging area is prepared. This basically symlinks the snapshot VHDs to a temporary folder in the SR.
- plugins/xenserver/xenapi/etc/xapi.d/plugins/utils.py
- def prepare_staging_area
4a4) The comments say it best:
Create a tarball of the image and then stream that into Glance using chunked-transfer-encoded HTTP.
A URL is constructed and a connection is opened to it. The image meta properties (like status) are collected and added as HTTP headers. The tarball is created, and streamed to glance in CHUNK_SIZE increments. The HTTP stream is terminated, the connection checks for an OK from glance and reports accordingly.
- plugins/xenserver/xenapi/etc/xapi.d/plugins/glance
- def _upload_tarball
(Glance Server)
- I’ve removed some of the obvious URL routing functions in glance to get down to the meat of this process. Basically, the PUT request goes to glance API. The API interacts with the registry again, but this time there is data to be uploaded. The image’s metadata is validated for activation, and then _upload_and_activate is called. _upload_and_activate is basically a way to call _upload and ensure that if it works, activate the image. _upload checks to see if we’re copying, but we’re not. It also checks to see if the HTTP request is application/octet-stream. Then, an object store like swift is inferred from the request or used from the glance configuration (self.get_store_or_400). Finally, the image is added to the object store and its checksum is verified and the glance registry is updated. Notifications are also sent for image.upload.
- glance/api/v1/images.py
- def update
- def _handle_source
- def _upload_and_activate
- def _upload