HUB
Sign inSign up

marlabsdocker/lesson5

By marlabsdocker

•Updated almost 9 years ago

Introduction to Linux. Lesson 5 . File Systems. Part 1.

Image
0

10K+

marlabsdocker/lesson5 repository overview

+++++++++++++++++++++++++++++

⁠FILE SYSTEMS.

⁠+++++++++++++++++++++++++++++

File systems are the mechanisms by which the data on a storage medium gets organized. They provide all of the abstraction layers above sectors and cylinders of disks. In this chapter, we’ll discuss the composition and management of these abstraction layers supported by Linux. Particular attention will be given to the default Linux file system, ext2/ext3. We will also cover the many aspects of managing disks. This includes creating partitions and volumes, establishing file systems, automating the process by which they are mounted at boot time, and dealing with them after a system crash. We will also touch on Logical Volume Management (LVM) concepts. NOTE Before beginning your study of this chapter, you should already be familiar with files, directories, permissions, and ownership in the Linux environment.

⁠++++++++++++++++++++++++++++

⁠THE MAKEUP OF FILE SYSTEMS

⁠++++++++++++++++++++++++++++

Let’s begin by going over the structure of file systems under Linux. This will help to clarify your understanding of the concept and let you see more easily how to take advantage of the architecture. i-Nodes The most fundamental building block of many Linux/UNIX file systems is the i-node. An i-node is a control structure that points either to other i-nodes or to data blocks. The control information in the i-node includes the file’s owner, permissions, size, time of last access, creation time, group ID, and so on. (For the truly curious, the entire kernel data structure for the ext2 file system is available in /usr/src/kernels/*/include/ linux/ext2_fs.h—assuming, of course, that you have the source tree installed in the /usr/ src directory.) The one information an i-node does not provide is the file’s name. Directories themselves are special instances of files. This means each directory gets an i-node, and the i-node points to data blocks containing information (filenames and i-nodes) about the files in the directory. The i-nodes are used to provide indirection so that more data blocks can be pointed to—which is why each i-node does not contain the filename. (Only one i-node works as a representative for the entire file; thus, it would be a waste of space if every i-node contained filename information.) Take, for example, a 6-gigabyte (GB) disk that contains 1,079,304 i-nodes. If every i-node consumed 256 bytes to store the filename, a total of about 33 megabytes (MB) would be wasted in storing filenames, even if they weren’t being used! Each indirect block, in turn, can point to other indirect blocks if necessary. With up to three layers of indirection, it is possible to store very large files on a Linux file system.

⁠++++++++++++++++++

⁠Superblocks

⁠++++++++++++++++++

The first piece of information read from a disk is its superblock. This small data structure reveals several key pieces of information, including the disk’s geometry, the amount of available space, and, most importantly, the location of the first i-node. Without a superblock, an on-disk file system is useless. Something as important as the superblock is not left to chance. Multiple copies of this data structure are scattered all over the disk to provide backup in case the first one is damaged. Under Linux’s ext2 file system, a superblock is placed after every group of blocks, which contains i-nodes and data. One group consists of 8192 blocks; thus, the first redundant superblock is at 8193, the second at 16,385, and so on. The designers of most Linux file systems intelligently included this superblock redundancy into the file system design.

⁠ext3 and ReiserFS.

Ext3 and ReiserFS are two popular Linux file systems used by the major Linux distributions. The ext3 file system is an enhanced extension of the ext2 file system. As of this writing, the ext2 file system is somewhere around 16 years old. This means two things for us as system administrators. First and foremost, ext2 is rock-solid. It is a well-tested subsystem of Linux and has had the time to become well optimized. Second, other file systems that were considered experimental when ext2 was created have matured and become available to Linux. The two file systems that are popular replacements for ext2 are the ext3 and ReiserFS file systems. Both offer significant improvements in performance and stability, but the most important component of both is that they have moved to a new method of getting the data to the disk. This new method is called journaling. Traditional file systems (such as ext2) must search through the directory structure, find the right place on disk to lay out the data, and then lay out the data. (Linux can also cache the whole process, including the directory updates, thereby making the process appear faster to the user.) Almost all new versions of Linux distributions now make use of one journaling file system or the other by default. Fedora (and other Red Hat Enterprise Linux [RHEL] derivatives), OpenSuSE, and Ubuntu, for example, use ext3 by default. The problem with not having a journaling file system is that in the event of an unexpected crash, the file system checker or file system consistency checker (fsck) program has to follow up on all of the files on the disk to make sure they don’t contain any dangling references (for example, i-nodes that point to other, invalid i-nodes or data blocks). As disks expand in size and shrink in price, the availability of these large-capacity disks means more of us will have to deal with the aftermath of having to fsck a large disk. And as anyone who has had to do that before can tell you, it isn’t fun. The process can take a long time to complete, and that means downtime for your users. Journaling file systems work by first creating an entry of sorts in a log (or journal) of changes that are about to be made before actually committing the changes to disk. Once this transaction has been committed to disk, the file system goes ahead and modifies the actual data or metadata. This results in an all-or-nothing situation; that is, either all or none of the file system changes get done. One of the benefits of using a journaling-type file system is the greater assurance that data integrity will be preserved, and in the unavoidable situations where problems arise, speed, ease of recovery, and likelihood of success are vastly increased. One such unavoidable situation might be in the event of a system crash. Here, you may not need to run fsck. Think how much faster you could recover a system if you didn’t have to run fsck on a 1 TB disk! (Haven’t had to run fsck on a big disk before? Think about how long it takes to run ScanDisk under Windows on large disks.) Other benefits of using journaling-type file systems are that system reboots are simplified, disk fragmentation is reduced, and input/output (I/O) operations can be accelerated (this depends on the journaling method used). If you want to learn more about the ext2 file system, we recommend that you read the latest edition of the book titled Linux Kernel Internals, edited by Michael Beck (AddisonWesley, 1998). Although the book is dated in many aspects in terms of advancement in the Linux kernel, the parts about the ext2 file system still hold true, since ext2 is the base of the ext3 file system.

⁠+++++++++++++++++++++++

⁠Which File System to Use?

⁠+++++++++++++++++++++++

You might be asking by now, “Which file system should I use?” As of this writing, the current trend is to shift toward any file system with journaling capabilities. As with all things Linux, the choice is yours. Your best bet is to try many file systems and see how they perform with the application you are using the system for. Just keep in mind that journaling has its own overhead.

⁠+++++++++++++++++++++++

⁠MANAGING FILE SYSTEMS

⁠+++++++++++++++++++++++

The process of managing file systems is trivial—that is, the management becomes trivial after you have memorized all aspects of your networked servers, disks, backups, and size requirements, with the condition that they will never again have to change. In other words, managing file systems isn’t trivial at all. Once the file systems have been created, deployed, and added to the backup cycle, they do tend to take care of themselves for the most part. What makes them tricky to manage are the administrative issues—such as users who refuse to do housekeeping on their disks, and cumbersome management policies dictating who can share what disk and under what conditions, depending, of course, on the account under which the storage/disk was purchased, and …(It sounds frighteningly like a Dilbert cartoon strip, but there is a good deal of truth behind that statement.) Unfortunately, there’s no cookbook solution available for dealing with office politics, so in this section, we’ll stick to the technical issues involved in managing file systems— that is, the process of mounting and unmounting partitions, dealing with the /etc/fstab file, and performing file-system recovery with the fsck tool.

⁠++++++++++++++++++++++++++++++++

⁠Mounting and Unmounting Local Disks

⁠++++++++++++++++++++++++++++++++

Linux’s strong points include its flexibility and the way it lends itself to seamless management of file locations. Partitions need to be mounted so that their contents can be accessed. (In actuality, it is the file system on a partition or volume that is mounted.) The file systems are mounted so that they appear as just another subdirectory on the system. This helps to promote the illusion of one large directory tree structure, even though there may be several different file systems in use. This characteristic is especially helpful to the administrator, who can relocate data stored on a physical partition to a new location (possibly a different partition) under the directory tree, with the system users being none the wiser. The file system management process begins with the root directory. This partition is also called slash and likewise symbolized by a slash character (/). The partition containing the kernel and core directory structure is mounted at boot time. It is possible and usual for the physical partition that houses the Linux kernel to be on a separate file system, such as /boot. It is also possible for the root file system (“/”) to house both the kernel and other required utilities and configuration files to bring the system up to single-user mode. As the boot scripts run, additional file systems are mounted, adding to the structure of the root file system. The mount process overlays a single subdirectory with the directory tree of the partition it is trying to mount. For example, let’s say that /dev/sda2 is the root partition. It has the directory /usr, which contains no files. The partition /dev/sda3 contains all the files that you want in /usr, so you mount /dev/sda3 to the directory /usr. Users can now simply change directories to /usr to see all the files from that partition. The user doesn’t need to know that /usr is actually a separate partition. NOTE In this and other chapters, we might inadvertently say that a partition is being mounted at such and such a directory. Please note that it is actually the file system on the partition that is being mounted. For the sake of simplicity, and in keeping with everyday verbiage, we might interchange these two meanings. Keep in mind that when a new directory is mounted, the mount process hides all the contents of the previously mounted directory. So in our /usr example, if the root partition did have files in /usr before mounting /dev/sda3, those /usr files would no longer be visible. (They’re not erased, of course—once /dev/sda3 is unmounted, the /usr files would become visible again.)

⁠+++++++++++++++++++++++++

⁠Using the mount Command

⁠+++++++++++++++++++++++++

Like many command-line tools, the mount command has a plethora of options, most of which you won’t be using in daily work. You can get full details on these options from the mount man page. In this section, we’ll explore the most common uses of the command. The structure of the mount command is as follows: mount [options] device directory Option for mount Description -a Mounts all the file systems listed in /etc/fstab (this file is examined later in this section). -t fstype Specifies the type of file system being mounted. Linux can mount file systems other than the ext2 standard. For example, File Allocation Table (FAT), Virtual File Allocation Table (VFAT), New Technology File System (NTFS), ReiserFS, etc. The mount command can usually sense this information on its own. -o options Specifies options applying to this mount process. These are usually options specific to the file system type (options for mounting network file systems may not apply to mounting local file systems).

Option for the mount -o Parameter (for Local Partitions) Description ro Mounts the partition as read-only. rw Mounts the partition as read/write (default). exec Permits the execution of binaries (default). noatime Disables update of the access time on i-nodes. For partitions where the access time doesn’t matter, enabling this improves performance. Issuing the mount command without any options will list all the currently mounted file systems. For example, type [root@fedora-serverA ~]# mount /dev/mapper/VolGroup00-LogVol00 on / type ext3 (rw) proc on /proc type proc (rw) sysfs on /sys type sysfs (rw) ...(OUTPUT TRUNCATED)... /dev/mapper/VolGroup00-LogVol03 on /tmp type ext3 (rw) /dev/sda1 on /boot type ext3 (rw) sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw) The following mount command mounts the /dev/sda3 partition onto the /bogusdirectory directory with read-only privileges: [root@fedora-serverA ~]# mount -o ro /dev/hda3 /bogus-directory

⁠+++++++++++++++++++++++++++++

⁠Unmounting File Systems

⁠+++++++++++++++++++++++++++++

To unmount a file system, use the umount command (note that the command is not unmount). Here’s the command format: umount [-f] directory where directory is the directory to be unmounted. For example, [root@fedora-serverA ~]# umount /bogus-directory unmounts the partition mounted on the /bogus-directory directory. Option for the mount -o Parameter (for Local Partitions) Description noauto Disables automatic mount of this partition when the -a option is specified (applies only to the /etc/fstab file). nosuid Disallows application of SetUID program bits to the mounted partition. sb=n Tells mount to use block n as the superblock. This is useful when the filesystem might be damaged.

⁠++++++++++++++++++++++++++

⁠When the File System Is in Use

⁠++++++++++++++++++++++++++

There’s a catch to umount: If the file system is in use (that is, someone is currently accessing the contents of the file system or has a file open on the file system), you won’t be able to unmount that file system. To get around this, you have the following choices: ▼ You can use the lsof or fuser program to determine which processes are keeping the files open, and then kill them off or ask the process owners to stop what they’re doing. (Read about the kill parameter in fuser in the fuser man page.) If you choose to kill the processes, be sure you understand the repercussions of doing so (read: Don’t get fired for doing this). ■ You can use the -f option with umount to force the unmount process. It is especially useful for Network File System (NFS)–type file systems that are no longer available. ■ Use the Lazy unmount. This is specified with the -l option. This option almost always works even when others fail. It detaches the file system from the filesystem hierarchy immediately, and it cleans up all references to the file system as soon as the file system stops being busy. ▲ The safest and most proper alternative is to bring the system down to singleuser mode and then unmount the file system. In reality, of course, you don’t always have this luxury.

⁠+++++++++++++++++++++++++

⁠The /etc/fstab File

⁠++++++++++++++++++++++++

As mentioned earlier, /etc/fstab is a configuration file that mount can use. This file contains a list of all partitions known to the system. During the boot process, this list is read and the items in it are automatically mounted with the options specified therein. Here’s the format of entries in the /etc/fstab file: /dev/device /dir/to/mount fstype Parameters fs_freq fs_passno Following is a sample /etc/fstab file:

  1. /dev/VolGroup00/LogVol00 / ext3 defaults 1 1
  2. LABEL=/boot /boot ext3 defaults 1 2
  3. devpts /dev/pts Devpts gid=5,mode=620 0 0
  4. tmpfs /dev/shm tmpfs defaults 0 0
  5. /dev/VolGroup00/LogVol02 /home ext3 defaults 1 2
  6. proc /proc proc defaults 0 0
  7. sysfs /sys sysfs defaults 0 0
  8. /dev/VolGroup00/LogVol03 /tmp ext3 defaults 1 2
  9. /dev/VolGroup00/LogVol01 swap swap defaults 0 0
  10. /dev/sr0 /media/cdrom auto user,noauto,exec 0 0 Let’s take a look at some of the entries in the /etc/fstab file that haven’t yet been discussed. Please note that line numbers have been added to the precceding output to aid readability. Line 1 The first entry in our sample /etc/fstab file is the entry for the root volume. The first column shows the device that houses the file system, i.e., the /dev/VolGroup00/ LogVol00 logical volume (more on volumes later on). The second column shows the mount point, i.e., the “/” directory. The third column shows the file system type, i.e., ext3 in this case. The fourth column shows the options with which the file system should be mounted—only the default options are required in this case. The fifth field is used by the dump utility (a simple backup tool) to determine which file systems need to be backed up. And the sixth and final field is used by the fsck program to determine if the file system needs to be checked and also to determine the order in which the checks are done. Line 2 The next entry in our sample file is the /boot mount point. The first field of this entry shows the device—in this case, it points to any device with the /boot label. The other fields mean basically the same thing as the field for the root mount point discussed previously. In the case of the /boot mount point, you might notice that the field for the device looks a little different from the usual /dev/ convention. The use of labels helps to hide the actual device (partition) that the file system is being mounted from. The device has been replaced with a token that looks like the following: LABEL=/boot. During the initial installation, the partitioning program of the installer automatically set the label on the partition. Upon bootup, the system scans the partition tables and looks for these labels. This is especially useful when Small Computer System Interface (SCSI) disks are being used. Typically, SCSI has a set SCSI ID. Using labels allows you to move the disk around and change the SCSI ID, and the system will still know how to mount the file system even though the device might have changed, for example, from /dev/sda10 to /dev/sdb10 (see the section “Traditional Disk- and Partition-Naming Conventions” further on). Line 4 Next comes the tmpfs file system, also known as a virtual memory (VM) file system. It uses both the system random access memory (RAM) and swap area. It is not a typical block device because it does not exist on top of an underlying block device; it sits directly on top of VM. It is used to request pages from the VM subsystem to store files. The first field—tmpfs—shows that this entry deals with a VM and, as such, is not associated with any regular UNIX/Linux device file. The second entry shows the mount point, /dev/shm. The third field shows the file system type, i.e., tmpfs. The fourth field shows that this file system should be mounted with the default options. The fifth and sixth fields have the same meanings as the ones for the previous entries discussed. Note especially that the values are zero in this case, which makes perfect sense, because there is no reason to run a dump on a temporary file system at bootup and there is also no reason to run fsck on it, since it does not contain an ext2/3-type file system. Line 6 The next notable entry is for the proc-type file system. Information concerning the system processes (hence the abbreviation proc) are dynamically maintained in this file system. The proc in the first field of the proc entry in the /etc/fstab file has the same implication as that of the tmpfs file system entry. The proc file system is a special file system that provides an interface to kernel parameters through what looks like any other file system; that is, it provides an almost human-readable look to the kernel. Although it appears to exist on disk, it really doesn’t—all the files represent something that is in the kernel. Most notable is /dev/kcore, which is the system memory abstracted as a file. People new to the proc file system often mistake this for a large, unnecessary file and accidentally remove it, which will cause the system to malfunction in many glorious ways. Unless you are sure you know what you are doing, it’s a safe bet to leave all the files in the /proc directory alone (more details on /proc appear in Chapter 10). Line 7 Next comes the entry for the sysfs file system. This is new and necessary in the Linux 2.6 kernels. Again, it is temporary and special, just like the tmpfs and proc file systems. It serves as an in-memory repository for system and device status information. It provides a structured view of a system’s device tree. This is akin to viewing the devices in Windows Device Manager as a series of files and directories instead of through Control Panel view. Line 8 The next entry is for the /tmp mount point. This refers to an actual physical entity or device on the system. just like the root (“/”) mount point and the /boot mount point. Line 9 This is the entry for the system swap partition. It is where virtual memory resides. In Linux, the virtual memory can be kept on a separate partition from the root partition. (It should be noted that a regular file can also be used for swap purposes in Linux.) Keeping the swap space on a separate partition helps to improve performance, since the swap partition can obey rules differently from a normal file system. Also, since the partition doesn’t need to be backed up or checked with fsck at boot time, the last two parameters on it are zeroed out. (Note that a swap partition can be kept in a normal disk file as well. See the man page on mkswap for additional information.) Line 10 The last entry in the fstab file that is worthy of mentioning is the entry for the removable media. In this example, the device field points to the device file that represents the CD-ROM device. The CD-ROM drive here is the master of the secondary Integrated Drive Electronics (IDE) controller (/dev/hdc). The mount point is /media/cdrom, and so when a CD-ROM is inserted and mounted on the system, the contents of the CD can be accessed from the /media/cdrom directory. The auto in the third field means that the system will automatically try to probe/detect the correct file system type for the device. For CD-ROMs, this is usually the iso9660 or the Universal Disk Format (UDF) file system. The fourth field lists the mount options. NOTE When mounting partitions with the /etc/fstab file configured, you can run the mount command with only one parameter: the directory you wish to mount to. The mount command checks /etc/fstab for that directory; if found, mount will use all parameters that have already been established there. For example, here’s the short command to mount a CD-ROM given the /etc/fstab file shown earlier: [root@fedora-serverA ~]# mount /media/cdrom/

Tag summary

Content type

Image

Digest

Size

91 MB

Last updated

almost 9 years ago

docker pull marlabsdocker/lesson5