Back

Create and attach persistent volumes to OpenStack instances

Overview

Instances spun up by OpenStack are ephemeral. For storage that persists across an instance lifecycle, it is necessary to attach and mount a volume to the instance.

Prerequisites

This guide assumes that access to the OpenStack CLI is installed and the user has authenticated with the cloud.

Process

If you don’t already have a keypair, create one and redirect the private part of the key to the default .ssh key store directory.

$ openstack keypair create KEYPAIR_NAME > ~/.ssh/KEYPAIR_FILENAME

where:

Change the permission on the private part of the keypair.

    sudo chmod 600 ~/.ssh/KEYPAIR_FILENAME

Create the volume.

    $ openstack volume create --size VOLUME_SIZE --type VOLUME_TYPE VOLUME_NAME

where:

If one doesn’t already exist, spin up an instance.

    $ openstack server create --image INSTANCE_IMAGE_NAME --flavor INSTANCE_FLAVOR --key-name KEYPAIR_NAME --network INSTANCE_NETWORK_NAME INSTANCE_NAME

where:

Attach the volume to the instance.

    $ openstack server add volume INSTANCE_NAME VOLUME_NAME

Assign a floating IP from an externally facing pool.

    $ openstack server add floating ip INSTANCE_NAME EXTERNAL_FLOATING_IP

where:

Remove any existing entry for the EXTERNAL_FLOATING_IP in the known_hosts file.

    $ ssh-keygen -f "/home/$(USER)/.ssh/known_hosts" -R EXTERNAL_FLOATING_IP > /dev/null

Partition the disk volume (note that the device mount location may be different than /dev/vdb if another storage device has already been mounted; check output of $ sudo fdisk -l if unsure) and create a new filesystem on the partition.

    $ ssh -l IAM_USERNAME -o "StrictHostKeyChecking no" -i ~/.ssh/KEYPAIR_FILENAME EXTERNAL_FLOATING_IP 'sudo parted -s /dev/vdb mklabel msdos && sudo parted -s /dev/vdb mkpart primary ext4 1MiB 100% && sudo mkfs -t ext4 /dev/vdb1'

Add an entry for mounting the partition to the file systems table, /etc/fstab.

    $ ssh -l IAM_USERNAME -o "StrictHostKeyChecking no" -i ~/.ssh/KEYPAIR_FILENAME EXTERNAL_FLOATING_IP 'sudo su -c "echo /dev/vdb1 /mnt/volume1 ext4 rw 0 0 >> /etc/fstab"'

Create a directory at the mount point, mount the volume to the instance filesystem and symlink the volume’s mount directory to /data.

    $ ssh -l IAM_USERNAME -o "StrictHostKeyChecking no" -i ~/.ssh/KEYPAIR_FILENAME EXTERNAL_FLOATING_IP 'sudo mkdir /mnt/volume1 && sudo mount /dev/vdb1 && sudo ln -s /mnt/volume1/ /data'

where:

The mount command can be flagged with -r to issue a read only mounted filesystem.

Change the mount directory permissions to allow read/write access for all users.

    $ ssh -l IAM_USERNAME -o "StrictHostKeyChecking no" -i ~/.ssh/KEYPAIR_FILENAME EXTERNAL_FLOATING_IP 'sudo chmod o+w /mnt/volume1'

Top