Formatting and mounting a volume in Linux

This guide assumes you associated your SSH Key Pair with the instance when it was created, and that you are connected to the Anvil VPN.

Once you have created and attached your volume, it must be formatted and mounted in your Linux instance to be usable.  This procedure is identical to what would be done when attaching a second hard drive to a physical machine.  In this example, a 1GB volume was created and attached to the instance.  Note that the majority of this guide is for a newly created volume.  

If you are attaching an existing volume with data already on it, skip to creating a directory and mounting the volume.

Formatting the volume

Follow the relevant guide (WindowsMac) for your operating system to connect to your instance.  Formatting and mounting the volume requires root privileges, so first run the command sudo su - to get a root shell.

Running commands as root
Extreme care should be taken when running commands as root. It is very easy to permanently delete data or cause irreparable damage to your instance.

Next, you will need to determine what device the volume is presented as within Linux.  Typically this will be /dev/vdb, but it is necessary to verify this to avoid mistakes, especially if you have more than one volume attached to an instance.  The command lsblk will list the hard drive devices and partitions.

Here there is a completely empty (no partitions) disk device matching the 1GB size of the volume, so /dev/vdb is the correct device. The parted utility will first be used to label the device and then create a partition.

parted /dev/vdb mklabel gpt
parted /dev/vdb mkpart primary 0% 100%

Now that a partition has been created, it can be formatted.  Here, the ext4 filesystem will be used.  This is the default filesystem used by many Linux distributions including CentOS and Ubuntu, and is a good general choice.  An alternate filesystem may be used by running a different format command.  To format the partition using ext4, run the command mkfs.ext4 /dev/vdb1.  You will see a progress message and then be returned to the shell prompt.

Mounting the volume

If you are attaching a pre-existing volume, start here.

Finally, the formatted partition must be mounted as a directory to be used.  By convention this is done under /mnt, but you may choose to mount it elsewhere depending on the usage.  Here, a directory called myvolume will be created and the volume mounted there.  Run the following commands to make the directory and mount the volume:

mkdir /mnt/myvolume
mount /dev/vdb1 /mnt/myvolume

Running the command df -h should then show the new mounted empty volume.  

The volume can now be used.