How Linux file System Management (hard disk) works

Source: Internet
Author: User

first, how to identify the hard disk when the system is initialized
1, the system initially according to the information of the MBR to identify the hard disk, including some executable files to load the system, these execution files are in the MBR in front of the 446bytes boot loader program, and the back of the 16x4 space is the location of the storage partition table information;

2. In the partition table, the following information is mainly stored:
(1) partition number, the common partition number is as follows: Other numbers you can use the FDISK directive and then execute L (lowercase l) to view

0x5 (or 0xf) Extended partition
0x82 Linux Swap
0x83 Linux
0x8e Linux LVM
0xfd Linux RAID Auto

(2) The starting magnetic column of the partition;
(3) The total number of magnetic columns;
So when the system is initialized, the hard disk is identified according to the three information in the partition table.

Second, the introduction of hard disk partition related matters
1, when the primary partition is exhausted, you can use the extended partition to add additional partitions, which has been described earlier, but in the Linux kernel:
· The IDE's hard disk can support up to 16 partitions;
· SCSI HDD supports up to 15 partitions;
2. The advantages of hard disk partitioning are:
(1) from the control considerations
Divide the hard disk into multiple partitions, you can put the application, user data, or some need to have security information, respectively into different partitions to facilitate management;
(2) Efficiency considerations
Because the hard disk in the use of a period of time, are divided into chunks of the situation, if a large capacity of the hard disk is not divided into a number of small partitions, then the hard disk search information, because the scope of the search is very large, so it will be relatively long, if the large capacity of the hard disk division of multiple small partitions, in the search
(3) function to use disk quotas
Since quotas can only be set on partitions, we can make a separate partition for the/home directory, and then we can quota the partition;
(4) From data backup and recovery considerations
For example,/home This directory is dedicated to storing user information directory, the directory is a separate partition, you can periodically be a partition for backup work, recovery time is more convenient.

Iii. Introduction of Zoning management
1, you can use the following instructions to establish the partition and view the partition
fdisk /dev/hda Creating a partition
Parameter:-L view Partition
After you execute the FDISK directive, such as:

Example 1: In the new partition , enter n, the system will be asked to enter the starting magnetic column number, the default system will specify a recent unused magnetic column number, enter, and then input the end of the magnetic column number, but according to the magnetic column number to calculate the capacity of a partition is not convenient, you can use the "+ value K or + value m 1000M) "This is directly in the capacity of the representation to set, so intuitive, and then the implementation of P, you can see the newly added partition;"

Example 2: Delete a partition , enter D, ask to enter the partition number to delete, in the Device field column you can see the number of each partition, enter the partition number you want to delete carriage return

Previously all operations were considered no problem, enter W, save exit.
At this point the host screen will prompt:
The kernel still uses old table indicates that the current system is still using the older partition table;
The new table is used at the next reboot. Indicates that the new partition table will be used only after the next reboot;
2. Use the following instructions to reload the partition table into kernel so that the new partition table can be enabled without rebooting.
Partprobe

Iv. Creating a file system (format the hard disk partition)
1. Formatting instructions and instruction syntax:
mke2fs [parameters] the partition to be formatted
For example, in the example above, format HDA6 this partitionMke2fs/dev/hda6
2, after formatting, there will be some information about the partition, in order to understand this information, first understand the format of the partition:
We use Fdisk to create a partition, can not be directly used to store data, the partition must be formatted to work, formatting is divided into a grid of blocks and how many inode can be used, and each block is the file system access to the smallest unit of data, So the data can be stored in these blocks, and each block will form a group, as shown in: In addition to the boot sector, the first block is called Super block to record how many blocks and inode in this partition have been used ; how many block and inode information;

As shown in the following:
Block groups: Indicates that the partition was divided into 13 block group;
8192 blocks per group: Indicates that there are 8,192 blocks in each group;
Inodes per group: Indicates that there are 8,192 inodes in each group;
If the super block is damaged, the partition will not be accessible, so the super block will be backed up once every block, such as:
Superblock backups stored on block: The following value is the location of the block where the Super block is backed up.

3. DUMPE2FS instructions View the details of the partition
Dumpe2fs/dev/hda6 | moreView the information for the specified partition (the contents of the Super block on the previous page, followed by the details of each group), because there is a lot of content displayed so add | More for pagination display;
A partial description of the display information:
The total number of Inode count:inode;
The total number of Block count:block;
Free Blocks: number of remaining blocks;
Fre inodes: Number of remaining inode;
Filesystem features:has_journal table is for ext3 file system
4. Format and parameters of MKE2FS formatting instructions
Format:mke2fs [parameter] the partition file name to format (example:/DEV/HDA6)
Mke2fs-j/dev/hda6
Parameters:
・the: Set the size of each block, preset to 1024bytes (1K), maximum 4096bytes (4K), if the setting is too large to use 4096bytes;
c: Check the partition before formatting to see if there is a damaged block;
• I: Set the size of each inode, example: Mke2fs–i 4096/dev/hda6
· N: The total number of inode is set directly, and the value set is only close;
• M: Set the partition, the amount of space reserved to root, preset reserved 5%;mke2fs–m 10/dev/hda6
· L: Set the partition volume label, preset does not set, usually used to set the label name to the same mount point directory name of the same convenient memory; Mke2fs–l Data/dev/hda6
J.: Set up to become Ext3 file system, that is, join the log function;

v. Introduce the ext2 format of the partition to ext3
1, the difference between ext2 and ext3 is as follows:
(1) ext2 and ext3 are in exactly the same format, Only in the ext3 hard disk at the back of a portion of the space used to hold Journal (log) records;
(2) in ext2, when writing data to the hard disk, the data is written to the cache, when the cache is full, it will be written to the hard disk;
(3) in ext3, when writing data to the hard disk, The data is written to the cache, the Melikyan cache when the system first notify Journal, and then write the data to the hard disk, the completion of the notification journal, the data has been completed writing work;
(4) whether there is a journal difference:
in ext2, The system will check the effective bit (Valid bit), if the value is 1, indicating that the system last had a normal shutdown, if 0, the last shutdown is not a normal shutdown, the system will check the data on the hard disk, so that time will be very long;
in Ext3, which is the journal mechanism, The system checks the journal data at boot time to see if there is an error, so it is much faster;
(5) Tune2fs–j/dev/hda6   convert ext2 to ext3
2, There are three journal modes in the Ext3:
(1) Ordered: Preset mode, only the information in Inode-table is recorded,
(2) journaled: The information of the information itself is recorded, and a lot of space is needed to record it;
(3) Writeback: Do not record information, can provide better performance;

Vi. Introduction of the corresponding volume label in the partition
1, the previous partition notation has been using HDA1 or sda1 similar to the notation, in fact, you can also use the volume of the underlying notation, so better memory.
2. Set and view the instruction format and description of the volume label as follows:
E2label directive:
Example:e2label/dev/hda6 View the volume label name of the Hda6 partition, or blank indicates that the label is not set;
E2LABEL/DEV/HDA6 Data sets the volume label of this partition as data;
Note: In here do not modify the HDA1~HDA5 volume label, may cause the inability to boot, and then do the instructions.

Vii. Introduction Mount instruction Mount partition
1, the concept of mount: When you want to use a device, for example, to read a formatted partition on the hard disk, CD-ROM or software and other devices, you must first have these devices mapped to a directory, and this directory is called "mount point", so that you can read these devices, and the corresponding action is "mount".
A detailed description of Mount will be introduced in the next section
Examples include:

The following is mounted as a volume label, note: Use the volume label to mount the parameter that must be added to the –L to use the volume label mount

Viii. Description of Mount directive and parameters
1, Mount instruction format:
mount   [-t file system type]   [-o parameter]    device name or volume label    mount point directory name
Note: If you mount using a volume label, you must use the –L parameter.
(1)-T after setting the type of file system to mount, for example: Vfat, Ext2, Ext3, iso9660 (CD-ROM), usually this part does not need to add kernel can judge by themselves
(2)-O after adding some parameters:
· SUID: Allow the mounted file system to use SUID, Sgid special permissions,
dev: Allow the mounted file system to establish device files, for example:/dev/hda6 is the device file;
Hda6: After allowing the file system to be mounted, Executable files can be executed;
noexec: Do not allow files;
Auto: Automatically mount the file system after the computer is powered on,
nouser: means only Super User (that is, root) is allowed to mount the file system;
· Async: Set to a different step, the computer writes the data to write to the cache and then write to the hard disk,
loop: Used to mount Loopback devices, such as the CD-ROM machine is loopback device;
ro: Mount file system is set to read-only;
· RW: Mount file system is set to be readable writable;
remount: Re-mount the file system;
If you mount Ext2 or ext3, the preset will use the following parameter settings:
Rw,suid,dev,exec,nouser,async

Ix. introduction of unloading file system
1. Use the Mount command to see which file systems are mounted;
The uninstall instructions and format are as follows:
Umount device file name or mount point
For example, a device that is mounted on a/data directory can be uninstalled using Umount/dev/hda6 or Umount/data
2, if the file system is in use, it will not be able to uninstall normally, you can use the following instructions to view and stop all the operation of the file system, and then the uninstall operation;
fuser-v The device file name or mount point to see which service or user is doing the file system operation;
fuser–km The device file name or mount point forces all operations on this file system to stop;
3, can use remount this parameter to change the state of the file system;
For example:mount–o remount,ro/data re-mount the file system as read-only so that it does not have to be uninstalled and then mounted.

X. Introduction of some examples of mounting
1. mount a file system that is not allowed to execute
mount–o Noexec/dev/hda6/data Noexec is said to be not allowed to execute;
2. Mount a file system image file
mount–t iso9660-o ro,loop boot.iso/iso plus loop parameter is because the disc is a loopback device, Boot.iso is the image file name,/iso is the mount point;

11. Introduce the shared resources on the mounted network
1, the network resources in UNIX and Linux are mainly divided into two kinds of NFS (net File System) and SMB
· Nfs:linux/unix and Unix/linux are used to share resources;
· Smb:linux/unix and Microsoft (Windows) are used to share resources;
2. Hosts that share NFS network resources are known as: NFS Server;
The shared SMB network resource is called by the host: Samba Server;
3. How to connect these network resources:
(1) How to connect the NFS section:
showmount–e IP Address #查看共享资源
Mount IP Address:/shared directory/mount point #挂载网络资源
(2) How to connect the SMB section:
smbclient–l IP address-n #查看指定Samba which shared directories are on the Server;
Mount//IP Address/shared directory/mount point-o username= user name% password #挂载windows网络共享资源;

12, introduce the function of/etc/fstab file
1, the function of the Fstab file is to automatically mount the device to the specified mount point according to the content set in the Fstab file;
2. The following is a detailed description of the contents of this fstab file:
such as: Fstab file content is divided into 6 fields

The following pairs of 6 fields are described separately:
(1)Device Name: You can use the "label=" volume label notation, or the device name notation "/dev/hda6";
(2)mount point: The mount point directory must be a directory that already exists;
(3)File System Type: such as EXT3,EXT2, etc.;
(4)Mount Parameters: is the parameter in the previous Mount instruction –o, defaults represents the default parameter settings, such as/DEV/HDC (disc) and/dev/fd0 (floppy disk) of these two lines of settings, exec can execute the file, Noauto will not automatically mount,
(5)Backup: Set the number of backups, 0 means no backup, 1 means to do a backup every day, 2 means to do a backup every two days;
(6)Check Order: Set the file system check order, 0 means no check, 1 for the first check, 2 for the second check, and so on, and so on the check order up to 9, if the values are the same, check from top to bottom, usually the root directory check order first;
So if you want to automatically mount the file system after the computer boot, you can modify the Fstab this file settings.
For example: You can write the Hda6 of the previous mount to this file, when the boot automatically mount, set the method as follows, use the VI edit fstab file to add the following line of settings to the last line:
/dev/hda6/data ext3 defaults 0 0
2, when the CD-ROM or floppy disk, only need to perform mount/media/cdrom or mount/media/floppy on it, instead of specifying the device to be mounted, because it is set in the Fstab file, because when the directory and the device is mounted, If you do not specify the device to be mounted, the computer will first look in the Fstab file, there is no corresponding device and directory mount, if not also to the mTAB file (in fact, mTAB file is the current system mount settings, so in the uninstall also view this mTAB file, When uninstalling, you only need to specify a mount point or device to view.

13. Introduction Auto-mounter Automatic Mount program automatically mounts network shared resources when using network sharing
1, Automounter is a daemon program, automounters can monitor a directory, such as: monitoring/mnt/nfs directory, when we need to access this directory, when using CD/MNT/NFS to enter this directory, When the Automounter program discovers that the user wants to access the directory, it automatically mounts the network shared resource according to the settings, and automatically uninstalls the directory if it is not used for a period of time (preset 60 seconds), thus reducing the load on the network and using a higher utilization network.
2, the Automounter setting, with the example explanation, only for the reference:
(1)Vi/etc/auto.masterEdit AutoMount Main settings file
(2) Copy has--timeout=60 that line to the following, modified as:

(3) Set up Auto.nfs set file, preset/etc directory without this file, need to establish themselves;
It can be copied by Auto.misc as a template, renamed to Auto.nfs, and edited Auto.nfs modified as follows:

(4)Service AutoFS StartStart the AutoMount service if you have started the executable services autofs restart or service AutoFS reload restart or reload the configuration file;
(5) Use the Cd/mnt/nfs directory, and then execute the LS command there is no file,
With CD server1, you can access it, and then you can view the shared files on the NFS server by automatically mounting them.
Using mount, you can see the NFS network share directory that you just mounted.
Note: I have not set up NFS file server here, so we can only do the client settings, and now can not test;

14, introduced in the ext2 and Ext3 file system which properties can be set
1, in the ext2 and Ext3 file system, support some special attributes to control the characteristics of the file:
·lsattrInstruction: Display the file's property setting;
·chattrdirective: Set the properties of the file;
Format:
chattr +|-|= Property [Properties ...] file name [filename ...]
+
: Add Property
-: Remove Attributes
=: Set Property
You can set one or more properties
Common properties are described below:
· A: The Atime (Access time) record will not be modified when the file is modified;
A: Let the file can only attach content, not allow the original content to be overwritten;
D: Allow the system to use the dump command to do backup, you can not back up this file;
• I: Let the file never change, can not delete or change the file name;
J.: Let the system record the information of the file itself in the ext3 log, even if the ext3 file system is mounted as ordered (ordered) or writeback mode, the information of the file itself is recorded in the Ext3 log.
· S: When the file is modified, do synchronous action, immediately write the data to the hard disk;
Cases:
chattr +a TestAdd a property that allows only additional content
chattr = TestGet rid of all the properties

15. Describe how to use partitioned partitions or files as virtual memory (swap)
1, in the Linux system virtual memory is called: SWAP;
2, swap is divided into two types:
• Use partitioned partitions as swap;
• Use documents as swap;
3, to establish swap virtual memory mainly has the following several basic settings:
(1) The partition or file of the swap is established, and the partition number is set to 82 when the swap is established;
(2) Use the Mkswap command to write a specific swap identifier to a swap partition or swap file;
(3) The/etc/fstab file must be added to the appropriate records, so that the system can be automatically mounted after the swap;
(4) If the partition is swap, to use the Swapon–a command to enable swap, in fact, the swapon–a instruction will read the Fstab file, and according to the Fstab record, to enable all the swap partition;
(5) If the swap file is used, then use the swapon instruction to specify which swap file to enable;
(6) The use of swapon–s can be used to check swap usage;
Example 1 is as follows:
(1) Use partition as Swap
·Fdisk/dev/hda
• Enter N, create a new partition, if you have been in the previous example, the hard disk should have the remaining space, where the creation of a 100M partition;
• Enter T; 7 (the new partition I created here is HDA7, you can enter p to see), and then enter 82 (82 = set to swap partition type);
• Re-enter p; You can see that the system (systems) to HDA7 has become a Linux swap
Wq: Save exit;
ImplementationPartprobe"Command, let kernel re-read the partition table;
(2)Mkswap/dev/hda7Writes a specific flag to a swap partition or swap file, similar to the format of a virtual memory;
(3) Edit the/etc/fstab file to add records, so that a meeting of the Swapon instructions can find the corresponding partition in the Fstab file, the next time you can also find the location of the new swap partition, copy the original swap record, the original volume label notation to the/dev/ HDA7 equipment representation, others remain unchanged;
(4)Swapon–acommand to start all swap partitions according to the Fstab file, in order to verify that you can first use theSwapon–scommand to view the status of the current swap;

Example 2 is as follows: Using files as virtual memory This is very similar to how virtual memory is used under windows
(1) Create swap file, here is 100M swap file
Instructions:DD If=/dev/zero of=/swapfile bs=1m count=100

Description: DD: Convert and copy files;
If: is inputfile abbreviation, If=/dev/zero represents to put zero file as input file, zero file can be deemed to be zero file;
Of: is the abbreviation of OutputFile, the Of=/swapfile representative wants to enter on the/swapfile file;
BS: is the abbreviation of block size, which indicates the size of the block that is forced to set swapfile;
Count: Represents how many blocks to build;
(2)Mkswap/swaplifeWrite a swap flag for the Swapfile file, equivalent to the format of the virtual memory format;
(3) Modify the/etc/fstab file, the same way as the same example, just to rewrite the/dev/hda7 into/swapfile, the other unchanged;

(4) swapon–a enable swap file
You can also view it with Swapon–s before you enable it, and then check to see if the swap is loaded again when enabled.

16. Introduce some tools to maintain the file system and some important related matters
1. If file system files are lost or corrupted, the fsck command can be used to maintain file system consistency:
Before checking a partition, the partition must be in a non-mounted state, if it is in use, it must be uninstalled before performing the inspection work;
Format: Fsck-y/dev/hda6-y said that the middle of the problem is answered yes;
2, the system will check the file when the boot is not lost or damaged, the system is based on the last column of the/etc/fstab file set to determine the order of inspection, this field has been introduced before;
3, if the check out the problem file, will be placed in the Lost+found directory as a spare point; So when a partition is mounted successfully, it will be found that the directory exists;
4, the computer in the boot, a serious error occurred, the system will be started with Sulogin way, the example I will fstab files in the root directory of the random modification of the label, after restarting will be a serious error, stopped in the location of the diagram:

5, the Ext2 file system into the Ext3 file system, instructions such as:
tune2fs-j/dev/hda6Convert the Hda6 partition into a ext3 file system, the conversion will have a prompt message, so that the data in the original partition will not be lost;
6. View the current state information of the file system
dumpe2fs-h/dev/hda6Displays the partition information of the HDA6, as described earlier in this instruction;
-H to display superblock information

17. How to add a new hard drive
1, the first step of course is to connect the hard drive on the computer;
2, after entering the system, use FDISK to partition the hard disk, and set the partition type number; For example: Linux is 83
3, after the establishment of a good partition, the implementation of Patrprobe re-loading partitions, the use of the latest partition table;
4, after the completion of the partition, formatted partition, if the establishment of a swap partition, the swap partition needs to write a swap flag;
5. If you want to use Volume label notation, you need to create a volume label name for the partition;
6. Establish a new mount point and attach the partition;
7, add the new partition record in the/etc/fstab file, so that the computer can automatically mount the new partition;

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.