10

On Linux I used to resize MBR partitions using fdisk, even on live filesystems, and then issue a resize2fs/pvresize/... (depending on fs type) to get the new space allocated.

Lately I've been using Xen and GPT partitions, and I've noticed that unfortunately parted doesn't seem to allow on-the-fly resizing of a mounted partition, in fact it will complain:

Error: Partition XXX is being used. You must unmount it before you modify it with Parted.

I've tried both the resize command and even rm + mkpart combination, but they will both complain about the partition being mounted.

How can I do that?

  • A way available out of the box on RHEL/CentOS would be highly appreciated – cyberz Oct 27 '13 at 9:23
11
+50

The safest way to do this is to boot using an emergency medium (a live CD or the like) and use GParted, which will resize both the partition and the filesystem it contains. This will work only if the partition is not currently being used, though.

If you can't afford any downtime, though, you could try using gdisk instead of parted. You'll need to delete the partition you want to resize and create a new one in its place with the same start point, much as you'd have done with fdisk. gdisk is willing to work on an in-use disk, although the kernel might not register any changes. In that case, you may need to use partprobe or kpartx to get the kernel to accept the new partition table, or even reboot the computer if that doesn't work. (This should all be pretty similar to using fdisk.)

  • Looks nice, but is not included by default on CentOS. Any more standard ways? I mean, a redhat-like system should be able to self resize without relying on external programs – cyberz Oct 19 '13 at 15:30
  • 2
    There is no tool that ships with CentOS that will do the job. You must go out of the CentOS repository to do what you want. Note that almost all other distributions (including Fedora) include gdisk in their repositories, and I've heard that gdisk will be included in the next version of RHEL (and therefore of CentOS). – Rod Smith Nov 3 '13 at 14:19
  • Thank you for the answer and the comment, it's exactly the information i was looking for – cyberz Nov 3 '13 at 14:33
  • 1
    gdisk is available in EPEL. – jornane Aug 27 '14 at 17:17
  • I tried both partx and partprobe but eventually had to reboot. Thank you anyway for those tips – Alexandre Bourlier Jan 7 '17 at 11:50
7

This usually only works with more recent Linux distributions. Tools needed:

  • partprobe (usually part of parted)
  • gdisk / sgdisk

A GPT partition stores a backup header at the end of the disk. If you have resized the underlying device, the backup header will be somewhere in the middle. The first step is to move the partition header to the end of the disk.

Assuming the disk is /dev/sda and the partition is /dev/sda3 (must also be the last partition):

sgdisk -e /dev/sda

Then delete, the last partition and re-create it:

sgdisk -d 3 /dev/sda
sgdisk -N 3 /dev/sda

You'll usually see a message indicating that the kernel is unable to re-load the partition table. You have to run partprobe so the partition is registered with the new size:

partprobe /dev/sda

If this is unsuccessful, you'll have to reboot the virtual machine. After that you can grow your filesystem with the appropriate tool, for ext4 etc.:

resize2fs /dev/sda3

Caution: running sgdisk can be destructive. Make sure you have proper backup procedures in place.

  • Awesome! Thanks especially for the partprobe step - I always thought it is necessary to reboot when altering partition table with any mounted partitions. – piit79 Feb 8 '18 at 8:40
  • Keep in mind that destroying and recreating the partition will generate a new PARTUUID for the partition, which is otherwise the only reliable and filesystem-independent way for fstab/GRUB/etc to reliably identify partitions in multi-disk setups. – Teo Klestrup Röijezon Dec 26 '18 at 19:22
4

Here's an example that an automated tool uses to resize a partition online, in one run:

sgdisk -d 1 -n 1:2048:0 -c 1: -u 1:E485F29F-A1F4-4953-9DD8-799EAEA0119B -t 1:0700 /dev/xvda

Here's list of options to sgdisk command:

  • -d 1 delete's first partition
  • -n 1:2048:0 says create new partition "number 1", with starting sector 2048. End sector = "0" which means "use all available space for this partition
  • -u sets unique guid for that partition (this is specific for GPT partitions); you could use 'R' for GUID to be set to a random value. You could also get current partitions's id through gdisk /dev/xvda; p output to reuse the same uid
  • -t 1:0700 basically means first partition is of typecode '0700'.

/dev/xvda was the disk which we repartitioned.

So it deletes and creates a new partition on its place right away.

PS. A few notes on typecode '0700'. From man SGDISK(8)

   -t, --typecode=partnum:{hexcode|GUID}
          Change a single partition's type  code.  You  enter  the  type  code  using  either  a  two-byte  hexadecimal  number,  as 

described earlier, or a fully-specified GUID value, such as EBD0A0A2-B9E5-4433-87C0-68B6B72699C7.

Found best explanation for what '0700' means here - http://www.rodsbooks.com/gdisk/walkthrough.html

"But wait," you say, "I thought the disk had a FAT partition!" Indeed it does. Windows uses a single GUID code for all its data partitions, be they FAT or NTFS. In the past, the same code has been used in Linux for its data partitions. (More on this shortly....) Thus, in this case several different MBR codes are all translated into a single GPT GUID code. GPT fdisk uses, somewhat arbitrarily, the 0x0700 code (or more precisely, EBD0A0A2-B9E5-4433-87C0-68B6B72699C7) for all of these.

In my case I believe that was a Linux ext4 partition, but partition's typecode doesn't mean filesystem type, so '0700' looks more like a catchall type for sgdisk. At least in cases I've seen.

PPS. You may need to run partprobe for kernel to become aware of the partitioning change without rebooting system.

  • 'p' will show you the UUID of the disk. 'i' will show you the UUID of a partition. – Keith Mar 21 at 16:22
2

I'm just summarising some answers and comments here:

parted will simply refuse to change any mounted partition. gdisk will do the job for you, but it is not in the standard RHEL or CentOS repository. It is in the EPEL repository, though.

Keep in mind that changing partitions on a disk that is in use might prevent the kernel from registering the changes. If that happens, use partprobe, partx or reboot.

1

Are you asking about resizing a logical volume? Here is how I would do it:

vgextend vgname /dev/sdb3
lvextend -L +3T /dev/mapper/location
resize2fs /mount/point

OR (xfs):

xfs_growfs /mount/point -D <bytes>
  • he probably not using ext2/ext3/ext4, otherwise resize2fs should work on the fly ? – user218473 Nov 3 '13 at 3:05
  • 1
    @Antony Lee: if you pass -r to lvextend, then it invokes resize2fs for you. – jmtd Mar 13 '15 at 15:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.