Shrinking AWS volumes

peterDevOps Engineer
Published:
Often times it's very very easy to extend a volume on a Linux instance in AWS, but impossible to shrink it. I wanted to contribute to the experts-exchange community a way of providing a procedure that works on an AWS instance. It can also be used on any Linux virtual machine.

DISCLAMER*** I am NOT responsible for your data loss, so please, test this out first on a test server, to verify it works for you. I apologize for any typo's in advance. I tried this out several times and it does work. ***


Note: You cannot shrink a volume, but you can replace the current volume with a smaller one.


This procedure is designed to work on AWS [Amazon EC2] Linux Ubuntu servers, but will also work on RHEL or AWS based instances.


The only gotcha is, when mounting a volume to the AWS instance differs between Ubuntu and AWS instances:

  • In Ubuntu, it's /dev/sda1
  • In AWS [rhel based instances] it's /dev/xvda


Here's the procedure, I'll add comments where appropriate:


Step 1. Create snapshot of root ebs volume [that needs shrinking] and create new volume from snapshot (let's call this volume-big)


Step 2. Create new temp instance with default ebs root volume with desired size. 


(let's call this volume-small) 


This EBS volume will have the correct partition for booting. (Creating a new EBS volume from scratch didn't work for me) So, I'm saying here, create a fresh instance, and set the desired size of the root volume to the desired size of the volume you want in step 1. Lets say you want a 30GB volume, so make it 30GB during creating time.


Step 3. Attach volume-small and volume-big to a new temp instance. 


Server-temp <this is a default 8gb volume vol-small /dev/sdf <this is the volument from step 2 vol-big   /dev/sfg  <this is the volume from step 1 


Step 4. Format volume-small. 

*Dont format volume-big as it has your data!!!! Of course, this is only a snapshot so if you do mess up and format the wrong volume, just make another snapshot from step 1 and start over. 


sudo fdisk -l     
sudo mkfs -t ext4 /dev/xvdf1 


Note: ensure partition volume is entered /dev/xvdf1 not /dev/xvdf 


check the disk To ensure that the file system is in order, run 

sudo e2fsck -f /dev/xvdf1
sudo e2fsck -f /dev/xvdg1


Step 5. Mount volume-small and volume-big 


mkdir /mnt/small mkdir /mnt/big
sudo mkdir /mnt/small
sudo mkdir /mnt/big
sudo mount /dev/xvdf1 /mnt/small
sudo mount /dev/xvdg1 /mnt/big


Step 6. Copy files


sudo rsync -aHAXxSP /mnt/big/ /mnt/small


you can also try the switch below, its untested, but wont copy over any "junk" files.

sudo rsync -aAXv --exclude={"dev/*","proc/*","sys/*","tmp/*","run/*","mnt/*","media/*","lost+found"} /old/ /new


Step 7. Ensure e2label is same as root volume *this is important


sudo e2label /dev/xvdg1 > cloudimg-rootfs
sudo e2label /dev/xvdf1 cloudimg-rootfs

*This is really important step : ) Your server will not boot with the UUID you copied over from volume-big


Step 8. Update grub.conf on volume-small to match new volume UUID


sudo blkid
/dev/xvda1: LABEL="cloudimg-rootfs" UUID="bf9a017a-931d-4191-84bc-b8434dbba527" TYPE="ext4" PARTUUID="ea059137-01"
/dev/xvdf1: LABEL="cloudimg-rootfs" UUID="7a572c53-42eb-4155-a20e-7a6436bea89e" TYPE="ext4" PARTUUID="ea059137-01"<<small vol in this example
/dev/xvdg1: LABEL="cloudimg-rootfs" UUID="3e13556e-d28d-407b-bcc6-97160eafebe1" TYPE="ext4" PARTUUID="6191704b-01"


Search and replace UUID in /boot/grub/grub.cfg


cd /mnt/small/boot/grub
sudo vi grub.cfg
:%s/foo/bar/g
:%s/<search_string>/<replace_string>/g
mine looked like the one below......look for the "/" after e1/   7 is the start of the new uuid
:%s/3e13556e-d28d-407b-bcc6-97160eafebe1/7a572c53-42eb-4155-a20e-7a6436bea89e/g


Check your work


cat grub.cfg |more


Step 9. Shutdown server and detach the small volume

This volume is now at your desired "shrunken" size and can be attached and booted on a new Ubuntu instance

Step 10. Attach new resized ebs small volume to instance at /dev/sda1

  • Create a new instance with the default volume. 
  • Boot it. 
  • Shut it down
  • detach its volume 
  • attach volume-small at /dev/sda1
  • Boot instance! 


DONE : )


1
1,231 Views
peterDevOps Engineer

Comments (0)

Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.