Scenario:

In AWS Solution Architect Associate Exam, you may be asked a question related to EC2 resizing. By default size of EBS volumes can be increased, however, there are situations where a client signs up for the free tier but accidentally uses 100GB for the EBS volume. Recently as a part of AWS Managed service program which SupportSages offers, we had a requirement to downsize the EBS volume to 30GB which is the HDD/EBS Volume size offered under free tier.

It was a bit tougher than we expected and hence I decided to document the resizing of the EBS volume so that it will be helpful for others with similar needs.

Scenario:

We have an EC2 Instance with a 50 GB EBS attached to it as the Root device. Let us call this instance `RszSrcInst` and the 50 GB EBS volume `RszSrcVol`. We are going to resize this EBS volume from 50 GB to 8 GB.

For this purpose, we need a new, small temporary instance and let's call it `NewRszInst`. In addition, we also need an EBS volume that is the size we are going to shrink the source, i.e, 8 GB. Let's Call this new EBS volume `NewVol`.

We will take a snapshot of our source volume just to be on the safe side. For this purpose, we are going to simply stop our source instance `RszSrcInst` and take the snapshot of `RszSrcVol`.

Now that we are all set, let's begin.

Step 1:

Detach the source EBS volume `RszSrcVol` from our source instance `RszSrcInst`.

Step 2:

Attach `RszSrcVol` to our new instance `NewRszInst` as /dev/sdf ( this will become /dev/xvdf ).

Step 3:

We will attach the new volume `NewVol` to `NewRszInst` as /dev/sdg ( this will become /dev/xvdg ).

Step 4:

Go ahead and start the new instance `NewRszInst`.

Step 5:

SSH into it and switch as the Root user.

$ sudo su -
# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 488M 56K 488M 1% /dev
tmpfs 497M 0 497M 0% /dev/shm
/dev/xvda1 50G 981M 49G 2% /

Step 6:

We will now force check whether the file system in the old volume is okay. So run the command `e2fsck -f /dev/xvdf1` ( 1 is the partition number and if you are resizing a different partition, then change the number accordingly ).

# e2fsck -f /dev/xvdf1
e2fsck 1.42.12 (29-Aug-2014)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/: 36608/3276800 files (0.1% non-contiguous), 489400/13106683 blocks

Step 7:

Resize the file system in the old volume using the command `resize2fs -M -p /dev/xvdf1`. The option `-M` is used to shrink the file system to the minimum size and the option `-p` will display the progress in percentage.

# resize2fs -M -p /dev/xvdf1
resize2fs 1.42.12 (29-Aug-2014)
Resizing the filesystem on /dev/xvdf1 to 416277 (4k) blocks.
Begin pass 2 (max = 125405)
Relocating blocks XXXXXXXXXXXXXXXXXXXXXX----XXXXXXXXXXXXXXXXXX
Begin pass 3 (max = 400)
Scanning inode table XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Begin pass 4 (max = 4169)
Updating inode references XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
The filesystem on /dev/xvdf1 is now 416277 (4k) blocks long.

Step 8:

Take note how many 4k blocks the file system is. You can get it from the last line of the last command output. In our case, the file system in the old volume is 416277 4k blocks long. We will now convert this to MB using the formula (( number of 4k blocks )*4 ) / 1024. In our case, it will be about 1627. Finally, round this number by adding a few MB to it to make it 1700.

Step 9:

Now we are going to copy using the above size from device to device. Please note that we are not copying from partition to partition. Copying from device to device will allow us to copy the partition table and the boot data in the old volume.

# dd if=/dev/xvdf of=/dev/xvdg bs=1M count=1700
1700+0 records in
1700+0 records out

Step 10:

Now that the copying is complete, we will have to fix the GPT partition table to make it work with the new size. For this purpose, we will use the `gdisk` tool to do the following.

# gdisk /dev/xvdg
GPT fdisk (gdisk) version 0.8.10

Warning! Disk size is smaller than the main header indicates! Loading secondary header from the last sector of the disk! You should use 'v' to verify disk integrity, and perhaps options on the experts' menu to repair the disk.

Caution: invalid backup GPT header, but valid main header; regenerating backup header from main header.

Warning! One or more CRCs don't match. You should repair the disk!

Partition table scan:
MBR: protective
BSD: not present
APM: not present
GPT: damaged

****************************************************************************
Caution: Found protective or hybrid MBR and corrupt GPT. Using GPT, but disk verification and recovery are STRONGLY recommended.
****************************************************************************

Command (? for help):

This is the prompt for the keyboard input. To be safe, we will backup the current GPT data structures first to the end of the disk. For this, use the following keys.

  • x: Enter the experts' menu.
  • e: To backup the current GPT data structures to the end of the disk.
  • m: Return to main menu.
Command (? for help): xExpert command (? for help): e
Relocating backup data structures to the end of the disk

Expert command (? for help): m

Now the steps to fix the partition table.

Step A:

First, we will display the details of the current partition. We are going to need it later. Use the following keys for this purpose.

  • i: Show detailed partition information.
  • 1: The partition number.
Command (? for help): i
Partition number (1-128): 1
Partition GUID code: 0FC63DAF-8483-4772-8E79-3D69D8477DE4 (Linux filesystem)
Partition unique GUID: 03F92EF3-9780-4F04-B56E-148249252A95
First sector: 4096 (at 2.0 MiB)
Last sector: 104857566 (at 50.0 GiB)
Partition size: 104853471 sectors (50.0 GiB)
Attribute flags: 0000000000000000
Partition name: 'Linux'

Note the Partition name and Partition unique GUID. We will need it later.

Step B:

Now we are going to delete the current partition using the following inputs.

  • d: Delete a partition.
  • 1: The partition number.
Command (? for help): d
Partition number (1-128): 1
Step C:

We have now deleted the old partition, so, we will create a new one. Use the following inputs for this purpose.

  • n: Create a new partition.
  • 1: The partition number.

Use the default values by pressing `Enter` for the next 3 input prompts.

Command (? for help): n
Partition number (1-128, default 1): 1
First sector (34-16777182, default = 4096) or {+-}size{KMGTP}:
Last sector (4096-16777182, default = 16777182) or {+-}size{KMGTP}:
Current type is 'Linux filesystem'
Hex code or GUID (L to show codes, Enter = 8300):
Changed type of partition to 'Linux filesystem'
Step D:

As you can see the name of the new partition is `Linux filesystem`. We will now change this to match our old partition's name. Use the following keys to change the current partition's name.

  • c: Change the GPT name of a partition.
  • 1: The partition number.
  • Enter name : Linux ( The parition name displayed in the old partition details.)
Command (? for help): c
Partition number (1-128): 1
Enter name: Linux
Step E:

Now the new partition name matches the old one. However, the GUID of the new and old partitions are still different. We are going to fix that now. Use the following keys for this.

  • x: Enter the experts' menu.
  • c: Change partition GUID.
  • Enter the partition's new unique GUID ('R' to randomize): 03F92EF3-9780-4F04-B56E-148249252A95 (The `Partition unique GUID` displayed in the old partition details.)
Command (? for help): xExpert command (? for help): c
Partition number (1-128): 1
Enter the partition's new unique GUID ('R' to randomize): 03F92EF3-9780-4F04-B56E-148249252A95
New GUID is 03F92EF3-9780-4F04-B56E-148249252A951
Step F :

Everything should be okay now. We will print the new partition details by going back to the main menu. Use the following inputs.

  • m: Return to the main menu.
  • i: Show detailed partition information.
  • 1: The partition number.
Expert command (? for help): mCommand (? for help): i
Partition number (1-128): 1
Partition GUID code: 0FC63DAF-8483-4772-8E79-3D69D8477DE4 (Linux filesystem)
Partition unique GUID: 03F92EF3-9780-4F04-B56E-148249252A95
First sector: 4096 (at 2.0 MiB)
Last sector: 16777182 (at 8.0 GiB)
Partition size: 16773087 sectors (8.0 GiB)
Attribute flags: 0000000000000000
Partition name: 'Linux'

Notice how the Partition size that is shown here is different from the one in the old partition details.

Step G :

Now that we have corrected the partition table, we can write it to the disk to finalize the change. Use the following inputs.

  • w: Write table to disk and exit.
  • y: The `yes` prompt to proceed with the overwrite.
Command (? for help): Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING PARTITIONS!!

Do you want to proceed? (Y/N): y
OK; writing new GUID partition table (GPT) to /dev/xvdg.
The operation has completed successfully.

Step 11:

Now we will resize the file system in the new volume using the `resize2fs` command to shrink it to its minimum size.

# e2fsck -f /dev/xvdg1
resize2fs 1.42.12 (29-Aug-2014)
Resizing the filesystem on /dev/xvdg1 to 2096635 (4k) blocks.
The filesystem on /dev/xvdg1 is now 2096635 (4k) blocks long.

Step 12:

Stop the instance `NewRszInst` and detach the volume `NewVol` from it.

Step 13:

Attach the volume `NewVol` to our source instance `RszSrcInst` as the Root device. You can attach it as Root device by adding it as /dev/xvda.

Step 14:

Start the instance `RszSrcInst` and SSH into it and verify the file system disk space.

$ df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 488M 56K 488M 1% /dev
tmpfs 497M 0 497M 0% /dev/shm
/dev/xvda1 7.8G 973M 6.8G 13% /

Step 15:

Terminate the instance `NewRszInst` and delete the volume `RszSrcVol`.

That's it. We have successfully resized an EBS Volume from 50 GB to 8 GB without any issues..

Afsal

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam ultrices faucibus metus porttitor euismod. Integer dapibus gravida quam, et suscipit felis pharetra vitae. In hac habitasse platea dictumst. Suspendisse potenti. Vivamus iaculis ornare venenatis.

You may also like

Comments (3)

  • Jhonathon Doe
  • Posted on 29 Jun, 2017

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.

  • Jhonathon Doe
  • Posted on 29 Jun, 2017

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.

  • Jhonathon Doe
  • Posted on 29 Jun, 2017

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.

Leave Us A Comment