Announcement

Collapse
No announcement yet.

GRUB and GPT partitioned disks

Collapse
This topic is closed.
X
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    GRUB and GPT partitioned disks

    This information is elsewhere on the web but I thought it might be useful to have it on the forum.

    I don't use windows so I've never seen a need to switch to UEFI style booting. Why add a layer of complication when I'm very comfortable with GRUB?

    However, the are several serious benefits to using the newer GPT (GUID Partition Table) rather than the ancient MBR (Master Boot Record) partitioning. Some of these are:
    • Full duplicate partition table on the disk as a backup in case the primary table gets corrupted or damaged.
    • Automatically optimal sector boundary alignment.
    • By default, allows up to 128 primary partitions - no "extended" or "logical" partitioning required.
    • Maximum device size is well beyond anything produced today.


    Since I'm still using GRUB I don't need an EFI partition, but I do need to reserve space for GRUB to install to. In the early MBR days, GRUB-Legacy (aka GRUB 1) fit neatly ahead within space already reserved by the MBR format. Later, when we went to GRUB-PC (aka GRUB 2) a bit more space was required. Now when using GPT we need to manually reserve the space needed for GRUB2. If you don't reserve this space, GRUB2 won't install to the disk. You'll get the dreaded error:
    Executing 'grub-install /dev/sda' failed.


    This is a fatal error.
    The good news is the space we need is part of the disk we're not using anyway. Just a few quick steps when you partition your disk before installing to it and you'll avoid the above "gotcha."

    What we need to do is create a small partition of a specific type at the beginning of the disk. If you have already formatted your disk using GPT but didn't do this first, don't panic. Unless you did something unusual the space GRUB needs is still there. GPT automatically aligns the first partition at Sector 2048. The easiest (thus best) way to accomplish this is using the "gdisk" utility in terminal mode or from konsole. There are numerous GUI tools for disk partitioning but I won't be explaining any of those as I rarely use them.

    To get started, open a terminal window and type:

    sudo gdisk /dev/sdb

    Use the correct device name for your disk - /dev/sda, /dev/sdb, etc. I will use /dev/sdb for this how-to.

    Once you've entered this command the prompt you see will change to:
    Code:
    Command (? for help):
    All commands for gdisk begin with entering a single letter. Here's an example printout of a GPT partition table from disk (using gdisk command "p"):
    Code:
    Command (? for help): p
    Disk /dev/sdb: 1953525168 sectors, 931.5 GiB
    Logical sector size: 512 bytes
    Disk identifier (GUID): 512242D7-F3BB-4EA1-914B-CEDA513DAB26
    Partition table holds up to 128 entries
    First usable sector is 34, last usable sector is 1953525134
    Partitions will be aligned on 8-sector boundaries
    Total free space is 7 sectors (3.5 KiB)
    
    
    Number  Start (sector)    End (sector)  Size       Code  Name
       1            2048       419430400   200.0 GiB   8300  Linux filesystem
       2       419430408      1953525134   731.5 GiB   8300  Linux filesystem
    If you've never used fdisk or gdisk before, note the first line above says "? for help". Entering a ? will print a command list. It's a good idea to review the list if you're new to gdisk. I recommend you hit the ? often as you follow this how-to to review what you're actually doing.

    You'll notice at the end of the printout above, the partition table information shows that the first partition starts at sector 2048. We'll be using the space between the first usable sector (34) and the last unused sector (2047) for GRUB. This requires some "expert" mode partitioning.

    First we have to change the "sector alignment value" to allow us to begin a sector before 2048. This command is in the "expert" mode of gdisk. To get there we enter "x" and we will see the prompt change again:
    Code:
    Command (? for help): x       
    
    
    Expert command (? for help):
    Now we will set the sector alignment value to "1" so we can manually pick our target sector 34 for our GRUB partition. This command is "l" (lowercase L):
    Code:
    Expert command (? for help): l
    Enter the sector alignment value (1-65536, default = 2048): 1
    
    
    Expert command (? for help):
    and then return to the main menu using "m":
    Code:
    Expert command (? for help): m
    
    Command (? for help):
    Now we can create our new partition using "n" and entering the values we need:
    Code:
    Command (? for help): n
    Partition number (3-128, default 3): 
    First sector (34-2047, default = 34) or {+-}size{KMGTP}: 
    Last sector (35-2047, default = 2047) or {+-}size{KMGTP}: 
    Current type is 'Linux filesystem'
    Hex code or GUID (L to show codes, Enter = 8300):
    Here, since the only free space on the disk is sectors 34 to 2047, it defaulted to those numbers. There are no entries after the colons above because I needed only hit enter three times to use the defaults. If your disk has other free space or you are starting with a blank partition table you may need to enter "34" and "2047" for the First and Last sectors. You can also specify a partition number here by entering it on the first line. This might be useful if you are regimented about your partition numbers and you have multiple drives on your computer. I allowed the default of "3" to be used (more on numbering partitions later).

    Now notice I haven't yet completed this command - I stopped at "Hex code or GUID...". This is where we tell gdisk to use the partition type that GRUB recognizes as space reserved for it to use. The correct entry here is "ef02" ( you can see the entire list of partition types by typing "L" if you wish ). My full entry looks like:
    Code:
    Command (? for help): n
    Partition number (3-128, default 3): 
    First sector (34-2047, default = 34) or {+-}size{KMGTP}: 
    Last sector (35-2047, default = 2047) or {+-}size{KMGTP}: 
    Current type is 'Linux filesystem'
    Hex code or GUID (L to show codes, Enter = 8300): ef02    
    Changed type of partition to 'BIOS boot partition'
    
    
    Command (? for help):
    and now my partition table looks like:
    Code:
    Command (? for help): p
    Disk /dev/sdb: 1953525168 sectors, 931.5 GiB
    Logical sector size: 512 bytes
    Disk identifier (GUID): 512242D7-F3BB-4EA1-914B-CEDA513DAB26
    Partition table holds up to 128 entries
    First usable sector is 34, last usable sector is 1953525134
    Partitions will be aligned on 1-sector boundaries
    Total free space is 0 sectors (0 bytes)
    
    
    Number  Start (sector)    End (sector)  Size       Code  Name
       1            2048       419430400   200.0 GiB   8300  Linux filesystem
       2       419430408      1953525134   731.5 GiB   8300  Linux filesystem
       3              34            2047   1007.0 KiB  EF02  BIOS boot partition
    
    
    Command (? for help):
    This will work fine, I can commit it to disk at this point with the command "w" to "write" the new partition table to disk. However, my personal preference is to have my partition table entries in the same order as they are on the disk. I try to work logically on computers and this is more logical to me. Fixing this is not a problem as gdisk has the "s" command to auto-magically "sort" the partition entries for me:
    Code:
    Command (? for help): s
    You may need to edit /etc/fstab and/or your boot loader configuration!
    
    
    Command (? for help): p
    Disk /dev/sdb: 1953525168 sectors, 931.5 GiB
    Logical sector size: 512 bytes
    Disk identifier (GUID): 512242D7-F3BB-4EA1-914B-CEDA513DAB26
    Partition table holds up to 128 entries
    First usable sector is 34, last usable sector is 1953525134
    Partitions will be aligned on 1-sector boundaries
    Total free space is 0 sectors (0 bytes)
    
    
    Number  Start (sector)    End (sector)  Size       Code  Name
       1              34            2047   1007.0 KiB  EF02  BIOS boot partition
       2            2048       419430400   200.0 GiB   8300  Linux filesystem
       3       419430408      1953525134   731.5 GiB   8300  Linux filesystem
    
    
    Command (? for help):
    The warning "You may need to edit /etc/fstab and/or your boot loader configuration!" can safely be ignored if you're using UUIDs in GRUB and fstab, UUID usage has been the default for Ubuntu for quite some time.

    Now I can hit "w" and write this all to disk, followed by a "q" to quit gdisk. Now your disk is ready for a GRUB install!

    If you started with a blank partition table and now want to create more partitions on this disk, you need to return to "expert" mode and reset the sector alignment back to 2048 (the default) before continuing. The entries to do this are "x", "l" (lower case L), enter, "m". Which, in order do:
    Code:
    x     = extra functionality
    l     = set the sector alignment value
    enter = accept the default of 2048
    m     = return to main menu
    and then continue creating more partitions. Or you could just enter "w" to write to disk as it stands, quit gdisk with a "q", and launch your favorite GUI partitioning tool.

    Additional comment: The GRUB installer will find and use this partition in it's raw state. There is no need to install a file system to it (aka format) as you will not be using it to store any files. Once you have created the ef02 partition, run grub-install to your disk - sudo grub-install /dev/sdb - as you would have in the past. Do not install grub to this partition - sudo grub-install /dev/sdb1 - as this will not make your drive bootable.
    Last edited by oshunluvr; Aug 15, 2017, 07:20 AM. Reason: more info

    Please Read Me

    #2
    Originally posted by oshunluvr View Post
    ... This is where we tell gdisk to use the partition type that GRUB recognizes as space reserved for it to use. The correct entry here is "ef02" ( you can see the entire list of partition types by typing "L" if you wish ). ...
    Using the L listing for those who haven't used gdisk:
    Code:
    Hex code or GUID (L to show codes, Enter = AF00): [B]L[/B]
    0700 Microsoft basic data  0c01 Microsoft reserved    2700 Windows RE          
    3000 ONIE boot             3001 ONIE config           3900 Plan 9              
    4100 PowerPC PReP boot     4200 Windows LDM data      4201 Windows LDM metadata
    4202 Windows Storage Spac  7501 IBM GPFS              7f00 ChromeOS kernel     
    7f01 ChromeOS root         7f02 ChromeOS reserved     8200 Linux swap          
    8300 Linux filesystem      8301 Linux reserved        8302 Linux /home         
    8303 Linux x86 root (/)    8304 Linux x86-64 root (/  8305 Linux ARM64 root (/)
    8306 Linux /srv            8307 Linux ARM32 root (/)  8400 Intel Rapid Start   
    8e00 Linux LVM             a500 FreeBSD disklabel     a501 FreeBSD boot        
    a502 FreeBSD swap          a503 FreeBSD UFS           a504 FreeBSD ZFS         
    a505 FreeBSD Vinum/RAID    a580 Midnight BSD data     a581 Midnight BSD boot   
    a582 Midnight BSD swap     a583 Midnight BSD UFS      a584 Midnight BSD ZFS    
    a585 Midnight BSD Vinum    a600 OpenBSD disklabel     a800 Apple UFS           
    a901 NetBSD swap           a902 NetBSD FFS            a903 NetBSD LFS          
    a904 NetBSD concatenated   a905 NetBSD encrypted      a906 NetBSD RAID         
    ab00 Recovery HD           af00 Apple HFS/HFS+        af01 Apple RAID          
    af02 Apple RAID offline    af03 Apple label           af04 AppleTV recovery    
    af05 Apple Core Storage    bc00 Acronis Secure Zone   be00 Solaris boot        
    bf00 Solaris root          bf01 Solaris /usr & Mac Z  bf02 Solaris swap        
    bf03 Solaris backup        bf04 Solaris /var          bf05 Solaris /home       
    bf06 Solaris alternate se  bf07 Solaris Reserved 1    bf08 Solaris Reserved 2  
    Press the  key to see more codes: 
    bf09 Solaris Reserved 3    bf0a Solaris Reserved 4    bf0b Solaris Reserved 5  
    c001 HP-UX data            c002 HP-UX service         ea00 Freedesktop $BOOT   
    eb00 Haiku BFS             ed00 Sony system partitio  ed01 Lenovo system partit
    ef00 EFI System            ef01 MBR partition scheme  [COLOR=#ff0000][B]ef02 BIOS boot partition[/B][/COLOR] 
    f800 Ceph OSD              f801 Ceph dm-crypt OSD     f802 Ceph journal        
    f803 Ceph dm-crypt journa  f804 Ceph disk in creatio  f805 Ceph dm-crypt disk i
    fb00 VMWare VMFS           fb01 VMWare reserved       fc00 VMWare kcore crash p
    fd00 Linux RAID
    "A nation that is afraid to let its people judge the truth and falsehood in an open market is a nation that is afraid of its people.”
    – John F. Kennedy, February 26, 1962.

    Comment


      #3
      Looks good (I read but didn't test it--as you have done the testing and actually used it).

      Up to 128 primary partitions - no "extended" or "logical" partitioning required.
      I believe that, in theory, at least:

      The EFI stipulates a minimum of 16,384 bytes be reserved for the partition table array, so there are 128 partition entries reserved, each 128 bytes long.
      You can have more than 128. But, who needs even as many as 128 partitions? and what partition utilities/software programs might choke beyond 128, anyway?

      Fun fact (from Wikipedia, I think):

      The globally unique identifier (GUID) for the BIOS boot partition in the GPT scheme is 21686148-6449-6E6F-744E-656564454649[2] (which, when written to a GPT in the required little endian fields, forms the ASCII string "Hah!IdontNeedEFI").
      An intellectual says a simple thing in a hard way. An artist says a hard thing in a simple way. Charles Bukowski

      Comment


        #4
        The bread crumbs lead me here. Thanks.

        Comment


          #5
          Originally posted by Qqmike View Post
          I believe that, in theory, at least: You can have more than 128. But, who needs even as many as 128 partitions? and what partition utilities/software programs might choke beyond 128, anyway?
          Yes you are technically correct, GPT will support more than 128 partitions if you increase the size of the partition table array, but as you also correctly pointed out, many of the utilities we use will not support partitions beyond 128. parted will allow you to expand to 65536 partitions but why? Linux has it's own limits also but these days the theoretical limits go well beyond any actual need. Since I do like to be as technically correct as I can, I have corrected the original post.

          Originally posted by Qqmike View Post
          The globally unique identifier (GUID) for the BIOS boot partition in the GPT scheme is 21686148-6449-6E6F-744E-656564454649[2] (which, when written to a GPT in the required little endian fields, forms the ASCII string "Hah!IdontNeedEFI").
          This is a cool tidbit, I like it!

          I also changed the "Maximum device size" comment as it was not correct either. I find conflicting opinions on the actual limit - somewhere between 7.02 and 9.4 Zettabytes. Another ridiculously large number to quibble over.
          Last edited by oshunluvr; Aug 15, 2017, 06:59 AM.

          Please Read Me

          Comment


            #6
            The globally unique identifier (GUID) for the BIOS boot partition in the GPT scheme is 21686148-6449-6E6F-744E-656564454649[2] (which, when written to a GPT in the required little endian fields, forms the ASCII string "Hah!IdontNeedEFI").
            Found the reference on that:
            Second paragraph under Overview:
            https://en.wikipedia.org/wiki/BIOS_boot_partition
            And there's others, like
            Magic Numbers: The Secret Codes that Programmers Hide in Your PC
            https://www.howtogeek.com/201059/mag...de-in-your-pc/
            An intellectual says a simple thing in a hard way. An artist says a hard thing in a simple way. Charles Bukowski

            Comment


              #7
              Additional note: If, during grub-install to a disk prepared in the above detailed manner, you see this (or very similar) error message:

              Code:
              ~$ sudo grub-install /dev/sda  
              Installing for i386-pc platform.
              grub-install: warning: Attempting to install GRUB to a disk with multiple partition labels.  This is not supported yet..
              grub-install: error: filesystem `btrfs' doesn't support blocklists.
              This usually indicates that some left-over info is present in the sectors from previous use of the drive.

              In my case it was that I had the drive previously formatted using btrfs on the full device (no partition table) so left over btrfs system data was still sitting somewhere in sectors 34 to 2047.

              To zero out this area so that grub will use it, simply write zeros to it using dd. In my case, the EF02 partition was /dev/sda1 so I did:

              Code:
              sudo dd if=/dev/zero of=/dev/sda1
              Then a subsequent grub-install went perfectly.
              Last edited by oshunluvr; Sep 11, 2017, 04:56 PM.

              Please Read Me

              Comment


                #8
                oshunluvr, what is your #2 partition for and why 200GiB?
                If you think Education is expensive, try ignorance.

                The difference between genius and stupidity is genius has limits.

                Comment


                  #9
                  Originally posted by SpecialEd View Post
                  oshunluvr, what is your #2 partition for and why 200GiB?
                  The short answer is nothing really.

                  This whole post was just educational, The disk I did this on was a playground disk - a disk I use for experimenting. I just wanted a partition table on there for demo purposes.

                  Please Read Me

                  Comment


                    #10
                    Originally posted by oshunluvr View Post
                    The short answer is nothing really.

                    This whole post was just educational, The disk I did this on was a playground disk - a disk I use for experimenting. I just wanted a partition table on there for demo purposes.
                    Oh ok. Last night --as I am a bit of an insomniac-- I followed your instructions and partitioned my SSD, while connected via usb-- and successfully installed Kubuntu 18.04. It's kind of nice being able to do this on a peripheral drive while still maintaining the system drive in the machine. Now hopefully when I install the SSD I will be most of the way done.

                    Best,
                    If you think Education is expensive, try ignorance.

                    The difference between genius and stupidity is genius has limits.

                    Comment

                    Working...
                    X