`fdisk': the Linux partition table editor
=========================================

`fdisk' is the Linux partition table editor.  In this section we
examine this utility and try to describe it thoroughly enough so that
anyone can use it.

* Contents:

* Disks and how they are described.
* Dividing up your disk.
* The `fdisk' command.
* Deleting and adding partitions.
* Active flags and system types.
* Extra commands for experts.
* Warnings for `fdisk' users.


Disks and how they are described
--------------------------------

A typical disk consists physically of one or more circular objects
called "platters", which rotate about a central axis.  Devices called
"heads" move to specified places on the disk surface to read or write
information.  There is usually one head on each side of every platter,
and all these heads are attached to a comb-like controller arm which
moves all of them at the same time, either closer to the centre of the
disk, or closer to the outer edge.

Suppose the arm is in one position, putting an area of the disk
surface within reach of one or another of the heads.  This total area,
everything that is accessible without moving the arm, is called a
"cylinder".  (A cylinder is a barrel-shaped cross section of a disk,
consisting of a circular strip from each side of each platter.)  The
part of a cylinder that one head can read or write without moving is
called a "track".

Each track is divided into several pie-shaped slices called
"sectors", which are the smallest parts of the disk which can be read
or written at a time.  The sectors on one disk are usually all the same
size.

In fact, there are not always two heads to every platter, there are
some disks which do not have the same amount of data in every cylinder,
and there may be disks which do not have the same amount of data in
every sector.  These features are usually hidden on PCs by the
controller card or the BIOS, which map the physical geometry of a disk
onto a logical geometry, which is what is actually used to access the
disk.

The numbers which describe the "geometry" of a disk are

  1. The number of cylinders it contains.

  2. The number of tracks per cylinder, which is the number of heads.

  3. The number of sectors per track.

  4. The number of bytes per sector.

These numbers vary from disk to disk, but a typical PC disk might
have about 1000 cylinders, half a dozen heads, and 15 or 20 sectors per
track, with each sector containing 512 bytes or characters; such a disk
contains 40 to 60 megabytes of data.  A "double density" floppy disk
contains 40 cylinders, with 2 heads (2 tracks per cylinder), and with 9
sectors per track; such a disk contains 360 kilobytes, or 360 * 1024
characters.  A "high density" 3.5 inch floppy contains 80 cylinders,
with 2 heads and 18 sectors per track, or 1.44 megabytes, or 1440 *
1024 characters.

The exact size of a track or cylinder in bytes varies from one disk
to another.  This `fdisk' for Linux deals mainly with cylinders, since
this is the best unit to use when allocating space for partitions.  It
reports partition sizes in "blocks" of 1024 bytes, or 2 sectors, since
`mkswap' and the various `mkfs' programs require this number.  A block
is the smallest amount of space which can be set aside for a file in
the current file systems.

An operating system, such as Linux or DOS or OS/2, may use a disk in
any way that it wishes, but if two operating systems share the same
disk, they must agree on who owns what, or else one will interfere with
the other (that is, by damaging the other's files).  A "partition" is a
section of a hard disk which is handled as a unit by all operating
systems which can access the disk.  The standard way to define
partitions (for the moment) is the "partition table", a list of
information which is stored in parts of the disk that don't belong to
any of the systems using the disk.  The beginning of the partition
table is stored in the disk's primary boot sector, and the rest is
stored in a chain of sectors scattered throughout the disk.

The first sector on the disk is called the "primary boot block" or
"primary boot sector" because (1) it comes first, before other, similar
sectors; (2) it tells where the other, similar sectors are found, so
that it is logically `prior' to them; and (3) it usually contains code
which is executed when the system boots up.  This sector contains a
table describing at most four partitions.  These areas are called
"primary partitions".

The partition table in the primary boot sector may also describe at
most one "extended partition".  This is a large area of the disk,
usually containing all the space which is not in any primary partition.
Within this space we can set aside other areas which are called
"logical partitions", because they look almost exactly like primary
partitions.  In fact, the main difference between them is that we can
boot from primary partitions, while we cannot boot from logical
partitions.  This happens because the address of a primary partition is
in a fixed place, whereas the address of a secondary partition is not,
so we require a more complicated process to discover it, one which is
too difficult for most primary boot programs.


Dividing up your disk
---------------------

It is a good idea to plan ahead before you start creating partitions
on your disk.  If you set aside a partition for some purpose, it is not
easy to change its size: you must backup all the data from the partition,
whether to floppies, to another partition, to another hard disk, or
somewhere else; then you must edit the table which describes this
partition, so changing its size; then you must reboot and initialise
the new partition, formatting it, for example, under DOS, or running
`mkfs' under Linux; finally you can copy all the data back.  It is
possible, if you have several partitions, to copy data back and forth
between them while you change their sizes, but this is a bit risky and
time consuming.  It is better to plan ahead what you will need, since
it is hard to change it afterwards.

Many people with large disks and recent versions of DOS have their
entire file system on one large partition.  They usually ask, `Isn't
there any way I can reformat my disk without copying everything off?'
There is no way to do it using standard DOS utilities, and there is no
truly safe way to do it using commercial software, because, if you make
a mistake, you will lose the entire contents of your disk.  If you are
going to back up your disk anyway, you might as well copy the data back
safely.  The Linux FAQ contains references to tools and procedures
which will allow you to do this, if you dare.

DOS and Linux both allow you to access several partitions on a
single disk; on DOS these are treated as if they were separate disks or
drives, and under Linux they are treated as different "devices".

You can have up to 64 partitions on a single IDE disk, or up to 16
partitions on a single SCSI disk, at least as far as Linux is
concerned; in practice you will rarely want so many.  The maximum size
of a Linux file system on a single partition depends on the type of
file system you use.  Minix file systems are limited to 64 megabytes.
You may have all of your Linux files in a single partition, or you may
have two, three, or more Linux file systems.  Similarly you may have
one or more DOS partitions.  If you have several small partitions, you
run much less risk of losing all your files if your disk gets
corrupted.  On the other hand, you may run out of space on a small
partition more easily.

Under DOS, you must refer to each partition by a separate drive
letter, but all partitions are automatically accessible.  Under Linux
only the root partition is automatically accessible, but once we mount
another partition, it is indistinguishable from the rest of the file
system.  Disks are usually mounted by a command in one of the system
startup files, `/etc/rc', so you need not worry about having to do it
yourself whenever you boot the system.  But even ordinary users may
be allowed to mount removable hard disks and floppy disks.

Linux requires at least one partition, which is the `root' of the
file system.  You may prefer to have a separate partition for `/usr',
which contains most of the executable files, or for `/home', which
contains most of your private files.  You may also wish to set aside a
partition to use for swap space, depending on the amount of memory your
PC has.  You will certainly need swap space if you have less than 4 Mb
of RAM and wish to compile anything substantial.  You can reserve swap
space in a file, but you need a partition big enough to hold it, and
this will probably be less efficient than having a partition devoted to
swap.

The disk space you need for Linux is discussed in README.prepare.

Are you going to boot Linux from the hard disk, or will you boot
from a floppy?  Some boot programs place severe restrictions on where
the boot partition can be.  LILO is more relaxed about this, but does
require either the Master Boot Record on your first hard disk, or the
boot record on one of the first four partitions on your first hard disk.

If you have an extended partition with logical partitions in it, you
can have only three primary partitions containing data.


The `fdisk' command
-------------------

Every operating system, whether DOS, OS/2, or Linux, should provide
its own utility for editing hard disk partition tables.  At least four
of these utilities have been called `fdisk', for `Fixed DISK setup
program', where `fixed' means `not removable'.  I believe the first PC
program named `fdisk' came from Microsoft in about 1985; before that
time disks were too small to divide into separate sections.

Every operating system has its own peculiarities.  Normally you
should set up a partition for the use of one operating system by using
its own `fdisk' program.  Do not use the Linux `fdisk' to create
partitions for DOS or for any system other than Linux; otherwise you
may have problems.

An `fdisk' program performs two functions: it reports how the disk is
configured, and it changes that configuration by adding or deleting
partitions.  Most `fdisk' programs can also change other information in
partition tables.

This `fdisk' for Linux operates on one hard disk at a time.  If you
give the command

     fdisk

it reports on, and is able to change, `/dev/hda', the first hard
disk.  (If you have no `/dev/hda', `fdisk' uses `/dev/sda' as the
default device.) To look at or change the second hard disk, `/dev/hdb',
give the command

     fdisk /dev/hdb

To look at or change the first SCSI disk, give the command

     fdisk /dev/sda

There are some special forms of the `fdisk' command.  One of them,
suggested by Jim Winstead, simply lists all partitions on all available
disks:

     fdisk -l     (where `l' is a letter, not the digit `1')

The option `-v' is provided to list the current version of the
`fdisk' command.  Finally, there is an option `-s' which is not really
intended for interactive use.  It causes fdisk to print the size of a
partition in blocks of 1024 bytes as follows:

     fdisk -s /dev/hda7
     39934

Because this is intended to be used by `mkfs' and `mkswap' programs,
it does not return the size of extended partitions or of partitions
whose system type code is less than 10 (hexadecimal a).  If you start
`fdisk' without using one of these special options, it responds by
asking for a command:

     Command (m for help): _

Each `fdisk' command consists of a single letter, which must be
followed by <RETURN> before it is obeyed.  Upper and lower case are not
distinguished.  Anything you type after the first character is ignored.
Give the command `m', and you should see this menu:
      Command action
         a   toggle a bootable flag
         d   delete a partition
         l   list known partition types
         m   print this menu
         n   add a new partition
         p   print the partition table
         q   quit without saving changes
         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): _

The simplest commands are Print, Verify, and Quit.  On a small disk, the
Print command might produce a display like this one:

     Disk /dev/hda: 5 heads, 17 sectors, 977 cylinders
     Units = cylinders of 85 * 512 bytes
     
        Device Boot  Begin   Start     End  Blocks   Id  System
     /dev/hda1   *       1       1     236   10021+   1  DOS 12-bit FAT
     /dev/hda2         837     837     977    5992+   5  Extended
     /dev/hda3   *     237     237     836   25500   83  Linux native
     /dev/hda5         837     837     936    4249+  82  Linux swap
     /dev/hda6         942     942     977    1522    1  DOS 12-bit FAT

There are 5 partitions reported; `/dev/hda4' does not appear because
it is not allocated.  Partitions 1 and 3 are flagged as bootable.  The
size of each partition is reported in 1 kilobyte blocks; hence the
primary Linux partition, partition 3, is 25 1/2 megabytes in size.  The
`+' after three of the sizes warns that these partitions contain an odd
number of sectors: Linux normally allocates filespace in 1 kilobyte
blocks, so the extra sector in partition 5 is wasted.  Id numbers are
reported in hexadecimal and explained in English.

The display/entry units may be either cylinders or sectors.  The
default is cylinders, but changing the units makes the print command
display the following table for the system reported above:

     Disk /dev/hda: 5 heads, 17 sectors, 977 cylinders
     Units = sectors of 1 * 512 bytes
     
        Device Boot  Begin   Start     End  Blocks   Id  System
     /dev/hda1   *       1      17   20059   10021+   1  DOS 12-bit FAT
     /dev/hda2       71060   71060   83044    5992+   5  Extended
     /dev/hda3   *   20060   20060   71059   25500   83  Linux native
     /dev/hda5       71061   71061   79559    4249+  82  Linux swap
     /dev/hda6       79985   80001   83044    1522    1  DOS 12-bit FAT

The start of data in both DOS partitions is 16 sectors after the
beginning of the partition: this is one reason why you should use DOS's
own `FDISK' to create DOS partitions.  Changing the units to sectors
also affects the way in which the new partition command asks for the
beginning and end of a new partition.

*Warning*: it is dangerous to create a new partition when the
display/entry units are sectors.

The Verify command is useful because

  1. It warns you if anything is wrong.  *Always* do a Verify command
     to check your work before writing any changes to disk.

  2. It reports how many unallocated sectors there are on the disk.

The Quit command is also useful.  `fdisk' does not actually change
any data on your disk unless you give a Write command.  If you are
unhappy about any changes you may have made, give the Quit command, and
your disk will remain as it was before you ran `fdisk'.  You can also
interrupt `fdisk' with `CTRL-C'.


Deleting and adding partitions
------------------------------

Deleting a partition is simple.  Give the Delete command by typing
`d'.  `fdisk' asks:

     Partition number (1-6): _

Once you get this far, you must either delete a partition or
interrupt the program with `CTRL-C' (or whatever your current interrupt
character is).  Note:

  1. You may delete a nonexistent partition.  You will get a warning
     message.

  2. You may delete an extended partition.  This has the side effect of
     deleting all partitions greater than or equal to 5.

  3. You may delete a logical partition.  In that case, all partitions
     above it are renumbered at once.  For example, if you delete
     partition 5, then partition 6 becomes known as partition 5, and
     partition 7 as partition 6.

Adding a partition is just a bit more complicated.  Give the New
command by typing `n'.  `fdisk' allows you to

  1. Create a primary partition, if there is a free slot in the primary
     partition table.

  2. Create an extended partition if there is a free slot in the
     primary partition table, and if there is no extended partition.

  3. Create a logical partition if an extended partition exists.

If more than one of these actions is possible, you will be asked to
select Primary, Extended, or Logical, depending on what is currently
permissible.  Before you create a primary or an extended partition, you
are asked what slot it is to have in the table (1-4).

You may not add a primary or an extended partition if the selected
slot in the primary partition table is already occupied: in that case
you simply return to the main menu.  You are not allowed to add a new
primary partition unless there are sectors available outside the
extended partition.  You are not allowed to add a new logical partition
unless there are sectors available inside the extended partition.

If space is available, you are prompted for the first cylinder:

     First cylinder ([237]-977): _

The limits are the lowest and the highest cylinders in which sectors
are available in the appropriate part of the disk.  The square-bracketed
number is what you'll get if you simply press enter.  Not all numbers in
this range are necessarily available: they may fall inside an existing
partition.  If you select a cylinder which is already in use, you are
told off and prompted again for the first cylinder.  After selecting the
first cylinder, you are prompted again:

     Last cylinder or +size or +sizeM or +sizeK (237-[836]): _

The limits are the cylinder you have chosen as the first cylinder,
and the highest cylinder which contains a legitimate upper boundary for
the new partition.  The square-bracketed number is what you'll get if
you simply press enter. In other words, all numbers in the given range are
legitimate, unlike those in the first range of cylinders.  You may also
specify the size of a partition in megabytes, kilobytes, or in the
current units (cylinders or sectors).  A plus sign `+' indicates that
your answer is a size rather than a boundary, and the suffix `m' or `k'
(upper or lower case) indicates that the size is not given in units of
sectors or cyliners, but in megabytes or kilobytes respectively.  Thus
possible answers to the last cylinder request above are

700
     Make cylinder 700 the last cylinder in the partition.

+300
     Make cylinder 237 + 300 = 537 the last cylinder in the partition.

+15m
     Make the partition at least 15 megabytes in size.

+12500k
     Make the partition at least 12,500 kilobytes in size.

If you specify a size which is too large or an end which is out of
range, fdisk complains and repeats the prompt.

Adding or deleting partitions has no effect unless you subsequently
give the Write command.  Please remember to give the Verify command
first, just before giving the Write command: this is a safety
precaution.  After giving the Write command, you will see this message:

     The partition table has been altered!
     Calling ioctl() to re-read partition table.
     Syncing disks.

If there are no further messages, the kernel has successfully copied
the information from the partition table into its own internal table.
But sometimes you will see a message like this one:

     Re-read table failed with error 16: Device or resource busy.
     Reboot your system to ensure the partition table is updated.

In this case, depending on what you have changed in the partition
table, it may be dangerous to continue working without rebooting,
since you may lose or corrupt your data.


Here are some important things to note:

  1. Before you reboot, you *may* run `fdisk' again, either to manage
     another disk, or to make additional changes to the same disk, or
     just to check that the changes have been made as you expected.
     This is true even after you receive the message warning you to
     reboot.

  2. It is not a good idea to run any of the programs `mkfs', `mkswap',
     `mount', or `swapon' if you have received the warning message but
     have not rebooted.  In this case it is dangerous to run any program,
     but these in particular may cause serious damage to the data on your
     disk, including the partition tables themselves.


Active flags and system types
-----------------------------

The active flag is a bit in the partition table entry which marks a
partition as bootable.  This is important to some primary boot sector
programs, which will not boot from an unflagged partition.  Other such
programs do not allow more than one partition to be flagged.  Some,
like LILO, ignore the flags completely.  I prefer to flag all bootable
partitions as active so that they stand out on the menu which `fdisk'
lists.  Fdisk prints a star after the name of a partition's device file
if its active flag is set.

The Active command changes, or toggles, a partition's active flag.
Give the Active command, and select a partition by number.  If it was
marked inactive, it will be flagged as active; if it was flagged as
active, it will be marked inactive.  You may set the active flag on an
extended or logical partition, though the meaning of such a flag is by
no means clear.  This can be used to install LILO as a secondary boot
loader to boot a Linux which lives on a second hard disk.

The Type command changes the ID number which describes what type a
partition is.  `fdisk' currently recognises 30 system IDs, in the sense
that it prints a string for each of them, but it allows you to change
any system ID to any other, with the following exceptions: you may not
change any partition to or from the type Extended, and you may not
change a partition whose type is Empty (0) to any other type.  You may,
however, change the type of any data partition to 0, which is
equivalent to deleting it.

The new system ID or type code is a hexadecimal number.  There are
two ways of listing the numbers which `fdisk' recognises: use the List
command, which prints the list, or use the Type command, which, when it
prompts you for the code, says

     Hex code (type L to list codes): _

where the upper case `L' is used for clarity.  The codes printed are:
Some of these numbers are a trifle uncertain.  By default `fdisk' uses
a type of 83.  It used to use 81, the type code used by the MINIX
`fdisk'.  It seemed prudent to change the default since (a) many Linux
`minix' file systems are no longer compatible with MINIX, (b) the ext2
file system, a native Linux file system, is fairly stable, as is the
Xia file system, and (c) the number 81 causes problems with DR-DOS.
Linux does not usually care what values you use for type codes, but
other systems, in particular DOS, OS/2, and DR-DOS, may.

The value of 82 for Linux swap partitions is my own invention, and
is intended to give some recognisable distinction to the partitions
when the values are displayed in hexadecimal.

New active flags and new system type codes are not written to the
disk until you exit from `fdisk' with the Write command, as described
above, in the section on deleting and adding partitions.


Extra commands for experts
--------------------------

The eXtra command `x' puts `fdisk' into `expert' mode, in which a
slightly different set of commands is available.  The Active, Delete,
List, New, Type, Verify, and `eXpert' commands are not available in
expert mode.  The commands Write and Quit are available as in ordinary
mode, the Print command is available, but produces output in a slightly
different format, and of course the Menu command prints the expert
menu.  There are several new commands.

  1. The Return command brings you back to the main menu.

  2. The Extended command prints the list of table entries which point
     to other tables.  Ordinary users do not need this information.
     The data is shown as it is stored.  The same format is used for
     the expert Print command.

  3. The dangerous Begin command allows you to move the start of data
     in a partition away from its beginning.  Other systems create
     partitions with this format, and it is sometimes useful to be able
     to reproduce it.

  4. The slightly dangerous Cylinders command allows you to change the
     available number of cylinders.  For SCSI disk owners, note that we
     require not the actual number of physical cylinders, but the
     number of logical cylinders used by DOS and other operating
     systems.

  5. The extremely dangerous Heads and Sectors commands allow you to
     change the number of heads and sectors.  It should not be
     necessary to use these commands unless you have a SCSI disk, whose
     geometry Linux is not always able to determine.  SCSI disk owners
     note that we need not the actual number of heads or of sectors per
     track, but the number believed to exist by DOS and other operating
     systems.  *Warning*: If you set either of these numbers to a bad
     value, you may lose all data on your disk.

Always, after giving any of the commands Begin, Cylinder, Heads, or
Sectors, you should Return to the main menu and give the Verify command.


Warnings for `fdisk' users
--------------------------

In general, you should not use this `fdisk' program to create
partitions for other operating systems, only for Linux.  Nor should you
use `fdisk' commands from other operating systems to create partitions
for Linux.

DR-DOS 5.0 and 6.0 are reported to have difficulties with partition
ID codes of 80 or more.  The Linux `fdisk' used to set the system type
of new partitions to hexadecimal 81.  DR-DOS seems to confuse this with
hexadecimal 1, a DOS code.  The values 82 for swap and 83 for file
systems should not cause problems with DR-DOS.  If they do, you may use
the `fdisk' command `t' to change the system code of any Linux
partitions to some number less than hexadecimal 80; I suggest 42 and 43
for the moment.

Partitioning a hard disk may destroy data which is on that disk if you
are not careful.  Go slowly, write down a description of the partition
tables before you changed them, and always verify before you write.

Most operating systems and utilities expect that all partitions begin and
end at cylinder boundaries.  This version of `fdisk' does so by default,
but you can use it to create partitions which begin or end anywhere.
This does not normally affect Linux, but it is very dangerous, as other
operating systems (including DOS) may try to `correct' the partition
boundaries.

It is dangerous to create a new partition when the display/entry
units are sectors.

The Verify command warns you if anything is wrong.  *Always* give a
Verify command before writing any changes to disk.

If you set the disk geometry (tracks per cylinder, or sectors per
track) to an incorrect value, you may lose all data on your disk.

Do create BSD/SUN and/or IRIX/SGI disk labels only when you are sure
that you want them.  Both features are intended to allow you READing
those labels and prevent unintentional formatting of these disks.
