惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

小众软件
小众软件
V
Visual Studio Blog
博客园 - 三生石上(FineUI控件)
Last Week in AI
Last Week in AI
Blog — PlanetScale
Blog — PlanetScale
爱范儿
爱范儿
J
Java Code Geeks
A
About on SuperTechFans
F
Fortinet All Blogs
B
Blog
aimingoo的专栏
aimingoo的专栏
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Engineering at Meta
Engineering at Meta
Y
Y Combinator Blog
有赞技术团队
有赞技术团队
G
Google Developers Blog
Apple Machine Learning Research
Apple Machine Learning Research
V
V2EX
博客园_首页
博客园 - 叶小钗
罗磊的独立博客
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
D
Docker
云风的 BLOG
云风的 BLOG

DigVPS

NiiHost Tokyonline RakSmart 野草云 速维云 KFCloud Evoxt(益沃斯) VPCKR KFCHost 干杯 Cloud 分流测试 - DigVPS - 专注服务器测评,总有一款服务器适合您! 全球延迟测试 - DigVPS - 专注服务器测评,总有一款服务器适合您! 网速测试 - DigVPS - 专注服务器测评,总有一款服务器适合您! DNS 出口检测 - DigVPS - 专注服务器测评,总有一款服务器适合您! WebRTC 测试 - DigVPS - 专注服务器测评,总有一款服务器适合您! 网络连通性 - DigVPS - 专注服务器测评,总有一款服务器适合您! Whois 查询 - DigVPS - 专注服务器测评,总有一款服务器适合您! 云悠(YUNYOO) Skyline Connect PanstarCloud(小妈云) 蔭雲(YINNET) OrangeVPS 阿里云 IP 查询 - DigVPS - 专注服务器测评,总有一款服务器适合您! DigitalVirt Ciallo Networks QQG BUG NET 腾讯云(Tencent Cloud) TNAHosting
磁盘
2026-09-09 · via DigVPS

磁盘信息查看

smartctl

terminal

apt install smartmontools
smartctl -a /dev/xxx

lsblk

terminal

lsblk -o NAME,HCTL,TRAN,SIZE,MODEL,SERIAL

udevadm

terminal

udevadm info --query=all --name=/dev/sda | grep ID_

NVMe 磁盘

terminal

apt install nvme-cli
nvme list

lshw

terminal

sudo apt install lshw -y
sudo lshw -class disk -class storage

fdisk 磁盘管理

结合视频食用: YouTube 哔哩哔哩

环境信息

查看当前磁盘:

示例输出:

terminal

Disk /dev/vda: 160 GiB
/dev/vda1 160G Linux filesystem

Disk /dev/vdb: 3.63 TiB

我们来格式化 /dev/vdb 并挂在到 /root 目录

创建分区

/dev/vdb 还没有分区,需要先创建。

执行:

输入:

terminal

# 创建 GPT 分区表  
Command (m for help): g
Created a new GPT disklabel (GUID: C60C8BFE-FF2F-45B7-8E85-84351C34A09C).

# 创建新分区  
Command (m for help): n
## 默认分区号  
Partition number (1-128, default 1):
## 默认起始
First sector (2048-7801405406, default 2048):
## 使用全部空间 
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-7801405406, default 7801403391):

Created a new partition 1 of type 'Linux filesystem' and of size 3.6 TiB.

# 写入
Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

完成后查看:

应该看到:

格式化文件系统

terminal

root@g3sstd8:~# mkfs.ext4 /dev/vdb1
mke2fs 1.47.2 (1-Jan-2025)
Discarding device blocks: done
Creating filesystem with 975175168 4k blocks and 243793920 inodes
Filesystem UUID: 640f14b7-e345-42b6-921b-906902897108
Superblock backups stored on blocks:
    32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
    4096000, 7962624, 11239424, 20480000, 23887872, 71663616, 78675968,
    102400000, 214990848, 512000000, 550731776, 644972544

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

备份 /root 目录(可选)

我需要挂载到 /root 目录,因为原来有文件,挂载 /root 会覆盖原目录,所以先备份。

备份:

terminal

mkdir /root_backup
cp -a /root/. /root_backup/

或者:

terminal

rsync -av /root/ /root_backup/

临时挂载测试

先测试挂载:

terminal

mount /dev/vdb1 /root

查看:

应该看到:

terminal

/dev/vdb1 3.6T /root

恢复 /root 数据(可选)

terminal

rsync -av /root_backup/ /root/

设置开机自动挂载

获取 UUID:

terminal

root@g3sstd8:~# blkid /dev/vdb1
/dev/vdb1: UUID="640f14b7-e345-42b6-921b-906902897108" BLOCK_SIZE="4096" TYPE="ext4" PARTUUID="27d01d8a-563d-4e3e-9914-ffb3ff13b7f8"

编辑 /etc/fstab

加入:

terminal

### dump 备份设置
# 
# fstab 的倒数第二个字段用于指定是否允许 dump 工具备份该文件系统:
# 
# - 0:不进行 dump 备份,也是现代 Linux 系统中最常见的配置。
# - 1:允许 dump 工具备份该文件系统。
# 
# ### fsck 检查顺序
# 
# fstab 的最后一个字段用于指定系统启动时 fsck 检查文件系统的顺序:
# 
# - 0:启动时不检查该文件系统。
# - 1:优先检查,通常仅用于系统根分区 /。
# - 2:在根分区检查完成后再检查,通常用于其他本地磁盘或数据分区。

例如,配置末尾的 `0 2` 表示不使用 `dump` 备份,并在启动时将该文件系统作为非根分区进行检查。
UUID=640f14b7-e345-42b6-921b-906902897108  /root  ext4  defaults  0  2

测试 fstab

如果没有报错说明配置正确。

再次确认:

重启验证

重启后查看:

应该看到:

terminal

/dev/vdb1 /root

说明挂载成功。

释放空间(可选)

如果磁盘主要用于存储文件,可以关闭 ext4 预留空间:

terminal

tune2fs -m 0 /dev/vdb1

默认 ext4 会预留 5% 空间。

例如:

3.6TB × 5% ≈ 180GB

关闭后可以释放空间。

mdadm 创建管理 RAID

mdadm(Multiple Devices Administration 多设备管理)是 Linux 下用于管理软件 RAID 的工具

terminal

# 安装
apt install mdadm

RAID 0

查看磁盘

terminal

fdisk -l | grep /dev/sd

创建 RAID 0

terminal

mdadm -C /dev/md0 -l 0 -n 2 /dev/sd{b,c}

查看状态

terminal

cat /proc/mdstat
mdadm -detail /dev/md0

格式化&挂载

传送门

删除做好的 RAID 0

terminal

# 卸载磁盘
umount /dev/md0

# 停止 RAID
mdadm --stop /dev/md0

# 释放磁盘
mdadm --zero-superblock /dev/sd{b,c}

RAID 1

创建 RAID 1

terminal

mdadm -C /dev/md1 -l 1 -n 2 /dev/sd{b,c}

添加新的硬盘

terminal

mdadm -a /dev/md1 /dev/sdd

设置故障硬盘

terminal

mdadm -f /dev/md1 /dev/sdb

其他操作同上

RAID 5

创建 RAID 5

terminal

mdadm -C /dev/md5 -l 5 -n 3 /dev/sd{b,c,d}

其他操作同上

LVM(Logical Volume Manager)

基本概念

  • 物理磁盘 (Physical Volume, PV):/dev/xxx 通常会被初始化为 LVM 的物理卷。
  • 卷组 (Volume Group, VG):多个 PV 可以组合成一个卷组(比如 vg0),它就像一个大的“存储池”。
  • 逻辑卷 (Logical Volume, LV):在卷组里面,你可以切分出多个逻辑卷,比如:/dev/mapper/vg0-root → 根目录的逻辑卷,大小 3.63 TiB,/dev/mapper/vg0-swap → swap 交换分区的逻辑卷,大小 4 GiB。它们的路径一般出现在 /dev/mapper/ 下,也会有 /dev/vg0/root、/dev/vg0/swap 这样的别名。

terminal

/dev/nvme0n1p3  (LVM PV)
   vg0 (卷组)
      ├── lv_root (/dev/mapper/vg0-root, ~3.63T)
      └── lv_swap (/dev/mapper/vg0-swap, 4G)

要查看逻辑卷 (LV) 的类型

terminal

lvs -a -o +devices,lv_attr

terminal

# -wi- → 普通卷 (linear)
# rwi- → RAID1
# rwi-a-r--- → active RAID1
# rwi-a-s--- → Striped (RAID0)

root@digvps:~# lvs -a -o +devices,lv_attr
  LV     VG        Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert Devices          Attr
  root   digvps-vg -wi-ao---- 59.79g                                                     /dev/sda5(0)     -wi-ao----
  swap_1 digvps-vg -wi-ao----  3.25g                                                     /dev/sda5(15307) -wi-ao----

Linear(普通卷)扩展

确认磁盘是否已分区

通常裸盘可以直接用作 PV(Physical Volume)。 如果之前分过区,可以先清理:

terminal

wipefs -a /dev/nvme1n1

创建 LVM 物理卷 (PV)

terminal

pvcreate /dev/nvme1n1

检查

扩展现有卷组 (VG)

terminal

vgextend vg0 /dev/nvme1n1

检查

扩展逻辑卷 (LV)

假设你有两个 LV:root 和 swap。 一般是扩展 root,例如把所有新空间都加到 root:

terminal

lvextend -l +100%FREE /dev/vg0/root

扩展文件系统

terminal

# 如果是 ext4:
resize2fs /dev/vg0/root

# 如果是 xfs
xfs_growfs /

检查结果

应该能看到 root 逻辑卷容量扩展了。

常用命令

磁盘空间不足,需快速定位日志目录

terminal

du -x --max-depth=1 /|sort -k1 -nr

# du                # disk usage,统计磁盘使用量
# -x                # 只统计同一文件系统,跨挂载点的目录不算
# --max-depth=1     # 只显示指定目录下一层子目录的大小
# /                 # 要统计的目录,这里是根目录
#
# sort              # 对输出结果排序
# -k1               # 以第 1 列(du 输出的大小)作为排序关键字
# -n                # 按数值大小排序(numeric sort)
# -r                # 逆序排序(从大到小)

系统产生很多碎片文件,导致 inode 资源不足

terminal

find -type f | awk -F / -v OFS=/ '{$NF="";dir[$0]++}END{for(i in dir)print dir[i]""i}' | sort -k1 -nr | head

# find           # 查找文件
# -type f        # 只查找普通文件,不包含目录
#
# awk            # 文本处理工具
# -F /           # 指定输入字段分隔符为 “/”
# -v OFS=/       # 指定输出字段分隔符为 “/”
# $NF            # NF = 字段总数,$NF 表示最后一个字段(文件名)
# $NF=""         # 删除最后一个字段,只保留目录路径
# dir[$0]++      # 以目录路径为键,统计该目录下文件数量
# END{...}       # 在输入处理完成后执行,输出统计结果
# print dir[i]""i  # 输出格式:文件数 + 目录路径
#
# sort           # 排序
# -k1            # 按第 1 列(文件数)排序
# -n             # 数字大小排序
# -r             # 逆序,从大到小
#
# head           # 只显示前 10 行(文件最多的 10 个目录)

磁盘数据恢复

删除文件时,文件正在被进程使用的恢复方式

Linux 中每个文件都有 2 个 link 计数器:i_counti_nlink,Linux 只有在这两个计数器都清零的时候才会被系统认为是删除的,假设此时删除的文件有进程在使用,那么(i_count)数值不为 0

这个时候看似文件被删除,但是操作系统还能便捷恢复

创建文件

terminal

echo "删除文件演示" > delete.txt
cat delete.txt

持续进程读取

terminal

tail -f delete.txt

删除文件

terminal

rm -rf delete.txt

找到使用文件的进程

terminal

lsof |grep delete.txt
tail      5900                            root    3r      REG              259,5       19         15 /root/delete.txt (deleted)

找到文件句柄

terminal

cd /proc/5900/fd
ls -l
total 0
lrwx------ 1 root root 64 Jul 19 22:48 0 -> /dev/pts/0
lrwx------ 1 root root 64 Jul 19 22:48 1 -> /dev/pts/0
lrwx------ 1 root root 64 Jul 19 22:48 2 -> /dev/pts/0
lr-x------ 1 root root 64 Jul 19 22:48 3 -> '/root/delete.txt (deleted)'
lr-x------ 1 root root 64 Jul 19 22:48 4 -> anon_inode:inotify

恢复

terminal

cp 3 /tmp/recover_delete.txt
cat /tmp/recover_delete.txt
删除文件演示