2

How can I create a partition for the GRUB legacy boot of GPT disks using free sectors between the GPT (33) and the first aligned partition (2048)?

If I already have partitioned all other sectors, fdisk, gdisk and parted will complain that there are no free sectors available.

What I ended up doing was:

  • exporting the GPT to a text file using sfdisk
  • adding another partition by manually specifying
    • first LBA (34 from 2048)
    • partition number
    • start sector (34)
    • size (2014 sectors)
    • GUID (21686148-6449-6E6F-744E-656564454649)
  • restoring the modified version to the disk
  • running partprobe to refresh the number of available partitions
  • changing the partition type (BIOS boot) using fdisk

I know it's a hacky workaround, but I couldn't find a way to do it with normal tools without destroying other partitions first.

1 Answer 1

5

parted usually works just fine. It will warn you about alignment issues, but in this case you can just ignore it.

# truncate -s 1G foobar.img
# parted foobar.img
GNU Parted 3.4
Using /dev/shm/foobar.img
Welcome to GNU Parted! Type 'help' to view a list of commands.

(parted) unit s
(parted) mklabel gpt
(parted) disk_set pmbr_boot on

(parted) print free
Model:  (file)
Disk /dev/shm/foobar.img: 2097152s
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: pmbr_boot

Number  Start  End       Size      File system  Name  Flags
        34s    2097118s  2097085s  Free Space

(parted) mkpart linux 2048s 2097118s
(parted) set 1 lvm on

Creating the grub partition at sector 34:

(parted) mkpart grub 34s 2047s
Warning: The resulting partition is not properly aligned for best performance: 34s % 2048s != 0s
Ignore/Cancel? Ignore
(parted) set 2 bios_grub on

The end result is a GPT partition table, with boot flag set in the legacy MBR header (required by some machines), and the grub partition squeezed in before the first 1MiB.

(parted) print free
Model:  (file)
Disk /dev/shm/foobar.img: 2097152s
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: pmbr_boot

Number  Start  End       Size      File system  Name   Flags
 2      34s    2047s     2014s                  grub   bios_grub
 1      2048s  2097118s  2095071s               linux  lvm

Of course, for an entirely new partition table, there is no need to do things this way. You can just stick to MiB alignment for the grub partition also, there is no harm in it either way.

1
  • 1
    Pure gold! Thank you, thank you, thank you!
    – Vladan
    Commented Sep 6, 2022 at 23:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .