How to reduce AWS EBS root partition
Amazon Elastic Block Store (EBS) is a block storage system used to store persistent data. Amazon EBS is suitable for EC2 instances by providing highly available block level storage volumes. It has three types of volume, i.e. General Purpose (SSD), Provisioned IOPS (SSD), and Magnetic.
This Tutorial explains the detailed steps to reduce the EBS root partition with data.
Step1: Create a snap shot of the volume that need to be shrinked.
Step2: Create a new volume from the this new snapshot and name it as source-volume for convenience.
Step3: Create a new EC2 instance and make it available to connect via ssh.
Step4: Attach the source-volume to the newly created instance as /dev/xvdf
Step5: Create a new volume with size to which the source-volume want to resized and name it as target volume for convenience.
Step6: Attch the target-volume to the newly created instance as /dev/xvdg
Step7: Partition the target-volume
#parted -s -a optimal /dev/xvdg mktable msdos mkpart primary 0% 100% toggle 1 boot
Step8: Format the target-volume
#mkfs.ext4 /dev/xvdg1
Step9: Label the newly created partition
#e2label /dev/xvdg1 /
Step10: Create mount points in new EC2 instance
#mkdir -p /mnt/source /mnt/target
Step11: Mount the source volumes to EC2 instance
#mount /dev/xvdf1 /mnt/source
#mount /dev/xvdg1 /mnt/target
Step12: Copy whole files from source-volume to target-volume
#tar -C /mnt/source -c . | sudo tar -C /mnt/target -xv
Step13: Check the UUID of volumes
#blkid
/dev/xvda1: UUID="9467f4de-4231-401f-bcaa-fee718d49e85" TYPE="ext4"
/dev/xvdf1: UUID="93a54a4a-e0f5-4152-ae59-2245e8d16ee4" TYPE="ext4"
/dev/xvdg1: UUID="f0acce91-a416-474c-8a8c-43f3ed3768f9" TYPE="ext4"
Step14: Change the UUID of target-volume '/dev/xvdg' to same as that of source volume '/dev/xvdf'
#tune2fs /dev/xvdg -U 93a54a4a-e0f5-4152-ae59-2245e8d16ee4
Step15: Install boot loader in target-volume
#grub-install --root-directory=/mnt/target /dev/xvdg
Step16: Unmount volumes from target
#umount /mnt/source /mnt/target
Step17: Take a snapshot of target-volume
Step18: Create an AMI from the snapshot of target-volume
Step19: Create a new instance from newly created AMI and verify the working
Step20: Remove the source-volume, target-volume and intermediate instance that we have created
That's all...........