16.3. Accessing a Volume from a Running Instance

Once you attach a volume to an instance a new device will appear to the guest operating system. The device is accessible both using a traditional device label such as /dev/vdc and in the /dev/disk/by-id/ tree.
Volumes appear in /dev/disk/by-id/ with identifiers of the form virtio-ID where ID is a subset of the volume identifier assigned when the volume was defined in Cinder.
For example a disk with the identifier 15a9f901-ba9d-45e1-8622-a5438473ae76 in Cinder appears as /dev/disk/by-id/virtio-15a9f901-ba9d-45e1-8 when viewed from within a virtual machine instance that it is attached to.
vm$ ls /dev/disk/by-id/
virtio-15a9f901-ba9d-45e1-8
Create a filesystem on the device and mount it in the virtual machine:
vm$ mkfs.ext4 /dev/disk/by-id/virtio-15a9f901-ba9d-45e1-8
vm$ mkdir -p /mnt/volume
vm$ mount /dev/disk/by-id/virtio-15a9f901-ba9d-45e1-8 /mnt/volume
Write some data to the mounted volume:
vm$ echo "Red Hat OpenStack" > /mnt/volume/test.txt
Unmount the volume inside the virtual machine.
vm$ umount /mnt/volume
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:
$ nova volume-detach <instanceid> <volumeid>
To verify that the data written to the volume has persisted, you 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 15a9f901-ba9d-45e1-8622-a5438473ae76 auto
$ ssh -i oskey.priv root@10.0.0.3
vm2$ mkdir -p /mnt/volume
vm2$ mount /dev/disk/by-id/virtio-15a9f901-ba9d-45e1-8 /mnt/volume
vm2$ cat /mnt/volume/test.txt
Red Hat OpenStack
vm2$ umount /mnt/volume
And now detach the volume, where the first id is the instance id (Nova) and the second id is the volume id (Cinder):
$ nova volume-detach b8d5c952-f2fc-4556-83f2-57c79378d867 15a9f901-ba9d-45e1-8622-a5438473ae76