Cinder: create a volume and attach it to one instance

To create a new block storage volume execute the following command

root@os-controller-01 .ssh(keystone_admin)]# cinder create 10 --display-name sbv_a-instance02_data_01
+---------------------+--------------------------------------+
|       Property      |                Value                 |
+---------------------+--------------------------------------+
|     attachments     |                  []                  |
|  availability_zone  |                 nova                 |
|       bootable      |                false                 |
|      created_at     |      2016-05-08T18:41:23.006885      |
| display_description |                 None                 |
|     display_name    |       sbv_a-instance02_data_01       |
|      encrypted      |                False                 |
|          id         | 7d257820-c696-44ec-8074-804eae087b3a |
|       metadata      |                  {}                  |
|         size        |                  10                  |
|     snapshot_id     |                 None                 |
|     source_volid    |                 None                 |
|        status       |               creating               |
|     volume_type     |                 None                 |
+---------------------+--------------------------------------+
[root@os-controller-01 .ssh(keystone_admin)]# cinder list
+--------------------------------------+-----------+--------------------------+------+-------------+----------+-------------+
|                  ID                  |   Status  |       Display Name       | Size | Volume Type | Bootable | Attached to |
+--------------------------------------+-----------+--------------------------+------+-------------+----------+-------------+
| 7d257820-c696-44ec-8074-804eae087b3a | available | sbv_a-instance02_data_01 |  10  |     None    |  false   |             |
| e10b8be0-bfd6-4f19-87d6-936ecd5a8194 | available |       sbv_data_01        |  10  |     None    |  false   |             |
+--------------------------------------+-----------+--------------------------+------+-------------+----------+-------------+

With this command we have created a block volume called sbv_a-instance02_data_01 of 10GB. You will have to run the command cinder list to check that the creation finished. When the status says available it means it has finished.

Before to attach the volume to the instance lets go to check the current disks that the instance has. This will facilitate the location of the new disk once attached.

$ sudo fdisk -l

Disk /dev/vda: 1073 MB, 1073741824 bytes
255 heads, 63 sectors/track, 130 cylinders, total 2097152 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *       16065     2088449     1036192+  83  Linux

By issuing fdisk -l you can see you only got one disk for now. Now you can proceed to attach the volume to the instance. To do it you will run the following command where the first parameter is the instance name and the second the volume id. Finally listing volumes we can be it has been associated.

[root@os-controller-01 .ssh(keystone_admin)]# nova volume-attach a-instance02 7d257820-c696-44ec-8074-804eae087b3a
+----------+--------------------------------------+
| Property | Value                                |
+----------+--------------------------------------+
| device   | /dev/vdb                             |
| id       | 7d257820-c696-44ec-8074-804eae087b3a |
| serverId | 8c13516c-0f47-4938-a943-9bc3847889e9 |
| volumeId | 7d257820-c696-44ec-8074-804eae087b3a |
+----------+--------------------------------------+
[root@os-controller-01 .ssh(keystone_admin)]# cinder list
+--------------------------------------+-----------+--------------------------+------+-------------+----------+--------------------------------------+
|                  ID                  |   Status  |       Display Name       | Size | Volume Type | Bootable |             Attached to              |
+--------------------------------------+-----------+--------------------------+------+-------------+----------+--------------------------------------+
| 7d257820-c696-44ec-8074-804eae087b3a |   in-use  | sbv_a-instance02_data_01 |  10  |     None    |  false   | 8c13516c-0f47-4938-a943-9bc3847889e9 |
| e10b8be0-bfd6-4f19-87d6-936ecd5a8194 | available |       sbv_data_01        |  10  |     None    |  false   |                                      |
+--------------------------------------+-----------+--------------------------+------+-------------+----------+--------------------------------------+

The volume created has been attached as the device /dev/vdb to the instance. If you run again the fdisk -l command you should be able to see the new disk.

$ sudo fdisk -l

Disk /dev/vda: 1073 MB, 1073741824 bytes
255 heads, 63 sectors/track, 130 cylinders, total 2097152 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *       16065     2088449     1036192+  83  Linux

Disk /dev/vdb: 10.7 GB, 10737418240 bytes
16 heads, 63 sectors/track, 20805 cylinders, total 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

Disk /dev/vdb doesn't contain a valid partition table

Finally you can proceed to use this disk as any other disk. You can create the partition table, create a partition, format the partition and mount the partition as you wish.

Leave a comment