Brother Bird's Linux Notes ------- Disks and File Systems

1. Indexed file system

The file system usually stores the data of these two parts in different blocks, the permissions and attributes are placed in the inode, and the actual data is placed in the data block block. In addition, there is a superblock (superblock) that records the overall information of the entire file system, including the total amount of inodes and blocks, usage, remaining amount, etc.

  •  superblock: record the overall information of this filesystem, including the total amount of inode/block, usage, remaining amount, and irrelevant information about the format of the filesystem;
  •  inode: record the attributes of the file, one file occupies one inode, and at the same time record the block number where the data of this file is located;
  •  block: actually record the content of the file, if the file is too large, it will occupy multiple blocks.

Since each inode and block has a number, each file will occupy an inode, and the inode has the block number where the file data is placed. Therefore, what we can know is that if we can find the inode of the file, we will naturally know the block number of the data placed in the file, and of course we can read the actual data of the file. This is a more efficient method, because in this way, our disk can read all the data in a short time, and the read and write performance is better. Ext2 is an indexed file system

                         

                     Schematic diagram of EXT2 file system

 

In the overall planning, there is a boot sector at the front of the file system. This boot sector can install the boot management program. This is a very important design, because in this way we can put different boot management programs. Install to the forefront of individual file systems, without overwriting the only MBR of the entire hard disk, so that a multi-boot environment can be created!

  • data block

The data block is used to place the file content data. The block size supported in the Ext2 file system is 1K, 2K and 4K only. The size of the block is fixed when formatting, and each block has a number to facilitate the recording of inodes. However, it should be noted that due to the difference in block size, the maximum disk capacity that the file system can support is not the same as the maximum single file capacity. Ext2 file system limitations due to block size are as follows:
       

Block size 1K 2K 4K
Maximum single file limit 16G 256G 2TB
Maximum single file limit 2TB 8TB 16TB

 

  •  inode table (inode table)

      The content of the inode is recorded in the attributes of the file and in which blocks the actual data of the file is placed! Basically, the file data recorded by the inode has at least the following:
 The access mode of the file (read/write/execute);
 The owner and group of the file (owner/group);
 The capacity of the file;
 The time when the file was created or the state changed (ctime);
 The last read time (atime);
 The last modified time (mtime);
 The flag defining the file characteristics (flag), such as SetUID...;
 pointer to the actual content of the file;

The number and size of inodes are also fixed during formatting
 The size of each inode is fixed at 128 bytes;
 Each file only occupies one inode;
 Therefore, the file system can create the number of files and the number of inodes Relevant;
 When the system reads the file, it needs to find the inode first, and analyze whether the permissions recorded by the inode match the user.

 

  • Superblock

Superblock is where the information about the entire filesystem is recorded. Without Superblock, there would be no such filesystem. The information it records mainly includes:
 The total amount of blocks and inodes;
 The number of unused and used inodes/blocks;
 The size of blocks and inodes (blocks are 1, 2, 4K, and inodes are 128 bytes);
 The mount time of the filesystem, the time when the data was last written, the time when the disk was last checked (fsck) and other file system related information;
 A valid bit value, if the file system has been mounted, the valid bit is 0 , if it is not mounted, the valid bit is 1.

 

In general, the size of the superblock is 1024bytes

 

  •  Filesystem Description

This section can describe the start and end block numbers of each block group, as well as which block number each section (superblock, bitmap, inodemap, data block) is between. This part can also be observed with dumpe2fs.

 

  • block bitmap (block comparison table)

This requires the assistance of the block bitmap. From the block bitmap, we can know which blocks are empty, so our system can quickly find usable space to deal with files.

  • inode bitmap (inode comparison table)

This is actually a similar function to the block bitmap, except that the block bitmap records the used and unused block numbers, and the inode bitmap records the used and unused inode numbers!

2. Capacity of disks and directories

   1. df: List the overall disk usage of the file system;

-h : Display itself in GBytes, MBytes, KBytes and other formats that are easier for people to read;

-i : Display the number of inodes instead of the hard disk capacity

 

   2. du: Evaluate the disk usage of the file system (commonly used to estimate the capacity of a directory)

Options and parameters:

-a : List the capacity of all files and directories, because by default only the amount of files under the directory is counted.

-h : Display in human-readable capacity format (G/M);

-s : List the total amount, not the occupied capacity of each individual directory;

-S : Does not include totals in subdirectories, a little different from -s.

-k : List the capacity display in KBytes;

-m : List the capacity display in MBytes;

3. Entity links and symbolic links: ln

 Hard Link (physical link, hard link or actual link)

  • Each file will occupy an inode, and the file content is pointed to by the inode record;
  • To read the file, it is necessary to point to the correct inode number through the file name recorded in the directory to read.

  In fact, the file name is only related to the directory, but the file content is related to the inode. A hard link is only to add a file name to a related record of an inode number in a certain directory. A hard link just writes one more related data to a block in a certain directory, neither increasing the inode nor consuming the number of blocks.

So hard link should only be done in a single file system, not a link directory.

 Symbolic Link (symbolic link, aka shortcut)

   

The content of the link file read by the No. 1 inode only has the file name. According to the file name, link to the correct directory to obtain the inode of the target file, and finally the correct data can be read. What you can find is that if the target file (/etc/crontab) is deleted, then the whole process will not continue, so there will be a problem of not being able to read through the link file!

 

use of ln

ln [-sf] source file object file

Options and parameters:

-s : If you connect without any parameters, it is a hard link, and -s is a symbolic link

-f : If the target file exists, it will actively remove the target file and then create it!

 

Fourth, disk management

fdisk [-l] device name

Options and parameters:

-l : Output all partition contents of the following device. If there is only fdisk -l, the system will list the partitions of the devices that can be searched in the entire system.

fsck is used to check the file system for errors.

 

5. Disk Mounting and Unmounting

 A single file system should not be mounted repeatedly in different mount points (directories);
 A single directory should not mount multiple file systems repeatedly;
 In theory, the directory to be used as a mount point should be empty directory. If the directory you want to mount is not empty, after the file system is mounted, the contents in the original directory will disappear temporarily.

mount [-t filesystem] [-L label name] [-o extra options] \ [-n] device filename mount point

Options and parameters:

-a : Mount all unmounted disks according to the data in the configuration file /etc/fstab

-l : Simply typing mount will display information about the current mount. Add -l to add the Label name!

-t : Very similar to the options of mkfs, you can add the file system type to specify the type to be mounted. Common Linux supported types are: ext2, ext3, vfat, reiserfs, iso9660 (CD format), nfs, cifs, smbfs (these three are network file system types)

-n : By default, the system will write the actual mount status to /etc/mtab in real time to facilitate the operation of other programs. But in some cases (such as single-person maintenance mode), in order to avoid problems, it will deliberately not write. At this point you have to use the -n option.

-L : In addition to using the device file name (eg /dev/hdc6), the system can also use the file system header name (Label) to mount. It's best to give your filesystem a unique name

 -o : You can follow some additional parameters when mounting! For example, account, password, read and write permissions, etc.:

       ro, rw: mount the filesystem as read only (ro) or rewritable (rw)

      async, sync: Whether this file system uses the synchronous write (sync) or asynchronous (async) memory mechanism, please refer to how the file system works. Default is async.

      auto, noauto: allow this partition to be mounted automatically with mount -a (auto)

      dev, nodev: Are device files allowed to be created on this partition? dev is allowable 

     suid, nosuid: Allow only suid/sgid file formats for this partition?

     exec, noexec: Allow executable binary files on this partition?

     user, nouser: Is this partition allowed to mount by any user? Generally speaking, mount can only be executed by root, but the user parameter can be used to allow ordinary users to mount this partition.

      defaults: Default values ​​are: rw, suid, dev, exec, auto, nouser, and async

      remount: remount, this is useful when the system fails or re-updates parameters!

 

When mounting, the following files will be used as a reference for the system, and will automatically match the files to see if they can be mounted.

  • /etc/filesystems: The test mount file system type specified by the system;
  •  /proc/filesystems: The type of file system that the Linux system has mounted.
  • /lib/modules/$(uname -r)/kernel/fs/ : Linux supported filesystem drivers

umount [-fn] device filename unmount point

Options and parameters:

-f : Force uninstall! Can be used in situations like Network File System (NFS) that cannot be read;

-n : Uninstall without updating /etc/mtab.

6. Mount /etc/fstab and /etc/mtab at boot

Some restrictions on system mounts:
 The root directory / must be mounted, and it must be mounted before other mount points.
 Other mount points must be established directories and can be specified arbitrarily, but must comply with the necessary system directory architecture principles
 All mount points can only be mounted once within the same time period.
 All partitions can only be mounted once at the same time.
 If unmounting, you must first move the working directory outside the mount point (and its subdirectories).

 

The meaning of each field in /etc/fstab

  • Column 1: Disk device filename or the device's Label
  • Second column: mount point
  • The third column: the file system of the disk partition slot
  • Fourth column: file system parameters
parameter  content meaning
async/syncasync
 /sync
Set whether the disk operates asynchronously! Default is async (better effect)
auto/noautoauto/nonauto Whether this filesystem will be actively test mounted when mount -a is issued. Default is auto
rw/ro Erasable/Read Only Let the split slot be mounted in a rewritable or read-only format. If the data you want to share is not free for users to change, it can also be set to read-only here. Then no matter whether the file in this file system is set with w permission or not, it cannot be written!
exec/noexec executable/non-executable Is it possible to "execute" work restricted to this filesystem? If it is purely used to store data, it will be safer to set it to noexec, but it will be more troublesome!
user/nouser allow/disallow user mounts Are users allowed to mount using the mount command? Generally speaking, of course, we do not want ordinary users to use mount, because it is too insecure, so it should be set to nouser here!
suid/nosuid with/without suid permission Does the file system allow the existence of SUIDs? If the executable file is not placed in the directory, it can also be set to nosuid to cancel this function!
usrquota Note that the name is "usrquota" and don't misspell it! This is to support disk quota mode when starting the filesystem.
grpquota Note that the name is "grpquota", which enables filesystem to support group disk quota mode.
defaults Also has rw, suid, dev, exec, auto, nouser, async and other parameters. Basically, use the defaults setting for the default situation!
  • Column 5: Can it be used by the dump backup command?

Dump is a command used as a backup, we can specify which file system needs to be dumped through fstab! 0 means not to do dump backup, 1 means to do dump every day. 2 also represents other indefinite dump backup actions, usually this value is either 0 or 1.

  • Whether to verify sectors with fsck:

During the boot process, the system will check whether our filesystem is complete (clean) with fsck by default. However, some filesystems do not require verification, such as memory swap space (swap), or special filesystems such as /proc and /sys, and so on. 0 means do not check, 1 means the earliest check (usually only the root directory will be set to 1), 2 is also to check, but 1 will be checked earlier! Generally speaking, the root directory is set to 1, and the other filesystems to be checked are set to 2.

 

/etc/fstab is the configuration file at boot time, but the actual filesystem mounts are recorded in the two files /etc/mtab and /proc/mounts. Every time we update the mount of the filesystem, we update both files at the same time! However, if the data entered in /etc/fstab is wrong, the boot cannot be successfully started, and the single-person maintenance mode is entered. At that time, / is in the read only state, of course, you cannot modify /etc/fstab. Can't update /etc/mtab so what should I do? It doesn't matter, you can use the following trick:

 [root@www ~]# mount -n -o remount,rw /

 

 

7. Special device loop mount

 

  •  Mount CD/DVD image file

root@www ~]# mount -o loop /root/centos5.2_x86_64.iso /mnt/centos_dvd

 

  • Create large files to make loop device files!

Use the dd command to create a large file, which needs to be formatted after the creation.

root@www ~]# dd if=/dev/zero of=/home/loopdev bs=1M count=512

512+0 records in <== read 512 records

512+0 records out <== output

512 data 536870912 bytes (537 MB) copied, 12.3484 seconds, 43.5 MB/s

# The simple meaning of this directive is as follows:

# if is input file, input file. That /dev/zero is the device that will always output 0!

# of is the output file, which writes a bunch of zeros to the following file.

# bs is the size of each block, just like the block meaning of the file system;

# count means a total of several bs.

dd is like stacking bricks, stacking 512, 1MB bricks into one big file (/home/loopdev)! Eventually a 512MB file will appear.

 

Eight, memory replacement space (swap)

Build swap using entity split slots

1. Split: First use fdisk to split a split slot in your disk to the system as swap. Since Linux's fdisk defaults to setting the ID of the split slot to the Linux filesystem, you may have to set the system ID as well.
2. Format: Use the "mkswap device file name" to create a swap format to format the split slot into a swap format
. 3. Use: Finally, start the swap device with the method: "swap device file name".
4. Observation: Finally, use the free command to observe the memory usage!

  • You can use extra disk slots to increase swap in the same way as below
  • Use files to make swap

1. Use the dd command to add a 512MB file under /tmp:

2. Use mkswap to format the /tmp/swap file as a swap file format:

3. Use swapon to start /tmp/swap

 

4. Use swapoff to close the swap file

 

 

9. Compression (gzip, bzip2, tar)

  • gzip

[root @www ~]# gzip [-cdtv#] filename

[root @www ~]# zcat filename.gz

Options and parameters:

-c : Output compressed data to the screen, which can be processed through data stream redirection;

-d : Decompression parameters;

-t : Can be used to check the consistency of a compressed file ~ see if the file has errors;

-v : It can display information such as the compression ratio of the original file/compressed file;

-# : Compression level, -1 is the fastest, but has the worst compression ratio, -9 is the slowest, but has the best compression ratio! Default is -6

  • bzip2 was created to replace gzip and provide a better compression ratio

[root @www ~]# bzip2 [-cdkzv#] filename

[root@www ~]# bzcat filename.bz2

Options and parameters:

-c : Output the data generated by the compressed process to the screen!

-d : parameters to decompress

-k : keep the source file without deleting the original archive!

-z : Compression parameters

-v : It can display information such as the compression ratio of the original file/compressed file;

-# : The same as gzip, it is a parameter to calculate the compression ratio, -9 is the best, -1 is the worst!

  • The tar packaging instruction does not have the function of compression. It needs to add parameters to achieve packaging and compression.

[root@www ~]# tar [-j|-z] [cv] [-f created filename] filename... <==packaging without compression

[root@www ~]# tar [-j|-z] [tv] [-f created filename] <== view filename

[root@www ~]# tar [-j|-z] [xv] [-f created filename] [-C directory] <== decompression

Options and parameters:

-c : Create a packaged file, you can use -v to view the filename (filename) packaged in the process

-t : Check which file names are contained in the contents of the packaged file, focusing on checking the "file name";

-x : The function of unpacking or decompressing, can be used with -C (uppercase) to unpack in a specific directory. Special attention is that -c, -t, -x cannot appear in a series of command lines at the same time.

-j : Compress/decompress with bzip2 support: the best file name is *.tar.bz2

-z : Compress/decompress with gzip support: the best file name is *.tar.gz

-v : During the compression/decompression process, display the name of the file being processed!

-f filename: The filename to be processed should be immediately followed by -f! It is recommended to write an option for -f alone!

-C directory: This option is used for decompression. To decompress in a specific directory, you can use this option. Other follow-up exercises will use

Options introduction:

-p : retain the original permissions and attributes of the backup data, often used to backup (-c) important configuration files

-P : Keep the absolute path, that is to say, the root directory exists in the backup data first;

--exclude=FILE: Do not pack FILE during compression!

 

 Compress: tar -jcv -f filename.tar.bz2 Name of the archive or directory to be compressed
 Query: tar -jtv -f filename.tar.bz2
 Decompress: tar -jxv -f filename.tar.bz2 -C directory to unzip

 

1. Unpack the entire /etc directory while unpacking it in /tmp

[root@www ~]# cd /tmp

[root@www ~]# tar -cvf - /etc | tar -xvf -

 

  • dump compresses the backup, restore restores the dump

root@www ~]# dump -W

Options and parameters:

-S : Only list how much disk space is required for the data to be backed up to be backed up;

-u : record the time of this dump to the /etc/dumpdates file;

-v : Display the dump file process;

-j : Add bzip2 support! Compress the data, the default bzip2 compression level is 2

-level: is the level we are talking about, there are ten levels from -0 to -9;

-f : Kind of like tar! Followed by the generated file, or, for example, /dev/st0 device file name, etc.

-W : List whether the partitions with dump settings in /etc/fstab have been backed up

 

# Let's back up the entire /etc directory through dump, with compression

[root@www ~]# dump -0j -f /root/etc.dump.bz2 /etc

 

root@www ~]# restore -t [-f dumpfile] [-h] <== used to view the dump file

[root@www ~]# restore -C [-f dumpfile] [-D mount point] <== Compare dump and actual file

[root@www ~]# restore -i [-f dumpfile] <== enter interactive mode

[root@www ~]# restore -r [-f dumpfile] <== restore the entire file system

Options and parameters:

Various related modes, various modes cannot be mixed! For example, you cannot write -tC!

-t : This mode is used to see what important data is contained in the dumped backup file! Similar to tar -t function;

-C : This mode can compare the data in the dump with the actual file system, and finally list the files "recorded in the dump file, and the current file system is different";

-i : Enter the interactive mode, you can restore only part of the file, which is used for the restoration of the dump directory!

-r : A mode of restoring the entire filesystem, used to restore the dump backup for the filesystem;

Other commonly used options:

-h : View information such as inode and file system label in the full backup data

-f : The dump file you want to process is followed by it!

-D : Combined with -C, you can find out that there are different files in the subsequent mount point and dump!

Guess you like

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