6. Linux 硬盘分区管理

MBR 分区方案

  • 该方案支持最多**4**个主分区。

  • 在 Linux系统上,管理员可以使用扩展分区和逻辑分区来创建最多 15个分区

    逻辑分区是可以格式化(format),扩展分区是不可以格式化。

  • MBR 记录用4个字节(1byte=8bit)存储分区的总扇区数,最大能表示2的32次方的扇区个

  • 数,按每扇区512字节计算,每个分区最大不能超过 2 TiB

类比为:笔记本只有4个usb接口,如果不够用,则将第四个usb接口外接扩展坞,由扩展坞

提供多个usb接口。

fdisk 工具

fdisk 工具可用于管理采用 MBR 分区方案的磁盘,用户可以根据实际情况进行划分分区。

查看分区

fdisk工具大部分操作通过交互式完成,除了查看分区表。

bash 复制代码
# 方法1:
[root@server ~ 13:53:29]# fdisk -l /dev/sdb
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

# 方法2:
[root@server ~ 13:54:12]# 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.

# 输入m,查看帮助信息
Command (m for help): `m`
Command action
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition
   g   create a new empty GPT partition table
   G   create an IRIX (SGI) partition table
   l   list known partition types
   m   print this menu
   n   add a new partition
   o   create a new empty DOS partition table
   p   print the partition table
   q   quit without saving changes
   s   create a new empty Sun disklabel
   t   change a partition's system id
   u   change display/entry units
   v   verify the partition table
   w   write table to disk and exit
   x   extra functionality (experts only)
   
# 输入p,打印分区表
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: 0x4c1f9f4f

   Device Boot      Start         End      Blocks   Id  System

# 输入q,退出管理
Command (m for help): `q`

创建分区

bash 复制代码
# 输入n,创建一个新分区
Command (m for help): `n`

# 选择分区类型
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended

# 直接回车,选择默认分区类型:primary
Select (default p): `回车`
Using default response p

# 直接回车,分区号,使用默认值1
Partition number (1-4, default 1):`回车`

# 直接回车,设置分区起始位置为:默认值2048扇区,也就是1M位置。
First sector (2048-41943039, default 2048): `回车`
Using default value 2048

# 设置分区结束位置,输入+2G,也就是起始位置之后2
Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): +5G
Partition 1 of type Linux and of size 5 GiB is set

# 执行 p 命令后,fdisk 会打印当前磁盘的完整分区信息
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: 0x0ee7f30f

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048    10487807     5242880   83  Linux

# 输入w,保存更改并退出
# 输入q,不保存更改并退出
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.

# 再次验证分区表变化
[root@server ~ 13:56:25]# lsblk /dev/sdb
NAME   MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sdb      8:16   0  20G  0 disk 
└─sdb1   8:17   0   5G  0 part 

注意:如果此时分区表未生成,执行以下命令,通知kernel重新生成分区表。有时候重启系统才

会生成最新分区表。

bash 复制代码
[root@centos7 ~]# partprobe

删除分区

bash 复制代码
[root@server ~ 13:56:36]# 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.

# 输入p,打印分区表
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: 0x0ee7f30f

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048    10487807     5242880   83  Linux

# 输入d,删除分区,因为只有1个分区,所以自动删除了第一个分区
Command (m for help): d
Selected partition 1
Partition 1 is deleted

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: 0x0ee7f30f

   Device Boot      Start         End      Blocks   Id  System

Command (m for help): q

如果硬盘空间比较大,需要的文件系统数量超过4个,那么就需要借助扩展分区创建逻辑分区了。

如下示例,创建第4个分区的时候,类型选择扩展分区;创建第5个分区的时候,类型选择逻辑分

区。

bash 复制代码
[root@server ~ 13:58:14]# 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.

# 再次创建一个容量为3G的分区
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): 
First sector (10487808-41943039, default 10487808): 
Using default value 10487808
Last sector, +sectors or +size{K,M,G} (10487808-41943039, default 41943039): +3G
Partition 2 of type Linux and of size 3 GiB is set

# 再次创建一个容量为4G的分区
Command (m for help): n
Partition type:
   p   primary (2 primary, 0 extended, 2 free)
   e   extended
Select (default p): p
Partition number (3,4, default 3): 
First sector (16779264-41943039, default 16779264): 
Using default value 16779264
Last sector, +sectors or +size{K,M,G} (16779264-41943039, default 41943039): +4G
Partition 3 of type Linux and of size 4 GiB is set

# 创建第5个分区的时候,选择扩展分区,并且使用剩余所有容量
Command (m for help): n
All primary partitions are in use
Adding logical partition 5
First sector (25169920-41943039, default 25169920): 
Using default value 25169920
Last sector, +sectors or +size{K,M,G} (25169920-41943039, default 41943039): 
Using default value 41943039
Partition 5 of type Linux and of size 8 GiB is set

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

Calling ioctl() to re-read partition table.
Syncing disks.
[root@server ~ 14:04:50]# lsblk /dev/sdb
NAME   MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sdb      8:16   0  20G  0 disk 
├─sdb1   8:17   0   5G  0 part 
├─sdb2   8:18   0   3G  0 part 
├─sdb3   8:19   0   4G  0 part 
├─sdb4   8:20   0   1K  0 part 
└─sdb5   8:21   0   8G  0 part 

# 创建第5个分区:在扩展分区中,创建逻辑分区,分配5G容量
Command (m for help): n
All primary partitions are in use
Adding logical partition 5
First sector (18878464-41943039, default 18878464):
Using default value 18878464
Last sector, +sectors or +size{K,M,G} (18878464-41943039, default
41943039): +5G
Partition 5 of type Linux and of size 5 GiB is set
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: 0x78ceaffe
Device Boot Start End Blocks Id System
/dev/sdb1 2048 4196351 2097152 83 Linux
/dev/sdb2 4196352 10487807 3145728 83 Linux
/dev/sdb3 10487808 18876415 4194304 83 Linux
/dev/sdb4 18876416 41943039 11533312 5 Extended
/dev/sdb5 18878464 29364223 5242880 83 Linux
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.

# 查看分区表
[root@centos7 ~]# lsblk /dev/sdb
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sdb 8:16 0 20G 0 disk
|-sdb1 8:17 0 2G 0 part
|-sdb2 8:18 0 3G 0 part
|-sdb3 8:19 0 4G 0 part
|-sdb4 8:20 0 1K 0 part
|-sdb5 8:21 0 5G 0 part

创建分区演示完成,只保留1个分区,多余的删除。

bash 复制代码
[root@centos7 ~]# 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): d
Partition number (1-5, default 5):
Partition 5 is deleted
Command (m for help): d
Partition number (1-4, default 4):
Partition 4 is deleted
Command (m for help): d
Partition number (1-3, default 3):
Partition 3 is deleted
Command (m for help): d
Partition number (1,2, default 2):
Partition 2 is deleted
Command (m for help): d
Selected partition 1
Partition 1 is deleted
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.

非交互方式管理

示例1:创建一个分区

bash 复制代码
# 将要执行的fdisk命令行写入到一个文本文件
[root@centos7 ~]# vim fdisk-create.txt
n
p
1
2048
+2G
p
w
# 执行
[root@centos7 ~]# fdisk /dev/sdb < fdisk-create.txt
[root@centos7 ~]# lsblk /dev/sdb
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sdb 8:16 0 20G 0 disk
|-sdb1 8:17 0 2G 0 part

示例2:删除一个分区

bash 复制代码
# 将要执行的fdisk命令行写入到一个文本文件
[root@centos7 ~]# vim fdisk-delete.txt
d
p
w
# 执行
[root@centos7 ~]# fdisk /dev/sdb < fdisk-delete.txt
[root@centos7 ~]# lsblk /dev/sdb
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sdb 8:16 0 20G 0 disk

挂载实验

bash 复制代码
[root@server ~ 14:10:35]# mkfs.xfs /dev/sdb1
# mkfs.xfs /dev/sdb1 是用于将 /dev/sdb1 分区格式化为 XFS 文件系统 的命令(XFS 是高性能日志文件系统,常用于大数据、数据库场景,支持大容量分区和快速读写)。
meta-data=/dev/sdb1              isize=512    agcount=4, agsize=196608 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=786432, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
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
[root@server ~ 14:14:36]# mkdir /data
[root@server ~ 14:15:07]# mount /dev/sdb1 /data
[root@server ~ 14:15:25]# df -h /data
Filesystem      Size  Used Avail Use% Mounted on
/dev/sdb1       3.0G   33M  3.0G   2% /data
[root@server ~ 14:15:33]# touch /data/filr-{0..9}
[root@server ~ 14:15:53]# ls /data/ -1
filr-0
filr-1
filr-2
filr-3
filr-4
filr-5
filr-6
filr-7
filr-8
filr-9
[root@server ~ 14:21:35]# df -hT /data
Filesystem   Type    Size  Used Avail Use% Mounted on
/dev/sdc1    ext4    4.8G   21M  4.6G   1% /data

GPT 分区方案

GPT是运行**统一可扩展固件接口(UEFI)**固件系统上硬盘分区表的标准。

  • MBR分区方案只能管理最大2TiB分区和磁盘。全局唯一标识分区表(GPT,GUID Partition

    Table)使用8个字节(1byte=8bit)存储分区的总扇区数,可支持最多18 ZiB(=264*512

    Byte),即18亿太字节的分区和磁盘。

  • MBR分区方案支持最多15个分区。GPT分区方案最多可提供128个分区

  • GPT 提供分区表信息的冗余

GPT分区表结构如下:

注意:下图是MBR的主引导扇区,GPT的保护性MBR(Protective MBR),除了分区表和MBR有些不一样,其他都一样。

  • 引导程序(占446个字节),硬盘启动时将系统控制权转给分区表中的某个操作系统。
  • 磁盘分区表项(DPT,Disk Partition Table),由四个分区表项构成(每个16个字节)。
  • 结束标志(占2个字节),其值为AA55(十六进制)。

gdisk 工具

gdisk工具用于管理采用GPT分区方案的磁盘分区,主要用于管理磁盘容量超过2T的磁盘。

gdisk命令语法

bash 复制代码
gdisk [ -l ] device

查看分区表

bash 复制代码
[root@server ~ 14:44:35]# gdisk -l /dev/sdb
GPT fdisk (gdisk) version 0.8.10

# 这里显示识别到了MBR分区方案
Partition table scan:
  MBR: MBR only
  BSD: not present
  APM: not present
  GPT: not present
***************************************************************
Found invalid GPT and valid MBR; converting MBR to GPT format
in memory. 
***************************************************************
Disk /dev/sdb: 41943040 sectors, 20.0 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): 053EF6BE-A25F-43E8-BEF0-4CB820CB0952
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 41943006
Partitions will be aligned on 2048-sector boundaries
Total free space is 35651517 sectors (17.0 GiB)
Number  Start (sector)    End (sector)  Size       Code  Name
   1            2048         6293503   3.0 GiB     8300  Linux filesystem

转换分区表方案

gdisk工具用于管理gpt分区,所以我们需要将磁盘的分区管理方案由MBR转换成GPT。

bash 复制代码
[root@server ~ 14:54:21]# gdisk /dev/sdb
GPT fdisk (gdisk) version 0.8.10
Partition table scan:
  MBR: MBR only
  BSD: not present
  APM: not present
  GPT: not present
***************************************************************
Found invalid GPT and valid MBR; converting MBR to GPT format
in memory. THIS OPERATION IS POTENTIALLY DESTRUCTIVE! Exit by
typing 'q' if you don't want to convert your MBR partitions
to GPT format!
***************************************************************

# 输入?,查看帮助信息。gdisk管理命令跟fdisk很相似。
Command (? for help): ?
b	back up GPT data to a file
c	change a partition's name
d	delete a partition
i	show detailed information on a partition
l	list known partition types
n	add a new partition
o	create a new empty GUID partition table (GPT)
p	print the partition table
q	quit without saving changes
r	recovery and transformation options (experts only)
s	sort partitions
t	change a partition's type code
v	verify disk
w	write table to disk and exit
x	extra functionality (experts only)
?	print this menu

# 输入o,将磁盘的分区管理方案由MBR转换成GPT
Command (? for help): o
This option deletes all partitions and creates a new protective MBR.
Proceed? (Y/N): Y

# 输入p,查看分区表
Command (? for help): p
Disk /dev/sdb: 41943040 sectors, 20.0 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): EBCF4040-3754-4D7C-AFCF-476CAF6AC6A7
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 41943006
Partitions will be aligned on 2048-sector boundaries
Total free space is 41942973 sectors (20.0 GiB)
Number  Start (sector)    End (sector)  Size       Code  Name

# 输入w,保存更改并退出
# 输入q,不保存更改并退出
Command (? for help): w
Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
PARTITIONS!!
Do you want to proceed? (Y/N): Y
OK; writing new GUID partition table (GPT) to /dev/sdb.
The operation has completed successfully.

创建分区

bash 复制代码
[root@server ~ 14:56:05]# gdisk /dev/sdb
GPT fdisk (gdisk) version 0.8.10

# 这里识别到了GPT分区方案
Partition table scan:
  MBR: protective
  BSD: not present
  APM: not present
  GPT: present
Found valid GPT with protective MBR; using GPT.

# 输入n,创建新分区
Command (? for help): `n`
# 设置分区ID
Partition number (1-128, default 1): `回车`
# 设置分区起始位置
First sector (34-41943006, default = 2048) or {+-}size{KMGTP}: `回车`
# 设置分区结束位置
Last sector (2048-41943006, default = 41943006) or {+-}size{KMGTP}:
`+2G`
Current type is 'Linux filesystem'

# 设置分区类型,输入L查看所有分区类型
Hex code or GUID (L to show codes, Enter = 8300): `L`
0700 Microsoft basic data  0c01 Microsoft reserved    2700 Windows RE          
3000 ONIE boot             3001 ONIE config           4100 PowerPC PReP boot   
4200 Windows LDM data      4201 Windows LDM metadata  7501 IBM GPFS            
7f00 ChromeOS kernel       7f01 ChromeOS root         7f02 ChromeOS reserved   
8200 Linux swap            8300 Linux filesystem      8301 Linux reserved      
8302 Linux /home           8400 Intel Rapid Start     8e00 Linux LVM           
a500 FreeBSD disklabel     a501 FreeBSD boot          a502 FreeBSD swap        
a503 FreeBSD UFS           a504 FreeBSD ZFS           a505 FreeBSD Vinum/RAID  
a580 Midnight BSD data     a581 Midnight BSD boot     a582 Midnight BSD swap   
a583 Midnight BSD UFS      a584 Midnight BSD ZFS      a585 Midnight BSD Vinum  
a800 Apple UFS             a901 NetBSD swap           a902 NetBSD FFS          
a903 NetBSD LFS            a904 NetBSD concatenated   a905 NetBSD encrypted    
a906 NetBSD RAID           ab00 Apple boot            af00 Apple HFS/HFS+      
af01 Apple RAID            af02 Apple RAID offline    af03 Apple label         
af04 AppleTV recovery      af05 Apple Core Storage    be00 Solaris boot        
bf00 Solaris root          bf01 Solaris /usr & Mac Z  bf02 Solaris swap        
bf03 Solaris backup        bf04 Solaris /var          bf05 Solaris /home       
bf06 Solaris alternate se  bf07 Solaris Reserved 1    bf08 Solaris Reserved 2  
bf09 Solaris Reserved 3    bf0a Solaris Reserved 4    bf0b Solaris Reserved 5  
c001 HP-UX data            c002 HP-UX service         ea00 Freedesktop $BOOT   
eb00 Haiku BFS             ed00 Sony system partitio  ed01 Lenovo system partit

# 输入回车继续查看
Press the <Enter> key to see more codes: `回车`
ef00 EFI System            ef01 MBR partition scheme  ef02 BIOS boot partition 
fb00 VMWare VMFS           fb01 VMWare reserved       fc00 VMWare kcore crash p
fd00 Linux RAID   

# 输入回车,选择默认分区类型
Hex code or GUID (L to show codes, Enter = 8300): 
Changed type of partition to 'Linux filesystem'

# 输入p,查看分区表
Command (? for help): p
Disk /dev/sdb: 41943040 sectors, 20.0 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): EBCF4040-3754-4D7C-AFCF-476CAF6AC6A7
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 41943006
Partitions will be aligned on 2048-sector boundaries
Total free space is 37748669 sectors (18.0 GiB)
Number  Start (sector)    End (sector)  Size       Code  Name
   1            2048         4196351   2.0 GiB     8300  Linux filesystem

更改分区名称

bash 复制代码
# 输入c,修改分区ID为1的分区名
Command (? for help): c
Using 1

# 输入新名称
Enter name: data01
Command (? for help): p
Disk /dev/sdb: 41943040 sectors, 20.0 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): EBCF4040-3754-4D7C-AFCF-476CAF6AC6A7
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 41943006
Partitions will be aligned on 2048-sector boundaries
Total free space is 37748669 sectors (18.0 GiB)
Number  Start (sector)    End (sector)  Size       Code  Name
   1            2048         4196351   2.0 GiB     8300  data01
   
# 查看分区表
Command (? for help): p
Disk /dev/sdb: 41943040 sectors, 20.0 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): EBCF4040-3754-4D7C-AFCF-476CAF6AC6A7
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 41943006
Partitions will be aligned on 2048-sector boundaries
Total free space is 37748669 sectors (18.0 GiB)

Number  Start (sector)    End (sector)  Size       Code  Name
   1            2048         4196351   2.0 GiB     8300  data01

查看分区详细信息

bash 复制代码
Command (? for help): i
Using 1
Partition GUID code: 0FC63DAF-8483-4772-8E79-3D69D8477DE4 (Linux filesystem)
Partition unique GUID: B9944C96-8CC2-4D57-98CD-63251A6A059B
First sector: 2048 (at 1024.0 KiB)
Last sector: 4196351 (at 2.0 GiB)
Partition size: 4194304 sectors (2.0 GiB)
Attribute flags: 0000000000000000
Partition name: 'data01'

删除分区

bash 复制代码
# 输入d,删除分区ID为1的分区
Command (? for help): `d`
Using 1
Command (? for help): `p`
Disk /dev/sdb: 41943040 sectors, 20.0 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): EBCF4040-3754-4D7C-AFCF-476CAF6AC6A7
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 41943006
Partitions will be aligned on 2048-sector boundaries
Total free space is 41942973 sectors (20.0 GiB)
Number  Start (sector)    End (sector)  Size       Code  Name
Command (? for help): `w`
Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
PARTITIONS!!
Do you want to proceed? (Y/N): `Y`
OK; writing new GUID partition table (GPT) to /dev/sdb.
The operation has completed successfully.

挂载实验

bash 复制代码
[root@server ~ 15:00:45]# lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0   20G  0 disk 
├─sda1            8:1    0    1G  0 part /boot
└─sda2            8:2    0   19G  0 part 
  ├─centos-root 253:0    0   17G  0 lvm  /
  └─centos-swap 253:1    0    2G  0 lvm  [SWAP]
sdb               8:16   0   20G  0 disk 
sdc               8:32   0   20G  0 disk 
└─sdc1            8:33   0    5G  0 part 
sr0              11:0    1  4.4G  0 rom  
[root@server ~ 15:00:47]# mkfs.ext4 /dev/sdc1
# mkfs.ext4 /dev/sdc1 是 Linux 下创建 ext4 文件系统的标准命令,核心是 确认设备名正确 + 卸载分区 + 备份数据,避免误操作导致数据丢失。格式化后通过挂 载即可正常使用分区。
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
327680 inodes, 1310720 blocks
65536 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=1342177280
40 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks: 
	32768, 98304, 163840, 229376, 294912, 819200, 884736

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done 

[root@server ~ 15:02:14]# lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0   20G  0 disk 
├─sda1            8:1    0    1G  0 part /boot
└─sda2            8:2    0   19G  0 part 
  ├─centos-root 253:0    0   17G  0 lvm  /
  └─centos-swap 253:1    0    2G  0 lvm  [SWAP]
sdb               8:16   0   20G  0 disk 
sdc               8:32   0   20G  0 disk 
└─sdc1            8:33   0    5G  0 part 
sr0              11:0    1  4.4G  0 rom  
[root@server ~ 15:02:37]# df
Filesystem              1K-blocks    Used Available Use% Mounted on
devtmpfs                  2000996       0   2000996   0% /dev
tmpfs                     2013056       0   2013056   0% /dev/shm
tmpfs                     2013056   11736   2001320   1% /run
tmpfs                     2013056       0   2013056   0% /sys/fs/cgroup
/dev/mapper/centos-root  17811456 1664876  16146580  10% /
/dev/sda1                 1038336  142252    896084  14% /boot
tmpfs                      402612       0    402612   0% /run/user/0
[root@server ~ 15:02:47]# umount /data
umount: /data: not mounted
[root@server ~ 15:02:58]# mount /dev/sdc1 /sda1
mount: mount point /sda1 does not exist
[root@server ~ 15:03:52]# mount /dev/sdc1 /sdc1
mount: mount point /sdc1 does not exist
[root@server ~ 15:04:01]# df /data
Filesystem              1K-blocks    Used Available Use% Mounted on
/dev/mapper/centos-root  17811456 1664876  16146580  10% /
[root@server ~ 15:04:15]# cp /etc/ho* /sdc1
cp: target '/sdc1' is not a directory
[root@server ~ 15:04:44]# umount /dev/sdc1 /sdc1
umount: /dev/sdc1: not mounted
umount: /sdc1: mountpoint not found
[root@server ~ 15:05:12]# df
Filesystem              1K-blocks    Used Available Use% Mounted on
devtmpfs                  2000996       0   2000996   0% /dev
tmpfs                     2013056       0   2013056   0% /dev/shm
tmpfs                     2013056   11736   2001320   1% /run
tmpfs                     2013056       0   2013056   0% /sys/fs/cgroup
/dev/mapper/centos-root  17811456 1664876  16146580  10% /
/dev/sda1                 1038336  142252    896084  14% /boot
tmpfs                      402612       0    402612   0% /run/user/0
[root@server ~ 15:05:19]# mkdir /data
mkdir: cannot create directory '/data': File exists
[root@server ~ 15:05:32]# touch /data
[root@server ~ 15:05:59]# mount /dev/sdc1 /data
[root@server ~ 15:06:13]# df /data
Filesystem     1K-blocks  Used Available Use% Mounted on
/dev/sdc1        5029504 20472   4730504   1% /data
[root@server ~ 15:06:18]# cp /etc/ho* /data
[root@server ~ 15:06:33]# ls /data
host.conf  hostname  hosts  hosts.allow  hosts.deny  lost+found
[root@server ~ 15:06:37]# lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0   20G  0 disk 
├─sda1            8:1    0    1G  0 part /boot
└─sda2            8:2    0   19G  0 part 
  ├─centos-root 253:0    0   17G  0 lvm  /
  └─centos-swap 253:1    0    2G  0 lvm  [SWAP]
sdb               8:16   0   20G  0 disk 
sdc               8:32   0   20G  0 disk 
└─sdc1            8:33   0    5G  0 part /data
sr0              11:0    1  4.4G  0 rom 

wipefs 工具

作用:清除磁盘分区表信息。

注意:数据无价,操作需谨慎,最好提前备份。

bash 复制代码
# 清除未挂载磁盘的分区表
[root@centos7 ~]# wipefs -a /dev/sdb

# 禁止使用 -f 强制清除分区表
[root@centos7 ~]# wipefs -fa /dev/sdb

parted 工具

parted 工具既可以管理采用MBR分区方案的磁盘,又可以管理采用GPT分区方案的磁盘。

parted 命令同时支持交互式操作和非交互式操作(编写脚本)。

我们先来看看交互式操作。

操作流程:

  1. 查看分区表。如果是未初始化硬盘,创建分区。
  2. 设置单位(MiB)
  3. 创建分区
  4. 调整分区大小
  5. 调整分区类型
  6. 删除分区
bash 复制代码
# 用 df 查看已挂载文件系统的磁盘使用情况(含根分区、/boot 等);
[root@server ~ 15:21:02]# df
Filesystem              1K-blocks    Used Available Use% Mounted on
devtmpfs                  2000996       0   2000996   0% /dev
tmpfs                     2013056       0   2013056   0% /dev/shm
tmpfs                     2013056   11772   2001284   1% /run
tmpfs                     2013056       0   2013056   0% /sys/fs/cgroup
/dev/mapper/centos-root  17811456 1663708  16147748  10% /
/dev/sda1                 1038336  142252    896084  14% /boot
tmpfs                      402612       0    402612   0% /run/user/0

# 用 lsblk 查看所有磁盘及分区结构(确认 sda 有系统分区、sdb/sdc/sdd 为空闲 20G 磁盘,sdc 初始有 5G 分区);
[root@server ~ 15:21:27]# lsblk 
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0   20G  0 disk 
├─sda1            8:1    0    1G  0 part /boot
└─sda2            8:2    0   19G  0 part 
  ├─centos-root 253:0    0   17G  0 lvm  /
  └─centos-swap 253:1    0    2G  0 lvm  [SWAP]
sdb               8:16   0   20G  0 disk 
sdc               8:32   0   20G  0 disk 
└─sdc1            8:33   0    5G  0 part 
sdd               8:48   0   20G  0 disk 
sr0              11:0    1  4.4G  0 rom  

#用 parted 命令查看 sdb、sdc、sda 的分区表类型(sdb 为 GPT 空表、sdc 初始为 GPT 且有 5G ext4 分区、sda 为 MSDOS 分区表含 /boot 和 LVM 分区)。
[root@server ~ 15:21:40]# parted /dev/sdb print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: `21.5GB`
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 
Number  Start  End  Size  File system  Name  Flags

[root@server ~ 15:21:59]# parted /dev/sdb unit MiB print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: `20480MiB`
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 
Number  Start  End  Size  File system  Name  Flags

# 用 parted /dev/sda rm 1 删除 /boot 分区(sdc1)
[root@server ~ 15:22:43]# parted /dev/sdc rm 1
Information: You may need to update /etc/fstab.
[root@server ~ 15:23:03]# parted /dev/sdc print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdc: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 
Number  Start  End  Size  File system  Name  Flags


[root@server ~ 15:23:28]# parted /dev/sdc help|grep delete
  rm NUMBER                                delete partition NUMBER
# 尝试用 parted /dev/sda rm 1 删除 /boot 分区(sda1),因该分区正被使用(挂载中),操作失败。
[root@server ~ 15:23:50]# parted /dev/sda rm 1
Error: Partition /dev/sda1 is being used. You must unmount it before you modify it
with Parted.
[root@server ~ 15:24:35]# lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0   20G  0 disk 
├─sda1            8:1    0    1G  0 part /boot
└─sda2            8:2    0   19G  0 part 
  ├─centos-root 253:0    0   17G  0 lvm  /
  └─centos-swap 253:1    0    2G  0 lvm  [SWAP]
sdb               8:16   0   20G  0 disk 
sdc               8:32   0   20G  0 disk 
sdd               8:48   0   20G  0 disk 
sr0              11:0    1  4.4G  0 rom  
[root@server ~ 15:25:09]# lsblk /dev/sdd
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sdd    8:48   0  20G  0 disk 

[root@server ~ 15:27:02]# parted /dev/sdd unit MiB print
Error: /dev/sdd: unrecognised disk label
Model: VMware, VMware Virtual S (scsi)                                    
Disk /dev/sdd: 20480MiB
Sector size (logical/physical): 512B/512B
Partition Table: unknown
Disk Flags: 
bash 复制代码
# 执行 parted /dev/sdd mklabel gpt,为无分区表的 sdd 磁盘创建 GPT 分区表(确认数据清除后完成)。
[root@server ~ 15:55:11]# parted /dev/sdd mklabel gpt
Warning: The existing disk label on /dev/sdd will be destroyed and all data on this
disk will be lost. Do you want to continue?
Yes/No? Yes                                                               
Information: You may need to update /etc/fstab.

[root@server ~ 15:55:46]# lsblk                                           
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0   20G  0 disk 
├─sda1            8:1    0    1G  0 part /boot
└─sda2            8:2    0   19G  0 part 
  ├─centos-root 253:0    0   17G  0 lvm  /
  └─centos-swap 253:1    0    2G  0 lvm  [SWAP]
sdb               8:16   0   20G  0 disk 
sdc               8:32   0   20G  0 disk 
sdd               8:48   0   20G  0 disk 
sr0              11:0    1  4.4G  0 rom  
[root@server ~ 15:56:51]# parted /dev/sdd print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdd: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 
Number  Start  End  Size  File system  Name  Flags

# 以 MiB 为单位,创建第一个分区 data01(1MiB~5121MiB,大小 5120MiB,约 5G);
[root@server ~ 15:57:16]# parted /dev/sdd unit MiB mkpart data01 xfs 1 5121
Information: You may need to update /etc/fstab.
[root@server ~ 15:58:04]# parted /dev/sdd unit MiB print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdd: 20480MiB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 
Number  Start    End      Size     File system  Name    Flags
 1      1.00MiB  5121MiB  5120MiB               data01

# 继续创建第二个分区 webapp01(5121MiB~10241MiB,大小 5120MiB,约 5G),剩余磁盘空间未分配。
[root@server ~ 15:58:26]# parted /dev/sdd unit MiB mkpart webapp01 5121 10241
Information: You may need to update /etc/fstab.
[root@server ~ 15:58:33]# parted /dev/sdd unit MiB print                  
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdd: 20480MiB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 
Number  Start    End       Size     File system  Name      Flags
 1      1.00MiB  5121MiB   5120MiB               data01
 2      5121MiB  10241MiB  5120MiB               webapp01

# 用 mkfs.xfs 格式化 data01 分区(/dev/sdd1)为 XFS 文件系统;
[root@server ~ 15:58:39]# mkfs.xfs /dev/sdd1
meta-data=/dev/sdd1              isize=512    agcount=4, agsize=327680 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=1310720, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
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
[root@server ~ 15:58:55] mkdir /usr/share/nginx/html/myweb
# 创建挂载目录 /usr/share/nginx/html/myweb,将 /dev/sdd1 挂载到该目录;
[root@server ~ 15:59:24]# mount /dev/sdd1 /usr/share/nginx/html/myweb
# 在挂载目录下创建测试文件 index.html,写入内容并通过 df 验证挂载成功(已占用 1% 空间)。
[root@server ~ 15:59:31] echo Hello World from parted disk. > /usr/share/nginx/html/myweb/index.html
[root@server ~ 15:59:45]# df /usr/share/nginx/html/myweb
Filesystem     1K-blocks  Used Available Use% Mounted on
/dev/sdd1        5232640 33016   5199624   1% /usr/share/nginx/html/myweb

综合案例:文件系统空间不足

情况1:大量的大文件占用空间

准备环境

bash 复制代码
# 执行该命令后,/dev/sdb 会新增一个 2G 的 GPT 分区 /dev/sdb1(分区编号从 1 开始)
[root@centos7 ~] parted /dev/sdb mklabel gpt
# parted 磁盘分区管理工具(支持 MBR/GPT 分区表,比 fdisk 更适合大容量磁盘)
# /dev/sdb	目标磁盘设备文件(需确认是要操作的磁盘,避免误删数据!)
# mklabel gpt	核心参数:创建 GPT 分区表(会覆盖磁盘原有分区表,数据全部丢失)

[root@centos7 ~] parted /dev/sdb unit MiB mkpart data01 xfs 1 2049
# parted /dev/sdb	调用 parted 工具操作 /dev/sdb 磁盘(必须已创建 GPT 分区表,否则报错)
# unit MiB	设定分区大小单位为 MiB(1 MiB = 1024×1024 字节,避免不同单位换算误差)
# mkpart	核心指令:创建分区
# data01	分区名称(GPT 分区支持命名,方便识别,MBR 分区不支持)

[root@centos7 ~] mkfs.xfs -f /dev/sdb1
# -f 强制格式化参数(关键!忽略分区已存在的文件系统 / 数据,直接覆盖,数据不可逆删除)
[root@centos7 ~]# mkdir /myapp-1
[root@centos7 ~]# mount /dev/sdb1 /myapp-1
[root@centos7 ~]# cp -r /etc/ /myapp-1/
# -r 递归复制参数(必须加!因为 /etc/ 是目录,含多层子目录和文件,无 -r 会报错)

# 创建一个大文件
[root@centos7 ~]# dd if=/dev/zero of=/myapp-1/etc/bigfile bs=1M count=2000
# dd	底层数据复制工具(可按指定块大小 / 数量生成文件、复制磁盘 / 分区数据)
# if=/dev/zero	输入文件(if=input file):/dev/zero 是系统虚拟设备,持续输出空字符(0x00)
# of=/myapp-1/etc/bigfile	输出文件(of=output file):生成的大文件路径和名称
# bs=1M	块大小(block size):每次读写 1MB(1M = 1024×1024 字节,避免小 block 导致速度慢)
# count=2000	块数量:共读写 2000 个块,总大小 = bs × count = 1M × 2000 = 2000MB = 2GB

原因 :大文件占用大量空间。
解决方法:找到文件后删除。

bash 复制代码
[root@server ~ 19:29:56]# df -h /myapp-1/
# df 磁盘空间查看命令(display free disk space),默认显示所有已挂载分区
# -h 人性化显示参数(human-readable),将字节转换为 GB/MB/KB,方便阅读
Filesystem      Size  Used Avail Use% Mounted on
/dev/sdb1       2.0G  2.0G  128K 100% /myapp-1

# 方法1:find 查找
[root@server ~ 19:30:09]# find /myapp-1/ -size +100M
/myapp-1/etc/bigfile

# 方法2:du 查找
[root@server ~ 19:30:18]# du -s /myapp-1/* |sort -n |tail -2
2052544	/myapp-1/etc
[root@server ~ 19:30:27]# du -s /myapp-1/etc/* |sort -n |tail -2
19452	/myapp-1/etc/selinux
2019776	/myapp-1/etc/bigfile
[root@server ~ 19:30:33]# du -s /myapp-1/etc/bigfile/* |sort -n |tail -2
du: cannot access '/myapp-1/etc/bigfile/*': Not a directory

# 删除大文件
[root@server ~ 19:30:38]# rm -f /myapp-1/etc/bigfile
[root@server ~ 19:30:48]# df -h /myapp-1/
Filesystem      Size  Used Avail Use% Mounted on
/dev/sdb1       2.0G   66M  2.0G   4% /myapp-1

找到文件后,删除即可。

情况2:删除文件后,空间没有释放

准备环境

bash 复制代码
[root@server ~ 19:30:51]# dd if=/dev/zero of=/myapp-1/etc/bigfile bs=1M count=2000
[root@server ~ 20:37:28]# tail -f /myapp-1/etc/bigfile &
[2] 1333

# 删除文件后,空间没有释放
[root@server ~ 19:36:29]# rm -f /myapp-1/etc/bigfile
[root@server ~ 19:36:57]# df -h /myapp-1/
# -h 人性化显示(将字节转为 GB/MB,避免冗长数字)
Filesystem      Size  Used Avail Use% Mounted on
/dev/sdb1       2.0G  2.0G  808K 100% /myapp-1

原因 :被删除的文件,仍然有程序在使用。
解决方法:找到像一个的程序并,终止程序。

bash 复制代码
[root@centos7 ~]# lsof |grep delete |grep /myapp-1
# lsof 列出系统中所有打开的文件(list open files),包括进程占用的文件、网络连接等(僵尸文件)
# grep delete	筛选出状态为 DELETE 的文件(即文件已被删除,但进程仍持有其句柄)
# grep /myapp-1	进一步筛选出路径包含 /myapp-1 的文件(聚焦目标目录下的 "僵尸文件")
tail       1331         root    3r      REG               8,17 2067791872        779 /myapp-1/etc/bigfile (deleted)
[root@server ~ 20:55:50]# kill -9 1331
[root@server ~ 20:56:14]# df -h /myapp-1/
Filesystem      Size  Used Avail Use% Mounted on
/dev/sdb1       2.0G   66M  2.0G   4% /myapp-1

建议:清理大文件,先使用重定向清空文件内容,再删除文件。

情况3:大量的小文件占用空间

准备环境

bash 复制代码
[root@centos7 ~]# parted /dev/sdb unit MiB mkpart data02 ext4 2049 3073
[root@centos7 ~]# mkfs.ext4 /dev/sdb2
[root@centos7 ~]# mkdir /myapp-2
[root@centos7 ~]# mount /dev/sdb2 /myapp-2
[root@server ~ 15:01:00]# df -i /myapp-2
文件系统 Inode 已用(I) 可用(I) 已用(I)% 挂载点
/dev/sdb2 65536 11 65525 1% /myapp-2
[root@centos7 ~]# touch /myapp-2/file-{00001..65530}
touch: 无法创建"/myapp-2/file-65526": 设备上没有空间
touch: 无法创建"/myapp-2/file-65527": 设备上没有空间
touch: 无法创建"/myapp-2/file-65528": 设备上没有空间
touch: 无法创建"/myapp-2/file-65529": 设备上没有空间
touch: 无法创建"/myapp-2/file-65530": 设备上没有空间
[root@server ~ 21:19:15]# df -h /myapp-2/
Filesystem      Size  Used Avail Use% Mounted on
/dev/sdb2       976M  4.5M  905M   1% /myapp-2

原因 :文件系统中inode使用完了。
解决方法:删除大量的小文件或者将这些小文件备份到其他地方。

bash 复制代码
[root@centos7 ~] df -hi /myapp-2
# 核心参数组合:
# -h:人性化显示(将 inode 数量转为 K/M/G 单位,避免冗长数字)
# -i:查看 inode 相关信息(而非默认的磁盘容量)
#inode(索引节点)是 Linux 文件系统的核心:常见问题:磁盘容量未占满,但 inode 耗尽(如创建大量小文件),此时无法再创建新文件(即使有剩余空间)。
Filesystem     Inodes IUsed IFree IUse% Mounted on
/dev/sdb2         64K   64K     0  100% /myapp-2

# 思路1:直接将这些小文件移走或删除
[root@centos7 ~]# rm -f /myapp-2/file-0*

# 思路2:合并大量小文件为单个文件
[root@centos7 ~]# cat /myapp-2/file-{00001..10000} > file-00001

思考:如何彻底擦除硬盘上的数据

解答

  1. 逻辑破坏 :用0填充所有数据,例如 dd if=/dev/zero of=/dev/sdb 。
    已用(I) 可用(I) 已用(I)% 挂载点
    /dev/sdb2 65536 11 65525 1% /myapp-2

    root@centos7 \~\]# touch /myapp-2/file-{00001...65530} touch: 无法创建"/myapp-2/file-65526": 设备上没有空间 touch: 无法创建"/myapp-2/file-65527": 设备上没有空间 touch: 无法创建"/myapp-2/file-65528": 设备上没有空间 touch: 无法创建"/myapp-2/file-65529": 设备上没有空间 touch: 无法创建"/myapp-2/file-65530": 设备上没有空间 \[root@server \~ 21:19:15\]# df -h /myapp-2/ Filesystem Size Used Avail Use% Mounted on /dev/sdb2 976M 4.5M 905M 1% /myapp-2 **原因**:文件系统中inode使用完了。 **解决方法**:删除大量的小文件或者将这些小文件备份到其他地方。 ```bash [root@centos7 ~] df -hi /myapp-2 # 核心参数组合: # -h:人性化显示(将 inode 数量转为 K/M/G 单位,避免冗长数字) # -i:查看 inode 相关信息(而非默认的磁盘容量) #inode(索引节点)是 Linux 文件系统的核心:常见问题:磁盘容量未占满,但 inode 耗尽(如创建大量小文件),此时无法再创建新文件(即使有剩余空间)。 Filesystem Inodes IUsed IFree IUse% Mounted on /dev/sdb2 64K 64K 0 100% /myapp-2 # 思路1:直接将这些小文件移走或删除 [root@centos7 ~]# rm -f /myapp-2/file-0* # 思路2:合并大量小文件为单个文件 [root@centos7 ~]# cat /myapp-2/file-{00001..10000} > file-00001

解答

  1. 逻辑破坏:用0填充所有数据,例如 dd if=/dev/zero of=/dev/sdb 。
  2. 物理破坏,例如消磁。
相关推荐
枫子有风几秒前
Docker的安装【下载安装、报错、镜像配置、常用命令】
运维·docker·容器
松涛和鸣1 分钟前
48、MQTT 3.1.1
linux·前端·网络·数据库·tcp/ip·html
leo825...1 分钟前
Docker常见命令(总结)
运维·docker·容器
仙仙学姐测评2 分钟前
开题报告PPT自动化生成工具研究
运维·自动化·powerpoint
希赛网4 分钟前
网工备考,华为ENSP基础配置命令
服务器·网络·网络工程师·华为认证·命令行·ensp命令·网工备考
大小鱼鱼鱼与鱼.4 分钟前
linux磁盘扩展
linux·运维·服务器
Solar20257 分钟前
构建高可靠性的机械设备企业数据采集系统:架构设计与实践指南
java·大数据·运维·服务器·架构
地球资源数据云17 分钟前
MODIS(MCD19A2)中国2000-2024年度平均气溶胶光学深度数据集
大数据·服务器·数据库·人工智能·均值算法
linux修理工18 分钟前
sed替换文字和vim里替换文字
linux
坚持学习前端日记23 分钟前
Nginx 搭建文件服务器
运维·服务器·nginx