There used to be a time when Hard disks were very expensive and used to cost a lot of money. Nowadays they are dirt cheap and it is impervious to assume that that is the case with AWS EBS volume too. BUT, this is not true. Infact, EBS volumes are still very expensive and can become costly affairs if you don’t control your temptation to allocate space liberally.
We are humans and mistakes happen. So, You have set a 50GB space for EBS Vol and the whole program is set and running. And you suddenly see that you require no more than 15GB of space. You might be wondering, can we decrease EBS volume size? Well, It is impossible to decrease EBS volume size. If you try to decrease using AWS console you will be welcomed by :
The size of a volume can only be increased, not decreased.
Shrinking size of EBS volume
Create new volume and transfer all data from old volume to new volume.
This is the best way of resizing, it may be time consuming but trust me it holds a lot less roadblocks. Typically has 3-4 applications and its data installed. You can easily transfer your data to a new one in more than one way. For e.g.
- In the case of databases, you can create replica sets with a smaller disk size and sync all your data.
- Web-servers can easily be installed and configured again and that is much cloud preferred.
- You can reattach IP-addresses to new machines.
Assuming you do not have a choice but to make an exact replica of the root disk. Follow the given approach. It is a bit hit and miss.
Create new volume using rsync
I stuck myself with the same problem and luckily after a lot of googling and tries, I finally got it to work. Here is the summary of the steps you need to take. I haven’t tested these steps other than the Bitnami Ubuntu machine.
- Take a snapshot of root volume for backup
- Create Smaller EBS volume
- Attach the new volume to instance
- Format the new EBS volume and mount it.
- Copy data from root volume to the new volume
- Install the grub loader again
- Detach and unmount old volume
- Rename UUID and label for EBS volume
Let us assume that we have:
- Instance named my-instance in
- 100GB EBS volume size named
- We wish to decrease into 30GB and name it
We would need to shutdown the instance to prevent inconsistencies.
Snapshot the volume
You should always take snapshots before such operations as you may end up corrupting the whole root disk.To take a snapshot of the existing volume, Simply go to AWS Console -> EC2 -> Volumes -> Select Volume -> Actions(Take Snapshot)
Create a new EBS volume
- Make sure instance is shutdown
- Go to Elastic Block Store Volumes
- Click Create Volume
- Choose the same volume type same with your old volume
- Enter your desired size; For eg. 30.
- Choose availability zone; The volume will be available to instance in the same availability zone, which is
ap-south-1a. (This is very important as you need to attach this volume to machine within same AZ. )
- Add tag with Key : Name and value : EBS Small
Attach the new volume
- Right click on the new volume.
- Click Attach Volume.
- Choose instance name
- Click Attach.
We could start the instance and login to SSH. List all available volumes with
The new volume is could be one of these:
/dev/nvmeXn1 Here X could be (1, 2, 3 and so on)
we will consider
xvdf from here
Format the new volume
- Check whether the volume has any data or not using command
sudo file -s /dev/xvdf. For newer Volumes you might get NVMe storage. Rules remain the same.If it is displaying
/dev/xvdf: data, it means the volume is empty. We could format the volume.If it is displaying other than above output, it means the volume has data. DO NOT format the volume if you saw this output. This means you must have chosen another Volume.Format the volume using command
sudo mkfs -t ext4 /dev/xvdf.
Mount the new volume
- Create a directory to mount using command
sudo mkdir /mnt/new-volume.
- Mount new volume into directory using command
sudo mount /dev/xvdf /mnt/new-volume.
- Check volume with command
df -h; The new volume should be mounted now.
Copy data from old volume to the new volume
rysncto copy from old volume to the new volume
sudo rsync -axv / /mnt/new-volume/.
- Relax and wait until it’s finished. Get a coffee!
Prepare new volume
sudo grub-install --root-directory=/mnt/new-volume/ --force /dev/xvdf. Grub Install is needed for boot loading the disk. Rsync never creates a bootable disk. You need to make sure these commands run. I have seen the cases where you might have an older grub-install version installed on your Linux machine.
- Unmount new-volume
sudo umount /mnt/new-volume.
- All EBS volumes come with a unique UUID which is basically a universal identifier for your disk. It helps you identify the disk attached to your machine. We need to check the older UUID and update the older one to the new one. This will maintain system integrity. Check UUID using command
- Copy UUID from /dev/xvda1 (paste anywhere to backup this UUID); This is your old UUID.
tune2fsto replace UUID
sudo tune2fs -U COPIED_UUID /dev/xvdf; COPIED_UUID is the string value from point 4. It might want you to run
sudo efsck -f /dev/xvdffirst
- Check the system label from
sudo e2label /dev/xvda1; It will display string like
- Replace new-volume label with old-volume label using command
sudo e2label /dev/xvdf cloudimg-rootfs.
- We can logout SSH now.
Detach old volume
- Stop instance
/dev/xvdadepending upon your attachment.
- Start the instance
After we finish all steps above, we can check our instance by login into SSH.
Give it a go for two or three days. If everything works, you can safely delete the older disks and snapshots. It will save you a lot of money.
Increasing size of EBS Volume
Increasing an EBS Volume size is a relatively easy task.
- Go to EBS Volumes
- Select EBS volume you want to increase
- Go to Actions -> Modify Volume. see screenshot
- Enter the new desired size and Click Modify
After that, you need to login into the instance and check volume.
To your surprise it will still show the previous volume size. You need to resize it using command
sudo resize2fs /dev/xvda1. Here xvda is disk name which can be also
nvmeXp1 depending on your account setting. That’s all. Your EBS volume is now resized.
If your volume has a partition, run following command to grow the partition size.
$] sudo growpart /dev/xvda 1 //check whether the size has increased or not by following command $] lsblk //should output something like this nvme0n1 259:0 0 238.5G 0 disk ├─nvme0n1p1 259:1 0 512M 0 part /boot/efi └─nvme0n1p2 259:2 0 238G 0 part /
Note: if your filesystem is xfs based then you will need to execute following command
sudo xfs_growfs /dev/nvme2n1
Reducing an EBS Volumes size could be tricky and the lesson learned is always start with minimum requirement because it is much easier to expand volume than reduce.
Hope it help!