Product SiteDocumentation Site

6.2. Creating and Attaching a Volume

We must first create a volume before we can attach it to an instance. When a new volume is created, it will be created as a logical volume in the nova-volumes volume group created in Chapter 4, Nova (Compute). Run the following commands to create a 1 GB volume and then attach it to an instance. Use the nova list command to get the ID of the instance you would like to attach the volume to.
$ nova volume-create --display_name "test volume" 1
$ nova volume-list
+----+-----------+--------------+------+-------------+-------------+
| ID |   Status  | Display Name | Size | Volume Type | Attached to |
+----+-----------+--------------+------+-------------+-------------+
| 1  | available | test volume  | 1    | None        |             |
+----+-----------+--------------+------+-------------+-------------+
$ nova volume-attach <instanceid> 1 /dev/vdc
You should now see the new device (/dev/vdc in this case) available from inside the virtual machine.
vm$ cat /proc/partitions
major minor  #blocks  name

 252        0   10485760 vda
 252        1        960 vda1
 252        2    9765625 vda2
 252       16   20971520 vdb
 252       32    1048576 vdc
Create a filesystem on the device and mount it in the virtual machine.
vm$ mkfs.ext4 /dev/vdc
vm$ mkdir -p /mnt/volume
vm$ mount /dev/vdc /mnt/volume
Write some data to the mounted volume.
vm$ echo "Red Hat Summit" > /mnt/volume/test.txt
Unmount the volume inside the virtual machine. From the host running Nova, detach the volume from the instance. The volume-detach command requires an instance ID and the volume ID you would like to detach from that instance.
vm$ umount /mnt/volume
$ nova volume-detach <instanceid> 1
To verify that the data written to the volume has persisted, we can start up a new instance. Once the new instance is in the ACTIVE state, attach the volume to that instance, and then mount the volume in the instance.
$ nova boot --image <imageid> --flavor 2 --key_name oskey rhel2
+------------------------+--------------------------------------+
|        Property        |                Value                 |
+------------------------+--------------------------------------+
| OS-DCF:diskConfig      | MANUAL                               |
| OS-EXT-STS:power_state | 0                                    |
| OS-EXT-STS:task_state  | scheduling                           |
| OS-EXT-STS:vm_state    | building                             |
| accessIPv4             |                                      |
| accessIPv6             |                                      |
| adminPass              | uPnzQhpdZZf9                         |
| config_drive           |                                      |
| created                | 2012-05-18T13:45:56Z                 |
| flavor                 | m1.small                             |
| hostId                 |                                      |
| id                     | b8d5c952-f2fc-4556-83f2-57c79378d867 |
| image                  | RHEL 6.2                             |
| key_name               | oskey                                |
| metadata               | {}                                   |
| name                   | rhel2                                |
| progress               | 0                                    |
| status                 | BUILD                                |
| tenant_id              | 05816b0106994f95a83b913d4ff995eb     |
| updated                | 2012-05-18T13:45:56Z                 |
| user_id                | 1d59c0bfef9b4ea9ab63e2a058e68ae0     |
+------------------------+--------------------------------------+
$ nova list
+--------------------------------------+---------+--------+------------------+
|                  ID                  |   Name  | Status |     Networks     |
+--------------------------------------+---------+--------+------------------+
| 0e4011a4-3128-4674-ab16-dd1b7ecc126e | rhel    | ACTIVE | demonet=10.0.0.2 |
| b8d5c952-f2fc-4556-83f2-57c79378d867 | rhel2   | BUILD  | demonet=10.0.0.3 |
+--------------------------------------+---------+--------+------------------+
$ nova list
+--------------------------------------+---------+--------+------------------+
|                  ID                  |   Name  | Status |     Networks     |
+--------------------------------------+---------+--------+------------------+
| 0e4011a4-3128-4674-ab16-dd1b7ecc126e | rhel    | ACTIVE | demonet=10.0.0.2 |
| b8d5c952-f2fc-4556-83f2-57c79378d867 | rhel2   | ACTIVE | demonet=10.0.0.3 |
+--------------------------------------+---------+--------+------------------+
$ nova volume-attach b8d5c952-f2fc-4556-83f2-57c79378d867 1 /dev/vdc
$ ssh -i oskey.priv root@10.0.0.3
vm2$ mkdir -p /mnt/volume
vm2$ mount /dev/vdc /mnt/volume
vm2$ cat /mnt/volume/test.txt
Red Hat Summit
vm2$ umount /mnt/volume
$ nova volume-detach b8d5c952-f2fc-4556-83f2-57c79378d867 1