Site icon Edumotivation

gdisk Command: How to Create GUID Partitions (GPT) in Linux

How to Create GUID Partitions (GPT) in Linux

In this lesson we’re going to discuss, How to manage GUID partitions (GPT) on a Linux system using gdisk command.

In my previous article, I had explained following concepts and I would request you to read that guide first.

Why you’d want to use GUID partitions

Now the GUID partitioning scheme is a newer partitioning scheme and we need to discuss why you’d want to use GUID partitions over older partitioning schemes such as MBR.

The key problem here is that MBR has a lot of limitations.

It’s been around for a very longtime it’s been around since the early 1980s.

And while it was great in its day, today it’s got a lot of limitations on a lot of short comings.

Here are some examples.

First of all the Master Boot Record under MBR has to be installed in the first 512 bytes of the hard disk drive.

And you can only have four primary partitions on a single storage device and that is really limiting.

Also the default block size used by MBR is 512 bytes and because of this an MBR partition on the hard drive can only be up to 2 Terabytes (TB) in size.

And in 1980s that was a huge amount of space almost an unthinkable amount of space.

Today, well 2 Terabyte (TB) drives are cheap you can go down to your local store and buy one.

Now to address these limitations many patches workarounds and really downright Kluge’s have been implemented over the years to try to get around these MBR shortcomings such as using Logical Block Addressing or LBA this allows you to use larger hard disk drives.

In addition, we have started using 4 Kilobyte (KB) sectors with MBR partitions in order to increase the maximum partition size on a particular disk.

And we’ve also started defining one of our available 4 partitions as an extended partition so it can hold many logical partitions.

Explaining GUID Partition Table

Now all of these MBR workarounds have served us well for a really long time but frankly they’ve outlived their usefulness.

We need bigger disks and we need better performance out of our disks.

Accordingly, a new disk partitioning scheme is now available in Linux.

It’s called the Globally Unique Identifier Partition (GUID) table scheme we just call it GUID or sometimes just GPT.

It’s intended to be a replacement for the old MBR partitioning scheme.

By the way GPT stands for GUID Partition Table.

Really GPT is a part of the overall Unified Extensible Firmware Interface or UEFI specification.

Which is designed really to replace the BIOS on most computer systems and provide a better Software to Hardware interface.

Basically, think of UEFI as a BIOS on steroids.

Now even though UEFI is only implemented on very late-model computers you can still use GPT partitioning on older Linux systems that still use a standard BIOS.

Really GPT is a better way to go vs. MBR and that’s because it has several key advantages that you see here.

GPT Advantages

Creating and Managing GPT Disk Partitions

So, to manage GPT partitions we can’t use fdisk anymore instead we use a utility named gdisk which works in many ways a lot like fdisk.

Now you can use gdisk command to do a lot of different things.

Features of gdisk:

For example, Suppose I added a second hard disk drive to my system, and I wanted to create a GPT partition on it so I can use it to store data.

So, in order to do this, I would enter gdisk at the shell prompt and then the device file that I wanted to manage partitions on /dev/sdb.

The syntax is the same as the old fdisk utility.

~$ 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): 

Really if you’re comfortable already using fdisk then using gdisk command will be really easy because really the commands are almost the same.

Print gdisk Help menu

Now it’s a good idea when you first start using gdisk to use the question mark (?) command to get a list of all the available commands.

~$ 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): 

Here are some commonly used commands listed.

Note:

q allows you to quit without saving changes which is a really useful thing.

if you’re going and creating partitions and realize you really don’t like what’s happened, you hit q and everything goes back to the way it was.

Because just like fdisk the gdisk command does not actually write the changes to disk immediately the changes are only saved in memory until you actually commit them with the w command.

Creating a new Partition

So, if you wanted to create a new partition on the disk you would enter n at the gdisk prompt and then you specify the partition number, you specify your first sector that you want to use for the partition then you specify the last sector you want to use for the partition.

~$ sudo gdisk /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.

Command (? for help): n
Partition number (1-128, default 1): 
First sector (34-6291422, default = 2048) or {+-}size{KMGTP}: 
Last sector (2048-6291422, default = 6291422) or {+-}size{KMGTP}: +2G

By default, what gdisk is going to do is pick the next available sector for the first sector in this situation there are no partitions already on the disk, so it picks the very first sector available.

And then for the last sector you can either specify the sector you want to use which I don’t anybody who actually does, what we usually do is just specify the size that we want to use.

So, in this case I specified 2 GB.

Here are the letters you can use when sizing a partition.

and so on.

When sizing the partition, you have to follow the syntax given below.

+size{K,M,G,T,P}

As you may have noticed in the above example, while setting the 2GB size for the partition, I mentioned the size something like this: +2G.

Specify the Partition type

Notice also that you’re given the option to specify the partition type that you want to use for the partition.

Now the partition type numbers that you use with gdisk command are very similar to those used with MBR partitions.

Notice here that in order to create a Standard Linux Partition we use the type code of 8300 that’s the default.

~$ sudo gdisk /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.

Command (? for help): n
Partition number (1-128, default 1): 
First sector (34-6291422, default = 2048) or {+-}size{KMGTP}: 
Last sector (2048-6291422, default = 6291422) or {+-}size{KMGTP}: +2G
Current type is 'Linux filesystem'        
Hex code or GUID (L to show codes, Enter = 8300): 8300
Changed type of partition to 'Linux filesystem'

Command (? for help):

Using MBR partitions the type code for a standard Linux partition is 83.

So, you can see there’s a lot of parallels there.

List of all possible Partition types

And just as with fdisk for MBR partitions with gdisk command you can use the L option at the gdisk prompt to view a list of all possible partition types and their associated codes.

Hex code or GUID (L to show codes, Enter = 8300): L   
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             a000 Android bootloader    a001 Android bootloader 2
a002 Android boot          a003 Android recovery      a004 Android misc        
a005 Android metadata      a006 Android system        a007 Android cache       
a008 Android data          a009 Android persistent    a00a Android factory     
a00b Android fastboot/ter  a00c Android OEM           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        

Print the Partition Table

Now once you’ve got all of your partitions defined you can use the p command to view a list of all the partitions on the disk.

Command (? for help): p 
Disk /dev/sdb: 6291456 sectors, 3.0 GiB
Model: VMware Virtual S
Sector size (logical/physical): 512/512 bytes
Disk identifier (GUID): 588FBB6B-5F9E-4807-92E6-576FD9C40CC9
Partition table holds up to 128 entries
Main partition table begins at sector 2 and ends at sector 33
First usable sector is 34, last usable sector is 6291422
Partitions will be aligned on 2048-sector boundaries
Total free space is 2097085 sectors (1024.0 MiB)

Number  Start (sector)    End (sector)  Size       Code  Name
   1            2048         4196351   2.0 GiB     8300  Linux filesystem

Command (? for help): 

As you can see here, we created the first partition on the /dev/sdb hard disk drive.

Here you can also see some important information’s like:

Write Partition Table to disk

If you decide that you want to back out and start over enter q to abandon all your changes.

If you do like your changes and you want to commit them to disk use the w command.

In this case, I am happy with whatever configuration I have done, so I want to save all the settings using the w command.

~$ sudo gdisk /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.

Command (? for help): n
Partition number (1-128, default 1): 
First sector (34-6291422, default = 2048) or {+-}size{KMGTP}: 
Last sector (2048-6291422, default = 6291422) or {+-}size{KMGTP}: +2G
Current type is 'Linux filesystem'        
Hex code or GUID (L to show codes, Enter = 8300): 8300
Changed type of partition to 'Linux filesystem'

Command (? for help): p 
Disk /dev/sdb: 6291456 sectors, 3.0 GiB
Model: VMware Virtual S
Sector size (logical/physical): 512/512 bytes
Disk identifier (GUID): 588FBB6B-5F9E-4807-92E6-576FD9C40CC9
Partition table holds up to 128 entries
Main partition table begins at sector 2 and ends at sector 33
First usable sector is 34, last usable sector is 6291422
Partitions will be aligned on 2048-sector boundaries
Total free space is 2097085 sectors (1024.0 MiB)

Number  Start (sector)    End (sector)  Size       Code  Name
   1            2048         4196351   2.0 GiB     8300  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.

Update new partition to kernel

Now after you’ve written your partitions to disk be aware that you may actually need to reboot the system before the Linux kernel will recognize the partition changes you made with gdisk Command.

Now that is actually kind of a pain because a lot of times you don’t want to take the system down just to allow the kernel to see a new partition.

That’s particularly true on a server system where there may be active connections going on with network clients and you don’t want to take that server out of commission even it’s only for a few minutes to reboot.

Therefore, what you can do is run the partprobe command.

The partprobe command will force the kernel to see the new partition table without rebooting the system.

~$ sudo partprobe -s
/dev/sdb: gpt partitions 1

The -s option causes partprobe to show a summary of what it finds.

So, as you can see here it found the new partition that was just created on /dev/sdb.

We have now successfully created a new partition.

Now before storing data in this partition, we need to format and mount it.

Formatting the partition in Linux

You can format any partition using the mkfs command.

So, type the following command to format the partition.

In this example, I am formatting this partition with the ext4 file system.

# mkfs.ext4 /dev/sdb1
mke2fs 1.45.4 (23-Sep-2019)
Creating filesystem with 524288 4k blocks and 131072 inodes
Filesystem UUID: 03b2ee34-1135-4d6b-91b8-a6846064ae5e
Superblock backups stored on blocks: 
	32768, 98304, 163840, 229376, 294912

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (16384 blocks): done
Writing superblocks and filesystem accounting information: done 

Mount the partition

Now you can mount this partition using the mount command.

Suggested Read: How to Mount and Unmount File Systems in Linux

Temporarily mount the File System.

Here I am mounting this partition in the /mydata directory.

# mount /dev/sdb1 /mydata/

To check whether the partition is mounted, run the following command.

# mount
/dev/sda3 on / type xfs (rw,relatime,seclabel,attr2,inode64,noquota)
/dev/sda1 on /boot type ext4 (rw,relatime,seclabel)
/dev/sdb1 on /mydata type ext4 (rw,relatime,seclabel)

This method will temporarily mount your partition.

Permanently mount the File System

To mount this partition permanently, you must enter this partition in the /etc/fstab file.

# nano /etc/fstab

# /etc/fstab
# Created by anaconda on Sun Jan 24 10:20:39 2021
#
# Accessible filesystems, by reference, are maintained under '/dev/disk/'.
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info.
#
# After editing this file, run 'systemctl daemon-reload' to update systemd
# units generated from this file.
#
UUID=b119d862-07b7-4842-a6ea-cd2da64f8eae /                       xfs     defaults        0 0
UUID=e9f23f2d-118e-4a5e-94ab-b08afb78a3be /boot                   ext4    defaults        1 2
UUID=d3e3cc9a-6c24-4d58-9d33-4aeaa2eef5d1 swap                    swap    defaults        0 0
UUID=03b2ee34-1135-4d6b-91b8-a6846064ae5e /mydata      	          ext4	  defaults	  0 0

You can use blkid to get the UUID number of any disk.

# blkid /dev/sdb1
/dev/sdb1: UUID="03b2ee34-1135-4d6b-91b8-a6846064ae5e" TYPE="ext4" PARTLABEL="Linux filesystem" PARTUUID="71377509-6445-4c5c-a98c-9487f4412eed"

Before concluding this article, Let’s create another partition.

I am giving more and more examples so that you can understand this topic more thoroughly.

As you can see, There is already a Partition in the /dev/sdb hard drive.

# sudo gdisk /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.

Command (? for help): p
Disk /dev/sdb: 6291456 sectors, 3.0 GiB
Model: VMware Virtual S
Sector size (logical/physical): 512/512 bytes
Disk identifier (GUID): 930CCA9E-09A8-4B6D-A63A-48F9094CD028
Partition table holds up to 128 entries
Main partition table begins at sector 2 and ends at sector 33
First usable sector is 34, last usable sector is 6291422
Partitions will be aligned on 2048-sector boundaries
Total free space is 2097085 sectors (1024.0 MiB)

Number  Start (sector)    End (sector)  Size       Code  Name
   1            2048         4196351   2.0 GiB     8300  Linux filesystem

Follow the below steps to create a new Partition.


### Creating a New Partition

# sudo gdisk /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.

Command (? for help): n
Partition number (2-128, default 2): 
First sector (34-6291422, default = 4196352) or {+-}size{KMGTP}: 
Last sector (4196352-6291422, default = 6291422) or {+-}size{KMGTP}: +500M
Current type is 'Linux filesystem'
Hex code or GUID (L to show codes, Enter = 8300): 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.

### Update the partition to kernel

[root@localhost ~]# partprobe -s
/dev/sda: msdos partitions 1 2 3
/dev/sdb: gpt partitions 1 2
/dev/sdc: msdos partitions

### Formatting the Partition

[root@localhost ~]# mkfs.ext4 /dev/sdb2
mke2fs 1.45.4 (23-Sep-2019)
Creating filesystem with 512000 1k blocks and 128016 inodes
Filesystem UUID: 7dbf105a-2a31-480f-a432-90cc1dafd14d
Superblock backups stored on blocks: 
	8193, 24577, 40961, 57345, 73729, 204801, 221185, 401409

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done 

### Print the Partition Table

# gdisk /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.

Command (? for help): p
Disk /dev/sdb: 6291456 sectors, 3.0 GiB
Model: VMware Virtual S
Sector size (logical/physical): 512/512 bytes
Disk identifier (GUID): 930CCA9E-09A8-4B6D-A63A-48F9094CD028
Partition table holds up to 128 entries
Main partition table begins at sector 2 and ends at sector 33
First usable sector is 34, last usable sector is 6291422
Partitions will be aligned on 2048-sector boundaries
Total free space is 1073085 sectors (524.0 MiB)

Number  Start (sector)    End (sector)  Size       Code  Name
   1            2048         4196351   2.0 GiB     8300  Linux filesystem
   2         4196352         5220351   500.0 MiB   8300  Linux filesystem

Now you can store data by mounting this Partition.

# mount /dev/sdb2 /data/

You can visit at following websites to get more information on fdisk command.

Conclusion

I hope that now you have a good understanding of How to Create GUID Partitions (GPT) in Linux and you have some ideas for how you can use gdisk command within your workflow.

If anyone does have any questions about what we covered in this guide then feel free to ask in the comment section below and I will do my best to answer those.

Exit mobile version