How to create an OpenStack Cinder volume and attach it to a VM instance

How do I create a Cinder volume in OpenStack and attach it to an instance? The OpenStack Cinder service provides persistent block storage management for virtual hard drives. With the OpenStack CLI and Horizon Web interface, you can manage the creation, attachment, and detachment of block devices to servers. In this guide, we will discuss how to create, attach, and detach Cinder volumes on a server running in an OpenStack environment.

You need to configure the OpenStack CLI for this guide to make sense. We have a separate article on how to install and configure the OpenStack client on Linux. For information on installing and configuring the OpenStack Client on a Linux system, refer to it.

Step 1: Check the OpenStack Cinder service

After setting up the OpenStack client, check to confirm that all Cinder services are running. Cinder consists of the following ingredients:

  • openstack-cinder-volume, You can allocate storage space for virtual machines on demand. A number of drivers are provided for interacting with the storage provider.
  • openstack-cinder-api, Which responds to and processes the request and places it in a message queue.
  • openstack-cinder-scheduler, Which assigns tasks to queues and determines the configuration volume server.

Use the following commands to confirm the status of these services.

for i in volume api scheduler; do
 systemctl status openstack-cinder-$i
done

Sample output-all states should be Run.

......
● openstack-cinder-volume.service - OpenStack Cinder Volume Server
    Loaded: loaded (/usr/lib/systemd/system/openstack-cinder-volume.service; enabled; vendor preset: disabled)
    Active: active (running) since Sat 2019-09-07 10:13:38 EAT; 14min ago
  Main PID: 6427 (cinder-volume)
     Tasks: 2
    CGroup: /system.slice/openstack-cinder-volume.service
            ├─6427 /usr/bin/python2 /usr/bin/cinder-volume --config-file /usr/share/cinder/cinder-dist.conf --config-file /etc/cinder/cinder.conf…
            └─6468 /usr/bin/python2 /usr/bin/cinder-volume --config-file /usr/share/cinder/cinder-dist.conf --config-file /etc/cinder/cinder.conf…

...............
● openstack-cinder-api.service - OpenStack Cinder API Server
    Loaded: loaded (/usr/lib/systemd/system/openstack-cinder-api.service; enabled; vendor preset: disabled)
    Active: active (running) since Sat 2019-09-07 10:13:42 EAT; 14min ago
  Main PID: 6599 (cinder-api)
     Tasks: 9
    CGroup: /system.slice/openstack-cinder-api.service
            ├─6599 /usr/bin/python2 /usr/bin/cinder-api --config-file /usr/share/cinder/cinder-dist.conf --config-file /etc/cinder/cinder.conf --…
            ├─6691 /usr/bin/python2 /usr/bin/cinder-api --config-file /usr/share/cinder/cinder-dist.conf --config-file /etc/cinder/cinder.conf --…
            ├─6692 /usr/bin/python2 /usr/bin/cinder-api --config-file /usr/share/cinder/cinder-dist.conf --config-file /etc/cinder/cinder.conf --…
            ├─6693 /usr/bin/python2 /usr/bin/cinder-api --config-file /usr/share/cinder/cinder-dist.conf --config-file /etc/cinder/cinder.conf --…
            ├─6694 /usr/bin/python2 /usr/bin/cinder-api --config-file /usr/share/cinder/cinder-dist.conf --config-file /etc/cinder/cinder.conf --…
            ├─6695 /usr/bin/python2 /usr/bin/cinder-api --config-file /usr/share/cinder/cinder-dist.conf --config-file /etc/cinder/cinder.conf --…
            ├─6696 /usr/bin/python2 /usr/bin/cinder-api --config-file /usr/share/cinder/cinder-dist.conf --config-file /etc/cinder/cinder.conf --…
            ├─6697 /usr/bin/python2 /usr/bin/cinder-api --config-file /usr/share/cinder/cinder-dist.conf --config-file /etc/cinder/cinder.conf --…
            └─6699 /usr/bin/python2 /usr/bin/cinder-api --config-file /usr/share/cinder/cinder-dist.conf --config-file /etc/cinder/cinder.conf --…

......................
● openstack-cinder-scheduler.service - OpenStack Cinder Scheduler Server
    Loaded: loaded (/usr/lib/systemd/system/openstack-cinder-scheduler.service; enabled; vendor preset: disabled)
    Active: active (running) since Sat 2019-09-07 10:13:46 EAT; 14min ago
  Main PID: 6792 (cinder-schedule)
     Tasks: 1
    CGroup: /system.slice/openstack-cinder-scheduler.service
            └─6792 /usr/bin/python2 /usr/bin/cinder-scheduler --config-file /usr/share/cinder/cinder-dist.conf --config-file /etc/cinder/cinder.c…

Step 2: Create cinder volume

You need to configure the Cinder storage backend and confirm that it is running properly. I use the LVM backend in the setup.

$ pvs | grep cinder
  /dev/nvme0n1p1 cinder-volumes lvm2 a--  <476.94g   23.59g

$ vgs | grep cinder
  cinder-volumes   1   4   0 wz--n- <476.94g   23.59g

Create a Cinder Volume using the following command syntax.

$ openstack volume create --size  

where:

  • Volume size (in GB)
  • Is the name of the volume to be created.

The following example will create a file named c4geeks-db-backups size 30GB

$ openstack volume create --size 30 c4geeks-db-backups

+---------------------+--------------------------------------+
| Field               | Value                                |
+---------------------+--------------------------------------+
| attachments         | []                                   |
| availability_zone   | nova                                 |
| bootable            | false                                |
| consistencygroup_id | None                                 |
| created_at          | 2019-09-07T07:37:14.000000           |
| description         | None                                 |
| encrypted           | False                                |
| id                  | 4a4f5cf1-9122-4147-83c1-5aa622f7ba57 |
| migration_status    | None                                 |
| multiattach         | False                                |
| name                | c4geeks-db-backups                   |
| properties          |                                      |
| replication_status  | None                                 |
| size                | 30                                   |
| snapshot_id         | None                                 |
| source_volid        | None                                 |
| status              | creating                             |
| type                | None                                 |
| updated_at          | None                                 |
| user_id             | 336acbb7421f47f8be4891eabf0c9cc8     |
+---------------------+--------------------------------------+

Confirm the volume creation status.

$ openstack volume list
+--------------------------------------+---------------------+----------------+------+--------------------------------------+
| ID                                   | Name                | Status         | Size | Attached to                          |
+--------------------------------------+---------------------+----------------+------+--------------------------------------+
| 4a4f5cf1-9122-4147-83c1-5aa622f7ba57 | c4geeks-db-backups  | available      |   30 |                                      |
+--------------------------------------+---------------------+----------------+------+--------------------------------------+

We can confirm the size of the volume 30GB Created and ready to use. Let's attach it to a server running on OpenStack.

But first, please pull the virtual machine name / ID

$ openstack server list
+--------------------------------------+-------------------+--------+-----------------------------------+----------+-----------+
| ID                                   | Name              | Status | Networks                          | Image    | Flavor    |
+--------------------------------------+-------------------+--------+-----------------------------------+----------+-----------+
| 08df4929-4d9e-4881-9f01-6d89e9862385 | c4geeks-db-server | ACTIVE | private=10.10.1.196               | CentOS-7 | m1.large  |
+--------------------------------------+-------------------+--------+-----------------------------------+----------+-----------+

Attach the created volume to the server.

openstack server add volume c4geeks-db-server c4geeks-db-backups 

Check Cinder Volumes attachment.

$ openstack volume list
+--------------------------------------+---------------------+----------------+------+--------------------------------------------+
| ID                                   | Name                | Status         | Size | Attached to                                |
+--------------------------------------+---------------------+----------------+------+--------------------------------------------+
| 4a4f5cf1-9122-4147-83c1-5aa622f7ba57 | c4geeks-db-backups  | in-use         |   30 | Attached to c4geeks-db-server on /dev/vdc  |
+--------------------------------------+---------------------+----------------+------+--------------------------------------------+

Step 3: Use Cinder volumes on the virtual machine

Log in to your server and check if the raw block device is visible.

$ lsblk /dev/vdc
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
vdc  253:32   0  30G  0 disk 

Now let's partition and mount it.

sudo parted -s -a optimal -- /dev/vdc mklabel gpt
sudo parted -s -a optimal -- /dev/vdc mkpart primary 0% 100%
sudo parted -s -- /dev/vdc align-check optimal 1
sudo mkfs.xfs /dev/vdc1
sudo mkdir /data
echo "/dev/vdc1 /data xfs defaults 0 0" | sudo tee -a /etc/fstab
sudo mount -a

View the current installation diagram.

$ df -hT
Filesystem     Type      Size  Used Avail Use% Mounted on
/dev/vda1      xfs        40G  2.6G   38G   7% /
devtmpfs       devtmpfs  3.8G     0  3.8G   0% /dev
tmpfs          tmpfs     3.9G     0  3.9G   0% /dev/shm
tmpfs          tmpfs     3.9G   17M  3.9G   1% /run
tmpfs          tmpfs     3.9G     0  3.9G   0% /sys/fs/cgroup
tmpfs          tmpfs     783M     0  783M   0% /run/user/1000
/dev/vdc1      xfs        30G   33M   30G   1% /data

Began to take advantage of increased block storage in daily operations.

Detach Cinder virtual disk from server

To detach and delete the disk, uninstall it on the server and run:

openstack server remove volume c4geeks-db-server c4geeks-db-backups
openstack volume delete c4geeks-db-backups

Check out other OpenStack tutorials available on our blog.

Best book for learning OpenStack Cloud Platform 2019

How to resize an OpenStack instance / virtual machine

OpenStack deployment on CentOS 7 with Packstack

How to install and configure OpenStack Client on Linux

OpenStack deployment on Ubuntu 18.04 with DevStack

How to add Glance Cloud images to OpenStack

Add SSH key pair to OpenStack using CLI

How to add flavor to OpenStack using the CLI

Sidebar