This tutorial explains how to manage MBR disk partition (Primary, Extended and Logical) and GPT disk partition in Linux from fdisk command with practical examples. Learn how to create, list, format, mount (temporary and permanent), remove and delete MBR & GPT partitions step by step in detail.
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 second 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 third part of this article. This part explains how to create GPT (GUID partition table) partitions from gdisk 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.
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 available.
To figure out which disk we added recently check its sequence number. Recently added disk will be the last disk in sequence
(sda, sdb and sdc). In our example it is /dev/sdc. We will use this disk for the practice.
Backup before start
We should always take the backup of associate files before making any system change. Backup copy allows us to revert in original state if anything goes wrong. Run following commands to create a backup copy of files which will change in this practice.
In first command we created a directory to store the backup files.
In second command we took the backup of first megabyte of raw blocks of /dev/sdc hard disk. Let’s understand this command in detail.
dd :- The dd (disk duplicator) command is used to copy or backup the low level data.
if=/dev/sdc :- This is the first option. This option accepts source location as argument.
In this example I copied from /dev/sdc disk. Change the disk name with the disk name which you want to use for the practice.
of=/root/backup/mbrbackupfile :- This is the second option.
This option accepts destination location as argument. In this example I copied at /root/backup/mbrbackupfile.
The mbrbackupfile is the name of file. You can use any descriptive name here.
bs=1 :- This is the third option. It is used to define the size of block. 1M stands for one megabyte.
count=1 :- This is the last option. It is used to define the number of block which we want to copy.
In third command we took the backup of /etc/fstab file. This file contains partitions information. This information is used to mount the file system while system boots.
Now we are ready to create the MBR partitions in /dev/sdc disk.
Creating MBR Partitions
Let’s initiate fdisk command. This command needs the name of disk where we want to create the partition as its argument.
There are two messages listed before the fdisk command prompt.
Changes will remain in memory only, until you decide to write them. Be careful before using the write command.
This massage says whatever change we will make here will not apply until we confirm the change with write command. If you make any mistake in this process, exit from fdisk utility without running write command and nothing will change in hard disk.
Device does not contain a recognized partition table building a new DOS disklabel with disk identifier 0x7a53c114.
This message says currently there is not partition table available in this disk. This is true because we are working with a new hard disk. If hard disk already contains partitions, they will be listed here.
We can also use p command to get the overview of hard disk any time in this utility.
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 a new partition. As we can see in above output currently no value is available in partition section which means there is no partition available and entire disk space is available for partition.
Type n and press Enter key to create new partition
Type p and press Enter key.
As we know in MBR partition scheme we are allowed to create maximum four primary partitions.
If more partitions are required, we have to use one primary partition as extended partition.
Extended partition allows us to create 15 logical partitions.
To learn more about MBR and GPT partition scheme, see the first part of this tutorial which explains these 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, please see the first part of this article.)
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
We have created three primary partitions from maximum allowed (four) partitions. Now we can create one more primary partition or can create one extended partition which can be used to create further logical partitions.
Let’s create one allowed extended partition. Since extended partition is used to create all allowed logical partitions, we should allocate the maximum disk space in this partition. We will assign 1GiB disk space in this partition.
At this point we have created all allowed primary partitions. If we use n command again, command mode will automatically set
to logical partition (only if one primary partition is created as extended).
We are allowed to create up to 15 logical partitions in extended partition. Since all logical partitions will be created in extended partition, the sum (of size) of all logical partitions cannot go beyond the extended partition. In this example we created an extended partition of 1GiB, so we can create logical partitions up to 1GiB. To understand it more clearly let’s create three logical partitions; first partition of 500MiB, second partition of 500MiB and third partition of remaining size.
We have used all space of extended partition in three logical partitions. So even we are allowed to create up to 15 logical partitions and free space is available outside the extended partition we cannot create the fourth logical partition. To create fourth logical partition we need free space inside the extended partition not outside the partition. To see it in action let’s try to create fourth logical partition.
As above output shows there is no free space available for fourth logical partition. So far we have created all possible partitions including primary, extended and logical partitions. Let’s have a overview of partitions.
As above output shows there are total 4194304 sectors available in hard disk. From which we used only 2713599 sectors in partitions. 1480705 sectors are still available for partitions but we cannot use them. MBR partition scheme allows us to create only four primary partitions including one extended partition which we have already created.
At first sight this limitation may look alarming, yep it’s an issue but not as much as it looks like. We are allowed to use 2TiB space in each partition. It means 2TiB x 4 = 8TiB hard disk can easily be used with MBR partition scheme.
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 type l and press Enter key.
As we can see in above output Linux supports several kinds of partitions type. Following table lists some important Linux partition types.
|Partition system Identifier||Description|
|82||Linux swap partition|
|83||Linux Standard partition (Default partition )|
|85||Linux extended partition|
|88||Linux plain text partition|
|8e||Linux LVM partition|
|fd||Linux RAID partition|
Unless we change the type of any partition, it is marked as Linux Standard partition. But if we want to change it, we can also do the same here. In next part of this tutorial we will learn how to change the partition type, for this part let’s keep the default.
When we started the fdisk utility, we got a message indicating that nothing will be saved on hard disk until we confirm the action.
To accept this partition table type w and press Enter key
If you are working with a disk which contains mounted partitions, you may get following warning message.
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 fdisk 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 can use partprobe command which will force the kernel to reread the partition table.
We did not get this warning message because we used a disk which does not contain any mounted partition. Whether we get above message or not we should always 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
Since extended partition is only used to store the logical partitions we cannot create file system in it.
Logical partitions are used to store the data. We have to create the file system in them.
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 the 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 will be used 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 helpful. Each time we reboot the system all temporary mounted resources are get un-mounted automatically. So if you have a partition which is going to be used regularly, 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 boot. 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 solve 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 the dump on this device use 1. Use 0 to disable the dump.|
|6||Automatic check||Whether this device should be checked 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 and save the file.
For demonstration purpose I used both device name and UUID to mount the partitions. 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 partitions. We can use this command to verify that all partitions are mounted correctly.
Above output confirms that we have successfully mounted all partitions. 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 fdisk /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. Let’s save the change and exit from fdisk.
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.
That’s all for this part. In next part we will learn how to create and manage GPT partitions.
Full Version EX300 Dumps
Try EX300 Dumps Demo