Openstack - Attatch Cinder volume to Nova Instance

## What's this?

Cinder volume を Nova instance に接続、OS から使えるようにする

## Initial State

##### Nova instance

    # nova list
    +--------------------------------------+------------+--------+------------+-------------+----------------------+
    | ID                                   | Name       | Status | Task State | Power State | Networks             |
    +--------------------------------------+------------+--------+------------+-------------+----------------------+
    | 09e352e8-a38f-4366-83a3-8edeca2dcc2b | admin_vm01 | ACTIVE | -          | Running     | net04=192.168.12.115 |
    +--------------------------------------+------------+--------+------------+-------------+----------------------+

##### Cinder volume

    # cinder list
    +--------------------------------------+-----------+--------------+------+-------------+----------+-------------+
    |                  ID                  |   Status  | Display Name | Size | Volume Type | Bootable | Attached to |
    +--------------------------------------+-----------+--------------+------+-------------+----------+-------------+
    | 87c583f1-9a23-4918-b725-c097748d55c5 | available | admin_vol01  |  10  |     None    |  false   |             |
    +--------------------------------------+-----------+--------------+------+-------------+----------+-------------+

##### Disk status of nova instance

    [instance ~]# lsblk
    NAME   MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
    vda    253:0    0   3G  0 disk
    `-vda1 253:1    0   3G  0 part /

## Attach a cinder volume to a nova instance

##### Attach a cinder volume to a nova instance

    # nova volume-attach admin_vm01 87c583f1-9a23-4918-b725-c097748d55c5
    +----------+--------------------------------------+
    | Property | Value                                |
    +----------+--------------------------------------+
    | device   | /dev/vdb                             |
    | id       | 87c583f1-9a23-4918-b725-c097748d55c5 |
    | serverId | 09e352e8-a38f-4366-83a3-8edeca2dcc2b |
    | volumeId | 87c583f1-9a23-4918-b725-c097748d55c5 |
    +----------+--------------------------------------+

##### Check the attached cinder volume status

    # nova show admin_vm01 | egrep "Property|volumes_attached"
    | Property                             | Value                                                                      |
    | os-extended-volumes:volumes_attached | [{"id": "87c583f1-9a23-4918-b725-c097748d55c5"}]                           |

##### Disk status of nova instance

    [instance ~]# lsblk
    NAME   MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
    vda    253:0    0   3G  0 disk
    `-vda1 253:1    0   3G  0 part /
    vdb    253:16   0  10G  0 disk

## Format the device and make filesystem and mount

##### Format disk

    [instace ~]# fdisk /dev/vdb
    Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
    Building a new DOS disklabel with disk identifier 0xd19b0bc9.
    Changes will remain in memory only, until you decide to write them.
    After that, of course, the previous content won't be recoverable.

    Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

    WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
             switch off the mode (command 'c') and change display units to
             sectors (command 'u').

    Command (m for help): h
    h: unknown command
    Command action
       a   toggle a bootable flag
       b   edit bsd disklabel
       c   toggle the dos compatibility flag
       d   delete a partition
       l   list known partition types
       m   print this menu
       n   add a new partition
       o   create a new empty DOS partition table
       p   print the partition table
       q   quit without saving changes
       s   create a new empty Sun disklabel
       t   change a partition's system id
       u   change display/entry units
       v   verify the partition table
       w   write table to disk and exit
       x   extra functionality (experts only)

    Command (m for help): n
    Command action
       e   extended
       p   primary partition (1-4)
    p
    Partition number (1-4): 1
    First cylinder (1-20805, default 1):
    Using default value 1
    Last cylinder, +cylinders or +size{K,M,G} (1-20805, default 20805):
    Using default value 20805

    Command (m for help): p

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

       Device Boot      Start         End      Blocks   Id  System
    /dev/vdb1               1       20805    10485688+  83  Linux

    Command (m for help): w
    The partition table has been altered!

    Calling ioctl() to re-read partition table.
    Syncing disks.

##### Check the new partition

    [instance ~]# lsblk
    NAME   MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
    vda    253:0    0   3G  0 disk
    `-vda1 253:1    0   3G  0 part /
    vdb    253:16   0  10G  0 disk
    `-vdb1 253:17   0  10G  0 part

##### Mkfs

    [instance ~]# mkfs.ext4 /dev/vdb1
    mke2fs 1.41.12 (17-May-2010)
    Filesystem label=
    OS type: Linux
    Block size=4096 (log=2)
    Fragment size=4096 (log=2)
    Stride=0 blocks, Stripe width=0 blocks
    655360 inodes, 2621422 blocks
    131071 blocks (5.00%) reserved for the super user
    First data block=0
    Maximum filesystem blocks=2684354560
    80 block groups
    32768 blocks per group, 32768 fragments per group
    8192 inodes per group
    Superblock backups stored on blocks:
            32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632

    Writing inode tables: done
    Creating journal (32768 blocks): done
    Writing superblocks and filesystem accounting information: done

    This filesystem will be automatically checked every 27 mounts or
    180 days, whichever comes first.  Use tune2fs -c or -i to override.

##### Mount the device

    [instance ~]# mkdir -p /mnt/cinder

    [instance ~]# mount /dev/vdb1 /mnt/cinder/

    [instance ~]# df -h
    Filesystem      Size  Used Avail Use% Mounted on
    /dev/vda1       3.0G  945M  1.9G  33% /
    tmpfs           499M     0  499M   0% /dev/shm
    /dev/vdb1       9.9G  151M  9.2G   2% /mnt/cinder

##### Check status

    [instance ~]# touch /mnt/cinder/test

    [instance ~]# ls -l /mnt/cinder/test
    -rw-r--r--. 1 root root 0 Apr 21 02:12 /mnt/cinder/test


## Environment

* Openstack : Juno
* Ceph : Firefly