RHEL7: List, create, delete partitions on MBR and GPT disks.

Share this link

Note: This is an RHCSA 7 exam objective.

Presentation

A disk can be used as a simple entity or broken up into one or more partitions.

Disks are generally called /dev/sda, /dev/sdb, etc, in physical servers (s for scsi even though they’ve got IDE, SATA or SAS interfaces) and /dev/vda, /dev/vdb, etc, in virtual machines.

Partitions get their names from the disk name itself and add a number starting at 1 (/dev/sda1, /dev/sda2, etc or /dev/vda1, /dev/vda2, etc).

A partition table is a special structure containing partitions organization.

Not recent disks use 512-byte sectors and the MBR partition table (MBR stands for Master Boot Record). This organization allows for 4 primary partitions only. If you want more than that, you need to create an extended partition (using one of the 4 primary slots), and then create logical partitions inside. More annoying, on disks with capacity greater than 2TB, space above this limit is not available.

To work around all these limitations, recent disks use 4096-byte sectors and the GPT partition table (GPT stands for GUID – Globally Unique IDentifier – Partition Table). More details are available on the GPT Wikipedia page.

Historically, two commands exist to manipulate disks and partitions: fdisk and parted.
As the fdisk command doesn’t handle GPT partition tables, it is not advisable to use it any more (for your information, some details are given at the end of this page about the fdisk command).
Recently, a new tool called gdisk has been created to deal with GPT partition tables, offering an alternative to the parted command.

Caution: In this tutorial, we are dealing with real disks. Any mistake could entirely destroy your system.

The parted Command

To start the parted command, type:

# parted
GNU Parted 2.1
Using /dev/sda
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted)

To list all the disks and partitions, type:

(parted) print all
Model: ATA Hitachi HDP72505 (scsi)
Disk /dev/sda: 500GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number  Start   End    Size   Type     File system  Flags
 1      1049kB  525MB  524MB  primary  ext4         boot
 2      525MB   500GB  500GB  primary               lvm


Model: ATA Hitachi HDP72505 (scsi)
Disk /dev/sdb: 500GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number  Start  End  Size  Type  File system  Flags


Model: Linux device-mapper (linear) (dm)
Disk /dev/mapper/vg_root-lv_root: 497GB
Sector size (logical/physical): 512B/512B
Partition Table: loop

Number  Start  End    Size   File system  Flags
 1      0.00B  497GB  497GB  ext4


Model: Linux device-mapper (linear) (dm)
Disk /dev/mapper/vg_root-lv_swap: 2147MB
Sector size (logical/physical): 512B/512B
Partition Table: loop

Number  Start  End     Size    File system     Flags
 1      0.00B  2147MB  2147MB  linux-swap(v1)

Here, we’ve got a disk called /dev/sdb without partition but with a MBR partition table (Partition Table: msdos).
To select the /dev/sdb disk, type:

(parted) select /dev/sdb
Using /dev/sdb

To create a GPT partition table on the /dev/sdb disk, type:

(parted) mktable 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? Y
(parted) print
Model: ATA Hitachi HDP72505 (scsi)
Disk /dev/sdb: 500GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt

Number  Start  End  Size  File system  Name  Flags

Note: Type mktable msdos to create a MBR partition table.

To create a primary partition with the ext4 type (here starting at 1MB and finishing at 400GB), type:

(parted) mkpart primary ext4 1MB 400GB

Note1: Specifying ext4 doesn’t format the partition in ext4, it only tags it as ext4 partition.
Note2: The partition doesn’t start at 0 but 1MB to avoid disk alignment problems.
Note3: To specify all the remaining space, use -1 as end position.
Note4: With parted, 1GB=1000MB.

Sometimes when setting up the first partition, you will get a warning: “Warning: The resulting partition is not properly aligned for best performance.
To solve this problem, type the following command where 400GB is the size of the partition (you can use 100% instead of 400GB if you want to allocate all the disk):

(parted) mkpart primary ext4 0% 400GB

To check that the first partition is correctly aligned, type:

(parted) align-check optimal 1
1 aligned

To create a swap partition with a size of 2GB (here starting at 400GB and finishing at 402GB), type:

(parted) mkpart primary linux-swap 400GB 402GB

Note1: parted checks that both partitions don’t overlap.
Note2: If, at a later stage, you want to change the type of partition, don’t drop and recreate the partition: format the partition as you want and parted will normally detect the new type.

To print the result, type:

(parted) print
Model: ATA Hitachi HDP72505 (scsi)
Disk /dev/sdb: 500GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt

Number  Start   End    Size    File system  Name     Flags
 1      1049kB  400GB  400GB   ext4         primary
 2      400GB   402GB  2000MB               primary

To set the first partition as bootable, type:

(parted) set 1 boot on
(parted) print
Model: ATA Hitachi HDP72505 (scsi)
Disk /dev/sdb: 500GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt

Number  Start   End    Size    File system  Name     Flags
 1      1049kB  400GB  400GB   ext4         primary  boot
 2      400GB   402GB  2000MB               primary

Note: Type set 1 boot off to remove the bootable flag.

To remove the swap partition (here partition number 2), type:

(parted) rm 2

To exit the parted prompt, type:

(parted) quit

To update the disk configuration seen by the kernel, type:

# partprobe /dev/sdb

The parted command can also be used for file system management. However, this usage is deprecated.

The gdisk Command

Install the gdisk package:

# yum install -y gdisk

Execute the gdisk command (here with the /dev/vda disk as parameter):

# gdisk /dev/vda
GPT fdisk (gdisk) version 0.8.6

Partition table scan:
  MBR: MBR only
  BSD: not present
  APM: not present
  GPT: not present

Type ? to display all the options:

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

Type p to print the partition table:

Command (? for help): p
Disk /dev/vda: 12582912 sectors, 6.0 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): C6F7C323-530D-40B5-A985-241A1B181354
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 12582878
Partitions will be aligned on 2048-sector boundaries
Total free space is 1318845 sectors (644.0 MiB)

Number  Start (sector)    End (sector)  Size       Code  Name
   1            2048         1026047   500.0 MiB   8300  Linux filesystem
   2         1026048        11266047   4.9 GiB     8E00  Linux LVM

Type n to create a new partition:

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

Type p to display the partition table:

Command (? for help): p
Disk /dev/vda: 12582912 sectors, 6.0 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): C6F7C323-530D-40B5-A985-241A1B181354
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 12582878
Partitions will be aligned on 2048-sector boundaries
Total free space is 1316831 sectors (643.0 MiB)

Number  Start (sector)    End (sector)  Size       Code  Name
   1            2048         1026047   500.0 MiB   8300  Linux filesystem
   2         1026048        11266047   4.9 GiB     8E00  Linux LVM
   3              34            2047   1007.0 KiB  8300  Linux filesystem

Type w to write the partition table to disk:

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/vda.
The operation has completed successfully.

To force the kernel to read the updated partition table, type:

# partprobe

Source: Sander van Vugt’s video about gdisk (5min/2014).

The fdisk Command

To list all the partitions, type:

# fdisk -l

To create a primary partition on a disk (here /dev/vda), type:

# fdisk /dev/vda

Press ‘c‘, ‘u‘, then ‘p‘ to print the partition table.
Then press ‘n‘ (for new), type the partition number (between 1 and 4), the first sector and the size.
Finally, press ‘w‘ to save the partition table.

To delete a primary partition on a disk (here /dev/vda), type:

# fdisk /dev/vda

Press ‘c‘, ‘u‘, then ‘p‘ to print the partition table.
Then press ‘d‘ (for delete) and type the partition number (between 1 and 4).
Finally, press ‘w‘ to save the partition table.

To set the type of a primary partition (here /dev/vda3), type:

# fdisk /dev/vda

Press ‘c‘, ‘u‘, then ‘p‘ to print the partition table.
Then press ‘t‘ (for tag), type the partition number (here ‘3‘) and the partition type (83 for linux, 8e for Linux LVM, 82 for swap).
Finally, press ‘w‘ to save the partition table.

To force the kernel to read the updated partition table, type:

# partprobe

Additional Resources

You can watch these videos from Ralph Nyberg about MBR vs GPT (7min/2015) and Using fdisk and gdisk (11min/2015).

(3 votes, average: 5.00 out of 5)
Loading...
22 comments on “RHEL7: List, create, delete partitions on MBR and GPT disks.
  1. mehboob says:

    Hi,
    Do we have to run restorecon -r command after creating encrypted partition in exam because in my VM system is not rebooting successfully with out running restorecon -r {path} the path where i have mounted my encrypted partition.

    • CertDepot says:

      I’m surprised. There is no encryption objective in the RHCSA and RHCE exams that I’m aware of. Even if there were, I wouldn’t see why you would need to run the restorecon command in this context.

      • mehboob says:

        Really!!!

        Are you sure there is no objective to create luks encrypted partition in RHCSA-7 exam.

        Because in two different training videos series i am having this topic.

        • CertDepot says:

          The objective existed in the RHCSA 6 exam but wasn’t in the RHCSA 7 version anymore. You can check yourself at the Red Hat website. I suppose that some trainers had already made their videos when they discovered that it was not in the curriculum anymore.

          • redhat0329 says:

            Hi CertDepot,

            When we are asked to create a disk partititon for logical volume creation on the exam, is it okay if we can choose whether fdisk or gdisk? Personally I prefer gdisk since it can extend more up to 128 partitions. I have tried it on my vm and it works fine. But please enlighten me if I’m losing track understanding the differences between the two. Thanks.

          • CertDepot says:

            I don’t think there is any problem to use gdisk instead of fdisk. While I’m sure the fdisk package is always installed whatever the kind of installation, I’m not so sure concerning the gdisk package …

          • redhat0329 says:

            Okay. Thanks

          • Gjorgi says:

            The difference between the two is very important. You absolutely should not use fdisk on GPT- partitioned disks. It will lead to very unpredictable results.

  2. Ahmad says:

    Hi ,

    is parted and gdisk tools are a part of RHCSA Exam objective. As I know, only fdisk is required for the exam.

    Thanks

    • CertDepot says:

      parted and gdisk are not a part of the RHCSA exam but you can use them to fulfill the objectives. More precisely, you can assume fdisk and parted will be available. For gdisk, you should be able to install it.

  3. Ahmad says:

    Hi

    1-If I asked to create for example 200M physical partition, should I use primary or extended partition type ?

    2-if he asked to create 200 M physical partition, does physical partition mean to use primary?

    3-if I used extended partition, for example /dev/sda3 is extended with 10 G, then I have to create new logical from the extended for the question /dev/sda4 and use it. Am I correct?

    for example:

    :n,:e,:3,:Enter,:+10G,:w — this will create 10 G extended called /dev/sda3

    so then I should create a logical partitions from the above extended one,

    for example, for the LVM question:
    :n,:l,:4,:Enter,:+3G,t 8e,:w

    and for the swap question:
    :n,:l,:5,:Enter,:+2G,t 82,:w

    Kindly advise on the below questions!

    Thanks a lot for your website and the great efforts

    • CertDepot says:

      1- Use a primary partition type if possible. Otherwise, use extended partition type 😉
      2- No, physical partition doesn’t mean primary partition: it can be primary or extended partition.
      3- Yes, you are correct.

  4. Ahmad says:

    Hi,

    Sorry for any inconvenience, but I have another question:

    1-How can I select the last sector value, and how can I check the available M or G in the device so then I can select the last sector?

    2-How can I check the device that I should use in the exam, for example /dev/sda or /dev/vda/ or /dev/sdb? If there is many of them, which one should I use and how to check which one is having available space, so then I can decide to use it.

    Thanks

    • CertDepot says:

      1- It depends on the command that you use: fdisk, parted or gdisk (the two first are installed by default).
      When using fdisk, it gives you the last available sector of the disk by default:
      # fdisk /dev/vda
      Welcome to fdisk (util-linux 2.23.2).

      Changes will remain in memory only, until you decide to write them.
      Be careful before using the write command.

      Command (m for help): p

      Disk /dev/vda: 6442 MB, 6442450944 bytes, 12582912 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 label type: dos
      Disk identifier: 0x0009288d

      Device Boot Start End Blocks Id System
      /dev/vda1 * 2048 800767 399360 83 Linux
      /dev/vda2 800768 12269567 5734400 8e Linux LVM

      Command (m for help): n
      Partition type:
      p primary (2 primary, 0 extended, 2 free)
      e extended
      Select (default p): p
      Partition number (3,4, default 3):
      First sector (12269568-12582911, default 12269568):

      2- Use the lsblk -a command to display your current disk configuration:
      # lsblk -a
      NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
      vda 252:0 0 6G 0 disk
      ├─vda1 252:1 0 390M 0 part /boot
      └─vda2 252:2 0 5.5G 0 part
      ├─rhel-swap 253:0 0 552M 0 lvm [SWAP]
      ├─rhel-root 253:1 0 3G 0 lvm /
      └─rhel-lv_test 253:3 0 100M 0 lvm /mnt

  5. Pat says:

    When I type mkpart primary linux-swap 400GB 402GB I definitely see a new partition. The type is primary there is nothing written in place of the filesystem. I thought I should see linux swap in place. Is that normal? What am I missing?

  6. Gjorgi says:

    Would lsblk command show /dev/sdb, the non partitioned attached disk?
    How would you go about freeing disk space on a system that has only one disk, without attaching anything external on that device?

    • CertDepot says:

      As it is written at the bottom of this page (http://www.pmsas.pr.gov.br/wp-content/?id=certdepot-EX200&exam=rhel-disk-tip/), you should use the command lsblk -a and not lsblk. By default, the lsblk command skips empty devices. The -a option corrects this behaviour and displays all devices, empty ones included.
      Concerning the lack of free disk space, the easiest way is to reduce or remove the swap partition. Otherwise, you will have to rearrange the existing partitions with extra care for the /boot and / partitions.

      • Gjorgi says:

        In cases where swap space isn’t big enough to satisfy the requirement, how would you go about rearranging existing partitions when the only partitions you have is xfs- formatted root and boot- without loosing data?
        Root partition holds all the volume groups and logical volumes within it.

        • CertDepot says:

          I don’t know the answer in this particular case: no available space, no way to reduce any partition.
          I also don’t think this extreme case could happen during an exam.

Leave a Reply

Upcoming Events (Local Time)

There are no events.

Follow me on Twitter

Archives

vceplus-200-125    | boson-200-125    | training-cissp    | actualtests-cissp    | techexams-cissp    | gratisexams-300-075    | pearsonitcertification-210-260    | examsboost-210-260    | examsforall-210-260    | dumps4free-210-260    | reddit-210-260    | cisexams-352-001    | itexamfox-352-001    | passguaranteed-352-001    | passeasily-352-001    | freeccnastudyguide-200-120    | gocertify-200-120    | passcerty-200-120    | certifyguide-70-980    | dumpscollection-70-980    | examcollection-70-534    | cbtnuggets-210-065    | examfiles-400-051    | passitdump-400-051    | pearsonitcertification-70-462    | anderseide-70-347    | thomas-70-533    | research-1V0-605    | topix-102-400    | certdepot-EX200    | pearsonit-640-916    | itproguru-70-533    | reddit-100-105    | channel9-70-346    | anderseide-70-346    | theiia-IIA-CIA-PART3    | certificationHP-hp0-s41    | pearsonitcertification-640-916    | anderMicrosoft-70-534    | cathMicrosoft-70-462    | examcollection-cca-500    | techexams-gcih    | mslearn-70-346    | measureup-70-486    | pass4sure-hp0-s41    | iiba-640-916    | itsecurity-sscp    | cbtnuggets-300-320    | blogged-70-486    | pass4sure-IIA-CIA-PART1    | cbtnuggets-100-101    | developerhandbook-70-486    | lpicisco-101    | mylearn-1V0-605    | tomsitpro-cism    | gnosis-101    | channel9Mic-70-534    | ipass-IIA-CIA-PART1    | forcerts-70-417    | tests-sy0-401    | ipasstheciaexam-IIA-CIA-PART3    | mostcisco-300-135    | buildazure-70-533    | cloudera-cca-500    | pdf4cert-2v0-621    | f5cisco-101    | gocertify-1z0-062    | quora-640-916    | micrcosoft-70-480    | brain2pass-70-417    | examcompass-sy0-401    | global-EX200    | iassc-ICGB    | vceplus-300-115    | quizlet-810-403    | cbtnuggets-70-697    | educationOracle-1Z0-434    | channel9-70-534    | officialcerts-400-051    | examsboost-IIA-CIA-PART1    | networktut-300-135    | teststarter-300-206    | pluralsight-70-486    | coding-70-486    | freeccna-100-101    | digitaltut-300-101    | iiba-CBAP    | virtuallymikebrown-640-916    | isaca-cism    | whizlabs-pmp    | techexams-70-980    | ciscopress-300-115    | techtarget-cism    | pearsonitcertification-300-070    | testking-2v0-621    | isacaNew-cism    | simplilearn-pmi-rmp    | simplilearn-pmp    | educationOracle-1z0-809    | education-1z0-809    | teachertube-1Z0-434    | villanovau-CBAP    | quora-300-206    | certifyguide-300-208    | cbtnuggets-100-105    | flydumps-70-417    | gratisexams-1V0-605    | ituonline-1z0-062    | techexams-cas-002    | simplilearn-70-534    | pluralsight-70-697    | theiia-IIA-CIA-PART1    | itexamtips-400-051    | pearsonitcertification-EX200    | pluralsight-70-480    | learn-hp0-s42    | giac-gpen    | mindhub-102-400    | coursesmsu-CBAP    | examsforall-2v0-621    | developerhandbook-70-487    | root-EX200    | coderanch-1z0-809    | getfreedumps-1z0-062    | comptia-cas-002    | quora-1z0-809    | boson-300-135    | killtest-2v0-621    | learncia-IIA-CIA-PART3    | computer-gcih    | universitycloudera-cca-500    | itexamrun-70-410    | certificationHPv2-hp0-s41    | certskills-100-105    | skipitnow-70-417    | gocertify-sy0-401    | prep4sure-70-417    | simplilearn-cisa    |
http://www.pmsas.pr.gov.br/wp-content/    | http://www.pmsas.pr.gov.br/wp-content/    |