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

Kamil Maciorowski
  • 69,815
  • 22
  • 136
  • 202
  • 4
    What is the output of `lsblk`? What is the output of `fdisk -l`? (use `sudo` if needed). Please [edit] and add this information to the question. Tell us what device the question is about and what exactly you want to do with the unallocated space (i.e. which partition you want to expand, if this is what you want to do). – Kamil Maciorowski Oct 04 '22 at 15:42
  • Not having a GUI isn't an issue because you can't manage partitions anyway within a running system. Knowing that you can boot a desktop Linux live session and use any of the standard graphical tools for that. – ChanganAuto Oct 04 '22 at 20:38
  • @ChanganAuto "you can't manage partitions anyway within a running system" – Depending on what partition(s) are to change, filesystem(s) therein and operations to perform, this statement may *or may not* be true. – Kamil Maciorowski Oct 04 '22 at 20:40
  • @KamilMaciorowski Indeed, but the assumption here is the OP wants to expand "/" and they may or may not even have a `swap` or other partition after it, with the unallocated space all at the end of drive, resulting from the cloning. – ChanganAuto Oct 04 '22 at 20:45
  • @ChanganAuto "May or may not" is the key. For now we know very little about the current setup and what the OP wants to do. Your assumption regarding `/` is a guess. Therefore IMO the definitive statement "you can't manage partitions anyway within a running system" is too strong. "You may or may not be able to manage …" is what I agree. – Kamil Maciorowski Oct 04 '22 at 21:00
  • @KamilMaciorowski I'm sure my guess will be correct in the end. If not I'll buy you dinner at the Jamie Oliver's restaurant of your convenience :) – ChanganAuto Oct 04 '22 at 21:05
  • [Please don't post images of text](https://unix.meta.stackexchange.com/q/4086/108618). Post text. If you think an image of text is for some reason better than text, post the image along the text, not instead of the text. – Kamil Maciorowski Oct 05 '22 at 16:26
  • If `/dev/sda` is the disk in question then what `fdisk -l /dev/sda` prints is the most important part of the output of `fdisk -l` (frankly: the only important part; I asked for the output of `fdisk -l`, not `fdisk -l /dev/sda`, because you hadn't told us the name of the disk). In the picture you posted this crucial part is truncated. – Kamil Maciorowski Oct 05 '22 at 16:33
  • @KamilMaciorowski I have edited the post with additional information regarding `fdisk -l`. Also I would prefer adding the unallocated space to `/` but if it is not possible then a new partition would work as well. – Ali Asghar Chakera Oct 05 '22 at 16:36
  • Again: what is the output of `lsblk`? It's better to post it as text. – Kamil Maciorowski Oct 05 '22 at 16:38
  • @KamilMaciorowski there is only 1 disk in my system right now, which sda. It only has 1 partition as well. I have attached the updated picture of `lsblk` as well now – Ali Asghar Chakera Oct 05 '22 at 16:38
  • Now we're getting somewhere. What is the output of `mount | grep /dev/sda2`? [Please don't post images of text](https://unix.meta.stackexchange.com/q/4086/108618). – Kamil Maciorowski Oct 05 '22 at 16:42
  • @KamilMaciorowski `/dev/sda2 on / type ext4 (rw, relatime) ` – Ali Asghar Chakera Oct 05 '22 at 16:46

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.

Kamil Maciorowski
  • 69,815
  • 22
  • 136
  • 202
  • Thank you. This worked. It now shows 931.5gb for the partition `/dev/sda2` – Ali Asghar Chakera Oct 05 '22 at 17:58
  • @AliAsgharChakera The first version of this answer didn't care about `PARTUUID`. If you followed it then please see if your `/etc/fstab` contains `UUID=` in the entry where the second field is `/`. If it does then your GRUB probably also uses `UUID` and it's safe to reboot. If your `fstab` uses `PARTUUID=` then don't reboot, we need to investigate further to avoid possible mishap. Sorry for this. – Kamil Maciorowski Oct 05 '22 at 18:02
  • Sorry for being too dumb but I am not sure how to read `etc/fstab`, I used `cat` but it is saying something about `blkid` but `blkid /etc/fstab` is not showing anything. – Ali Asghar Chakera Oct 05 '22 at 18:10
  • This is output I am getting on `cat` `# /etc/fstab: static file system information. # # Use 'blkid' to print the universally unique identifier for a # device; this may be used with UUID= as a more robust way to name devices# that works even if disks are added and removed. See fstab(5). # # # / was on /dev/sda2 during curtin installation /dev/disk/by-uuid/77e0f9ea-8ca8-42ae-9800-e795f3a2b438 / ext4 defaults 0 1/swap.img none swap sw 0 0` – Ali Asghar Chakera Oct 05 '22 at 18:11
  • @AliAsgharChakera OK, `/dev/disk/by-uuid` in `fstab` is fine. `by-partuuid` would be unfortunate for us. My answer is now improved but I think you don't need to do anything more, your OS should reboot fine. – Kamil Maciorowski Oct 05 '22 at 18:14
  • Ok, great. Thank you so much for your help. – Ali Asghar Chakera Oct 05 '22 at 18:16