Previous Index Next

Device numbers and device names

In Linux, partitions are represented by device files. 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. Unlike other Unices, Linux does not offer "raw" character versions of disks and their partitions.

The only important thing with a device file are its major and minor device number, shown instead of the files size:


$ ls -l /dev/hda
brw-rw----   1 root     disk       3,   0 Jul 18  1994 /dev/hda
                                   ^    ^
                                   |    minor device number
                                   major device 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.
device files have certain names and many system programs have knowledge about these names compiled in. They expect your IDE disks to be named /dev/hd* and your SCSI disks to be named /dev/sd*. Disks are numbered a, b, c and so on, so /dev/hda is your first IDE disk and /dev/sda is your first SCSI disk. Both devices represent entire disks, starting at block one. Writing to these devices with the wrong tools will destroy the master boot loader and partition table on these disks, rendering all data on this disk unusable or making your system unbootable. Know what you are doing and, again, back up before you do it.

Primary partitions on a disk are 1, 2, 3 and 4. So /dev/hda1 is the first primary partition on the first IDE disk and so on. Logical partitions have numbers 5 and up, so /dev/sdb5 is the first logical partition on the second SCSI disk.

Each partition entry has a starting and an ending block address assigned to it and a type. The type is a numerical code (a byte) which designates a particular partition to a certain type of operating system. For the benefit of computing consultants partition type codes are not really unique, so there is always the probability of two operating systems using the same type code.

Linux reserves the type code 0x82 for swap partitions and 0x83 for "native" file systems (that's ext2 for almost all of you). The once popular, now outdated Linux/Minix file system used the type code 0x81 for partitions. OS/2 marks it's partitions with a 0x07 type and so does Windows NT's NTFS. MS-DOS allocates several type codes for its various flavors of FAT file systems: 0x01, 0x04 and 0x06 are known. DR-DOS used 0x81 to indicate protected FAT partitions, creating a type clash with Linux/Minix at that time, but neither Linux/Minix nor DR-DOS are widely used any more. The extended partition which is used as a container for logical partitions has a type of 0x05, by the way.

Partitions are created and deleted with the fdisk program. Every self respecting operating system program comes with an fdisk and traditionally it is even called fdisk (or FDISK.EXE) in almost all OSes. Some fdisks, noteable the DOS one, are somehow limited when they have to deal with other operating systems partitions. Such limitations include the complete inability to deal with anything with a foreign type code, the inability to deal with cylinder numbers above 1024 and the inability to create or even understand partitions that do not end on a cylinder boundary. For example, the MS-DOS fdisk can't delete NTFS partitions, the OS/2 fdisk has been known to silently "correct" partitions created by the Linux fdisk that do not end on a cylinder boundary and both, the DOS and the OS/2 fdisk, have had problems with disks with more than 1024 cylinders (see the "large-disk" Mini-Howto for details on such disks).

Previous Contents Next