How to partition a drive using fdisk

fdisk is the Linux disk partitioning utility. It should only be ran on unmounted drives, so a Live CD or similar is advised.

To use fdisk, you must be root
su

In this instance, I am running off a CentOS 6.4 Live CD, with a populated 1.5TB drive (/sdb) with many partitions and a brand new 2.0TB drive (sda) that I indend to format similarly.

You can see what drives are available by looking in the /dev/ folder

ls -l /dev/sd*

brw-rw----. 1 root disk 8, 0 Oct 16 01:30 /dev/sda
brw-rw----. 1 root disk 8, 16 Oct 16 01:30 /dev/sdb
brw-rw----. 1 root disk 8, 17 Oct 16 01:30 /dev/sdb1
brw-rw----. 1 root disk 8, 18 Oct 16 01:30 /dev/sdb2
brw-rw----. 1 root disk 8, 19 Oct 16 01:30 /dev/sdb3
brw-rw----. 1 root disk 8, 20 Oct 16 01:30 /dev/sdb4
brw-rw----. 1 root disk 8, 21 Oct 16 01:30 /dev/sdb5
brw-rw----. 1 root disk 8, 22 Oct 16 01:30 /dev/sdb6

You can plainly see that /sda is the new drive, since it contains no partitions.

Current partitioning rules say you can only have 4 primary partitions. If you need more than 4, you will have to create one partition as an extended partition, which can then be subdivided into several logical partitions. This should become clear shortly.

The Plan

Understanding how you will use your drive now will eliminate a lot of headaches down the road. For this application, I’m going to create 3 Primary Partitions plus 1 extended, in which I will put a couple of logical partitions.

Run fdisk

fdisk /dev/sdb
blah blah blah

Command (m for help):

To view the current partitions and disk information, use p for Print

Command (m for help): p

Disk sda: 2000.4 GB, 2000398934016 bytes
255 heads, 63 sectors/track, 243201 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk identifier: 0x4f67c248

Device Boot      Start         End      Blocks   Id  System

Command (m for help):

Create Primary Partitions

If you are going to install Windows, Windows will ONLY install in the 1st partition.

To create a new partition, type ‘n’

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)

Since this will be a primary partition, press ‘p’, then ‘1’ for the first partition.

Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1 

Now we need to determine the start cylinder and size of the partition. Since this is the first, use the default of ‘1’ by pressing Enter

First cylinder (1-243201, default 1):Enter 
Using default value 1

Finally we determine the size. I’m going to make this 250G, so I’ll enter “+250G” and let the program determine my last cylinder for me

Last cylinder, +cylinders or +size{K,M,G} (1-243201, default 243201): +250G

Command (m for help):

That’s it for the first partition! Here are the entries for creating the second and 3rd, which will both be 250G as well.

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 2
First cylinder (32636-243201, default 32636): Enter 
Using default value 32636
Last cylinder, +cylinders or +size{K,M,G} (32636-243201, default 243201): +250G

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 3
First cylinder (65272-243201, default 65272): Enter
Using default value 65272
Last cylinder, +cylinders or +size{K,M,G} (65272-243201, default 243201): +250G

Lets take a peek at what our drive should look like at this point by getting a current printout:

Command (m for help): p

Disk sda: 2000.4 GB, 2000398934016 bytes
255 heads, 63 sectors/track, 243201 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk identifier: 0x4f67c248

Device Boot      Start         End      Blocks   Id  System
  sda1               1       32636   262148636+  83  Linux
Partition 1 does not start on physical sector boundary.
  sda2           32636       65272   262140640   83  Linux
  sda3           65272       97907   262148668   83  Linux

Command (m for help):

Create an Extended Partition

In order to create more than 1 more partition (we will create a Linux Swap partition, a data partition and leave some room for additional partitions at a later date), we will create an extended partition with the remaining space and then subdivide that into the remaining partitions.

This process is the same as the previous, except this time we will select e for the type and to use the remaining available space, we just need to use the default for both the first and last cylinders.

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
e
Selected partition 4
First cylinder (97907-243201, default 97907): Enter
Using default value 97907
Last cylinder, +cylinders or +size{K,M,G} (97907-243201, default 243201): Enter
Using default value 243201

Command (m for help):

Create Logical Partitions

Okay, we are finally ready to create our logical partitions. This time, the program knows we are out of partitions, so we can only create logical paritions out of the extended that we just created, so you will NOT be asked if this is primary or extended, or for a partition number!

Our swap partition will be 20G.

Command (m for help): n
First cylinder (97908-243201, default 97908): Enter
Using default value 97908
Last cylinder, +cylinders or +size{K,M,G} (97908-243201, default 243201): +20G

Command (m for help):

Our data partition will be 1TB (1000G)

Command (m for help): n 
First cylinder (100520-243201, default 100520): Enter
Using default value 100520
Last cylinder, +cylinders or +size{K,M,G} (100520-243201, default 243201): +1000G  

Command (m for help):

Lets take another look at where we stand:

Command (m for help): p

Disk sda: 2000.4 GB, 2000398934016 bytes
255 heads, 63 sectors/track, 243201 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk identifier: 0x4f67c248

Device Boot      Start         End      Blocks   Id  System
  sda1               1       32636   262148636+  83  Linux
Partition 1 does not start on physical sector boundary.
  sda2           32636       65272   262140640   83  Linux
  sda3           65272       97907   262148668   83  Linux
  sda4           97907      243201  1167074056+   5  Extended
  sda5           97908      100520    20980860+  83  Linux
Partition 5 does not start on physical sector boundary.
  sda6          100520      231063  1048586616+  83  Linux
Partition 6 does not start on physical sector boundary.

Command (m for help):

Believe it or not, there are only a few steps remaining.

Setting the Swap Partition Type

You’ll notice in the last step, all partitions (except 4) are created as type 83, or Linux partitions. Our swap partition (Partition 5) needs to be set as a ‘Swap’.

The command to set the partition type is t
In the section below, I’ve opted to List the available type codes. The type code for a swap partition is ’82’.

Command (m for help): t
Partition number (1-6): 5
Hex code (type L to list codes): L

 0  Empty           24  NEC DOS         81  Minix / old Lin bf  Solaris        
 1  FAT12           39  Plan 9          82  Linux swap / So c1  DRDOS/sec (FAT-
 2  XENIX root      3c  PartitionMagic  83  Linux           c4  DRDOS/sec (FAT-
 3  XENIX usr       40  Venix 80286     84  OS/2 hidden C:  c6  DRDOS/sec (FAT-
 4  FAT16 <32M      41  PPC PReP Boot   85  Linux extended  c7  Syrinx         
 5  Extended        42  SFS             86  NTFS volume set da  Non-FS data    
 6  FAT16           4d  QNX4.x          87  NTFS volume set db  CP/M / CTOS / .
 7  HPFS/NTFS       4e  QNX4.x 2nd part 88  Linux plaintext de  Dell Utility   
 8  AIX             4f  QNX4.x 3rd part 8e  Linux LVM       df  BootIt         
 9  AIX bootable    50  OnTrack DM      93  Amoeba          e1  DOS access     
 a  OS/2 Boot Manag 51  OnTrack DM6 Aux 94  Amoeba BBT      e3  DOS R/O        
 b  W95 FAT32       52  CP/M            9f  BSD/OS          e4  SpeedStor      
 c  W95 FAT32 (LBA) 53  OnTrack DM6 Aux a0  IBM Thinkpad hi eb  BeOS fs        
 e  W95 FAT16 (LBA) 54  OnTrackDM6      a5  FreeBSD         ee  GPT            
 f  W95 Ext'd (LBA) 55  EZ-Drive        a6  OpenBSD         ef  EFI (FAT-12/16/
10  OPUS            56  Golden Bow      a7  NeXTSTEP        f0  Linux/PA-RISC b
11  Hidden FAT12    5c  Priam Edisk     a8  Darwin UFS      f1  SpeedStor      
12  Compaq diagnost 61  SpeedStor       a9  NetBSD          f4  SpeedStor      
14  Hidden FAT16 <3 63  GNU HURD or Sys ab  Darwin boot     f2  DOS secondary  
16  Hidden FAT16    64  Novell Netware  af  HFS / HFS+      fb  VMware VMFS    
17  Hidden HPFS/NTF 65  Novell Netware  b7  BSDI fs         fc  VMware VMKCORE 
18  AST SmartSleep  70  DiskSecure Mult b8  BSDI swap       fd  Linux raid auto
1b  Hidden W95 FAT3 75  PC/IX           bb  Boot Wizard hid fe  LANstep        
1c  Hidden W95 FAT3 80  Old Minix       be  Solaris boot    ff  BBT            
1e  Hidden W95 FAT1
Hex code (type L to list codes): 82
Changed system type of partition 5 to 82 (Linux swap / Solaris)

Command (m for help):

Setting the Boot Flag

To allow a partition to function as a bootable OS, the boot flag needs to be set. We’ll set this on partitions 1, 2 and 3.

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

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

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

Command (m for help): p

Disk sda: 2000.4 GB, 2000398934016 bytes
255 heads, 63 sectors/track, 243201 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk identifier: 0x4f67c248

Device Boot      Start         End      Blocks   Id  System
  sda1   *           1       32636   262148636+  83  Linux
Partition 1 does not start on physical sector boundary.
  sda2   *       32636       65272   262140640   83  Linux
  sda3   *       65272       97907   262148668   83  Linux
  sda4           97907      243201  1167074056+   5  Extended
  sda5           97908      100520    20980860+  82  Linux swap / Solaris
Partition 5 does not start on physical sector boundary.
  sda6          100520      231063  1048586616+  83  Linux
Partition 6 does not start on physical sector boundary.
Note that in the printout above, there is now an asterisk “*” in Boot column for partitions 1,2 and 3.

Save and Exit

Until now, all settings are in memory only and NO changes have been made to the drive. To set these changes in stone, (from which there is no turning back), simply press w.

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

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

Verify Changes

Remember how we checked the available drives and partitions at the start of this tutorial? Let’s run that command again and you’ll now see the new partitions on /sda

ls -l /dev/sd*
brw-rw----. 1 root disk 8,  0 Oct 16 04:12 /dev/sda
brw-rw----. 1 root disk 8,  1 Oct 16 04:12 /dev/sda1
brw-rw----. 1 root disk 8,  2 Oct 16 04:12 /dev/sda2
brw-rw----. 1 root disk 8,  3 Oct 16 04:12 /dev/sda3
brw-rw----. 1 root disk 8,  4 Oct 16 04:12 /dev/sda4
brw-rw----. 1 root disk 8,  5 Oct 16 04:12 /dev/sda5
brw-rw----. 1 root disk 8,  6 Oct 16 04:12 /dev/sda6
brw-rw----. 1 root disk 8, 16 Oct 16 01:30 /dev/sdb
brw-rw----. 1 root disk 8, 17 Oct 16 01:30 /dev/sdb1
brw-rw----. 1 root disk 8, 18 Oct 16 01:30 /dev/sdb2
brw-rw----. 1 root disk 8, 19 Oct 16 01:30 /dev/sdb3
brw-rw----. 1 root disk 8, 20 Oct 16 01:30 /dev/sdb4
brw-rw----. 1 root disk 8, 21 Oct 16 01:30 /dev/sdb5
brw-rw----. 1 root disk 8, 22 Oct 16 01:30 /dev/sdb6

Comments

So empty here ... leave a comment!

Leave a Reply

Sidebar