Seven storage structure and disk partitioning, the role of primary partition swap partition!

File storage structure (FHS standard)

In the Linux system, directories, character devices, block devices, sockets, printers, etc. are all abstracted into files, that is, "everything in the Linux system is a file". Since what we usually deal with are files, how should we find them? In the Windows operating system, if we want to find a file, we have to enter the disk partition where the file is located (assuming it is D drive here), and then enter the specific directory under the partition, and finally find the file. However, there are no drive letters such as C/D/E/F in the Linux system. All files in the Linux system start from the "root (/)" directory and adopt the tree in accordance with the File System Hierarchy Standard (FHS) Shape structure to store files, and defines the purpose of common directories. In addition, file and directory names in Linux systems are strictly case sensitive. For example, root, rOOt, Root, and rooT all represent different directories, and the file name must not contain a slash (/). The file storage structure in the Linux system is shown in the figure.


Insert picture description here

Tip: FHS defines what type of files the user should store in and where, but the user does not have to comply with it. This is just a definition, not a gun.

Common directory names and corresponding contents in Linux systems

Insert picture description here

Physical device naming rules (udev)

Everything is a file in a Linux system, and hardware devices are no exception. Since it is a file, it must have a file name. The udev device manager in the system kernel will automatically standardize the hardware name. The purpose is to allow users to guess the general attributes and partition information of the device through the name of the device file; this is especially convenient for unfamiliar devices. In addition, the service of the udev device manager will always run as a daemon and listen to the signals sent by the kernel to manage the device files in the /dev directory. The file names of common hardware devices in Linux systems are shown in the table.
Insert picture description here
Since IDE devices are rare now, general hard disk devices will start with "/dev/sd". And a host can have multiple hard disks, so the system uses a ~ p to represent 16 different hard disks (the default allocation starts from a, if there are more than 26 hard disks, then the first 26 are named sd[az], the 27th- 52 are named sd[aa-az], 53-78 are named sd[ba-bz], and so on) and the hard disk partition number is also very particular:

  1. The number of the primary partition or the extended partition starts from 1 and ends at 4;
  2. Logical partitions start with number 5.
  3. The name of the storage device is named sequentially starting from a, which is determined by the recognition sequence of the system kernel, and has nothing to do with the slot number of the device.
  4. The partition number does not represent the number of partitions, and the number of partitions is not necessarily postponed, or it may be manually specified.

-[] Hard Disk Naming Rules

Insert picture description here
It is precisely because the computer has a hard disk device that we can save at any time during the game playing or after the game is cleared, instead of starting over every time. The hard disk device is composed of a large number of sectors, and the capacity of each sector is 512 bytes. Among them, the first sector is the most important, it holds the master boot record and partition table information. As far as the first sector is concerned, the master boot record needs to occupy 446 bytes, the partition table is 64 bytes, and the terminator occupies 2 bytes; among them, 16 bytes are required for each partition information recorded in the partition table. Only 4 partition information can be written to the first sector at most, and these 4 partitions are 4 primary partitions. The data information in the first sector is shown in the figure.
Insert picture description here

File system and data

Common file system

**Ext3:** is a log file system that can avoid file system data loss when the system goes down abnormally, and can automatically repair data inconsistencies and errors. However, when the hard disk capacity is large, the required repair time will be long, and there is no guarantee that data will not be lost. It will record the details of each write action of the entire disk in advance, so that it can trace back to the interrupted part after an abnormal downtime, and then try to repair it.

**Ext4:** An improved version of Ext3, as the default file management system in the RHEL 6 system, it supports a storage capacity of up to 1EB (1EB=1,073,741,824GB) and can have an unlimited number of subdirectories. In addition, the Ext4 file system can allocate block blocks in batches, which greatly improves read and write efficiency.

**XFS:** is a high-performance log file system, and it is the default file management system in RHEL 7. Its advantages are especially obvious after an unexpected downtime, that is, it can quickly restore files that may be damaged. Moreover, the powerful log function only uses extremely low-cost computing and storage performance. And its maximum supported storage capacity is 18EB, which meets almost all needs.

1. Inode stores file permissions and attribute records. Each file occupies an independent inode table. The size of the table is 128 bytes by default, and the following information is recorded in it:

该文件的访问权限(read、write、execute);
该文件的所有者与所属组(owner、group);
该文件的大小(size);
该文件的创建或内容修改时间(ctime);
该文件的最后一次访问时间(atime);
该文件的修改时间(mtime);
文件的特殊权限(SUID、SGID、SBIT);
该文件的真实数据地址(point)。

2. The block stores the actual content of the file (the size can be 1KB, 2KB, 4KB).

文件很小(1KB),但依然会占用一个block,因此会潜在地浪费3KB。
文件很大(5KB),那么会占用两个block(5KB-4KB后剩下的1KB也要占用一个block)。

VFS virtual file system:

Various file systems hide their characteristics and details under the VFS. When operating files, users operate on the VFS uniformly, and users do not need to care about the underlying hard disk structure.
Insert picture description here

Mount the hardware device

1. Partition

fdisk command: used to manage disk partitions. Format: fdisk [disk name]. It provides a "one-stop partition service" that integrates the functions of adding, deleting, and converting partitions. This is an interactive command.

参数	作用
m	查看全部可用的参数
n	添加新的分区
d	删除某个分区信息
l	列出所有可用的分区类型
t	改变某个分区的类型
p	查看分区表信息
w	保存并退出
q	不保存直接退出

Add a new partition:

[root@lizhiqiang Desktop]# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0xc60ca334.

Command (m for help): p

Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0xc60ca334

   Device Boot      Start         End      Blocks   Id  System

Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-41943039, default 2048): 
Using default value 2048
Last sector, +sectors or +size{
    
    K,M,G} (2048-41943039, default 41943039): +2G
Partition 1 of type Linux and of size 2 GiB is set

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

View the new partition:

[root@lizhiqiang Desktop]# df -h
Filesystem                        Size  Used Avail Use% Mounted on
/dev/mapper/rhel_lizhiqiang-root   18G  3.0G   15G  17% /
devtmpfs                          985M     0  985M   0% /dev
tmpfs                             994M   84K  994M   1% /dev/shm
tmpfs                             994M  8.9M  986M   1% /run
tmpfs                             994M     0  994M   0% /sys/fs/cgroup
/dev/sda1                         497M  119M  379M  24% /boot

Format the new partition:

[root@lizhiqiang Desktop]# mkfs.xfs /dev/sdb
mkfs.xfs: /dev/sdb appears to contain a partition table (dos).
mkfs.xfs: Use the -f option to force overwrite.
[root@lizhiqiang Desktop]# mkfs.xfs /dev/sdb1
meta-data=/dev/sdb1              isize=256    agcount=4, agsize=131072 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=0
data     =                       bsize=4096   blocks=524288, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=0
log      =internal log           bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0

Mount the new partition:

[root@lizhiqiang /]# mkdir opo
[root@lizhiqiang /]# mount /dev/sdb1 /opo

Add to system startup items:

vim /etc/fstab 

Insert picture description here
Restart and save! The new partition was created successfully!

2. Format

mkfs command: used for formatting operations. Format: mkfs. File type disk device file.

3. Mount and unmount

①The mounting operation is the action of associating the hardware device with the directory.
②The uninstalling operation is the action of canceling the association between the hardware device and the directory. Unmounting the file system means that the hardware device resources are no longer used.

Mount device directory

-a:挂载所有在/etc/fstab中定义的文件系统
-t:指定文件系统的类型

Unmount umount device or directory

Use the mount command to mount, it will become invalid after the system restarts. It is necessary to append mount information in the /etc/fstab file to mount it permanently. In the /etc/fstab file, the format of the mount information: "Does the device file mount directory format type permission option backup or self-check".

The meaning of each field in the mount information format:

字段	   意义
设备文件	   一般为设备的路径+设备名称,也可以写唯一识别码(UUID,Universally Unique Identifier)
挂载目录	   指定要挂载到的目录,需在挂载前创建好
格式类型	   指定文件系统的格式,比如Ext3、Ext4、XFS、SWAP、iso9660(此为光盘设备)等
权限选项	   若设置为defaults,则默认权限为:rw, suid, dev, exec, auto, nouser, async
是否备份	   若为1则开机后使用dump进行磁盘备份,为0则不备份
是否自检	   若为1则开机后自动进行磁盘自检,为0则不自检

df command: used to view the mounting status and hard disk usage information. The commonly used parameter is -h. Format: df -h.
du command: used to view the file data usage. Format: du [option] [file].

du -sh /*        #查看在Linux系统根目录下所有一级目录分别占用的空间大小
du -sh /newFS    #查看/newFS下的内容占了多少容量

Insert picture description here

Add swap partition

SWAP (swap) partition is a technology that divides a certain amount of space in the hard disk in advance, and then temporarily stores the temporarily unused data in the memory to the hard disk to free up physical memory space for more active program services. , Its design purpose is to solve the problem of insufficient real physical memory. However, since the swap partition reads and writes data through the hard disk device after all, the speed is definitely slower than the physical memory, so the swap partition resources will only be called when the real physical memory is exhausted.

The process of creating a swap partition is very similar to the process of mounting and using storage devices mentioned earlier. Before partitioning the /dev/sdb storage device, it is necessary to talk about the division of the swap partition: in a production environment, the size of the swap partition is generally 1.5 to 2 times the real physical memory.

Process of adding swap partition

1. Create a new hard disk for
Insert picture description here
the virtual machine first. After adding the virtual machine, restart the virtual machine and add a swap partition!

[root@lizhiqiang /]# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): p

Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0xc60ca334

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     4196351     2097152   83  Linux

Command (m for help): n
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): p
Partition number (2-4, default 2): 2
First sector (4196352-41943039, default 4196352): 
Using default value 4196352
Last sector, +sectors or +size{
    
    K,M,G} (4196352-41943039, default 41943039): +6G
Partition 2 of type Linux and of size 6 GiB is set

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.

Check memory after success

[root@lizhiqiang /]# free -h
             total       used       free     shared    buffers     cached
Mem:          1.9G       1.1G       828M       9.8M       1.6M       257M
-/+ buffers/cache:       900M       1.1G
Swap:         2.0G         0B       2.0G

Format the new swap partition and find that it cannot be formatted. At this time, run partprobe twice to format:

[root@lizhiqiang /]# mkswap /dev/sdb2
/dev/sdb2: No such file or directory
[root@lizhiqiang /]# partprobe
[root@lizhiqiang /]# partprobe
[root@lizhiqiang /]# mkswap /dev/sdb2
Setting up swapspace version 1, size = 6291452 KiB
no label, UUID=474d933b-b93d-41b9-82dd-7261dc676ccf

Mount the new swap partition:

[root@lizhiqiang /]# swapon /dev/sdb2
[root@lizhiqiang /]# free -h
             total       used       free     shared    buffers     cached
Mem:          1.9G       1.1G       821M       9.8M       1.6M       257M
-/+ buffers/cache:       906M       1.1G
Swap:         8.0G         0B       8.0G

Add the swap partition to the system startup item:

vim /etc/fstab 进入设置开机启动项

Insert picture description here
Setup is complete, restart the virtual machine free -h partition is complete!

shutdown -h now Shut down the virtual machine!

shutdown -t -s 0 Shut down the machine!

Guess you like

Origin blog.csdn.net/SYH885/article/details/109130671