2

I'm running an EC2 instance in AWS with 12 GB volume attached to it. The OS is Ubuntu. I want to create a partition of 2GB from this 12GB volume.

So, here is the output of sudo fdisk -l:

$ sudo fdisk -l 
Disk /dev/xvda: 12 GiB, 12884901888 bytes, 25165824 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x89cdb65f

Device       Boot   Start        End     Sectors    Size    Id    Type
/dev/xvda1   *       2048   25165790    25163743     12G    83    Linux

How to create a partition without deleting the OS? To be exact how to create a partition from xvda1 and how to decide first and last sector values?

galoget
  • 349

2 Answers2

1

As the discussion has shown, there isn’t an easy way to shrink the root file system of a running system (resize2fs doesn’t support it). You have two options:

  1. If the VM can’t be restarted, you’ll need to do an online resize (see, for example, this answer).
  2. If the VM can be restarted and can have a period of downtime for the resize, the better approach is to take a snapshot of the EBS volume, mount it on another VM and use that VM to resize it. There is a good guide in this answer.

Regarding your question about the first and last sectors - the first sector of partition 1 remains 2048 (ie enter 2048 when prompted) and fdisk will calculate the last sector number based on the partition size you enter (eg, if you specify a partition size of 2GiB it will calculate the last sector automatically for you).

As others have suggested, having a single partition per EBS volume does make management easier, so you may want to take the opportunity to do that while you’re at it (ie, shrink the root EBS volume to 2GiB and add a second one of 10GiB instead of having a single partitioned volume).

mjturner
  • 7,300
0

All space available in your volume /dev/xvda is already used up by partition /dev/xvda1.

If a filesystem (and data) exists on the volume, but unused space is available at the end of the file system, first shrink the file system using resize2fs <device> <size>, e.g. resize2fs /dev/xvda 2G.

Then use fdisk to reduce end of partition /dev/xvda1 to match the new size of the file system placed on this partition (here: reduce by 2G).

Now you can create a new partition, e.g. /dev/xvda2, using the free space at the end of the volume.

Start of the second partition will be end of the first partition + 1.

End of the second partition depends on your needs, e.g. the number of available sectors (here: 25165824).

Note: In a virtualized environment, I recommend placing only one disk partition per disk device/volume. This makes later changes much easier, e.g. if file system/disk partition must get increased for any reason.

Garstlig
  • 116
  • I tried to run "resize2fs /dev/xvda 2G" but got the error message " esize2fs 1.42.13 (17-May-2015) resize2fs: Device or resource busy while trying to open /dev/xvda Couldn't find valid filesystem superblock." – Masud Shaik Jan 31 '18 at 23:42
  • @MasudShaik As the filesytem is on the partition, you need to use /dev/xvda1 and not /dev/xvda when calling resize2fs. Can you try that and let us know the results. – mjturner Feb 01 '18 at 10:41
  • @mjturner I tried to resize the /dev/xvda1, Got the message "resize2fs 1.42.13 (17-May-2015) Filesystem at /dev/xvda1 is mounted on /; on-line resizing required resize2fs: On-line shrinking not supported " – Masud Shaik Feb 02 '18 at 01:55
  • Ah yes - I had forgotten that resize2fs doesn’t support online shrinking. There are ways to work around it but they are risky - see this answer. – mjturner Feb 02 '18 at 06:53