本附录对于非 x86 体系不一定适用。然而,在这里提及的一般原理可能适用。
25.1. 硬盘基本概念
硬盘功能极为简单 — 它们被用来可靠地储存及检索数据。
没什么可看的,是不是?不过,若我们仅在一个最基本的层次上讨论磁盘驱动器,此图表已足够。假设我们要在这个磁盘驱动器上面储存一些数据,就目前而言,这还不行。我们首先要做一些准备工作。
25.1.1. 不是你写入什么,而是你怎么写入
Experienced computer users probably got this one on the first try. We need to format the drive. Formatting (usually known as "making a file system") writes information to the drive, creating order out of the empty space in an unformatted drive.
A small percentage of the drive's available space is used to store file system-related data and can be considered as overhead.
A file system splits the remaining space into small, consistently-sized segments. For Linux, these segments are known as blocks.
由于文件系统带来创建目录和文件的可能性,以上牺牲可以被看作所需付出的一个很小的代价。
It is also worth noting that there is no single, universal file system. As
图 25.3 “含有不同文件系统的磁盘驱动器”, shows, a disk drive may have one of many different file systems written on it. As you might guess, different file systems tend to be incompatible; that is, an operating system that supports one file system (or a handful of related file system types) may not support another. This last statement is not a hard-and-fast rule, however. For example, Red Hat Enterprise Linux supports a wide variety of file systems (including many commonly used by other operating systems), making data interchange between different file systems easy.
当然,将文件系统写入磁盘仅仅是一个开端。这个过程的目标实际上是储存并且检索数据。写入一些文件后,让我们再来看一看磁盘。
As
图 25.4 “已写入数据的磁盘驱动器”, shows, some of the previously-empty blocks are now holding data. However, by just looking at this picture, we cannot determine exactly how many files reside on this drive. There may only be one file or many, as all files use at least one block and some files use multiple blocks. Another important point to note is that the used blocks do not have to form a contiguous region; used and unused blocks may be interspersed. This is known as
fragmentation. Fragmentation can play a part when attempting to resize an existing partition.
如同许多与计算机相关的科技,磁盘驱动器自问世后一直在不断地变化。特别是,它们越来越大。不是实际尺寸越来越大,而是它们储存信息的能力越来越大。这种新增的容量导致了磁盘驱动器使用方法的根本改变。
25.1.2. 分区:将一个驱动器变成多个驱动器
由于磁盘驱动器容量的不断增大,一些人开始质问将所有格式化的空间并为一大块是否明智。这一类想法的动机有哲学上的,也有技术上的。从哲学角度上讲,一个较大的磁盘驱动器所提供的额外空间若超过了一定的大小似乎只会造成更多的杂乱无章。从技术角度上讲,某些文件系统不是为支持大于一定容量的磁盘驱动器而设计的。或者,某些文件系统可能会支持拥有巨大容量的较大的驱动器,但是由文件系统跟踪文件所强加于上的管理费用也随之变得过高过大。
解决这个问题的办法是将磁盘划分为分区 (partition)。每一分区都可以像一个独立的磁盘一样被访问。这是通过添加分区表(partition table)来做到的。
虽然本章图表中所显示的分区表和实际磁盘驱动器是分开的,这并不完全正确。事实上,分区表被保存在磁盘的最起首,在任何文件系统或用户数据之前。但是为了清楚起见,我们在图表中将之分开。
每个分区表项目都包含着该分区的几项重要的特征:
Let us take a closer look at each of these characteristics. The starting and ending points actually define the partition's size and location on the disk. The "active" flag is used by some operating systems' boot loaders. In other words, the operating system in the partition that is marked "active" is booted.
The partition's type can be a bit confusing. The type is a number that identifies the partition's anticipated usage. If that statement sounds a bit vague, that is because the meaning of the partition type is a bit vague. Some operating systems use the partition type to denote a specific file system type, to flag the partition as being associated with a particular operating system, to indicate that the partition contains a bootable operating system, or some combination of the three.
在许多情况下,整个磁盘上只有一个分区,基本上是重复分区以前所使用的方法。分区表内只有一个项目,它指向分区的起点。
We have labeled this partition as being of the "DOS" type. Although it is only one of several possible partition types listed in
表 25.1 “分区类型”, it is adequate for the purposes of this discussion.
表 25.1. 分区类型
分区类型 | 值 | 分区类型 | 值 |
---|
Empty | 00 | Novell Netware 386 | 65 |
DOS 12-bit FAT | 01 | PIC/IX | 75 |
XENIX root | 02 | Old MINIX | 80 |
XENIX usr | 03 | Linux/MINUX | 81 |
DOS 16-bit <=32M | 04 | Linux swap | 82 |
Extended | 05 | Linux Native | 83 |
DOS 16-bit >=32 | 06 | Linux extended | 85 |
OS/2 HPFS | 07 | Amoeba | 93 |
AIX | 08 | Amoeba BBT | 94 |
AIX bootable | 09 | BSD/386 | a5 |
OS/2 Boot Manager | 0a | OpenBSD | a6 |
Win95 FAT32 | 0b | NEXTSTEP | a7 |
Win95 FAT32 (LBA) | 0c | BSDI fs | b7 |
Win95 FAT16 (LBA) | 0e | BSDI swap | b8 |
Win95 Extended (LBA) | 0f | Syrinx | c7 |
Venix 80286 | 40 | CP/M | db |
Novell | 51 | DOS access | e1 |
PPC PReP Boot | 41 | DOS R/O | e3 |
GNU HURD | 63 | DOS secondary | f2 |
Novell Netware 286 | 64 | BBT | ff |
25.1.3. 分区内的分区 — 扩展分区概述
经过一段时间后,四个分区很明显将不够用。随着磁盘驱动器的不断增大,配置了四个相当大的分区后仍有剩余空间的可能性会越来越大。我们需要有一些创建更多分区的方法。
Enter the extended partition. As you may have noticed in
表 25.1 “分区类型”, there is an "Extended" partition type. It is this partition type that is at the heart of extended partitions.
When a partition is created and its type is set to "Extended," an extended partition table is created. In essence, the extended partition is like a disk drive in its own right — it has a partition table that points to one or more partitions (now called
logical partitions, as opposed to the four
primary partitions) contained entirely within the extended partition itself.
图 25.7 “带有扩展分区的磁盘驱动器”, shows a disk drive with one primary partition and one extended partition containing two logical partitions (along with some unpartitioned free space).
如图表中所暗示,主分区与逻辑分区之间有一个区别 — 主分区只能有四个,但是可以存在的逻辑分区数量却无固定限制。不过,鉴于 Linux 进入分区的方式,你应该避免在一个磁盘驱动器上定义 12 个以上逻辑分区。
现在,我们已经大致讨论了分区概念,让我们来看一看如何将这些知识应用到安装Red Hat Enterprise Linux 上。
25.1.4. 为Red Hat Enterprise Linux 腾挪空间
当你试图为你的硬盘重新分区时,有三种可能的情况:
有可用的未分区的空闲空间
有可用的未使用过的分区
被活跃使用的分区内有可用的空闲空间
让我们依次来看一看每一种情况。
请记住,以下图解是为清晰起见而经简化的,它们并不反映当你实际安装Red Hat Enterprise Linux 时所会遇到的确切分区布局。
25.1.4.1. 使用未经分区的空闲空间
如果细想一下,你就会认识到一个未经使用的硬盘也属这种类型。唯一的区别是后者的全部空间都不属于任何定义的分区。
接下来,我们将讨论一种更普遍的情况。
25.1.4.2. 使用一个未使用过的分区中的空间
如果你发现自己处于这种情况,你可以使用那些拨给未使用分区的空间。首先,你应该删除该分区,然后在其上创建相应的 Linux 分区。你可以在安装过程中删除未用分区,然后再手工创建新分区。
25.1.4.3. 使用活跃分区中的空闲空间
这是最常见的情况。不幸的是,这也是最难处理的情况。主要问题是,即便你有足够的空闲空间,它们目前已被分配给一个正在使用中的分区。如果你购买了一个带有预装软件的计算机,很可能整个硬盘是一个单个的带有操作系统和数据的大分区。
除了给你的系统添加一个新硬盘驱动器外,你还有以下两种选择:
-
Destructive Repartitioning
简而言之,这种方法让你删除一个大分区而创建几个小一些的分区。你可能想象得到,贮存在原来分区上的所有数据将会被破坏。这意味着你有做完全备份的必要。为你自己的利益着想,请做两个备份,并校验(如果你的备份软件提供了的话)这些备份,在你删除分区之前试着从你的备份中读取数据。
如果该分区上装有某类操作系统,该操作系统也需要被重新安装。需要注意的是,那些带有预装操作系统售出的计算机可能没有包括重装该系统所需的光盘介质。请在破坏你的原有分区及原有操作系统之前觉查到这一点。
- Non-Destructive Repartitioning
这要求你运行一个似乎可以“为所不可为”的程序:它会把大分区变小,却不会丢失该分区上的原有文件。许多人都发现这个办法既可靠又简单可行。但是哪一个软件可以帮助你达到这一目的呢?在软件市场上有好几种磁盘管理软件。你应该做一番调查来找到最适合你的情况的一种。
非破坏性分区过程是非常直捷了当的,它包括以下几个步骤:
接下来,我们将详细说明每一步骤。
25.1.4.3.1. 压缩现存数据
这一步骤至关重要。不执行这一步骤,你的数据所在位置可能会阻止分区被重新划分为想要的大小。还请注意的是,由于某种原因,某些数据不能被移动。如果情况如此(这会严重地限制你的新分区的大小),你可能会被迫在你的磁盘上进行破坏性重新分区。
25.1.4.3.2. Resize the existing partition
理解你所使用的重新分区软件对新空出的空间的操作是很重要的,只有这样,你才能正确地采取相应措施。在我们示范的情况中,最佳措施是删除新建的 DOS 分区,然后创建恰当的 Linux 分区。
25.1.4.3.3. Create new partition(s)
下列信息是基于 intel 的计算机所特有的。
为了方便用户,我们提供了 parted
工具。它是一个可免费获得的重新划分分区大小的程序。
如果你决定使用 parted
来重新给你的硬盘驱动器分区,你必须熟悉磁盘贮存区,并备份了你的计算机上的数据。这一点至关重要。你应该给你的计算机上重要的数据做两个备份。这些备份应该保存在可移介质上(如磁盘、光盘或磁带),而且你应该在继续前确定它们可以被读取。
如果你决定要使用 parted
,请留意在 parted
运行后,你会有两个分区:一个是你重新划分大小的分区,另一个是 parted
用新空出的空间创建的新分区。如果你的目的是使用新空出的空间来安装 Red Hat Enterprise Linux,你应该删除新分区。你既可以使用在目前操作系统下的分区工具来删除它,也可以在安装过程中设置分区时删除它。
25.1.5. 分区命名方案
Linux refers to disk partitions using a combination of letters and numbers which may be confusing, particularly if you are used to the "C drive" way of referring to hard disks and their partitions. In the DOS/Windows world, partitions are named using the following method:
Each partition's type is checked to determine if it can be read by DOS/Windows.
If the partition's type is compatible, it is assigned a "drive letter." The drive letters start with a "C" and move on to the following letters, depending on the number of partitions to be labeled.
驱动器字母可以用来指代那个分区,也可以用来指带分区所含的文件系统。
Red Hat Enterprise Linux 使用一种更灵活的命名方案。它所传达的信息比其它操作系统采用的命名方案更多。该命名方案是基于文件的,文件名的格式类似 /dev/xxyN
。
下面说明了解析分区命名方案的方法:
/dev/
这个字串是所有设备文件所在的目录名。因为分区位于硬盘上,而硬盘是设备,所以这些文件代表了在/dev/
上所有可能的分区。
xx
分区名的前两个字母标明分区所在设备的类型。通常是 hd
(IDE 磁盘)或 sd
(SCSI 磁盘)。
y
这个字母标明分区所在的设备。例如,/dev/hda
(第一个 IDE 磁盘)或 /dev/sdb
(第二个 SCSI 磁盘)
N
最后的数字代表分区。前四个分区(主分区或扩展分区)是用数字从 1
排列到 4
。逻辑分区从 5
开始。例如,/dev/hda3
是在第一个 IDE 硬盘上的第三个主分区或扩展分区;/dev/sdb6
是在第二个 SCSI 硬盘上的第二个逻辑分区。
该命名方案中没有表明分区类型的地方;与 DOS/Windows 不同,所有分区都可在Red Hat Enterprise Linux 下被识别。当然,这并不是说Red Hat Enterprise Linux 能够访问每一类分区上的数据,但是在许多情况下,访问专用于另一操作系统的分区上的数据是可能的。
请切记以上信息;它会帮助你在设置Red Hat Enterprise Linux 所需分区时更容易地理解许多步骤。
25.1.6. 磁盘分区以及其它操作系统
如果你的Red Hat Enterprise Linux 分区将会与其它操作系统所用的分区共享一个硬盘,多数情况下,应该没什么问题。不过,某类 Liunx 和其它操作系统的组合需要特别加以注意。
25.1.7. 磁盘分区和挂载点
One area that many people new to Linux find confusing is the matter of how partitions are used and accessed by the Linux operating system. In DOS/Windows, it is relatively simple: Each partition gets a "drive letter." You then use the correct drive letter to refer to files and directories on its corresponding partition.
这与 Linux 处理分区及磁盘贮存问题的方法截然不同。其主要的区别在于,Linux 中的每一个分区都是构成支持一组文件和目录所必需的贮存区的一部分。它是通过挂载(mounting)来实现的,挂载是将分区关联到某一目录的过程。挂载分区使起始于这个指定目录(通称为挂载点,mount point)的贮存区能够被使用。
例如,如果分区 /dev/hda5/
被挂载在 /usr/
上,这意味着所有在 /usr/
之下的文件和目录在物理意义上位于 /dev/hda5/
上。因此文件 /usr/share/doc/FAQ/txt/Linux-FAQ
被保存在 /dev/hda5/
上,而文件 /etc/X11/gdm/Sessions/Gnome
却不是。
继续以上的例子,/usr/
之下的一个或多个目录还有可能是其它分区的挂载点。例如,某个分区(假设为,/dev/hda7/
)可以被挂载到 /usr/local/
下,这意味着 /usr/local/man/whatis
将位于 /dev/hda7
上而不是 /dev/hda5
上。
25.1.8. 多少个分区?
At this point in the process of preparing to install Red Hat Enterprise Linux, you must give some consideration to the number and size of the partitions to be used by your new operating system. The question of "how many partitions" continues to spark debate within the Linux community and, without any end to the debate in sight, it is safe to say that there are probably as many partition layouts as there are people debating the issue.
记住,我们推荐,除非你有特殊的原因,你应该至少创建下面的分区:swap
、/boot/
(或者是用于 Itanium 系统的 /boot/efi/
分区)、用于 Itanium 系统的 /var/
分区以及 /
(根分区)。