fdisk
Linux Commands - Managing Drivers and Formats

fdisk in Linux is a tool to partition hard drives. The ability to modify the physical disk partition is a tool that will be valuable in any Linux situation. Do not mistake this fdisk for the DOS version; they are not the same.

Use this command to list your drives:

 

fdisk -l

 

Partioning Using fdisk

Start fdisk - When you want to start fdisk it is important that you know what disk on your computer you would like to partition. This is especially important if you have multiple disks because you may end up partitioning the wrong drive. When you are at the command prompt, type:

fdisk

You will see the following:


Usage: fdisk [-l] [-b SSZ] [-u] device

E.g.: fdisk /dev/hda

: fdisk /dev/hdb

: fdisk /dev/sdc

etc.


If you are partitioning the first hard drive on the primary controller it is /dev/hda. The second drive on the primary controller is /dev/hdb and so on.


Basic fdisk Commands


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

0 create a new empty DOS partition table

p print the partition table

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)


These are the commands you will need to know and will benefit you when you actually begin to use fdisk.


option p (print) the current partition table entries


A good place to start once you have entered fdisk is to view what the current partition table is by using the p option:


This example is a good example of the print option. Notice that this hard drive had one drive on the primary controller, hda, and that drive is partitioned three times, one being a Linux swap.


Creating a Partition in fdisk Summary


Step #1: Print the partition entries - see what is there before you begin the process.

It is always a good idea to verify that what you are about to destroy is what you want to destroy. No doubt about it, if you partition it all information will be gone.


Step #2: Delete unwanted partitions.

Once you have verified that you want to get rid of the existing partitions, use the d option to delete them.


Step #3: Create new partitions.

Before you create new partitions it is important you ask yourself what you want to do on the drive, is it a workstation or server, each will have different needs. Use the n option to create the new partitions.


Step #4: Verify partition types.

View the partition types that you may need with the l option and then check that each of your partitions are the correct type.


Step #5: Write the information to disk.

Nothing has changed on the drive until you use the w option, then all the information you have created will be written to the drive.



Step #1 Detail:

By using the command fdisk -l you will be able to see what partitions are actually on the hard drives. The command p once you enter fdisk will provide the same information.



Once you view the partitions that exist on the hard drives, you will need to make some decisions about which partitions you would like to keep and which ones you would like to delete. This may be especially important if you are going to dual boot your computer and you need to keep your Windows partitions.



Step 2: Delete Unwanted Partitions


The command for deleteing a partition is d. You can see in the example you will use the d command and then you will need to choose which partition you want to delete. In this example there are only 3 partitions; however, you see that partition numbers 1-4 are listed. If you would try to delete 4 it would return that this is an "empty partition", in other words it doesn't exist.


Once you have deleted your partitions you will need to write the partition table for it to become active. If you quit before you write to the table nothing will happen. Write to the partition table with w.


Step 3: Create New Partitions


You need to figure out what you would like to do with your partitions before you start randomly creating partitions. The workstation example gives you a basic idea of what you might do with a 5 GB hard drive for a workstation and a 6 GB drive for a server(basic).


Workstation:

/ 256M

/boot 256M

/home 1000M

/usr 3000M

/tmp 100M

/var 100M

swap 2x the RAM on the machine


The Server installation may require a larger /var for logs and a larger /home for users. This is just a basic example.


Server:

/ 256M

/boot 256M

/home 2000M

/usr 3000M

/tmp 300M

/var 1000M

swap 2x the RAM on the machine


Once you have decided what partitions you would like to create you are ready to create new partitions. Use the n command in fdisk for a new partition. Once you have done this you will need to decide if it is an extended partition you will create or a primary partition. Let's assume this drive has no partitions at all. You will start with 3 primary partitions first and then the 4th primary partition will actually be an extended partition if you would like to create more than 4 partitions, which you would if you are following our previous examples..


Server:

/ 256M

/boot 256M

/home 2000M

/usr 3000M

/tmp 300M

/var 1000M

swap 2x the RAM on the machine


Workstation:

/ 256M

/boot 256M

/home 1000M

/usr 3000M

/tmp 100M

/var 100M

swap 2x the RAM on the machine



Both of these examples will create 7 partitions: 3 primary (/ /boot and /home) and 4 extended (/usr /tmp /var swap). Actually the first extended partition will be a partition that encompasses all of the drive space after the first 3 primary partitions. This is because you will need to slice up the extended partition into all of the smaller extended partitions.


The first thing you need to do to create a partition is decide on the size of the partition and what that partition will become. If you have decided that the first partition will be /boot and it will be 256M then you would follow this procedure.


Create a new partition by choosing n.


Choose primary partition by choosing p and the number 1 when asked because it will be the first partition. The example is creating the 4 partition so you would need to place a 1 instead of a 4.You will be asked which cylinder you would like to start your partition on, choose the default by hitting return. Now you will need to choose the last cylinder, this specifies the size of your partition. Notice in the example that +250M is used. This will create a partition size of 256 Megabytes. If you want 250 M you would enter +250M. Create all of your partitions and then write the partition table with the w command.


Once your partitions are created you are still not done. You need to make sure they are the correct type. You can view the type options by entering fdisk and using the l command to view the types of partitions. You will need to use type 83 for Linux and type 82 for Linux swap. As you look at your partitions you will find that they are already type 83, so you will need to change your swap partition to 82. This is easily accomplished by using the t command which will allow you to change types.


Once you issue the t command you will need to choose which partition you want to make the swap partition. Then you can change it by entering the code, see the example.


Make sure you write this information to the partition table with the w command.


Your partitions should now be complete.