DEV Community

desawsume
desawsume

Posted on

How to shrink the EBS Volume

## TL;DR

  1. Stop the target EC2 instance (Target machine is Windows server 2019)
  2. Snapshot the root EBS volume.
  3. Create a brand new EBS volume and make sure it is at the same AZ.
  4. Detach the source volume from the EC2 and attach it as non-root volume to the worker EC2 below
  5. Launch a new EC2 worker instance in the same availability zone as the target instance. Here I launched a small Amazon Linux 2 box to do this demo

Main steps

Fetch the epel to install ntfsprogs

amazon-linux-extras install epel -y

yum install ntfsprogs

List the attached volume

fdisk -l
Note down the target volume device name

source volume - /dev/xvdf (The original volume)

target volume - /dev/xvdg (The smaller size of the EBS volume)

Dump current source disk partition info to a file

sfdisk -d /dev/xvdf > xvdf.info

Find out the minimum size the filesystem can be reduced to

ntfsresize --info /dev/xvdf

ntfsresize --info /dev/xvdf1

Resize the filesystem to the suggested number. Enter ‘y’ when prompted to proceed.

In this example it is 30Gb
ntfsresize -s 30000M /dev/xvdf

Install pv, which is a tool for monitoring the progress of data through a pipeline.

yum install pv

Find out the total MB we need to copy from the source disk to the new smaller disk. Here we need to cover the reserved sectors (2048 sectors, or 1MB, from step 6 above) at the front of the disk, to the end of the filesystem.

echo $((30000*2048))

alway + 1 of the total of your drive

Block copy from xvdf to xvdg, with 1MB block size and total count
dd bs=1M if=/dev/xvdf count=30001 | pv -s 30001m | dd of=/dev/xvdg bs=1M

Dump the new target disk xvdg partition info to a file. Edit the file by updating the xvdg1 size to the new size in sector

sfdisk -d /dev/xvdg > sfdisk-d.xvdg

Detach the xvdg and attach it as root back to the original instance

Wait for few mins and check!

Top comments (0)