Self-study Linux Shell1.3-linux file system

Source: Internet
Author: User
Tags what file type file permissions

Click Back to learn the path of the Linux command line and shell script

1.3-linux File System

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

1. Overview of File Types 1.1 Linux The following file types mainly include:
    • normal files : C-Language meta-code, shell scripts, binary executables, and so on. Divided into plain text and binary.
    • catalog Files : Directories, the only place where files are stored.
    • linked file : A file that points to the same file or directory.
    • device files : Related to system peripherals, usually under/dev. Divided into block devices and character devices.
    • Pipeline (FIFO) files: a way to provide process-building communication
    • socket File: This file type is related to network traffic

The file system refers to the physical space in which a file exists, and each partition in a Linux system is a file system with its own directory hierarchy. Linux will form the overall directory hierarchy of a system in a way that is divided into separate file systems that belong to different partitions. An operating system can not operate without the operation of the file, so it is necessary to own and maintain their own file system.

    • Ext2: File systems commonly used in early Linux
    • Upgraded version of EXT3:EXT2 with logging capability
    • Upgraded version of EXT3:EXT3 with advanced logging capabilities
    • RAMFS: Memory file system, fast
    • NFS: Network File system, invented by sun, primarily for remote file sharing
    • Ms-dos:ms-dos File System
    • Vfat:windows 95/98 file system used by the operating system
    • Fat:windows the file system used by the XP operating system
    • Ntfs:windows file system used by NT/XP operating system
    • File systems used by the HPFS:OS/2 operating system
    • PROC: Virtual Process file System
    • ISO9660: The file system used by most discs
    • File systems used by the Ufssun:os
    • File system used by the Ncpfs:novell server
    • Smbfs:samba Shared File system
    • XFS: Advanced log file system developed by SGI to support ultra-large capacity files
    • JFS:IBM the log file system used by AIX
    • ReiserFS: File system based on the balanced tree structure
    • UDF: rewritable data Disc File system
1.2 File system Features:

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

Each operating system can use a different file system. such as Windows 98 before the main use of the Microsoft operating system is FAT (or FAT16), the later version of Windows 2000 has the so-called NTFS file system, as for the Linux Orthodox file system is EXT2 (Linux second Exten Ded file System, EXT2FS) this one. In addition, the Windows operating system will not recognize Linux Ext2 by default.

In the traditional disk and file system application, a partition can only be formatted as 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 arrays (software RAID), which we often hear, these technologies can format a partition as multiple file systems (such as LVM), and also be able to synthesize multiple partitions into one file system (LVM, RAID)! So, at the moment, we're no longer talking about formatting for partition, and we can usually call a data file system instead of a partition that can be mounted.

So how does the file system work? This is related to the operating system's file data. The newer operating system's file data, in addition to the actual contents of the file, usually contains a lot of attributes, such as the Linux operating system file permissions (RWX) and file attributes (owner, group, time parameters, etc.). The file system typically stores the two parts of the data in separate chunks, with permissions and attributes placed in the inode, and the actual data is placed in the block. In addition, there is a super block (Superblock) that records the overall information of the entire file system, including the total number of inode and block, usage, surplus, etc.

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

    1. Super Block (superblock): This is the first block of space for the entire file system. This includes information about the entire file system, such as the block size, total amount of inode/block, usage, remaining amount, pointers to spatial inode and data blocks, and so on.
    2. inode block (file index node): A file system index that records the properties of a file. It is the most basic unit of the file system and is the bridge of any subdirectory or file connected to the file system. Each subdirectory and file has only one inode block. It contains information about the basic properties of files in the file system (length of file, creation and modification time, permissions, affiliation), location of the data stored, and so on. Under Linux, you can view the Inode information for a file by using the "Ls-li" command. Hard connections and source files have the same inode.
    3. Block : The actual record of the contents of the file, if the file is too large, will occupy more than block. In order to improve the efficiency of directory Access, Linux also provides the dentry structure of the corresponding relationship between the expression path and the inode. It describes the path information and connects to the node Inode, which includes various directory information, and also points to inode and super blocks.

It's like a book with a cover, a table of contents, and a text. In the file system, the Super block is equivalent to the cover, from the cover can know the basic information of the book, the Inode block is equivalent to the directory, from the table of contents can know the location of the content, while the data block is equivalent to the text of the book, recording the specific content.

Linux Orthodox file systems (such as ext2, 3, and so on) partition the hard disk into chunks, inode table chunks, and data block regions. A file consists of a super block, Inode, and data region block. 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.

Ext2 File System

We use the Inode and block block plots to illustrate, as shown, the file system first format the Inode and block block, assuming that a file's attributes and permissions data is placed in the inode 4th (smaller squares), and this inode records the actual placement of file data points for 2, 7, 13, 15 of these four block numbers, at this time our operating system can arrange the reading order of the disk, can be a breath of four block content read out! Then the reading of the data is like the arrows specified in the shape.


Figure Inode/block Data Access

This method of data access is called an indexed file system (indexed allocation). is there any other idiomatic file system that can be compared? Yes, that's our usual flash drive (flash), the file system used in the flash drive is generally in FAT format. The FAT file system does not exist in this format, so fat has no way to read all blocks of the file from the beginning. Each block number is recorded in the previous block, which reads a bit like the following:


Figure, FAT file system data access

We assume that the data in the file is written sequentially to the four block numbers of the 1->7->4->15 number, but this filesystem has no way of knowing the number of four blocks in one breath, and he will have to read the block in a single time before he knows where the next block is. If the block that writes the same file data is too fragmented, our disk read head will not be able to read all the data on the disk, so the disk will be more than a few laps to complete the contents of this file!

do you often hear the so-called "defragmentation" ? The reason for defragmentation is that the block of file writes is too discrete, and the performance of the file reads becomes poor. This time can be through the defragmentation of the same file belongs to the blocks together, so that the data will be easier to read! Assuming that the FAT file system needs to be regularly defragmented, does Ext2 need disk re-use?

since EXT2 is an indexed file system , it is largely not necessary to defragment it frequently. However, if the file system is used too long, often delete/edit/Add files, then it is possible to cause the file data is too discrete problem, this time may need to be restructured. However, to be honest, brother Bird is not in the Linux operating system above the Ext2/ext3 file system defragmentation said! It doesn't seem to be necessary! ^_^

You can use the LN command to create a new connection to an already existing file without copying the contents of the file. The connection has the soft connection and the hard connection cent, the soft connection is also called the symbolic connection. Each of them is characterized by:

    • Hard Connect: is a copy of the file, both the original file name and the connection file name point to the same physical address. The directory can not have a hard connection, hard connections can not cross the file system (not across different partitions) file on the disk only one copy, save hard disk space;
      Because deleting a file succeeds when the same index node belongs to a unique connection, you can prevent unnecessary accidental deletions.
    • Soft Connect/Symbolic Connection: Creating a symbolic connection to a file with the Ln-s command is a special file of Linux, as a file whose data is the pathname of the file it is connected to. Like a shortcut under Windows. Can delete the original file and save the connection file, there is no prevention of accidental deletion function.

The specific relationship can be seen:

Soft links and Hard links

The difference between a hard link and a soft link can be seen from the diagram:

1: The inode number of the hard link original file and the new file is the same. and soft links are different.

2: Deleting the original file causes soft links to be unavailable, and hard links are not affected.

3: The original file modification, soft and hard link file content is also modified, because all points to the same file content.

2 File system representation in the kernel

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

File and IO: Each process maintains a file descriptor table in the PCB (process Control Block), which is the index of the list, each table entry has a pointer to the open file. Now let's be clear: The opened file is represented in the kernel with the file struct, and the pointer in the document descriptor table points to the file structure body.

Maintains the file Status Flag (member f_flags of the file struct) and the current read and write location in the file structure (member F_pos of the file struct). In, Process 1 and Process 2 both open the same file, but correspond to different file structure bodies, so you can have different file Status flags and read and write locations. The more important members of the file struct are the F_count, which represents the reference count (Reference count), which we'll talk about, which will cause multiple file descriptors to point to the same document struct, such as DUP, fork, and so on. For example, FD1 and FD2 both refer to the same file struct, then its reference count is 2, when close (FD1) does not release the file structure, but only the reference count to 1, if close (FD2), the reference count will be reduced to 0 while releasing the file structure, This really closed the file.

Each file struct points to a file_operations struct, the members of which are function pointers to kernel functions that implement various file operations. For example, in the user program read a file descriptor, read through the system call into the kernel, and then find the file descriptor pointed to the document structure, to find the structure of the file_operations, which is pointed to the body, Invokes the kernel function pointed to by its read member to complete the user request. In the user program calls Lseek, read, write, IOCTL, open and other functions, and ultimately by the kernel calls file_operations the members of the kernel function point to complete the user request.

The release member in the File_operations struct is used to complete the close request of the user program, which is called release instead of close because it does not necessarily close the file, but reduces the reference count, and only the reference count is reduced to 0 to close the file. For regular files that are open on the same file system, the steps and methods for file operations such as read, write, and so on, should be the same, and the calling function should be the same, so the file structure of the three open files in the diagram points to the same file_operations struct. If you open a character device file, then its read, write operation must be different from the regular file, not read and write disk data block, but read and write hardware devices, so the file structure should point to the various file_operations structure body, The various file manipulation functions are implemented by the driver of the device.

Each file struct has a pointer to the dentry struct, and "Dentry" is an abbreviation for the directory entry (directory entry). The arguments we pass to the open, stat, and other functions are a path, such as/home/akaedu/a, where the inode of the file needs to be found based on the path. To reduce the number of reads, the kernel caches the tree structure of the directory, called the Dentry cache, where each node is a dentry struct, as long as the dentry is searched along the parts of the path, from the root directory/to the home directory, then to the akaedu directory, and then to the file a. Dentry cache saves only recently accessed directory entries, and if the directory entry you are looking for is not in the cache, it will be read from disk to memory.

Each dentry struct has a pointer pointing to the INODE structure. The INODE structure holds the information read from the disk inode. In the example, there are two dentry, representing/home/akaedu/a and/home/akaedu/b, each pointing to the same inode, stating that the two files are mutually hard links. The inode structure holds information that is read from the inode in the partition, such as owner, file size, file type, and permission bit. Each inode struct has a pointer to the inode_operations struct, which is a set of function pointers that point to some kernel functions that complete the file directory operation.

Unlike File_operations, inode_operations points to a function that does not operate on a file, but functions that affect the layout of files and directories, such as adding deleted files and directories, tracking symbolic links, and so on. Each inode structure that belongs to the same file system can point to the same inode_operations struct.

The inode struct has a pointer to the Super_block struct. The Super_block structure holds information that is read from a super block on a disk partition, such as file system type, block size, and so on. The S_root member of the Super_block struct is a pointer to Dentry, indicating where the root directory of the filesystem is being mount, in which case the partition is mount to the/home directory.

The structure of file, Dentry, inode and Super_block constitute the core concept of VFS. For ext2 file systems, there is also the concept of inode and Super block on disk storage layouts, so it is easy to establish correspondence with the concepts in VFS. Other file system formats from non-UNIX systems (such as Windows FAT32, NTFS), may not have inode or Super block concept, but in order to mount to the Linux system, but also in the driver hard to gather, Looking at FAT32 and NTFS partitions under Linux will find that the permission bits are wrong, and all files are rwxrwxrwx, because they do not have the concept of inode and permission bit, which is hard to come out of.

3 Mounting the file system3.1 Overview

Each partition in a Linux system is a file system with its own directory hierarchy. Linux will form the overall directory hierarchy of a system in a way that is divided into separate file systems that belong to different partitions. The word "in a certain way" means the mount.

hang the top-level directory of one file system on a subdirectory of another file system, making them a whole, called mounts. refer to this subdirectory as a mount point.

For example, to read a well-formatted partition, disc, or software on a hard disk, you must first map these devices to a directory, which is called a mount point, so that you can read these devices. After mounting, the physical partition details are screened out and the user has only a unified logic concept. Everything is a file.

Attention:

    • The mount point must be a directory.
    • A partition is mounted on an existing directory, and this directory may not be empty, but the previous content in this directory will not be available after mounting.

This is true for the mounting of file systems created by other operating systems. However, it is necessary to understand that the format of the file system used by the CD, floppy disk, and other operating systems is not the same as the file system format used by Linux. The disc is ISO9660, the floppy disk is FAT16 or Ext2;windows NT is FAT16, NTFS;WINDOWS98 is FAT16, fat32;windows2000 and WindowsXP are FAT16, FAT32, NTFS. Before mounting, see if Linux supports the file system format that you want to mount.

Mount when Mount command is used, format:mount [-parameter] [device name] [Mount Point]
Among the parameters commonly used are

    • -t specifies the file system type of the device (what file type is mentioned)
    • -o Specifies the option to mount the file system. Some are also available in/etc/fstab. Commonly used to have

CODEPAGE=XXX code page
Iocharset=xxx Character Set
RO mount in read-only mode
RW mount in read-write mode
Nouser makes it impossible for ordinary users to mount
User can mount a device to a normal user

For example:

1. Mount the file system for Windows:

    • 1) First we use sudo fdisk-l to view mounted devices, such as the bottom:/dev/hda5
    • 2) mkdir Create a directory where the directory is to be hung in the directory, that is, you want to put the e-disk into this directory: Mk/mnt/winc
    • 3) Windows and Linux are not using a file system, typically Linux does not mount the Windows file system, so you manually mount:

# mount-t Vfat/dev/hda5/mnt/winc (-t vfat points out the file system FAT32 here)
You are now ready to read and write these files into directories such as/mnt/winc.

2. Mount Disc: # Mk/mnt/cdrom
# mount-t Iso9660/dev/cdrom/mnt/cdrom (the name of the switch is usually CDROM, this command is generally common)

3. Virtual Machine shared folder: For example, under VirtualBox, the host is Windows,ubuntu is guest. It is divided into three steps:

1). First install the Virtual Computer Toolkit: In the VirtualBox menu, select "Device", "Install the virtual computer Kit", you will find the Ubuntu desktop with a more disc icon, this disc is automatically loaded into the folder/media/cdom0 by default, and/ CDROM automatically points to this folder. By default, the File Manager will automatically open this disc and you will see a "vboxlinuxadditions.run" file in it. Open a command-line terminal, enter "Cd/cdrom" and "sudo sh./vboxlinuxadditions.run", without double quotes, to begin the installation of the toolkit. After installation, you will be prompted to restart Ubuntu in English, it is recommended to restart 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, the Virtual Machine Toolkit has been installed successfully.

2). Next set up the shared folder.
In the Shared Folder Settings window, click "Add a Shared folder" on the right, the path to choose the Windows folder you want to share, the share name whichever one you like, such as "myshare", the option read-only is to allow only Ubuntu to read this folder, Please select this option as needed.

3). Mount this shared folder under Ubuntu: sudo mount-t vboxsf myshare/media/share
where "MyShare" is the name of the shared folder previously taken, "/media/share" is the target file to mount.

3.2 Automatically mount Windows partitions

It's obviously too cumbersome to run the Mount command every time you access the Windows partition, why not use the Mount command to access other Linux partitions?

In fact, Linux automatically mounts the Linux partitions that need to be mounted on each boot. So can we set Linux to mount the partitions we want to mount, such as Windows partitions, when booting up, to automatically mount the file system?

This is perfectly possible. There is a 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 mounted file system type, the fourth column is the Mount option, and the options are separated by commas. The 56th column does not know what to mean, but also looks at the expert pointing.

The last two lines are the c;d disks that I added manually under Windows, plus the codepage=936 and iocharset=cp936 parameters to support Chinese filenames. The parameter defaults actually contains a set of default parameters:

    • RW is mounted in read/write mode
    • SUID Open User ID and group ID setting bit
    • Dev interprets character or block devices on a file system
    • EXEC executable binary file
    • Auto Mount automatically
    • Nouser makes it impossible for ordinary users to mount
    • Async performs a file system's input-output operation in an unsynchronized manner

As you can see in this list, the optical drive and floppy drive are not mounted automatically and the parameters are set to Noauto. (If you want to set the automatic mount, you have to make sure that every time you turn on the CD-ROM drive and floppy disk, hehe.) )

4. File Directory Management commands

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 directories and permissions:chmod chown chgrp umask

file Lookup:which, Whereis, locate, find, find

Self-study Linux Shell1.3-linux file system

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.