Menu

parted gets confused by an USB stick that has grub bootloader code in MBR

2016-08-13
2016-08-14
  • David Tonhofer

    David Tonhofer - 2016-08-13

    So I wanted to use makeboot.sh to my trusty USB stick, whcih has "been around" so to say.

    In particular, the MBR of that stick contained grub bootloader code.

    This mightily confuses parted, and then makeboot.sh says it cannot install onto that stick because the stars aren't right.

    The following code is responsible for this, lines 249 ff:

    # If the destination disk is not MBR partition table (e.g. it's GPT), exit. This program only works for MBR disk.
    if [ -z "$(LC_ALL=C parted -s $target_disk print | grep -iE "^Partition Table:" | grep -iE "msdos")" ]; then
      [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
      echo "The partition table of $target_disk is not for MBR (Master Boot Record). Its layout is:"
      LC_ALL=C parted -s $target_disk print
      [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
      echo "This program is for making a bootable disk with MBR partition table."
      echo "For GPT disk, there is no need to run this program. Just make sure the partition is FAT32 with ID=ef00 and all the files are copied on that. That's all."
      echo "Program terminated!"
      exit 1
    fi
    

    I am not sure why parted has problems. I tried from the command line using fdisk, which works perfectly well:

    [root@elf linux]# fdisk /dev/sdb
    
    Welcome to fdisk (util-linux 2.28).
    Changes will remain in memory only, until you decide to write them.
    Be careful before using the write command.
    
    Command (m for help): p
    Disk /dev/sdb: 7.5 GiB, 8054112256 bytes, 15730688 sectors
    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: 0x20ac7dda
    
    Device     Boot Start      End  Sectors  Size Id Type
    /dev/sdb1        2048 15730687 15728640  7.5G 83 Linux
    

    But parted becomes a bit unhinged and tries to guess (i.e. uses the "loop" partition table):

    [root@elf linux]# parted /dev/sdb
    GNU Parted 3.2
    Using /dev/sdb
    Welcome to GNU Parted! Type 'help' to view a list of commands.
    (parted) p                                                                
    Model: Generic USB Flash Disk (scsi)
    Disk /dev/sdb: 8054MB
    Sector size (logical/physical): 512B/512B
    Partition Table: loop
    Disk Flags: 
    
    Number  Start  End     Size    File system  Flags
     1      0.00B  8054MB  8054MB  fat32
    
    (parted)  
    

    The solution is to remove bootloader code (446 byte) from the MBR:

    dd if=/dev/zero of=/dev/sdb bs=446 count=1

    And for good measure remove anything past the MBR and before the start of the first partition:

    dd if=/dev/zero of=/dev/sdb bs=512 seek=1 count=2047

    But I don't think that is necessary.

    After that, parted behaves correctly:

    [root@elf linux]# parted /dev/sdb
    GNU Parted 3.2
    Using /dev/sdb
    Welcome to GNU Parted! Type 'help' to view a list of commands.
    (parted) p
    Model: Generic USB Flash Disk (scsi)
    Disk /dev/sdb: 8054MB
    Sector size (logical/physical): 512B/512B
    Partition Table: msdos
    Disk Flags:
    
    Number  Start   End     Size    Type     File system  Flags
     1      1049kB  8054MB  8053MB  primary  ext4
    

    THEREFORE, should the makeboot.sh script be changed to offer the user the possibility to zero out bootloader code before parted gets to complain? The user should at least be told to zero the MBR out himself .... I was rather stumped for some time as to what the hell wasn't working.

    Regards,

    -- David

     

    Last edit: David Tonhofer 2016-08-13
    • Tom Roche

      Tom Roche - 2016-08-14

      David Tonhofer[1]

      I wanted to use "makeboot.sh" to [configure a] USB stick, [and] the MBR of that stick contained grub bootloader code. This mightily confused parted.

      Consider pinging bug-parted[2] about this. They might not know about it, or it might be version-specific. Note that bug-parted, despite its name, is the project's "main discussion list[,] used to discuss most aspects of Parted, including development and enhancement requests, as well as bug reports"[3].

      FWIW, Tom Roche Tom_Roche@pobox.com

       

Log in to post a comment.

MongoDB Logo MongoDB