tencent cloud

TencentOS Server

Disk and Partition Management

Baixar
Modo Foco
Tamanho da Fonte
Última atualização: 2026-10-19 17:22:50
Traduzido por IA
This document describes how to manage disks and partitions in TencentOS Server, including disk type descriptions, partition table creation and maintenance, and procedures for managing partitions and swap space using tools such as parted and fdisk.

Disk type

In TencentOS Server, all disks appear as block device files in the /dev directory. The naming of these block device files varies depending on the disk interface type, as shown below:
Disk Interface Type
Disk Block Device File Name
Description
Example
SCSI/SATA/SAS
/dev/sdX
X represents letters a-z, corresponding to storage devices in sequence. When the number of disks of this type exceeds 26, two letters are used, such as /dev/sdaa.
/dev/sda
virtio-blk
/dev/vdX
X represents letters a-z, corresponding to storage devices in sequence.
/dev/vda
NVMe
/dev/nvmeXnY
X represents the NVMe controller number (starting from 0), and Y represents the NVMe namespace number (starting from 1). A namespace typically corresponds to a storage device.
/dev/nvme0n1
IDE
/dev/hdX
X represents letters a-z, corresponding to storage devices in sequence.
/dev/hda

Disk Partition

Before using a disk, you typically divide it into one or more logical areas, known as disk partitions. The created disk partitions appear as block device files in the /dev directory, as shown below:
Disk Block Device File Name
Disk Partition Block Device File Name
Description
Example
/dev/sdX
/dev/sdXN
N indicates the partition number (starting from 1).
/dev/sda1
/dev/vdX
/dev/vdXN
N indicates the partition number (starting from 1).
/dev/vda1
/dev/nvmeXnY
/dev/nvmeXnYpN
N indicates the partition number (starting from 1).
/dev/nvme0n1p1
/dev/hdX
/dev/hdXN
N indicates the partition number (starting from 1).
/dev/hda1

Disk Partition Table

You must create a disk partition table before partitioning a disk. The partition table stores disk partition information. TencentOS Server primarily uses two types of partition tables: MSDOS (MBR) and GPT (GUID Partition Table).
Partition Table
Maximum Number of Partitions
Maximum Partition Size
MSDOS(MBR)
4 primary partitions or 3 primary partitions and 1 extended partition (which can be divided into multiple logical partitions)
2 TiB (sector size: 512 B) 16 TiB (sector size: 4 KB)
GPT
128
8 ZiB (sector size: 512 B) 64 ZiB (sector size: 4 KB)

MSDOS Partition Table

The MSDOS partition table resides in the first sector of the disk, which is typically 512B in size, and mainly contains the following data:
Master Boot Record (MBR): can hold a bootloader program and is 446B in size.
Partition Table: records disk partition information and is 64B in size. Each partition entry occupies 16B, and the table can contain up to four partition entries.
MBR signature: contains the value 55AA and is 2B in size.
Typically, the msdos partition table is used for legacy boot, as shown in the following example:
lsblk
# Expected output
# NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
# sda 8:0 0 50G 0 disk
# ├─sda1 8:1 0 1G 0 part /boot
# └─sda2 8:2 0 49G 0 part
# ├─os-root 253:0 0 30G 0 lvm /
# └─os-home 253:1 0 19G 0 lvm /home

parted /dev/sda print
# Expected output
# Model: ATA QEMU HARDDISK (scsi)
# Disk /dev/sda: 53.7GB
# Sector size (logical/physical): 512B/512B
# Partition Table: msdos
# Disk Flags:
#
# Number Start End Size Type File system Flags
# 1 1049kB 1075MB 1074MB primary ext4 boot
# 2 1075MB 53.7GB 52.6GB primary lvm
The system disk in this example uses the msdos partition table and contains two partitions: one boot partition and the other used as a physical volume for logical volumes.

GPT Partition Table

The GPT partition table uses LBAs to address the disk, with each LBA being 512B. The GPT partition structure is as follows:
LBA0: MBR compatibility block. The first 446B stores the bootloader (under UEFI boot, the bootloader is not stored here), and the last 66B is a special signature indicating that the disk uses GPT partitioning.
LBA1: GPT header, which records the location and size of the partition table itself, the location of the backup partition, and the CRC32 checksum.
LBA(2~33): partition table, which records partition information. Each LBA can record 4 partition entries.
LBA(-1): backup of the GPT header.
LBA(-33~-2): backup of the partition table.
For UEFI boot, use the GPT partition table, as shown in the following example:
lsblk
Output example:
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
sda 8:0 0 64G 0 disk
├─sda1 8:1 0 50M 0 part /boot/efi
├─sda2 8:2 0 512M 0 part /boot
└─sda3 8:3 0 63.4G 0 part
├─os-root 253:0 0 30G 0 lvm /
└─os-home 253:1 0 33.4G 0 lvm /home
parted /dev/sda print
Output example:
Model: ATA Fedora Linux-0 S (scsi)
Disk /dev/sda: 68.7GB
Sector size (logical/physical): 512B/4096B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
1 1049kB 53.5MB 52.4MB fat16 EFI System Partition boot, esp
2 53.5MB 590MB 537MB ext4
3 590MB 68.7GB 68.1GB lvm
The system disk in this example uses the GPT partition table and contains three partitions: one EFI partition, one boot partition, and the other used as a physical volume for logical volumes.

Disk and Partition Management Tools

Viewing Block Device Information with lsblk

lsblk can list block device information, as shown below:
lsblk
Output example:
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
sda 8:0 0 60G 0 disk
├─sda1 8:1 0 1M 0 part
├─sda2 8:2 0 1G 0 part /boot
└─sda3 8:3 0 59G 0 part
├─os-root 253:0 0 39.6G 0 lvm /
└─os-home 253:1 0 19.4G 0 lvm /home
sdb 8:16 0 30G 0 disk
├─sdb1 8:17 0 1G 0 part
├─sdb2 8:18 0 1G 0 part
├─sdb3 8:19 0 1K 0 part
├─sdb4 8:20 0 26G 0 part
└─sdb5 8:21 0 1G 0 part
sr0 11:0 1 13.8G 0 rom
The parameters in the returned result are described as follows:
NAME: disk or partition device name
MAJ:MIN: major and minor device numbers of a disk or partition
RM: whether the device is removable. 0 indicates non-removable, and 1 indicates removable.
SIZE: disk or partition capacity
RO: whether the device is read-only. 0 indicates read-write, and 1 indicates read-only.
TYPE: device type. disk indicates a disk, part indicates a partition, and lvm indicates a logical volume.
MOUNTPOINTS: mount point
Additionally, you can view the parent-child relationships between disk partitions from the lsblk output. For more command options, see the man page.

Viewing Block Device Information with blkid

blkid can print device attributes, as shown below:
blkid
Output example:
/dev/mapper/os-home: UUID="f30c7d4a-c97d-427e-ba4e-xxxxxxxxxxxx" BLOCK_SIZE="4096" TYPE="ext4"
/dev/sr0: BLOCK_SIZE="2048" UUID="2023-05-08-xx-xx-xx-xx" LABEL="TencentOS Server" TYPE="iso9660" PTTYPE="PMBR"
/dev/mapper/os-root: UUID="2985bd9c-facc-49eb-8752-xxxxxxxxxxxx" BLOCK_SIZE="4096" TYPE="ext4"
/dev/sda2: UUID="57139a63-0e14-410b-ae05-xxxxxxxxxxxx" BLOCK_SIZE="4096" TYPE="ext4" PARTUUID="846b8e84-0d7e-xxxx-xxx"
/dev/sda3: UUID="q6Vgxu-Wvpn-d3M9-c8Hk-dMT0-xxxx-xxxxx" TYPE="LVM2_member" PARTUUID="5d88c31f-ab83-46bd-xxxx-xxxxxx"
/dev/sdb4: PARTUUID="1144xxxx-04"
/dev/sdb2: PARTUUID="1144xxxx-02"
/dev/sdb5: PARTUUID="1144xxxx-05"
/dev/sdb1: PARTUUID="1144xxxx-01"
/dev/sda1: PARTUUID="603c9161-aa71-4196-bbb8-xxxxxxxxxxxx"
The part before the colon indicates the device name, and the part after the colon indicates the attribute information of the device or file system. For more command options, see the man page.

Using parted to Manage Disk Partitions

Overview of parted

The parted command supports MSDOS and GPT partition tables and can perform disk operations such as viewing partition information, creating partition tables, creating partitions, modifying partitions, and deleting partitions.
The parted command supports both interactive and command line modes. You can directly run parted <block-device> to enter the interactive interface, as shown below:
parted <block-device>
You can directly use subcommands in the interactive interface. Enter help to view all supported subcommands, as shown below:
help
Enter quit to exit the interactive interface.
quit
The following example uses the /dev/sdb disk. After entering the interactive interface, run help and then exit the interactive interface.
parted /dev/sdb
Output example:
GNU Parted 3.5
Using /dev/sdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) help
align-check TYPE N check partition N for TYPE(min|opt) alignment
help [COMMAND] print general help, or help on COMMAND
mklabel,mktable LABEL-TYPE create a new disklabel (partition table)
mkpart PART-TYPE [FS-TYPE] START END make a partition
name NUMBER NAME name partition NUMBER as NAME
print [devices|free|list,all] display the partition table, or available devices, or free space,
or all found partitions
quit exit program
rescue START END rescue a lost partition near START and END
resizepart NUMBER END resize partition NUMBER
rm NUMBER delete partition NUMBER
select DEVICE choose the device to edit
disk_set FLAG STATE change the FLAG on selected device
disk_toggle [FLAG] toggle the state of FLAG on selected device
set NUMBER FLAG STATE change the FLAG on partition NUMBER
toggle [NUMBER [FLAG]] toggle the state of FLAG on partition NUMBER
unit UNIT set the default unit to UNIT
version display the version number and copyright information of GNU Parted
(parted) quit
Note:
If <block-device> is not specified, the first disk is used by default, which is usually /dev/sda. You can confirm this from the command output.

Viewing Partitions with parted

You can use the print subcommand to view partition information.
Viewing Partitions in the parted Interactive Interface
1. Enter the interactive interface.
parted <block-device>
2. You can use the print subcommand to display partition information of the current disk or all disks.
print [devices|free|list,all]
The parameter of the print subcommand is optional. If no parameter is specified, the partition information of the current device is displayed by default. The parameters are described as follows:
devices: displays all active disks.
free: views the remaining capacity of the current disk.
list,all: views partition information of all disks.
The following example shows how to print partition information of the block device /dev/sda:
parted /dev/sda
Output example:
GNU Parted 3.5
Using /dev/sda
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) print
Model: ATA QEMU HARDDISK (scsi)
Disk /dev/sda: 64.4GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: pmbr_boot
Number Start End Size File system Name Flags
1 1049kB 2097kB 1049kB bios_grub
2 2097kB 1076MB 1074MB ext4
3 1076MB 64.4GB 63.3GB lvm
The parameters in the returned result are described as follows:
Model: ATA QEMU HARDDISK (scsi): disk type, manufacturer, model, and interface.
Disk /dev/sda: 64.4GB: the block device file and capacity corresponding to the disk.
Sector size (logical/physical): 512B/512B: sector size.
Partition Table: gpt: partition table type.
Disk Flags: pmbr_boot: a disk flag that appears only on disks with GPT partition tables, indicating that the disk has special attributes. In the example, pmbr_boot indicates that a protective MBR (PMBR) exists on the disk, supporting the BIOS boot method.
Number: partition number.
Start/End: the start and end positions of a partition.
Size: partition capacity.
File system: the type of file system on the partition.
Name: the partition name, which appears only on disks with GPT partition tables and can be repeated.
Flags: partition flags. In the example, bios_grub indicates a BIOS boot partition, which is required when the system disk uses a GPT partition table under BIOS boot.
Type: the partition type, which appears only on disks with MSDOS partition tables. Its values are primary, extended, and logical, corresponding to primary partitions, extended partitions, and logical partitions respectively.
The command to view partitions using the parted command line is as follows:
parted <block-device> print
The following example shows how to print partition information of the block device /dev/sda:
parted /dev/sda print
Output example:
Model: ATA QEMU HARDDISK (scsi)
Disk /dev/sda: 64.4GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: pmbr_boot
Number Start End Size File system Name Flags
1 1049kB 2097kB 1049kB bios_grub
2 2097kB 1076MB 1074MB ext4
3 1076MB 64.4GB 63.3GB lvm
Changing Display Units
The print command displays the location and capacity. By default, the unit is compact, with input in MB and output in a human-readable format such as kB, MB, or GB. This data is not precise enough and is based on decimal units. When more precise capacity data is required, you can use the unit subcommand to change the display unit.
The default unit for the capacity displayed by the print command is compat, where input is in MB and output is presented in a human-readable format (such as kB, MB, or GB). This unit is decimal-based and has limited precision. If you need more precise capacity data, use the unit subcommand to change the display unit.
unit <UNIT>
<UNIT> specifies the unit for displaying capacity. The following values are supported:
s indicates a sector.
kB, MB, GB, TB are based on the decimal system, where each unit is 1000 times larger than the previous one.
For compat, the default input unit is MB, and the output unit is in a human-readable format.
cyl: cylinder.
chs represents a disk location described as a triplet (cylinders, heads, sectors), corresponding to cylinders, heads, and sectors respectively. One cylinder contains 255 heads, and one head contains 2 sectors. The value ranges of the components are as follows: heads ranges from 0 to 254, and sectors ranges from 0 to 1.
When the unit is set to chs, the disk capacity is displayed as the triplet position of the last sector, where sectors is typically 1. The formula for calculating the total number of sectors on the disk is: total sectors = cylinders × 255 × 2 + heads × 2 + sectors + 1.
%: percentage.
kiB, MiB, GiB, TiB are based on the binary system, where each unit is 1024 times larger than the previous one.

Creating a Partition Table with parted

You can use the mklabel or mktable subcommand to create a partition table.
Note:
The mklabel command creates a new partition table on the disk, which erases all existing partitions and data on that disk, and the data cannot be recovered. Before proceeding, confirm that the target disk is correct and that you have backed up all data you want to keep.
Creating a Partition Table in the parted Interactive Interface
1. Enter the interactive interface.
parted <block-device>
2. Create a partition table. If a partition table already exists on the disk, the system will print an alarm message and require manual confirmation to continue.
mklabel LABEL-TYPE
3. Confirm whether the partition table was created successfully.
print
The following example shows how to create a gpt partition table on the block device /dev/sdb.
parted /dev/sdb
Output example:
GNU Parted 3.5
Using /dev/sdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) mklabel gpt
(parted) quit
Information: You may need to update /etc/fstab.
Creating a Partition Table with the parted Command Line
The command is as follows:
parted <block-device> mklabel LABEL-TYPE
The following example shows how to create a gpt partition table on the block device /dev/sdb.
parted /dev/sdb mklabel gpt
No output is returned when the command succeeds. If the partitions on the disk are already written to /etc/fstab, parted will prompt Information: You may need to update /etc/fstab.. Update /etc/fstab promptly as prompted.

Creating a Partition with parted

You can use the mkpart subcommand to create a partition.
Creating a Partition in the parted Interactive Interface
1. Enter the interactive interface.
parted <block-device>
2. Modify the display unit based on actual requirements.
unit <UNIT>
3. First, check the remaining space information to confirm whether it meets your requirements. If not, adjust the partitions.
print free
4. Create a partition.
mkpart PART-TYPE [FS-TYPE] START END
PART-TYPE: When the partition table is MSDOS, the available values are primary, extended, and logical, corresponding to primary partitions, extended partitions, and logical partitions respectively. A logical partition can be set only when an extended partition already exists. When the partition table is GPT, this option is set as the partition name, which can be any string.
FS-TYPE: Optional. Sets the file system for the partition. It only sets the metadata value of the partition file system field and does not actually create a file system.
START/END: the start and end positions of a partition. Supported units include s, B, kB, MB, GB, TB, compact, cyl, chs, %, kiB, MiB, GiB, and TiB. If no unit is specified, the default unit under the current UNIT is used.
5. After the partition is created, run the partprobe command. In some environments, the OS may not update in time after a partition is created, and the old partition information remains in memory. The partprobe command can notify the OS to rescan the disk partition table and load the new partition information.
partprobe <block-device>
The following example shows how to create a partition named test-part on /dev/sdb, with the start position at 1 MiB and the end position at 1 GiB.
parted /dev/sdb
Output example:
GNU Parted 3.5
Using /dev/sdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) unit kiB
(parted) print free
Model: ATA QEMU HARDDISK (scsi)
Disk /dev/sdb: 31457280kiB 9Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
17.0kiB 31457263kiB 31457247kiB Free Space
(parted) mkpart test-part 1MiB 1GiB
(parted) print free
Model: ATA QEMU HARDDISK (scsi)
Disk /dev/sdb: 31457280kiB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
17.0kiB 1024kiB 1007kiB Free Space
1 1024kiB 1048576kiB 1047552kiB test-part
1048576kiB 31457263kiB 30408688kiB Free Space
(parted) quit
partprobe /dev/sdb
Creating a Partition with the parted Command Line
The command is as follows:
parted <block-device> mkpart PART-TYPE [FS-TYPE] START END
Example:
parted /dev/sdb unit MiB print free
Output example:
Model: ATA QEMU HARDDISK (scsi)
Disk /dev/sdb: 30720MiB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
0.02MiB 1.00MiB 0.98MiB Free Space
1 1.00MiB 1024MiB 1023MiB test-part
1024MiB 30720MiB 29696MiB Free Space
parted /dev/sdb mkpart test-part2 1024MiB 2048MiB
Information: You may need to update /etc/fstab.
partprobe /dev/sdb
parted /dev/sdb unit MiB print free
Model: ATA QEMU HARDDISK (scsi)
Disk /dev/sdb: 30720MiB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
0.02MiB 1.00MiB 0.98MiB Free Space
1 1.00MiB 1024MiB 1023MiB test-part
2 1024MiB 2048MiB 1024MiB test-part2
2048MiB 30720MiB 28672MiB Free Space

Resizing a Partition with parted

Resizing a Partition in the parted Interactive Interface
1. Enter the interactive interface.
parted <block-device>
2. Modify the display unit based on actual requirements.
unit UNIT
3. View partition information.
print free
4. Resize the partition.
resizepart NUMBER END
NUMBER: number of the partition to be adjusted.
END: the end position of the adjusted partition. END cannot be smaller than the start size of the partition.
5. Rescan the partition table.
partprobe <block-device>
Example:
parted /dev/sdb
Output example:
GNU Parted 3.5
Using /dev/sdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) unit MiB
(parted) print free
Model: ATA QEMU HARDDISK (scsi)
Disk /dev/sdb: 30720MiB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
0.02MiB 1.00MiB 0.98MiB Free Space
1 1.00MiB 1024MiB 1023MiB test-part
2 1024MiB 2048MiB 1024MiB test-part2
2048MiB 30720MiB 28672MiB Free Space
(parted) resizepart 2 4096MiB
(parted) print free
Model: ATA QEMU HARDDISK (scsi)
Disk /dev/sdb: 30720MiB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
0.02MiB 1.00MiB 0.98MiB Free Space
1 1.00MiB 1024MiB 1023MiB test-part
2 1024MiB 4096MiB 3072MiB test-part2
4096MiB 30720MiB 26624MiB Free Space
(parted) quit
partprobe /dev/sdb
Resizing a Partition with the parted Command Line
The command is as follows:
parted <block-device> resizepart NUMBER END
Example:
parted /dev/sdb unit MiB print free
Output example:
Model: ATA QEMU HARDDISK (scsi)
Disk /dev/sdb: 30720MiB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
0.02MiB 1.00MiB 0.98MiB Free Space
1 1.00MiB 1024MiB 1023MiB test-part
2 1024MiB 2048MiB 1024MiB test-part2
2048MiB 30720MiB 28672MiB Free Space
parted resizepart 2 4096MiB
parted /dev/sdb resizepart 2 4096MiB
Information: You may need to update /etc/fstab.
partprobe /dev/sdb
parted /dev/sdb unit MiB print free
Model: ATA QEMU HARDDISK (scsi)
Disk /dev/sdb: 30720MiB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
0.02MiB 1.00MiB 0.98MiB Free Space
1 1.00MiB 1024MiB 1023MiB test-part
2 1024MiB 4096MiB 3072MiB test-part2
4096MiB 30720MiB 26624MiB Free Space
Note: Shrinking a partition may cause data loss or even file system corruption.

Deleting a Partition with parted

Deleting a Partition in the parted Interactive Interface
1. Enter the interactive interface.
parted <block-device>
2. View partition information.
print
3. Delete the partition.
rm NUMBER
4. Rescan the partition table.
partprobe <block-device>
Note:
If the deleted partition contains a file system and has been written to /etc/fstab, remove the related configuration from /etc/fstab. Otherwise, the OS may fail to start.
Example:
parted /dev/sdb
Output example:
GNU Parted 3.5
Using /dev/sdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) print
Model: ATA QEMU HARDDISK (scsi)
Disk /dev/sdb: 32.2GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
1 1049kB 1074MB 1073MB ext4 test-part
2 1074MB 2147MB 1074MB ext4 test-part2
(parted) rm 2
(parted) print
Model: ATA QEMU HARDDISK (scsi)
Disk /dev/sdb: 32.2GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
1 1049kB 1074MB 1073MB ext4 test-part
(parted) quit
Information: You may need to update /etc/fstab.
partprobe /dev/sdb
Deleting a Partition with the parted Command Line
The command is as follows:
parted <block-device> rm NUMBER
Example:
parted /dev/sdb print
Output example:
Model: ATA QEMU HARDDISK (scsi)
Disk /dev/sdb: 32.2GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
1 1049kB 1074MB 1073MB ext4 test-part
2 1074MB 2147MB 1074MB ext4 test-part2
parted /dev/sdb rm 2
Information: You may need to update /etc/fstab.
parted /dev/sdb print
Model: ATA QEMU HARDDISK (scsi)
Disk /dev/sdb: 32.2GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
1 1049kB 1074MB 1073MB ext4 test-part
For more command options, see the man page.

Managing Disk Partitions with fdisk/gdisk

fdisk Overview

fdisk supports both MSDOS and GPT partition tables and can perform disk operations such as viewing partition information, creating partition tables, creating partitions, modifying partitions, and deleting partitions.
Run fdisk <block-device> to enter the interactive interface. Most fdisk operations need to be completed in the interactive interface, as shown below:
fdisk <block-device>
Output example:
Command (m for help):
Use m to view help information:
Command (m for help): m
Use q to exit the interactive interface:
Command (m for help): q

Viewing Partitions with fdisk

You can use the p command to view partitions.
1. Enter the interactive interface.
fdisk <block-device>
2. View partitions.
Command (m for help): p
The following example views the partitions of disk /dev/sda.
fdisk /dev/sda
Output example:
Welcome to fdisk (util-linux 2.37.4).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
This disk is currently in use - repartitioning is probably a bad idea.
It's recommended to umount all file systems, and swapoff all swap
partitions on this disk.
Command (m for help): print
Disk /dev/sda: 60 GiB, 64424509440 bytes, 125829120 sectors
Disk model: QEMU HARDDISK
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: gpt
Disk identifier: 3C81803B-8934-47FF-BE0B-37AE73EC68C0
Device Start End Sectors Size Type
/dev/sda1 2048 4095 2048 1M BIOS boot
/dev/sda2 4096 2101247 2097152 1G Linux filesystem
/dev/sda3 2101248 125827071 123725824 59G Linux LVM
Disk /dev/sda: the disk capacity, displayed in GiB, bytes, and sectors respectively.
Disk model: the disk model.
Units: the display unit.
Sector size: sector size.
Disklabel type: the partition table type. In fdisk, an MSDOS partition table is displayed as dos.
Disk identifier: the disk identifier.
Device: the partition block device file name.
Start/End: the start and end sectors.
Sectors: the total number of sectors in the partition.
Size: partition size.
Type: the partition type, corresponding to the partition flag in parted.
You can also use the fdisk command line to view partitions. The command is as follows:
fdisk -l [<block-device>]
If <block-device> is not specified, all disk partition information is displayed.

Creating a Partition Table with fdisk

You can use the g subcommand to create a GPT partition table, and the o subcommand to create an MSDOS partition table.
1. Enter the interactive interface.
fdisk <block-device>
2. Create a partition table.
Command (m for help): g
3. Save the changes and exit the interactive interface.
Command (m for help): w
The following example creates a gpt partition table on the block device /dev/sdb.
fdisk /dev/sdb
Output example:
Welcome to fdisk (util-linux 2.37.4).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Command (m for help): g
Created a new GPT disklabel (GUID: AC247B3E-7137-6B42-8F85-7D570A41D7FE).
The device contains 'dos' signature and it will be removed by a write command. See fdisk(8) man page and --wipe option for more details.
Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

Creating a Partition with fdisk

You can use the n subcommand to create a partition.
1. Enter the interactive interface.
fdisk <block-device>
2. Use the n subcommand, and then follow the prompts to create a partition.
Command (m for help): n
3. Save the changes and exit the interactive interface.
Command (m for help): w
4. Rescan the partition table.
partprobe <block-device>
Example 1: Create a partition on a disk with an MSDOS partition table.
The following example creates a primary partition, an extended partition, and a logical partition on /dev/sdb.
fdisk /dev/sdb
Output example:
Welcome to fdisk (util-linux 2.37.4).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Command (m for help): n
Partition type
p primary (0 primary, 0 extended, 4 free)
e extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1):
First sector (2048-62914559, default 2048):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-62914559, default 62914559): +1G
Created a new partition 1 of type 'Linux' and of size 1 GiB.
Command (m for help): n
Partition type
p primary (1 primary, 0 extended, 3 free)
e extended (container for logical partitions)
Select (default p): e
Partition number (2-4, default 2):
First sector (2099200-62914559, default 2099200):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2099200-62914559, default 62914559):
Created a new partition 2 of type 'Extended' and of size
GiB.
Command (m for help): n
All space for primary partitions is in use.
Adding logical partition 5
First sector (2101248-62914559, default 2101248):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2101248-62914559, default 62914559): +1G
Created a new partition 5 of type 'Linux' and of size 1 GiB.
Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
partprobe /dev/sdb
Example 2: Create a partition on a disk with a GPT partition table.
The following example creates a 1G partition on /dev/sdb.
fdisk /dev/sdb
Output example:
Welcome to fdisk (util-linux 2.37.4).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Command (m for help): n
Partition number (1-128, default 1):
First sector (2048-62914526, default 2048):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-62914526, default 62914526): +1G
Created a new partition 1 of type 'Linux filesystem' and of size 1 GiB.
Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
partprobe /dev/sdb

Deleting a Partition with fdisk

Use the d subcommand to delete a partition.
1. Enter the interactive interface.
fdisk <block-device>
2. Use the p subcommand to view partition information.
Command (m for help): p
3. Use the d subcommand, and then follow the prompts to delete the partition.
Command (m for help): d
4. Save the changes and exit the interactive interface.
Command (m for help): w
5. Rescan the partition table.
partprobe <block-device>
Note:
If the deleted partition contains a file system and has been written to /etc/fstab, remove the related configuration from /etc/fstab. Otherwise, the OS may fail to start.
Example:
fdisk /dev/sdb
Output example:
Welcome to fdisk (util-linux 2.37.4).
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: 30 GiB, 32212254720 bytes, 62914560 sectors
Disk model: QEMU HARDDISK
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xe7cfb001
Device Boot Start End Sectors Size Id Type
/dev/sdb1 2048 2099199 2097152 1G 83 Linux
/dev/sdb2 2099200 4196351 2097152 1G 83 Linux
/dev/sdb3 4196352 8390655 4194304 2G 83 Linux
/dev/sdb4 8390656 62914559 54523904 26G 5 Extended
/dev/sdb5 8392704 10489855 2097152 1G 83 Linux
Command (m for help): d
Partition number (1-5, default 5): 2
Partition 2 has been deleted.
Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

partprobe /dev/sdb
For more command options, see the man page.

Managing Disk Partitions with gdisk

gdisk is the GPT version of fdisk and supports only GPT partition table disk operations. When used on an MSDOS partition table disk, it converts the partition table to GPT.
gdisk is similar to fdisk in features and operation. For more information, see the fdisk operation guide.

Comparison of parted and fdisk Tools

Both parted and fdisk support the msdos and GPT partition tables.
parted provides two interaction modes: interactive mode and single-command mode, while fdisk supports only interactive mode.
parted supports more units, such as sectors, cylinders, heads, percentages, and sizes, for partitioning, while fdisk supports only sectors and sizes.
parted is feature-rich with many subcommands and parameters, while fdisk is relatively simple.

swap Space

swap Space Overview

When the physical memory is full, the OS enables swap space if available, and the kernel swaps inactive pages from memory to the swap space.
swap space can be either a dedicated swap partition or a swap file. The recommended system swap space is as follows:
Physical Memory (RAM)
Recommended swap Space Size (Without Hibernation)
Recommended swap Space Size (With Hibernation)
Less than 2 GB
2 times the RAM
3 times the RAM
2G ~ 8G
Equal to RAM
2 times the RAM
8G ~ 16G
At least 4 GB
1.5 times the RAM
Greater than 64 GB
At least 4 GB
Hibernation not recommended

Adding swap Space

Adding a swap Partition

1. First, create a partition using the parted or fdisk command, such as /dev/sdb1:
sdb 8:16 0 64G 0 disk
└─sdb1 8:17 0 8G 0 part
2. Format the swap space:
mkswap /dev/sdb1
3. Add a mount entry to /etc/fstab:
/dev/sdb1 swap swap defaults 0 0
4. Run the following command to generate a mount unit:
systemctl daemon-reload
5. Activate swap:
swapon -v /dev/sdb1
6. Check whether swap is effective:
cat /proc/swaps
Output example:
Filename Type Size Used Priority
/dev/sdb1 partition 8388604 0 -2
free -h
Output example:
total used free shared buff/cache available
Mem: 7.3Gi 404Mi 6.5Gi 33Mi 444Mi 6.7Gi
Swap: 8.0Gi 0B 8.0Gi
Check the swap information in the output to confirm that the configuration has taken effect. You can also use a logical volume to create swap space, and the procedure is similar.

Adding a swap File

1. First, create an empty file:
dd if=/dev/zero of=/data/swapfile bs=1M count=8192
2. Format the swap space:
mkswap /data/swapfile
3. Add a mount entry to /etc/fstab:
/data/swapfile swap swap defaults 0 0
4. Run the following command to generate a mount unit:
systemctl daemon-reload
5. Activate swap:
swapon -v /data/swapfile
6. Check whether swap is effective:
cat /proc/swaps
Output example:
Filename Type Size Used Priority
/data/swapfile file 8388604 0 -2
free -h
Output example:
total used free shared buff/cache available
Mem: 7.3Gi 432Mi 75Mi 33Mi 6.8Gi 6.7Gi
Swap: 8.0Gi 0B 8.0Gi
The output shows the swap information, indicating that it has taken effect.

Deleting swap Space

Notes on Deleting swap Space

You can use the swapoff command to disable swap space. Before disabling swap space, pay attention to system memory usage. The swapoff command moves memory from swap space back to physical memory by requesting memory, which can cause OOM if system memory is insufficient. If the OOM policy is set to kill processes, the swapoff command will be killed. If the OOM policy is set to Panic, the system will restart.

Deleting a swap Partition

1. Disable swap:
swapoff -v /dev/sdb1
2. Delete the sdb1 partition.
parted /dev/sdb rm 1
3. Delete the corresponding entry in /etc/fstab.
4. Regenerate the mount unit:
systemctl daemon-reload
5. Check whether it takes effect:
cat /proc/swaps
Output example:
Filename Type Size Used Priority
free -h
Output example:
total used free shared buff/cache available
Mem: 7.3Gi 421Mi 92Mi 33Mi 6.8Gi 6.7Gi
Swap: 0B 0B 0B
The output shows that swap has been disabled.

Deleting a swap File

1. Disable swap:
swapoff -v /data/swapfile
2. Delete the swap file.
rm /data/swapfile
3. Delete the corresponding entry in /etc/fstab.
4. Regenerate the mount unit:
systemctl daemon-reload
5. Check whether it takes effect:
cat /proc/swaps
Output example:
Filename Type Size Used Priority
free -h
Output example:
total used free shared buff/cache available
Mem: 7.3Gi 421Mi 92Mi 33Mi 6.8Gi 6.7Gi
Swap: 0B 0B 0B
The output shows that swap has been disabled.

Ajuda e Suporte

Esta página foi útil?

comentários