Mounting Linux File System and high-level architecture (1)

Source: Internet
Author: User

We have introduced the Linux File System in this article. In terms of file systems, Linux can be regarded as the "Swiss Army Knife" in the operating system ". Linux supports many file systems, from log file systems to cluster file systems and encrypted file systems.

Linux is an excellent platform for using standard and fancy file systems and developing file systems. This article discusses the Virtual File System VFS in the Linux kernel, sometimes called the Virtual File System switch), and then introduces the main structure of connecting the file system together.

Basic File System Architecture

The Linux File System architecture is an interesting example of abstracting complex systems. By using a set of common API functions, Linux supports many file systems on many storage devices. For example, a read function call can read a certain number of bytes from a specified file descriptor. The read function does not know the type of the file system, such as ext3 or NFS. It also does not know the storage media of the file system, such as AT Attachment Packet InterfaceATAPI) disk, Serial-Attached SCSISAS) disk, or Serial Advanced Technology AttachmentSATA) disk. However, when you call the read function to read a file, the data will return normally. This article describes how to implement this mechanism and introduces the main structure of the Linux File System layer.

What is a file system?

First, answer the most common question "What is a file system ". A file system organizes data and metadata on a storage device. Since the definition is so broad, the code that supports it is very interesting. As mentioned above, there are many file systems and media. Due to the existence of so many types, we can expect that the Linux File System Interface is implemented as a hierarchical architecture, thus separating the user interface layer, file system implementation from the driver of the operating storage device.

Mounting

In Linux, the process of associating a file system with a storage device is called mounting ). Use the mount command to attach a file system to the root of the current file system hierarchy ). During mounting, you must provide the file system type, file system, and a mount point.

To illustrate the functions of the Linux File System layer and the mounting method), we create a file system in a file in the current file system. The implementation method is to first use the dd command to create a file of the specified size and use/dev/zero as the source line to copy the file)-in other words, a file initialized with zero, see list 1.

Listing 1. Creating an initialized File

 
 
  1. $ dd if=/dev/zero of=file.img bs=1k count=10000 
  2. 10000+0 records in  
  3. 10000+0 records out  

Now there is a 10 MB file. img file. Use the losetup command to associate a cyclic device with this file to make it look like a block device, rather than a regular file in the file system:

 
 
  1. $ losetup /dev/loop0 file.img  

This file is now represented by/dev/loop0 as a block device ). Then, use mke2fs to create a file system on this device. This command creates a new ext2 File System of the specified size, as shown in Listing 2.

Listing 2. Create an ext2 file system with a circulating Device

 
 
  1. $ mke2fs -c /dev/loop0 10000  
  2. mke2fs 1.35 (28-Feb-2004)  
  3. max_blocks 1024000, rsv_groups = 1250, rsv_gdb = 39 
  4. Filesystem label=  
  5. OS type: Linux  
  6. Block size=1024 (log=0)  
  7. Fragment size=1024 (log=0)  
  8. 2512 inodes, 10000 blocks  
  9. 500 blocks (5.00%) reserved for the super user  
  10. ...  

Use the mount command to mount the file. img file represented by the cyclic device/dev/loop0 to the mount point/mnt/point1. Note that the file system type is ext2. After mounting, you can use this mount point as a new file system, such as using the ls command, as shown in listing 3.

Listing 3. Create a mount point and mount the file system through a circulating Device

 
 
  1. $ mkdir /mnt/point1  
  2. $ mount -t ext2 /dev/loop0 /mnt/point1  
  3. $ ls /mnt/point1  
  4. lost+found  

As shown in Listing 4, you can continue this process: Create a new file in the mounted file system, associate it with a cyclic device, and then create another file system on it.

Listing 4. Create a new cyclic File System in the cyclic File System

 
 
  1. $ dd if=/dev/zero of=/mnt/point1/file.img bs=1k count=1000 
  2. 1000+0 records in  
  3. 1000+0 records out  
  4. $ losetup /dev/loop1 /mnt/point1/file.img  
  5. $ mke2fs -c /dev/loop1 1000  
  6. mke2fs 1.35 (28-Feb-2004)  
  7. max_blocks 1024000, rsv_groups = 125, rsv_gdb = 3 
  8. Filesystem label=  
  9. ...  
  10. $ mkdir /mnt/point2  
  11. $ mount -t ext2 /dev/loop1 /mnt/point2  
  12. $ ls /mnt/point2  
  13. lost+found  
  14. $ ls /mnt/point1  
  15. file.img lost+found  

Through this simple demonstration, you can easily see how powerful the Linux File System and cyclic device are. You can create an encrypted file system on a cyclic device in the same way. You can use cyclic devices to temporarily Mount files as needed, which helps protect data.

File System Architecture

Now that we have seen the file system construction method, let's look at the Linux File System layer architecture. This article examines the Linux File System from two perspectives. First, use the high-level architecture. Then we will discuss the main structure of implementing the file system layer in depth.

High-level Architecture

Except for the user space file systems discussed later in the kernel ), however, the architecture shown in Figure 1 shows the relationship between the user space and the main components related to the file system in the kernel.

Figure 1. Architecture of Linux File System Components

The user space contains some applications, such as file system users) and gnu c library glibc, which provide user interfaces for opening, reading, writing, and closing file system calls. The function of a system call interface is like a switch, which sends system calls from the user space to an appropriate endpoint in the kernel space.

VFS is the main interface of the underlying file system. This component exports a set of interfaces and abstracts them to various file systems. The behavior of each file system may vary greatly. There are two cache inode and dentry for file system objects ). They cache recently used file system objects.

Each file system implementation, such as ext2 and JFS) exports a set of common interfaces for VFS. The buffer cache caches requests between the file system and related Block devices. For example, read/write requests to underlying device drivers are passed through the buffer cache. This allows you to cache requests to reduce the number of accesses to physical devices and speed up access. Manage the buffer cache in the form of recently used LRU) list. Note: You can use the sync command to send requests in the buffer cache to the storage media to force all unwritten data to be sent to the device driver and then to the storage device ).

This is the high-level situation of VFS and file system components. Now we will discuss the main structure for implementing this subsystem.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.