How to create and attach volumes in Openstack Virtual Machine (VM)

Share this article

We know that openstack is open standard for cloud computing platform. It provides Infrastructure as a service which allows us to create the Virtual Machine. There are multiple projects which are used to provide different services in the openstack. Cinder service is used to provide block storage services. This article discusses how to create the cinder volume by using the command line.

Check cinder service

As I am a fan of kolla-ansible deployment, in kolla-ansible deployment you can just check the cinder service by listing the docker container and see the cinder containers are there or not. Use the following command to see that.

sudo docker ps -a|grep cinder

You can check the endpoint of the cinder service by using the endpoint list command in the OpenStack environment.

(wallaby) [foofunc@cont ~]$ openstack endpoint list |grep cinder
 ______________________________________________________________________________________________________________________________________________________________
| ID                               | Region   | Service Name    | Service Type   | Enabled | Interface | URL                                                   |
|----------------------------------|----------|-----------------|----------------|---------|-----------|-------------------------------------------------------|
| fd49729a3bec48d18766a98af2ddde88 | Regionone | cinderv3        | volumev3       | True    | internal  | http://192.168.0.1:8776/v3/%(tenant_id)s              |
| f67896e487e7493eb004f8ed0be58dbc | Regionone | cinderv3        | volumev3       | True    | public    | http://10.0.0.10:8776/v3/%(tenant_id)s                |
| e5d2a4f5d3d649bebe41bbe6ddaf2912 | Regionone | cinderv3        | volumev3       | True    | admin     | http://192.168.0.1:8776/v3/%(tenant_id)s              |


Create Cinder Volume

Source your credential in the Linux environment to run the command. Then use the below mention command to create the volume. In the command <volume_size> is the size of the volume in Gb you want to create and <volume_name> is the name for the volume.

Command Syntax:

openstack volume create --size <volume_size> <volume_name>

In the following example, we are creating a 5 GB volume name is demo-2 see the output command

(wallaby) [foofunc@cont ~]$ openstack volume create --size 5 demo-2
+---------------------+------------------------------------------------------------------+
| Field               | Value                                                            |
+---------------------+------------------------------------------------------------------+
| attachments         | []                                                               |
| availability_zone   | nova                                                             |
| bootable            | false                                                            |
| consistencygroup_id | None                                                             |
| created_at          | 2021-08-28T12:51:04.000000                                       |
| description         | None                                                             |
| encrypted           | False                                                            |
| id                  | 4350087b-9f5f-4a95-6c8d-8e43789b302a                             |
| multiattach         | False                                                            |
| name                | demo-2                                                           |
| properties          |                                                                  |
| replication_status  | None                                                             |
| size                | 5                                                                |
| snapshot_id         | None                                                             |
| source_volid        | None                                                             |
| status              | creating                                                         |
| type                | __DEFAULT__                                                      |
| updated_at          | None                                                             |
| user_id             | 1027fa268cbe223f45e14b167685bf72f3c4e68e3765fd99095d1e30972e1748 |
+---------------------+------------------------------------------------------------------+
(wallaby) [foofunc@cont ~]$ 

Verify

You can verify the volume is it created or not by running the following command and the output of the command shows that volume is created successfully.

(wallaby) [foofunc@cont ~]$ openstack volume list 
+--------------------------------------+-------------+-----------+------+-------------------------------+
| ID                                   | Name        | Status    | Size | Attached to                   |
+--------------------------------------+-------------+-----------+------+-------------------------------+
| 4350087b-9f5f-4a95-6c8d-8e43789b302a | demo-2      | available |    5 |                               |
| 6184e16d-4136-4c48-b443-01bebe38b8ea | demo-1      | in-use    |    5 | Attached to test on /dev/sdc  |
+--------------------------------------+-------------+-----------+------+-------------------------------+
(wallaby) [foofunc@cont ~]$ 

Also, you can run the following command to check the status. Where <volume_id> is the id assigned by the OpenStack.

Command syntax:

openstack volume show <Volume_id>

Example:

(wallaby) [foofunc@cont ~]$ openstack volume show 4350087b-9f5f-4a95-6c8d-8e43789b302a
+------------------------------+------------------------------------------------------------------+
| Field                        | Value                                                            |
+------------------------------+------------------------------------------------------------------+
| attachments                  | []                                                               |
| availability_zone            | nova                                                             |
| bootable                     | false                                                            |
| consistencygroup_id          | None                                                             |
| created_at                   | 2021-08-28T12:51:04.000000                                       |
| description                  | None                                                             |
| encrypted                    | False                                                            |
| id                           | 4350087b-9f5f-4a95-6c8d-8e43789b302a                             |
| multiattach                  | False                                                            |
| name                         | demo-2                                                           |
| os-vol-tenant-attr:tenant_id | a1cde705931e4dea960840b28c2b711a                                 |
| properties                   |                                                                  |
| replication_status           | None                                                             |
| size                         | 5                                                                |
| snapshot_id                  | None                                                             |
| source_volid                 | None                                                             |
| status                       | available                                                        |
| type                         | __DEFAULT__                                                      |
| updated_at                   | 2021-08-28T12:51:19.000000                                       |
| user_id                      | 1027fa268cbe223f45e14b167685bf72f3c4e68e3765fd99095d1e30972e1748 |
+------------------------------+------------------------------------------------------------------+
(wallaby) [foofunc@cont ~]$

Attach Volume to the VM

Before attaching the volume to the VM you have to know the id or name of the VM.

Use the following command to list all the VM and note down the name or ID

openstack server list
(wallaby) [foofunc@cont ~]$ openstack server list
+--------------------------------------+---------------+--------+---------------------------------+------------------------------------------------+----------------+
| ID                                   | Name          | Status | Networks                        | Image                                          | Flavor         |
+--------------------------------------+---------------+--------+---------------------------------+------------------------------------------------+----------------+
| d9d97e7a-c99b-42fa-8beb-f073c750c757 | test          | ACTIVE | Tnet=172.16.0.3  | CentOS 7                                       | s8             |
+--------------------------------------+---------------+--------+---------------------------------+------------------------------------------------+----------------+
(wallaby) [foofunc@cont ~]$ 

Now attach the volume (demo-2) which we created earlier to test VM use the following command.

Command Syntax

openstack server add volume <server_name or ID> <volume_name or ID >

Command Example:

(wallaby) [foofunc@cont ~]$ openstack server add volume test demo-2

Verify

For verification we check the VM details by running below mention commands here I omitted extra information I am just mentioning the volume field.

(wallaby) [foofunc@cont ~]$ openstack server show test
+-----------------------------+------------------------------------------------------------------+
| Field                       | Value                                                            |
+-----------------------------+------------------------------------------------------------------+
| volumes_attached            | id='4350087b-9f5f-4a95-6c8d-8e43789b302a'                        |
|                             | 
+-----------------------------+------------------------------------------------------------------+
(wallaby) [foofunc@cont ~]$ 

This article only shows till how to attach a block device into the Virtual Machine but it is just a block device, inorder to use this block device we have to create a filesystem ontop of it. For that you can follow How to use Logical Volume Manager (LVM) to create file system link.

Leave a Comment

Your email address will not be published. Required fields are marked *