目录
一、磁盘结构
1.设备文件
对于Linux,一切皆文件,硬件设备同样是以文件形式呈现出来的。设备文件是操作系统中一种特殊的文件类型,其核心作用是作为硬件设备与软件(驱动程序)之间的桥梁。



添加硬盘,不重启识别
[root@rocky136 ~]# echo '- - -' > /sys/class/scsi_host/host0/scan
[root@rocky136 ~]# echo '- - -' > /sys/class/scsi_host/host1/scan
[root@rocky136 ~]# echo '- - -' > /sys/class/scsi_host/host2/scan
##但是在 ubuntu 中,该目录下的内容太多
[root@rocky-136 ~]#ls /sys/class/scsi_host
host0 host12 host16 host2 host23 host27 host30 host5 host9
host1 host13 host17 host20 host24 host28 host31 host6
host10 host14 host18 host21 host25 host29 host32 host7
host11 host15 host19 host22 host26 host3 host4 host8
#两种方法
#1.循环遍历
[root@ubuntu2204 ~]# for i in `ls /sys/class/scsi_host/`;do echo '- - -' >
/sys/class/scsi_host/$i/scan;done
#2.找出SPI总线对应的 host,只扫描该 host
[root@ubuntu2204 ~]# grep mptspi /sys/class/scsi_host/host*/proc_name
/sys/class/scsi_host/host32/proc_name:mptspi
#此处是重新扫描 SCSI 总线来添加设备,之所以是 SCSI 总线,是因为我们添加的是 SCSI 类型的硬盘
[root@ubuntu2204 ~]# echo '- - -' > /sys/class/scsi_host/host32/scan
SCSI:(small computer system interface),小型计算机系统接口,是常用且重要的数据传输协议之一,它支撑着操作系统对外部设备的i/o操作。实际应用场景中,在linux系统上添加一个新的 scsi 磁盘是不需要重启的,一般可以通过扫描发现的操作来识别就绪设备。
/sys 文件是系统是和 /proc 一样,是一个内存文件系统 ,在磁盘上并没有对应的内容。通常称其为sysfs, 这是内核 “暴露” 给用户空间的一个 驱动模型层次结构的展现。因此,host0, host1 这些 “文件” 是内核根据设备驱动程序 “发现” 的设备后在内存中创建的对应的 “文件” 和 "文件层次"。
2.硬盘类型
3.机械硬盘和固态硬盘
机械硬盘(HDD)是传统存储设备,核心结构包含盘片、磁头等精密部件,工作时盘片高速旋转,磁头以极微小间隙贴近盘面并沿半径移动来定位数据,通过电磁流改变磁极读写数据。因依赖机械运动,对环境洁净度要求苛刻,不过具有成本低、容量大的优势,适用于大规模数据存储。
固态硬盘(SSD)则采用固态电子芯片阵列,无机械结构,靠电信号直接读写数据,接口和外形与HDD兼容,性能上防震抗摔、传输速率快、功耗低、重量轻且无噪音,适合高效数据访问和移动场景,但单位容量成本高,大容量产品价格远超HDD 。

#识别SSD和HDD类型
#1表示机械,0表示SSD
[root@rocky86 ~]# lsblk -d -o name,rota
NAME ROTA
sda 1
sr0 1
nvme0n1 0
nvme0n2 0
[root@rocky86 ~]# ls /sys/block/
nvme0n1 nvme0n2 sda sr0
[root@rocky86 ~]# cat /sys/block/*/queue/rotational
0
0
1
1
[root@rocky86 ~]# cat /sys/block/sda/queue/rotational
1
#测速
[root@rocky-136 ~]#dd | hdparm -t /dev/sda
/dev/sda:
Timing buffered disk reads: 322 MB in 3.01 seconds = 107.10 MB/sec
磁盘寻址:CHS、LBA。
二、管理存储
使用磁盘空间过程:
1.设备分区
2.创建文件系统
3.挂载新的文件系统
1.磁盘分区(MBR/GPT)
1.1目的
优化I/O性能
实现磁盘空间配额限制
提高修复速度
隔离系统和程序
安装多个OS
采用不同文件系统
1.2MBR
MBR,硬盘的第一个物理扇区(0 柱面 0 磁头 1 扇区),大小 512 字节
446bytes: 主引导程序代码,用于在系统启动时查找并加载操作系统引导程序
64bytes:分区表,其中每16bytes标识一个分区
2bytes: 55AA,标识位,用于标识该扇区为有效的 MBR 扇区MBR分区中一块硬盘最多有4个主分区,也可以3主分区+1扩展(N个逻辑分区) 。
MBR分区:主和扩展分区对应的1--4,/dev/sda3,逻辑分区从5开始,/dev/sda。
#查看分区表信息
[root@rocky-136 ~]#hexdump -C -n 512 /dev/sda
备份MBR分区表,破坏后恢复
#查看分区表
[root@rocky86 ~]# fdisk -l /dev/sda
Disk /dev/sda: 200 GiB, 214748364800 bytes, 419430400 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
Disklabel type: dos
Disk identifier: 0x5b8e1003
Device Boot Start End Sectors Size Id Type
/dev/sda1 * 2048 2099199 2097152 1G 83 Linux
/dev/sda2 2099200 419430399 417331200 199G 8e Linux LVM
#备份MBR分区表
[root@rocky86 ~]# dd if=/dev/sda of=/data/dpt.img bs=1 count=64 skip=446
#64字节大小
[root@rocky86 ~]# ll /data/dpt.img
-rw-r--r-- 1 root root 64 Jul 30 11:49 /tmp/dpt.img
#查看具体内容
[root@rocky86 ~]# hexdump -vC /data/dpt.img
#备份到远端
[root@rocky86 ~]# scp /tmp/dpt.img 192.168.61.140:
#破坏MBR分区表
[root@rocky86 ~]# dd if=/dev/zero of=/dev/sda bs=1 count=64 seek=446
64+0 records in
64+0 records out
64 bytes copied, 0.00248682 s, 25.7 kB/s
#再次查看前512字节
[root@rocky86 ~]# hexdump -vC -n 512 /dev/sda
#再次查看分区表,没有分区信息
[root@rocky86 ~]# fdisk -l /dev/sda
Disk /dev/sda: 200 GiB, 214748364800 bytes, 419430400 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
Disklabel type: dos
Disk identifier: 0x3475e2b0
#但内存中还有相关信息
[root@rocky86 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 200G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 199G 0 part
├─rl-root 253:0 0 70G 0 lvm /
├─rl-swap 253:1 0 2G 0 lvm [SWAP]
└─rl-home 253:2 0 127G 0 lvm /home
sr0 11:0 1 10.5G 0 rom
#重启后无法进系统
[root@rocky86 ~]# reboot
#用光盘启动,进入Rescue mode,选第3项skip to shell
#配置网络
ifconfig ens33 192.168.61.136/24
#从远端拿回分区表信息
scp 192.168.61.140:/root/dpt.img .
#恢复MBR分区表
dd if=dpt.img of=/dev/sda bs=1 seek=446
#重启
reboot
1.3GPT
GPT优势显著,支持 128 个分区,采用 64 位技术,兼容 8Z和64Z(4096Byte/block)模式,用 128 位 UUID 标识磁盘与分区。分区表具备自动备份功能,头尾各存一份,还设有 CRC 校验位保障数据准确。同时,UEFI 硬件对 GPT 的支持,让操作系统能基于 GPT 分区启动。GPT 分区结构由 GPT 头、分区表、GPT 分区以及备份区域这 4 个区域构成。
#ubuntu默认使用GPT分区
[root@ubuntu-140 ~]#fdisk -l /dev/sda
Disk /dev/sda: 150 GiB, 161061273600 bytes, 314572800 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: B2665541-61DD-47CD-9C55-6F66731D73F1
Device Start End Sectors Size Type
/dev/sda1 2048 4095 2048 1M BIOS boot
/dev/sda2 4096 4198399 4194304 2G Linux filesystem
/dev/sda3 4198400 314570751 310372352 148G Linux filesystem
1.4BIOS和UEFI
BIOS是固化在主板上用于系统自检和引导操作系统的程序,诞生于1975年,用16位汇编语言编写,只能访问1M内存,仅支持MBR初始化的硬盘,最大支持2T硬盘和4个主分区,采用字符操作界面,已无法满足64位系统需求。EFI作为BIOS的继任标准,由UEFI继承发展,后者由国际组织统一管理,采用32/64位C语言编写,突破实模式限制,提供图形化界面和文件式信息存储,支持GPT分区表及2T以上硬盘,能实现更灵活的硬盘分区。

2.管理分区
2.1列出块设备
lsblk [options] [<device> ...]
#常用选项
-a|--all #输出所有设备信息
-b|--bytes #以字节为单位显示设备大小
-d|--nodeps #不显示分区信息
-e|--exclude <list> #以主设备号排除设备
-f|--fs #显示文件系统
-i|--ascii #只使用 ascii 字符输出
-I|--include <list> #仅显示指定的设备
-J|--json #以json格式显示输出
-l|--list #以列表显示
-T|--tree #以树状结构显示,默认项
-m|--perms #显示属主属组及权限
-n|--noheadings #不显示表头
-o|--output <list> #只显示指定列
-O|--output-all #显示所有列
-p|--paths #显示设备全路径
-P|--pairs #以 k=>v 的格式显示输出
-r|--raw #原样输出
-s|--inverse #反向显示关联信息
-S|--scsi #显示scsi设备(small computer system interface device,小型计算机接口设备)
-t|--topology #显示拓扑信息
#常用字段
NAME #设备名称
MAJ:MIN #主设备号:次设备号
RM #是否是可移动设备
SIZE #设备容量大小
RO #是否是只读设备
TYPE #设备类型
MOUNTPOINT #挂载点
2.2创建分区
① parted
高级分区操作命令,可以是交互或非交互方式,实时生效的,没有交互式确认。
parted [OPTION]... [DEVICE [COMMAND [PARAMETERS]...]...]
#常用选项
-l|--list #显示所有硬盘分区信息
-s|--script #不输出提示信息
#常用子命令
align-check TYPE N #检查分区是否满足对齐(最小|最佳)类型的对齐方式
help [COMMAND] #显示命令帮助
mklabel|mktable LABEL-TYPE #指定磁盘的分区类型 gpt|msdos(mbr)
mkpart PART-TYPE [FS-TYPE] START END #新建分区,指定分区类型,文件系统,开始结束位置
name NUMBER NAME #重命名指定分区
print [devices|free|list,all|NUMBER] #显示
quit #退出
rescue START END #空间碎片整理
resizepart NUMBER END #重置分区大小
rm NUMBER #删除指定分区
select DEVICE #选择设备
disk_set FLAG STATE #为设备打标签
disk_toggle [FLAG] #修改flag
set NUMBER FLAG STATE #设置flag
toggle [NUMBER [FLAG]] #修改flag
unit UNIT #设置默认单位, 默认为MB,B|KB|MB|GB|TB
version #显示版本
#显示所有硬盘分区信息
[root@ubuntu-140 ~]#parted -l
#查看指定磁盘设备分区信息,unknown无分区信息,新磁盘
[root@ubuntu-140 ~]#parted /dev/sdb print
Error: /dev/sdb: unrecognised disk label
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: unknown
Disk Flags:
#磁盘上指定GPT分区
[root@ubuntu-140 ~]#parted /dev/sdb mklabel gpt
Information: You may need to update /etc/fstab.
#查看
[root@ubuntu-140 ~]#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@ubuntu-140 ~]#parted /dev/sdb mkpart primary 1 1001
Information: You may need to update /etc/fstab.
#查看
[root@ubuntu-140 ~]#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
1 1049kB 1001MB 1000MB primary
#再次创建
[root@ubuntu-140 ~]#parted /dev/sdb mkpart primary 1002 1102
Information: You may need to update /etc/fstab.
#再次查看
[root@ubuntu-140 ~]#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
1 1049kB 1001MB 1000MB primary
2 1002MB 1102MB 99.6MB primary
#删除
[root@ubuntu-140 ~]#parted /dev/sdb rm 2
Information: You may need to update /etc/fstab.
#再次查看
[root@ubuntu-140 ~]#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
1 1049kB 1001MB 1000MB primary
② fdisk
fdisk [options] <disk>
fdisk [options] -l [<disk>]
#类似于fdisk 的GPT分区工具
gdisk [options] [device...]
#常用选项
-b|--sector-size <size> #指定扇区大小,默认512字节
-L|--color[=color] #显示时是否添加颜色(auto|always|never)默认启用颜色
-l|--list #显示所有磁盘设备
-o|--output <list> #只显示指定列
-u|--units[=<unit>] #设置显示单位 cylinders|sectors,默认sectors
-s|--getsz #显示设备有多少个扇区
-b|--bytes N #以指定的字节大小来计算扇区数量
-t|--type type #只显示指定类型的分区表
-C|--cylinders N #指定柱面数
-H|--heads N #指定磁头数
-S|--sectors N #指定每条磁道的扇区数
#常用子命令
p #输出分区列表
t #更改分区类型
n #创建新分区
d #删除分区
v #校验分区
u #转换单位
w #保存并退出
q #不保存并退出
x #高级功能(专家模式)
#不同类型分区可用的列,配合 -o 选项
gpt: Device Start End Sectors Size Type Type-UUID Attrs Name UUID
dos: Device Start End Sectors Cylinders Size Type Id Attrs Boot End-C/H/S StartC/H/S
bsd: Slice Start End Sectors Cylinders Size Type Bsize Cpg Fsize
sgi: Device Start End Sectors Cylinders Size Type Id Attrs
sun: Device Start End Sectors Cylinders Size Type Id Flags
#显示所有设备
[root@rocky86 ~]# fdisk -l
#显示指定设备
[root@rocky86 ~]# fdisk -l /dev/sda
#显式指定列
[root@ubuntu-140 ~]#fdisk /dev/sda -lo start,end,type
Disk /dev/sda: 150 GiB, 161061273600 bytes, 314572800 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: B2665541-61DD-47CD-9C55-6F66731D73F1
Start End Type
2048 4095 BIOS boot
4096 4198399 Linux filesystem
4198400 314570751 Linux filesystem
#查看内核是否已经识别新的分区
[root@ubuntu-140 ~]#cat /proc/partitions
...
#同步分区表
partprobe
#创建分区
[root@rocky-136 ~]#fdisk /dev/sdb
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.
Device does not contain a recognized partition table.
Created a new DOS disklabel with disk identifier 0x75d3fb8f.
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: 0x75d3fb8f
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-41943039, default 2048):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-41943039, default 41943039): +5G
Created a new partition 1 of type 'Linux' and of size 5 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: 0x75d3fb8f
Device Boot Start End Sectors Size Id Type
/dev/sdb1 2048 10487807 10485760 5G 83 Linux
.
.
.
Command (m for help): n
Partition type
p primary (2 primary, 0 extended, 2 free)
e extended (container for logical partitions)
Select (default p): e
Partition number (3,4, default 3):
First sector (20973568-41943039, default 20973568):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (20973568-41943039, default 41943039): +8G
Created a new partition 3 of type 'Extended' 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: 0x75d3fb8f
Device Boot Start End Sectors Size Id Type
/dev/sdb1 2048 10487807 10485760 5G 83 Linux
/dev/sdb2 10487808 20973567 10485760 5G 83 Linux
/dev/sdb3 20973568 37750783 16777216 8G 5 Extended
.
.
.
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: 0x75d3fb8f
Device Boot Start End Sectors Size Id Type
/dev/sdb1 2048 10487807 10485760 5G 83 Linux
/dev/sdb2 10487808 20973567 10485760 5G 83 Linux
/dev/sdb3 20973568 37750783 16777216 8G 5 Extended
/dev/sdb5 20975616 27267071 6291456 3G 83 Linux
/dev/sdb6 27269120 37750783 10481664 5G 83 Linux
Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
[root@rocky-136 ~]#lsblk /dev/sdb
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
sdb 8:16 0 20G 0 disk
├─sdb1 8:17 0 5G 0 part
├─sdb2 8:18 0 5G 0 part
├─sdb3 8:19 0 1K 0 part
├─sdb5 8:21 0 3G 0 part
└─sdb6 8:22 0 5G 0 part
#删除第二个分区
Command (m for help): d
Partition number (1-3,5,6, default 6): 2
Partition 2 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: 0x75d3fb8f
Device Boot Start End Sectors Size Id Type
/dev/sdb1 2048 10487807 10485760 5G 83 Linux
/dev/sdb3 20973568 37750783 16777216 8G 5 Extended
/dev/sdb5 20975616 27267071 6291456 3G 83 Linux
/dev/sdb6 27269120 37750783 10481664 5G 83 Linux
③ gdisk
gdisk [ -l ] device
#常用子命令
b #备份分区表到指定文件
c #修改分区名
d #删除分区
i #显示分区详细信息
l #列出所有分区类型
n #新建分区
o #创建新的分区表
p #查看分区
q #退出
r #恢复和转换选项,非专业人士勿用
s #排序
t #修改分区类型,默认 8300,表示普通分区
v #检测硬盘是否有问题
w #保存退出
x #额外功能,专家模式
? #显示帮助
#创建分区
[root@ubuntu-140 ~]#gdisk /dev/sdc
GPT fdisk (gdisk) version 1.0.8
Partition table scan:
MBR: not present
BSD: not present
APM: not present
GPT: not present
Creating new GPT entries in mem


886

被折叠的 条评论
为什么被折叠?



