Chapter 8 Linux disk management

Source: Internet
Author: User
Tags ide hard drive

Chapter 8 Linux disk management

View disk or directory capacityDfAndDu]

DfYou can view the total capacity, usage, and remaining capacity of a mounted disk without adding any parameters. The default value is displayed in k.

Common df parameters include-I-h-k-m.

-I use inodes to display results

-H use an appropriate unit for display, such as G

-K-m indicates that K is used, and M indicates that the Unit is displayed.

Briefly introduce the data you see. Filesystem indicates the sector, that is, the partition when you divide the disk; 1K-blocks/1M-blocks indicates that the Unit is 1 K/1 m; Used and Available are Used and remaining; use % is the percentage that has been used. If this value is greater than 90%, you should note that the disk may be full immediately; Mounted on indicates the partition (sector) the location where the disk is attached.

DuUsed to view the space occupied by a directory

Syntax: du [-abckmsh] [file or directory name] common parameters include:

-A: all files and directory sizes are listed. If no options or parameters are added, only the directory (including subdirectories) size is listed.

-B: the values listed are output in bytes. The default value is Kbytes.

-C: add the final sum

-K: Output in KB

-M: Output in MB

-S: only lists the sum.

-H: the system automatically adjusts the unit. For example, if the file is too small, it may be a few K. The unit is K. If the file is as large as several GB, the unit is G. I am used to the format of du-sh filename.

Disk partitioning and formatting]

What I often do is partition and format a new disk. This also shows that as a linux system administrator, you must be familiar with disk operations. So please study this part carefully.

Hard Disk partitioning tool in fdisk linux

Syntax: fdisk [-l] [device name]

-L: all disk devices and partition tables in the system are directly listed without the device name. The partition table of the device is listed with the device name.

If-l is not added, the system enters another mode. In this mode, you can partition the disk.

When you first enter this mode, a prompt Command (m for help) will be displayed: press m to print the help list. If you are good at English, it is difficult for you to understand the functions of these letters. I usually use p, n, d, w, q.

P: print the partition information of the current disk.

N: Create a New partition.

W: Save the operation.

Q: exit.

D: delete a partition.

Because my linux system is installed on a virtual machine, I can add a new disk. Then I will divide the new disk into multiple partitions.

When you run the fdisk-l command again, A/dev/hdb device is added and no partition table is available for the device. Next we will share this/dev/hdb.

First, use p to check that there is no partition information.

If you use n to create a new partition, you will be prompted to create an e (extended partition) or p (primary partition). Here I select the primary partition, so press p and press Enter, you can enter First cylinder or press ENTER or enter a number. Because this disk is new and does not have any partitions, the press ENTER starts from 1. You can also customize the input, but not more than 2080. Enter 1 here and press Enter. At this time, you will be prompted about the score, you can write a value (2-2080), you can also enter + sizeK or + sizeM, the latter is more intuitive and easy to understand, so here I enter + 100 M, that is, I divided a master partition of M. When you use p to view the data, a partition is actually added. Then I continue to repeat the previous operations and create four primary partitions. When I enter n again to create a partition, an error is returned.

As a result, you will find that you can create up to four primary partitions in linux. What if you want to create more partitions? It is easy to select the extended partition when creating the fourth partition after the third partition is created.

Delete the fourth primary partition and create an extended partition.

When creating an extended partition, I will ask you how many partitions you want to allocate to this extended partition. I will press enter to distribute all the space to this extended partition. This extended partition/dev/hdb4 cannot write data into it. It is just an empty shell and we need to continue creating partitions in this shell.

When you create an extended partition and then press n to create a new partition, you will no longer be prompted to create p or e, because we can no longer create p. What you need to understand here is that hdb5 is actually only a subpartition in hdb4, and only four available partitions have been available so far, so I will create another 5th partitions.

Then press w to save. This mode automatically exits. If you do not want to save the partition information, press q to exit.

Next we will delete the partition we just got and re-create the partition. Remember how to delete it. Right, press d and select the appropriate number. After all partitions are deleted, the disk is restored.

In the first partition, we create an extended partition. And allocate 200 M to it.

When the partition is created again, it is no longer p or e, but p or l (logical partition). Why? As mentioned above, an extended partition is just an empty shell. Only smaller partitions can be further divided under the extended partition. This smaller partition is actually a logical partition.

In addition, this logical partition starts from 5 words by default, because the preceding number is either reserved for the primary partition or for the extended partition. We can also obtain that a maximum of four primary partitions can be created in linux. Once four primary partitions are created, no partitions can be added. In addition, you can create up to one extended partition. How many logical partitions can be created under an extended partition? IDE Hard Drive (similar to hda, hdb, hdc, etc.) can create up to 10 (hdb5-hdb15), this is the result of my experiment. Some documents say that there is no limit on the logical partitions in linux, and some may say that the maximum value is 64. If yes, you need to take a closer look, it makes no sense to study this issue in depth.

Through the above operations, I believe you have learned how to partition using fdisk. It is worth noting that it is highly risky not to be idle and free from partitioning. If you leave nothing to worry about, you will lose all the data on your server. If there is a partition operation, please be 200% careful, remember to remember!

Mkfs. ext2/mkfs. ext3/mke2fsFormatLinuxHard Disk partitioning

When you use man to query the help documents of these three commands, you will find that we see the same help document, which means the three commands are the same. Common options include:

-B: Set the space occupied by each data block during partitioning. Currently, 1024,204 bytes and 4096 bytes are supported.

-I: Set inode size

-N: set the number of inode. Sometimes the default number of inode is insufficient. Therefore, you must set the number of inode.

-C: Check whether the disk is faulty before formatting. If this option is added, it will be very slow.

-L: default label of the partition

-J: Create partitions in ext3 format. This option is not required if mkfs. ext3 is used.

Format/dev/hdb1 directly without adding any options

In the preceding example, the default value of block size is 4096, while that of inode is 1024.

The label name of the custom partition in the following example.

E2labelUsed to view or modify the partition label (Label)

This command is very simple. You can view the label of the partition directly following the partition number. When you want to modify the label name, you can follow the partition number with the desired label name.

FsckCheck whether there is any bad track on the hard disk

Syntax: fsck [-Aar] [partition]

-A: When this parameter is added, you do not need to use the partition name as the parameter. It will automatically check all the partitions in the/etc/fstab file (this operation will be performed once during the boot process );

-A: the faulty partition is automatically repaired;

-R: When a partition with a bad track is detected, the user is asked to decide whether to fix it.

When you use fsck to check whether the disk has a bad track, you will be prompted to run this task, which may cause damage to some mounted file systems. Therefore, do not run this command easily. Otherwise, the system cannot even start up.

Mount/Detach Disk]

In the above content, we talked about partitioning and formatting the disk. After formatting, how can we use it? This involves attaching this disk. The formatted disk is actually a block device file of type B. You may think that since this block file is the partition, then, won't I write data directly in that file into that partition? Of course not.

You must create a mount point before mounting a partition. This mount point appears as a directory. Once a partition is mounted to this mount point (directory), data will be written to this directory. Note that the mount point (directory) must be an empty directory before mounting the partition. In fact, if the directory is not empty, it does not affect the use of the mounted partition. However, once the directory is mounted, the previous contents cannot be seen. You can only view the partition after it is detached.

MountMount a device

First create the/test1/test2 directory, and then create a 1.txt file under the/test1directory.

Mount the/dev/hdb1 partition to the/test1 directory, and then view the/test1directory. 1.txt does not exist. Write Data to the/test1 directory and write it to the/dev/hdb1 partition. When talking about the mount-a option, we need to first understand this file/etc/fstab

This file is the partitions to be mounted when the system starts. The first column is the partition label, the second column is the mount point, the third column is the partition format, and the fourth column is some mount parameters of the mount, which will be described in detail, generally, you can write defaults directly. The number in the fifth column indicates whether the data is backed up by dump. If yes, it is 1; otherwise, it is 0; and the sixth column indicates whether the data disk is self-checked during startup, the fsck detection we just talked about. 1, 2 indicates detection, 0 indicates no detection. in Redhat, this 1, 2 also indicates that/partition must be set to 1, and only one of fstab can appear, here is a saying of priority. 1 has a higher priority than 2, so check 1 first and then test 2. If multiple partitions need to be checked at startup, set them to 2, 1. After the detection is complete, the system will detect 2 at the same time. The following describes the parameters commonly used in the fourth column.

Async/sync: async indicates that it is not synchronized with the disk and memory. The system writes the memory data to the disk at intervals, while sync synchronizes the data in the memory and disk from time to time;

Auto/noauto: automatic mounting/not automatic mounting at startup;

Default: Set the Mount definition according to the default values of most permanent file systems. It contains rw, suid, dev, exec, auto, nouser, async;

Ro: mounting Based on read-only permissions;

Rw: Mount data by read/write permissions;

Exec/noexec: Allow/disallow execution of the file, but do not mount the root partition as noexec, so the system cannot be used and the mount command cannot be used, at this time, only the system is re-built;

User/nouser: allows/does not allow other users except root to mount partitions. For security reasons, use nouser;

Suid/nosuid: allows/does not allow partitions to have the suid attribute. Generally, set nosuid;

Usrquota: enable the user's disk quota mode. The disk quota is described in subsequent sections;

Grquota: enable the Group Disk Quota mode;

After learning this/etc/fstab, we can modify the file and add a row to mount the new partition. For example, the author adds such a line

/Dev/hdb1/test1 ext3 defaults 0 0

Then the partition will be mounted when the system restarts.

After the lecture, we will continue to talk about the mount. If mout-a runs this command, it will mount all the disk partitions in/etc/fstab. So when you add a line to the/etc/fstab file, you can directly run mount-a to mount the line you added, so that you do not need to restart it.

You can use the mount-o option to re-mount a partition and specify the options you want (that is, those in column 6 of fstab described above)

You can see that if the ro option is used, you cannot create a file.

Remount the file and restore it to normal. If no other options are added, it is ults.

I have encountered such a situation in my daily O & M work. a new bright disk and disk a (displayed as sdc on the server) are installed on a server) and disk B (displayed as sdd on the server), once the two disks are unplugged, and then plug in again, restart the machine, the result is that the disk number is changed, a is changed to sdd, B is changed to sdc (this is because the disk is inserted into the wrong slot. Through the above learning, you mount a disk by using a partition name such as/dev/hdb1. If you have previously joined/etc/fstab, after the system is started, an error partition is mounted. So how can we avoid this situation?

BlkidThis command is used to display the disk partition uuid. uuid is actually a large string of characters. in linux, each partition has a unique uuid. Speaking of this, you can think of it smartly. If there is no error, we will use this unique uuid to mount disk partitions.

This command is only used to display the uuid. It has no other purposes and is not described in detail. Of course, you can also follow a partition behind the command to only display the uuid of the partition.

You can see that it is actually very useful. So how can I enable it? Simply write the mount disk command you just typed to the/etc/rc. d/rc. local file. By the way, I haven't told you the role of this rc. local file. Simply put, the system will execute the commands in this file after it is started. So as long as you want to write all the commands to this file after the boot.

In fact, this file is a shell script. I will introduce it in a separate chapter later. "#" At the beginning of the line indicates that the line does not take effect in this script. If you want to run any command after the system starts up, write the command here. For example, the command that I mounted just now. You can achieve this:

Mount also has a common option-t, which specifies the type of the file system. For example, you need to specify vfat when mounting a floppy disk, and you need to specify iso9660 when mounting a CD, however, with my years of experience, the current systems are smart identification of the System Format category of the partition to be mounted. That is to say, it will be automatically judged if you do not need to specify it.

UmountDetach a device

Now you have learned how to mount a device, and how to detach a device requires umount. This command is also very simple and can be followed by a mount point, you can also follow the partition name (/dev/hdb1)

Sometimes you may encounter a device that is difficult to uninstall, just as you cannot delete a USB flash drive in windows. The only way to use it is umount-l/dev/hdb1, this-l option indicates forced uninstallation. You must remember that it is very useful.

CreateSwapFile]

The swap has been touched since the system was installed. As mentioned earlier, it is similar to the virtual memory of windows. Generally, the size of the swap is twice the size of the memory, if your memory exceeds 4 GB, 8 GB does not seem necessary. 4G is enough for daily exchange. However, there may also be insufficient virtual memory. If so, do you have to split the disk again? Of course not! Then we add a virtual disk.

The basic idea is: Create swapfile? Formatted as swap? Enable this virtual disk

Use dd to create a 419M file/tmp/newdisk, where if indicates which file to read and/dev/zero is a unique 0 generator in linux, of indicates the file to be output. bs indicates the block size, and count indicates the number of blocks.

The mkswap command is used to format the swap format partition. When using this command, you must be clear about it. Otherwise, when formatting other partitions, you can only cry.

Free is used to view the system memory and virtual memory usage, and the-m option is in the form of M. You can see the current system. Swapon is used to enable the newly created swap file. After it is enabled, use free to check and find that 400 mb is more.

We can also use swapoff to disable the enabled swap file.

Disk Quota]

Disk cooperation is actually to assign a certain disk quota to each user, only allowing him to use the disk space within the quota range. In linux, It is a multi-user multi-task environment, so many people share a disk. It is necessary to limit a certain amount of disk space for each user so that it is fair.

In linux, quota is used to manage disk quotas. If you do not have quota on linux, you need to install the package quota-3.13-5.el5.RPM (in fact, the version is not much, the key is this package ). In practice, quota restricts the entire partition. If your/dev/hda3 is mounted to the/home directory, all/home directories will be restricted.

The quota module is mainly divided into quota quotacheck quotaoff quotaon quotastats edquota setquota warnquota repquota commands. The following describes these commands respectively.

Quota is used to display the quota of a group or user.

Syntax: quota [-guvs] [user, group]

-G: displays the quota of a group.

-U: displays the quota of a user.

-V: Display meaning

-S: Select inod or hard disk space to display

 

Quotacheck is used to scan the quota space of a disk.

Syntax: quotacheck [-auvg]/path

-A: scans all mounted disks with quota support.

-U: scan a user's files and directories.

-G: scan files and directories in a group.

-V: displays the scanning process.

-M: Force Scan

 

Edquota is used to edit the quota value of a user or group.

Syntax: edquota [-u user] [-g group] [-t]

Edquota-p user-u user

-U: edit a user's quota

-G: edit the quota of a group.

-T: edit the grace period

-P: copy the quta of a user or group to another user or group.

When you run edquota-u user, the system opens a file and you will see that there are seven columns in this file, which respectively indicate the following meanings:

Filesystem: disk partition, such as/dev/hda3

Blocks: the disk capacity occupied by the current user in the current Filesystem. The unit is Kb. Do not modify this value.

Soft/hard: the quota value of the current user in the Filesystem. soft refers to the minimum limit and can exceed this value, however, the disk capacity must be reduced below this value within the grace period. Hard refers to the maximum limit, that is, it cannot exceed this value. When your disk usage is higher than the soft value, the system will warn you that you want to reduce the space used to the soft value within the grace period.

Inodes: the status of the currently used inode, which does not need to be modified.

 

Quotaon starts quta. After quota is edited, quta takes effect only after being started.

Syntax: quotaon [-a] [-uvg directory]

-A: All set quota boot

-U: Start a user's quota

-G: Start the quota of a group.

-S: displays related information.

 

Quotaoff disable quota

This command is commonly used only when quotaoff-a disables all quota

The above describes a lot of quota-related commands, so I will teach you how to apply this disk quota in practice. The entire execution process is as follows:

First, check whether your/home directory is attached to a single partition. use df to view it. If not, you need to work with me. Otherwise, this step is omitted.

In my linux system,/home does not occupy a single partition. Therefore, you need to mount the/home directory to a separate partition, because quota Limits partitions.

I use fdisk-l to check that the current/dev/hdb disk has five available partitions, so I plan to mount/dev/hdb1 to the/home directory.

Now, the author's/home directory is already a separate partition.

1) create a test user

First, a test user is created, and a test group is created. You can have rows starting with test in/etc/passwd, where both uid and gid are 500, and then create a test1 account to add it to the test group, view the/etc/passwd file and find that the gid of the test and test1 users is 500. (Maybe you are not familiar with the/etc/passwd file, add a user, and the concepts of uid and gid. It doesn't matter. It will be introduced in later chapters, here you only need to understand)

2) Enable the disk's quota Function

By default, linux does not support quota for any partitions, so we need to manually enable the disk's quota function. Do you remember, when analyzing the fourth column of the/etc/fstab file in the previous section, we talked about this quota option (usrquota, or kgquota ). Yes. To enable the quota support for this disk, You need to modify the fourth column. Use vim to edit/etc/fstab and add a line, for example:

The vim command will be detailed in the subsequent sections. The previous section describes how to enter the editing mode and how to save files. If your linux system already has the/home line, you can directly modify the fourth column, and add usrquota and kgguota (no space in the middle ). Next we need to re-mount/home.

You can also re-mount/home

How can I check whether quota is enabled?

You only need to check whether the/etc/mtab file contains usrguota and kgquota in the/home line. The author's/dev/hdb1 now supports quota

3) scan the disk user usage and generate important aquota. group and aquota. user

Quotacheck is used in this step. aquota. group and aqouta. user are the configuration files required for the group and the user's disk quota. If these two files are not available, the disk quota does not take effect.

When you use the quotacheck command for the first time, the system prompts "cannot stat old user quota file ......" In fact, it is prompted that you do not have two files, aquota. user and aquota. group, in the/home directory. It does not matter, because the disk quota has not been configured before, of course, there are no these two files. After the quotacheck command is executed, the two files are generated in the/home directory.

4) Start the quota

5) edit the user's Disk Quota

First, set the quota of the test account, and then copy the quota of test to test1. Edquota is required here.

Modify the content above

The Unit is Kb, so the soft value is about 20 Mb, and the hard value is 30 Mb. Save the file in the same way as vim.

Copy the quota of test to test1. Next, we will continue to set the grace period.

The default value is 7 days. Here we change it to 1 days. Next, let's take a look at the quotas of test and test1 users.

6) Edit Group Disk Quota

Set the soft quota of the test group to 40 M, and the hard value to 50 M. View the quota of the test group.

7) set startup

The command used to start the disk quota is quotaon-aug. To start the disk, you only need to add this command to the/etc/rc. d/rc. local file.

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.