Attaching volumes to an instance

Now that we have a usable volume, we can attach it to any instance. We'll do this using the openstack server volume add command.

Getting ready

To attach a volume to an instance, you will need the following:

  • The openstack command-line client
  • An openrc file with appropriate credentials for the environment
  • The name or ID of the volume to attach
  • The name or ID of the instance to attach the volume to

For our example, these values are as follows:

  • Volume: cookbook.volume
  • Instance: cookbook.test

How to do it...

Carry out the following steps to attach a volume to an instance using the openstack client:

  1. First, let's list running instances to get the ID of our instance:
    openstack server list -c Name -c ID -f table
    

    An example showing our running instance called cookbook.test is shown here:

    How to do it...
  2. Now list the available volumes to get the ID of our volume:
    openstack volume list
    

    This shows the information we need about our volume:

    How to do it...
  3. Using the instance and volume name or IDs, we'll attach the volume to the instance as follows:
    openstack server add volume
        cookbook.test
        cookbook.volume
        --device /dev/vdc
    

    This command produces no output when successful.

    Note that the --device option is not always honored, depending on the operating system and image type. Always check which device the target instance operating system assigns to the new volume before performing any actions on it.

    Tip

    Tip: Volume or server names do not have to be unique in OpenStack; where volume and server names are not unique, replace the names with the IDs assigned instead. In the preceding example, this would achieve the same goal:

    openstack server add volume
        90654098-477f-417f-b102-453c0f1f9119
        daeddd6c-908a-4ea8-8632-d93d198c2621
        --device /dev/vdb
    
  4. Now we will perform actions inside our running instance. Log in to the instance and verify that the volume is now attached:
    lsblk
    

    This will list the block devices available to our instance. Here we can see that our volume, attached as /dev/vdb, is available but not mounted:

    NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
    sr0     11:0    1  558K  0 rom
    vda    253:0    0  2.2G  0 disk
    `-vda1 253:1    0  2.2G  0 part /
    vdb    253:16    0  10G   0 disk
    
  5. We should see 10G of space available for use by the running instance. As this is a new volume, this is like adding a fresh disk to a system. We need to format it for use and then mount it as part of your filesystem:
    sudo mkfs.ext4 /dev/vdb
    sudo mkdir /mnt1
    sudo mount /dev/vdb /mnt1
    

    Tip

    Tip: Volumes created with Cinder are persistent storage volumes; formatting the volume (disk) is only required once. Should you need to re-attach this data volume to another instance in the future, do not reformat this drive!

  6. We should now see the newly attached disk available at /mnt1:
    df -h
    

    This will show output like the following:

    Filesystem Size Used Avail Use% Mounted on
    udev       238M 0    238M  0%   /dev
    tmpfs      49M  1.8M 48M   4%   /run
    /dev/vda1  2.1G 843M 1.3G  41%  /
    tmpfs      245M 0    245M  0%   /dev/shm
    tmpfs      5.0M 0    5.0M  0%   /run/lock
    tmpfs      245M 0    245M  0%   /sys/fs/cgroup
    tmpfs      49M  0    49M   0%   /run/user/1000
    /dev/vdb    9.8G 23M  9.2G   1%   /mnt1
    

How it works...

Attaching a new Cinder volume is very much like plugging in an unformatted USB stick into your own computer. When it needs to first be used, it must be formatted for use.

Tip

On subsequent uses of this disk (when connecting to other instances in the future), you wouldn't need to perform this formatting step.

Under the openstack client, the server add volume option takes the following syntax:

openstack server add volume
    instance_ID
    volume_ID
    --device /dev/device_ID

instance_ID is the ID returned from openstack server list for the instance that we want to attach the volume to.

volume_ID is the ID of the volume returned from openstack volume list.

device_ID is the device that will be created on our instance that we use to mount the volume. Remember that this parameter can sometimes be ignored; so, see this as a hint to pass to a running instance only.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.184.223