Disk Partitioning With fdisk

So, you added a new disk to your dedicated server. That’s great! Before you can really utilize it you’ll want to partition it out so that you can create file systems on it. There are several utilities available to help you with disk partitioning, but the single utility that you can be sure to find on most any Linux server is fdisk. Read on to find out how to use fdisk to partition your new drive.

WARNING: Just to be clear, the use of fdisk is somewhat dangerous. It’s very important to, among other things, ensure that you’re editing the proper block device. Editing the partition table of a disk that is in use will most certainly result in data loss.

If you don’t know the block device that correlates to your new disk, you can find out by issuing the following command:

fdisk -l | less

That will list out the partitioning scheme of every known block device on your server. You will want to note the name of the block device (which will probably be something to the effect of /dev/sda, /dev/sdb, et cetera) that doesn’t contain any partition information. You can then start up fdisk to partition this new disk with the following command, replacing “NEWDISK” with the device name that you just discovered:

fdisk NEWDISK

In terms of disk partitioning, fdisk is really just about as low-level as you can find. It uses a command-driven interface to help through the process, but it can honestly still be a bit confusing to use. First things first, let’s start up fdisk:

[root@som-00] ~ >> fdisk /dev/storage/testlv
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel. Changes will remain in memory only,
until you decide to write them. After that, of course, the previous
content won't be recoverable.

The number of cylinders for this disk is set to 1044.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
(e.g., DOS FDISK, OS/2 FDISK)
Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)
Command (m for help):

As you can see, this is a completely fresh disk, so fresh that it doesn’t have a partition table at all. That’s nothing to worry about because we are about to create one. The command prompt will always let you know if there is more help available at any given point. Right now, since there is no other context, issuing the ‘m’ command will get you the full command list:

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)

Assuming that the only thing we really want to do is to create partitions, we’re going to stick with the ‘n’ command.

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

Now we have a decision to make: do we want to create a primary partition, or do we want to create an extended partition? Typically, unless you need more than four partitions, you’ll want to create nothing but primary partitions. If you need more than four partitions, you’ll want to make your fourth partition an extended partition that encompasses the remaining unused space on the disk. Since this is the first partition, we’ll make it primary.

p
Partition number (1-4):

Typically speaking, you’ll want to create the partitions in numerical order. There doesn’t appear to be any problem with creating them out of order, but the best practice is to avoid doing so.

Partition number (1-4): 1
First cylinder (1-13054, default 1):

There is rarely a reason to stray from the default first cylinder, so we’ll just hit enter here.

Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-13054, default 13054):

This step is somewhat important, as this is where you specify the amount of space that the new partition will use. As you can see, we can specify this via cylinder (which is pretty difficult to figure out), size in bytes, size in megabytes, size in kilobytes, or the default: give this partition the entirety of the free space on the disk. My disk is a 100GB device, and I know that I only really want to specify 10GB of that for the first partition, so I’ll tell it to use 10240MB (1GB is roughly equivalent to 1024MB).

Last cylinder or +size or +sizeM or +sizeK (1-13054, default 13054): +10240M
Command (m for help):

The first partition has been created and fdisk is ready for a completely new command. Let’s take a look at the partition configuration so far. Use the ‘p’ command to print the config.

Command (m for help): p
Disk /dev/storage/testlv: 107.3 GB, 107374182400 bytes
255 heads, 63 sectors/track, 13054 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/storage/testlv1 1 1246 10008463+ 83 Linux

As you can see fdisk automatically set the partition type to “Linux.” That should be a fine enough default for most, but if you need a specific partition type you can change it with the ‘t’ command from the main command prompt.

Let’s make one more partition that uses the remainder of the disk then print out the partition table again to make sure that everything looks good.

Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 2
First cylinder (1247-13054, default 1247):
Using default value 1247
Last cylinder or +size or +sizeM or +sizeK (1247-13054, default 13054):
Using default value 13054

Command (m for help): p

Disk /dev/storage/testlv: 107.3 GB, 107374182400 bytes
255 heads, 63 sectors/track, 13054 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/storage/testlv1 1 1246 10008463+ 83 Linux
/dev/storage/testlv2 1247 13054 94847760 83 Linux

Everything looks good so now we’ll write out the configuration to the disk, which will also exit the program.

Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
WARNING: Re-reading the partition table failed with error 22: Invalid argument.
The kernel still uses the old table.
The new table will be used at the next reboot.

Syncing disks.
[root@som-00] ~ >>

The warning message that you saw (shown above in red) is somewhat important. Occasionally you will need to reboot after partitioning a disk. If you do not receive this warning when you do this on your own then you should have nothing to worry about.

You are now ready to do some basic disc partitioning using fdisk!

===

Liquid Web’s Heroic Support is always available to assist customers with this or any other issue. If you need our assistance please contact us:
Toll Free 1.800.580.4985
International 517.322.0434
support@liquidweb.com
https://manage.liquidweb.com/

Be Sociable, Share!
Here's $75, Launch a New VPS Today. Find out why 30,000 customers have chosen our Best-in-Class Performance & 24x7 Heroic Support.