0

I'm running Ubuntu Server 16.04 LTS on an Amazon EC2 instance. I did expand one of the attached volumes and tried to do so in my terminal. Since I've already had a partition with the old size I thought it would be easy to tell it: "Go ahead and take all the space from your parent's device".

As you can see on the picture my device is xvdb with 18G and the partition with the old size is xvdb1 with 10G

enter image description here

According to the German ubuntu wiki I need to use resize2fs for filesystems ext2, ext3 and ext4.

However if I try to use resize2fs -p /dev/xvdb1 18G it is giving me this error back:

The containing partition (or device) is only 2621184 (4k) blocks.
You requested a new size of 4718592 blocks.

I understand that my partition was made with mkfs and the given size was set back there which means it can't be exceeded at the moment. The newly added device size isn't formatted and therefore not ready to use. This means I should delete the partition, generate a new one with the bigger size and restore my data from a backup, right? Even so that feels somewhat wrong...

I'd like to have a nice way to expand partitions on the fly without deleting it and restoring it with the new size (even if that means possible hardware errors). I hope someone knows a decent way of doing this. Please keep in mind that I am limited to the terminal, and have other processes running, which means I can't just reboot every time I change something.

PS.: This Q&A wasn't helping either.

Regards, Megajin

Megajin
  • 101
  • 5

1 Answers1

0

Ok, I've found a solution for myself. In case someone has the same problem as me, here is the solution:

Original Solution from Amazon Docs (root partitions included): http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/storage_expand_partition.html

  1. Identify the device that contains the partition that you want to expand. Use the lsblk command to list all devices and partitions attached to the instance.
  2. Unmount the partition if it is mounted. Run the umount command with the value of MOUNTPOINT from the lsblk command.
  3. Run the parted command on the device (and not the partition on the device). Remember to add the /dev/ prefix to the name that lsblk outputs.
  4. Change the parted units of measure to sectors with unit s.
  5. If you recieve strange error messages you can type ignore.
  6. Examine the output for the total size of the disk, the partition table type, the number of the partition, the start point of the partition, and any flags, such as boot. For gpt partition tables, note the name of the partition; for msdos partition tables, note the Type field (primary or extended). These values are used in the upcoming steps. You can print all these informations through the print command.
  7. Delete the partition entry for the partition using the number from the previous step. For example: rm partitionNumber.
  8. Create a new partition that extends to the end of the volume. (For the msdos partition table example) Note the start point and the partition type of partition 1 above. For the msdos example, there is a start point of 2048s and a partition type of primary. Run the mkpart command with a primary partition type, the start point of partition 1, and 100% to use all of the available space. Example: mkpart primary 2048s 100%.
  9. Run the print command again to verify your partition.
  10. Check to see that any flags that were present earlier are still present for the partition that you expanded. In some cases the boot flag may be lost. If a flag was dropped from the partition when it was expanded, add the flag with the following command, substituting your partition number and the flag name. For example, the following command adds the boot flag to partition 1. E.g : set 1 boot on.
  11. Run the quit command to exit parted.
  12. Check the file system to make sure there are no errors (this is required before you may extend the file system). Note the file system type from the previous print commands. Choose one of the commands below based on your file system type; if you are using a different file system, consult the documentation for that file system to determine the correct check command.

    For ext3 or ext4 file systems: sudo e2fsck -f /dev/partitionName

    For xfs file systems: sudo xfs_repair /dev/partitionName

If you have a non root partition just remount it and everything should work.

Regards, Megajin

Megajin
  • 101
  • 5
  • 1
    In the future, when you create filesystems in EC2, don't partition the volume. Just create the filesystem directly on the block device, e.g. mkfs -t ext4 /dev/xvdc. Then, all you have to do to resize the filesystem... is resize the filesystem. You don't even have to tell resize2fs how big to make it -- it just expands to fill the new device size. There's no technical need for partitions at all on additional volumes. – Michael - sqlbot Apr 11 '17 at 01:13
  • Wow... why didn't I come up with that solution. Thank you very much! – Megajin Apr 11 '17 at 13:46