Book: LPI Linux Certification in a Nutshell
Section: Chapter 4.  Devices, Linux Filesystems, and the Filesystem Hierarchy Standard (Topic 2.4)



4.1 Objective 1: Create Partitions and Filesystems

In many PCs, disk organization schemes use a single disk containing a single filesystem. This filesystem contains all data on the computer, mixing system files and user files together. On MS-DOS and Windows systems, that volume is usually labeled C: and thought of as the C drive. If additional space is made available, it is seen as one or more additional volumes, each with a separate drive letter. If these separate drives are intended for user data, it is the user's responsibility to remember which drive letter to use when storing files. While this simplicity has some value for most users, others prefer the ability to create filesystems across multiple partitions, devices, and even multiple computers. Linux offers this ability.

4.1.1 Disk Drives Under Linux

Linux supports many types of disk devices and formats. Any SCSI or IDE hard disk will work with Linux, as will floppy disks, CD-ROMs, CD-Rs, Zip® and Jaz® disks, and other types of removable media. These media can contain the standard Linux ext2 filesystem, FAT, FAT32, NTFS, as well as other filesystem types. This flexibility makes Linux coexist nicely with other operating systems on multiboot systems.

The most commonly found hard disks on PCs are IDE (Integrated Device Electronics) drives. These disks feature a relatively simple system interface, and most of the "smarts" of the disk are onboard the disk itself. The IDE standard allows disk manufacturers to sell their product at a very attractive price point. Also used on PCs are Small Computer System Interface (SCSI, pronounced "scuzzy") drives. SCSI is an older standard for connecting peripherals; however, modern SCSI versions are quite fast and flexible.

In general, IDE disks offer reasonable performance at a low price point, which is highly desirable for consumer products. A single IDE interface is capable of attaching two disk drives to a system. One device is named master and the other is the slave (an unfortunate naming convention). Most PCs have a primary and secondary IDE interface. Together, these interfaces allow up to four devices (primary master, primary slave, secondary master, and secondary slave). At a minimum, these devices will include a hard disk and a CD-ROM, leaving two available positions for CD-R, Zip, tape, or other IDE devices. Adding additional IDE controllers or specialized IDE subsystems can further expand a PC's capabilities.

Compared to IDE, SCSI offers excellent performance, lower CPU utilization, and a much more flexible connection scheme capable of handling up to 15 devices on a single bus. These conveniences allow SCSI systems to grow as space requirements increase without major hardware reconfiguration. Unfortunately, SCSI usually implies higher cost, which reduces demand for SCSI in the cost-sensitive PC market.

Typically, IDE is considered appropriate for desktop use. SCSI is usually specified for servers, for high-performance workstations, and in situations in which expansion capability is a concern.

4.1.1.1 Hard disk devices

By default, Linux defines IDE device files as follows:

/dev/hda

Primary master IDE (often the hard disk)

/dev/hdb

Primary slave IDE

/dev/hdc

Secondary master IDE (often a CD-ROM)

/dev/hdd

Secondary slave IDE

SCSI device files are similar, except that there is no four-device limitation:

/dev/sda

First SCSI drive

/dev/sdb

Second SCSI drive

/dev/sdc

Third SCSI drive (and so on)

Under Linux, a typical PC with a single hard disk on the primary IDE interface and a single CD-ROM on the secondary IDE interface would have disk drive /dev/hda and CD-ROM /dev/hdc.

On the Exam

You should be prepared to identify IDE and SCSI devices based on their device filenames.

4.1.1.2 Disk partitions

On each disk in a PC, there may be between 1 and 16 partitions. A partition can be thought of as a container on the disk, into which a filesystem (or in one circumstance, more partitions) can be placed. Unlike MS-DOS, which assigns letter names to partitions, each partition under Linux is assigned an integer number on the disk, which is appended to the disk's device name. For example, the first partition on IDE disk /dev/hda is /dev/hda1. There are three types of partitions found on PCs:

Primary partitions

This type of partition contains a filesystem. At least one primary partition must exist, and up to four can exist on a single physical disk. If all four primary partitions exist, they are numbered as follows:

  • /dev/hda1

  • /dev/hda2

  • /dev/hda3

  • /dev/hda4

One of these primary partitions may be marked active, in which case the PC BIOS will be able to select it for boot.

Extended partitions

An extended partition is a variant of the primary partition but cannot contain a filesystem. Instead, it contains logical partitions. Only one extended partition may exist on a single physical disk. If an extended partition exists, it takes one of the four possible spots for primary partitions, leaving room for only three primary partitions. The partitions on a disk with one primary partition and the sole extended partition are numbered as follows:

  • /dev/hda1 (primary)

  • /dev/hda2 (extended)

Logical partitions

Logical partitions exist within the extended partition. 1 to 12 logical partitions may be created. Logical partitions are numbered from 5 to 16. The partitions on a disk with one primary partition, the sole extended partition, and four logical partitions are numbered as follows:

  • /dev/hda1 (primary)

  • /dev/hda2 (extended)

  • /dev/hda5 (logical)

  • /dev/hda6 (logical)

  • /dev/hda7 (logical)

  • /dev/hda8 (logical)

Under this PC partitioning scheme, a maximum of 15 partitions with filesystems may exist on a single physical disk (3 primary plus 12 logical), more than enough for any Linux installation. In practice, the last example is typical for a Linux installation. It is unlikely that all of the 15 possible partitions on a disk would be necessary just to support Linux.

On the Exam

Be sure that you understand how partition numbering works. In particular, pay attention to the differences in numbering between primary, extended, and logical partitions.

4.1.1.3 The root filesystem and mount points

As a Linux system boots, the first filesystem that becomes available is the top level, or root filesystem, denoted with a single forward slash.[1] In a simple installation, the root filesystem could contain nearly everything on the system. However, such an arrangement could lead to system failure if the root filesystem fills to capacity. Instead, multiple partitions are typically defined, each containing one of the directories under /. As the Linux kernel boots, the partitions are mounted to the root filesystem, and together create a single unified filesystem. (Mounting is the subject of Objective 3.) Everything on the system that is not stored in a mounted partition is stored locally in /. The mounted filesystems are placed on separate partitions and possibly multiple disk drives.

[1] The root filesystem /, often called the root directory, shouldn't be confused with the root superuser account or the superuser's home directory, /root. The distinct directories / and /root are unrelated and are not required to share the same disk partition.

The choice of which directories are placed into separate partitions is both a personal and technical decision. Here are some guidelines for individual partitions:

/ (the root directory)

Since the only filesystem mounted at the start of the boot process is /, certain directories must be part of it in order to be available for the boot process. These include:

/bin and /sbin

Contain required system binary programs

/dev

Contains device files

/etc

Contains boot configuration information

/lib

Contains program libraries

These directories are always part of the single / partition. See the description of the FHS in Objective 8 for more on the requirements for the root filesystem.

/boot

This directory holds static files used by the boot loader, including kernel images. On systems where kernel development activity occurs regularly, making /boot a separate partition eliminates the possibility that / will fill with kernel images and associated files during development.

/home

User files are usually placed in a separate partition. This is often the largest partition on the system and may be located on a separate physical disk or disk array.

/tmp

This directory is often a separate partition used to prevent temporary files from filling.

/var

Log files are stored here. Just like /tmp, log files could grow unchecked unless they are rotated regularly, filling.

/usr

This directory holds a hierarchy of directories containing user commands, source code, and documentation. It is often quite large, making it a good candidate for its own partition. Because much of the information stored under /usr is static, some users prefer that it be mounted as read-only, making it impossible to corrupt.

In addition to the preceding six partitions listed, a /swap partition is also necessary for a Linux system to enable virtual memory. For information on determining the size of a swap partition, see Chapter 14 later in this book.

Using these guidelines at installation time, the disk partitions for an IDE-based system with two physical disks on the primary IDE controller might look as described in Table 4-1.

Table 4-1. An Example Partitioning Scheme

Partition

Type

Mounted Filesystem

Size

/dev/hda1

Primary

/

300 MB

/dev/hda2

Extended

-

-

/dev/hda5

Logical

/boot

300 MB

/dev/hda6

Logical

/opt

300 MB

/dev/hda7

Logical

/tmp

300 MB

/dev/hda8

Logical

/usr

600 MB

/dev/hda9

Logical

/var

300 MB

/dev/hda10

Logical

(/swap partition)

128 MB

/dev/hdb1

Primary

/home

6 GB

See Figure 4-1 later in this chapter for a graphical depiction of this partitioning scheme.

Once a disk is partitioned, it can be difficult or risky to change the partition sizes. Commercial and open source tools are available for this task, but a full backup is recommended prior to their use.

4.1.1.4 Managing partitions

Linux has two basic options for partitioning disk drives. The fdisk command is a text-based program that is easy to use and exists on every Linux distribution. It is also required for Exam 101. Another option you may wish to explore after mastering fdisk is cfdisk, which is still a text-mode program but which uses the curses system to produce a GUI-style display.

fdisk

Syntax

fdisk [device]

Description

Manipulate or display the partition table for device using a command-driven interactive text interface. device is a physical disk such as /dev/hda, not a partition such as /dev/hda1. If omitted, device defaults to /dev/hda. Interactive commands to fdisk are a single letter followed by a carriage return. The commands do not take arguments, but start an interactive dialog. Commands that operate on a partition will request the partition number, which is an integer. For primary and extended partitions, the partition number is from 1 to 4. For logical partitions, which are available only if the extended partition already exists to contain them, the partition number is from 5 to 16.

When making changes to the partition table, fdisk accumulates changes without writing them to the disk, until it receives the write command.

4.1.1.5 Frequently used commands
a

Toggle the bootable flag on/off for a primary partition.

d

Delete a partition. You are prompted for the partition number to delete. If you delete a logical partition when higher numbered logical partitions exist, the partition numbers are decremented to keep logical partition numbers contiguous.

l

List the known partition types. A table of partition types is printed.

m

Display the brief help menu for these commands.

n

Add a new partition. You are prompted for the partition type (primary, extended, or logical).[2] For primary and extended partitions, you are asked for the partition number (1- 4). For logical partitions, the next logical partition number is selected automatically. You are then prompted for the starting disk cylinder for the partition and are offered the next free cylinder as a default. Finally, you are prompted for the last cylinder or a size, such as "+300M." By default, new partitions are assigned as Linux ext2, type 83. To create another partition type, such as a swap partition, first create the partition with the n command, then change the type with the t command.

[2] Note that fdisk displays options for extended and primary partition types if an extended partition does not yet exist. If the extended partition already exists, fdisk displays options for logical and primary partition types.

p

Display the partition table as it exists in memory. This depiction will differ from the actual partition table on disk if changes have not been saved.

q

Quit without saving changes.

t

Change a partition's system ID. This is an octal number that indicates the type of filesystem the partition is to contain. Linux ext2 partitions are type 83, and Linux swap partitions are type 82.

w

Write (save) the partition table to disk and exit. No changes are saved until the w command is issued.


Example 1

Display the existing partition table on /dev/hda without making any changes:

# fdisk /dev/hda
Command (m for help): p

Disk /dev/hda: 255 heads, 63 sectors, 1027 cylinders
Units = cylinders of 16065 * 512 bytes

   Device Boot    Start       End    Blocks   Id  System
/dev/hda1   *         1       250   2008093+  83  Linux
/dev/hda2           251       280    240975   82  Linux swap
/dev/hda3           281      1027   6000277+   5  Extended
/dev/hda5           281       293    104391   83  Linux
/dev/hda6           294       306    104391   83  Linux
/dev/hda7           307       319    104391   83  Linux

Command (m for help): q

#

In this configuration, /dev/hda has two primary partitions, /dev/hda1, which is bootable, and /dev/hda2, which is the swap partition. The disk also has an extended partition /dev/hda3, which contains three logical partitions, /dev/hda5, /dev/hda6, and /dev/hda7. All other primary and logical partitions are Linux ext2 partitions.

Example 2

Starting with a blank partition table, create a bootable primary partition of 300 MB on /dev/hda1, the extended partition on /dev/hda2 containing the remainder of the disk, a logical partition of 200 MB on /dev/hda5, a logical swap partition of 128 MB on /dev/hda6, and a logical partition on /dev/hda7 occupying the remainder of the extended partition:

# fdisk /dev/hda

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

Command (m for help): a
Partition number (1-4): 1

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

Command (m for help): n
Command action
   l   logical (5 or over)
   p   primary partition (1-4)
l
First cylinder (40-1027, default 40):
Using default value 40
Last cylinder or +size or +sizeM or +sizeK (40-1027, 
   default 1027): +200M

Command (m for help): n
Command action
   l   logical (5 or over)
   p   primary partition (1-4)
l
First cylinder (79-1027, default 79):
Using default value 79
Last cylinder or +size or +sizeM or +sizeK (79-1027, 
   default 1027): +128M

Command (m for help): t
Partition number (1-6): 6
Hex code (type L to list codes): 82
Changed system type of partition 6 to 82 (Linux swap)

Command (m for help): n
Command action
   l   logical (5 or over)
   p   primary partition (1-4)
l
First cylinder (118-1027, default 118):
Using default value 118
Last cylinder or +size or +sizeM or +sizeK (118-1027, 
   default 1027):<return>
Using default value 1027

Command (m for help): p

Disk /dev/hda: 255 heads, 63 sectors, 1027 cylinders
Units = cylinders of 16065 * 512 bytes

   Device Boot    Start       End    Blocks   Id  System
/dev/hda1   *         1        39    313236   83  Linux
/dev/hda2            40      1027   7936110    5  Extended
/dev/hda5            40        65    208813+  82  Linux swap
/dev/hda6            66        82    136521   83  Linux
/dev/hda7            83      1027   7590681   83  Linux

Command (m for help): w

The partition table has been altered!

Calling ioctl(  ) to re-read partition table.
Syncing disks.

#

Note the use of defaults for the partition start cylinders and for end cylinder selections, indicated by <return> in this example. Other partition sizes are specified in megabytes using responses such as +128M .

At boot time, the BIOS of many PCs can access only the first 1024 cylinders of the disk. The Linux kernel, however, has no such limitation, but the BIOS must be able to load the boot loader LILO and the entire kernel image into memory. Thus, the entire contents of /boot, either as part of / or as a separate partition, must be located within the 1024-cylinder boundary. In April 2000, Version 0.21.4.2 of LILO was released; which is within the 1024-cylinder limit. If you have a Linux distribution that includes a newer version of LILO, you may not need to worry about this issue. Check your LILO documentation to be sure.

If you are attempting to create partitions for other operating systems with the Linux fdisk utility, you could run into a few problems. As a rule, it is safest to prepare the partitions for an operating system using the native tools of that operating system.

As you might expect, using fdisk on a working system can be dangerous, because one errant w command can render your disk useless. Use extreme caution when working with the partition table of a working system, and be sure you know exactly what you intend to do and how to do it.

On the Exam

You should understand disk partitions and the process of creating them using fdisk.

4.1.1.6 Creating filesystems

Once a disk is partitioned, filesystems may be created in those partitions using the mkfs utility. Usually, partitions intended for use with Linux will use the native second extended filesystem, or ext2. MS-DOS filesystems can also be created.[3] In reality, mkfs is a front-end program for filesystem-specific creation tools named mkfs.ext2 and mkfs.msdos, which are in turn linked to mke2fs and mkdosfs, respectively. mkfs offers a unified front-end, while the links provide convenient names. The choice of which executable to call is up to you.

[3] You can also use mkfs to create filesystems of type minix, in the rare event that you encounter such a requirement.

mkfs

Syntax

mkfs [-t fs_type] [ fs_options] device

Description

Make a filesystem of type fs_type on device. The fs_type is either ext2 or msdos. If fs_type is omitted, ext2 is used by default. When called by mkfs, these programs are passed any fs_options included on the command line. See the manpages for mke2fs and mkdosfs for full details on their individual options.

Frequently used fs_options

-c

Check device for bad blocks (mke2fs and mkdosfs).

-L label

Set the volume label for the filesystem (mke2fs only).

-n label

Set the 11-character volume label for the filesystem (mkdosfs only).

-q

Uses mkfs in quiet mode, resulting in very little output (mke2fs only).

-v

Used to enter verbose mode (mke2fs and mkdosfs).

Example 1

Using defaults, quietly create an ext2 partition on /dev/hda3:

# mkfs -q /dev/hda3
mke2fs 1.14, 9-Jan-1999 for EXT2 FS 0.5b, 95/08/09
#

Example 2

Create an ext2 filesystem labeled rootfs on existing partition /dev/hda3, checking for bad blocks and with full verbose output:

# mkfs -t ext2 -L rootfs -cv /dev/hda3
mke2fs 1.14, 9-Jan-1999 for EXT2 FS 0.5b, 95/08/09
Linux ext2 filesystem format
Filesystem label=rootfs
26208 inodes, 104422 blocks
5221 blocks (5.00%) reserved for the super user
First data block=1
Block size=1024 (log=0)
Fragment size=1024 (log=0)
13 block groups
8192 blocks per group, 8192 fragments per group
2016 inodes per group
Superblock backups stored on blocks:
        8193, 16385, 24577, 32769, 40961, 49153, 
        57345, 65537, 73729, 81921, 90113, 98305

Checking for bad blocks (read-only test): done
Writing inode tables: done
Writing superblocks and filesystem accounting 
   information: done

#

Additional options are available in the mke2fs and mkdosfs programs, which may be needed to fine-tune specific filesystem parameters for special situations. In most cases, the default parameters are appropriate and adequate.

An additional command not specifically cited in the LPI Objectives for this Topic is mkswap. This command prepares a partition for use as Linux swap space and is needed if you plan to fully configure a disk from scratch. It is also needed if you need to add an additional swap partition.

mkswap

Syntax

mkswap device

Description

Prepare a partition for use as swap space. This command can also set up swap space in a file on another filesystem.

Example

On an existing partition, which should be set to type 82 (Linux swap), ready swap space:

# mkswap /dev/hda5
Setting up swapspace version 1, size = 139792384 bytes
#

Running any of the filesystem creation programs is, like fdisk, potentially dangerous. All data in any previously existing filesystems in the specified partition will be deleted. Since mkfs does not warn you prior to creating the filesystem, be certain that you are operating on the correct partition.

On the Exam

The exam is likely to contain general questions about using mkfs, though details such as inode allocation are beyond the scope of the LPIC Level 1 exams.