Chapter 3 Linux Filesystem
- Chapter 3 Linux Filesystem
- The Linux Filesystem
- Filesystem Organization
- Keeping Your Disks Healthy
- Summary
Linux Filesystem
Partitions
Partitions are used to divide hard drives into sections. A hard drive must have at least one partition and can have up to four. After creating your partitions, you need to format them, which creates the filesystem your operating system uses to store files. These steps are covered in more detail later in this chapter in the section "fdisk."
There are two types of partitions that can be used to store data, primary and extended. A primary partition is formatted and then used to store files. In addition, a computer can be booted only from a primary partition.
Key Concept
There are two types of partitions: primary and extended. An extended partition is divided into one or more logical drives that are used to store data.
An extended partition, on the other hand, cannot be used to store data directly. It is only a container for another structure called a logical drive. An extended partition is divided into one or more logical drives. Each logical drive is then formatted and used for data storage. Logical drives can exist only on extended partitions. You cannot directly use an extended partition. Rather, you use the logical drives contained in the extended partition.
A hard disk can have up to four primary partitions. Or, it can be divided into up to three primary partitions plus one extended partition. Figure 3.1 shows two different partitioning schemes.
Figure 3.1 On the left is a hard drive scheme divided into four primary partitions. The right side shows a hard drive scheme divided into two primary partitions and one extended partition. The extended partition has been divided into logical drives.
Key Concept
There can be a total of no more than four partitions on each disk. These can be all primary or you can have up to three primary and one extended partition.
One more issue must be addressed before you finalize your partition plan. As mentioned in Chapter 2, "Booting Linux," when LILO is used to boot Linux, it does not recognize beyond the 1023rd cylinder of your hard drive. Therefore, if you are using LILO to boot your system, the partition where LILO resides must exist entirely within the first 504MB.
Interpreting Partition Names
Partitions are named according to the type of disk controller the hard disk contains, the partition, and which partition it is on that disk. Under DOS, the first partition of the first drive would be disk(0) partition(1). Linux refers to the same partition as hda1 if it is IDE and sda1 if it's SCSI.
When interpreting partition names, the first letter identifies the type of controller (SCSI or IDE). The d stands for disk; the third letter numbers the disks (the first disk is a, the second is b, and so on); and the last character is the partition number. Table 3.1 shows some partitions.
Table 3.1 Naming Partitions Under Linux
Name |
Controller |
Disk |
Partition |
hda1 |
IDE controller |
first disk |
first partition |
sda1 |
SCSI controller |
first disk |
first partition |
hdc3 |
IDE controller |
third disk |
third partition |
sdb4 |
SCSI controller |
second disk |
fourth partition |
Key Concept
Partition names are based on the type of controller and partition number. Partitions on SCSI drives are referred to as sdan and partitions on IDE drives are named hdan.
How Many and What Size?
Linux will run just fine with only two partitions, root (/) and swap (swap). All files, applications, and so on, are installed on the root partition. The swap partition is used as virtual memory.
Virtual memory refers to a combination of the amount of physical RAM on your computer added to the amount of swap space you have configured. The operating system uses the swap space so it can function more efficiently.
If your system has 8MB of physical memory, a swap partition can give the operating system more memory, which results in a faster system. If you create a swap partition of 16MB, your system will have a total of 24MB of memory to use.
The type of partition you create is specified when using the fdisk utility as discussed later in this chapter. The advantages of using multiple partitions are as follows:
If one partition is damaged, the other partitions remain isolated from the damage.
If you have to reformat a partition, data on the other partitions are unaffected.
Files that grow rapidly, such as log and mail files, can be kept on separate partitions to prevent them from taking all available space.
Upgrades are easier to perform because the system and data files are isolated from each other.
Backups are easier if system files and data files are located on separate partitions.
The time required for filesystem checks at boot can be reduced.
Key Concept
The minimum number of partitions used to install Linux is two: one primary partition as the root (/) and a swap partition.
Your swap partition should be twice the amount of physical RAM installed on your system. The maximum size of a swap partition is 128MB, but you can have up to 16 separate swap partitions. The recommended minimum size is 16MB for the operating system to function at its best.
If space is tight, you can make the size of your swap partition equal to the amount of physical RAM, as long as it is at least 16MB.
Key Concept
A swap partition should be at least 16MB in size. Its maximum allowable size is 128MB. You can have up to 16 separate swap partitions on a single system.
Although a swap partition is more efficient, you can use a swap file if you do not have enough room to create a separate swap partition.
How all this is accomplished is relatively simple. Linux moves unused pages of memory out to the swap partition, allowing more applications to run simultaneously on your system. However, swapping is slower than using physical RAM. If you have 4MB of RAM or less, you will need to have a swap partition or the installation will fail.
One possible partitioning plan is shown in Table 3.2.
Table 3.2 Sample Partitioning Plan
Partition |
Contents |
/ |
Contains applications and configuration files |
/var |
Contains log files |
/usr |
Contains system software |
/tmp |
Contains temp files |
/home |
Contains users' home directories |
swap |
Acts as virtual memory |
One disadvantage of multiple partitions is that you cannot enlarge a partition dynamically if it should get full. Rather, you would have to delete the partition and then re-create it with a larger size. With the larger hard drives available now, many system administrators use fewer partitions.
Plan your partitioning scheme out on paper. Although you can always blow away your partitions and start all over, having a well thought-out plan before starting the partitioning process will keep you from having to reinstall because of space constraints.
Key Concept
An advantage of multiple partitions is that you can better control the amount of space dynamic files such as log files can consume. On the other hand, a partition cannot be dynamically enlarged if it becomes full.
fips
If you are installing Linux on a computer running another operating system and there is no free space to create a new partition, you can use fips.exe to non-destructively divide a single FAT16 partition in two.
This program does not work with any filesystem except FAT16. Although fips usually works without losing any existing data, be sure to make a complete backup of your system before using it.
fips.exe can be downloaded at http://sunsite.unc.edu/pub/Linux/system/install. Whenever you use a program such as fips, you should always make a full backup and read the documentation before starting.
Copy fips.exe and restorbb.exe to a floppy. A copy of your old Master Boot Record (MBR) will be copied to this floppy during the installation program. This backup copy of your MBR can be used in case you ever need to restore your system to its original state. Be sure to label and date the diskette.
A copy of the old MBR also will be saved on your Linux partition as /boot/boot.0300 (for IDE drives) or /boot/boot.0800 (for SCSI drives). Do not ever delete the original file from your hard disk. If you delete it, you will not be able to restore the old MBR.
Key concept
You can use the fips utility to dynamically divide an existing FAT partition to create space for installing Linux.
fdisk
Be sure to keep good notes while partitioning your drive. You will want to note starting and ending cylinders, partition size in blocks, and any error messages you might get.
Cylinder refers to the total of the tracks that have the same location on each disk's surface. Tracks are the concentric circles on a disk.
When using fdisk, you must enter starting and ending cylinders for each partition. To create a partition, follow these steps:
Size the partition.
Designate its type.
Write it to the partition table.
Using fdisk and each of the previous steps are covered in the following sections.
fdisk takes the name of the disk you want to partition as an argument or value.
For example, to create partitions on the second SCSI drive in your system, you would type
fdisk /dev/sdb
If you use fdisk without an argument, it assumes you want to partition /dev/hda. Some operating systems require that they be installed on the first partition of the first disk. Because Linux does not care what disks you use, you might want to create partitions on different hard disks. Just run fdisk for each hard disk you need to partition.
When using fdisk, you can get a menu of available options by typing m, as in the following example:
Command (m for help): m 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 extrafunctionality Command (m for help):
If you create logical drives, you must reboot your system and go through the same options as before. However, when fdisk asks if you want to partition your drive the second time, say no.
You should use the Linux version of fdisk only to create or delete partitions for the Linux operating system. If you try to create or delete partitions for other operating systems with this version of fdisk, the other operating system will not recognize the partition.
After creating your partitions, you need to activate your swap partition(s), select the partition to be used as root, and format your partitions.
Key Concept
The fdisk utility can be used to create or delete partitions. It also can be used to view your partition table.
Let's walk through creating a couple partitions. If you have any existing partitions you will not be using, you can use the d command to delete them.
Step 1: Create a Primary Partition
First, type n to create a new partition. The Linux root partition will be 80MB in size. The following is an example:
Command (m for help): n Command action e extended p primary partition (1-4)
When creating a new partition, you are first asked whether you want to create a primary or extended partition.
Key Concept
If you have existing partitions on your drive, your options might be different. Remember, you can have only a maximum of four primary partitions or three primary partitions and one extended partition per hard disk. If you already have four partitions, you will not be able to create a new partition. In addition, if you already have an extended partition, you will not have this option.
For your first partition, you must create a primary partition because operating systems can boot only from primary partitions. After entering the p command, you will be prompted for the partition number and starting and ending cylinders.
Partition number (1-4): 1 First cylinder (0-788): 0 Last cylinder or +size or +sizeM or +sizeK (0-788): +80M
You can specify either the ending cylinder or the size of the partition. In our example, we are instructing fdisk to create an 80MB partition to use as our first partition. If you type +80k, the partition will be 80KB in size. Typing +80 creates an 80-byte partition.
Now, when you issue the p command, fdisk displays the following:
Command (m for help): p Disk /dev/hda: 255 heads, 63 sectors, 788 cylinders Units = cylinders of 16065 * 512 bytes Device Boot Begin Start End Blocks Id System /dev/hda1 * 1 1 459 3686759 83 Linux native Command (m for help):
Step 2: Create a Swap Partition
Next, you need to create a swap partition. When creating successive partitions, they should start with the next cylinder after the last one ended. Because our primary partition ended at 459, we will start the next one at 460:
Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 3 First cylinder (460-788): 460 Last cylinder or +size or +sizeM or +sizeK (474-683): +128M
Display your partition scheme again using the p command and write down this information, especially the size of each partition in blocks.
Command (m for help): p Disk /dev/hda: 255 heads, 63 sectors, 788 cylinders Units = cylinders of 16065 * 512 bytes Device Boot Begin Start End Blocks Id System /dev/hda1 * 1 1 459 3686759 83 Linux native /dev/hda2 460 460 475 128488 83 Linux native
In our example, the swap partition, /dev/hda2, is listed as a Linux native partition. To complete the creation of a swap partition, you must change the type to swap by using the t command:
Command (m for help): t Partition number (1-4): 2 Hex code (type L to list codes): 82 Command (m for help): p Disk /dev/hda: 255 heads, 63 sectors, 788 cylinders Units = cylinders of 16065 * 512 bytes Device Boot Begin Start End Blocks Id System /dev/hda1 * 1 1 459 3686759 83 Linux native /dev/hda2 460 460 475 128488 82 Linux swap
If you typed L instead of 82, you will see that Linux swap is type 82 and Linux native is 83. The type for extended partitions is 5.
Be sure to review your partition table after each change to ensure that the changes you made are what you intended.
It is customary to create your primary and swap partitions before creating an extended partition. However, you can create a primary partition after creating an extended partition as long as there is free space available and you have not reached the four-partition limit.
Step 3: Create an Extended Partition
Next, you should create an extended partition. Use the following:
Command (m for help): n Command action e extended p primary partition (1-4) e First cylinder (476-788): 476 Last cylinder or +size or +sizeM or +sizeK (474-788): 788
When your partition table is displayed again, you'll see the following:
Command (m for help): p Disk /dev/hda: 255 heads, 63 sectors, 788 cylinders Units = cylinders of 16065 * 512 bytes Device Boot Begin Start End Blocks Id System /dev/hda1 * 1 1 459 3686759 83 Linux native /dev/hda2 460 460 475 128488 82 Linux swap /dev/hda3 476 476 788 2513751 5 Extended
Next, you will need to create logical drives in your extended partition. When you issue the n command, you will have a new choice:
Command (m for help): n Command action l logical (4 or over) p primary partition (1-4) Command (m for help):
You can elect to divide your extended partition into one or more logical drives by using the l command and then specifying the appropriate size. The starting and ending cylinders for each logical drive must be completely contained within the starting and ending cylinders of the extended partition.
After you have finished defining your partitions, you can quit fdisk and save the changes to the partition table by using the w command. If you decide you don't want to make these changes, use the q command to quit fdisk without saving your changes.
Step 4: Assign Filesystem Names
Next, you must associate the appropriate Linux filesystem names with each of the partitions you are going to use. This is done through the installation program for your specific distribution. Remember, you can install Linux with only the / and swap partitions.