Linux Getting Started Tutorial: P13->Disk Management

This series of articles is the learning notes
of the Linux introductory tutorial of Mr. Wu Shengran of Shang Silicon Valley. The previous series of articles are linked as follows . Linux introductory tutorial: P6->system management Linux introductory tutorial: P7->Shell introduction Linux introductory tutorial: P8->file directory Linux introductory tutorial: P9->time and date introductory Linux tutorial: P10->user authority introductory Linux Tutorial: P11->File Search Linux Introductory Tutorial: P12->Compression and Decompression









1. View the space occupied by the directory

background

①Display directory details
As we said earlier, you can use ls -lor llcommand to display directory information in detail. But if there are nested subdirectories in its subdirectories, and there are many levels, it is inconvenient for us to see it directly at this time. For this kind of display, there is another small tool in Linux, tree. It lists all subdirectory levels under the directory you want to list in a standard tree directory. There is no such tool in the default Centos7, and it can be yum install treeinstalled through. After the installation is complete, view the structure of the current directory, as shown below. Because there are Chinese characters in it, these numbers are the corresponding Unicode.
insert image description here
②Display disk usage
But we still have another requirement, that is, in the actual operation process, we not only want to know the current directory structure, but as the usage time gets longer and longer, there are more and more file directories. The hierarchical structure will become more and more complex, and the hard disk space occupied will also become larger and larger. Therefore, we should be concerned about the current hard disk usage at any time. Although ls -lhit can be clearly seen how big each file is under the current directory, if we use to ls -lg /view the file size of the root directory, we can find that the size is only 28K.
insert image description here
This is because ls -lhthe size of all the files and directories listed here is simply superimposed, and when the size is listed here, the total size of each directory is not nested and calculated together. For example, the root here, as we have seen before, there are many tens of megabytes of things in the root, but here it is only 4K. In fact, it only counts the current directory.
So what we hope more is that there is a single command that can directly count all the contents in the current directory, including the size of the hard disk space occupied by sub-files and sub-folders below it.

of


du: disk usage Basic syntax of disk usage : du 目录/文件(Function description: display the disk usage of each subdirectory under the directory)
If you use du directly, all the things that occupy space in the current directory will be listed. It will be messy to look at this way. In fact, we just want to see how big the current directory is in a relatively simple way. We can directly look at the last line, which is the size of the current directory. Of course, du has many optional options that can make the output more friendly.
insert image description here
Option Description:
-h Display in GBytes, MBytes, KBytes and other formats that are easier for people to read;
-anot only check the size of subdirectories, but also include files.
-cAfter displaying the size of all files and subdirectories, display the total.
-sOnly display the total.
--max-depth=nSpecify the depth of the statistical subdirectory as Example of layer n
: Use du -ahsimplified display.
insert image description here
Use to du -shdisplay the sum of the size of the current directory
insert image description here
Use to du --max-depth=1 -ahdisplay the sum of the size of the current directory, but only the first level of depth is displayed.
insert image description here


Second, check the disk usage

background

We can now use to du -sh /view the size of the root directory, and we can see that the size is 11G.
insert image description here
But this is not the total size of our current hard disk. It looks similar, but it is still not the same as the real disk. Before installing the system, I did the disk partition operation, and divided a boot partition, a swap partition and a root partition. Usually, all the files we use are stored in this root partition. Because the Linux file system is a virtual directory, we have no way to see which disk occupies how much space from the directory structure. At this time, we can't use the du command, we need to use it df.

df

df: disk free Basic syntax of free disk
: df 选项 (Function description: list the overall disk usage of the file system, check the disk space usage of the file system)
Example: use df -hto view the disk usage, as shown below.
insert image description here
It can be seen that:
①We allocated 55G memory for the root partition before, and the corresponding device is /dev/sda3
②We allocated 1G memory for the boot partition before, and the corresponding device is /dev/sda1
③The rest is actually not all the information occupied by the disk, because we It can be seen that their file system is not managed under the dev directory. This file system is called tmpfs or dvtmpfs, which means a temporary file system.
Memory-based file system
It is a special type of file system in Linux. So here we not only have a display for the hard disk, but also have a display for the devices corresponding to the memory.
Question: What kind of device corresponds to the temporary file system set by the memory?
Answer: real memory and swap partition. The swap partition is mainly used to exchange pages with the memory, which can be used as virtual memory, so the file systems of the two are consistent, and both use the temporary file system tmpfs.
Here we can see very clearly:
① There is a file system tmpfs, and the mount point is /dev/shm. shm is shared memory, that is, shared memory. That is to say, this memory space is shared by all system processes, and all these processes can access this directory. The size of this memory is half of the system memory by default, 2G.
② There is a file system tmpfs, the mount point is/run. These are all related to the runtime, so we can think of the memory and virtual memory swap partition as something related to the system or runtime.
③There is a file system devtmpfs, which means that the linux kernel will create a file system when it starts, and then create a /devdirectory, which is the mount point of the current file system, which is mainly used to manage all our current devices. So these 4 2Gs add up to 8G. Our swap was allocated 4G at that time, and the memory itself is also 4G, so the total is 8G.

free

Example: Use free -hto view memory usage.
insert image description here
We can see the total space of the current physical memory and virtual memory (swap partition), how much space is used, how much space is free, and the memory space being shared.


3. Check the device mounting status

lsblk

Function description: View the device mounting status.
The previous two commands did not see the real disk information in detail, but lsbkc can. lsblk is the abbreviation of list block, which lists the mounting status of all current block devices (storage devices such as hard disks and optical disks).
Example:
Use lsblkto view the mounting status of the disk.
insert image description here
It can be seen that:
①There is currently a storage device sda, and its type is disk-hard disk.
②This hard disk is divided into 3 partitions
③The mountpoint behind is the corresponding mount point.
④The first boot partition is mounted under boot, the second swap partition is mounted under swap, and the last partition is mounted under the root directory. These partitions are also the same size as we originally allocated. In addition to the hard disk sda,
cdrom , we see that there is another one below , and its type is rom. We used a CD when we installed the system. We used the CD drive to load the ISO image file into it, and started the system from the CD drive. Now although the information in that CD is not used, and it is not directly mounted, the CD-ROM device is still there, and its type is cdrom. All devices are under the /dev/ directory, we can find them through. Use show complete information. It can be found that the type of sr0 is b, that is, block, a block storage device. In addition to this there is a cdrom which is a soft link (type is ) directly linked to sr0. This is because Linux directly gave sr0 a more understandable name, called cdrom, for the convenience of our management.
sr0
ls /dev/ | grep sr0
insert image description here
ll /dev/ | grep sr0
insert image description here
l

The origin of the name of the hard disk

When you use lsblk to view the device mounting status, you will see different names depending on the hard disk, such as sda, hda, vda. Here we mainly look at the meaning of the first letter, which is the most common sda. Regardless of whether it is a personal computer or a server, the hard disks we use are mainly divided into the following categories according to different interface types:
IDE硬盘: Early hard disks used more IDEs, but now it is rare, because its read and write performance is relatively high. Difference.
SATA硬盘: Hard disk with serial ATA interface specification, supports hot swap. Compared with the traditional IDE hard disk, it is much faster, and has a large data storage capacity and lower cost, so now our personal computers generally use SATA hard disks.
SCSI硬盘:
SCSI is a computer system standard interface, so not only hard drives can use this interface, but also various computer peripherals such as optical drives and scanners can use this interface. So it itself has a dedicated controller for data operation and data transmission, and its data transmission speed will be faster. The server requires stronger performance, so there will be more SCSI hard disks. When we were doing virtual machine configuration before, we selected SCSI hard disk as the default hard disk.
insert image description here

Linux rules for naming hard disk partitions

①If there are multiple hard disks, such as IDE hard disks, their names all HDstart with the beginning. The first hard disk is called HDA, the second is HDB...and so on.
②If there are multiple SATA or SCSI hard disks, they are all SDat the beginning. Similarly, the first hard disk is called SDA, and the second hard disk is called SDB.
③If you use a virtualized analog device, start VDwith .
If each hard disk needs to be partitioned, just add numbers 1, 2, 3... after the name. So we saw sda1, sda2, sda3 earlier.

lsblk -f

lsblk -f : View detailed device mounts and display file system information.
Example: use lsblk -f, you can see the file system information.
insert image description here
It can be seen that:
①In addition to the current hard disk and partition name and the subsequent mount point, the two main information, there is also the type of the file system.
②We chose XFS at that time, a 64-bit high-performance log file system. If we chose the EXT4 fourth-generation extended file system at that time, then the display here must be EXT4.
③The file type of the second partition is different, it is swap.
④ There is a string of characters behind each partition, which is UUID. UUID is a unique symbol created by the system for each current partition, with a total of 40 bits. Use this ID to uniquely specify the current partition.


Fourth, mount and uninstall

background

①Before we found that in addition to the mounted hard disk, there is also a CD underneath. This disc has no mount point, so it cannot be accessed directly, because all devices must correspond to a directory. Under /dev/ are our device files, and it is definitely impossible to find things in the CD. To find the contents of the disc, the disc must be mounted somewhere.
②When talking about hard disk partitions, we mentioned the mount point. We currently have a hard disk, and we divide it into three partitions: boot partition (1G), swap partition (4G), and root partition (45G). Usually, when performing operations, it does not say which partition to directly put a large file into. Generally speaking, there is a file system with many directories in it, such as root directory, root directory, home directory and so on. Now we can directly put this file under the corresponding directory.
③Which storage location on our hard disk does this directory correspond to? This correspondence needs to be represented by a mapping, which is what we call a mount point. The file system is originally a virtual directory structure. When we use this directory, we don't care where its underlying storage location is, but Linux needs to know this correspondence, as long as it is configured once when the hard disk is partitioned. , we don’t need to worry about it later.
④ During this configuration process, we know that the mount point of the first partition is mounted under the boot, so as long as all your files under the boot directory will be stored in the disk space of the first partition of a G . The 4 G of the second partition is the swap partition, we will not put files in it, this is our swap partition, virtual memory. In the end, the 45 G are directly mounted under the root directory, so except for the content in the boot directory, we are all branches based on the extension of this root directory. So all the remaining files and folders are stored in the 45G space of the third partition.
⑤So now we have another CD, although we don't need to partition it anymore, we should also consider the files in the current CD. As long as we set a mount point for this disc and mount it, we can access it through the file system. We said before that there is a directory called /mnt/, which originally allows us to mount some external storage devices, so we mount this CD under the /mnt/ directory, and then in this directory You can access the contents of the CD.


mount/umount

Preparations:
Next, let's do the actual operation. Since we want to mount a CD, we must first have this CD. You can use the image file when installing Centos, we first do some configuration on the virtual machine. Select which image file, and select the status as connected.
insert image description here
When we go back to the desktop, we will find that a disc icon pops up here, which is actually the same as in Windows. It defaults to a desktop environment for the entire system, and it will directly mount it for us. After loading, you can click directly to see the content inside.
insert image description here
But we don't want to access the CD-ROM files directly on the desktop, we still want to access them on the command line. Let's first check where the mount point of the current image file is, and lsblkyou can check it through.
insert image description here
Now we want to try the mount command manually, so eject it first.
insert image description here
It can be seen that it is not mounted. Basic syntax of
insert image description here
mount : (Function description: Mount device) Parameter function: As shown in the figure below We create a new cdrom folder under /mnt, and then prepare to mount the CD there. It is directly mentioned that the media cannot be found here, because we did the pop-up operation just now. After the CD/DVD is ejected, it is not connected again. If you choose Connected again, because we have a graphical interface here, it will automatically mount it for us. Solution: ① Log out first ② Then select Connected. ③Finally, log in again and use lsblk to see that it is not automatically mounted. So execute the mount command again (you can also replace cdrom with sr0), and you can see that the mount is successful. You can also see that the mount is successful by executing it again .
mount [-t vfstype] [-o options] device dir

insert image description here

insert image description here

insert image description here


insert image description here

insert image description here

insert image description here
mount /dev/cdrom /mnt/cdrom/
insert image description here
lsblk
insert image description here
We enter /mnt/cdrom/ and click ls to see the file content of the CD. Basic syntax of
insert image description here
umount : Example: Use to unmount sr0/cdrom. If we visit this directory again, we will find that this directory still exists, but there is nothing in it.
umount 设备文件名或挂载点
umount /mnt/cdrom
insert image description here
/mnt/cdrom
insert image description here

Set up automount

Background: Every time I boot up and come in, I hope that it can recognize the disc above by default, and mount it directly, just like a partition I have divided in advance.
Automatic mount: need to modify /etc/fstabthe configuration file.
When we do automatic mount configuration, we need to change a configuration file called fstab (file system tab). Let's go in and see what's inside.
insert image description here
It can be seen that it actually contains the mount information of all devices
①The mount point, UUID, and file type of the three partitions.
② There are two 0s at the end, the meanings are as follows:
----The first 0 means the dump option. When we were installing the system, there was a check box called kdump, and we removed it directly. The main purpose of kdump is to make regular backups for the entire system. If a failure occurs, it can be rolled back to the previous state directly, so this flag indicates whether to make backups. If 1 is given here, it means that the backup operation is performed regularly every day, and 0 means no backup.
---- The second 0 indicates the priority of the current file system check. There is a command called fsckfile system check, which is used to check and repair the current file system. When booting, the system will use this command to check the file system by default. When checking, it will determine the order of checking the file system according to the priority set here. If it is set to 1 here, the priority is the highest, and the root directory will generally be set to 1. If it is set to 2, it is to wait for the full inspection of 1 and then check 2. We now set it to 0 to indicate no check, and do not use fsck to check the file system when booting.
Add information: We don't need to write UUID, just write the device name directly. Then follow the mount point, file type, default options, two 0's.
insert image description here


Five, disk partition

background

Now we know:
①Check the usage of the current hard disk
②Check the mounting status of the current block device
③Think about mounting a newly added device
: What should I do if I mount the hard disk for the previously mounted CD? This is not just a matter of mounting. We first partition it, then format it according to different file system types, and finally set the mount point. For hard disk partition formatting operations, there is a special command, namely fdisk.

fdisk -l

fdisk -l: Just check the partition details of the current disk, and the information you see will be lsblkmore detailed than the partition information you see.
Example: View the partition details of the current disk.
insert image description here
It can be seen that:
① Disk name: /dev/sda
② Disk size: 64.4GB
③ Specific number of bytes, number of sectors, logical sector size, physical sector size, IO size, corresponding label type, symbol
④ Information of each partition:
----Device: partition name, here are sda1, sda2, sda3
----Boot: indicates whether it is the current boot partition (boot partition). If there is an asterisk here, it means that this is the boot partition. Here it is obvious that the mount point of sda1 is boot.
----Start and End: The location where the partition starts and ends. At the beginning of our configuration, we always start from 2048, so the current size is a little bit vacant.
----Blocks: the capacity of the current partition
----ID: the ID of the current partition type
----System: the type of the current system partition. The two partitions sda1 and sda3 are the standard partitions of the system, and their names are called Linux. The second swap partition is Linux swap or Salaries.

fdisk

Some configurations:
Currently we only have one hard disk, and we need to add another one.
insert image description here
The default disk type is SCSI, create a new virtual disk, the disk size is 20GB, and the default name.
insert image description here
Then it can be seen that there is a new hard disk
insert image description here
to check:
we fdisk -lcan see that there is still only the original sda hard disk.
insert image description here
Then input lsblkcan also see that there is only the hard disk sda.
insert image description here
This is because hard disks are not like CDs that can be hot-started and hot-swapped. We have to reboot now, so we reboot directly. Execute now lsblk, you can see that there is sdb.
insert image description here
Execution fdisk -lcan see that there is one more sdb, and its type is also a hard disk. There is no partition under it, and there is no mount point, so the next thing we need to do is to process sdb.
insert image description here

Hard disk partition

Basic syntax:fdisk 硬盘名
we execute here fdisk /dev/sdb, and see that there is a very clear Chinese prompt here.
insert image description here
We enter M to get help.
insert image description here
The main command operations we can use are as follows:
n: Add a new partition.
q: Exit without saving any changes.
w: Save and exit (write the information of the partition table into the hard disk, and then exit directly).
p: Print the information of the current partition table, which is the same as the information seen by fdisk -l outside, except that what we see is only the information of the disk sdb itself, we
enter nthe partition, and then we need to select the type of partition.
insert image description here
p:p refers to the primary, the primary partition. For Linux, there are up to 4 primary partitions in one disk. e: If you want to divide more partitions, you can replace a primary partition here with an extended partition. After replacing it with an extended partition, several logical partitions can be expanded. Strictly speaking, a logical partition is not a real partition. The maximum number of logical partitions can be divided into 12. The first four primary partitions are 1 2 3 4, and the subsequent extended partition numbers are 5 to 16.
We enter p here, or just press Enter. Then enter the partition number, we can directly enter 1.
insert image description here
Next, select the default option, set the starting sector to 2048, and the partition size to 20G,
insert image description here
then enter p to view.
insert image description here
Next, press w to directly save and exit, and start to synchronize the disk.
insert image description here
Then we enter lsblk -fto view, and we can find that the partition information of sdb is already there. But there is no file system and no UUID here, so the system has no way to recognize it. Because we haven't formatted it, we don't know what file system to use, of course, no UID will be assigned, and there is no corresponding mount point.
insert image description here

format

Basic syntax:mkfs -t 文件系统类型 设备名 (make file system)
we input mkfs -t xfs /dev/sdb1, the result is as follows.
insert image description here
Input lsblk -f, you can see that the type here is also there, and the UUID behind it is also there. Now it is the last step to mount.
insert image description here

mount

Thinking: Where shall we mount sdb1 next? Before we had a hard disk called sda, which had 3 partitions. The first partition is mounted under the boot, the second partition is swap, and the third partition is directly mounted under the root directory. Except for the content under boot, all other content is stored in the third partition of the hard disk sda, so there are too many things stored in it. Are there home directories of various ordinary users under our /home/ directory, such as atguigu? We hope to put all the things under atguigu on a new hard disk, that is, the new hard disk we added sdb,
mount
the basic syntax: mount 设备名 挂载点
we enter mount /dev/sdb1 /home/atguigu/, you can see that sdb1 is mounted under atguigu.
insert image description here
Test:
Use df -hto check the space occupied by the current hard disk. Some initial configuration information has been written in the current sdb disk, which occupies 33MB.
insert image description here
We now copy a large file (xshell installation package) to /home/atguigu, and we can see that the used size of sdb1 has become larger.
insert image description here

uninstall

We use to umount /dev/sdb1uninstall sdb1, and then use it df -hto see that the uninstallation is successful without any sdb information.
insert image description here
You lsblkcan still recognize this hard disk, but it has been uninstalled, and the corresponding files cannot be accessed.
insert image description here
The /athhuigu directory is also divided into the sda3 partition. Let's check the content under atguigu, and we can see that there is no installation package, only these two configuration files.
insert image description here

Guess you like

Origin blog.csdn.net/InnerPeaceHQ/article/details/126125264