Shrinking AWS EC2 root EBS volume
When you wish to shrink an EBS root volume, you will need to start a new, small EC2 instance that you can attach the volume you wish to resize. A t2.micro instance should be more than sufficient for this task. Once you have this instance created, proceed with the following steps.
- Take a backup
- Create a new EBS volume that is the size you wish to shrink to, do this by creating a new t2.micro EC2 instance...ensure delete on termination is not enabled. After instance is created you will have an EBS volume that is partitioned and has valid boot partition. Terminate the temporary instance.
- Detach the volume you wish to resize from the current EC2 instance and attach both volumes to the new, small EC2 instance you created
- Mount the old volume as /dev/sdf (this becomes /dev/xvdf)
- Mount the new volume as /dev/sdg (this becomes /dev/xvdg)
- Power on the new, small instance and wait for it to come online
- SSH into the instance and run the following commands
- To ensure that the file system is in order, run
sudo e2fsck -f /dev/xvdf1
. If you're resizing a different partition on the drive, change the number 1 to the partition number you wish to resize. - If the e2fsck command ran without errors, now run
sudo resize2fs -M -p /dev/xvdf1
. Again, change the 1 to the partition number you wish to resize if you're not resizing the first one. - The last line from the resize2fs command should tell you how many 4k blocks the filesystem now is. To calculate the number of 16MB blocks you need, use the following formula: blockcount * 4 / (16 * 1024). Round this number up to give yourself a little buffer.
- Execute the following command, using the number you came up with in the previous step.
sudo dd bs=16M if=/dev/xvdf1 of=/dev/xvdg1 count=numberfrompreviousstep
. Depending on how large your volume is this may take several minutes to run -- let it finish. - After the copy finishes, resize and check and make sure that everything is in order with the new filesystem by running
sudo resize2fs -p /dev/xvdg1
followed bysudo e2fsck -f /dev/xvdg1
. - After this step is complete, detach both volumes from the new instance you created. Attach the shrunken volume to the old EC2 instance as /dev/xvda1 (your boot device) and restart your old instance. Save the previous, larger volume until you've validated that everything is working properly. When you've verified things are working well, feel free to delete the new EC2 instance you created, plus the larger volume and snapshot.
Image Your Hard Drive using dd, for partition table and MBR see here
- Boot from the live linux distribution.
- Switch to root.
- Make sure NO partitions are mounted from the source hard drive.
- Mount the external HD.
# mount -t vfat /dev/sda1 /mnt/sda1
- Backup the drive.
# dd if=/dev/hda conv=sync,noerror bs=64K | gzip -c > /mnt/sda1/hda.img.gz
"dd" is the command to make a bit-by-bit copy of "if=/dev/hda" as the "Input File" to "of=/mnt/sda1/hda.img.gz" as the "Output File". Everything from the partition will go into an "Output File" named "hda.img.gz". "conv=sync,noerror" tells dd that if it can't read a block due to a read error, then it should at least write something to its output of the correct length. Even if your hard disk exhibits no errors, remember that dd will read every single block, including any blocks which the OS avoids using because it has marked them as bad. "bs=64K" is the block size of 64x1024 Bytes. Using this large of block size speeds up the copying process. The output of dd is then piped through gzip to compress it. - To restore your system:
# gunzip -c /mnt/sda1/hda.img.gz | dd of=/dev/hda conv=sync,noerror bs=64K
NOTE: I've had much success leaving out "conv=sync,noerror" during restore. - Store extra information about the drive geometry necessary in order to interpret the partition table stored within the image. The most important of which is the cylinder size.
# fdisk -l /dev/hda > /mnt/sda1/hda_fdisk.info