04 Jun
04Jun

If you search the latest way to decrease / reduce your ebs volume and find only old guides that no longer work or aws official docs says it is not supported or find your instance none bootable after you have tried to reduce the volume then this guide is for you.


I test it working on Ubuntu 22.04 so you can probably port it to any Linux based OS

the old way to reduce the AWS EBS volume used to work for me ok but since the bios changed on the latest EC2 instances to UEFI from Basic Input/Output System,  this task of reducing the root volume size now requires different method then what you'll find in the old guides, it took me while to figure it out so i'm happy to share it

i'll start saying the obvious use it at your own risk make sure you backup your data and take snapshot before you start, making changes to your root volume and to the Bootloader can cause your instance to become none bootable none usable.

Lets start with the following steps:

1) create new Ec2 instance with same OS version similar to the instance you use, in the same availability zone(lsb_release -a to check your OS on Ubuntu), but create it with a smaller volume size that you need (best if same instance type but not a must)


2) stop the new instance and detach the volume


3) attache the previous old volume as default root /dev/sda1 and new volume as /dev/sdf


4) start the instance and mount the new volume (go into privilege mode (sudo -i))

run lsblk to make sure your new volume name is "nvme1n1p1" and "nvme1n1p15"( use for /boot/efi), else adjust the below commands with the correct naming of the new volume.

 mkdir -p /new-volume

mkdir -p /new-boot

mount /dev/nvme1n1p1 /new-volume/

mount /dev/nvme1n1p15 /new-boot/


5)backup the grub files from the new volume

cp /new-volume/boot/grub/grub.cfg /var/tmp/grub.cfg

cp /new-boot/EFI/ubuntu/grub.cfg /var/tmp/grub-efi.cfg


6)delete the new (small)volume:  rm -rf /new-volume/*


7)copy the old volume data to the new volume(make sure you stop the services you run before copying to avoid file locks):

rsync -axv / /new-volume/


8)restore the grub files 

cp /var/tmp/grub.cfg /new-volume/boot/grub/grub.cfg

cp /var/tmp/grub-efi.cfg /new-boot/EFI/ubuntu/grub.cfg


9)make sure that the restore grub match the UUID and PARTUUID of the new volume:

run:  blkid

 and write down the old volume and the new volume UUID and PARTUUID numbers.

for example:

OLD VOLUME - /dev/nvme0n1p1 UUID="5d7cb414-1c8e-40a3-b39e-56b0e51cb4d9" BLOCK_SIZE="4096" TYPE="ext4" PARTUUID="c5e56b64-ecde-4212-9c4a-638e7f21b7fc"

NEW  VOLUME- /dev/nvme1n1p1: LABEL="cloudimg-rootfs" UUID="3e31f17d-926c-4e2f-9e03-080e614e50b1" BLOCK_SIZE="4096" TYPE="ext4" PARTUUID="8d1d56fb-f3bf-486c-9d6b-7b7c0bb9c4f5"

check if the PARTUUID of the new volume are in the /new-boot/EFI/ubuntu/grub.cfg:

grep PARTUUID /new-boot/EFI/ubuntu/grub.cfg  if PARTUUID is wrong search replace them

sed -i 's/old-PARTUUID/new-PARTUUID/g' /new-boot/EFI/ubuntu/grub.cfg

do the same for /new-volume/boot/grub/grub.cfg


10)stop the instance detach both volumes attach the new smaller volume to the instance on /dev/sda1

  

good luck, leave comments if it works for you :-)




Comments
* The email will not be published on the website.