Intro

Block storage allows you to create and attach additional storage volumes to your instances. These volumes are a totally independent resource that can be moved from one instance to another. Attached block storage will work like locally connected storage drives, which means managing your storage is easy and you can use familiar tools to do so.

In this learn guide, we will cover the following subjects:

  • What is Block Storage?
  • What are the common ways to use Block Storage?
  • How to create and attach volumes to your Instances
  • How to partition and format volumes
  • How to resize volumes
  • How to detach and delete volumes

What is Block Storage?

Block Storage is a convenient and flexible way of managing additional storage for your instances. Block Storage is configured in units known as volumes. Volumes function as block devices, this means they appear to the operating system as locally attached storage drives which can be partitioned and formatted to your individual needs ( handy right?! ).

A couple of points you should know about Block Storage:

  • All Block Storage volumes are backed by SSD's
  • Volumes are region specific resources. You are able to move your volumes between any instance in the same region
  • A volume can only be attached to a single instance at any one time

When And Why Should I Use Block Storage?

Good question! Block Storage is a fantastic solution for when you need that little ( or much ) more storage space, but you do not need or require the additional processing power or memory that a larger Civo instance can provide. The volumes can be created, destroyed and expanded easily as your needs for them change. You can start small and grow as you require which means you only use the storage space you need, rather than having a big chunk you're not using but still paying for.

Volumes are great because they function as generic block devices, they can be very useful in a number of different ways. A few examples of this are:

  • To store database files for a database server such as MySQL, PostgreSQL and others
  • As a target device for backups
  • As the media uploads directory for a web server

Block Storage is not a specialised resource, which sounds bad but that's what makes it so versatile. It can be used for just about anything that will benefit from additional disk space, which in our books is only a good thing!

How to Create and Attach New Volumes

Creating a new volume is nice and easy through the Civo control panel. You can create a volume by clicking on the Volumes section your dashboard and then select Create Volume :

Civo-create-vol-step-1.png

Civo-Create-vol-step-1.1.png

Once done, it will take you to the next section where you can decide what size you want your volume to be and to give it a name of your choice. Once you have selected a name and size of your volume , simply click on the Create button to setup your first volume:

Civo-create-vol-step-2.png

We now have the volume created and we can attach it to any instance of your choice. To do this simply click on the Attach to an instance icon to the right of your volume name and the select the instance from the drop down menu provided:

Civo-create-vol-step-3.png

testvol-attach.png

Click Attach and you will now have your volume attached to your instance:

Civo-create-vol-step-5.png

You can also see how it looks from your instance page as well:

Civo create-vol-step-6.png

Getting the Volume ready for Use in Linux

Once you attach a volume to your instance, it will need to be prepared for use as you would for any new disk added to a Linux operating system. The steps required by this will depend on whether you are attaching a newly created volume or setting up and existing volume on a new instance. So we will cover both to make sure no mistakes are made.

Preparing a Newly Created Volume

Once you have created a new volume, it will require some preparation before your instance can begin to utilise the space. A volume must be partitioned, formatted with a filesystem and then finally mounted. We would also recommend that you modify the /etc/fstab so that the volume will automatically mount at boot.

This process can be modified significantly depending on your individual needs and it may also vary depending on the Linux distribution, the procedure we are using can be used to get up and running quickly in most user cases. The following commands below will:

  • Create a single GPT partition ( using parted ) that will span the entire volume
  • Format the partition with the Ext4 filesystem
  • Create a mount point under the /mnt directory
  • Mount the filesystem
  • Adjust the /etc/fstabto define a persistent mount

Before running the commands, make sure you are logged in with a user that as sudo privileges or you are the root user.

First we need to get back the volume ID. To do this, simply click on the Volumes link on your Civo dashboard which will display the ID of the volume for you:

Civo-create-vol-step-5.png

As we can see the ID is /dev/vdb.

Next we need to use parted to create a GPT partition. First we need to enter the disk we want to make the partiton on:

sudo parted /dev/vdb
GNU Parted 3.2
Using /dev/vdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted)

Next we need to print the disk to make sure we are using the right one:

(parted) print                                                            
Error: /dev/vdb: unrecognised disk label
Model: Virtio Block Device (virtblk)                                      
Disk /dev/vdb: 10.7GB
Sector size (logical/physical): 512B/512B
Partition Table: unknown
Disk Flags:

We now need to give the disk a GPT label name using the mklabel command:

(parted) sudo mklabel gpt
(parted)

Now we have created the label for the disk, we need to use the mkpart command to create a primary partition.:

(parted) mkpart                                                           
Partition name?  []? testvol                                              
File system type?  [ext2]? ext4                                           
Start? 1                                                                  
End? 10700

*Note, the start and end units are in megabytes and as we are using a 10.7GB partition it will start from 1 and end at 10700 *

Now that is done we can quit the parted manager and format our new partition in the ext4 file system using the mkfs command:

(parted) quit                                                             
Information: You may need to update /etc/fstab.
mkfs.ext4 /dev/vdb1
mke2fs 1.42.13 (17-May-2015)
Creating filesystem with 2441216 4k blocks and 610800 inodes
Filesystem UUID: 3e71e9c7-96a6-4025-9c83-b4122c36ac42
Superblock backups stored on blocks: 
    32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632

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

Now we have created the filesystem, we can use the parted command again to check it has worked and it has been formatted with the ex4 filesystem:

sudo parted /dev/vdb
/dev/vdb
GNU Parted 3.2
Using /dev/vdb
Welcome to GNU Parted! Type 'help' to view a list of commands.

(parted) print                                                            
Model: Virtio Block Device (virtblk)
Disk /dev/vdb: 10.7GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start   End     Size    File system  Name     Flags
 1      1049kB  10.0GB  9999MB  ext4         testvol

Excellent! As we can see we have the correct size and the right filesystem type. We can now exit parted and make a folder to mount our volume:

(parted) quit

sudo mkdir /mnt/myfirstvol
sudo mount /dev/vdb1 /mnt/myfirstvol

Testing the Mounted Filesystem

We should now have a mounted filesystem and we can test that it was successful by using the following command:

findmnt /mnt/myfirstvol

You should get a similar output as below:

TARGET               SOURCE       FSTYPE  OPTIONS
/mnt/myfirstvol /dev/vdb1 ext4   rw,relatime,data=ordered

Adjust /etc/fstab to define a persistent mount

Now we have a mounted filesystem we want to ensure it remains persistent during reboots of your instance. To do this we need to edit the /etc/fstab and pass it some options to ensure the volume comes up on boot:

sudo vim /etc/fstab

You should get an output similar to below:

LABEL=cloudimg-rootfs   /        ext4   defaults        0 0

We can now add our volume so it is also mounted using the default values for the ext4 filesystem:

/dev/vdb1        /mnt/myfirstvol ext4   defaults        0 0

Once you have added the values, save and exit your editor. Hurray! Your volume will now automatically come up at boot time so you don't have to worry about mounting the filesystem manually every time you reboot your instance.

How to resize a volume

Resizing a volume can be done in a few easy steps and it will allow you to grow your volume easily as and when you require it. The steps we need to take are:

  • Increase the volume size through the Civo dashboard.
  • Login to your instance and make sure the volume isn't being written to by unmounting it.
  • Adjust the partition table if the volume is partitioned
  • Expand the filesystem on the instance
  • Remount the volume on your instance

Inrease the volume size through the Civo dashboard

Login to your dashboard and select the Volumes link. Once done click on the Resize button and select the new size you want for your volume:

Resize-Civo.png

Login to your instance and make sure the volume isn't being written to by unmounting it

Now we need to login to our instance and unmount the volume using the umount command:

root@testvol:~# umount /mnt/myfirstvol

Adjust the partition table if the volume is partitioned

Now we have unmounted the volume, we can now resize the partiton table using the growpart command:

root@testvol:~# growpart /dev/vdb 1
CHANGED: partition=1 start=2048 old: size=20895744 end=20897792 new: size=41940959,end=41943007

Now we have grown the partiton table and we are using the Ext4 filesystem, we can run a quick check on the partition using the e2fsck command and then run resize2fs:

root@testvol:~# e2fsck -f /dev/vdb1
e2fsck 1.42.13 (17-May-2015)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/vdb1: 11/654080 files (0.0% non-contiguous), 79565/2611968 blocks
root@testvol:~# resize2fs /dev/vdb1
resize2fs 1.42.13 (17-May-2015)
Resizing the filesystem on /dev/vdb1 to 5242619 (4k) blocks.
The filesystem on /dev/vdb1 is now 5242619 (4k) blocks long.

Remount the volume on your instance

Almost there! Now we have successfully increased the filesystem, we simply need to remount your volume again and check the size:

root@testvol:~# mount /dev/vdb1 /mnt/myfirstvol/
root@testvol:~# lsblk 
NAME   MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
vda    253:0    0  25G  0 disk 
└─vda1 253:1    0  25G  0 part /
vdb    253:16   0  20G  0 disk 
└─vdb1 253:17   0  20G  0 part /mnt/myfirstvol

Excellent! We have now successfully increased the volume size to 20G.

How to detach and delete a volume

If you wish to remove your volume from your instance and fully delete it, this can be done in a few small steps which are:

  • Unmount the volume from your instance
  • Remove the path from the fstab on your instance
  • Delete the volume from the Civo dashboard

Unmount the volume from your instance

Before you unmount your volume it is good practice to make sure nothing is still being written to it ( As the volume will not unmount ). SSH into your instance and once you are happy nothing is using the volume we can unmount it by using the umount command. First we need to get the mount point back for your volume which you can do a number of ways. In this example we will us lsblk

root@testvol:~# lsblk 
NAME   MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
vda    253:0    0  25G  0 disk 
└─vda1 253:1    0  25G  0 part /
vdb    253:16   0  10G  0 disk 
└─vdb1 253:17   0  10G  0 part /mnt/myfirstvol

Now we have the mount point /mnt/myfirstvol we can unmount it using the umount command:

root@testvol:~# umount /mnt/myfirstvol

The mount point will now be unmounted and we can check again with lsblk

root@testvol:~# lsblk 
NAME   MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
vda    253:0    0  25G  0 disk 
└─vda1 253:1    0  25G  0 part /
vdb    253:16   0  10G  0 disk 
└─vdb1 253:17   0  10G  0 part 

Success! As we can see there is no longer a mount point attached to the /dev/vdb1 disk.

Remove the path from the fstab on your instance

Now we have unmounted the volume we can remove it from the fstab to ensure that your instance doesn't try to bring up the volume on boot. To do this we just need to remove the entry from /etc/fstab using your favourite editor:

root@testvol:~# vim /etc/fstab

LABEL=cloudimg-rootfs   /        ext4   defaults        0 0

/dev/vdb1        /mnt/myfirstvol ext4   defaults        0 0

Ensure that when removing any entry from the fstab, that you only remove any mount points that have been added in manually. If you were to remove the default mount point, your instance would not boot up after a reboot.

Delete the volume from the Civo dashboard

Now we have done all of the steps on the instance, the final step is to delete the volume from the Civo dashboard. Simply click on the volumes link and then select the Delete volume button:

Civo-delete-vol.png

Excellent! The volume has now been deleted.