AWSLinux

Move /var on an AWS EC2 Instance

By June 29, 2018 No Comments

So you’ve decided to try out Amazon Web Services finally.  Easy enough, in a few clicks you register, setup billing then dive into the EC2 server setup.  Your first instinct when you reach the hard drive settings might be to increase the 8gig or 20gig default to the size of drive you’ll need.  Most other hosts you’ve used in the past keep all the data on the drive so why not?

Early on you won’t notice the difference, but if you ever reach a point where you need to expand, update, move or change the drive you’ll run into a rude awakening.  You can follow the options below, but depending on your amount of data it could prove troublesome.

If I’ve learned anything it’s to take the time to do things right the first time so you don’t end up with these problems later on.  It will cost you more time, money and clients.

Now on to the tutorial of how to create a separate /var partition on an AWS EBS volume.  During your setup of the EC2 instance you can choose to add an additional volume.  In doing this the drive has been setup but it is not formatted or mounted to be used with your EC2 instance by default.  Another option is adding another volume later on after you’ve setup the EC2 instance, this walkthrough will work for both.

Go ahead and connect to your instance and run the following command:

$lsblk

With the output of this you can identify your new volume you’d like to mount.  Once you have identified the volume use it to run this command.  The output will not show the /dev/ part so be sure to add it.

$ sudo fdisk /dev/xvdf

Next you’ll format the drive with the desired file system, most likely ext.

sudo mkfs -t ext4 /dev/xvdf1

Now you’ll need to mount the formatted partition.

$ mount /dev/xvdf1 /mnt

Copy over your data from the existing drive to a temporary location.

$ sudo shopt -s dotglob
$ sudo rsync -aulvXpogtr /var/* /mnt

Now lets unmount the temporary partition.

$ umount /mnt

Open up fstab at /etc/fstab using you’re preferred editor, I use vi like so.

vi /etc/fstab

Add this line and save.

/dev/xvdf1   /var       ext4    defaults,noatime,nofail 0   2

Now lets copy the existing /var path just in case we need it later.

$ sudo mv /var/ /var.old
$ sudo mkdir /var

Lastly lets mount our new setup.

$ sudo mount -av

Reboot your server and verify it’s working, you should be all set!

Leave a Reply