Thursday, May 28, 2020

Adding new disk to a system? MBR Partition Table or GUID Partition Table?

I wanted to add a new virtual harddisk drive to a Debian Linux system on an Oracle VirtualBox virtual machine. A little bit planning goes a long way. One question we ought to ask is, what type of partition table should we choose?

For PCs, there are two types of partitions one can choose from,
  1. Master Boot Record Partition Table (MBR Partition Table, or MBR), and
  2. GUID Partition Table (GPT).
The former was developed in 1980's with DOS and enjoys a wide support, but has a major limitation.  The major limitation is rooted from the fact that the logical address to a sector on a MBR partition is 32-bit wide. Given sector size is 512 bytes, the largest partition is limited to

232*512=241=2*240=2 TB

To overcome this limitation, GPT uses 64-bit logical addresses. This arguably allows us to address the number of bytes of an astronomical scale.

Master Boot Record Partition Table

The complete process of adding a harddisk drive to a Debian Linux sytem using the MBR partition is as follows,
  1. Look up the name the attached harddisk drive using lsblk
  2. Create an extended partition using the fdisk tool
  3. Create a logical partition on the extended partition using the fdisk tool
  4. Create a file system on the logical partition
  5. Mount the file system and use it
This method is in fact similar to the method to create GUID partition table. The details are thus skipped here.

GUID Partition Table

There are a couple of tools we can use to create GUID partition tables, e.g.,
  • fdisk. The new version of fdisk has GPT support. We can know whether the version installed in the system has the GPT support by viewing its manual page, e.g., the manual page of the version of fdisk on the Linux system I am using states,

    "
    fdisk is a dialog-driven program for creation and manipulation of partition tables. It understands GPT, MBR, Sun, SGI and BSD partition tables.
    "
  • gdisk. It is a tool much like fdisk. The manual page states that it is a

    "
    GPT fdisk (aka gdisk) is a text-mode menu-driven program for creation and manipulation of partition tables.
    "
  • parted. This is a more powerful tool than fdisk and gdisk.

Below we show 3 examples using these 3 tools to create a GPT.

Using fdisk

  1. Look up the name the attached harddisk drive using lsblk
  2. Create a GPT.
    
    $ sudo fdisk /dev/sdb
    
    Welcome to fdisk (util-linux 2.33.1).
    Changes will remain in memory only, until you decide to write them.
    Be careful before using the write command.
    
    Command (m for help): m
    
    Help:
    
      GPT
       M   enter protective/hybrid MBR
    
      Generic
       d   delete a partition
       F   list free unpartitioned space
       l   list known partition types
       n   add a new partition
       p   print the partition table
       t   change a partition type
       v   verify the partition table
       i   print information about a partition
    
      Misc
       m   print this menu
       x   extra functionality (experts only)
    
      Script
       I   load disk layout from sfdisk script file
       O   dump disk layout to sfdisk script file
    
      Save & Exit
       w   write table to disk and exit
       q   quit without saving changes
    
      Create a new label
       g   create a new empty GPT partition table
       G   create a new empty SGI (IRIX) partition table
       o   create a new empty DOS partition table
       s   create a new empty Sun partition table
    
    
    Command (m for help): g
    
    Created a new GPT disklabel (GUID: 08486B58-59CB-E849-B814-38DD8434D43C).
    
    Command (m for help): n
    Partition number (1-128, default 1):
    First sector (2048-32767966, default 2048):
    Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-32767966, default 32767966):
    
    Created a new partition 1 of type 'Linux filesystem' and of size 15.6 GiB.
    Partition #1 contains a ext4 signature.
    $
    
  3. Display partition information
    
    $ sudo fdisk -l /dev/sdb
    Disk /dev/sdb: 15.6 GiB, 16777216000 bytes, 32768000 sectors
    Disk model: VBOX HARDDISK
    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: gpt
    Disk identifier: 85CF1437-0605-4C91-BE0C-F3B416BEA1D8
    
    Device     Start      End  Sectors  Size Type
    /dev/sdb1   2048 32767966 32765919 15.6G Linux filesystem
    $
    
  4. Make the system to re-load the partition tables
    
    $ sudo partprobe
    $
    
  5. Mount the file system on the partition
    
    $ sudo mount -t ext4 /dev/sdb1 /mnt
    $
    

Using gdisk

  1. Look up the name the attached harddisk drive using lsblk
  2. Create a GPT.
    
    $ sudo gdisk /dev/sdb
    GPT fdisk (gdisk) version 1.0.3
    
    Partition table scan:
      MBR: MBR only
      BSD: not present
      APM: not present
      GPT: not present
    
    
    ***************************************************************
    Found invalid GPT and valid MBR; converting MBR to GPT format
    in memory. THIS OPERATION IS POTENTIALLY DESTRUCTIVE! Exit by
    typing 'q' if you don't want to convert your MBR partitions
    to GPT format!
    ***************************************************************
    
    
    Command (? for help): ?
    b       back up GPT data to a file
    c       change a partition's name
    d       delete a partition
    i       show detailed information on a partition
    l       list known partition types
    n       add a new partition
    o       create a new empty GUID partition table (GPT)
    p       print the partition table
    q       quit without saving changes
    r       recovery and transformation options (experts only)
    s       sort partitions
    t       change a partition's type code
    v       verify disk
    w       write table to disk and exit
    x       extra functionality (experts only)
    ?       print this menu
    
    Command (? for help): n
    Partition number (1-128, default 1):
    First sector (34-32767966, default = 2048) or {+-}size{KMGTP}:
    Last sector (2048-32767966, default = 32767966) or {+-}size{KMGTP}:
    Current type is 'Linux filesystem'
    Hex code or GUID (L to show codes, Enter = 8300):
    Changed type of partition to 'Linux filesystem'
    
    Command (? for help): w
    
    Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
    PARTITIONS!!
    
    Do you want to proceed? (Y/N): y
    OK; writing new GUID partition table (GPT) to /dev/sdb.
    Warning: The kernel is still using the old partition table.
    The new table will be used at the next reboot or after you
    run partprobe(8) or kpartx(8)
    The operation has completed successfully.
    $
    
  3. Display partition information
    
    $ sudo gdisk -l /dev/sdb
    GPT fdisk (gdisk) version 1.0.3
    
    Partition table scan:
      MBR: protective
      BSD: not present
      APM: not present
      GPT: present
    
    Found valid GPT with protective MBR; using GPT.
    Disk /dev/sdb: 32768000 sectors, 15.6 GiB
    Model: VBOX HARDDISK
    Sector size (logical/physical): 512/512 bytes
    Disk identifier (GUID): 08486B58-59CB-E849-B814-38DD8434D43C
    Partition table holds up to 128 entries
    Main partition table begins at sector 2 and ends at sector 33
    First usable sector is 2048, last usable sector is 32767966
    Partitions will be aligned on 2048-sector boundaries
    Total free space is 0 sectors (0 bytes)
    
    Number  Start (sector)    End (sector)  Size       Code  Name
       1            2048        32767966   15.6 GiB    8300
    $
    
  4. Make the system to re-load the partition tables
    
    $ sudo partprobe
    $
    
  5. Mount the file system on the partition
    
    $ sudo mount -t ext4 /dev/sdb1 /mnt
    $
    

Using parted

  1. Look up the name the attached harddisk drive using lsblk
  2. Create a GPT.
    
    $ sudo parted /dev/sdb
    GNU Parted 3.2
    Using /dev/sdb
    Welcome to GNU Parted! Type 'help' to view a list of commands.
    (parted) mklabel gpt
    Warning: The existing disk label on /dev/sdb will be destroyed and all data on
    this disk will be lost. Do you want to continue?
    Yes/No? Yes
    (parted) mkpart primary 1 100%
    (parted) align-check optimal 1
    1 aligned
    (parted) print
    Model: ATA VBOX HARDDISK (scsi)
    Disk /dev/sdb: 16.8GB
    Sector size (logical/physical): 512B/512B
    Partition Table: gpt
    Disk Flags:
    
    Number  Start   End     Size    File system  Name     Flags
     1      1049kB  16.8GB  16.8GB               primary
    
    (parted) quit
    Information: You may need to update /etc/fstab.
    .
    $
    
  3. Display partition information
    
    $ sudo parted -l /dev/sdb
    ...
    
    
    Model: ATA VBOX HARDDISK (scsi)
    Disk /dev/sdb: 16.8GB
    Sector size (logical/physical): 512B/512B
    Partition Table: gpt
    Disk Flags:
    
    Number  Start   End     Size    File system  Name     Flags
     1      1049kB  16.8GB  16.8GB  ext4         primary
    
    
    $
    
  4. Make the system to re-load the partition tables
    
    $ sudo partprobe
    $
    
  5. Create an ext4 file system on the partition.
    
    $ sudo mkfs -t ext4 /dev/sdb1
    mke2fs 1.44.5 (15-Dec-2018)
    /dev/sdb1 contains a ext4 file system
            last mounted on Thu May 28 21:45:35 2020
    Proceed anyway? (y,N) y
    Creating filesystem with 4095488 4k blocks and 1024000 inodes
    Filesystem UUID: 6b43a1a4-ab37-4788-acef-df13149f7bc6
    Superblock backups stored on blocks:
            32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208
    
    Allocating group tables: done
    Writing inode tables: done
    Creating journal (16384 blocks): done
    Writing superblocks and filesystem accounting information: done
    $
    
  6. Mount the file system on the partition
    
    $ sudo mount -t ext4 /dev/sdb1 /mnt
    $
    
From the above example, we see that parted is a more powerful tool. For instance, it has a
align-check command. This is to check if partition satisfies some alignment constraint.

Mouting File System using UUID

We can mount the file system using its UUID. Below is an example,

  1. Display UUID

    
    $ sudo blkid /dev/sdb1
    /dev/sdb1: UUID="6b43a1a4-ab37-4788-acef-df13149f7bc6" TYPE="ext4" PARTLABEL="primary" PARTUUID="d9b95367-7e63-4789-9b6d-65ef8e7c61c0"
    $
    
  2. Mount the file system using its UUID

    
    $ sudo mount --uuid=6b43a1a4-ab37-4788-acef-df13149f7bc6 /mnt
    $
    

No comments:

Post a Comment