Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of Linux system structure

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the example analysis of Linux system structure, which is very detailed and has certain reference value. Friends who are interested must finish it!

Generally speaking, the Linux system has four main parts:

Kernel, shell, file system, and applications. Together, the kernel, shell, and the file system form the basic operating system structure that allows users to run programs, manage files, and use the system. Part of the hierarchy is shown in figure 1-1.

1. Linux kernel

The kernel is the core of the operating system, which has many basic functions. It is responsible for managing the process, memory, device drivers, files and network system of the system, and determines the performance and stability of the system.

The Linux kernel consists of the following parts: memory management, process management, device driver, file system and network management. As shown in the figure:

Figure 1

System call interface: the SCI layer provides some mechanism to perform function calls from user space to the kernel. This interface depends on architecture, even within the same processor family. SCI is actually a very useful function call multiplexing and demultiplexing service. You can find the implementation of SCI in. / linux/kernel and the architecture-dependent parts in. / linux/arch.

1. Memory management

For any computer, its memory and other resources are limited. In order to make the limited physical memory meet the large memory demand of applications, Linux adopts a memory management method called "virtual memory". Linux divides memory into easy-to-handle "memory pages" (4KB for most architectures). Linux includes ways to manage available memory, as well as hardware mechanisms used for physical and virtual mapping.

But memory management doesn't just manage 4KB buffers. Linux provides abstractions for 4KB buffers, such as the slab allocator. This memory management model uses 4KB buffers as the cardinality, allocates structures from them, and tracks memory page usage, such as which pages are full, which pages are not fully used, and which pages are empty. This allows the mode to dynamically adjust memory usage according to system needs.

In order to support multiple users to use memory, there are sometimes situations where available memory is consumed. For this reason, pages can be moved out of memory and placed on disk. This process is called swapping because pages are swapped from memory to the hard disk. The source code for memory management can be found in. / linux/mm.

2. Process management

A process is actually a running entity of a particular application. In a Linux system, multiple processes can be run at the same time, and Linux can realize "multitasking" by running these processes in turn in a short interval of time. This short time interval is called "time slice", the method of letting the process run in turn is called "process scheduling", and the program that completes the scheduling is called the scheduler.

Process scheduling controls the process's access to CPU. When you need to select the next process to run, it is up to the scheduler to select the process that is most worth running. A runnable process is actually a process that only waits for CPU resources, and if a process is waiting for other resources, it is not runnable. Linux uses a relatively simple priority-based process scheduling algorithm to select new processes.

Through the multitasking mechanism, each process can be regarded as monopolizing the computer, thus simplifying the writing of the program. Each process has its own separate address space and can only be accessed by this process, so that the operating system avoids mutual interference between processes and the harm that "bad" programs may cause to the system. In order to accomplish a particular task, it is sometimes necessary to combine the functions of two programs, such as one program outputs text and the other program sorts the text. To this end, the operating system also provides an inter-process communication mechanism to help accomplish such tasks. The common inter-process communication mechanisms in Linux are signals, pipes, shared memory, semaphores, sockets and so on.

The kernel provides an application programming interface (API) through SCI to create a new process (fork, exec, or Portable Operating System Interface [POS Ⅸ] function), stop processes (kill, exit), and communicate and synchronize between them (signal or POS Ⅸ mechanism).

3. File system

Unlike operating systems such as DOS, individual file systems in Linux operating systems are not identified by drive letters or drive names (such as A: or C:, etc.). Instead, like the UNIX operating system, the Linux operating system combines independent file systems into a hierarchical tree structure that is represented by a single entity. Linux mounts a new file system to a directory through an operation called "mount" or "mount", thus combining different file systems into a whole. An important feature of the Linux operating system is that it supports many different types of file systems. The most commonly used file system in Linux is Ext2, which is also Linux's home-grown file system. However, Linux can also support different types of file systems, such as FAT, VFAT, FAT32, MINIX, etc., so that it can easily exchange data with other operating systems. Because Linux supports many different file systems and organizes them into a unified virtual file system.

Virtual file system (VirtualFileSystem,VFS): hides the specific details of various hardware, separates the file system operation from the specific implementation details of different file systems, and provides a unified interface for all devices. VFS provides dozens of different file systems. Virtual file system can be divided into logical file system and device driver. Logical file system refers to the file system supported by Linux, such as ext2,fat, and device driver refers to the device driver module written for each kind of hardware controller.

Virtual file system (VFS) is a very useful aspect of the Linux kernel because it provides a common interface abstraction for file systems. VFS provides a switching layer between SCI and the file systems supported by the kernel. That is, VFS provides a switching layer between the user and the file system.

VFS provides a switching layer between the user and the file system:

Above VFS is a generic API abstraction of functions such as open, close, read, and write. Below VFS is the file system abstraction, which defines how the upper-level functions are implemented. They are plug-ins for a given file system (more than 50). The source code for the file system can be found in. / linux/fs.

Below the file system layer is the buffer cache, which provides a common set of functions for the file system layer (independent of the specific file system). This cache layer optimizes access to physical devices by keeping the data for a period of time (or then reading the data in advance to be available when needed). Beneath the buffer cache is the device driver, which implements the interface to a specific physical device.

Therefore, users and processes do not need to know the file system type on which the files reside, but only need to use them like files in the Ext2 file system.

4. Device driver

Device drivers are the main part of the Linux kernel. Like other parts of the operating system, device drivers run in a highly privileged processor environment, so they can operate directly on the hardware, but because of this, any error in the device driver can lead to a crash of the operating system. The device driver actually controls the interaction between the operating system and hardware devices.

The device driver provides a set of abstract interfaces that can be understood by the operating system to complete the interaction between the operating system and the operating system, while the specific operation details related to the hardware are completed by the device driver. Generally speaking, the device driver is related to the control chip of the device. For example, if your computer's hard disk is a SCSI hard drive, you need to use a SCSI driver instead of an IDE driver.

5. Network Interface (NET)

It provides access to various network standards and support for all kinds of network hardware. Network interface can be divided into network protocol and network driver. The network protocol part is responsible for implementing every possible network transport protocol. As we all know, TCP/IP is not only the standard protocol of Internet, but also the de facto industrial standard.

The network implementation of Linux supports BSD sockets and all TCP/IP protocols. The network part of Linux kernel consists of BSD socket, network protocol layer and network device driver. The network device driver is responsible for communicating with the hardware device, and every possible hardware device has a corresponding device driver.

II. Linux shell

Shell is the user interface of the system, which provides an interface for users to interact with the kernel. It receives the command entered by the user and sends it to the kernel for execution. It is a command interpreter. In addition, shell programming language has many characteristics of ordinary programming language, and shell programs written in this programming language have the same effect as other application programs.

Currently, there are mainly the following versions of shell.

1. Bourne Shell: it was developed by Bell Labs.

2. BASH: GNU's Bourne Again Shell, the default shell on the GNU operating system, and most linux distributions use this shell.

3. Korn Shell: it is the development of Bourne SHell and is compatible with Bourne Shell in most content.

4. C Shell: it is the BSD version of SUN's Shell.

III. Linux file system

A file system is a method of organizing files on storage devices such as disks. Linux systems can support a variety of popular file systems, such as EXT2, EXT3, FAT, FAT32, VFAT and ISO9660.

3.1 File types

The following file types of Linux are:

1) ordinary files: C language meta-code, SHELL scripts, binary executable files, etc. It is divided into plain text and binary.

2) Catalog files: directories, the only place where files are stored.

3) linked file: a file that points to the same file or directory.

4) device files: related to system peripherals, usually under / dev. It is divided into block devices and character devices.

5) FIFO file: a way to provide process building communication

6) socket file: this file type is related to network communication

You can view the file type and other related information through the ls-l, file, stat commands.

3.2 Linux directory

File structure is the organization method in which files are stored on disk and other storage devices. It is mainly reflected in the organization of files and directories.

Directories provide a convenient and effective way to manage files.

Linux uses a standard directory structure, and at the time of installation, the installer has created a file system and a complete and fixed directory composition for the user, and specifies the role of each directory and the file types in it.

The complete directory tree can be divided into small parts, which can be stored separately on your own disk or partition. In this way, relatively stable parts and frequently changing parts can be stored separately in different partitions, thus facilitating backup or system management. The main parts of the directory tree are root, / usr, / var, / home, and so on (figure 2). This layout makes it easy to share parts of the file system between Linux computers.

Figure 2

Linux uses a tree structure. The top layer is the root directory, and all other directories are generated from the root directory.

Microsoft's DOS and windows also use a tree structure, but in DOS and windows, the root of such a tree structure is the drive letter of a disk partition, and several partitions have several tree structures, and the relationship between them is juxtaposed. At the top of the list are different disks (partitions), such as: C-Magi-D-J-E-F and so on.

But in linux, no matter how many disk partitions the operating system manages, there is only one such directory tree. Structurally, the tree directories on each disk partition are not necessarily juxtaposed.

3.3 Linux disk Partition

1. Primary partition, extended partition and logical partition:

Linux partition is different from windows. Both hard disk and hard disk partition are represented as devices in Linux.

There are three kinds of hard disk partitions: primary partition, extended partition and logical partition.

The partition of the hard disk is mainly divided into two types: primary partition (Primary Partion) and extended partition (Extension Partion). The sum of the number of primary partition and extended partition can not be more than four.

Primary Primary Partion: can be used immediately but can no longer be partitioned.

Extended partition (Extension Partion): it must be partitioned again before it can be used, that is, it must also be partitioned twice.

Logical partition ((Logical Partion)): a partition created by an extended partition. There is no limit to the number of logical partitions.

Extended partitions are just "containers" of logical partitions. In fact, only primary and logical partitions are used for data storage.

2. Identification of hard disk partition under Linux

The identification of hard disk partitions is generally identified by / dev/hd [a murz] X or / dev/sd [a murz] X, where [A murz] represents the hard disk number and X represents the partition number in the hard disk.

Block number identification of the whole hard disk partition: hda, hdb, sda, sdb and so on are used to identify different hard drives under Linux.

Where:

IDE interface hard disk: expressed as / dev/hda1, / dev/hdb.

The hard disk of SCSI interface and the hard disk of SATA interface are expressed as / dev/sda, / dev/sdb. ...

Partition in the hard disk: if the value of X is 1 to 4, it indicates the primary partition of the hard disk (including the extended partition); the logical partition starts from 5, for example, / dev/hda5 must be a logical partition.

For example:

Use hda1, hda2, hda5, and hda6 to identify different partitions. The letter a represents the first hard disk, b represents the second hard disk, and so on. The number 1 represents the first partition of a hard disk, 2 represents the second partition, and so on. 1 to 4 correspond to the primary partition (Primary Partition) or the extended partition (Extension Partition). Starting from 5, it all corresponds to the logical partition (Logical Partition) of the hard disk. Even if a hard disk has only one primary partition, logical partitions are numbered starting with 5, which should be paid special attention to.

Summary: a hard disk partition should first confirm which hard disk it is on, and then confirm which partition in the hard drive it is located in.

It's not uncommon to use a similar representation of / dev/hda; we can find out whether the hard drive is / dev/hda or / dev/hdb through fdisk-l in Linux.

[root@localhost] # fdisk-l Disk / dev/hda: 80.0 GB, 80026361856 bytes 255heads, 63 sectors/track 9729 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Device Boot Start End Blocks Id System / dev/hda1 * 1,9707791493 + 7 HPFS/NTFS / dev/hda2 971 9729 70356667 + 5 Extended / dev/hda5 971 2915 15623181 b W95 FAT32 / dev/hda6 2916 4131 9767488 + 83 linux / dev/hda7 4132 5590 1171986 83 linux / dev/hda8 5591 6806 9767488 + 83 linux / dev/hda9 6807 965722900626 83 linux / dev/hda10 9658 97298308 + 82 linux swap / Solaris

Please note the first line, Disk / dev/hda: 80.0 GB, 80026361856 bytes, this means that there is only one hard disk device / dev/hda in the machine, the size is 80.0G; the following is the partition of the hard disk, each partition has detailed information, so I won't go into details here

The relationship between disk partitions and directories under Linux is as follows:

-any partition must be mounted to a directory.

-the directory is a logical distinction. Zoning is a physical distinction.

-disk Linux partitions must be mounted to a specific directory in the directory tree before you can read and write.

-the root directory is where all the Linux files and directories are located, and a disk partition needs to be mounted.

3.4 the function of the main directory of linux.

/ bin binary executable commands / dev device special files / etc system management and configuration files / etc/rc.d startup configuration files and scripts / the base point of the home user home directory, for example, the home directory of the user user is / home/user, which can be represented by ~ user / lib standard programming library, also known as dynamic link sharing library, which acts like .dll files / sbin system management commands in windows What is stored here is the management program used by the system administrator / the tmp common temporary file storage point / the home directory of the root system administrator (hehe, privileged class) / the mnt system provides this directory for users to temporarily mount other file systems. / lost+found this directory is usually empty, the system abnormal shutdown and left the "homeless" file (what is called under the windows .chk) here / proc virtual directory, is the mapping of system memory. You can access this directory directly to get system information. / var the overflow area of some large files, such as the log files for various services / the largest directory of usr, where almost all the applications and files to be used are located. It contains: / usr/X11R6 directory where X window is stored / usr/bin 's numerous applications / some hypervisors of usr/sbin superusers / usr/doc linux documents / header files needed to develop and compile applications under usr/include linux / usr/lib commonly used dynamic link libraries and software package configuration text Pieces / usr/man help documentation / usr/src source code The source code of the linux kernel is placed in / usr/src/linux / commands added locally in usr/local/bin / libraries added locally in usr/local/lib

3.5 linux file system

The file system refers to the physical space where the file exists, and each partition in the linux system is a file system with its own directory hierarchy. Linux will form a system's overall directory hierarchy of these separate file systems that belong to different partitions in a certain way. The operation of an operating system is inseparable from the operation of files, so it is necessary to own and maintain its own file system.

1. File system type:

Ext2: a file system commonly used in early linux

Ext3: an upgraded version of ext2 with log function

RAMFS: memory file system, fast

NFS: network file system, invented by SUN, used primarily for remote file sharing

MS-DOS: MS-DOS file system

VFAT: the file system used in the Windows 95gam98 operating system

FAT: the file system used by the Windows XP operating system

NTFS: the file system used by the Windows NT/XP operating system

HPFS: the file system used by the OS/2 operating system

PROC: virtual process file system

ISO9660: the file system used by most CDs

UfsSun: the file system used by OS

NCPFS: the file system used by the Novell server

SMBFS: Samba's shared file system

XFS: an advanced journaling file system developed by SGI that supports very large files

JFS: the journaling file system used by IBM's AIX

ReiserFS: file system based on balanced Tree structure

Udf: erasable data CD file system

2. File system characteristics:

After the partition is completed, the disk needs to be formatted (format) before the operating system can use the partition. The purpose of formatting is to make the file system format available to the operating system (that is, we mentioned the file system type above).

Each operating system can use different file systems. For example, the Microsoft operating system before windows 98 mainly uses FAT (or FAT16), the version after windows 2000 has the so-called NTFS file system, and the orthodox file system of Linux is Ext2 (Linux second extended file system, ext2fs). In addition, by default, the windows operating system does not recognize Linux's Ext2.

In traditional disk and file system applications, a partition can only be formatted into a file system, so we can say that a filesystem is a partition. But due to the use of new technologies, such as LVM and Software disk Array (software raid), these technologies can format a partition into multiple file systems (such as LVM), or synthesize multiple partitions into a single file system (LVM, RAID)! So, at present, we are no longer talking about formatting for partition, usually we can call a mountable data a file system rather than a partition!

So how does the file system work? This is related to the file data of the operating system. In addition to the actual content of the file, the file data of newer operating systems usually contains many attributes, such as file permissions (rwx) and file attributes (owner, group, time parameters, etc.) of the Linux operating system. The file system usually stores these two parts of data in different blocks, permissions and attributes in the inode, and the actual data in the data block block. In addition, there is a super block (superblock) that records the overall information of the entire file system, including the total amount of inode and block, usage, remaining, and so on.

For a disk partition, after being designated as the corresponding file system, the entire partition is divided into 1024pr 2048 and 4096 byte blocks. Depending on the use of the block, it can be divided into:

1. Superblock (Superblock): this is the first block of space in the entire file system. Including the basic information of the entire file system, such as block size, total inode/block, usage, surplus, pointers to spatial inode and data blocks, and other related information.

2. Inode block (file index node): file system index, recording the attributes of the file. It is the most basic unit of the file system and the bridge between any subdirectory and any file in the file system. There is only one unique inode block per subdirectory and file. It contains the basic attributes of files in the file system (file length, creation and modification time, permissions, ownership relationship), location of data storage and other related information. Under Linux, you can view the inode information of a file through the "ls-li" command. Hard connections and source files have the same inode.

3. Data block (Block): actually record the contents of the file. If the file is too large, it will take up multiple block. In order to improve the efficiency of directory access, Linux also provides a dentry structure to express the relationship between path and inode. It describes the path information and connects to the node inode, which includes various directory information, as well as points to inode and super blocks.

It's like a book with a cover, a catalogue and a text. In the file system, the super block is equivalent to the cover, from which you can know the basic information of the book; the inode block is the directory, from which you can know the location of the chapters; and the data block is equivalent to the body of the book, recording the specific content.

Linux orthodox file systems (such as ext2, 3, etc.) partition the hard disk into super blocks, inode Table blocks, and data block data areas. A file consists of a super block, an inode, and a data area block. Inode contains the attributes of the file (such as read-write attributes, owner, and so on, and pointers to data blocks), while data area blocks are the contents of the file. When viewing a file, the file attributes and data store points are first found in the inode table, and then the data is read from the data block.

Schematic diagram of ext2 file system

Let's illustrate the inode and block blocks with diagrams. As shown in the following figure, the file system formats the inode and block blocks first, assuming that the attribute and permission data of a file is placed in inode 4 (in the smaller box below), and this inode records the actual placement points of the file data as the four block numbers 2, 7, 13, 15. Our operating system will be able to arrange the reading order of the disk accordingly. You can read out four block contents in one breath! Then the data is read as specified by the arrow in the following figure.

Figure inode/block data access schematic diagram

This method of data access is called indexed file system (indexed allocation). Are there any other idiomatic file systems that can be compared? Yes, that is our usual flash drive (flash memory). The file system used by flash drive is usually in FAT format. There is no inode in the file system of FAT format, so FAT has no way to read out all the block of this file at the beginning. Each block number is recorded in the previous block, which is read a bit like the following figure:

Figure, data access diagram of FAT file system

In the figure above, we assume that the data of the file is written into the four block numbers 1-> 7-> 4-> 15, but the file system has no way to know the numbers of the four block in one breath. He has to read the block one by one before he knows where the next block is. If the block written to the same file is too scattered, our disk read head will not be able to read all the data in a circle on the disk, so the disk will rotate several more times to fully read the contents of the file!

Do you often hear the so-called "defragmentation"? The reason for the need for defragmentation is that the block written to the file is too discrete, and the performance of file reading will become very poor. At this time, the blocks belonging to the same file can be gathered together through defragmentation, so that the data can be read more easily. If you think about it, FAT's file system needs to be defragmented frequently, so does Ext2 need disk reorganization?

Because Ext2 is an indexed file system, it doesn't need to be defragmented very often. However, if the file system is used for too long and often deletes / edits / adds files, it may still cause the problem that the file data is too discrete and may need to be restructured at this time. However, to be honest, Bird has not defragmented the Ext2/Ext3 file system on the Linux operating system. It doesn't seem to be necessary! ^ _ ^

You can use the ln command to establish a new connection to an existing file without copying the contents of the file. Connections can be divided into soft connections and hard connections, and soft connections are also called symbolic connections. Their respective characteristics are as follows:

Hard connection: both the original file name and the connection file name point to the same physical address. Directories cannot have hard connections; hard connections cannot span file systems (cannot span different partitions) there is only one copy of files on disk, saving hard disk space

Because deleting files can only be successful if the same Inode belongs to a unique connection, unnecessary misdeletions can be prevented.

Symbolic connection: using the ln-s command to establish a symbolic link to a file is a special linux file whose data is the pathname of the file to which it connects. Similar to shortcuts under windows.

You can delete the original file and save the connection file without preventing erroneous deletion.

The content of this paragraph is too abstract, which is both node and array. I have tried my best to be popular and popular, but it is not easy to add examples to demonstrate. If you are still in the clouds, there is nothing I can do. I can only remember that I will slowly experience and understand in the practical application in the future. This is also a way for me to learn.

3.6 representation of the file system in the kernel

Kernel data structure

The VFS subsystem of the Linux kernel can be illustrated as follows:

File and IO: each process holds a table of file descriptors in PCB (Process Control Block). The file descriptor is the index of this table, and each table item has a pointer to an open file. Now let's be clear: open files are represented by file structures in the kernel, and pointers in the file descriptor table point to file structures.

Maintains the File Status Flag (member f_flags of the file structure) and the current read-write location (member f_pos of the file structure) in the file structure. In the figure above, both process 1 and process 2 open the same file, but correspond to different file structures, so they can have different File Status Flag and read and write locations. The more important member of the file structure is f_count, which represents the reference count (Reference Count). As we will talk about later, system calls such as dup and fork will cause multiple file descriptors to point to the same file structure. For example, if both fd1 and fd2 refer to the same file structure, then its reference count is 2. When close (fd1) does not release the file structure, it just reduces the reference count to 1. If close (fd2) The reference count is reduced to 0 and the file structure is freed, which really closes the file.

Each file structure points to a file_operations structure whose members are function pointers to kernel functions that implement various file operations. For example, read a file descriptor in the user program, and read enters the kernel through system call, then finds the file structure pointed to by the file descriptor, finds the file_operations structure pointed to by the file structure, and calls the kernel function pointed to by its read member to complete the user request. When the functions such as lseek, read, write, ioctl and open are called in the user program, the kernel calls the kernel function pointed to by each member of file_operations to complete the user request.

The release member in the file_operations structure is used to complete the close request of the user program. It is called release rather than close because it does not necessarily close the file, but reduces the reference count, only when the reference count is reduced to 0. For regular files opened on the same file system, the steps and methods for operating files such as read and write should be the same, and the functions called should be the same, so the file structures of the three open files in the figure point to the same file_operations structure. If you open a character device file, its read and write operations must be different from regular files, not reading and writing disk blocks but reading and writing hardware devices, so the file structure should point to different file_operations structures, in which various file operation functions are implemented by the device driver.

Each file structure has a pointer to the dentry structure, and "dentry" is an acronym for directory entry (directory entries). What we pass to the parameters of the open, stat, and other functions is a path, such as / home/akaedu/a, according to which we need to find the inode of the file. To reduce the number of disk reads, the kernel caches the tree structure of the directory, called dentry cache, where each node is a dentry structure, just search along the dentry of each part of the path, find the home directory from the root directory, then find the akaedu directory, and then find the file a. Dentry cache saves only the most recently accessed directory entries, and if the directory entries you are looking for are not in cache, read them from disk to memory.

Each dentry structure has a pointer to the inode structure. The inode structure holds information read from disk inode. In the example in the figure above, there are two dentry, representing / home/akaedu/an and / home/akaedu/b, both pointing to the same inode, indicating that the two files are hard links to each other. The inode structure holds information read from the inode of the disk partition, such as owner, file size, file type, and permission bits. Each inode structure has a pointer to the inode_operations structure, which is also a set of function pointers to kernel functions that complete file directory operations.

Unlike file_operations, inode_operations does not point to functions that operate on a file, but functions that affect the layout of files and directories, such as adding and deleting files and directories, tracking symbolic links, and so on. Inode structures belonging to the same file system can point to the same inode_operations structure.

The inode structure has a pointer to the super_block structure. The super_block structure holds information read from super blocks of disk partitions, such as file system type, block size, and so on. The s_root member of the super_block structure is a pointer to dentry, indicating where the root directory of the file system is mount, and in the example above, the partition is mount to the / home directory.

File, dentry, inode and super_block constitute the core concepts of VFS. For the ext2 file system, there are also concepts of inode and super blocks in the disk storage layout, so it is easy to correspond to the concepts in VFS. While some other file system formats come from non-UNIX systems (such as Windows's FAT32, NTFS), they may not have the concept of inode or super block, but in order to mount to the Linux system, they have to be cobbled together in the driver. If you look at the FAT32 and NTFS partitions under Linux, you will find that the permission bits are wrong and all files are rwxrwxrwx, because they do not have the concept of inode and permission bits.

3.6 Mount the file system

Each partition in the linux system is a file system with its own directory hierarchy. Linux will form a system's overall directory hierarchy of these separate file systems that belong to different partitions in a certain way. The term "in a certain way" here refers to mounting.

Mount the top-level directory of one file system to a subdirectory of another file system, making them a whole, called mount. This subdirectory is called the mount point.

For example, when you want to read a formatted partition, CD, or software on a hard disk, you must first map these devices to a directory, which is called a "mount point (mount point)" so that you can read these devices. After mounting, the physical partition details are shielded, and the user has only a unified logical concept. Everything is a file.

Note: 1. The mount point must be a directory.

2. A partition is mounted on an existing directory, which may not be empty, but the previous contents in this directory will not be available after mounting.

The same is true for mounts of file systems established by other operating systems. But it is important to understand that the format of the file system used by CDs, floppy disks, and other operating systems is different from that used by linux. CD is ISO9660; floppy disk is fat16 or ext2;windows NT is fat16, NTFS;windows98 is fat16, fat32;windows2000 and windowsXP are fat16, fat32, NTFS. Before mounting, you should know whether linux supports the file system format that you want to mount.

Use the mount command when mounting, in the format of mount [- parameter] [device name] [mount point]

The commonly used parameters are

-t specifies the file system type of the device (what is the file type mentioned)

-o specify options for mounting the file system. Some can also be used in / etc/fstab. The commonly used ones are

Codepage=XXX code page iocharset=XXX character set ro mounts rw read-only mounts nouser read-write makes it impossible for ordinary users to mount user allows ordinary users to mount devices

For example:

1. Mount the file system of windows:

1) first, we use sudo fdisk-l to view the mounted devices, for example, at the bottom is: / dev/hda5

2) mkdir creates a directory, and the directory here is used as a hanging directory, that is, you want to hang the E disk to this directory: mk / mnt/winc

3) windows and linux do not use the same file system. Generally speaking, linux does not mount the windows file system, so you need to manually mount:

# mount-t vfat / dev/hda5 / mnt/winc (- t vfat indicates the file system fat32 here)

You can now go to directories such as / mnt/winc to read and write these files.

2. Mount CD: # mk / mnt/cdrom

# mount-t iso9660 / dev/cdrom / mnt/cdrom (the name of the shutdown is usually cdrom, and this command is generally common)

3. Virtual machine shared folder: for example, under VirtualBox, the host is Windows,Ubuntu and Guest. There are three steps:

1)。 First of all, install the virtual computer toolkit: select "device"-> "install Virtual computer Toolkit" in the VirtualBox menu, and you will find an extra CD icon on the Ubuntu desktop, which is automatically loaded into the folder / media/cdom0 by default, and / cdrom automatically points to this folder. The file manager automatically opens the CD by default, and you can see that there is a "VBoxLinuxAdditions.run" file inside. Open a command line terminal and type "cd / cdrom" and then "sudo sh. / VBoxLinuxAdditions.run" without double quotation marks to start the installation of the toolkit. After installation, you will be prompted to restart Ubuntu in English. It is recommended to restart it immediately. After rebooting, the obvious change is that the mouse is in shared mode and the clipboard is shared with Windows. If there are these changes, it means that the virtual computer toolkit has been installed successfully.

2)。 The next step is to set up shared folders.

In the shared folder settings window, click "add a shared folder" on the right, select the Windows folder you want to share in the path, and choose any one you like with the shared name, such as "myshare". The option read-only refers to whether only ubuntu is allowed to read this folder, please select this option as needed.

3)。 Mount the shared folder under ubuntu: sudo mount-t vboxsf myshare / media/share

Where "myshare" is the name of the previously shared folder, and "/ media/share" is the target file to mount to.

3.7 automatically mount windows partitions

It is obviously too cumbersome to run the mount command every time you boot to access the windows partition, so why don't you use the mount command to access other linux partitions?

In fact, every time you boot, linux automatically mounts the linux partition that needs to be mounted. So can we configure linux to mount the partition we want to mount at startup, such as windows partition, so that the file system can be mounted automatically?

This is absolutely possible. There is a fstab file in the / etc directory that lists the file systems that are mounted automatically when linux boots. My / etc/fstab file is as follows:

/ dev/hda2 / ext3 defaults 1 1 / dev/hda1 / boot ext3 defaults 12 none / dev/pts devpts gid=5,mode=620 0 0 none / proc proc defaults 0 0 none / dev/shm tmpfs defaults 0 0 / dev/hda3 swap swap defaults 0 0 / dev/cdrom / mnt/cdrom iso9660 noauto,codepage=936,iocharset=gb2312 0 0 / dev/fd0 / mnt/floppy auto noauto,owner Kudzu 0 0 / dev/hdb1 / mnt/winc vfat defaults,codepage=936,iocharset=cp936 0 0 / dev/hda5 / mnt/wind vfat defaults,codepage=936,iocharset=cp936 0 0

In the / etc/fstab file, the first column is the device name of the mounted file system, the second column is the mount point, the third column is the mounted file system type, and the fourth column is the mount options, separated by commas. I don't know what the fifth and sixth column means, but I still hope the master can give me some advice.

The last two lines are the C; D disk under windows that I added manually, with codepage=936 and iocharset=cp936 parameters added to support the Chinese file name. The parameter defaults actually contains a set of default parameters:

Rw is mounted in read-write mode

Suid enables user ID and group ID setting bits

Dev can interpret characters or block devices on a file system

Exec executable binary file

Auto automatic mount

Nouser makes it impossible for ordinary users to mount.

Async performs input and output operations of the file system in an asynchronous manner

You can see that in this list, optical and floppy drives are not automatically mounted, and the parameter is set to noauto. If you have to set it to mount automatically, make sure you have a disk in your CD-ROM and floppy drive every time you boot, hehe.)

3.8. Soft links, hard links

You can use the ln command to establish a new connection to an existing file without copying the contents of the file. Connections can be divided into soft connections and hard connections, and soft connections are also called symbolic connections. Their respective characteristics are as follows:

Hard connection: is to give a copy of the file, the original file name and the connection file name point to the same physical address. Directories cannot have hard connections; hard connections cannot span file systems (cannot span different partitions) there is only one copy of files on disk, saving hard disk space

Modify one of them, and the file connected to it is modified at the same time. If you delete any of the remaining files, it will not be affected.

Because deleting files can only be successful if the same Inode belongs to a unique connection, unnecessary misdeletions can be prevented.

Symbolic connection (soft connection): establishing a symbolic connection of a file with the ln-s command is a special linux file whose data is the pathname of the file to which it is connected. Similar to shortcuts under windows.

Of course, deleting this connection will not affect the source file, but the use and reference of the connection file will call the source file directly.

The specific relationship can be seen in the following figure:

Figure 5: soft and hard links

You can see the difference between hard links and soft links from the figure:

1: the inode numbers of the hard link original file and the new file are the same. Soft links are different.

2: deleting the original file will cause the soft link to be unavailable, while the hard link will not be affected.

3: for the modification of the original file, the contents of the soft and hard link files are also modified, because they all point to the same file content.

3.9. File directory management command

Disk and file space: fdisk df du

File directory and management: cd pwd mkdir rmdir ls cp rm mv

View file contents cat, tac, more, less, head, tail

File directory and permissions: chmod chown chgrp umask

File lookup: which, whereis, locate, find, find

4. Linux application

Standard Linux systems generally have a set of assemblies called applications, which include text editors, programming languages, X Window, office suites, Internet tools, and databases.

5. Linux kernel parameter optimization.

Kernel parameters are an interactive interface between the user and the system kernel. Through this interface, users can dynamically update the kernel configuration while the system is running, and these kernel parameters exist through the Linux Proc file system. Therefore, you can optimize Linux performance by tuning the Proc file system.

The above is all the contents of the article "sample Analysis of Linux system structure". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report