0

I am running a linux server on one of my machines and I recently upgraded the boot drive to a larger capacity drive. I used dd to clone my older boot drive but now my new boot drive has the same size showing up as my old drive, so how do I merge the unallocated space to my new drive partition using the terminal as I have no GUI.

Edit: This is my current display of the server and fdisk - l:

Server info

fdisk - l output lsblk output

The output of mount | grep /dev/sda2 is:

/dev/sda2 on / type ext4 (rw, relatime)

mount | grep /dev/sda2

1 Answers1

1

Your setup seems very simple. The relevant partition is /dev/sda2, it contains ext4 filesystem, the filesystem is mounted as /. The free space is adjacent to the partition, after the partition (i.e. "to the right").

You want to expand the partition and the filesystem to the right. As ext4 can be expanded to the right online (i.e. when mounted), you can do all this from within the running system. No reboot required.

Please read the entire answer before proceeding.

To be on the safe side, invoke sudo blkid /dev/sda2 and save its output. We will need the string in double-quotes just after PARTUUID=.

Expand the partition:

  1. Run sudo fdisk /dev/sda.
  2. Delete (d) the partition number 2.
  3. Create anew (n) a partition number 2. The start sector must be what it was (4096). Accept the default end sector, it will be somewhat lower than 1953525167. The type of the partition by default should be Linux filesystem and this is right.
  4. After seeing Created a new partition, examine it (p). You should see /dev/sda2 with the size of about 931.5G and the type Linux filesystem. /dev/sda1 should be unaffected (i.e. exactly as it was).
  5. To be on the safe side, change partition UUID for the second partition (x, then u). Type the exact string you got in the output of blkid /dev/sda after PARTUUID= (type the string without double-quotes). Return to the main menu (r).
  6. In case of any doubt quit without writing (q) and wait until you get further help. But if everything looks like I described, write the modified partition table to the disk (w).

Yes, deleting a partition and creating it anew is the right way. Now it's time to expand the filesystem.

Expand the filesystem:

  1. Run sudo resize2fs /dev/sda2. By default the tool will expand the filesystem so it takes the entire (new) partition.
  2. Verify with df -h the filesystem is as large as you expected and there is free space in it.

The filesystem is still the old filesystem with its old UUID. We took care to replicate the PARTUUID of the old partition to the new one. Regardless if your GRUB and /etc/fstab use one or the other, they shouldn't notice the difference and the OS should still be able to boot.