Creating and attaching external volumes is one of those things in administration that is really nice to know how to do but for me is also one that doesn’t happen every day so it is really easy to forget how to do which makes it a little bit more painful, especially with deadlines and people watching over your shoulder. So, having said that, I think it is probably worth writing a post about how to do it because it happens just enough that I have trouble getting everything straight, and I’m sure otheres run in to this as well, so that’s what I will be writing about today.
There is good documentation for how to do this but there are a lot of separate steps so consolidating the components might be helpful to readers who stumble across this. I’m sure there are other ways to accomplish this but I don’t think it is necessary to cover everything here.
Create your “floating volume”
This step is straight forward. In the AWS EC2 console choose the type of volume this will be (SSD or magnetic), availability zone, and any other options here.
After your volume has been created you will want to attach it to an instance. This part is important because the changes could break your OS volume if you write to your fstab file incorrectly. In this example I am choosing to attach the EBS volume as /dev/xvdf, but you could name it differently if it corresponds to your setup.
After the volume has been mounted you can check that it has been picked up by the OS by either checking the /dev directory or by running fdisk -l and looking for the size of the disk you just attached.
It is worth pointing out that all of the steps in the AWS console can alternatively be done with the aws-cli tool. It is probably easier but for the sake of time and illustration I am leaving those steps out. Feel free to reach out if you are interested in the cli tool and I can update this post.
If you run fdisk -l you will notice that the device is empty, so you will need to format the disk. In this instance I am formatting the disk as ext4. So use the following command to format it.
sudo mkfs.ext4 /dev/xvdf
After the volume has been formatted you can mount it to your OS.
Attaching the volume
sudo mkdir /data sudo mount -t ext4 /dev/xvdf /data
If you need to resize the filesystem for whatever reason, you can use the resize2fs command.
sudo resize2fs <mount point> sudo resize2fs /dev/xvdf
Here you will create the directory (if it doesn’t already exist) to mount the volume to and then mount it. At this point it would be fine to be done if you just needed temporary access to the storage on this device. But if you want your mount to persist and to survive a reboot then you just add an entry to your /etc/fstab file to make sure the /data directory gets the volume mounted to it after a reboot. Something like the following would work.
/dev/xvdf /data ext4 defaults,nobootwait 0 0
The entry is pretty easy to follow but may be confusing for those who are not familiar with how fstab works. I will break down the various components here.
The first parameter is the location of the volume (/dev/xvdf) and is referred to as the file_system field.
The second parameter specifies where to mount the volume to (/data) and is referred to as the dir field.
The third field is the type. This is where you specify the file system type or device to be mounted. If you didn’t format this volume previously it would crate problems for OS when it tried to load in your volume from this file.
The fourth section is the options for the mount. Here, the defaults,nobootwait section is very important. If you don’t have the nobootwait option specified here then your OS could potentially hang on boot up if it couldn’t find the specified volume, so this option helps escape it if there are any problems.
The fifth field is to either enable or disable the dump option. Unless you are familiar with or use the dump command you will almost always set this to 0.
The last section is the pass section. This simply tells the OS if it should run an fsck or not on this volume. Here I have it set to 0 so it doesn’t get checked but for OS volumes, this could be important to turn on.
There are many more things you can do with fstab so if you are interested in other options for how to mount volumes you can look at the fstab documentation for more insights and information.
If you ever wanted to float this volume to another host it would be easy to do, and would not require any new or special formatting since this was already taken care of here. The steps would looks similar to the following.
- Unmount volume from current OS
- Remove entry in /etc/fstab for volume mount
- Detach mount in AWS console from current OS
- Attach mount to new OS
- Mount volume manualy in new OS to test if it works
- If the mount works add a new entry in /etc/fstab
So that’s pretty much it. Hopefully this is useful for everybody.