8i | 9i | 10g | 11g | 12c | 13c | 18c | 19c | 21c | 23c | Misc | PL/SQL | SQL | RAC | WebLogic | Linux

Home » Articles » Linux » Here

Linux Disk Partitioning (fdisk, parted)

This article discusses the disc partitioning utilities available Linux, with specific reference to the information needed for the RHCSA EX200 and RHCE EX300 certification exams.

Remember, the exams are hands-on, so it doesn't matter which method you use to achieve the result, so long as the end product is correct.

This article does not cover the Logical Volume Manager (LVM). That will be covered in a separate article.

My test VM originally had a single virtual SCSI disk that was partitioned during the installation of OL6.2 (a RHEL6 clone). Checking the SCSI devices gave the following.

# ls /dev/sd*
/dev/sda  /dev/sda1  /dev/sda2
# 

After adding a new virtual SCSI disk, the unpartitioned "/dev/sdb" device was visible.

# ls /dev/sd*
/dev/sda  /dev/sda1  /dev/sda2  /dev/sdb
# 

This is the starting point for the examples in this article.

Related articles.

Disk Druid

For many servers, the only time you will need to consider partitioning is during the installation process. When doing an interactive installation the partitioning will be done using Disk Druid. It's a very simple GUI, so it doesn't need much by way of explanation.

Disk Druid

In many cases, the default partitioning scheme is fine, but it's always worth checking.

Once an interactive installation is complete, the installation choices, including the disk partitioning choices, are saved to a kickstart configuration file called "/root/anaconda-ks.cfg". This can be used to perform silent installations using kickstart.

fdisk

The fdisk utility could be considered the classic disk partitioning tool as it is available on several operating systems.

The utility is called from the "root" user, passing the disk device as a parameter. In RHEL6/OL6 you are immediately presented with a warning message.

# fdisk /dev/sdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x50af35a4.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

Command (m for help): c
DOS Compatibility flag is not set

Command (m for help): u
Changing display/entry units to sectors

Command (m for help):

From the above output you can see I have followed the advice of the warning and issued the "c" and "u" options, although this is not mandatory. To simplify the output, in the following examples I will not include the warning message and the setting of the "c" and "u" options, but they are present.

Issuing the "m" command produces a list of the available options.

Command (m for help): m
Command action
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition
   l   list known partition types
   m   print this menu
   n   add a new partition
   o   create a new empty DOS partition table
   p   print the partition table
   q   quit without saving changes
   s   create a new empty Sun disklabel
   t   change a partition's system id
   u   change display/entry units
   v   verify the partition table
   w   write table to disk and exit
   x   extra functionality (experts only)

Command (m for help):

If at any point you make a mistake and don't want to write the partition table to disk, simply use the "q" option to exit without saving.

Create a Partition

To create a new partition use the "n" command. The following output shows the creation of a primary partition using the whole disk. The choices are highlighted in bold.

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First sector (2048-20971519, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-20971519, default 20971519):
Using default value 20971519

Command (m for help): p

Disk /dev/sdb: 10.7 GB, 10737418240 bytes
222 heads, 30 sectors/track, 3148 cylinders, total 20971520 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 identifier: 0x50af35a4

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048    20971519    10484736   83  Linux

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
#

The above output shows we made the following choices:

The new partition is now visible from the OS.

# ls /dev/sd*
/dev/sda  /dev/sda1  /dev/sda2  /dev/sdb  /dev/sdb1
# 

Delete a Partition

To delete a partition use the "d" command. The following output shows the deletion of the partition we just created.

# fdisk /dev/sdb
Command (m for help): p

Disk /dev/sdb: 10.7 GB, 10737418240 bytes
195 heads, 20 sectors/track, 5377 cylinders, total 20971520 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 identifier: 0x50af35a4

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048    20971519    10484736   83  Linux

Command (m for help): d
Selected partition 1

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
#

The call to "p" is not necessary, but it is worth checking what partitions are present before attempting to delete them. In this case, as we only have a single partition, it was chosen by default when we selected the "d" command. If there were more than one we would have been prompted to select a partition number. After writing the changes to disk, the partition is no longer visible from the OS.

# ls /dev/sd*
/dev/sda  /dev/sda1  /dev/sda2  /dev/sdb
# 

Boot Partitions

Boot partitions are created in a similar way to regular partitions. Typically you would only expect to create a boot partition during installation, so you will probably do this using Disk Druid or Kickstart.

If you do use fdisk to create a boot partition, the typical size for Linux boot partitions is 500M and remember to flip the bootable flag using the "a" command before writing the partition table to disk. The output below shows the creation of a boot partition. The options are highlighted in bold. Notice the boot flag in the output from the "p" command.

# fdisk /dev/sdb
Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First sector (2048-20971519, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-20971519, default 20971519): +500M

Command (m for help): a
Partition number (1-4): 1

Command (m for help): p

Disk /dev/sdb: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders, total 20971520 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 identifier: 0x50af35a4

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1   *        2048     1026047      512000   83  Linux

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
# 

Swap Partitions

Swap partitions are created in a similar way to regular partitions, but before writing the partition table to disk, you must use the "t" command to change the partition's system id to "82" (Linux Swap). The default system id is "83" (Linux). The following output shows the creation of a 2G swap partition as the second partition on the disk.

# fdisk /dev/sdb
Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 2
First sector (1026048-20971519, default 1026048):
Using default value 1026048
Last sector, +sectors or +size{K,M,G} (1026048-20971519, default 20971519): +2G

Command (m for help): t
Partition number (1-4): 2
Hex code (type L to list codes): 82
Changed system type of partition 2 to 82 (Linux swap / Solaris)

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
[root@rhce1 ~]#

LVM Partitions

Creating partitions for use with the Logical Volume Manager (LVM) is similar to creating swap partitions, except you set the system id of the partition to "8e" (Linux LVM). The Logical Volume Manager will be covered in a separate article.

parted

The parted utility is an alternative to fdisk. It can perform all the operations of fdisk, but it has additional functionality, such as resizing and copying partitions and creating file systems. The utility is called from the "root" user, passing the disk device as a parameter. Typing help at the prompt lists the commands available.

# parted /dev/sdb
GNU Parted 2.1
Using /dev/sdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) help
  align-check TYPE N                        check partition N for TYPE(min|opt)
        alignment
  check NUMBER                             do a simple check on the file system
  cp [FROM-DEVICE] FROM-NUMBER TO-NUMBER   copy file system to another partition
  help [COMMAND]                           print general help, or help on
        COMMAND
  mklabel,mktable LABEL-TYPE               create a new disklabel (partition
        table)
  mkfs NUMBER FS-TYPE                      make a FS-TYPE file system on
        partition NUMBER
  mkpart PART-TYPE [FS-TYPE] START END     make a partition
  mkpartfs PART-TYPE FS-TYPE START END     make a partition with a file system
  move NUMBER START END                    move partition NUMBER
  name NUMBER NAME                         name partition NUMBER as NAME
  print [devices|free|list,all|NUMBER]     display the partition table,
        available devices, free space, all found partitions, or a particular
        partition
  quit                                     exit program
  rescue START END                         rescue a lost partition near START
        and END
  resize NUMBER START END                  resize partition NUMBER and its file
        system
  rm NUMBER                                delete partition NUMBER
  select DEVICE                            choose the device to edit
  set NUMBER FLAG STATE                    change the FLAG on partition NUMBER
  toggle [NUMBER [FLAG]]                   toggle the state of FLAG on partition
        NUMBER
  unit UNIT                                set the default unit to UNIT
  version                                  display the version number and
        copyright information of GNU Parted
(parted)

For additional information on a specific command, type help followed by the command.

(parted) help mklabel
  mklabel,mktable LABEL-TYPE               create a new disklabel (partition
        table)

	LABEL-TYPE is one of: aix, amiga, bsd, dvh, gpt, mac, msdos, pc98, sun,
        loop
(parted)

When working with a new disk, the first thing we need to do is label the disk using the mklabel command displayed above. Oddly enough, we have to use the label type of "msdos" for Linux partitions.

(parted) mklabel msdos
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? Yes
(parted)

To check the free space on the drive using the print free command.

(parted) print free
Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sdb: 10.7GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number  Start   End     Size    Type  File system  Flags
        32.3kB  10.7GB  10.7GB        Free Space

(parted)

Create a Partition

To create a new partition use the mkpart command. If the FS-TYPE parameter is specified, the system ID of the partition is set accordingly, but no file system is created. The following example uses mkpart to create a 2G partition with no file system on it.

(parted) mkpart
Partition type?  primary/extended? primary
File system type?  [ext2]? ext4
Start? 1MB
End? 2GB
(parted) print free
Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sdb: 10.7GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number  Start   End     Size    Type     File system  Flags
        32.3kB  1049kB  1016kB           Free Space
 1      1049kB  2000MB  1999MB  primary
        2000MB  10.7GB  8738MB           Free Space

(parted)

Notice the partition starts at "1MB". This is to leave space for the master boot record.

The mkpartfs command allows you to create a new partition with a file system, but when you try to use it you get a warning that the file system creation is not robust. For this reason, it should probably be avoided.

(parted) mkpartfs
WARNING: you are attempting to use parted to operate on (mkpartfs) a file system.
parted's file system manipulation code is not as robust as what you'll find in
dedicated, file-system-specific packages like e2fsprogs.  We recommend
you use parted only to manipulate partition tables, whenever possible.
Support for performing most operations on most types of file systems
will be removed in an upcoming release.
Partition type?  primary/extended?

Delete a Partition

The rm command is used to delete a partition. It accepts the partition number as a parameter. The following example deletes the partition we just created.

(parted) print free
Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sdb: 10.7GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number  Start   End     Size    Type     File system  Flags
        32.3kB  1049kB  1016kB           Free Space
 1      1049kB  2000MB  1999MB  primary
        2000MB  10.7GB  8738MB           Free Space

(parted) rm 1
(parted) print free
Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sdb: 10.7GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number  Start   End     Size    Type  File system  Flags
        32.3kB  10.7GB  10.7GB        Free Space

(parted)

Boot Partitions

Boot partitions are created in a similar manner to regular partitions, but the boot flag is set using the toggle or set command after the partition is created, as shown below.

(parted) mkpart
Partition type?  primary/extended? primary
File system type?  [ext2]? ext4
Start? 1MG
End? 500MB
(parted) toggle 1 boot
(parted) print free
Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sdb: 10.7GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number  Start   End     Size    Type     File system  Flags
        32.3kB  1049kB  1016kB           Free Space
 1      1049kB  500MB   499MB   primary               boot
        500MB   10.7GB  10.2GB           Free Space

(parted)

Swap Partitions

Swap partitions are created in a similar manner to regular partitions, but the file system type is set to linux-swap. The example below creates a 2G swap partition after the boot partition created previously.

(parted) mkpart
Partition type?  primary/extended? primary
File system type?  [ext2]? linux-swap
Start? 500MB
End? 2.5GB
(parted) print free
Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sdb: 10.7GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number  Start   End     Size    Type     File system  Flags
        32.3kB  1049kB  1016kB           Free Space
 1      1049kB  500MB   499MB   primary               boot
 2      500MB   2500MB  2000MB  primary
        2500MB  10.7GB  8238MB           Free Space

(parted)

LVM Partitions

LVM partitions are created in a similar manner to regular partitions, but lvm flag is set using the toggle or set command after the partition is created, as shown below.

(parted) mkpart
Partition type?  primary/extended? primary
File system type?  [ext2]? ext4
Start? 2.5GB
End? 10.7GB
(parted) toggle 3 lvm
(parted) print free
Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sdb: 10.7GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number  Start   End     Size    Type     File system  Flags
        32.3kB  1049kB  1016kB           Free Space
 1      1049kB  500MB   499MB   primary               boot
 2      500MB   2500MB  2000MB  primary
 3      2500MB  10.7GB  8238MB  primary               lvm

(parted)

Disk Utility

The "Disk Utility" application is a GUI disk management tool in the "gnome-disk-utility" package, available from the console menu (Applications > System Tools > Disk Utility). Alternatively it can be started by running the "palimpsest" command as the "root" user. Clicking on a disk on interest in the "Storage Devices" pane causes the information about that disk to be loaded in the right-hand pane.

Disk Utility

In addition to information, it also displays possible administrative tasks. Clicking on the "+ Create Partition" button allows you to specify the size, file system type and volume name for a new partition.

Disk Utility - Create Partition

Once the partition is created and formatted, it is visible in the previous screen. Clicking on the partition highlights it and displays the possible administrative tasks available for the partition.

Disk Utility - Partition Admin

To make a partition bootable, highlight it and click the "Edit Partition" button. On the resulting dialog, check the "Bootable" checkbox and click the "Apply" button.

Disk Utility - Partition Bootable

Clicking on the free space allows you to add more partitions.

For more information see:

Hope this helps. Regards Tim...

Back to the Top.