OpenStack - convert an instance to an image

Posted on Tue 14 February 2017 by Pavlo Khmel

You cannot create image from a volume in-use. Volume status must be "available" to create image. So:
1. stop instance
2. create snapshoot
3. create new volume from snapshoot
4. convert volume and upload image to the glance
5. download image (optional)

Load your environment veraibles

[root@controller ~]# cat openrc 
export OS_PROJECT_DOMAIN_NAME=default
export OS_USER_DOMAIN_NAME=default
export OS_PROJECT_NAME=myproject
export OS_USERNAME=myuser
export OS_PASSWORD=MyPassword!
export OS_AUTH_URL=http://controller:35357/v3
export OS_IDENTITY_API_VERSION=3
export OS_IMAGE_API_VERSION=2

[root@controller ~]# source openrc

List instances

[root@controller ~]# nova list
+--------------------------------------+--------------+--------+------------+-------------+----------------------+
| ID                                   | Name         | Status | Task State | Power State | Networks             |
+--------------------------------------+--------------+--------+------------+-------------+----------------------+
| ba2e110b-b947-46db-a361-5824d42e5a46 | mysqrver     | ACTIVE | -          | Running     | provider=192.168.0.5 |
+--------------------------------------+--------------+--------+------------+-------------+----------------------+

Stop instance

[root@controller ~]# nova stop mysqrver
Request to stop server mysqrver has been accepted.

Wait for status SHUTOFF

[root@controller ~]# nova list
+--------------------------------------+--------------+---------+------------+-------------+----------------------+
| ID                                   | Name         | Status  | Task State | Power State | Networks             |
+--------------------------------------+--------------+---------+------------+-------------+----------------------+
| ba2e110b-b947-46db-a361-5824d42e5a46 | mysqrver     | SHUTOFF | -          | Shutdown    | provider=192.168.0.5 |
+--------------------------------------+--------------+---------+------------+-------------+----------------------+

Create snapshoot

[root@controller ~]# nova image-create --poll mysqrver mysqrver-snapshoot
Server snapshotting... 100% complete
Finished

List snapshoots

[root@controller ~]# cinder snapshot-list
+--------------------------------------+--------------------------------------+-----------+---------------------------------+------+
| ID                                   | Volume ID                            | Status    | Name                            | Size |
+--------------------------------------+--------------------------------------+-----------+---------------------------------+------+
| 8daa9b3d-3d08-4085-9e45-1dfa83b673dc | 41eee4cc-718f-4aa9-836b-3e26473ed937 | available | snapshot for mysqrver-snapshoot | 50   |
+--------------------------------------+--------------------------------------+-----------+---------------------------------+------+

Create new volume from snapshoot

[root@controller ~]# cinder create --snapshot-id 8daa9b3d-3d08-4085-9e45-1dfa83b673dc 50
+------------------------------+--------------------------------------+
| Property                     | Value                                |
+------------------------------+--------------------------------------+
| attachments                  | []                                   |
| availability_zone            | nova                                 |
| bootable                     | false                                |
| consistencygroup_id          | None                                 |
| created_at                   | 2017-02-14T19:03:32.000000           |
| description                  | None                                 |
| encrypted                    | False                                |
| id                           | d06881e8-847a-44b6-9ad9-9c4f85bc2e41 |
| metadata                     | {}                                   |
| multiattach                  | False                                |
| name                         | None                                 |
| os-vol-tenant-attr:tenant_id | 6dc8896fbda5482e9ce0a8556df5d4b9     |
| replication_status           | disabled                             |
| size                         | 50                                   |
| snapshot_id                  | 8daa9b3d-3d08-4085-9e45-1dfa83b673dc |
| source_volid                 | None                                 |
| status                       | creating                             |
| updated_at                   | 2017-02-14T19:03:32.000000           |
| user_id                      | 4c64d92d07604571bcee3bfac60a95f8     |
| volume_type                  | None                                 |
+------------------------------+--------------------------------------+

Wait for status "available"

[root@controller ~]# cinder list 
+--------------------------------------+----------+------+------+-------------+----------+--------------------------------------+
| ID                                   | Status   | Name | Size | Volume Type | Bootable | Attached to                          |
+--------------------------------------+----------+------+------+-------------+----------+--------------------------------------+
| d06881e8-847a-44b6-9ad9-9c4f85bc2e41 | creating | -    | 50   | -           | false    |                                      |
+--------------------------------------+----------+------+------+-------------+----------+--------------------------------------+

[root@controller ~]# cinder list 
+--------------------------------------+-----------+------+------+-------------+----------+--------------------------------------+
| ID                                   | Status    | Name | Size | Volume Type | Bootable | Attached to                          |
+--------------------------------------+-----------+------+------+-------------+----------+--------------------------------------+
| d06881e8-847a-44b6-9ad9-9c4f85bc2e41 | available | -    | 50   | -           | true     |                                      |
+--------------------------------------+-----------+------+------+-------------+----------+--------------------------------------+

Convert volume and upload image to the glance

[root@controller ~]# cinder upload-to-image --disk-format qcow2 d06881e8-847a-44b6-9ad9-9c4f85bc2e41 node-template
+---------------------+--------------------------------------+
| Property            | Value                                |
+---------------------+--------------------------------------+
| container_format    | bare                                 |
| disk_format         | qcow2                                |
| display_description | None                                 |
| id                  | d06881e8-847a-44b6-9ad9-9c4f85bc2e41 |
| image_id            | 0b833779-e6e6-455c-9f5e-628894fb2938 |
| image_name          | node-template                        |
| is_public           | False                                |
| protected           | False                                |
| size                | 50                                   |
| status              | uploading                            |
| updated_at          | 2017-02-14T19:17:38.000000           |
| volume_type         | None                                 |
+---------------------+--------------------------------------+

Wait for status "available"

[root@controller ~]# cinder list
+--------------------------------------+-----------+------+------+-------------+----------+--------------------------------------+
| ID                                   | Status    | Name | Size | Volume Type | Bootable | Attached to                          |
+--------------------------------------+-----------+------+------+-------------+----------+--------------------------------------+
| d06881e8-847a-44b6-9ad9-9c4f85bc2e41 | uploading | -    | 50   | -           | true     |                                      |
+--------------------------------------+-----------+------+------+-------------+----------+--------------------------------------+

[root@controller ~]# cinder list
+--------------------------------------+-----------+------+------+-------------+----------+--------------------------------------+
| ID                                   | Status    | Name | Size | Volume Type | Bootable | Attached to                          |
+--------------------------------------+-----------+------+------+-------------+----------+--------------------------------------+
| d06881e8-847a-44b6-9ad9-9c4f85bc2e41 | available | -    | 50   | -           | true     |                                      |
+--------------------------------------+-----------+------+------+-------------+----------+--------------------------------------+

Download image to a file (optional)

[root@controller ~]# glance image-list
+--------------------------------------+--------------------+
| ID                                   | Name               |
+--------------------------------------+--------------------+
| 96a0b871-b9e7-46ee-8af5-3d2a458e1e7c | mysqrver-snapshoot |
| 0b833779-e6e6-455c-9f5e-628894fb2938 | node-template      |
+--------------------------------------+--------------------+

[root@controller ~]# glance image-download --file node-template.qcow2 0b833779-e6e6-455c-9f5e-628894fb2938

[root@controller ~]# ls node-template.qcow2 
node-template.qcow2

Upload image file to the glance again (optional)

[root@controller ~]#  glance image-create --name "my-new-image" --visibility private --disk-format qcow2 --container-format bare --file node-template.qcow2
+------------------+--------------------------------------+
| Property         | Value                                |
+------------------+--------------------------------------+
| checksum         | 80f0bf05d15fc0c443333df25e900884     |
| container_format | bare                                 |
| created_at       | 2017-02-14T19:44:15Z                 |
| disk_format      | qcow2                                |
| id               | ff53d25f-940a-4b42-b59e-3470927f0d7c |
| min_disk         | 0                                    |
| min_ram          | 0                                    |
| name             | my-new-image                         |
| owner            | 6dc8896fbda5482e9ce0a8556df5d4b9     |
| protected        | False                                |
| size             | 6142230528                           |
| status           | active                               |
| tags             | []                                   |
| updated_at       | 2017-02-14T19:45:24Z                 |
| virtual_size     | None                                 |
| visibility       | private                              |
+------------------+--------------------------------------+

[root@controller ~]# glance image-list
+--------------------------------------+--------------------+
| ID                                   | Name               |
+--------------------------------------+--------------------+
| ff53d25f-940a-4b42-b59e-3470927f0d7c | my-new-image       |
| 96a0b871-b9e7-46ee-8af5-3d2a458e1e7c | mysqrver-snapshoot |
| 0b833779-e6e6-455c-9f5e-628894fb2938 | node-template      |
+--------------------------------------+--------------------+