Operating principle of Linux File System Management (hard disk)

Source: Internet
Author: User

I. How does the system identify hard disks during initialization? 
1. The system identifies the hard disk based on the MBR information at the beginning, including some execution files to load to the system. These execution files are the boot loader program in 446bytes before MBR, the following 16x4 space is the location where the partition table information is stored. For example:
 
2. The Partition Table stores the following information:
(1) Partition Number. Common partition numbers include the following: You can use the fdisk command to view other numbers and then execute the L (lower case l) command.

0x5 (or 0xf) Extended partition
0x82 Linux swap
0x83 Linux
0x8e Linux LVM
0xfd Linux raid auto

(2) Start column of the shard;
(3) Total number of magnetic columns;
Therefore, during system initialization, the hard disk is identified based on the three items in the partition table.

II. Introduction to hard disk partitioning
1. When all primary partitions are used up, you can use extended partitions to add additional partitions. This has been described earlier, but in the Linux kernel:
· IDE Hard Disks support up to 16 partitions;
· A scsi hard disk supports up to 15 partitions;
2. Benefits of multiple partitions on a hard disk:
(1) control considerations
By dividing a hard disk into multiple partitions, you can put applications, user data, or data that requires security into different partitions for convenient management;
(2) efficiency considerations
After a hard disk is used for a period of time, the blocks are not consecutive. If a large-capacity hard disk is not divided into multiple small partitions, because the search range is very large, it will take a long time. If a large-capacity hard disk is divided into multiple small partitions, the search will be faster;
(3) to use the disk quota Function
Because the quota can only be set for the partition, we can separate the/Home Directory into a single partition, and then make a quota for this partition;
(4) data backup and recovery considerations
For example, the/home directory is a directory dedicated to storing user information. If you set a separate partition for this directory, You can regularly back up a partition for ease of recovery.

Iii. Introduction to partition management
1. You can use the following command to create and view partitions.
Fdisk/Dev/hda create a partition
Parameter:-l view partitions
After the fdisk command is executed, for example:
 
Example 1: Create a partition, Enter n. The system requires that you enter the start column number. By default, the system specifies a recent unused column number. After you press enter, enter the end column number, however, it is not convenient to calculate the capacity of a partition based on the magnetic column number. You can use "+ value K or + value m (for example: + 1000 m)" to directly set the capacity, this is more intuitive. Execute P to see the newly added partition;
 
Example 2: delete a partitionEnter D. Enter the partition number to be deleted. In the device field column, you can see the ID of each partition. Enter the partition number to be deleted and press Enter.
 
In the past, all operations were considered correct. Enter W and save and exit.
At this time, the host screen will prompt:
The kernel still uses old tableThe system still uses the old partition table;
The new table will be used at the next reboot.Indicates that the new partition table will be used only after the next reboot;
2. Use the following command to re-load the Partition Table to the kernel, so that you can enable the new partition table without restarting the system.
Partprobe

4. Create a file system (Format hard disk partitions)
1. Format Commands and command syntax:
Mke2fs [parameter] partition to be formatted
For example, in the preceding example, format the hda6 partition.Mke2fs/dev/hda6
2. After formatting, some partition information will appear. To understand this information, first understand the partition format:
Partitions created using fdisk cannot be directly used to store data. You must first format the partition. Formatting means dividing the partition into one block and how many inodes can be used, each block is the smallest unit for the file system to access data. Therefore, the data can be stored in these blocks, and each block will constitute a group, as shown in: apart from the boot sector, the first block is called the super block, which records the total number of blocks and inode used in the partition, the number of blocks, inode, and other information;
 
As shown in:
13 block groups: This partition is divided into 13 block groups;
8192 blocks per group: indicates that each group has 8192 blocks;
2008 inodes per group: indicates that each group has 8192 inodes;
If the super block is damaged, this partition cannot be accessed, so the super block will be backed up once every block, for example:
Superblock backups stored on block: The value below is the location of the block where the super block is backed up.
 
3. Run the dumpe2fs command to view the partition details.
Dumpe2fs/dev/hda6 | moreView the information of the specified partition (the first half of the page is the content of super block, followed by the details of each group), because a lot of content is displayed, so added | more for paging display;
Description of the displayed information:
Inode count: Total inode count;
Block count: the total number of blocks;
Free blocks: number of remaining blocks;
Fre inodes: Number of inode remaining;
Filesystem features: The has_journal table is an ext3 file system.
4. Format and parameters of the mke2fs formatting command
Format:Mke2fs [parameter] Name of the partition file to be formatted (for example,/dev/hda6) 
Mke2fs-J/dev/hda6
Parameters:
· B: set the size of each block. The default value is 1024 bytes (1 K), and the maximum value is 4096 bytes (4 K). If the value is too large, 4096 bytes is used;
· C: Check whether the partition contains damaged blocks before formatting;
· I: set the size of each inode, for example, mke2fs-I 4096/dev/hda6
· N: directly set the total number of inode. the set value is close;
· M: Set how much space is reserved for the root user on the partition. The default value is 5%. mke2fs-M 10/dev/hda6.
· L: Set the partition volume label. If this parameter is not set by default, you are often used to setting the volume label name to the same directory name as the mount point for easy memory. mke2fs-l data/dev/hda6
· J: Create an ext3 file system, that is, add the log function;

V. convert an ext2 partition to an ext3 Partition
1. The differences between ext2 and ext3 are as follows:
(1) ext2 and ext3 are in the same format, but there is a part of space at the end of the ext3 hard disk to store Journal (log) records;
(2) In ext2, when writing data to the hard disk, the data is first written into the cache, and the data is written into the hard disk only when the cache is full;
(3) In ext3, when writing data to the hard disk, the data is first written into the cache. When the memory cache is full, the system notifies journal before writing the data to the hard disk, then, the journal will be notified. The data has been written;
(4) Whether there is a journal difference:
In ext2, the system will check the valid bit (valid bit) when it is started. If the value is 1, it indicates that the system was shut down normally last time. If the value is 0, it indicates that the last shutdown was not properly shut down, then the system will check the data in the hard disk from the beginning, which will take a long time;
In ext3, there is a journal mechanism. When the system is started, check Journal information to see if any errors have occurred. This is much faster;
(5)Tune2fs-J/dev/hda6Convert ext2 to ext3
2. There are three journal modes in ext3:
(1) ordered: In the default mode, only information in inode-table is recorded;
(2) journaled: record the information of the data itself, which requires a large amount of space for record;
(3) writeback: it does not record information and can provide better performance;

6. Set the corresponding volume label in the Partition 
1. The representation of the preceding partition has been using hda1 or sda1 like this. In fact, you can also use the volume label notation to better remember it.
2. Set and view the volume label Instruction format and description as follows:
E2label command:
Example:E2label/dev/hda6View the volume name of the hda6 partition. If it is blank, no volume label is set;
E2label/dev/hda6 dataSet the volume of this partition to data;
Note: Do not modify hda1 ~ here ~ The hda5 volume label may fail to start up.

7. Introduction to mount partitions using the mount command 
1. Concept of mounting: When you want to use a device, for example, when you want to read a formatted partition, disk, or software from a hard disk, you must first map these devices to a directory, which is called "mount point" to read these devices, the corresponding actions are "attaching ".
Detailed descriptions of Mount will be introduced in the next section
Example:
 
The following is a volume label mounting method. Note: The-l parameter must be added to the volume label mounting method to use the volume label mounting method.

8. Introduction to the mount command and Parameters
1. mount command format:
Mount [-T file system type] [-O parameter] device name or volume mount point directory name
Note: If you use the volume label method for mounting, you must use the-l parameter.
(1) set the type of the file system to be mounted after-T, for example, vfat, ext2, ext3, and iso9660 (optical disc). Generally, you do not need to add a kernel to determine the type of the file system.
(2) Add some set parameters after-O:
· SUID: the attached file system can use the special permissions of SUID and SGID;
· Dev: allows the mounted file system to create device files. For example, hda6 under/dev/hda6 is the device file;
· Exec: After the file system can be mounted, You can execute the execution files in it;
· Noexec: files cannot be executed;
· Auto: The file system is automatically mounted after the computer is started;
· Nouser: allows only super user (that is, root) to mount the file system;
· Async: set to not synchronize, that is, when the computer writes data, it first writes the data to the cache and then writes it to the hard disk;
· Loop: used to mount loopback devices. For example, an optical disk is a loopback device;
· Ro: After mounting the file system, it is set to read-only;
· RW: After mounting the file system, it is set to readable and writable;
· Remount: Remount the file system;
If ext2 or ext3 is mounted, the following parameter settings are used by default:
RW, SUID, Dev, exec, nouser, async

9. Introduction to detaching a File System
1. Run the mount command to check which file systems have been mounted;
The uninstall command and format are as follows:
Umount device file name or mount point
For example, you can useUmount/dev/hda6 or umount/DataUninstall
2. If the file system is in use, it cannot be uninstalled normally. You can run the following command to view and stop all operations on the file system, and then uninstall the file system;
Fuser-V device file name or mount pointCheck which service or user is operating on the file system;
Fuser-km device file name or mount pointForce stop all operations on this file system;
3. You can use the remount parameter to change the file system status;
For example:Mount-O remount, RO/DataRemount the file system as read-only, so that you do not need to detach it first.

10. Introduce some mounting examples
1. mount a file system that cannot be executed
Mount-O noexec/dev/hda6/DataNoexec indicates that execution is not allowed;
2. Mount the image file of a file system
Mount-T iso9660-O Ro, loop boot. ISO/ISOThe loop parameter is added because the disc is a loopback device, Boot. ISO is the image file name, And/ISO is the mount point;

11. Introduction to shared resources on attached Networks 
1. Network Resources in UNIX and Linux are mainly divided into two types: Network File System (NFS) and SMB
· NFS: resources are shared between Linux/Unix and Unix/Linux;
· SMB: resources are shared between Linux/Unix and Microsoft (Windows;
2. The host that shares NFS network resources is called: NFS server;
Shared SMB network resource discount host is called Samba server;
3. Connection Methods of these network resources:
(1) NFS connections:
Showmount-e IP Address# View shared resources
Mount IP Address:/shared directory/mount point# Attach network resources
(2) SMB Connection Methods:
Smbclient-l IP address-n# View the shared directories on the specified Samba server;
Mount // ip address/shared directory/mount point-O username = username % Password# Mount Windows network shared resources;

12. Introduction to the functions of the/etc/fstab file
1. The function of the fstab file is to automatically mount the device to the specified mount point based on the content set in the fstab file;
2. The following describes the content of the fstab file in detail:
For example, the content of the fstab file is divided into six columns.
 
The following describes the 6 columns respectively:
(1)Device Name: You can use label = or device name notation "/dev/hda6 ";
(2)Mount point: The mount point directory must be an existing Directory;
(3)File System Type: Such as ext3 and ext2;
(4)Mount Parameters: The parameters following-O in the preceding mount command. defaults indicates that the preset parameters are used. For example,/dev/HDC and/dev/fd0 (floppy disk) the exec can execute the file, and noauto will not automatically mount the file,
(5)Backup: Sets the number of backups. 0 indicates no backup, 1 indicates a backup every day, and 2 indicates a backup every two days;
(6)Check order: Sets the check sequence of the file system. 0 indicates no check, 1 indicates the first check, 2 indicates the second check, and so on. If the values are the same, check in the order from top to bottom. The check order of the root directory is usually the first;
Therefore, if you want to automatically mount the file system after the computer starts up, you can modify the fstab file settings.
For example, you can write the previously mounted hda6 to this file. After the file is started, it is automatically mounted. The setting method is as follows: Use VI to edit the fstab file and add the following row to the last row:
/Dev/hda6/Data ext3 defaults 0 0
2. When attaching a CD or floppy disk, you only need to mount/Media/CDROM or Mount/Media/floppy, instead of specifying the device to be mounted, this is because it is set in the fstab file, because when the Directory and device are mounted, if the device to be mounted is not specified, the computer will first view it in the fstab file, are there any corresponding devices and directories mounted? If they are not, they will still be mounted to the mtab file (in fact, the mtab file is the current system mount settings, so you can also view the mtab file when uninstalling it, you only need to specify the mount point or device to view the mount point.

13. Introduction: auto-Mounter auto-mounting programs automatically mount network shared resources when using network sharing 
1. automounter is a daemon program. automounters can monitor a directory, for example, monitoring/mnt/NFS directory. When we need to access this directory, when you use CD, MNT, and NFS to access this directory, the automounter program will automatically mount the shared network resources according to the settings, if this directory is not used for a period of time (60 seconds by default), the directory will be automatically uninstalled, which reduces the load on the network and increases the utilization of the network.
2. Set automounter as an example for your reference only:
(1)VI/etc/auto. MasterEdit the main settings file of automount
(2) copy the row -- timeout = 60 to the following, as shown in the following figure:
 
(3) create an auto. NFS setting file. The file is not in the/etc directory by default and needs to be created by yourself;
It can be copied from auto. Misc as a template and renamed as auto. NFS. Edit auto. NFS as follows:
 
(4)Service autofs startStart the automount service. if the service autofs restart or service autofs reload is enabled, restart or reload the configuration file;
(5) use the CD/mnt/NFS directory and then execute the LS command without any files,
You can use CD server1 to access the NFS server. Then, you can view the shared files on the NFS server.
You can use mount to view the NFS network shared directory just mounted.
Note: I have not yet created an NFS file server, so I can only set the client here, and I cannot try it now;

14. describes the attributes that can be set in the ext2 and ext3 file systems.
1. ext2 and ext3 file systems support special attributes to control file features:
·LsattrCommand: display the attribute settings of the file;
·ChattrCommand: sets the attributes of a file;
Format:
Chattr + |-| = attribute [attribute…] File name [file name…]
+
: Add attributes
-: Remove attributes
=: Set attributes
One or more attributes can be set.
Common attributes are described as follows:
· A: When a file is modified, the atime (access time) record will not be modified;
· A: Only content can be appended to a file, and the original content cannot be overwritten;
· D: The system does not need to back up this file when using the dump command for backup;
· I: never change the file name. You cannot delete or change the file name;
· J: Let the system record the file information in the ext3 log, even if the ext3 file system is mounted to ordered (ordered) or writeback mode, the file information is recorded in the ext3 log.
· S: When a file is modified, the data is synchronized and immediately written to the hard disk;
Example:
Chattr + A TestAdd attributes that only allow content to be appended
Chattr = testRemove all attributes

15. describes how to use a partitioned partition or file as a virtual memory (SWAP)
1. in Linux, the virtual memory is called swap;
2. Swap is divided into two types:
· Use partitioned partitions as swap;
· Use files as SWAP;
3. There are several basic settings for creating a swap virtual memory:
(1) create a swap partition or file, and set the number of the zone to 82 when the swap partition is created;
(2) Use the mkswap command to write a specific swap recognition mark to the swap partition or swap file;
(3) appropriate records must be added to the/etc/fstab file so that the system can automatically mount swap after it is started;
(4) if it is a swap partition, use the Swapon-a command to enable swap. In fact, the Swapon-a command will read the fstab file and follow the records in the fstab, to enable all swap partitions;
(5) If the swap file is used, use the Swapon command to specify the swap file to be enabled;
(6) You can use Swapon-s to check the usage of swap;
Example 1 is as follows: 
(1) Use partition as swap
·Fdisk/dev/hda 
· Input N; create a new partition. If the previous example is used, the hard disk should have space left. Here, create a m partition;
· Input t; then input 7 (the new partition here is hda7, which can be viewed by P); input 82 (82 indicates that it is set to swap partition type );
· Enter P. You can see that the hda7 system has changed to Linux swap.
· WQ: Save and quit;
· Execute"Partprobe"Command to allow the kernel to re-read the partition table;
(2)Mkswap/dev/hda7Write a specific Identifier to the swap partition or swap file, similar to the format formatted into virtual memory;
(3) edit the/etc/fstab file to add records so that the Swapon command will be able to find the corresponding partition in the fstab file, the location of the new swap partition can be found at the next boot. Copy the row record of the original swap and change the original volume notation to the device notation of/dev/hda7, others remain unchanged;
(4)Swapon-Command to start all swap partitions Based on the fstab file.Swapon-SCommand to view the status of the current swap;
 
Example 2: Using files as virtual memory is similar to using virtual memory in windows.
(1) create a swap file. a m swap file is created here.
Command:Dd If =/dev/Zero of =/swapfile BS = 1 m COUNT = 100
 
Note: DD: Convert and copy files;
If: Short for inputfile. If =/dev/zero indicates that the zero file is used as the input file, and the zero file can be used as a zero file;
Of: the abbreviation of outputfile. Of =/swapfile indicates that the file is to be input to the/swapfile file;
BS: Short for block size, which indicates forcibly setting the block size of swapfile;
Count: number of blocks to be created;
(2)Mkswap/swaplifeWrite a swap flag to the swapfile file, which is equivalent to the memory format. If it is set successfully, information will be provided;
(3) modify the/etc/fstab file in the same way as the previous example, but rewrite/dev/hda7 to/swapfile. The others remain unchanged;
 
(4) Swapon-A enables swap files
You can also use Swapon-s to check whether swap is loaded after it is enabled.

16. Introduce some tools for maintaining the file system and some important related matters
1. If files in the file system are lost or damaged, run the fsck command to maintain the consistency of the file system:
Before checking a partition, it must be in the unmounted state. If it is in use, it must be unmounted first and then checked;
Format: fsck-y/dev/hda6-y indicates that all questions are answered in the middle;
2. When the system starts up, it will check whether the file is lost or damaged. The system determines the check Sequence Based on the setting in the last column of the/etc/fstab file, this field has been introduced before;
3. If a problematic file exists, it will be first placed in the lost + found directory as a slave; therefore, this directory will be found after a partition is mounted successfully;
4. When the computer is turned on, a serious error occurs, and the system will start it using sulogin. In this example, I randomly modified the volume label in the root directory of the fstab file, after restarting, a serious error will occur, and the error will be stopped at the position of the graph:
 
5. Convert the ext2 File System to the ext3 file system. The command is as follows:
Tune2fs-J/dev/hda6After converting the hda6 partition to the ext3 file system, a message is displayed. In this way, data in the original partition will not be lost;
6. view the current status of the file system
Dumpe2fs-H/dev/hda6Display the hda6 partition information. This command has been described earlier;
-H: displays superblock information.

17. How to add a new hard disk
1. The first step is to connect the hard disk to the computer;
2. After entering the system, use fdisk to partition the hard disk and set the partition type number. For example, if Linux is 83
3. After the partition is created, run patrprobe to re-load the partition and use the latest partition table;
4. After the partition is created, format the partition. If the swap partition is created, you need to write a swap flag for the swap partition;
5. If you want to use the volume label notation, you need to create the volume label name for the partition;
6. Create a new mount point and mount the partition;
7. Add a new partition record to the/etc/fstab file so that the computer can automatically mount the new partition later;

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.