Create ROOT SWAP and LVM partition on EBS volume (AWS)

root-swap-lvm-partition-banner

In this article I will show how to Create ROOT SWAP and LVM partition on EBS volume

Usecase scenario : A customer wants to partition their one EBS volume into multiple logical partition hence we will create ROOT SWAP and LVM partition on EBS volume.




Lets say they have 150 GB EBS volume which they want to logically divide into 3 partitions of 40GB Root/OS, 10GB Swap(decided as per instance type), 100 GB for /data.

partition-division

Now normally we would go ahead and create partitions using parted or any other tool but in AWS specifically you cannot partition a root volume when it in use so for this you will have to stop the instance and detach the volume, attach same volume to a already running instance as a secondary partition. Dont’t mount it as you cannot work on mounted volume.

In-order to do this please follow steps shown in this article to switch EBS volume to : https://bhargavamin.com/how-to-do/steps-move-ebs-volume-ec2-instances/

Once you’ve done that your good to perform partitioning on EBS volume.

Step 1 :  Display the Block Device Information :

$ lsblk
lsblk-nat-instance
Output will be like this, showing one root partion of 6GB for my RHEL instance

This will display the size and the volume you’ve have recently attached, make sure you know the partition name properly before performing any task.

Step 2 : Open Parted for block device you want to partition

$ sudo parted /dev/xvdp
sudo-parted-xvdp-print
Supply print command to display partition on block device

Step 3 : Delete the Boot/OS partition

rm-1-partition
rm 1 will delete the partition listed as number 1

Step 4 : Create a new partition as a Boot or OS partition of 40Gb

First the partition was limited to 6GB which we deleted above, Now we will make new partition with extended size of 40GB

mkpart-41gb-boot-partition
Follow the steps to create partition for boot

Note : Remember to Flag same partition as boot

(parted) set boot 1 on

Step 5 : Create a partition for SWAP space

10gb-swap-partition

Step 6 : Create partition for /data directory

110gb-partition-data

 

Step 7 : Once all the partitions are done, now your good to attach volume back to the original instance.

Follow the same steps in article : https://bhargavamin.com/how-to-do/steps-move-ebs-volume-ec2-instances/

To move back EBS volume back to original instance.

Once you’ve have attach volume back to original instance follow below mentioned steps to perform LVM, SWAP on EBS volume.

Step 8 : Perform check and list the block information of root volume using lsblk command

$ lsblk

test1-lsblk
Output will be something like this for lsblk command where you will see 3 patitions we made before

Step 9 : Make & Enable SWAP partition 

test1-mkswap

sudo mkswap /dev/xvda2

Enable SWAP with following command :

test1-swapon

sudo swapon /dev/xvda2

Add entry to fstab for SWAP :

sudo nano /etc/fstab

Copy paste this line at end in fstab :

/dev/xvda2 none swap sw 0 0

 

Step 10 :  Create LVM partition for /data

First initialize partition /dev/xvda3 :

test1-pvcreate

sudo pvcreate /dev/xvda3

Create Volume Group :

test1-vgcreate

sudo vgcreate vg_data /dev/xvda3

Create Logical Volume :

test1-lvcreate-102-5GB
Note : Mention the space in MB
sudo lvcreate -L 102500 -n data vg_data

Create ext4 file system for logical volume :

test1-mkfs-ext4
Output should look like this
sudo mkfs.ext4 /dev/vg_data/data

Create a directory to be mapped to logical volume :

sudo mkdir /home/xyzuser/data

Edit fstab file :

sudo nano /etc/fstab

Copy paste this line in fstab file:

/dev/vg_data/data data ext4 defaults 0 0

Step 11 : Mount all the entries in fstab 

sudo mount -a

 

That’s it you successfully create ROOT SWAP and LVM partition on EBS volume on AWS.

If you have any queries or error, comment below or mail me at contact@bhargavamin.com

-Bhargav

 

Blogger & Assc Cloud Architect

Site Footer