Reading Notes (10): Linux disk and file system management (3), Linux private dish

Source: Internet
Author: User

Reading Notes (10): Linux disk and file system management (3), Linux private dish

This article mainly summarizes the commands used to manage hard disks and file systems in Linux. When we add a hard disk to the system, what steps do we need to take to truly use it? You can perform the following steps:

(1) partition the disk and create available partitions;

(2) format the partition and format it into a file system that can be used by our operating system;

(3) Check the new file system;

(4) create a mount point and mount the file system.

Next we will introduce the commands related to disk partitioning, formatting, validation, and mounting.

The fdisk command is used for disk partitioning. The basic usage of the fdisk command is "fdisk [-l] device name ". After entering the fdisk command, you can press m to view the help information. Several important commands are: d: delete a partition; n: Add a partition; p: display the partition table on the screen; q: Leave the fdisk program without saving any settings; w: write the settings to the partition table. Of course,You can also run the fdisk-l command to view the hard disk information and partition information of the current host.:


There are still some precautions when using the fdisk command. When we use the w command to write the settings to the partition table, a warning may appear. Let's restart to update the partition table information, this is because our disk is still in use and cannot be detached, so the kernel cannot obtain partition table information again. In addition to restarting, you can also use the partprobe tool released by GNU to force the kernel to re-find the partition table. In addition, the fdisk command cannot be processed for disks larger than 2 TB. In this case, you need the parted command to partition the disk.

The format command is run using the mkfs (make file system) command. The method is "mkfs [-t File System Format] device file name ". Mkfs is a comprehensive command. When we use the "mkfs-t ext3..." command, the system will call the mkfs. ext3 command for corresponding formatting.After entering the mkfs command, we can press two tabs consecutively to viewThe file format supported by mkfs.The following is the file system format supported by mkfs on my system:


The vfat format can be supported in both Windows and Linux. Therefore, you need to share the USB flash drive in Windows and Linux to use this format. When using the mkfs command, in addition to specifying the file system type, you can also specify the file system's volume label, block size, inode size, and so on. Mkfs calls the mke2fs utility to set these parameters, the usage of mke2fs is "mke2fs [-B blocksize] [-I inodesize] [-L volume label] [-cj] device ".

The fsck command is used to check and correct file system errors. The fsck command is used only when it is a root user and the file system is faulty. Otherwise, the command may cause damage to the system, of course, you can use the fsck command to detect the successfully formatted partitions. At this time, the detected partitions must not be mounted to the system, that is, the file system must be uninstalled.When the fsck command is used to detect a disk, problematic data is stored in the directory lost + found of the file system.The top-level directory of each file system (that is, under the directory of the vertex) will have a lost + found directory. In addition to the fsck command, you can also use the badlocks command to check whether the hard disk sector has bad tracks.

Next we will discuss the concept of attaching and detaching a disk. The mount point must be a directory, which is the entry to the disk partition. You can access the data in the file system by accessing this directory. In Linux, the mount command is used to mount the file system. The mount command is a very useful command that can mount file systems such as ext3, vfat, and nfs. The mount command is used as follows:

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

Mount-l: Enter the mount command to display the mounting information of the current system. Add the-l parameter to add the Label name;

Mount [-t file system] [-L Label name] [-o additional options] [-n] device file name mount point: mount a device as specified.

The mount command is very simple to use. In actual mounting, you do not even need to use the-t parameter to specify the file system type, because almost all file systems have super blocks, during mounting, Linux automatically analyzes the super block and uses the Linux driver to test the mounting. If the test succeeds, the system immediately uses this type of file system to mount the device. So when we insert a USB flash disk into the computer, if you want to use the USB flash disk, you also need to use the mount command to mount the USB flash disk. The mount command can also be remounted using the-o remount parameter. When we enter the single-user maintenance mode,Because the root directory is often mounted as read-only by the system, you can use this parameter to re-mount it.

In addition to mounting devices, you can mount a directory to another directory through the mount -- bind command. Accessing a mount point in the future is equivalent to accessing this directory, of course, this effect can also be achieved through symbolic connections. The following is an example of mounting a directory using mount:


After mounting, you can run the umount command to detach the device. The usage of umount is "umount [-fn] device file name or mount point ". You can use the device file name or mount point to unmount the device,Run the df or mount-l command to check whether the file system exists in the directory tree.However, if a directory is mounted instead of a disk device, it can only be detached through a mount point. Note that you need to change the working directory to a directory other than the mount point (and its subdirectories.

Next we will introduce some commands for modifying disk parameters. As mentioned before, in Linux, all devices are represented by files. How to express a device through files is replaced by the major and minor values of the files. Major is the master device code, and minor is the sub-device code. The kernel determines the device data based on these two values. After the Linux kernel is upgraded to 2.6, the device file name of the hardware can be automatically generated by the system in real time. You do not need to manually create the file, but you still need to know how to manually create the device file for emergency purposes. The mknod command is used to create a device file. The command is used as "mknod device file name [bcp] [Major] [Minor]".

Each file system has a file system Label, which can be specified when the file system is formatted (mkfs) or modified using the e2label command. E2label is used as the "e2label device name new Label name". The volume name of the Linux file system is similar to a name (C or D) in each disk in the Window ). During mounting, we can also use the volume name of the file system for mounting. In CentOS, the/etc/fstab file settings use the Label name by default. What are the advantages of using the volume name? No matter how the file name of your disk device changes, the system uses the Label name to identify your hard disk, so no matter which interface your hard disk is inserted to, it will not affect.

There is also the tune2fs command. The tune2fs command has a wide range of functions. You can run the following commands on your own. The hdparm command can be used to set many advanced parameters for hard disks on the IDE interface, but it is of little use for hard disks on the SATA interface, the only thing that can be done is to test the disk performance through the "hdparm-t device name" method, but the test results are not very accurate and can only be used as a reference.

It is not convenient to manually use the mount command to mount the file system. You can modify the/etc/fstab file to automatically mount the file system when it is started. Every time we use the mount command to mount the file system, we write the corresponding parameters to the/etc/fstab file. This file has six fields. You can understand the meaning of each field by name. The Device field indicates the file name of the disk Device or the Label name of the Device. By default, the Device field uses the Label name. Parameters in the fourth column represents the parameters of the file system. You can set many parameters, such as synchronous or asynchronous, whether to allow user mounting, and so on. The dump column indicates whether the file system can be backed up by the dump command. 0 indicates no backup, 1 indicates daily dump backup, and 2 indicates another dump backup with an indefinite date. The fsck column indicates whether to use the fsck program to detect the file system. 0 indicates no detection, 1 indicates detection, and 2 indicates detection, but it is later than 1, therefore, set the file system where the root directory is located to 1, and set the other to 2. After we modify the/etc/fstab file, we cannot start the system to prevent syntax errors,After modification, use the df command or the mount-l command to test whether the command can be obtained.Properly hangs information.

In Linux, we can access the data in an image file without burning it into a CD. We can directly use the special standby loop method for mounting,That isMount-o loop to mount an image fileAnd then access the mount point to access the data in the image file. It's really convenient. in Windows, we have to use third-party software to open the image file. In addition to image files, we can also use this method to mount a large file. This is equivalent to creating partitions without modifying the partition table. Mount the CentOS installation image file using mount-o loop:


Finally, we will introduce the construction of memory swap space (swap. When installing CentOS, it indicates the need to build swap (memory swap space ). When the host's physical memory is insufficient, the kernel will move the programs and data temporarily unavailable in the physical memory to the swap, so as to empty the physical memory to load the program to be executed. Therefore, the function of swap is to expand the memory when the physical memory is insufficient. If you forget to build a swap when installing Linux, you can use the following method to create a swap: first, use the fsck command to separate a partition for the system as a swap, then, use the mkswap command to format the partition into the swap format, and then start the swap device using the "swapon Device File Name" method.AvailableFree memory check command to check whether swap is correctly loaded.In addition to building a partition as a swap, you can also use a large file to construct a swap. The method is similar. You can also use swapoff to release swap files when using files to build swap files.


Summary of Linux commands in this chapter:

Fdisk, partprobe, mkfs, mke2fs, fsck, badlocks, mount, umount, mknod, e2label, tune2fs, hdparm, mkswap, swapon, swapoff, and free.
 
How does laruence's Linux basic learning for Private food (the third edition) book?

It must be better for the third edition. The basic knowledge of the third edition is more detailed and new things are added. If you want to buy books, I suggest you check the electronic version on the Internet, I think it is suitable for me to buy again.

Laruence's Linux Private food: Basic Learning (Third edition) [PDF] Email: huaifang0224 @ 126com

Sent ,..
 

Related Article

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.