This tutorial explains how to manage GPT disk partitions and convert MBR disk in GPT disk from gdisk command in detail. Learn how to create, list, format, mount (temporary and permanent), remove and delete GPT partitions in Linux step by step with practical examples.
For this tutorial I assume that you have either non-partitioned disk space or have a separate disk for exercise. If you are using virtual program (such as VMware or Virtual Box) for the practice, I suggest you to add a new virtual hard disk. To learn how to add new virtual disk in VMware please see the first part of this tutorial.
This tutorial is the third part of our article “Linux Disk Management Explained in Easy Language with Examples”. You can read other parts of this article here.
This is the first part of this article. This part explains basic concepts of Linux disk management such as BIOS, UEFI, MBR, GPT, SWAP, LVM, RAID, primary partition, extended partition and Linux file system type.
This is the second part of this article. This part explains how to create primary, extended and logical partitions from fdisk command in Linux step by step with examples.
This is the fourth part of this article. This part explains how to create primary, extended, logical and GPT partitions from parted command in Linux step by step with examples.
This is the fifth part of this article. This part explains how to create swap partition in Linux with examples including basic swap management tasks such as how to increase, mount or clear swap memory.
This is the sixth part of this article. This part explains basic concepts of LVM in detail with examples including how to configure and manage LVM in Linux step by step.
This is the last part of this article. This part explains basic concepts of RAID in detail with examples including how to configure and manage software RAID in Linux step by step.
Creating GPT Partitions
For this exercise we added one additional virtual disk in our system. We can list all attached hard disks with fdisk –l command.
As above figure shows there are two un-partitioned disks /dev/sdb and /dev/sdc. To figure out which disk we added recently check its sequence number. Recently added disk will be last in sequence. Sequence starts with sda and goes up to the last disk. For example if there are three SCSI disk, first disk will be sda and last disk will be sdc.
We will use /dev/sdc disk in this tutorial.
Although we can use fdisk command to create GPT partitions, but GPT support in fdisk is added recently and not in stable stage.
So it is better to use gdisk utility which is specially created for GPT disk.
Let’s initiate gdisk command. This command needs the name of disk where we want to create the partition as its argument.
We can use only one partition scheme from MBR or GPT. If disk is used for MBR partition previously or it contains any MBR partition currently, we will get above warning message.
We used the same disk /dev/sdc in second part of this article to practice the MBR partition with fdisk command. At the end of that practice we deleted all partitions but kept MBR record in hard disk.
Before we start creating partitions let’s view the current layout of disk with p command.
The p command also provides information about available space in hard disk to create a new partition. If last partition ends before the last sector of hard disk, we have available space to create new partition. As we can see in above output currently no value is available in partition section that means there is no partition available and entire disk space is available for partition.
Type n and press Enter key to create new partition
In GPT partition scheme we are allowed to create maximum 128 partitions.
Due to huge numbers of supported partition, GPT does not distinguish partitions in primary, extended and logical partitions. In GPT all partitions are equal.
To learn more about GPT partition scheme, see the first part of this tutorial which explains this and other relative topics in detail with examples.
Type the partition number (or keep the default number) and press Enter key.
Type the first available sector for partition (or accept the default sector) and press Enter key.
Type the last available sector for partition. Alternatively we can use KiB, MiB or GiB units to define the size of partition. (To learn what are the differences between KiB, MiB, GiB and KB, MB, GB, see the first part of this article.)
Each partition uses a special marking known as partition system identifier. This marking is used to identify the purpose of partition. To print all available partition types we can use l command.
Unless we change the type of any partition it is marked as Linux Standard partition. But if requires, we can change it here. In next part of this tutorial we will learn how to change the partition type, for this part let’s keep the default and press Enter key.
Following figure illustrates above process step by step
We can verify the creation of partition with p command
Following same process create two more partitions
Following the same way we can create more partitions (maximum allowed 128) as per requirement. To view the available space for new partition use p command.
As above output shows there are total 4194270 sectors available in hard disk. From which we used only 2508799 sectors in partitions. 1685471 sectors are still available for partitions.
Just like fdisk, gdisk also keep all changes in memory, nothing will be saved in hard disk until we confirm the same.
To accept the change type w and press Enter key
Since changes once written in disk cannot be reverted, gdisk will ask for final confirmation. This is the important feature. If you made any mistake in above process, just type N here and nothing will be changed in hard disk.
If we are working with a disk which contains mounted partitions, we may get following warning.
WARNING: Re-reading the partition table failed with error 16: Device or resource busy. The kernel still uses the old table. The new table will be used at the next reboot or after you run partprobe(8) or kpartx(8) Syncing disks.
This message indicates that gdisk cannot update the in-memory kernel partition table because disk is busy (mounted). Kernel reads partition table when system boots. So the kernel will be updated about this change at next reboot automatically. Meanwhile if we want to update the kernel immediately, we have to use partprobe command which force kernel to reread the partition table.
Run following command.
Replace /dev/sdc with your disk name.
We have successfully created the partitions. To view the partitions use fdisk –l command.
These partitions are useless unless we put a file system in them. File system is a logical container which allows us to store files and directories. To learn more about file system see the first part of this tutorial.
How to create file system
To create a file system in partition we have to format it with supported file system. To format a partition following command is used.
#mkfs –t [supported file system type] [partition]
Some common supported file systems are ext3, ext4 and xfs. Let’s format the recently created partitions with these file systems.
File system type ext3 partition /dev/sdc1
File system type ext4 partition /dev/sdc2
File system type xfs partition /dev/sdc3
If disk was formatted with xfs system previously, mkfs will detect that and display a warning message. To ignore this message and create new file system use –f option.
Now partitions have file system, can we use them for data storage now? No, even though partitions are ready to use, we cannot use them until they are connected with primary directory structure (Main Linux file system).
Linux file system (primary directory structure) starts with root (/) directory and everything goes under it or its subdirectories. We have to mount partitions somewhere under this directory tree. We can mount partitions temporary or permanently.
Mounting partitions temporary
Following command is used to mount partition temporary.
#mount [what to mount] [where to mount]
Mount command accepts several options and arguments which I will explain separately in another tutorial. For this tutorial this basic syntax is sufficient.
what to mount :- This is the partition.
where to mount :- This is the directory which we will use to access the mounted resource.
Once mounted, whatever action we will perform in mounted directory will be performed in mounted resources. Let’s understand it practically.
- Create a mount directory in / directory
- Mount /dev/sdc1 partition
- List the content
- Create a test directory and file
- List the content again
- Un-mount the /dev/sdc1 partition and list the content again
- Now mount the /dev/sdc2 partition and list the content
- Again create a test directory and file. Use different name for file and directory
- List the content
- Un-mount the /dev/sdc2 partition and list the content again
Following figure illustrates this exercise step by step
As above figure shows whatever action we performed in mount directory was actually performed in respective partition.
Temporary mount option is good for partitions which we access occasionally. If we access partition on regular basis then this approach will not work. Each time we reboot the system all temporary mounted resources are get un-mounted automatically. So if we have a partition which is going to be used regularly, we should mount it permanently.
Mounting partitions permanently
Each resource in file system has a unique ID called UUID. When mounting a partition permanently we should use UUID instead of its name. From version 7, RHEL also uses UUID instead of device name.
The UUID stands for Universally Unique Identifier. It is a 128-bit number, expressed in hexadecimal (base 16) format.
If you have a static environment, you may use device name. But if you have dynamic environment, you should always use UUID. In dynamic environment device name may change each time when system boots. For example we attached an additional SCSI disk in system; it will be named as /dev/sdb. We mounted this disk permanently with its device name. Now suppose someone else removed this disk and attached new SCSI disk in the same slot. New disk will also be named as /dev/sdb. Since name of old disk and new disk is same, new disk will be mounted at the place of old disk. This way, device name could create a serious problem in dynamic environment. But this issue can be solved with UUID. No matter how we attach the resource with system, its UUID will remain always fix.
If you have static environment, you may consider device name to mount the partition. But if you have dynamic environment, you should always use UUID.
To know the UUID of all partitions we can use blkid command. To know the UUID of a specific partition we have to use its name as argument with this command.
Once we know the UUID, we can use it instead of device name. We can also use copy and paste option to type the UUID.
- Use blkid command with partition name to print the UUID of partition.
- Copy the UUID of partition.
- Use mount command to mount the partition. Use paste option instead of typing UUID.
Following figure illustrates above steps
When system boots, it looks in /etc/fstab file to find out the partitions which need to be mount automatically.
By default this file has entry for those partitions which were created during the installation.
To mount any additional partition automatically we have to make an entry for that partition in this file.
Each entry in this file has six fields.
|1||What to mount||Device which we want to mount. We can use device name, UUID and label in this filed to represent the device.|
|2||Where to mount||The directory in main Linux File System where we want to mount the device.|
|3||File system||File system type of device.|
|4||Options||Just like mount command we can also use supported options here to control the mount process. For this tutorial we will use default options.|
|5||Dump support||To enable dump on this device use 1. Use 0 to disable dump.|
|6||Automatic check||Whether this device should be checking while mounting or not. To disable use 0, to enable use 1 (for root partition) or 2 (for all partitions except root partition).|
Let’s make some directories to mount the partitions which we have created recently
Now open the fstab file and make entries for partitions. We can also use copy and paste operation here to type the UUID.
To use copy and paste operation, open another terminal and run blkid command. Now copy UUID of partition from second terminal
and paste in fstab file which is opened in first terminal. Once you are done close the second terminal.
As I mentioned earlier we can also use device name or label here. Let’s use device name for remaining partitions and save the file.
After saving always check the entries with mount –a command. This command will mount everything listed in /etc/fstab file. So if we made any mistake while updating this file, we will get an error as the output of this command.
If you get any error as the output of mount –a command, correct that before rebooting the system. If there is no error, reboot the system.
The df –h command is used to check the available space in all mounted partition. We can use this command to verify that all partitions are mounted correctly.
Above output confirms that all partitions are mounted correctly. We put some data in /dev/sdc1 and in /dev/sdc2 while these partitions were mounted temporary. Let’s verify that is available in respective partitions.
We have successfully finished this practice. Now it’s time to clean up the system for next practice.
How to delete partitions
We cannot delete a mounted partition. Un-mount all partitions which we created in this exercise
Run gdisk /dev/sdc command again
The d command is used to delete a partition. If there are multiple partitions, command will ask for partition number which we want to delete. Delete all partitions one by one.
As we know whatever change we made in this utility will not apply until we save them with w command. Use p command to view the changes. If everything is fine, confirm the change and exit form gdisk.
Finally remove the mount directory and copy the original fstab file back. If you haven’t taken the backup of original fstab file, remove all entries from this file which you made.
Now reboot the system and use df –h command again to verify that all partitions which we created in this exercise are gone.
Right now we have deleted all partitions from disk but this disk still contains valid GPT record with empty partition table.
We cannot create MBR partition in this disk until we convert it back to MBR and vice versa a MBR disk cannot be used for GPT partition. Remember we started this tutorial with a MBR disk. So we had to covert that disk in GPT disk. If we use this disk in next practice we will be able to create only GPT partition. To make this disk blank again we have to replace first Mb of this disk with raw data. In second part of this tutorial when we started to work with this disk we took the backup of first Mb. Let’s restore that data back to the disk.
In second part of this article we took the backup of first MB in /root/backup/mbrbackupfile.
Now this disk is reverted back in its original state. It doesn’t contain any record or partition table. This disk will appear as a blank disk in any disk management utility.
That’s all for this part. In next part we will learn how to create and manage partitions with parted command.
Full Version EX300 Dumps
Try EX300 Dumps Demo