6.1. Objective 1: Create Partitions and Filesystems The term filesystem refers to two different things. First, it can mean the way files and directories are physically structured on a disk or other storage medium. Linux supports many different filesystems (in this sense of the word), including ext2 and ext3, the non-journalled and journalled (respectively) native filesystems; msdos or vfat, the native MS-DOS and Windows (respectively) filesystems; JFS, a filesystem used on OS/2 and AIX; XFS, the native IRIX filesystem; and many, many others. In the second sense of the word, it refers to the structure and contents of some storage medium. To view the contents of a filesystem (in this sense of the word) on a Linux system, the device must be mounted, or attached to the hierarchical directory structure on the system. Much of the strength and flexibility of Linux (and Unix) comes from the ability to mount any filesystem that it supports, whether that filesystem is somewhere remote on the network or on a locally-attached disk, anywhere in its directory structure, in a way that is completely transparent to users. For example, the files under /usr will work equally well whether they are on a disk attached to the system or mounted from a master server. Even the / (root) filesystem can be located on a distant server if the system is properly configured. 6.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 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 competitive price, expanding their markets to more consumers and limited budget commercial customers. 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. 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). Also used on PCs are SCSI drives. SCSI is an older standard for connecting peripherals; however, modern SCSI versions are quite fast and flexible. Typically, SCSI devices are used for their increased speed and reliability in large-scale and high-end server environments. With the increased speeds, however, come increased pricesoften two to five times the price of their IDE counterparts. 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. 6.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. |
6.1.1.2. Disk partitions Almost every operating system supports a system for dividing a disk into logical devices, called partitions. Other terms for the same basic concept are slices and logical volumes, although logical volumes generally also imply the ability to span physical disks. Linux supports several different partitioning formats, but by default it uses the MS-DOS format. The MS-DOS partition table allows for up to four primary partitions. One of these four primary partitions can be replaced with an extended partition, which can contain up to 12 logical partitions, for a total of 15 possible usable partitions (16 if you count the extended partition "container," but it is not usable for data). The type of partition (as well as the type of device) affects the name of the device Linux uses to access the partition.
Primary partitions This type of partition contains a filesystem. If all four primary partitions exist on an IDE drive, 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. For example, the partitions on a disk with one primary partition and the sole extended partition might be numbered as follows: /dev/hda1 (primary) /dev/hda2 (extended)
Logical partitions Logical partitions exist within the extended partition. Logical partitions are numbered from 5 to 16. The partitions on a disk with one primary partition, one extended partition, and four logical partitions might be numbered as follows: /dev/hda1 (primary) /dev/hda2 (extended) /dev/hda5 (logical) /dev/hda6 (logical) /dev/hda7 (logical) /dev/hda8 (logical) If the above partitions were made on a SCSI drive, the hda would be replaced by sda such as /dev/sda2. 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. |
6.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. The root filesystem /, also known as 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 filesystem. 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 (see "Objective 3: Control Filesystem Mounting and Unmounting" later in this chapter for a discussion about mounting). Everything on the system that is not stored in a mounted partition is stored locally in the / (root) partition. The mounted filesystems are placed on separate partitions and possibly multiple disk drives. 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 to be available for the boot process. These include:
/bin and /sbin Contains required system binary programs
/dev Contains device files
/etc Contains configuration information used on boot
/lib Contains shared libraries These directories are always part of the single / partition. See the description of the FHS in See Objective 8: Find System Files and Place Files in the Correct Location" 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 the root filesystem.
/var Log files are stored here. This is similar to the situation with /tmp, where user files can fill any available space if something goes wrong or if the files are not cleaned periodically.
/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 4. 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 6-1. Table 6-1. An example partitioning schemePartition | Type | Mounted filesystem | Size |
---|
/dev/hda1 | Primary | /boot | 100 MB | /dev/hda2 | Primary | / | 500 MB | /dev/hda3 | Extended | - | - | /dev/hda5 | Logical | /usr | 4 GB | /dev/hda6 | Logical | /var | 2 GB | /dev/hda7 | Logical | /opt | 1 GB | /dev/hda8 | Logical | /tmp | 500 MB | /dev/hda4 | Primary | (swap partition) | 1 GB | /dev/hdb1 | Primary | /home | 60 GB |
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. Tip: If you are resizing your partitions, you may want to investigate setting up your system using Logical Volume Manager (LVM). LVM is currently not covered on the LPI exams, but its use is quickly growing. For more information read the LVM-HOWTO at the Linux Documentation Project (http://www.tldp.org). 6.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.
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.
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). For primary and extended partitions, you are asked for the partition number (14). 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. Tip: 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 a hex 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): Enter 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): Enter Using default value 40 Last cylinder or +size or +sizeM or +sizeK (40-1027, default 1027): Enter 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): Enter 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): Enter 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): Enter Using default value 118 Last cylinder or +size or +sizeM or +sizeK (118-1027, default 1027): Enter 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 Enter in this example. Other partition sizes are specified in megabytes using responses such as +128M. Warning: 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. |
6.1.1.5. Creating filesystems Once a disk is partitioned, filesystems may be created in those partitions using the mkfs utility. mkfs is a front-end program for filesystem-specific creation tools such as 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.
Syntax mkfs [-t fstype] [fs_options] device
Description Make a filesystem of type fstype on device. If fstype 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 the various filesystem-specific mkfs commands, such as mke2fs and mkdosfs, for full details on their individual options.
Frequently used 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).
-j Create an ext3 journal file (mke2fs only). Using -t ext3 or running mkfs.ext3 has the same effect as using the -j option.
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.27 (8-Mar-2002) Filesystem label=rootfs OS type: Linux Block size=1024 (log=0) Fragment size=1024 (log=0) 26208 inodes, 104422 blocks 5221 blocks (5.00%) reserved for the super user First data block=1 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 Running command: badblocks -b 1024 -s /dev/hda3 104422 Checking for bad blocks (read-only test): done Writing inode tables: done Writing superblocks and filesystem accounting information: done This filesystem will be automatically checked every 28 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override. 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. 6.1.1.6. Creating swap partitions 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.
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 # Warning: 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. |
|