这里讲的主要是关于文件,目录以及一部分磁盘的相关知识。主要依据是操作系统第4章 文件管理(参考书:王道的书)和鸟哥的私房菜的第567章。
在系统运行时,计算机以进程为基本单位进行资源的调度和分配;而在用户进行的输入、输出中,则以文件为基本单位。
1、什么是文件?
从比喻的角度来讲,操作系统是图书管理员对于书的管理,则文件就是图书馆的书。
从底层向上的角度来讲,数据项:用于描述一个对象的某种属性值,如姓名,日期或证件号(注意这三个例子中的一个就是数据项)
--->记录:一组相关数据项的集合,如一个考生报名记录包括:姓名,出生日期,学号,身份证号等等。
--->文件:由创建者所定义的一组相关数据集合,逻辑上可分为结构文件(类似于记录组成)和无结构文件(字符流,又称流式文件,在Linux下使用od反汇编的二进制文件?)两种。
对于结构文件分为顺序文件和索引文件。其中的顺序文件分为串结构和顺序结构。
另外,我拜读了 David Both的三篇文件An introducition to Linux filesystems,An introduction to Linux‘s EXT4 filesystem和Everything is files。
对于深刻理解什么是文件(上面三篇文章做的简述):
这三篇文章分别从3个方面讲述文件,第一篇是从一个high-level讲述Linux文件系统的概念。对应的第二篇是从low-level描述特殊的文件系统类型EXT4。最后一篇主要想说就是题目everything is files。
第一篇:
先讲述了电脑需要存储各种各样类型的数据在HDD(hard disk drive)或者USBmemory stick之类的地方。因为1、standard volatile RAM将会在他断电后丢失内容(如DDR3 DDR4),而flash RAM太贵了(如USB 和solid state drive SSD)2、standard RAM依然比磁盘空间贵,16GB of RAM vs. a 2TB hard drive贵71倍。如今一个典型的RAM大概是$0.0000000043743750/Byte。(注:这个价格是文章给出的,我大概算了一下某东的金士顿DDR3 8G 450/16/1024/1024/1024/6.8=$0.000000007703 貌似涨价了一倍?)。
然后对于filesystems从不同角度进行了定义(definitions):(filesystems is)1、the entire Linux directory structure starting at the top(/)root directory;2、A specific type of data storage format,such as EXT34,BTRS,XFS and so on.(Linux supports almost 100 types of filesystems),3、A partition or logical volume formatted with a specific type of filesystem that can be mounted on a specified mount point on a Linux filesystem.
1、 Basic filesystem function is to provide space for non-volatile storage of data.All filesystems need to provide a namespace --that is ,a naming and organizational methodology.It also defines the logical structure of data on a disk ,such as the use of directories for organizing files instead of just dumping them all together in a single,huge conglomeration(混合物) of files.
Once the namespace has been defined,a metadata structure is necessary to provide the logical foundation for that namespace.
Filesystem also require an Application Programming Interface(API) that provide access to system function calls which manipulate filesystem objects like files amd directories.APIs provide for tasks such as creating,moving,and deleting files.It also provides algorithms that determine things like where a file is placed on filesystem.
Modern filesystem also provide a security model,which is a scheme for defining access rights of files and directories.
The final building block is the software required to implement all of these functions.Linux use a two-part software implementation as a way to improve both system and programmer efficiency.
(First Part)Virtual File System provides a single set of commands for the kernel,and developers, to access all types of filesystems.
(Second Part)the filesystem-specific device drivers (特定文件系统的设备驱动) interprets the standard set of filesystem commands to ones secific to the type of filesystem on the paratition or logical volume.
Directory Structure--------Tree-like hierarchy
The Linux directory structure is well defined and documented in the Linux Filesystem Hierarchy Standard (FHS).
/ :The root filesystem is the top-level directory of the filesystem.All the things(such as files executables and libraries)required to boot are in it.After the system is booted,all other filesystems are mounted on standard .well-defined mount points as subdirectories of the root filesystem.
/bin :This directory contains user executable files.
/boot :Contains the static bootloader and kernel executable and configuration files required to boot the Linux computer.
/dev :This deirectory contains the device files for every hardware device attached to the system.These are not device drivers,rather they are files that represent each divice on the computer and facilitate(帮助) access to those devices.
/etc :Contains the local system configuration files for the host computer.
/home :Home directory storage for user files. Each user has a subdirectory in /home.
/lib :Contains shared library files that are required to boot the system.
/media :A place to mount external removable media devices such as USB thumb drives that may be connected to the host.
/mnt :A temporary mountpoint for regular filesystems (as in not removable media) that can be used while the administrator is repairing or working on a filesystem.
/opt :Optional files(鸟叔书中称为第三方协力文件) such as vendor supplied application programs should be located here.
/root :This is not the root (/) filesystem. It is the home directory for the root user.
/sbin :System binary files. These are executables used for system administration.
/tmp :Temporary directory. Used by the operating system and many programs to store temporary files. Users may also store files here temporarily. Note that files stored here may be deleted at any time without prior notice.
/usr :(Unix software resource,like ProgramFiles in windows)These are shareable, read-only files, including executable binaries and libraries, man files, and other types of documentation.
/var :Variable data files are stored here. This can include things like log files, MySQL, and other database files, web server data files, email inboxes, and much more.
1、All the red directories and their subdirectories cannot be created as a separate filesystem and mounted at startup time. This is because they (specifically, their contents) must be present at boot time in order for the system to boot properly.
2、The /media and /mnt directories are part of the root filesystem, but they should never contain any data. Rather, they are simply temporary mount points.
3、All the black directories do not need to be present during the boot sequence, but will be mounted later, during the startup sequence that prepares the host to perform useful work.
Linux unified directory structure
In windows you need to know a file or program is located on which hard drives,such as C: D: E:.Then you can use cd command to change to the correct directory to locate the desired file. Each hard drive has its own separate and complete directory tree.But linux filesystem unifies all physical hard drives and partitions into a single directory structure.
This can work only because a filesystem, such as /home, /tmp, /var, /opt, or /usr can be created on separate physical hard drives, a different partition, or a different logical volume from the / (root) filesystem and then be mounted on a mountpoint (directory) as part of the root filesystem tree. Even removable drives such as a USB thumb drive or an external USB or ESATA hard drive will be mounted onto the root filesystem and become an integral part of that directory tree.
One good reason to do this is apparent during an upgrade from one version of a Linux distribution to another, or changing from one distribution to another. There are other reasons for maintaining certain parts of the Linux directory tree as separate filesystems.
Mounting
A mount point is simply a directory, like any other, that is created as part of the root filesystem. So, for example, the home filesystem is mounted on the directory /home. Filesystems can be mounted at mount points on other non-root filesystems but this is less common.
The Linux root filesystem is mounted on the root directory (/) very early in the boot sequence. Other filesystems are mounted later, by the Linux startup programs, either rc under SystemV or by systemd in newer Linux releases. Mounting of filesystems during the startup process is managed by the /etc/fstab configuration file. An easy way to remember that is that fstab stands for "file system table," and it is a list of filesystems that are to be mounted, their designated mount points, and any options that might be needed for specific filesystems.
Filesystems are mounted on an existing directory/mount point using the mountcommand.
原文地址:https://www.cnblogs.com/SsoZhNO-1/p/9196739.html