Ultra-detailed linux mount details

First, the linux file structure

File structure is an organization method for files stored on storage devices such as disks. Mainly reflected in the organization of files and directories. Directories provide a convenient and efficient way to manage files.  
Linux uses a standard directory structure. During installation, the installation program has created a file system and a complete and fixed directory composition for the user, and specified the role of each directory and the file types in it.   

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 the disk partition. There are several tree structures in several partitions, and the relationship between them is juxtaposed. of. 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 side by side.   

If this is not easy to understand, let me give an example:   

There is a hard disk divided into 4 partitions, namely /; /boot; /usr and fat under windows. For / and /boot or / and /usr, they are subordinate; for /boot and /usr, they are side by side relation.   
If I mount the fat partition under windows to /mnt/winc, then for /mnt/winc and /usr or /mnt/winc and /boot, they are two subordinate to the directory tree without any relationship branch.   
Because linux is a multi-user system, formulating a fixed directory plan is helpful for unified management of system files and different user files. But this is the headache for many beginners who switch from windows to linux. The functions of some main directories under linux are listed below.

/ (this is the famous root)
├── bin (most programs you run in the terminal, such as cp, mv...)
├── boot (the kernel is placed here, and this directory is often used as a mount for a separate partition Loading point)
│ └── grub (grub boot program and boot menu are placed here)
├── cdrom
├── dev (store device files, this is equivalent to a device manager, which is automatically generated by the system. Depending on the hardware environment Great changes)
│ ├── block
│ ├── bsg
│ ├── bus
│ ├── char
│ ├── disk (disk information, you must pay attention to the information here if you want to mount a hard disk partition)
│ │ ├─ ─ by-id (permanent symbolic link of hard disk partition)
│ │ ├── by-label (hard disk partition identified by volume, often used for mounting)
│ │ ├── by-path (node ​​link of hard disk partition)
│ │ └── by-uuid (hard disk partition identified by UUID, commonly used for mounting)
│ ├── dri
│ ├── fd
│ ├── input
│ ├── net
│ ├── pts
│ ├── shm
│ └── snd
├── etc (store all program and system configuration files and global variables, effective for all users, it is worth backing up)
├── home (this is the famous home directory, note that it is not a "home directory", it is strongly recommended to mount a separate partition here!)
│ ├── adagio (this is my real home! Generally speaking, the directory The name is the account name, of course it doesn’t have to be, whatever. In the command line, the tilde ~ represents here)
│ ├── MNT (This is where I mount other hard disk partitions, you can see the brand, capacity or purpose of the hard disk used │ │ ├──
MAX40NT1 (Maxtor 40G)
│ │ ├── ST160NT1 (Seagate 160G first partition, the following two are similar)
│ │ ├── ST160NT2
│ │ ├── ST160SYS │ │
├─ ─ ST320G (Seagate 320G)
│ │ │ ├── MOVIE
│ │ │ ├── MUSIC
│ │ │ └── P2P (e-Donkey, BT buffer)
│ │ ├── ST80G (Seagate 80G)
│ │ │ ├─ ─ PROGRAM
│ │ │ ├── ST80PE
│ │ │ └── YEAR
│ │ └── WD1000 (Western Digital 1T)
│ │ ├── WD2
│ │ ├── WD3
│ │ ├── WD5
│ │ ├── WD6 .Lib
│ │ └── WD7
│ └── test (The home directory of another account I created is dedicated to testing. Once it gets too far, just simple
│ Delete all the files in it to restore the default. You can actually have an infinite number of accounts)
├── lib (library files shared by all programs)
├── lost+found (lost data from disk scans)
├── media (automatic when you click in the file manager The mounted partition is here, named according to the volume label, if there is no volume label, it is named according to the size)
├── mnt (also used to mount the disk, this is the most traditional location, you can mount it wherever you like)
├── opt ( Some special programs like to put data here, such as JAVA)
├── proc (all the detailed information of the current system, the "file" here does not exist in the hard disk, but in the memory or cache, every time you start ├──
root (this is the home of the most authoritative root user in the system! He is the boss, so he does not live in /home, where the grass people live)
├── sbin (similar to /bin, stores common programs , but the programs here are all fatal, such as formatting, so only the root user or sudo program has the right to use)
├── srv (files to be accessed by some services)
├── sys (system core files, similar to / proc, don't worry about it)
├── tmp (stores temporary files, all users can use, but you have to be careful, all files here will be gone once restarted and automatically emptied)
├── usr (you are in X All the program data used in the following are here, including icons, manuals, etc. All users can use it. It is also the largest directory)
└── var (variation, as the name suggests, is a variable, which stores frequently changed data in the system. And /tmp is different, very useful)
├── backups
├── cache
│ └── apt
│ └── archives (deb package that stores all the programs you installed! It is very useful when reinstalling the system, be sure to back it up and put it back.
│ I suggest putting the whole / var is mounted to a separate partition, like /home. In this way, after you reinstall the system, you only
need to simply mount the entire partition to /var, which saves the backup-restore time. You need to know These deb packages
│ are not just tens of M, but may be hundreds of M, several G, which can be enough to go back and forth. You can also mount it separately
│ a partition to /var/cache/apt/archives, Do not need anything else.
│ Of course, this adds a little more complexity to the system structure, so please decide how you like.)
├── crash
├── games
├── lib
├── local
├── lock
├── log (Hehe, the file here is the complete record of the system running, you must come here to see if there is a problem)
├── mail (this is where all user emails are stored)
├── opt
├── run
├── spool
└ ── tmp

40616 directories (there are 40616 subdirectories in the root directory, you don't want to browse them all, right?)

Second, the linux file system

The file system refers to the physical space where files exist. Each partition in the Linux system is a file system with its own directory hierarchy. Linux will form these separate file systems belonging to different partitions into a general directory hierarchy of a system in a certain way. The operation of an operating system is inseparable from the operation of files, so it is necessary to have and maintain its own file system.   
The Linux file system uses inodes to record file information, acting like the Windows file allocation table. An inode is a structure that contains information such as the length of a file, creation and modification times, permissions, ownership, and location on disk. A file system maintains an array of inodes, and each file or directory corresponds to a unique element in the inode array. The system assigns a number to each index node, that is, the index number of the node in the array, which is called the index node number.   

The linux file system stores both the file inode number and the file name in the directory. So, a directory is just a table that combines the name of a file with its inode number, and each pair of file name and inode number in the directory is called a connection.   
For a file, there is a unique inode number corresponding to it, but for an inode number, there can be multiple file names corresponding to it. Therefore, the same file on disk can be accessed through different paths. You can use the ln command to establish a new connection to an existing file without copying the contents of the file.   

Connections are divided into soft connections and hard connections, and soft connections are also called symbolic connections. Their respective characteristics are:   
hard link: both the original file name and the connection file name point to the same physical address. Directories cannot have hard links;  
hard links cannot span file systems (cannot span different partitions). There is only one copy of a file on the disk, saving hard disk space;  
since deleting a file can only succeed when the same inode belongs to the only connection, it can be Prevent unnecessary deletion by mistake.   
Symbolic link: Use the ln -s command to create a symbolic link to a file. A symbolic link is a kind of special Linux file. As a file, its data is the path name of the file it is connected to. Similar to the shortcut under Windows. You can delete the original file and save the connection file, but there is no function to prevent accidental deletion.   

The content of this paragraph is too abstract, and it is a node and an array. I have tried to make it as simple as possible, and it is not easy to add an example to demonstrate. If you are still in a fog, I have no choice but to remember it first, and then slowly experience and understand it in practical applications in the future. That's one way I've learned.

3. Mount the file system

As we know from the previous section, each partition in the Linux system is a file system and has its own directory hierarchy. Linux will form these separate file systems belonging to different partitions into a general directory hierarchy of a system in a certain way. The "in a certain way" mentioned here refers to the mount.   

Mounting the top-level directory of one filesystem onto the subdirectories of another filesystem, making them a whole, is called mounting. Call this subdirectory the mount point.   
Note: 1. The mount point must be a directory.     
2. A partition is mounted on an existing directory. This directory may not be empty, but the previous content in this directory will not be available after mounting. The same is true for mounts of filesystems established by other operating systems. But what needs to be understood is that the format of the file system used by CD-ROMs, floppy disks, and other operating systems is different from the file system format 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, it is necessary to know whether Linux supports the file system format to be mounted.   
Use the mount command when mounting:   

Format: mount [-parameter] [device name] [mount point]   

The commonly used parameters are     
-t <file system type> to specify the file system type of the device. The common ones are:    
minix linux, the earliest used file system,    
ext2, linux's currently commonly used file system    
, msdos, and the fat of MS-DOS, which is commonly used in fat16     
vfat windows98 fat32     
nfs network file system    
iso9660 CD-ROM disc standard file system    
ntfs file system for windows NT 2000    
hpfs OS/2 file system    
auto auto-detect file system    
-o <options> Specifies options when mounting file systems. Some are also available in /etc/fstab. Commonly used are 
   codepage=XXX code page    
iocharset=XXX character set    
ro mount    
rw in read-only mode mount    
nouser in read-write mode so that ordinary users cannot mount    
user, allow ordinary users to mount devices   

As a reminder, the mount command has no function to create a mount point, so you should make sure that the mount point already exists when you execute the mount command. (Don't understand? To put it bluntly, where do you want to mount the file system, you must first create a directory. Is this OK?)   

Example: windows98 is installed in the hda1 partition, and there are floppy disks and CD-ROMs that need to be mounted on the computer.     

# mk /mnt/winc     
# mk /mnt/floppy     
# mk /mnt/cdrom     
# mount -t vfat /dev/hda1 /mnt/winc     
# mount -t msdos /dev/fd0 /mnt/floppy     
# mount -t iso9660 /dev/cdrom /mnt/cdrom  

Now you can enter /mnt/winc and other directories to read and write these file systems.   
To ensure that the last two lines of commands are correct, make sure there are disks in the floppy and CD-ROM drives. (If the platters of the hard disk can also be replaced at any time, I don't think I would make such a mistake :-> )   

If there are Chinese file names in your windows98 directory, after using the above command to mount, it will display a bunch of garbled characters. This uses the codepage iocharset option in the -o parameter. codepage specifies the code page of the file system, the Chinese code for Simplified Chinese is 936; iocharset specifies the character set, and cp936 or gb2312 is generally used for Simplified Chinese.   
When the mounted file system is not supported by Linux, mount must report an error, such as the ntfs file system of Windows 2000. The linux kernel can be recompiled to gain support for this filesystem. Regarding recompiling the linux kernel, I won't talk about it here.

Fourth, automatic mounting   

It is obviously too cumbersome to run the mount command every time you boot to access the windows partition. Why don't you use the mount command to access other Linux partitions?  
In fact, every time you boot up, linux automatically mounts the linux partition that needs to be mounted. So can we set the partition that we want to mount, such as the windows partition, to be mounted when linux starts, so as to realize the automatic mounting of the file system? This is totally possible. There is an fstab file in the /etc directory, which lists the list of file systems that are automatically mounted when linux is powered on.
My /etc/fstab file is as follows:     

/dev/hda2 / ext3 defaults 1 1     
/dev/hda1 /boot ext3 defaults 1 2     
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 type of the mounted file system, and the fourth column is the mounted option, option Separate them with commas. The fifth and sixth columns do not know what they mean, but I still hope that the experts will give guidance.   
In the last two lines, I manually added the C;D disk under Windows, and added codepage=936 and iocharset=cp936 parameters to support Chinese file names. The parameter defaults actually contains a set of default parameters:     

rw mount    
suid in read-write mode enable user ID and group ID set bit    
dev can interpret characters or block devices on the    
filesystem exec executable binary    
auto mount    
nouser automatically make it impossible for normal users to mount    
async to asynchronous way to perform input and output operations of the file system   

As you can see in this list, CD-ROM and floppy drives are not automatically mounted, and the parameter is set to noauto. (If you insist on automounting, make sure you have disks in your CD-ROM and floppy drive every time you boot up.)

Reprinted in: https://blog.csdn.net/demo_deng/article/details/51527197

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325951361&siteId=291194637