Create separate /var partition on EBS volume AWS

separate-var-partition-on-ebs-volume-aws

I guess this is the first article on the internet that provides full proof solution for creating a separate /var partition AWS. I’ve gone through a lot of articles in-order to trouble shoot problem of creating separate /var partition on EBS volume AWS which is …

The problem! : Even if you try partitioning given any way you find on the internet, it won’t succeed, after a reboot of the instance you won’t be able to login into the instance as it will refuse the connection which indirectly means you’ve screwed up the instance and the only way to connect to it!

I would suggest to strictly follow below steps as it is in-order to create separate /var partition on EBS volume…

Step 1: Create the volume in AWS Console and attach it to the instance. (Assuming you know this already!)

Step 2: By default, it is always mounted on /dev/xvdf, please create the partition using fdisk and confirm the lsblk output, it should look like below:

$ sudo fdisk /dev/xvdf


(Use options N to create a new partition and all the defaults for creating 1 full partition for entire volume and option W to write the partition in the filesystem)


NAME    MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
xvda    202:0    0    8G  0 disk
└─xvda1 202:1    0    8G  0 part /
xvdf    202:80   0  20G  0 disk
└─xvdf1 202:81   0  20G  0 part


*All the work ahead would be done on this xvdf1 partition, make sure you are NOT using /dev/xvdf anywhere.


Step 3 : Format the below partition using

$ sudo mkfs -t ext4 /dev/xvdf1

Step 4 : Confirm default context type for /var
$ ls -Zd /var
drwxr-xr-x. root root system_u:object_r:var_t:s0  /var

* Note the context type “var_t”

Step 5 : Mount the formatted partition on /mnt
$ mount /dev/xvdf1 /mnt

Step 6 : Copy over data from /var to /mnt
$ sudo shopt -s dotglob
$ sudo rsync -aulvXpogtr /var/* /mnt


Step 7 : Confirmed the SELinux context on /mnt
$ ls -Zd /mnt
drwxr-xr-x. root root system_u:object_r:file_t:s0 mnt

* Note the context type “file_t”

Step 8 : Change context type on /mnt to match /var
$ sudo chcon -t var_t /mnt

Step 9 : Check context on /mnt
ls -Zd /mnt
drwxr-xr-x. root root system_u:object_r:var_t:s0 /mnt


Step 10 : Umount from /mnt

$ umount /mnt

Step 11 : Make the entry in fstab as below:
/dev/xvdf1   /var       ext4    defaults,noatime,nofail 0   2

Step 12 : Change the /var to a new location and create another /var directory and mount the new volume:
$ sudo mv /var/ /var.old
$ sudo mkdir /var


Step 13 : This time use the mount -a in verbose mode as below:
$ sudo mount -av
* Make sure you are not getting any error message, if there is some inconsistency it will throw an error message, if so, please reply back with the error message.

Step 14 : Reboot and Check!

 

That’s it you will be able to SSH onto instance successfully!

Finally, you have a separate /var partition on EBS volume AWS

You can move or create many different separate partitions just by following simple steps.

I hope this article was helpful, Please comment below your views and share!

You can also write to me at mail@bhargavamin.com

Thank you for reading

-Bhargav

Blogger & Assc Cloud Architect

Site Footer