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
Swap(decided as per instance type), 100 GB for
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 : http://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 :
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
Step 3 : Delete the Boot/OS partition
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
Note : Remember to Flag same partition as boot
(parted) set boot 1 on
Step 5 : Create a partition for SWAP space
Step 6 : Create partition for /data directory
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 : http://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
Step 9 : Make & Enable SWAP partition
sudo mkswap /dev/xvda2
Enable SWAP with following command :
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
First initialize partition
sudo pvcreate /dev/xvda3
Create Volume Group :
sudo vgcreate vg_data /dev/xvda3
Create Logical Volume :
sudo lvcreate -L 102500 -n data vg_data
Create ext4 file system for logical volume :
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 email@example.com