Disk partitions are the basis of storing our data on disks. To be able to handle partitions, in this part of RHCSA exam preparation tutorial we will add an empty disk to our test RHEL 8 system, and create, list, and delete a new partition on it. First we will use the classic MBR (Master Boot Record) setup, then we’ll do the same on GPT (GUID Partitioning Table) setup. GPT is a more advanced partitioning technique that allows large partitions, whereas MBR is limited to 2 TB disk space per partition. So if that doesn’t seem like a very limiting size at the moment, think of the trend of disk usage, which may be not that much from the end user perspective, but certainly it is from the Linux system administration job perspective.
In this tutorial you will learn:
- How to create a new MBR partition using fdisk
- How to list classic partitions using fdisk
- How to delete partition using fdisk
- How to create GPT partition using gdisk
- How to list GPT partitions
- How to delete GPT partitions
Software Requirements and Conventions Used
Category | Requirements, Conventions or Software Version Used |
---|---|
System | Red Hat Enterprise Linux 8.0 |
Software | util-linux 2.32.1, gdisk 1.0.3 |
Other | Privileged access to your Linux system as root or via the sudo command. |
Conventions |
# – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command$ – requires given linux commands to be executed as a regular non-privileged user |
Managing partitions
Partitioning is the first step of expanding disk space that can be used by the system for storing data. We’ll cover both MBR and GPT partitioning, creating, listing, and finally deleting partitions.
If a disk already contains any data that is valuable, always create a backup before modifying partitions, just in case. While adding new partitions to empty disk space does not harm data, deleting a partition will do exactly that, destroying everything on that partition.
In our test setup, our system just received a brand new 2GB disk that is available for partitioning. It is completely raw, no data or filesystem present, so it is perfectly safe for us to change it’s partitioning at will. Our primary disk that contains the operating system is called /dev/sda
which is 15 GB of size, while the new disk is recognized by our system as /dev/sdb
. We’ll see the partitioning layout in the first step.
- To list partitions, we can use
fdisk
. We’ll do so to see our setup on start.# fdisk -l Disk /dev/sda: 15 GiB, 16106127360 bytes, 31457280 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: 0x906d1ee8 Device Boot Start End Sectors Size Id Type /dev/sda1 * 2048 2099199 2097152 1G 83 Linux /dev/sda2 2099200 31457279 29358080 14G 8e Linux LVM Disk /dev/sdb: 2 GiB, 2147483648 bytes, 4194304 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 Disk /dev/mapper/rhel-root: 12.5 GiB, 13417578496 bytes, 26206208 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 Disk /dev/mapper/rhel-swap: 1.5 GiB, 1610612736 bytes, 3145728 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
In the output we can see that we have the main disk
/dev/sda
with two partitions,/dev/sda1
and/dev/sda2
on it. We can see our new empty/dev/sdb
with no partitions yet, as well as the logical volumes the system contains. For this tutorial we’ll ignore all else and focus on the empty disk. - To create a new partition on the empty disk, we’ll provide it as argument to
fdisk
:# fdisk /dev/sdb Welcome to fdisk (util-linux 2.32.1). Changes will remain in memory only, until you decide to write them. Be careful before using the write command.
The
fdisk
utility awaits our commands. We would like to create a new partition, so we press “n”.Command (m for help): n
We are to create a primary partition, so the answer to the next question is “p”.
Partition type p primary (0 primary, 0 extended, 4 free) e extended (container for logical partitions) Select (default p): p
The next questions are about partition number, first and last sector, which will determine the actual size of the partition. In our example we are creating a single partition that will cover the disk, and default values are first partition, first available sector to start, and last sector to end with, which is just what we need. So we’ll accept the defaults for these questions.
Partition number (1-4, default 1): First sector (2048-4194303, default 2048): Last sector, +sectors or +size{K,M,G,T,P} (2048-4194303, default 4194303): Created a new partition 1 of type 'Linux' and of size 2 GiB.
We are not limited to count in sectors when we define the end of the partition. As the utility hints, we can specify an exact size. For example, if we would like a partition of 1 GB in size, at the last sector we could provide:
Last sector, +sectors or +size{K,M,G,T,P} (34-4194270, default 4194270): +1G
The partition is now complete, but as the utility points out on start, the changes are in-memory only until we write them out to the disk. This is on purpose and the warning is in place for a good reason: by writing out the changes to the disk, we destroy anything that resided on the sector range we cover with our new partition. We are sure there will be no data loss, so we write the changes to disk:
Command (m for help): w The partition table has been altered. Calling ioctl() to re-read partition table. Syncing disks.
To be sure that the operating system knows of the changes, we run
partprobe
:# partprobe
We can use the
fdisk -l
feature to be more specific by adding the device name we are interested in.# fdisk -l /dev/sdb Disk /dev/sdb: 2 GiB, 2147483648 bytes, 4194304 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: 0x29ccc11b Device Boot Start End Sectors Size Id Type /dev/sdb1 2048 4194303 4192256 2G 83 Linux
And in the output we can see that our disk now contains a new
/dev/sdb1
partition that is ready to be used. - Deleting partition is basically the same process backwards. The utility is built in a logical way: we specify the device we would like to work on, and when we select partition deletion with the “d” command, it will delete our sole partition without any question, because there is only one on the disk.
# fdisk /dev/sdb Welcome to fdisk (util-linux 2.32.1). Changes will remain in memory only, until you decide to write them. Be careful before using the write command. Command (m for help): d Selected partition 1 Partition 1 has been deleted.
While this is quite convenient, note that this tooling makes it really easy to wipe data from disk with a single keypress. This is why all the warnings are in place, you have to know what you are doing. Safeguards are still in place, nothing changes on disk until we write it out.
Command (m for help): w The partition table has been altered. Calling ioctl() to re-read partition table. Syncing disks. # partprobe # fdisk -l /dev/sdb Disk /dev/sdb: 2 GiB, 2147483648 bytes, 4194304 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: 0x29ccc11b
- To create a GPT based partition layout, we’ll use the
gdisk
(GPT fdisk) utility. Its inner working will be familiar if you are reading from the start of this tutorial.# 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): n Partition number (1-128, default 1): First sector (34-4194270, default = 2048) or {+-}size{KMGTP}: Last sector (2048-4194270, default = 4194270) 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. The operation has completed successfully.
From the point of commands we did the same, initiated creating a new partition with “n”, accepted the defaults that cover the whole disk with the new partition, then written the changes to disk. Two new warnings appear, the first is there only because we partitioned the same disk with
fdisk
earlier, which was detected bygdisk
. The last one is an additional “are you sure?” type of question, before we are allowed to finally overwrite that poor disk. - Listing GPT partitions requires the same switch to
gdisk
:# 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: 4194304 sectors, 2.0 GiB Sector size (logical/physical): 512/512 bytes Disk identifier (GUID): 3AA3331F-8056-4C3E-82F3-A67254343A05 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 4194270 Partitions will be aligned on 2048-sector boundaries Total free space is 2014 sectors (1007.0 KiB) Number Start (sector) End (sector) Size Code Name 1 2048 4194270 2.0 GiB 8300 Linux filesystem
fdisk
also recognizes the disk is partitioned:# fdisk -l /dev/sdb Disk /dev/sdb: 2 GiB, 2147483648 bytes, 4194304 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: gpt Disk identifier: 3AA3331F-8056-4C3E-82F3-A67254343A05 Device Start End Sectors Size Type /dev/sdb1 2048 4194270 4192223 2G Linux filesystem
- Deleting the GPT partition we created is done similar as in the MBR case, with the additional sanity check added:
# 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): d Using 1 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. The operation has completed successfully.
Listing the disk now shows that we indeed deleted the GPT partition from the disk.
# 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: 4194304 sectors, 2.0 GiB Sector size (logical/physical): 512/512 bytes Disk identifier (GUID): 3AA3331F-8056-4C3E-82F3-A67254343A05 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 4194270 Partitions will be aligned on 2048-sector boundaries Total free space is 4194237 sectors (2.0 GiB) Number Start (sector) End (sector) Size Code Name
Exercises
- Create multiple partitions on the same disk. Verify their capacity and device name.
- Create partition on a GPT disk with
fdisk
. Does your version handle both types? - Test the safety belts built in to
fdisk
andgdisk
utilites. Try to specify sectors out of range, larger first sector than last, etc.