📄 lsg18.htm
字号:
<META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW">
<SCRIPT>
<!--
function displayWindow(url, width, height) {
var Win = window.open(url,"displayWindow",'width=' + width +
',height=' + height + ',resizable=1,scrollbars=yes');
}
//-->
</SCRIPT>
</HEAD>
-->
<LINK REL="ToC" HREF="index.htm">
<LINK REL="Index" HREF="htindex.htm">
<LINK REL="Next" HREF="lsg19.htm">
<A NAME="I0"></A>
<H2>Linux System Administrator's Survival Guide lsg18.htm</H2>
<P ALIGN=LEFT>
<HR ALIGN=CENTER>
<P>
<UL>
<UL>
<UL>
<LI>
<A HREF="#E68E98" >Mounting and Unmounting Filesystems</A>
<LI>
<A HREF="#E68E99" >Mounting Filesystems Automatically with the /etc/fstab File</A>
<UL>
<LI>
<A HREF="#E69E130" >Filesystem Types</A>
<LI>
<A HREF="#E69E131" >Options Values</A></UL>
<LI>
<A HREF="#E68E100" >Managing Disk Space</A>
<UL>
<LI>
<A HREF="#E69E132" >Checking Filesystems</A>
<LI>
<A HREF="#E69E133" >Displaying Filesystem Statistics</A>
<LI>
<A HREF="#E69E134" > Making the Most of Your Disk Space</A></UL>
<LI>
<A HREF="#E68E101" >Understanding Links</A>
<LI>
<A HREF="#E68E102" >Summary</A></UL></UL></UL>
<HR ALIGN=CENTER>
<A NAME="E66E21"></A>
<H1 ALIGN=CENTER>
<CENTER>
<FONT SIZE=6 COLOR="#FF0000"><B>Chapter 18</B></FONT></CENTER></H1>
<BR>
<A NAME="E67E24"></A>
<H2 ALIGN=CENTER>
<CENTER>
<FONT SIZE=6 COLOR="#FF0000"><B>Filesystems and Disks</B></FONT></CENTER></H2>
<BR>
<P>One of a system administrator's most important tasks is managing the Linux system's hard disks and filesystems. Keeping both in proper order helps the Linux operating system perform at its best. This task involves doing a set of actions regularly. This chapter describes the actions involved in keeping the Linux filesystems and the hard disks they reside on in peak condition. (This chapter does not look at the steps involved in adding new hard disks to your Linux system, as this was covered in <A HREF="lsg08.htm">Chapter 8</A>, "Hard Disks.")
<BR>
<P>The general actions a system administrator must perform to keep filesystems performing smoothly are the following:
<BR>
<UL>
<LI>Check filesystems for corrupt sectors
<BR>
<BR>
<LI>Check filesystems for integrity and correct i-node tables
<BR>
<BR>
<LI>Check file permissions and ownerships to ensure proper access
<BR>
<BR>
<LI>Make filesystems (local and remote) available to users as necessary
<BR>
<BR>
<LI>Manage the Linux system's disk space
<BR>
<BR>
<LI>Perform regular backups for data security
<BR>
<BR>
</UL>
<P>Although some of these actions are performed automatically every time Linux boots (such as checking the filesystem for corruption), you should know how to force these processes manually, as well as know what they do and how to correct problems that may arise. With the exception of performing backups for data security (covered in more detail in <A HREF="lsg22.htm">Chapter 22</A>, "Backup, Backup, Backup!") and checking file permissions (covered in <A HREF="lsg17.htm">Chapter 17</A>), this chapter looks at all these actions.
<BR>
<BR>
<A NAME="E68E98"></A>
<H3 ALIGN=CENTER>
<CENTER>
<FONT SIZE=5 COLOR="#FF0000"><B>Mounting and Unmounting Filesystems</B></FONT></CENTER></H3>
<BR>
<P>To understand why filesystems must be mounted, you have to know how Linux organizes the disks and filesystems that make up the entire directory structure. Linux uses a single directory structure, regardless of how many disks and disk partitions are involved. Each partition's filesystem must be part of the larger directory structure. The entire directory tree has only one root directory, and other filesystems are attached at lower levels.
<BR>
<P>To visualize this concept, imagine a standard Linux filesystem with the root partition (/) at the top; all the other partitions branch off from the root partition. The root partition is on a partition of the first hard disk. Usually, that disk also has other directories on it, such as /dev, /lib, /etc, and so on. Essentially, all the directories needed to start a minimal Linux operating system have to be on the primary partition.
<BR>
<P>However, suppose you want to have a very large /usr filesystem because you intend to support a lot of users with very large database files. Your primary disk partition may not be able to contain all the files you want to save, so you can use another partition (on the same or a different hard disk) and format it as a Linux filesystem, and then attach it to the root filesystem at the /usr directory point. Whenever a user moves from the /bin directory (on the first partition) to /usr/tparker, for example, the user moves to another partition or disk. The move across partitions is completely unnoticeable to the user because the two partitions look like a single directory tree. The /usr directory is said to be mounted on the root directory.
<BR>
<P>More accurately, the partition that holds the /usr filesystem is mounted on the root filesystem in the /usr location. It could just as easily have been mounted at the /home location. Linux doesn't care where you mount a filesystem as long as you mount it as a directory that exists in the root filesystem (so /usr or /home, depending on where you mount the filesystem, would have to be an empty directory on the root fileystem) and no conflict exists between directory names. If the partition were mounted at /home, the user would access /home/tparker instead of /usr/tparker.
<BR>
<P>You can stretch this concept even further. Suppose one user, such as /usr/tparker, has to access a very large library of pictures stored on a CD-ROM drive. You can attach the filesystem on the CD-ROM to the existing filesystem as /usr/tparker/cd_rom, for example, with the operating system knowing to move to the CD-ROM whenever the user accesses that directory. Again, this transition is unnoticeable to the user. This example shows that you can mount a filesystem onto another mounted filesystem.
<BR>
<P>Linux lets you mount partitions anywhere from any source, as long as they fit into the overall filesystem structure. The only place you cannot mount a filesystem is at the root directory location, which must exist on the root filesystem.Linux also allows you to mount some other operating system filesystems, such as a DOS or OS/2 filesystem, onto your Linux filesystem. Essentially, you let Linux know where to access the filesystem (/dos, /usr/dos, or some other directory name) and tell Linux the type of filesystem, and it lets you move through that filesystem's directories and files as you would any Linux directory. You can mount a filesystem in only one location at a time; you cannot mount one filesystem (of any kind) as both /usr and /home, for example.
<BR>
<P>All these filesystem mounting options make Linux very versatile. If a friend has a hard drive full of data you want to access and the data is a filesystem Linux can understand, your friend can bring the hard drive to your machine and attach it to your controller, and then you can mount your friend's filesystem anywhere that is available on your existing filesystem. You can mount any device that can hold a filesystem, including CD-ROMs, floppy disks, magneto-optical drives, removable cartridges, and so on.
<BR>
<P>To mount a filesystem, you use the mount command. The general syntax of the mount command is
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">mount <I>device_name mount_point</I></FONT></PRE>
<P>where <I>device_name</I> is the name of the device (partition, hard disk, CD-ROM, and so on) and <I>mount_point</I> is the name of the directory to which you want to mount the device. For example, to mount the partition /dev/sda4 (fourth partition on the first SCSI hard disk) to the /usr directory, issue the following command:
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">mount /dev/sda4 /usr</FONT></PRE>
<P>To mount a CD-ROM filesystem (such as /dev/cdrom) on the directory /cdrom (assuming the directory exists), use the following command:
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">mount /dev/cdrom /cdrom</FONT></PRE>
<P>Alternatively, you can use the following command to mount a CD-ROM filesystem, because you can mount a filesystem anywhere:
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">mount /dev/cdrom /usr/tparker/data/pictures/cd-rom</FONT></PRE>
<BLOCKQUOTE>
<BLOCKQUOTE>
<HR ALIGN=CENTER>
<BR>
<NOTE>Only the root mounts and unmounts filesystems. Although it's possible to enable users to mount filesystems, this practice can lead to security problems and is therefore generally discouraged. Log in as the superuser to mount or unmount filesystems.</NOTE>
<BR>
<HR ALIGN=CENTER>
</BLOCKQUOTE></BLOCKQUOTE>
<P>You can mount a filesystem as read-only so that any attempt to write to the filesystem generates an error message. This feature is useful to prevent frustrated users of a mounted CD-ROM filesystem, for example, or if you want to make sure nobody writes to a mounted filesystem on another partition (which may contain data you don't want to be corrupted). To mount a filesystem as read-only, use the -r option:
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">mount -r /dev/cdrom /cdrom</FONT></PRE>
<P>Some older versions of UNIX and Linux allow the -r option to be at the end of the command line:
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">mount /dev/cdrom /cdrom -r</FONT></PRE>
<P>When one of the mounted filesystems is disconnected (so users cannot access the directories), the filesystem has been unmounted. Any mounted filesystem can be unmounted except for the root filesystem, which is always active. To unmount a filesystem, use the umount command. (One of the most common errors for system administrators is typing this command as unmount instead of umount). The umount command takes the name of either the device or the mount point. To unmount the CD-ROM mounted in the last example, you can use either of the following two commands:
<BR>
<PRE>
<FONT COLOR="#000080">umount /dev/cdrom
umount /cdrom</FONT></PRE>
<P>You don't have to unmount all filesystems before you shut down the system, as Linux can handle the unmounting as part of the shutdown process.
<BR>
<BR>
<A NAME="E68E99"></A>
<H3 ALIGN=CENTER>
<CENTER>
<FONT SIZE=5 COLOR="#FF0000"><B>Mounting Filesystems Automatically with the /etc/fstab File</B></FONT></CENTER></H3>
<BR>
<P>Any previously mounted filesystems are not necessarily mounted automatically when the system restarts(other than root, which is always mounted automatically when the system starts). When Linux boots, it must know where to find the filesystems to be mounted. Linux uses the /etc/rc initialization file (run when Linux boots) to execute the command:
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">mount -av</FONT></PRE>
<P>When Linux executes this command, it knows to read the file /etc/fstab to find out which filesystems have to be mounted and where they should be mounted.
<BR>
<BLOCKQUOTE>
<BLOCKQUOTE>
<HR ALIGN=CENTER>
<BR>
<NOTE>You also can use the following command to mount all the filesystems in the /etc/fstab file:
<BR>mountall
<BR>Not all versions of Linux support the mountall command, but all should support the mount command line.</NOTE>
<BR>
<HR ALIGN=CENTER>
</BLOCKQUOTE></BLOCKQUOTE>
<P>Each line in the /etc/fstab file follows this format:
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">device mount_location filesystem_type options dump_frequency pass_number</FONT></PRE>
<P>This section looks at a few of these parameters in more detail, as well as provide valid values. In practice, the /etc/fstab file is an ASCII file composed of several columns. The following is a sample /etc/fstab file:
<BR>
<PRE>
<FONT COLOR="#000080">/dev/sda1 / ext2 defaults 1 1
/dev/sda2 /usr ext2 defaults 1 1
/dev/sda3 /usr/data ext2 defaults 1 1
/dev/cdrom /cdrom iso9660 ro 1 1
/dev/sda4 /dos msdos defaults 1 1
/dev/sdb1 /data ext2 defaults 1 1</FONT></PRE>
<P>This rather complex-looking table is quite easy to understand. The first column gives the device name, followed by the mount point, the type of filesystem, and instructions about how to treat the filesystem. For example, the root filesystem in the above table is /dev/sda1 and is a typical ext2 Linux filesystem. The CD-ROM device is mounted as /cdrom; it is an ISO 9660 (CD-ROM) filesystem and is mounted as read-only. The DOS filesystem is mounted as /dos.
<BR>
<P>Linux mounts the filesystems in the order they are given in /etc/fstab. Note in the preceding sample file that the entry that mounts /usr/data follows the entry that mounts /usr. If the /usr/data entry came before the /usr entry, the mount wouldn't work because the /usr directory wouldn't yet exist. If one mount fails, Linux ignores it and executes the rest of the entries. If a mount of a directory that is used further down the file fails, the dependent mounts fail too. For example, if the mount of /usr fails for some reason, the mount of /usr/data fails too, as /usr doesn't exist.
<BR>
<P>The last two numbers on each line in /etc/fstab show the dump frequency and the pass number. These two numbers do not mean anything with some versions of Linux, so check the fstab man page for more information. The dump frequency tells Linux how often the filesystem should be backed up. One means the backup should occur daily, two means the backup should be every other day, and so on. This number is used for automated backup routines that can parse the /etc/fstab file for this information.
<BR>
<P>The pass number indicates the order in which the fsck utility should check the filesystem. One means the filesystem should be checked first, two means it should be checked second, and so on. If more than one filesystem has a pass number of one, the filesystems are checked in the order they occur in the /etc/fstab file. The root filesystem must have a value of one, and the convention is to set other partitions higher. However, because most Linux versions don't use the pass number, all filesystems usually have this number set to one. If your version of Linux does use this number and you have more than one disk drive on your Linux filesystem, set the numbers on each disk in order (1, 2, 3 and so on to match the mount order), and then use a parallel scheme for each additional disk drive. This way, fsck checks filesystems on each disk in parallel.
<BR>
<P>You can include swap partitions can be included in the /etc/fstab file as well. List these partitions as type swap, with the mount directory set to none and the dump frequency and pass number set to zero, as shown in the following example:
<BR>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -