April 24, 2019

New HOWTO: Linux Partition HOWTO - page 3

Table of Contents

  • April 5, 2001
2. Devices

There is a special nomenclature that linux uses to refer to hard drive
partitions that must be understood in order to follow the discussion on the
following pages.

In Linux, partitions are represented by device files. These are phoney files
located in /dev. Here are a few entries:
brw-rw----    1 root     disk       3,   0 May  5  1998 hda                  
brw-rw----    1 root     disk       8,   0 May  5  1998 sda                  
crw-------    1 root     tty        4,  64 May  5  1998 ttyS0                
A device file is a file with type c ( for "character" devices, devices that
do not use the buffer cache) or b (for "block" devices, which go through the
buffer cache). In Linux, all disks are represented as block devices only.

2.1. Device names

2.1.1. Naming Convention

By convention, IDE drives will be given device names /dev/hda to /dev/hdd.
The first drive is 'a' the second drive 'b' and so on. For example, /dev/hda
is the first drive on the first IDE controller and /dev/hdd is the second
drive on the second controller (the fourth IDE drive in the computer). You
can write to these devices directly (using cat or dd). However, since these
devices represent the entire disk, starting at the first block, you can
mistakenly overwrite the master boot record and the partition table, which
will render the drive unusable.

Once a drive has been partitioned, the partitions will represented as numbers
on the end of the names. For example, the second partition on the second
drive will be /dev/hdb2. SCSI drives follow a similar pattern; They are
represented by 'sd' instead of 'hd'. The first partition of the second SCSI
drive would therefore be /dev/sdb1.

Primary partitions (Section 3.3) on a disk are 1, 2, 3 and 4. Logical
partitions (Section 3.4) have numbers 5 and up, for reasons explained later (
Section 5.1.3).

2.1.2. Name Assignment

Under (Sun) Solaris and (SGI) IRIX, the device name given to a SCSI drive has
some relationship to where you plug it in. Under linux, there is only wailing
and gnashing of teeth. Lower SCSI ID numbers are assigned lower-order
letters, so if you remove one drive from the chain, the names of the higher
ID number drives will change. If you have two SCSI controllers in your linux
box, you will need to examine the output of /bin/dmesg in order to see what
name each drive was assigned. If you remove one of two controllers, the
remaining controller might have all its drives renamed. Grrr...

This is all you have to know to deal with linux disk devices. For the sake of
completeness, see Kristian's discussion of device numbers below.

2.2. Device numbers

The only important thing with a device file are its major and minor device
numbers, which are shown instead of the file size:
$ ls -l /dev/hda                                                             

Table 2. Device file attributes
|brw-rw---- |   1    |  root  |  disk  |   3,    |    0    | Jul 18 |/dev/hda|
|           |        |        |        |         |         |  1994  |        |
|permissions|        | owner  | group  |  major  |  minor  |  date  | device |
|           |        |        |        | device  | device  |        |  name  |
|           |        |        |        | number  | number  |        |        |

When accessing a device file, the major number selects which device driver is
being called to perform the input/output operation. This call is being done
with the minor number as a parameter and it is entirely up to the driver how
the minor number is being interpreted. The driver documentation usually
describes how the driver uses minor numbers. For IDE disks, this
documentation is in /usr/src/linux/Documentation/ide.txt. For SCSI disks, one
would expect such documentation in /usr/src/linux/Documentation/scsi.txt, but
it isn't there. One has to look at the driver source to be sure (/usr/src/
linux/driver/scsi/sd.c:184-196). Fortunately, there is Peter Anvin's list of
device numbers and names in /usr/src/linux/Documentation/devices.txt; see the
entries for block devices, major 3, 22, 33, 34 for IDE and major 8 for SCSI
disks. The major and minor numbers are a byte each and that is why the number
of partitions per disk is limited.

Most Popular LinuxPlanet Stories