Table of contents
1. View disk information (block device) information
The standard MBR structure is as follows
Why MBR can only have up to 4 primary partitions
1. Use fdisk to manage MBR partitions
2. Use gdisk to manage GPT partitions
3. Use parted to divide partitions
Set automatic mounting at system startup
The meaning of each field in the specified filling format for mounting information
Maintain basic storage space
1. View disk information (block device) information
root@ubuntu:~# lsblk
2. Create partitions
(1)MBR partition
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
- By default, GPT supports up to 128 partitions
- Supports total capacity greater than 2.2TB and partitions greater than 2.2TB , up to 18EB (1 EB=1024PB, 1PB=1024TB, 1TB=1024GB)
- GPT partition comes with backup
- 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
non-interactive
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
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
Syntax format
mkfs|mkfs.xfs|mkfs.ext4 [option ] Device name of the partition
Parameters and functions
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
Syntax format
mount [-t file system type ] device name mount point directory
Unmount partition
umount 挂载点目录或存储设备名
Set automatic mounting at system startup
Permanent mounting method
- Mount by partition file name
- Mount via UUID
Notice:Each formatted partition has a unique UUID , so when you mount it, you can specify the UUID to mount it.
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
How to extend swap partition
- Expand swap partition by disk partition
- 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
|