Maintaining basic storage space in Linux

Table of contents

Maintain basic storage space

1. View disk information (block device) information

2. Create partitions

(1)MBR partition

        The standard MBR structure is as follows

        Why MBR can only have up to 4 primary partitions

(2)GPT partition

        advantage

3.Partition Tool

1. Use fdisk to manage MBR partitions

        Syntax format

        Parameters and functions

2. Use gdisk to manage GPT partitions

        Steps

3. Use parted to divide partitions

        Usage

        Partition

        interactive

        No interactive mode

4.Formatting

Purpose

Syntax format

Parameters and functions

5.Mount

Purpose

Syntax format

Unmount partition

Set automatic mounting at system startup

Permanent mounting method

The meaning of each field in the specified filling format for mounting information

6.Manage swap partition

How to extend swap partition

Commands and functions


Maintain basic storage space

1. View disk information (block device) information

root@ubuntu:~# lsblk

2. Create partitions

(1)MBR partition

        MBR (Master Boot Record ) is a traditional partitioning mechanism . For PC devices booted using BIOS, the addressing space is only 32 bits long and supports a maximum of 2.19TB.

The standard MBR structure is as follows

address describe Length (bytes)
0 code area 440 (maximum 446)
440 Select disk flag 4
444 Generally a null value; 0x0000 2
446 Standard MBR partition table planning (four 16-byte primary partition table entries) 64
511 MBR valid flag: 0x55AA 2

Why MBR can only have up to 4 primary partitions

        The partition table occupies 64 bytes and can describe the information of four partitions, of which each partition occupies 16 bytes.

MBR partition type

1. Primary partition

        A hard disk can have up to 4 primary partitions, and the primary partition cannot be divided into secondary partitions . It can be used to boot and start the operating system. At the same time, the primary partition can directly create a file system , such as Windows NTFS , to store data.

2. Extend the partition

        A hard disk can have at most one , plus a maximum of 4 primary partitions . File systems cannot be created , but logical partitions can be divided.

3. Logical partition

        File systems can be created to store data . At the same time, there is no limit to the number of logical partitions.

Classification from the perspective of booting the operating system

        System boot partition --- used to start the operating system , must be the primary partition

        Boot partition ---  the partition where the operating system is installed . It can be a primary partition or a logical partition.

        Active partition ---can only act on the system partition , there is only one active partition

(2)GPT partition

GPT is a more advanced and flexible disk partitioning mode         than MBR partitioning

advantage

  1. By default, GPT supports up to 128 partitions
  2. Supports total capacity greater than 2.2TB and partitions greater than 2.2TB , up to 18EB (1 EB=1024PB, 1PB=1024TB, 1TB=1024GB)
  3. GPT partition comes with backup
  4. Backwards compatible with MBR , protective MBR partition included on GPT partition table

3.Partition Tool

  • fdisk can only be used for MBR partitions , gdisk, parted can be used for GPT partitions
  • Most fdisk operation and maintenance staff are already accustomed to this interaction mode
  • The parted command is more convenient to use when creating and deleting partitions . It can divide partitions larger than 2TB , but its function is not perfect and there is no backup and restore command.
  • The gdisk command on the partition has the same style as fdisk . It is easy to use, easy to learn and powerful. It is recommended to use

1. Use fdisk to manage MBR partitions

Syntax format

        fdisk device name

Parameters and functions

instruction effect instruction effect
a Adjust the boot partition of the disk p Display the partition information of the current disk
d Delete disk partition t Change partition type
l Show all supported partition types u Toggle displayed partition size units
m View help information for all commands n Create new partition
q Exit the fdisk command without saving changes w Write the modifications to the disk partition table, and then exit the fdisk command
g Create a new empty GPT partition table o Create a new empty DOS partition table

2. Use gdisk to manage GPT partitions

        The gdisk command tool divides the disk into GPT format partitions by default.

Steps

  • Step 1 --- Add a new disk and view the new disk name
[root@localhost ~]# lsblk 

  • Step 2 --- Execute the gdisk command → enter the n command to create a new partition
[root@localhost ~]# gdisk /dev/nvme0n2 

  • Step 3 --- Specify the partition number (the default is the smallest unused partition number)

  • Step 4 --- Specify where the new partition starts and ends

  • Step 5 --- Display the created partition information

  • Step 6 --- Enter the w subcommand to write the partition settings to the partition table → When prompted for final confirmation, enter "y"

  • Step 7 --- Execute the partprobe command to let the operating system kernel learn the new partition table information

3. Use parted to divide partitions

Usage

interactive

        Manual sequential interactive creation

non-interactive

        You can write the command line in a script and run the script to achieve one-click creation ; suitable for remote batch management of multiple hosts.

 

The usage and principles         of the two methods are actually the same ; to achieve non-interactive creation, just add parted DEVICES_NAME before each specific interactive command .
[root@localhost ~]# parted /dev/nvme0n2

#新建磁盘标签类型为GPT

(parted) mklabel gpt

Partition

interactive

Syntax format

        mkpart PART-TYPE [FS-TYPE] START END

PART-TYPE ( partition type )
        primary --- primary partition
        logical --- logical partition
        extended --- extended partition
FS-TYPE ( file system type )
        ext4
        ext3
        ext2
        xfs
        other......
START
Set the starting point of the disk partition ; it can be 0 , numberMiB/GiB/TiB
        0 --- Set the starting point of the current partition to the first sector of the disk
        1G --- Set the starting point of the current partition to start at 1G of the disk
END
Set disk partition end point
        -1 --- Set the end point of the current partition to the last sector of the disk
        10G --- Set the end point of the current partition to 10G of the disk
mkpart primary xfs 1MB 500MB

No interactive mode

删除第1个分区
[root@localhost ~]# parted /dev/nvme0n2 rm 1

创建分区
[root@localhost ~]# parted /dev/nvme0n2 mklabel gpt

[root@localhost ~]# parted /dev/nvme0n2 mkpart primary xfs 1MB 800MB 

创建扩展分区

parted /dev/nvme0n2 mklabel msdos

parted /dev/nvme0n2 mkpart extended 800M 1.5G

创建逻辑分区

parted /dev/nvme0n2 mkpart logical xfs 900M 1G

4.Formatting

Purpose

        is to form a file system

Syntax format

        mkfs|mkfs.xfs|mkfs.ext4 [option ] Device name of the partition

Parameters and functions

        -t file system type ---  when the command name is mkfs , specify the type of file system to be created ( such as : xfs , ext4 , vfat , etc. ) . This option is not required when the command name is mkfs.xfs , mkfs.ext4, etc.
        -c --- Check for bad blocks before creating the file system
        -V --- Output detailed information about creating the file system
Notice:
        If other file systems have been created in this partition, you must add the option -f to the mkfs.xfs command to force formatting.

5.Mount

Purpose

        Mount partition ( or file system )

Syntax format

        mount [-t file system type ] device name mount point directory

Unmount partition

        To remove USB disks, U disks, CD-ROMs and hard disks , you need to uninstall them first
umount 挂载点目录或存储设备名

Set automatic mounting at system startup

        Manually mounted partitions will become invalid after the system restarts . If the user needs to permanently mount the partition , he/she needs to edit the /etc/fstab file to achieve this.
        When the system starts, the system will automatically read information from this file and automatically mount the file system specified in this file to the specified directory.

Permanent mounting method

  1. Mount by partition file name
  2. Mount via UUID

Notice:
        Each formatted partition has a unique UUID , so when you mount it, you can specify the UUID to mount it.
View UUID
blkid

[root@localhost ~]# blkid /dev/nvme0n1p3

 Use -U to mount

[root@localhost ~]# mount -U 75b1d1ec-3d6b-4840-ae31-44ca09f3314e /mnt/hgfs/

The meaning of each field in the specified filling format for mounting information

Field
significance
device file
Generally it is the path of the device + device name , you can also write a unique identification code ( UUID , Universally Unique Identifier)
Mount directory
Specify the directory to be mounted, which needs to be created before mounting.
format type
Specify the format of the file system , such as Ext3 , Ext4 , XFS , SWAP , iso9660 (this is a CD device), etc.
Permission options
If set to defaults , the default permissions are: rw, suid, dev, exec, auto, nouser, async
Backup or not
If it is 1 , use dump to perform disk backup after booting ; if it is 0, there will be no backup.
Whether to self-test
If it is 1 , it will automatically perform disk self-test after booting , if it is 0, it will not self-test.

6.Manage swap partition

        In the Linux system , the role of the swap partition is similar to the "virtual memory" in the Windows system . When a program is transferred into the memory , but the program is not often used by the CPU , then these infrequently used programs It will be placed in the swap partition of the hard disk , and the faster memory space will be released for the programs that really need it to avoid the problem of low system performance caused by insufficient physical memory . If the system does not have a swap partition , or the capacity of the existing swap partition is not enough , you can expand the swap partition.

How to extend swap partition

  1. Expand swap partition by disk partition
  2. Expand the swap partition as an image file

Commands and functions

Order Function
mkswap partition device name
Format the specified partition as swap file system
swapon swap partition device name |-a
Enable ( or activate ) the specified swap partition or all swap partitions
swapoff swap partition device name |-a
Disable specified swap partition or all swap partitions
swapon -s
Check the usage of swap partition
free -m
Displays physical memory, swap partition usage in megabytes

Guess you like

Origin blog.csdn.net/qq_57289939/article/details/132377898