In this article we will see how you can reduce the root disk of an EC2 instance from where the instance is booting up.

Usually, the software is not installed on the root disk. Another disk is mounted to the EC2 instance and everything is installed there. Because of this, the root disk might be too big for your needs.

VMware Training – Resources (Intense)

These are high-level steps needed to follow in order to achieve this:

  • Stop the EC2 instance
  • Create a snapshot of the original large volume
  • Create a volume based on the snapshot
  • Create the smaller volume that will be used as the new boot volume
  • Attach all volumes to the EC2 instance
  • Copy the content from the large volume to the small volume
  • Detach all the volumes from the EC2 instance and attach only the small volume using the same device
  • Start the EC2 instance

    All these steps will be explained in detail below.

So let’s get started. I have an EC2 instance running in EU-CENTRAL region:

As expected, the instance has a volume attached and the size of the volume is 30GB. The volume is attached as /dev/xvda to the EC2 instance. Also, the EC2 instance is launched in the eu-central-1b Availability Zone (AZ). This is important to remember, as all the work will be done in the same AZ:

Below are some details about the volume. As you can see I tagged (LARGE_VOL) the original volume so we can identify it later on when we will have other volumes that will come into play:

Now let’s stop the instance:

Next we need to create a snapshot of the volume used by the EC2 instance. Select the volume, right click on it and then select “Create Snapshot”:

In the next window, you need to provide the name and the description of the snapshot. Click “Create” to move further:

Now the snapshot is created and you can see it in the snapshot section of the EC2 Management console:

Now it’s time to create a volume based on the snapshot that we’ve just created. Basically we will create an identical volume with the original volume. Select the snapshot, right click on it and then click on “Create Volume”:

In the next window, you will need to provide some details. The most important is the AZ where the volume was created. Click on “Create” to create the volume:

Now, we have two volumes: the original one and the one created from the snapshot. As you can see, only the original one is in use, meaning it is attached to an EC2 instance:

Now we need to create the small volume that will be used. The volume will be empty. From the “VOLUMES” section, click on “Create Volume” and then specify the details. In this case, I chose the size to be 4GB and the AZ eu-central-1b like the other volumes. Click on “Create” to create the small volume:

We ended up with three volumes:

It’s time to attach the newly created volumes to the stopped EC2 instance. Right click on the volume created from the snapshot and select “Attach Volume”:

Select the stopped EC2 instance:

And then select the device where this volume will be attached to the EC2 instance. You can leave what is already put there. You just need to remember that /dev/sdf will be converted to /dev/xvdf, /dev/sdg will be converted to /dev/xvdg and so on. Click on “Attach”:

Do the same for the small volume and pay attention to the device used:

Now all the volumes are attached and you have the attachment information:

Start the EC2 instance:

We need to perform some work with the volumes attached to the EC2 instance. For this we need to connect to the EC2 instance:

lab@UBUNTU:~/AWS$ ssh -i LINUX_AMAZON.pem ec2-user@52.28.27.249
The authenticity of host '52.28.27.249 (52.28.27.249)' can't be established.
ECDSA key fingerprint is e2:b3:87:1f:21:4b:5c:3f:d9:1a:87:17:df:3e:d9:ee.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '52.28.27.249' (ECDSA) to the list of known hosts.

       __|  __|_  )
       _|  (     /   Amazon Linux AMI
      ___|\___|___|

https://aws.amazon.com/amazon-linux-ami/2015.03-release-notes/
25 package(s) needed for security, out of 54 available
Run "sudo yum update" to apply all updates.
[ec2-user@ip-172-31-6-137 ~]$

Let’s see the how the volumes were attached to the EC2 instance:

[ec2-user@ip-172-31-6-137 ~]$ lsblk
NAME    MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
xvda    202:0    0  30G  0 disk 
└─xvda1 202:1    0  30G  0 part 
xvdf    202:80   0  30G  0 disk 
└─xvdf1 202:81   0  30G  0 part /
xvdg    202:96   0   4G  0 disk 
[ec2-user@ip-172-31-6-137 ~]$

The EC2 instance booted from /dev/xvdf1 which is the volume created from the snapshot. /dev/xvda1 is the original volume from where we will copy the data to the smaller volume(dev/xvdg).

Make yourself root:

[ec2-user@ip-172-31-6-137 ~]$ sudo su
[root@ip-172-31-6-137 ec2-user]#

Let’s create a filesystem on /dev/xvdg:

[root@ip-172-31-6-137 ec2-user]# mkfs -t ext4 /dev/xvdg
mke2fs 1.42.12 (29-Aug-2014)
Creating filesystem with 1048576 4k blocks and 262144 inodes
Filesystem UUID: cb235e78-acfe-4867-bd0b-95d1c413eb33
Superblock backups stored on blocks:
	32768, 98304, 163840, 229376, 294912, 819200, 884736

Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

[root@ip-172-31-6-137 ec2-user]#

Now we need to create two directories where the original volume and the small volume will be mounted:

[root@ip-172-31-6-137 ec2-user]# mkdir /mnt/small_vol
[root@ip-172-31-6-137 ec2-user]# mkdir /mnt/large_vol
[root@ip-172-31-6-137 ec2-user]#

Next we will mount the volumes to the new directories created:

[root@ip-172-31-6-137 ec2-user]# mount /dev/xvdg /mnt/small_vol/
[root@ip-172-31-6-137 ec2-user]# mount /dev/xvda1 /mnt/large_vol/
[root@ip-172-31-6-137 ec2-user]#

Let’s see if they were mounted:

[root@ip-172-31-6-137 ec2-user]# mount | grep xvd
/dev/xvdf1 on / type ext4 (rw,noatime,data=ordered)
/dev/xvdg on /mnt/small_vol type ext4 (rw,relatime,data=ordered)
/dev/xvda1 on /mnt/large_vol type ext4 (rw,relatime,data=ordered)
[root@ip-172-31-6-137 ec2-user]#

Next we need to copy the data from the original volume to the small volume like this:

[root@ip-172-31-6-137 ec2-user]# rsync -aHAXxSP /mnt/large_vol/ /mnt/small_vol
sending incremental file list
./
bin/
bin/arch
       28448 100%    0.00kB/s    0:00:00 (xfer#1, to-check=1296/1320)
	   
	   
==== cut here for brevity====

sent 1065437343 bytes  received 741798 bytes  19562920.02 bytes/sec
total size is 1093776663  speedup is 1.03
[root@ip-172-31-6-137 ec2-user]#

Pay attention on the “rsync” command. The source directory (where the large volume is mounted) ends with “/” whereas the destination directory(where the small volume is mounted) does not.

Dismount the volumes previously mounted:

[root@ip-172-31-6-137 ec2-user]# umount /mnt/large_vol/
[root@ip-172-31-6-137 ec2-user]# umount /mnt/small_vol/
[root@ip-172-31-6-137 ec2-user]#

And now you can logout from the EC2 instance and stop it.

Detach all the volumes from the stopped EC2 instance:

And attach only the small volume:

You need to be careful when you choose the device. It has to be the same device used by the original volume as we saw in the beginning of the article:

Start the EC2 instance using the small volume:

And once it has booted, you can connect to it:

lab@UBUNTU:~/AWS$ ssh -i LINUX_AMAZON.pem ec2-user@52.28.48.185
The authenticity of host '52.28.48.185 (52.28.48.185)' can't be established.
ECDSA key fingerprint is e2:b3:87:1f:21:4b:5c:3f:d9:1a:87:17:df:3e:d9:ee.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '52.28.48.185' (ECDSA) to the list of known hosts.

       __|  __|_  )
       _|  (     /   Amazon Linux AMI
      ___|\___|___|

https://aws.amazon.com/amazon-linux-ami/2015.03-release-notes/
25 package(s) needed for security, out of 54 available
Run "sudo yum update" to apply all updates.
[ec2-user@ip-172-31-6-137 ~]$

And you are done. You now have a smaller root disk adapted for your needs. While the example was for Amazon Linux EC2 instances, similar procedures can be applied for other Linux distributions with small adjustments.

Reference