Linux文件系统&Linux内核编译
本文最后更新于一年前或更久前,其中的信息可能已经有所发展或是发生改变。

文件系统

题目要求

The following exercise examines the relationship between files and inodes on a UNIX or Linux system. On these systems, files are represented with inodes. That is, an inode is a file (and vice versa). You can complete this exercise on the Linux virtual machine that is provided with this text. You can also complete the exercise on any Linux, UNIX, or Mac OS X system, but it will require creating two simple text files named file1.txt and file3.txt whose contents are unique sentences.
In the source code available with this text, open file1.txt and examine its contents. Next, obtain the inode number of this file with the command ls -li file1.txt This will produce output similar to the following: 16980 -rw-r–r– 2 os os 22 Sep 14 16:13 file1.txt where the inode number is boldfaced. (The inode number of file1.txt is likely to be different on your system.) The UNIX ln command creates a link between a source and target file. This command works as follows: ln [-s] <source file> <target file> UNIX provides two types of links: (1) hard links and (2) soft links. A hard link creates a separate target file that has the same inode as the source file. Enter the following command to create a hard link between file1.txt and file2.txt: ln file1.txt file2.txt What are the inode values of file1.txt and file2.txt? Are they the same or different? Do the two files have the same—or different— contents? Next, edit file2.txt and change its contents. After you have done so, examine the contents of file1.txt. Are the contents of file1.txt and file2.txt the same or different? Next, enter the following command which removes file1.txt: rm file1.txt Does file2.txt still exist as well? Now examine the man pages for both the rm and unlink commands. Afterwards, remove file2.txt by entering the command strace rm file2.txt The strace command traces the execution of system calls as the command rm file2.txt is run. What system call is used for removing file2.txt? A soft link (or symbolic link) creates a new file that “points” to the name of the file it is linking to. In the source code available with this text, create a soft link to file3.txt by entering the following command: ln -s file3.txt file4.txt After you have done so, obtain the inode numbers of file3.txt and file4.txt using the command ls -li file*.txt Are the inodes the same, or is each unique? Next, edit the contents of file4.txt. Have the contents of file3.txt been altered as well? Last, delete file3.txt. After you have done so, explain what happens when you attempt to edit file4.txt.

解决方案

我们首先按照题目要求创建file1.txt和file3.txt这两个文件,并向其中写入不同的内容

随后使用ls命令查看file1.txt的相关信息,我们可以看到其 inode number 为 2238055.

根据网络上找到的资料:

inode 是 UNIX 操作系统中的一种数据结构,其本质是结构体,它包含了与文件系统中各个文件相关的一些重要信息。在 UNIX 中创建文件系统时,同时将会创建大量的 inode 。通常,文件系统磁盘空间中大约百分之一空间分配给了 inode 表。

有时,人们使用了一些不同的术语,如 inode 和索引编号 (inumber)。这两个术语非常相似,并且相互关联,但它们所指的并不是同样的概念。inode 指的是数据结构;而索引编号实际上是 inode 的标识编号,因此也称其为inode 编号 或者索引编号。索引编号只是文件相关信息中一项重要的内容。下一个部分将介绍 inode 中的其他一些属性。

inode 表包含一份清单,其中列出了对应文件系统的所有 inode 编号。当用户搜索或者访问一个文件时,UNIX 系统通过 inode 表查找正确的 inode 编号。在找到 inode 编号之后,相关的命令才可以访问该 inode ,并对其进行适当的更改。

例如,使用 vi 来编辑一个文件。当您键入 vi 时,在 inode 表中找到 inode 编号之后,才允许您打开该 inode 。在 vi 的编辑会话期间,更改了该 inode 中的某些属性,当您完成操作并键入 :wq 时,将关闭并释放该 inode 。通过这种方式,如果两个用户试图对同一个文件进行编辑, inode 已经在第一个编辑会话期间分配给了另一个用户 ID (UID),因此第二个编辑任务就必须等待,直到该 inode 释放为止。

接下来测试文件链接,在Linux下有着【硬链接】与【软链接】两种文件链接的方式:

– 对于硬链接文件而言,其所对应的inode number相同,对任一文件的修改会影响所有文件,也就是说两个硬链接了的文件本身就是一个文件,只有当我们删除所有硬链接文件后该文件才会被删除;

– 而对于软链接而言,链接文件仅仅是指向源文件,其与源文件inode不同,当源文件被删除之后链接文件则会失效,也就是说,软连接更像是Windows下的【快捷方式】

使用硬链接链接file1.txt和file2.txt后的结果:

接下来使用vimfile2进行编辑,保存后发现file1同时也被改动了

 

rm file1.txt后,我们可以看到file2.txt仍然存在

使用strace rm file2.txt后,我们可以看到rm file2.txt的系统调用,