1

Topic: [SOLVED] Rearranging partitions, including boot partition

I have a 500GB NVMe drive on my Ubuntu machine. A year ago I upgraded from 18.04 to 22.04 by creating a new partition and installing 22.04 to it, so that I could retain the old Ubuntu for file retrieval. (Booting uses Grub 2.06.)

Now I want to get rid of the old partition and merge it with the new. Problem 1: the boot partition is in the middle of my drive. (Don't ask me how it got this way; likely user error.)

So, I want to delete the old data partition (nvme0n1p1 below), move the boot partition (nvme0n1p2 below) to the beginning of the drive, and resize the new data partition (nvme0n1p5 below) to fill the drive. Problem 2: if I try to move the boot partition, GParted (quite reasonably) warns me that moving it will break Grub if I do so.

So, how can I move the boot partition without breaking my booting? Just move it, and then go through the "Fix Grub Boot problem" mentioned in the manual?

P.S. Here's the format of my drive (from GParted and command line tools):

https://i.imgur.com/KVxCQQy.png

griscom@nob:~$ sudo lsblk -o name,maj:min,rm,size,ro,type,fstype,label,mountpoint /dev/nvme0n1
NAME        MAJ:MIN RM   SIZE RO TYPE FSTYPE LABEL MOUNTPOINT
nvme0n1     259:0    0 465.8G  0 disk              
|-nvme0n1p1 259:1    0 240.3G  0 part ext4         /mnt/oldroot
|-nvme0n1p2 259:2    0   513M  0 part vfat         /boot/efi
|-nvme0n1p3 259:3    0     1K  0 part              
`-nvme0n1p5 259:4    0 224.9G  0 part ext4         /
griscom@nob:~$ sudo fdisk -l /dev/nvme0n1
Disk /dev/nvme0n1: 465.76 GiB, 500107862016 bytes, 976773168 sectors
Disk model: Samsung SSD 970 EVO 500GB               
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xc743bd49

Device         Boot     Start       End   Sectors   Size Id Type
/dev/nvme0n1p1           2048 503910399 503908352 240.3G 83 Linux
/dev/nvme0n1p2 *    504016896 505067519   1050624   513M ef EFI (FAT-12/16/32)
/dev/nvme0n1p3      505069566 976771071 471701506 224.9G  5 Extended
/dev/nvme0n1p5      505069568 976771071 471701504 224.9G 83 Linux
griscom@nob:~$ 

2

Re: [SOLVED] Rearranging partitions, including boot partition

By the fact that your drive is using MSDOS partition table and that partition /dev/nvme0n1p2 fat32 /boot/efi looks empty I assume your PC boots using BIOS and not UEFI.  And that the /boot/efi partition came about because Ubuntu creates it by default during installation.

In such a case GRUB2 will have the stage 1 boot code in the first 446 bytes of sector 0, the MBR.  The stage 1 boot code will load the stage 2 boot code from sectors 1 upwards immedately after the MBR.  Stage 2 boot code will look for partition 5 (/dev/nvme0n1p5) and read the Linux kernel (/boot/vmlinuz-*) and initrd image (/boot/initrd*) from the root file system.  Partition /dev/nvme0n1p2 fat32 /boot/efi is unused.

Steps are:
1. Boot your Ubuntu OS, run GParted and delete partition 1.
   Edit /etc/fstab to remove /mnt/oldroot entry to match.
2. Boot GParted Live or other Linux Live rescue media.  Run GParted.
   (Can't move root file system while it's in use).
2.1. Move partition 2 to the start of drive and apply.
2.2. Move start of extended partition to left as much as possible and apply.
2.3. Move start of partition 5 to the left as much as possible and apply.

There should be no need to fix Grub booting.

3

Re: [SOLVED] Rearranging partitions, including boot partition

Thanks for the detailed response, and sorry I've been slow in responding.

I checked, and my unit is indeed booting using UEFI, not BIOS. Does that change your recommendations?


Many thanks,
Dan

4

Re: [SOLVED] Rearranging partitions, including boot partition

For machines with a UEFI firmware there are 2 cases:
1. UEFI is configured to boot in BIOS Compatibility Mode.
   In which case it loads and executes code from drive sector 0 as described above.  Typically used with MBR/MSDOS partition tables but can use GPT.
2. UEFI native mode.
   Recommends use of GPT but theoretically can be used with MBR/MSDOS partition table.  The EFI System Partition (partition type 0xEF on MBR/MSDOS and partition type UUID C12A...C9EB on GPT).  On Linux this is typically mounted at /boot/efi after booting.  Executes UEFI code from /efi/${DISTRO}/grubx64.efi.  This is the GRUB2 boot loader, equivalent to stage 2 described above.  Then the Linux kernel (/boot/vmlinuz-*) and initrd image (/boot/initrd*) are read from the root file system.

References:
* UEFI boot: how does that actually work, then?
  https://www.happyassassin.net/posts/201 … work-then/
* UEFI, Linux
  https://en.wikipedia.org/wiki/UEFI#Linux
* GNU GRUB, Version 2 (GRUB 2), Startup on systems using UEFI formware
  https://en.wikipedia.org/wiki/GNU_GRUB# … I_firmware

Whether your machine uses BIOS, UEFI in BIOS Compatility Mode or UEFI native mode the above steps will work and and there should be no need to fix GRUB booting afterwards.

5

Re: [SOLVED] Rearranging partitions, including boot partition

And it worked! Thanks.

Note that, being uncertain and paranoid, I rebooted between every step to make sure that nothing had been damaged, and reducing the scope of the repair had something been damaged. And indeed: nothing had, and I now have one big partition I can use.


Thanks again,
Dan