内容全为个人理解和自查资料梳理,欢迎各位大神指点!
每天学习较为零散。
day21
一、磁盘维护流程
新硬盘(虚拟机可添加)
新硬盘需要做lvm管理
数据库迁移(夜间网站停机维护):
停止数据库监控
停止前端(关闭前端数据入口)关闭端口
systemctl stop apache2
停止后端(可选)。
停止mysql数据库(防止数据还在写入、或者锁表)
备份数据库(全备)
迁移数据到新硬盘(rsync)新硬盘已经做好了Ivm,且挂载
cp /usr/local/mysql
启动数据库
启动前端入口
测试数据速写
博客功能重新恢复上线
打开数据库监控
二、磁盘和硬盘
磁盘
类型 | 全称 | 主要特点 | 适用场景 |
---|---|---|---|
IDE | Integrated Drive Electronics | 老旧并行接口,速度慢(≤133 MB/s) 不支持热插拔,兼容性极强 虚拟化中模拟传统硬件 |
兼容旧系统(如DOS/Windows 98) |
SCSI | Small Computer System Interface | 中高速接口(SAS可达12 Gbps) 支持多设备队列和热插拔 虚拟化中性能稳定(推荐选项) |
企业级服务器/数据库存储 |
SATA | Serial ATA | 主流机械硬盘接口(6 Gbps) 性价比高,容量大 延迟高于NVMe |
家用PC/冷数据存储 |
NVMe | Non-Volatile Memory Express | PCIe通道直连,超低延迟(3.0 x4可达4 GB/s) 支持高并发IOPS(数万级) 需硬件支持 |
高性能需求(AI/高频交易/虚拟机宿主盘) |
硬盘
特性 | 机械硬盘(HDD) | 固态硬盘(SSD) |
---|---|---|
存储原理 | 磁性盘片 + 机械磁头读写 | NAND 闪存芯片(无机械部件) |
速度(典型值) | 80-160 MB/s(SATA HDD) | 500-3500 MB/s(SATA/NVMe SSD) |
随机读写(IOPS) | 50-200 IOPS(7200 RPM) | 50,000-500,000+ IOPS |
延迟 | 毫秒级(ms) | 微秒级(µs) |
抗震性 | 低(怕震动、跌落) | 高(无机械部件) |
噪音 | 有(磁头寻道声) | 无(静音) |
功耗 | 较高(5-7W) | 较低(2-5W) |
寿命 | 理论无限(磁头磨损) | 有限(P/E 擦写次数) |
容量 | 大(1TB-20TB+) | 较小(128GB-8TB) |
价格($/GB) | 低(约 $0.02/GB) | 较高(约 0.08−0.2/GB) |
接口 | SATA, SAS | SATA, NVMe (PCIe), U.2 |
适用场景 | 大容量存储、冷数据备份 | 系统盘、数据库、游戏、高性能计算 |
三、Linux 磁盘命名规则
Linux 磁盘设备命名遵循 /dev/[前缀][字母][数字]
的格式
/dev/
:设备文件目录[前缀]
:表示设备类型[字母]
:表示设备序号[数字]
:表示分区号(如有)
物理磁盘命名规则
接口类型 | 命名格式 | 示例 | 说明 |
---|---|---|---|
SATA/SCSI/SAS | /dev/sdX |
/dev/sda |
sd 表示标准块设备,X 为字母(a-z),按内核检测顺序分配 |
NVMe | /dev/nvmeXnY |
/dev/nvme0n1 |
X 为控制器编号(从0开始),Y 为命名空间编号(通常为1) |
IDE(已淘汰) | /dev/hdX |
/dev/hda |
现代Linux系统已统一改为sdX 命名 |
USB/移动设备 | /dev/sdX |
/dev/sdc |
与SATA规则相同,按插入顺序分配字母 |
虚拟化环境磁盘命名
虚拟化类型 | 命名格式 | 示例 | 说明 |
---|---|---|---|
KVM/QEMU | /dev/vdX |
/dev/vda |
vd 表示虚拟磁盘,常见于虚拟机环境 |
云服务器(AWS等) | /dev/xvdX 或 /dev/nvmeXnY |
/dev/xvda |
云厂商可能自定义命名(如AWS使用xvda ,部分云NVMe仍用标准命名) |
NVMe特殊命名
NVMe设备采用更复杂的命名:
/dev/nvme[控制器号]n[命名空间号]p[分区号]
- 示例:
/dev/nvme0n1
:第一个控制器的第一个命名空间/dev/nvme0n1p1
:上述设备的第一个分区
四、虚拟机ubuntu系统新磁盘分区
(一)、查看当前分区
root@xun-virtual-machine:~# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
loop0 7:0 0 74.3M 1 loop /snap/core22/1612
loop1 7:1 0 4K 1 loop /snap/bare/5
loop2 7:2 0 271.2M 1 loop /snap/firefox/4848
loop3 7:3 0 505.1M 1 loop /snap/gnome-42-2204/176
loop4 7:4 0 91.7M 1 loop /snap/gtk-common-themes/1535
loop5 7:5 0 12.9M 1 loop /snap/snap-store/1113
loop6 7:6 0 38.8M 1 loop /snap/snapd/21759
loop7 7:7 0 500K 1 loop /snap/snapd-desktop-integration/178
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 1M 0 part
├─sda2 8:2 0 513M 0 part /boot/efi
└─sda3 8:3 0 19.5G 0 part /
sdb 8:16 0 20G 0 disk
sr0 11:0 1 4.4G 0 rom /media/xun/Ubuntu 22.04.5 LTS amd64
root@xun-virtual-machine:~# ls /dev/sd*
/dev/sda /dev/sda1 /dev/sda2 /dev/sda3 /dev/sdb
#分区类型: gpt
root@xun-virtual-machine:~# fdisk -l
磁盘名 容量大小 扇区数量
Disk /dev/sda: 20 GiB, 21474836480 bytes, 41943040 sectors
Disk model: VMware Virtual S
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: DA4CF18B-4933-4267-98FC-3BA281C856A6
Device Start End Sectors Size Type
/dev/sda1 2048 4095 2048 1M BIOS boot
/dev/sda2 4096 1054719 1050624 513M EFI System
/dev/sda3 1054720 41940991 40886272 19.5G Linux filesystem
Disk /dev/sdb: 20 GiB, 21474836480 bytes, 41943040 sectors
Disk model: VMware Virtual S
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 1M 0 part
├─sda2 8:2 0 513M 0 part /boot/efi
└─sda3 8:3 0 19.5G 0 part /
sdb 8:16 0 20G 0 disk
(二)、分区
小于2TB用fdisk 大于2TB用gdisk
1、DOS (MBR) 相关命令
-
a
:切换分区的“可启动”标志(用于设置引导分区)。 -
b
:编辑嵌套的 BSD 磁盘标签(高级功能)。 -
c
:切换 DOS 兼容性标志(影响旧系统兼容性)。
2、通用分区操作
-
d
:删除分区。 -
F
:列出未分区的空闲空间。 -
l
:显示已知的分区类型列表(如 Linux swap、NTFS 等)。 -
n
:创建新分区(需指定主分区/扩展分区)。 -
p
:打印当前分区表(查看分区信息)。 -
t
:更改分区类型(如将分区设为 Linux 文件系统或 swap)。 -
v
:验证分区表(检查错误)。 -
i
:显示某个分区的详细信息。
3、其他功能
-
m
:显示帮助菜单(即当前列表)。 -
u
:切换显示单位(如扇区/柱面)。 -
x
:进入专家模式(高级操作,谨慎使用)。
4、脚本与磁盘标签
-
I
:从脚本文件加载分区布局。 -
O
:将当前分区布局导出为脚本文件。
5、保存与退出
-
w
:保存分区表并退出(生效修改)。 -
q
:不保存修改,直接退出。
6、创建新分区表
-
g
:创建空的 GPT 分区表(适用于大容量磁盘或 UEFI)。 -
G
:创建空的 SGI (IRIX) 分区表(特殊用途)。 -
o
:创建空的 DOS (MBR) 分区表(传统 BIOS 兼容)。 -
s
:创建空的 Sun 分区表(Solaris 系统使用)。
(三)、主分区和扩展分区
MBR分区类型可有四个主分区、扩展分区,从编号5开始是逻辑分区
GPT分区类型可有128个主分区
主分区(Primary Partition)
- 定义:直接存储数据的独立分区,可安装操作系统(如 Windows、Linux)。
- 特点:
- 一个硬盘最多只能有 4 个主分区(MBR 限制)。
- 可以直接格式化并挂载使用(如
/dev/sda1
、/dev/sda2
)。 - 主分区可以直接用于 启动操作系统(如
/boot
分区)。
- 适用场景:
- 安装操作系统(如
/
根分区、/boot
引导分区)。 - 存储重要数据(如单独的数据盘)。
- 安装操作系统(如
扩展分区(Extended Partition)
- 定义:一种特殊的分区,本身不能直接存储数据,而是用来 容纳逻辑分区(Logical Partition)。
- 特点:
- 一个硬盘 最多只能有 1 个扩展分区(MBR 限制)。
- 扩展分区 不能直接使用,必须在其内部创建 逻辑分区 才能存储数据。
- 逻辑分区的编号从
5
开始(如/dev/sda5
、/dev/sda6
)。 - 突破 4 个分区的限制:通过扩展分区 + 逻辑分区,可以创建 超过 4 个分区。
- 适用场景:
- 当需要 超过 4 个分区 时(如多个数据盘)。
- 不需要直接启动系统,仅用于存储数据。
(四)、分区过程
分区
root@xun-virtual-machine:~# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.37.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.
Created a new DOS disklabel with disk identifier 0xf4811a33.
Command (m for help): m
Help:
DOS (MBR)
a toggle a bootable flag
b edit nested BSD disklabel
c toggle the dos compatibility flag
Generic
d delete a partition
F list free unpartitioned space
l list known partition types
n add a new partition
p print the partition table
t change a partition type
v verify the partition table
i print information about a partition
Misc
m print this menu
u change display/entry units
x extra functionality (experts only)
Script
I load disk layout from sfdisk script file
O dump disk layout to sfdisk script file
Save & Exit
w write table to disk and exit
q quit without saving changes
Create a new label
g create a new empty GPT partition table
G create a new empty SGI (IRIX) partition table
o create a new empty DOS partition table
s create a new empty Sun partition table
Command (m for help):
#打印当前分区
Command (m for help): p
Disk /dev/sdb: 20 GiB, 21474836480 bytes, 41943040 sectors
Disk model: VMware Virtual S
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: 0xf4811a33
#创建新分区
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
#回车默认从1开始
Partition number (1-4, default 1):
#回车默认从当前扇区开始
First sector (2048-41943039, default 2048):
# +2G 给主分区2G内存
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-41943039, default 41943039): +2G
Created a new partition 1 of type 'Linux' and of size 2 GiB.
#打印当前分区
Command (m for help): p
Disk /dev/sdb: 20 GiB, 21474836480 bytes, 41943040 sectors
Disk model: VMware Virtual S
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: 0xf4811a33
Device Boot Start End Sectors Size Id Type
/dev/sdb1 2048 4196351 4194304 2G 83 Linux
#创建新分区
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
#回车默认2
Partition number (2-4, default 2):
#回车默认从当前扇区开始
First sector (4196352-41943039, default 4196352):
#回车默认将剩余内容全部给扩建分区
Last sector, +/-sectors or +/-size{K,M,G,T,P} (4196352-41943039, default 41943039):
Created a new partition 2 of type 'Extended' and of size 18 GiB.
#打印当前分区
Command (m for help): p
Disk /dev/sdb: 20 GiB, 21474836480 bytes, 41943040 sectors
Disk model: VMware Virtual S
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: 0xf4811a33
Device Boot Start End Sectors Size Id Type
/dev/sdb1 2048 4196351 4194304 2G 83 Linux
/dev/sdb2 4196352 41943039 37746688 18G 5 Extended
#新建分区(主分区和扩展分区名额已用完(MBR 限制),只能在扩展分区内创建逻辑分区)
Command (m for help): n
All space for primary partitions is in use.
Adding logical partition 5
#回车从当前连续扇区开始
First sector (4198400-41943039, default 4198400):
#给第一个逻辑分区分10G内存
Last sector, +/-sectors or +/-size{K,M,G,T,P} (4198400-41943039, default 41943039): +10G
Created a new partition 5 of type 'Linux' and of size 10 GiB.
#打印
Command (m for help): p
Disk /dev/sdb: 20 GiB, 21474836480 bytes, 41943040 sectors
Disk model: VMware Virtual S
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: 0xf4811a33
Device Boot Start End Sectors Size Id Type
/dev/sdb1 2048 4196351 4194304 2G 83 Linux
/dev/sdb2 4196352 41943039 37746688 18G 5 Extended
/dev/sdb5 4198400 25169919 20971520 10G 83 Linux
#新建分区
Command (m for help): n
All space for primary partitions is in use.
Adding logical partition 6
First sector (25171968-41943039, default 25171968):
#回车将剩余内存全部给第二个逻辑分区
Last sector, +/-sectors or +/-size{K,M,G,T,P} (25171968-41943039, default 41943039):
Created a new partition 6 of type 'Linux' and of size 8 GiB.
Command (m for help): p
Disk /dev/sdb: 20 GiB, 21474836480 bytes, 41943040 sectors
Disk model: VMware Virtual S
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: 0xf4811a33
Device Boot Start End Sectors Size Id Type
/dev/sdb1 2048 4196351 4194304 2G 83 Linux
/dev/sdb2 4196352 41943039 37746688 18G 5 Extended
/dev/sdb5 4198400 25169919 20971520 10G 83 Linux
/dev/sdb6 25171968 41943039 16771072 8G 83 Linux
#保存分区并退出
Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
#查看分区信息
root@xun-virtual-machine:~# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
loop0 7:0 0 74.3M 1 loop /snap/core22/1612
loop1 7:1 0 4K 1 loop /snap/bare/5
loop2 7:2 0 271.2M 1 loop /snap/firefox/4848
loop3 7:3 0 505.1M 1 loop /snap/gnome-42-2204/176
loop4 7:4 0 91.7M 1 loop /snap/gtk-common-themes/1535
loop5 7:5 0 12.9M 1 loop /snap/snap-store/1113
loop6 7:6 0 38.8M 1 loop /snap/snapd/21759
loop7 7:7 0 500K 1 loop /snap/snapd-desktop-integration/178
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 1M 0 part
├─sda2 8:2 0 513M 0 part /boot/efi
└─sda3 8:3 0 19.5G 0 part /
sdb 8:16 0 20G 0 disk
├─sdb1 8:17 0 2G 0 part
├─sdb2 8:18 0 1K 0 part
├─sdb5 8:21 0 10G 0 part
└─sdb6 8:22 0 8G 0 part
sr0 11:0 1 4.4G 0 rom /media/xun/Ubuntu 22.04.5 LTS amd64
#更新分区信息
root@xun-virtual-machine:~# partx /dev/sdb
NR START END SECTORS SIZE NAME UUID
1 2048 4196351 4194304 2G f4811a33-01
2 4196352 41943039 37746688 18G f4811a33-02
5 4198400 25169919 20971520 10G f4811a33-05
6 25171968 41943039 16771072 8G f4811a33-06
#分区类型是dos
root@xun-virtual-machine:~# fdisk -l /dev/sdb
Disk /dev/sdb: 20 GiB, 21474836480 bytes, 41943040 sectors
Disk model: VMware Virtual S
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: 0xf4811a33
Device Boot Start End Sectors Size Id Type
/dev/sdb1 2048 4196351 4194304 2G 83 Linux
/dev/sdb2 4196352 41943039 37746688 18G 5 Extended
/dev/sdb5 4198400 25169919 20971520 10G 83 Linux
/dev/sdb6 25171968 41943039 16771072 8G 83 Linux
parted指令修改磁盘分区表类型
(通常用gdisk修改磁盘表类型)
mklabel gpt 会清空分区,需用户手动备份,且仅创建 GPT 标签
先删除原dos类型磁盘里的分区
root@xun-virtual-machine:~# fdisk /dev/sdb
Device Boot Start End Sectors Size Id Type
/dev/sdb1 2048 4196351 4194304 2G 83 Linux
/dev/sdb2 4196352 41943039 37746688 18G 5 Extended
/dev/sdb5 4198400 25169919 20971520 10G 83 Linux
/dev/sdb6 25171968 41943039 16771072 8G 83 Linux
root@xun-virtual-machine:~# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.37.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Command (m for help): m
Help:
DOS (MBR)
a toggle a bootable flag
b edit nested BSD disklabel
c toggle the dos compatibility flag
Generic
d delete a partition
F list free unpartitioned space
l list known partition types
n add a new partition
p print the partition table
t change a partition type
v verify the partition table
i print information about a partition
Misc
m print this menu
u change display/entry units
x extra functionality (experts only)
Script
I load disk layout from sfdisk script file
O dump disk layout to sfdisk script file
Save & Exit
w write table to disk and exit
q quit without saving changes
Create a new label
g create a new empty GPT partition table
G create a new empty SGI (IRIX) partition table
o create a new empty DOS partition table
s create a new empty Sun partition table
#分别删除6、5、2、1分区
Command (m for help): d
Partition number (1,2,5,6, default 6):
Partition 6 has been deleted.
Command (m for help): d
Partition number (1,2,5, default 5):
Partition 5 has been deleted.
Command (m for help): d
Partition number (1,2, default 2):
Partition 2 has been deleted.
Command (m for help): d
Selected partition 1
Partition 1 has been deleted.
Command (m for help): p
Disk /dev/sdb: 20 GiB, 21474836480 bytes, 41943040 sectors
Disk model: VMware Virtual S
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: 0xf4811a33
Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
root@xun-virtual-machine:~# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
loop0 7:0 0 74.3M 1 loop /snap/core22/1612
loop1 7:1 0 4K 1 loop /snap/bare/5
loop2 7:2 0 271.2M 1 loop /snap/firefox/4848
loop3 7:3 0 505.1M 1 loop /snap/gnome-42-2204/176
loop4 7:4 0 91.7M 1 loop /snap/gtk-common-themes/1535
loop5 7:5 0 12.9M 1 loop /snap/snap-store/1113
loop6 7:6 0 38.8M 1 loop /snap/snapd/21759
loop7 7:7 0 500K 1 loop /snap/snapd-desktop-integration/178
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 1M 0 part
├─sda2 8:2 0 513M 0 part /boot/efi
└─sda3 8:3 0 19.5G 0 part /
sdb 8:16 0 20G 0 disk
sr0 11:0 1 4.4G 0 rom /media/xun/Ubuntu 22.04.5 LTS amd64
用parted命令将磁盘类型修改为gpt
root@xun-virtual-machine:~# parted /dev/sdb
GNU Parted 3.4
Using /dev/sdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
#打印分区表信息和类型
(parted) print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system Flags
#修改
(parted) mklabel gpt
Warning: The existing disk label on /dev/sdb will be destroyed and all data on this disk will be lost. Do you
want to continue?
Yes/No? y
#打印分区表信息和类型
(parted) 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
(parted)
用gdisk指令修改磁盘分区
gdisk命令将dos类型转为gpt类型会保留分区数据,精细操作、无损转换
root@xun-virtual-machine:~# gdisk -l /dev/sdb
GPT fdisk (gdisk) version 1.0.8
Partition table scan:
MBR: protective
BSD: not present
APM: not present
GPT: present
Found valid GPT with protective MBR; using GPT.
Disk /dev/sdb: 41943040 sectors, 20.0 GiB
Model: VMware Virtual S
Sector size (logical/physical): 512/512 bytes
Disk identifier (GUID): 03BE9DAE-41A2-4DCB-9E45-6B8F352D6BAA
Partition table holds up to 128 entries
Main partition table begins at sector 2 and ends at sector 33
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
#用gdisk命令给gpt类型磁盘分区
root@xun-virtual-machine:~# gdisk /dev/sdb
GPT fdisk (gdisk) version 1.0.8
Partition table scan:
MBR: protective
BSD: not present
APM: not present
GPT: present
Found valid GPT with protective MBR; using GPT.
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
#添加新分区
Command (? for help): n
#回车 默认
Partition number (1-128, default 1):
#回车 默认
First sector (34-41943006, default = 2048) or {+-}size{KMGTP}:
#分2GB
Last sector (2048-41943006, default = 41943006) or {+-}size{KMGTP}: +2G
Current type is 8300 (Linux filesystem)
#回车 默认
Hex code or GUID (L to show codes, Enter = 8300):
Changed type of partition to 'Linux filesystem'
#打印分区信息
Command (? for help): p
Disk /dev/sdb: 41943040 sectors, 20.0 GiB
Model: VMware Virtual S
Sector size (logical/physical): 512/512 bytes
Disk identifier (GUID): 03BE9DAE-41A2-4DCB-9E45-6B8F352D6BAA
Partition table holds up to 128 entries
Main partition table begins at sector 2 and ends at sector 33
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
#添加新分区
Command (? for help): n
#回车 默认
Partition number (2-128, default 2):
#回车 默认
First sector (34-41943006, default = 4196352) or {+-}size{KMGTP}:
#分5GB
Last sector (4196352-41943006, default = 41943006) or {+-}size{KMGTP}: +5G
#回车 默认
Current type is 8300 (Linux filesystem)
Hex code or GUID (L to show codes, Enter = 8300):
Changed type of partition to 'Linux filesystem'
#打印分区信息
Command (? for help): p
Disk /dev/sdb: 41943040 sectors, 20.0 GiB
Model: VMware Virtual S
Sector size (logical/physical): 512/512 bytes
Disk identifier (GUID): 03BE9DAE-41A2-4DCB-9E45-6B8F352D6BAA
Partition table holds up to 128 entries
Main partition table begins at sector 2 and ends at sector 33
First usable sector is 34, last usable sector is 41943006
Partitions will be aligned on 2048-sector boundaries
Total free space is 27262909 sectors (13.0 GiB)
Number Start (sector) End (sector) Size Code Name
1 2048 4196351 2.0 GiB 8300 Linux filesystem
2 4196352 14682111 5.0 GiB 8300 Linux filesystem
#将剩余内存给第三个分区
Command (? for help): n
Partition number (3-128, default 3):
First sector (34-41943006, default = 14682112) or {+-}size{KMGTP}:
Last sector (14682112-41943006, default = 41943006) or {+-}size{KMGTP}:
Current type is 8300 (Linux filesystem)
Hex code or GUID (L to show codes, Enter = 8300):
Changed type of partition to 'Linux filesystem'
#打印分区信息
Command (? for help): p
Disk /dev/sdb: 41943040 sectors, 20.0 GiB
Model: VMware Virtual S
Sector size (logical/physical): 512/512 bytes
Disk identifier (GUID): 03BE9DAE-41A2-4DCB-9E45-6B8F352D6BAA
Partition table holds up to 128 entries
Main partition table begins at sector 2 and ends at sector 33
First usable sector is 34, last usable sector is 41943006
Partitions will be aligned on 2048-sector boundaries
Total free space is 2014 sectors (1007.0 KiB)
Number Start (sector) End (sector) Size Code Name
1 2048 4196351 2.0 GiB 8300 Linux filesystem
2 4196352 14682111 5.0 GiB 8300 Linux filesystem
3 14682112 41943006 13.0 GiB 8300 Linux filesystem
#保存退出
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.
root@xun-virtual-machine:~# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
loop0 7:0 0 74.3M 1 loop /snap/core22/1612
loop1 7:1 0 4K 1 loop /snap/bare/5
loop2 7:2 0 271.2M 1 loop /snap/firefox/4848
loop3 7:3 0 505.1M 1 loop /snap/gnome-42-2204/176
loop4 7:4 0 91.7M 1 loop /snap/gtk-common-themes/1535
loop5 7:5 0 12.9M 1 loop /snap/snap-store/1113
loop6 7:6 0 38.8M 1 loop /snap/snapd/21759
loop7 7:7 0 500K 1 loop /snap/snapd-desktop-integration/178
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 1M 0 part
├─sda2 8:2 0 513M 0 part /boot/efi
└─sda3 8:3 0 19.5G 0 part /
sdb 8:16 0 20G 0 disk
├─sdb1 8:17 0 2G 0 part
├─sdb2 8:18 0 5G 0 part
└─sdb3 8:19 0 13G 0 part
sr0 11:0 1 4.4G 0 rom /media/xun/Ubuntu 22.04.5 LTS amd64
root@xun-virtual-machine:~#
用gdisk命令将gpt改为dos
root@xun-virtual-machine:~# gdisk /dev/sdb
GPT fdisk (gdisk) version 1.0.8
Partition table scan:
MBR: protective
BSD: not present
APM: not present
GPT: present
Found valid GPT with protective MBR; using GPT.
# 进入恢复/转换模式
Command (? for help): r
# 将 GPT 转换为 MBR
Recovery/transformation command (? for help): g
#保存退出
MBR command (? for help): w
Converted 3 partitions. Finalize and exit? (Y/N): y
GPT data structures destroyed! You may now partition the disk using fdisk or
other utilities.
root@xun-virtual-machine:~# fdisk -l /dev/sdb
Disk /dev/sdb: 20 GiB, 21474836480 bytes, 41943040 sectors
Disk model: VMware Virtual S
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: 0x00000000
Device Boot Start End Sectors Size Id Type
/dev/sdb1 2048 4196351 4194304 2G 83 Linux
/dev/sdb2 4196352 14682111 10485760 5G 83 Linux
/dev/sdb3 14682112 41943006 27260895 13G 83 Linux
五、主流文件系统
1、Linux 主流文件系统
文件系统 | 特点 | 适用场景 |
---|---|---|
ext4 | 默认文件系统,稳定高效,支持日志、大文件(16TB)、快照(实验性) | 通用 Linux 系统 |
XFS | 高性能(尤其大文件并发读写),支持在线扩容、崩溃恢复快 | 服务器、数据库、大文件存储 |
Btrfs | 支持写时复制(CoW)、快照、压缩、RAID 集成,但稳定性待优化 | 实验性需求(如快照备份) |
ZFS | 高级功能(数据校验、压缩、RAID-Z),但内存占用高,需额外安装 | NAS/企业存储(非默认内核支持) |
2、Windows 主流文件系统
文件系统 | 特点 | 适用场景 |
---|---|---|
NTFS | 支持 ACL 权限、加密、压缩、日志,兼容大文件(16EB) | 现代 Windows 系统盘/数据盘 |
FAT32 | 兼容性强(UEFI/旧设备),但单文件 ≤4GB,无权限控制 | U 盘、SD 卡、跨平台传输 |
exFAT | 改进版 FAT32,支持大文件(16EB),无日志,适合闪存 | 大容量移动设备(如 SSD 移动硬盘) |
3、macOS 主流文件系统
文件系统 | 特点 | 适用场景 |
---|---|---|
APFS | 优化 SSD 性能,支持快照、加密、空间共享(多个卷共用存储池) | macOS 10.13+ 系统盘 |
HFS+ | 旧版文件系统,兼容老设备,逐渐被淘汰 | 旧版 macOS(10.12 及更早) |
4、跨平台/专用文件系统
文件系统 | 特点 | 适用场景 |
---|---|---|
FAT32 | 几乎全平台兼容(Win/Linux/macOS/游戏机),但功能受限 | U 盘、相机 SD 卡 |
exFAT | 跨平台 + 大文件支持,无日志 | 大容量移动设备(如外接硬盘) |
NFS | 网络文件系统,支持远程挂载 | Linux/Unix 服务器共享 |
SMB/CIFS | Windows 网络共享协议,跨平台访问 | Windows/Linux/macOS 文件共享 |
六、mkfs格式化文件系统
#格式化之前,将/dev/sdb分区重新分为一个分区
#输入mkfs按Tab 查看可格式化的文件类型
root@xun-virtual-machine:~# mkfs
mkfs.bfs mkfs.ext2 mkfs.ext4 mkfs.minix mkfs.ntfs
mkfs mkfs.cramfs mkfs.ext3 mkfs.fat mkfs.msdos mkfs.vfat
#下载xfs文件类型
root@xun-virtual-machine:~# apt install xfsprogs
root@xun-virtual-machine:~# mkfs.xfs /dev/sdb1
meta-data=/dev/sdb1 isize=512 agcount=4, agsize=1310656 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=1, sparse=1, rmapbt=0
= reflink=1 bigtime=0 inobtcount=0
data = bsize=4096 blocks=5242624, 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@xun-virtual-machine:~# lsblk -f
NAME FSTYPE FSVER LABEL UUID FSAVAIL FSUSE% MOUNTPOINTS
loop0 squashf 4.0 0 100% /snap/core22/1612
loop1 squashf 4.0 0 100% /snap/bare/5
loop2 squashf 4.0 0 100% /snap/firefox/4848
loop3 squashf 4.0 0 100% /snap/gnome-42-2204/176
loop4 squashf 4.0 0 100% /snap/gtk-common-themes/1535
loop5 squashf 4.0 0 100% /snap/snap-store/1113
loop6 squashf 4.0 0 100% /snap/snapd/21759
loop7 squashf 4.0 0 100% /snap/snapd-desktop-integration/178
sda
├─sda1
├─sda2 vfat FAT32 93B7-EECE 505.9M 1% /boot/efi
└─sda3 ext4 1.0 c1b5a816-3361-4198-824d-9eccd35fb055 6.1G 63% /
sdb
└─sdb1 xfs ec06f339-9c80-49fa-8c4d-14726b8d274a
sr0 iso9660 Joliet Extens Ubuntu 22.04.5 LTS amd64 2024-09-11-14-37-52-00 0 100% /media/xun/Ubuntu 22.04.5 LTS amd64
查看xfs文件系统信息
root@xun-virtual-machine:~# xfs_info /dev/sdb1
meta-data=/dev/sdb1 isize=512 agcount=4, agsize=1310656 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=1, sparse=1, rmapbt=0
= reflink=1 bigtime=0 inobtcount=0
data = bsize=4096 blocks=5242624, 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
参数 | 值/状态 | 说明 |
---|---|---|
isize | 512 |
inode 大小(字节),默认 512 字节足够。 |
agcount | 4 |
Allocation Group(分配组)数量,影响并行性能。 |
agsize | 1310656 blks |
每个分配组的块数(块大小 bsize=4096 ,即 4KiB)。 |
crc | 1 (enabled) |
启用元数据校验和,提高数据完整性。 |
finobt | 1 (enabled) |
启用空闲 inode B+树,加速文件创建。 |
reflink | 1 (enabled) |
支持写时复制(CoW),适合快照和备份。 |
log | internal |
日志(journal)内置于文件系统中,无需额外分区。 |
bsize | 4096 |
数据块大小(4KiB),与大多数 SSD 的物理块大小对齐。 |
sectsz | 512 |
磁盘扇区大小(通常为 512B 或 4K)。 |
七、mount指令临时和永久挂载
磁盘分区有文件系统才能挂载目录,之后就能用该分区存储数据了
#临时挂载
# mount命令 磁盘分区 需要挂载到的目录
root@xun-virtual-machine:~# mount /dev/sdb1 /a1/my_sdb1/
#永久挂载
#编辑 添加
/dev/sdb1:设备路径
/a1/my_sdb1:挂载点目录
xfs:文件系统类型
defaults:挂载选项(包括 rw,relatime 等)
0 0:不备份(dump)和不检查(fsck)
root@xun-virtual-machine:~# vim /etc/fstab
/dev/sdb1 /a1/my_sdb1 xfs defaults 0 0
#验证当前挂载状态
root@xun-virtual-machine:/a1/my_sdb1# mount | grep /dev/sdb1
/dev/sdb1 on /a1/my_sdb1 type xfs (rw,relatime,attr2,inode64,logbufs=8,logbsize=32k,noquota)
#测试永久挂载
#卸载并重新挂载
root@xun-virtual-machine:/a1# umount /dev/sdb1
#重新挂载所有 /etc/fstab 中的条目
root@xun-virtual-machine:/a1# mount -a
#验证挂载
root@xun-virtual-machine:/a1# ls /a1/my_sdb1/
test.txt
#检查挂载的文件系统类型和空间
root@xun-virtual-machine:/a1# df -hT | grep /dev/sdb1
/dev/sdb1 xfs 20G 175M 20G 1% /a1/my_sdb1
取消挂载时的报错
在挂载目录
#此时退出当前目录即可取消挂载
root@xun-virtual-machine:/a1/my_sdb1# umount /a1/my_sdb1/
umount: /a1/my_sdb1/: target is busy.
不在挂载目录
#有进程正在访问挂载点
root@xun-virtual-machine:~# umount /a1/my_sdb1/
umount: /a1/my_sdb1/: target is busy.
用lsof指令查看目录下的进程
root@xun-virtual-machine:~# lsof /a1/my_sdb1/
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
bash 2486 root cwd DIR 8,17 22 128 /a1/my_sdb1
root@xun-virtual-machine:~#
延迟卸载
#如果无法立即终止进程,可尝试延迟卸载(内核会在空闲时自动卸载)
umount -l /a1/my_sdb
立即卸载
#强制结束占用进程
sudo kill -9 <PID>
八、dd指令
场景 | 命令示例 |
---|---|
磁盘备份 | dd if=/dev/sda of=/backup.img bs=4M status=progress |
磁盘克隆 | dd if=/dev/sdb of=/dev/sdc bs=4M status=progress |
创建空文件 | dd if=/dev/zero of=file.bin bs=1G count=1 |
安全擦除 | dd if=/dev/urandom of=/dev/sdb bs=1M status=progress |
测试速度 | dd if=/dev/zero of=testfile bs=1G count=1 oflag=direct |
找到磁盘中占内存的大文件并删除
#在a1目录下创建一个3G的空文件
root@xun-virtual-machine:/a1# dd if=/dev/zero of=/a1/3Gfile.txt bs=1G count=3
3+0 records in
3+0 records out
3221225472 bytes (3.2 GB, 3.0 GiB) copied, 11.0103 s, 293 MB/s
#查看磁盘信息/dev/sda3 20G 15G 3.1G 83% 使用83%
root@xun-virtual-machine:/a1# df -h
Filesystem Size Used Avail Use% Mounted on
tmpfs 387M 1.7M 386M 1% /run
/dev/sda3 20G 15G 3.1G 83% /
tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs 5.0M 4.0K 5.0M 1% /run/lock
/dev/sdb1 20G 175M 20G 1% /a1/my_sdb1
/dev/sda2 512M 6.1M 506M 2% /boot/efi
tmpfs 387M 92K 387M 1% /run/user/1000
/dev/sr0 4.5G 4.5G 0 100% /media/xun/Ubuntu 22.04.5 LTS amd64
#筛选找到大于2g的文件
root@xun-virtual-machine:/a1# find / -size +2G -type f
/swapfile
/a1/3Gfile.txt
/media/xun/Ubuntu 22.04.5 LTS amd64/casper/filesystem.squashfs
/proc/kcore
#删除
root@xun-virtual-machine:/a1# rm 3Gfile.txt
#/dev/sda3 20G 12G 6.1G 67% 使用67%
root@xun-virtual-machine:/a1# df -h
Filesystem Size Used Avail Use% Mounted on
tmpfs 387M 1.7M 386M 1% /run
/dev/sda3 20G 12G 6.1G 67% /
tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs 5.0M 4.0K 5.0M 1% /run/lock
/dev/sdb1 20G 175M 20G 1% /a1/my_sdb1
/dev/sda2 512M 6.1M 506M 2% /boot/efi
tmpfs 387M 92K 387M 1% /run/user/1000
/dev/sr0 4.5G 4.5G 0 100% /media/xun/Ubuntu 22.04.5 LTS amd64