Advanced guide: How to add disks to your virtual machines

ozgurCity CloudLeave a Comment

City CloudEvery virtual machine in City Cloud comes packed with a default disk of 20 GB, which is sufficient for most situations. Small database servers, web servers, backup or personal repositories.

In some cases, you might need more. Thanks to virtualization, the process of adding one or more disks has become simpler.

Here we show you a straightforward guide on how to do this.

Preparations

First of all, you need a working virtual server. You can add disks at the moment of creation or later on, and since the setup process is exactly the same, you don’t need to worry about creating new virtual machines if you have one or more already.

Disks as the rest of the resources such as memory and cpu, are virtualized. Ultimately that means that you don’t have to worry where exactly your disks are, we take care of that for you. You can also create as many disks as you want, the main difference is the increased cost which as you can see here, is still cheap.

A couple of notes before we continue. We are writing this guide for GNU-Linux Debian Squeeze 64-bit (6.0.2.1), so the tools used might be different according to the distribution of your choosing. This guide might also work, albeit not tested, with Ubuntu since it’s Debian based. If you have trouble setting things up, just contact us with the distribution your are using and we’ll give you a couple of hints of which tool to use.

Let’s get started.

Adding a disk

There are two main ways of adding a disk. During server creation or to a current one.

Let’s suppose you are creating a new virtual machine, this is your usual first step:

Add disk - new vm

As you can see, you have a “Additional hard disk” option where you can select the size. This basically tells the system that we want a new empty hard disk attached to the server, while the default one (with 20 GB of space) will contain the operating system.

The other way is similar, but requires that you turn off the server. This is important to understand, because we need to account for downtime. All in all, the process is pretty quick so the downtime will be minimal.

Go ahead and turn your server off like you usually would. Go to the “Powered-off VMs” section and locate your server, then click on “Details” (the last icon on the server row). There you have a tab called “Disk”, which should lead you to:

Add disk - disk tab

Just click on “New Disk” and select the size, varying from 10 GB to a whopping 3 TB of pure data. Next, click on “Create” and then “Save”.

Wonderful! You just added a new disk to your virtual server. Similarly you can add more or even, treading lightly, delete current ones.

Turn on the server and it’s now time to set things up.

Setting up

Connect to your server through SSH, with root or a user with administrative privileges. What we are going to do now is basically partition the disk, format it, mount it and tell Linux to enable it for next boot.

Let’s partition the freshly created disk with the excellent tool cfdisk (already installed on our Debian GNU/Linux image).

cfdisk /dev/vdb

Add disk - cfdisk

You should see a screen similar to the one above, telling us that we have a disk of a certain size and ready to be partitioned. Hit “New” and then select “83” (Linux) as the partition type. Once we are done, hit “Write”, confirm with “Yes” and then quit the application.

This process can be repeated several times, according to your needs. In our example, we are creating just one partition occupying the entire disk.

Also important, we are using the disk “/dev/vdb” since it’s the first one after the default one, if you already did this before it might be “/dev/vdc” or “/dev/vdd”.

Now, we need to give the disk a proper format so the operating system can use it.

mkfs.ext4 /dev/vdb1

This will take a couple of minutes (maybe less, depending on the size of the disk). Please, remember to do this to all of the partitions you created. Replace /dev/vdb1 with the corresponding file descriptor (i.e. /dev/vdb2, /dev/vdb3, /dev/vdc1, etc). We are also using “ext4” as the file system, but you can choose whatever you like and feel comfortable with, examples of these are ext2, ext3, minix, reiserFS, etc.

Great so far, now we are going to create a folder where the disk will be mounted to. In our case, we are choosing “/myfiles” which, as we mentioned, is an example and you could use whatever you see fit.

mkdir /myfiles

To the folder above you can additionally set other permissions (with the chown or chmod commands).

Before we continue, we need to get the identification string of the disk in question. Just execute the following:

blkid

Copy & paste the line that matches your disk, and save it for later.

Now, let’s edit the disks configuration file with our favorite text editor (we like Vim).

vim /etc/fstab

Add disk - fstab

As you can see, here you have all the disks setup. We are going to tell Linux to mount this new disk at boot time, where and put a couple of directives.

Remember the ID of the server? Bring that up and add the following line at the bottom:

UUID=e6fcad12-20fc-4010-9ad7-034093cae939    /myfiles        ext4         errors=remount-ro          0            0

Of course, you should replace the UUID string with the one from your disk. Most of this line is self explanatory, the first parameter tells us which disk we are talking about (alternatively you could use something in the form /dev/deviceX), the second one is the mount point, the third one is the type of file system and the rest are parameters. In this case, for instance, we are telling the operating system that if there are errors found, the disk should remount itself as read only, to ensure no further writes could damage the integrity of the data.

Save the file and if everything went fine, that should be all. If you have encountered any problems along the way, just check the parameters on each case, a wrong ID or a different partition type could mean there is no mounted disk.

You can also check the disk before you restart the system, using:

mount –t ext4 /dev/vdb1 /myfiles

If that command executed successfully, it means that the disk is working just fine. Common problems have to do with the misconfiguration of the fstab file.

We are good to go!

Congratulations! You have now a new disk ready to be filled with files. This is a great way to expand your server capacity and as you can see, the process is painless.

Remember that this is just the beginning of what you can do by adding disks. The rest is up to you.

In the future we are going to add more guides that have to do with added disks. For instance, have you ever wondered how to move a /home partition to another disk (a common problem when your needs grow) or how to move a MySQL database?.

Check our blog regularly and find out.