Linux file system---User interface to the kernel

Source: Internet
Author: User

From disk to data, from data to files, from files to directories, from directories to file systems, from file systems to operating systems. constitutes the IO read-write mechanism in the computer.

The entire disk can be divided into 1 mbr (Master Boot Record) and 4 partitions. A partition, in fact, is a file system, the file system is the management of the organization's hard disk data on a bunch of structure programs, a file system consists of many block blocks. Block is the smallest unit of file system read-write storage data, usually 4KB. Each block can contain only one file of data. If the file is larger than the block size, the file occupies more than one block. For files, it is divided into catalog items, inode, data blocks. For both the operating system and the application, the files on the hard disk are accessed through the file system.

1, hard disk read and write principle

Most permanent or semi-permanent computer data are implemented by magnetization of a small piece of metal on the disk. These magnetic graphs can then be converted into raw data. This is the principle of magnetic storage.

We know that the information is stored on the hard drive, and it is not visible that there is anything in it, only some platters. Assuming that you enlarge the disc with a microscope, you will see the uneven surface of the platter, the convex place is magnetized, the concave place is not magnetized, the raised place represents the number 1 (magnetization is 1), the concave place represents the number 0. Therefore, the hard disk can be binary to store representations of text, pictures and other information.

Hard disk, we can liken it to a large warehouse where our computers store data and information. Generally speaking, no matter what kind of hard disk, it is composed of platters, head, disc spindle, control motor, head controller, data converter, interface, cache and so on.

The head is the most expensive part of the hard drive, and it is also the most important and critical link in hard disk technology.

When the disk is rotated and the head remains in one position, each head will draw a circular trajectory on the disk surface called the track. These tracks are invisible to the naked eye, because they are only magnetic areas that are magnetized in a special way on the disk, and the information on the disks is stored along such tracks. Each track on the disk is divided into segments, which are the mountains of the disk, where each sector can hold 512 bytes of information, and the disk drives are sector-based when reading and writing data to disk. 1.44mb3.5-inch floppy disks, each track divided into 18 sectors.

Disks are usually composed of overlapping sets of platters, each of which is divided into equal numbers of tracks and numbered from the "0" of the outer rim, with the same numbered tracks forming a cylinder called the disk's cylinder. The number of cylinders on a disk is equal to the number of tracks on one side of a disk. The number of disks equals the total number of heads, since each disc has its own unique head, whether it is a double-disc or a single-disc face. The so-called CHS, namely cylinder (cylinder), head (head), Sector (sector), as long as the number of CHS to know the hard disk, you can determine the capacity of the hard disk, the capacity = number of cylinders * Number of heads * Number of sectors *512b.

2. Documents

There is a philosophy in Linux, all documents. So, what are the file types under the Linux file system? How are files stored in the file system?

Under Linux, the following seven file types are commonly used:

1), ordinary files regular file, with-(dash) marked, such as-rwxr--r--, Rxw front-Indicates this is a normal file.

2), directory files directory, marked with D, such as drwx------directory is also a file, which holds the file name and document index node relationship between.

3), block equipment File Block Deivce, with the B flag, the actual expression of hardware equipment, can be accessed in the/dev directory, including disk drive CD drive.

4), character equipment file character device, with C mark, also means hardware equipment, but the data is sent by byte stream, equipment including terminal equipment and serial device.

5), linked link device with L flag, is the link created by ln, divided into soft and hard links.

6), named pipe files pipe with P Mark, pipeline is also a file, as a data pipeline convenient communication between programs, also known as FIFO.

7) Socket file socket is marked with s, and unlike pipelines, sockets enable communication between processes of different computers connected over a network.

When a file is executed, the process accesses the file itself directly through its file name. Thus, each time the file is read, the process is as follows:

The file name----(through the catalog entry)----The >inode number----(via the inode)-----> get the address of the document (block numbers)----> read the file.

In Linux file systems (such as ext2, Ext3, EXT4), a file consists of a directory entry, an inode, and a data block.

1), directory entry: includes the file name and inode node number.

2), Inode: Also known as the file Index node, is a file containing the basic information and data block pointers.

Linux Orthodox file system, the partition of the hard disk will be divided into the directory block, Inode table block and data block area. The inode contains the attributes of the file (such as read and write properties, owner, and pointers to the data block), and the data region block is the file content. When you view a file, the file attributes and data points are isolated from the Inode table and the data is read from the data block. To understand the inode, start with the file storage. The file is stored on the hard disk, and the minimum storage unit of the hard disk is called "Sector" (Sector), with each sector storing 512 bytes (equivalent to 0.5KB).

When the operating system reads the hard disk, it does not read one sector at a time, so the efficiency is too low, but it reads multiple sectors at once, that is, a one-time read "block". This "block", composed of multiple sectors, is the smallest unit of file access. "Block" size, the most common is 4KB, that is, eight consecutive sector to form a block.


File data is stored in "blocks", then obviously, we must also find a place to store the meta-information of the file, such as the creator of the file, the date the file was created, the size of the file, and so on. This area of stored file meta information is called Inode, and the Chinese name is "Index node".

3), data block: The specific content of the file to be stored.

3. File directory

The directory, in effect, is a mapping of the system's access to individual file combinations under the file. Under Linux, both the device and the hard disk are mounted to a directory, the most primitive being mounted to the root directory. The user then accesses the data on the hard drive by accessing the directory, which is the file. So, the directory is a real access to the hard disk file data. The directory structure under Linux is as follows:

/bin: The system has a lot of directories to place the execution files, but the/bin is quite special. Because the/bin is placed in the single maintenance mode can also be operated instructions. The commands under/bin can be used by root and general accounts, mainly: Cat,chmod (Modify permissions), Chown, date, MV, mkdir, CP, bash, etc. commonly used directives.

/boot: The main file to be used to boot, including the Linux core files and boot menu and boot required profiles and so on. Linux kernel commonly used file name: Vmlinuz, if you are using grub this boot manager, there will also be/boot/grub/this directory.

/dev: On Linux systems, any device and peripherals are in this directory in the form of files. Simply by accessing a file in this directory, you are accessing a device. More important files are/dev/null,/dev/zero,/dev/tty,/dev/lp*,/dev/hd*,/dev/sd*, etc.

/etc: The main configuration of the system is placed in this directory, such as the person's account password file, the start of various services and so on. In general, the file attributes in this directory are accessible to the general user, but only root has the power to modify it. In addition, the following important directories are:/etc/init.d/. The preset startup script for all services is placed here, for example to start or close iptables:/etc/init.d/iptables start,/etc/init.d/iptables stop.

/HOMT: This is the user home directory (home directory) of the system presets. When you add a general user account, the default user home directory will be standardized here. More importantly, the home directory has two types of code: ~: Represents the current user's home directory, and ~guest: Represents the home directory of users named guest.

/lib: The function Library of the system is very much, and the/lib place is the function library that will be used at boot, and the function library that the instruction under/bin or/sbin will call. What is a function library? You can think of him as a plug-in, some instructions must have these plug-ins in order to successfully complete the implementation of the program meaning. It is particularly important to/lib/modules/this directory, as it will place the core-related modules (drivers).

/media:media is the media of English, as the name implies, this/media is placed under the removable device. The devices, including soft discs, CDs, DVDs and so on, are temporarily mounted here. Common file names are:/media/floppy,/media/cdrom, and so on.

/MNT: If you want to temporarily mount some additional devices, it is generally recommended that you place them in this directory.

/OPT: This is a directory for third-party software placement.

/root: The home directory of the system administrator (root).

/sbin:linux has a lot of instructions to set up the system environment, these instructions only root can use to set up the system, the other users can only be used to query. Placed under the/sbin for the boot process required, which includes the boot, repair, restore the system required instructions.

/srv:srv can be regarded as the abbreviation of service, which is the data directory that some services need to use after the network service is started. Common services such as WWW, FTP and so on. For example, the Web page data required by the WWW server can be placed inside the/srv/www/. Oh, it seems that the usual code we write should be put here.

/tmp: This is where the general user or an executing program temporarily places the file. This directory is accessible to anyone, so clean it up regularly.


4. File system

A file system is a method and data structure that is used in the operating system to clarify files on a disk or partition, that is, the method of organizing files on disk. The existence of a file system allows data to be accessed efficiently and transparently. Traditionally, a partition can only be formatted as a file system, so a filesystem is generally considered a partition. In computers, DOS, Windows, OS/2, Macintosh, and unix-based operating systems have file systems in which files are placed somewhere in a hierarchical (tree-like) structure. The file is placed into a directory or subdirectory in the tree structure where you want it.

The difference between a disk or partition and the file system it includes is important. A few programs (including the most justified programs that produce file systems) operate directly on the original sector of the disk or partition, which can corrupt an existing file system. Most programs operate on file systems and do not work on different types of file systems.

Under the Linux system, you can view Linux hard disk partitions and file system information through df-th, while du-sh is viewing file size information:

When installing a Linux system, the hard disk must be properly partitioned according to the purpose of the server. In the process of system operation, with the change of business, the requirement of increasing data storage, the disk storage needs to be adjusted and managed. Currently the most used on the server, the most mature and reliable technology is still the traditional mechanical hard disk. With the development of technology, flash-based SSD drives have been used in quite a number of areas. The hard disk is, of course, used to store the data, which is the file of the system and the user, and the partitioned file system specifies the rules for the data storage unit to be arranged on the hard disk.

Different file systems store data differently, and these different file systems can coexist on the same system's hard disk, but cannot coexist on the same disk partition. One of the most important features of Linux is the support for multiple file systems.

Linux, through the VFS (Virtual file system), supports several different file systems, enabling the sharing of resources with different operating environments. With regard to file systems, there is an important philosophy under Linux, that is, all documents. The principle of its realization is through the VFS. As a subsystem of the kernel, the virtual file system (VFS) provides the file system interface for the user space program, and all the file systems in the system rely on the VFS system to coordinate the work. With the virtual file system, you can use standard UNIX system calls to read and write different files of different media without having to consider the specific file system and physical media.

All, in general. From disk to data, from data to files, from files to directories, from directories to file systems, from different file systems to operating systems, there is a complex implementation principle, but the underlying design ideas form the basis of the IO read-write mechanism in the computer.

Linux file System---User interface to the kernel

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.