上传文件至 /
This commit is contained in:
parent
502303ee5e
commit
696226bb1b
720
存储管理.md
Normal file
720
存储管理.md
Normal file
@ -0,0 +1,720 @@
|
||||
<h2><center>存储管理</center></h2>
|
||||
|
||||
------
|
||||
|
||||
## 一:基本分区
|
||||
|
||||
### 1. 分区方式
|
||||
|
||||
- MBR分区:主引导记录(MBR,Master Boot Record)
|
||||
1. 支持最大的磁盘容量 < 2TB,
|
||||
- GPT分区:全局唯一标识分区表(GUID Partition Table)
|
||||
|
||||
|
||||
|
||||
### 2. 创建分区
|
||||
|
||||
**MBR 分区方式**
|
||||
|
||||
工具:`fdisk`
|
||||
|
||||
```bash
|
||||
# 查看磁盘列表
|
||||
[root@wxin ~]# fdisk -l
|
||||
|
||||
# 创建分区:
|
||||
[root@wxin ~]# fdisk /dev/sdb # 启动分区工具
|
||||
命令(输入 m 获取帮助):n # 新建分区
|
||||
Partition type:
|
||||
p primary (0 primary, 0 extended, 4 free) # 主分区
|
||||
e extended # 扩展分区
|
||||
Select (default p): p # 选择主分区
|
||||
分区号 (1-4,默认 1): # 默认回车
|
||||
起始 扇区 (2048-10485759,默认为 2048): # 默认回车
|
||||
将使用默认值 2048
|
||||
Last 扇区, +扇区 or +size{K,M,G} (2048-10485759,默认为 10485759):+1G # 输入分区大小
|
||||
分区 1 已设置为 Linux 类型,大小设为 1 GiB
|
||||
|
||||
命令(输入 m 获取帮助):w # 保存分区信息
|
||||
The partition table has been altered!
|
||||
|
||||
Calling ioctl() to re-read partition table.
|
||||
正在同步磁盘。
|
||||
[root@wxin ~]# partprobe /dev/sdb # 刷新分区表
|
||||
[root@wxin ~]# fdisk -l /dev/sdb # 查看分区结果
|
||||
|
||||
磁盘 /dev/sdb:5368 MB, 5368709120 字节,10485760 个扇区
|
||||
Units = 扇区 of 1 * 512 = 512 bytes
|
||||
扇区大小(逻辑/物理):512 字节 / 512 字节
|
||||
I/O 大小(最小/最佳):512 字节 / 512 字节
|
||||
磁盘标签类型:dos
|
||||
磁盘标识符:0x7ebdf7b5
|
||||
|
||||
设备 Boot Start End Blocks Id System
|
||||
/dev/sdb1 2048 2099199 1048576 83 Linux
|
||||
|
||||
常用命令:
|
||||
n - 创建新分区
|
||||
d - 删除分区
|
||||
p - 查看分区表
|
||||
t - 修改分区类型(例如将分区设为Linux swap的82)
|
||||
w - 保存并退出
|
||||
```
|
||||
|
||||
**GPT 分区方式**
|
||||
|
||||
工具:gdisk
|
||||
|
||||
```bash
|
||||
[root@wxin ~]# gdisk /dev/sdc # 启动分区工具
|
||||
Command (? for help): n # 创建新分区
|
||||
Partition number (1-128, default 1): # 默认回车
|
||||
First sector (34-10485726, default = 2048) or {+-}size{KMGTP}: # 默认回车
|
||||
Last sector (2048-10485726, default = 10485726) or {+-}size{KMGTP}: +2G # 输入磁盘大小
|
||||
Current type is '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/sdc: 10485760 sectors, 5.0 GiB
|
||||
Logical sector size: 512 bytes
|
||||
Disk identifier (GUID): 33461B34-CB6D-4B38-B148-E2FC057F30BF
|
||||
Partition table holds up to 128 entries
|
||||
First usable sector is 34, last usable sector is 10485726
|
||||
Partitions will be aligned on 2048-sector boundaries
|
||||
Total free space is 6291389 sectors (3.0 GiB)
|
||||
|
||||
Number Start (sector) End (sector) Size Code Name
|
||||
1 2048 4196351 2.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/sdc.
|
||||
The operation has completed successfully.
|
||||
[root@wxin ~]# partprobe /dev/sdc # 刷新分区表
|
||||
|
||||
|
||||
常用命令:
|
||||
n - 新建分区
|
||||
w - 保存退出
|
||||
```
|
||||
|
||||
### 3. 格式化与挂载分区
|
||||
|
||||
**格式化分区**
|
||||
|
||||
```bash
|
||||
# 格式化为xfs(CentOS 7默认)
|
||||
[root@wxin ~]# mkfs.xfs /dev/sdb1
|
||||
meta-data=/dev/sdb1 isize=512 agcount=4, agsize=65536 blks
|
||||
= sectsz=512 attr=2, projid32bit=1
|
||||
= crc=1 finobt=0, sparse=0
|
||||
data = bsize=4096 blocks=262144, 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
|
||||
|
||||
|
||||
# 格式化为ext4
|
||||
[root@wxin ~]# mkfs.ext4 /dev/sdb2
|
||||
mke2fs 1.42.9 (28-Dec-2013)
|
||||
文件系统标签=
|
||||
OS type: Linux
|
||||
块大小=4096 (log=2)
|
||||
分块大小=4096 (log=2)
|
||||
Stride=0 blocks, Stripe width=0 blocks
|
||||
65536 inodes, 262144 blocks
|
||||
13107 blocks (5.00%) reserved for the super user
|
||||
第一个数据块=0
|
||||
Maximum filesystem blocks=268435456
|
||||
8 block groups
|
||||
32768 blocks per group, 32768 fragments per group
|
||||
8192 inodes per group
|
||||
Superblock backups stored on blocks:
|
||||
32768, 98304, 163840, 229376
|
||||
|
||||
Allocating group tables: 完成
|
||||
正在写入inode表: 完成
|
||||
Creating journal (8192 blocks): 完成
|
||||
Writing superblocks and filesystem accounting information: 完成
|
||||
```
|
||||
|
||||
**手动挂载分区**
|
||||
|
||||
```bash
|
||||
# 创建挂载点目录
|
||||
[root@wxin ~]# mkdir /mnt/sdb1
|
||||
|
||||
# 临时挂载
|
||||
[root@wxin ~]# mount /dev/sdb1 /mnt/sdb1
|
||||
|
||||
# 卸载分区
|
||||
[root@wxin ~]# umount /mnt/sdb1
|
||||
```
|
||||
|
||||
**永久挂载**
|
||||
|
||||
获取分区的UUID:
|
||||
|
||||
```bash
|
||||
[root@wxin ~]# blkid /dev/sdb1
|
||||
/dev/sdb1: UUID="52cd25d2-fc4f-4c9a-a9ab-18c86391f8ce" TYPE="xfs"
|
||||
```
|
||||
|
||||
编辑`/etc/fstab`,添加以下行:
|
||||
|
||||
```bash
|
||||
[root@wxin ~]# vim /etc/fstab
|
||||
UUID=52cd25d2-fc4f-4c9a-a9ab-18c86391f8ce /mnt/sdb1 xfs defaults 0 0
|
||||
```
|
||||
|
||||
应用配置:
|
||||
|
||||
```bash
|
||||
[root@wxin ~]# mount -a
|
||||
```
|
||||
|
||||
|
||||
|
||||
## 二:逻辑卷 LVM
|
||||
|
||||
**逻辑卷管理(Logical Volume Manager, LVM)** 是一种灵活的存储管理方式,允许动态调整磁盘空间、合并多个物理磁盘的容量,并支持快照和卷扩展等高级功能。
|
||||
|
||||
### 1. LVM 核心概念
|
||||
|
||||
**术语:**
|
||||
|
||||
- 物理卷(PV,Physical Volume):物理磁盘或分区(如:`/dev/sdb1`),需初始化为 LVM 可用的物理卷。
|
||||
- 卷组(VG,Volume Group):有多个物理卷(PV)组成的存储池,VG 的容量是所有 PV 的总合。
|
||||
- 逻辑卷(LV,Logical Volume):从卷组(VG)中划分的逻辑存储单元,可动态调整大小,类似传统分区。
|
||||
- 物理扩展(PE,Physical Extent):LVM 管理的最小存储单元(默认 4MB),用于分配和扩展逻辑卷。
|
||||
|
||||
### 2. LVM 基本操作
|
||||
|
||||
**创建逻辑卷**
|
||||
|
||||
```bash
|
||||
# 初始化物理卷(将分区或磁盘设为PV)
|
||||
[root@wxin ~]# pvcreate /dev/sdb4
|
||||
|
||||
# 查看物理卷
|
||||
[root@wxin ~]# pvs
|
||||
PV VG Fmt Attr PSize PFree
|
||||
/dev/sdb4 lvm2 --- <2.00g <2.00g
|
||||
|
||||
[root@wxin ~]# pvscan
|
||||
PV /dev/sda2 VG centos lvm2 [<19.00 GiB / 0 free]
|
||||
PV /dev/sdb4 lvm2 [<2.00 GiB]
|
||||
Total: 2 [<21.00 GiB] / in use: 1 [<19.00 GiB] / in no VG: 1 [<2.00 GiB]
|
||||
|
||||
[root@wxin ~]# pvdisplay
|
||||
"/dev/sdb4" is a new physical volume of "<2.00 GiB"
|
||||
--- NEW Physical volume ---
|
||||
PV Name /dev/sdb4
|
||||
VG Name
|
||||
PV Size <2.00 GiB
|
||||
Allocatable NO
|
||||
PE Size 0
|
||||
Total PE 0
|
||||
Free PE 0
|
||||
Allocated PE 0
|
||||
PV UUID VENObl-2fk8-vQf3-3xyE-aNJK-I5LZ-RHX7iC
|
||||
|
||||
# 创建卷组(VG),将PV加入VG
|
||||
[root@wxin ~]# vgcreate vg /dev/sdb4
|
||||
Volume group "vg" successfully created
|
||||
|
||||
# 查看卷组
|
||||
[root@wxin ~]# vgs
|
||||
VG #PV #LV #SN Attr VSize VFree
|
||||
vg 1 0 0 wz--n- <2.00g <2.00g
|
||||
|
||||
[root@wxin ~]# vgscan
|
||||
Reading volume groups from cache.
|
||||
Found volume group "vg" using metadata type lvm2
|
||||
|
||||
[root@wxin ~]# vgdisplay
|
||||
--- Volume group ---
|
||||
VG Name vg
|
||||
System ID
|
||||
Format lvm2
|
||||
Metadata Areas 1
|
||||
Metadata Sequence No 1
|
||||
VG Access read/write
|
||||
VG Status resizable
|
||||
MAX LV 0
|
||||
Cur LV 0
|
||||
Open LV 0
|
||||
Max PV 0
|
||||
Cur PV 1
|
||||
Act PV 1
|
||||
VG Size <2.00 GiB
|
||||
PE Size 4.00 MiB
|
||||
Total PE 511
|
||||
Alloc PE / Size 0 / 0
|
||||
Free PE / Size 511 / <2.00 GiB
|
||||
VG UUID ahZ9N2-U8vr-fX8R-n9Xl-bPVe-kQmo-o7E2uP
|
||||
|
||||
# 从VG中创建逻辑卷(LV)
|
||||
[root@wxin ~]# lvcreate -n lv -L 1G vg
|
||||
Logical volume "lv" created.
|
||||
|
||||
# 或使用剩余所有空间:
|
||||
[root@wxin ~]# lvcreate -n lv2 -l 100%Free vg
|
||||
Logical volume "lv2" created.
|
||||
|
||||
# 查看逻辑卷
|
||||
[root@wxin ~]# lvs
|
||||
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
|
||||
lv vg -wi-a----- 1.00g
|
||||
lv2 vg -wi-a----- 1020.00m
|
||||
|
||||
[root@wxin ~]# lvscan
|
||||
ACTIVE '/dev/vg/lv' [1.00 GiB] inherit
|
||||
ACTIVE '/dev/vg/lv2' [1020.00 MiB] inherit
|
||||
|
||||
[root@wxin ~]# lvdisplay
|
||||
--- Logical volume ---
|
||||
LV Path /dev/vg/lv
|
||||
LV Name lv
|
||||
VG Name vg
|
||||
LV UUID j5RSvc-2aSh-1GN2-JKOf-LPyg-R6n4-cqodjy
|
||||
LV Write Access read/write
|
||||
LV Creation host, time wxin, 2025-03-25 13:25:54 +0800
|
||||
LV Status available
|
||||
# open 0
|
||||
LV Size 1.00 GiB
|
||||
Current LE 256
|
||||
Segments 1
|
||||
Allocation inherit
|
||||
Read ahead sectors auto
|
||||
- currently set to 8192
|
||||
Block device 253:2
|
||||
|
||||
--- Logical volume ---
|
||||
LV Path /dev/vg/lv2
|
||||
LV Name lv2
|
||||
VG Name vg
|
||||
LV UUID hPMJg9-ZWDM-xzvE-tFfG-KDPe-x2KZ-zjzPUp
|
||||
LV Write Access read/write
|
||||
LV Creation host, time wxin, 2025-03-25 13:26:24 +0800
|
||||
LV Status available
|
||||
# open 0
|
||||
LV Size 1020.00 MiB
|
||||
Current LE 255
|
||||
Segments 1
|
||||
Allocation inherit
|
||||
Read ahead sectors auto
|
||||
- currently set to 8192
|
||||
Block device 253:3
|
||||
|
||||
|
||||
# 格式化逻辑卷(如XFS)
|
||||
[root@wxin ~]# mkfs.xfs /dev/vg/lv
|
||||
meta-data=/dev/vg/lv isize=512 agcount=4, agsize=65536 blks
|
||||
= sectsz=512 attr=2, projid32bit=1
|
||||
= crc=1 finobt=0, sparse=0
|
||||
data = bsize=4096 blocks=262144, 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@wxin ~]# mkdir /mnt/lv
|
||||
[root@wxin ~]# mount /dev/vg/lv /mnt/lv
|
||||
```
|
||||
|
||||
**删除逻辑卷**
|
||||
|
||||
```bash
|
||||
# 查看所有逻辑卷
|
||||
[root@wxin ~]# lvs
|
||||
|
||||
# 或查看详细信息
|
||||
[root@wxin ~]# lvdisplay
|
||||
|
||||
# 检查挂载点
|
||||
[root@wxin ~]# mount | grep <LV名称或挂载点>
|
||||
|
||||
# 卸载
|
||||
[root@wxin ~]# umount /mnt/vg-lv
|
||||
|
||||
# 删除指定逻辑卷
|
||||
[root@wxin ~]# lvremove /dev/vg/lv
|
||||
|
||||
# 查看卷组详细信息
|
||||
[root@wxin ~]# vgdisplay vg
|
||||
|
||||
# 删除卷组
|
||||
[root@wxin ~]# vgremove vg
|
||||
|
||||
# 删除物理卷
|
||||
[root@wxin ~]# pvremove /dev/sdb4
|
||||
```
|
||||
|
||||
**扩展逻辑卷**
|
||||
|
||||
```bash
|
||||
# 添加物理卷
|
||||
[root@wxin ~]# pvcreate /dev/sdb3
|
||||
|
||||
# 扩展卷组
|
||||
[root@wxin ~]# vgextend vg /dev/sdb3
|
||||
|
||||
# 查看卷组
|
||||
[root@wxin ~]# vgs
|
||||
VG #PV #LV #SN Attr VSize VFree
|
||||
vg 2 1 0 wz--n- 2.99g 1.99g
|
||||
|
||||
# 扩展逻辑卷
|
||||
[root@wxin ~]# lvextend -L +1G /dev/vg/lv
|
||||
|
||||
[root@wxin ~]# df -hT
|
||||
文件系统 类型 容量 已用 可用 已用% 挂载点
|
||||
/dev/mapper/vg-lv ext4 976M 2.6M 907M 1% /mnt/vg-lv
|
||||
|
||||
# 扩展文件系统
|
||||
[root@wxin ~]# resize2fs /dev/vg/lv
|
||||
|
||||
[root@wxin ~]# df -hT
|
||||
文件系统 类型 容量 已用 可用 已用% 挂载点
|
||||
/dev/mapper/vg-lv ext4 2.0G 3.0M 1.9G 1% /mnt/vg-lv
|
||||
```
|
||||
|
||||
## 三:交换分区 SWAP
|
||||
|
||||
### 1. 交换分区的类型
|
||||
|
||||
| 类型 | 说明 |
|
||||
| :-----------: | :----------------------------------------: |
|
||||
| **交换分区** | 独立的磁盘分区(类型为`82`或`8200`) |
|
||||
| **交换文件** | 文件形式的虚拟交换空间,无需分区 |
|
||||
| **LVM逻辑卷** | 将逻辑卷(LV)格式化为交换空间,灵活性更高 |
|
||||
|
||||
### 2. 创建交换空间
|
||||
|
||||
**创建交换分区**
|
||||
|
||||
```bash
|
||||
# 创建分区
|
||||
[root@wxin ~]# fdisk /dev/sdc
|
||||
|
||||
# 格式化交换分区
|
||||
[root@wxin ~]# mkswap /dev/sdc1
|
||||
|
||||
# 激活交换分区
|
||||
[root@wxin ~]# swapon /dev/sdc1
|
||||
|
||||
# 永久挂载
|
||||
[root@wxin ~]# blkid /dev/sdc1
|
||||
[root@wxin ~]# vim /etc/fstab
|
||||
UUID=3a412f3b-3877-4984-b9c0-2464f047c65e none swap defaults 0 0
|
||||
```
|
||||
|
||||
**创建交换文件**
|
||||
|
||||
```bash
|
||||
# 生成交换文件
|
||||
[root@wxin ~]# fallocate -l 4G /swapfile
|
||||
|
||||
[root@wxin ~]# dd if=/dev/zero of=/swapfile bs=1M count=4096
|
||||
|
||||
# 设置权限并格式化
|
||||
[root@wxin ~]# chmod 600 /swapfile
|
||||
[root@wxin ~]# mkswap /swapfile
|
||||
|
||||
# 激活并永久挂载
|
||||
[root@wxin ~]# swapon /swapfile
|
||||
[root@wxin ~]# echo "/swapfile none swap defaults 0 0" | sudo tee -a /etc/fstab
|
||||
```
|
||||
|
||||
**使用 LVM 逻辑卷**
|
||||
|
||||
```bash
|
||||
# 创建逻辑卷
|
||||
[root@wxin ~]# pvcreate /dev/sdb2
|
||||
[root@wxin ~]# vgcreate vg2 /dev/sdb2
|
||||
[root@wxin ~]# lvcreate -n lv_swap -L 500M vg2
|
||||
|
||||
# 格式化为交换空间
|
||||
[root@wxin ~]# mkswap /dev/vg2/lv_swap
|
||||
[root@wxin ~]# swapon /dev/vg2/lv_swap
|
||||
|
||||
# 永久挂载
|
||||
[root@wxin ~]# blkid /dev/vg2/lv_swap
|
||||
[root@wxin ~]# vim /etc/fstab
|
||||
UUID=7d7b8efd-2009-4e6b-8d78-03aab7097460 none swap defaults 0 0
|
||||
```
|
||||
|
||||
### 3. 禁用或删除交换分区
|
||||
|
||||
**临时禁用**
|
||||
|
||||
```bash
|
||||
[root@wxin ~]# swapoff /dev/sdb1 # 分区或逻辑卷
|
||||
[root@wxin ~]# swapoff /swapfile # 交换文件
|
||||
```
|
||||
|
||||
**永久删除**
|
||||
|
||||
- 禁用交换分区:
|
||||
|
||||
```bash
|
||||
[root@wxin ~]# swapoff /swapfile
|
||||
```
|
||||
|
||||
- 删除文件或分区:
|
||||
|
||||
```bash
|
||||
[root@wxin ~]# rm -f /swapfile # 删除交换文件
|
||||
[root@wxin ~]# lvremove /dev/vg_data/lv_swap # 删除逻辑卷
|
||||
```
|
||||
|
||||
- 清理`/etc/fstab`中的对应条目。
|
||||
|
||||
### 4. 查看交换空间状态
|
||||
|
||||
```bash
|
||||
# 查看所有交换设备/文件
|
||||
free -h
|
||||
swapon -s
|
||||
cat /proc/swaps
|
||||
```
|
||||
|
||||
## 四:文件系统
|
||||
|
||||
### EXT4 文件系统
|
||||
|
||||
**EXT4(Fourth Extended File System)** 是 Linux 系统中最广泛使用的日志文件系统。支持最大单个文件为16TB。
|
||||
|
||||
**名词**
|
||||
|
||||
组块(Block Groups)是Ext4文件系统的核心管理单元,将整个存储空间划分为多个逻辑分区,每个组块独立管理元数据和数据块,旨在提升性能、减少碎片并增强可靠性。
|
||||
|
||||
**组块的结构**
|
||||
|
||||
- 超级快(Superblock):存储全局文件系统信息,如总块数、块大小、Inode总数、挂载状态等。
|
||||
- 组描述符(Group Descriptor):记录组块内部资源分配状态,如空闲块数、空闲Inode数、块位图位置等。
|
||||
- 快位图(Block Bitmap):标记组块内每个数据块的使用状态(1位表示1个块,0=空闲,1=已用)。
|
||||
- 索引节点(Inode):存储文件的元数据(权限、时间戳、大小、数据块指针等)
|
||||
- 数据块(Block):存储文件的**实际内容**(二进制数据、目录条目等)
|
||||
|
||||
```bash
|
||||
# 查看索引节点
|
||||
[root@wxin ~]# df -i
|
||||
|
||||
# 查看数据块
|
||||
[root@wxin ~]# df -k
|
||||
|
||||
一个文件或目录占用一个inode,一个文件占用一个或多个block
|
||||
```
|
||||
|
||||
## 五:文件链接
|
||||
|
||||
### 1. 定义
|
||||
|
||||
| **特性** | **硬链接(Hard Link)** | **软链接(Symbolic Link)** |
|
||||
| :------------: | :--------------------------------: | :------------------------------------------------: |
|
||||
| **本质** | 直接指向文件的 **Inode** | 指向文件的 **路径名**(类似快捷方式) |
|
||||
| **创建命令** | `ln 源文件 硬链接名` | `ln -s 源文件 软链接名` |
|
||||
| **Inode 关系** | 与原文件 **共享同一 Inode** | **独立 Inode**,存储源文件路径字符串 |
|
||||
| **文件类型** | 普通文件(`ls -l` 显示无特殊标记) | 特殊文件(`ls -l` 显示 `l` 类型,如 `lrwxrwxrwx`) |
|
||||
|
||||
### 2. 关键区别
|
||||
|
||||
#### 1. 跨文件系统支持
|
||||
|
||||
- 硬链接:仅限同一文件系统(同一块磁盘分区)。原因为Inode编号仅在文件系统内唯一。
|
||||
- 软连接:可跨文件系统(甚至指向网络路径)
|
||||
|
||||
#### 2. 目录链接
|
||||
|
||||
- 硬链接:不允许用户创建目录的硬链接
|
||||
- 软连接:可指向目录
|
||||
|
||||
#### 3. 源文件删除的影响
|
||||
|
||||
- 硬链接:删除原文件后,硬链接仍可访问数据(只要存在至少一个硬链接)。
|
||||
- 软连接:原文件删除后,软链接变为“悬空链接”(Dangling Link),访问报错。
|
||||
|
||||
#### 4. 文件属性同步
|
||||
|
||||
- 硬链接:所有硬链接共享同一 Inode,修改权限、时间戳等属性会同步。
|
||||
- 软连接:自身属性(如权限、时间戳)独立,与目标文件无关。
|
||||
|
||||
### 3. 示例
|
||||
|
||||
**创建与验证**
|
||||
|
||||
```bash
|
||||
# 创建原文件
|
||||
[root@wxin ~]# echo "hello world" > original.txt
|
||||
|
||||
# 创建硬链接
|
||||
[root@wxin ~]# ln original.txt hardlink.txt
|
||||
|
||||
# 创建软链接
|
||||
[root@wxin ~]# ln -s original.txt softlink.txt
|
||||
|
||||
# 查看 Inode 和链接数
|
||||
[root@wxin ~]# ls -li
|
||||
35586588 -rw-r--r--. 2 root root 12 3月 25 19:47 hardlink.txt
|
||||
35586588 -rw-r--r--. 2 root root 12 3月 25 19:47 original.txt
|
||||
33830562 lrwxrwxrwx. 1 root root 12 3月 25 19:48 softlink.txt -> original.txt
|
||||
|
||||
观察点:硬链接与原文件 Inode 相同(1050323),链接数均为 2;软链接有独立 Inode(1050324)。
|
||||
```
|
||||
|
||||
**删除原文件**
|
||||
|
||||
```bash
|
||||
[root@wxin ~]# rm -rf original.txt
|
||||
|
||||
# 访问硬链接(正常)
|
||||
[root@wxin ~]# cat hardlink.txt
|
||||
hello world
|
||||
|
||||
# 访问软链接(报错)
|
||||
[root@wxin ~]# cat softlink.txt
|
||||
cat: softlink.txt: 没有那个文件或目录
|
||||
```
|
||||
|
||||
## 六:RAID(独立磁盘冗余阵列)
|
||||
|
||||
RAID(Redundant Array of Independent Disks)通过组合多个物理磁盘提升存储系统的性能、容量和可靠性。
|
||||
|
||||
### 1. 常见 RAID 对比
|
||||
|
||||
| **RAID 级别** | **最小磁盘数** | **容错能力** | **存储利用率** | **读写性能** | **典型应用场景** |
|
||||
| ------------- | -------------- | --------------------- | ------------------ | ---------------------- | ------------------------ |
|
||||
| **RAID 0** | 2 | ❌ 无冗余 | 100% | 读写性能最优 | 临时数据、高速缓存 |
|
||||
| **RAID 1** | 2 | ✅ 允许1块磁盘故障 | 50% | 读性能优,写性能一般 | 关键数据镜像(如系统盘) |
|
||||
| **RAID 5** | 3 | ✅ 允许1块磁盘故障 | (N−1)/N(*N*−1)/*N* | 读优,写需计算奇偶校验 | 通用文件存储、数据库 |
|
||||
| **RAID 6** | 4 | ✅ 允许2块磁盘故障 | (N−2)/N(*N*−2)/*N* | 读优,写性能较差 | 大容量归档存储 |
|
||||
| **RAID 10** | 4 | ✅ 每组镜像允许1块故障 | 50% | 读写性能优 | 高并发事务处理、虚拟化 |
|
||||
|
||||
### 2. 核心原理
|
||||
|
||||
#### 1. RAID 0(条带化)
|
||||
|
||||
- **原理**:数据分块(Striping)后轮询写入多个磁盘。
|
||||
- **优势**:最大化吞吐量(理论速度 = 单盘速度 × 磁盘数)。
|
||||
- **风险**:任一磁盘故障导致全部数据丢失。
|
||||
- **示例**:4 块磁盘的 RAID 0,存储利用率 100%,容量 = 4×单盘容量。
|
||||
|
||||
#### 2. RAID 1(镜像)
|
||||
|
||||
- **原理**:数据完全复制到所有成员盘(Mirroring)。
|
||||
- **优势**:高可靠性,读操作可并行(多副本)。
|
||||
- **缺点**:存储效率低(50%),写性能受限于最慢磁盘。
|
||||
- **变体**:RAID 1E(支持奇数磁盘的镜像)。
|
||||
|
||||
#### 3. RAID 5(分布式奇偶校验)
|
||||
|
||||
- **原理**:数据与奇偶校验块(Parity)分布式存储在所有磁盘。
|
||||
1. 奇偶校验块位置轮换(如磁盘1存块1的校验,磁盘2存块2的校验等)。
|
||||
- **校验计算**:异或(XOR)或更复杂的算法(如 Reed-Solomon)。
|
||||
- **重建**:故障盘数据通过剩余磁盘的校验和数据恢复。
|
||||
- **缺点**:写惩罚(Write Penalty)高,每次写入需更新校验块。
|
||||
|
||||
#### **4. RAID 6(双分布式奇偶校验)**
|
||||
|
||||
- **原理**:类似 RAID 5,但使用两种独立校验(如 P+Q 校验)。
|
||||
- **优势**:容忍双盘故障,适合大容量磁盘(降低重建失败风险)。
|
||||
- **缺点**:存储利用率更低,写性能进一步下降。
|
||||
|
||||
#### **5. RAID 10(条带化镜像)**
|
||||
|
||||
- **原理**:先镜像(RAID 1)再条带化(RAID 0)。
|
||||
- 磁盘分为多组镜像对,数据条带化写入各组。
|
||||
- **优势**:高读写性能与容错能力的平衡。
|
||||
- **示例**:4 块磁盘的 RAID 10 分为两组镜像,每组2块,容量 = 2×单盘容量。
|
||||
|
||||
### 3. 软 RAID 示例
|
||||
|
||||
```bash
|
||||
[root@wxin ~]# ll /dev/sd*
|
||||
brw-rw----. 1 root disk 8, 0 3月 25 17:03 /dev/sda
|
||||
brw-rw----. 1 root disk 8, 1 3月 25 17:03 /dev/sda1
|
||||
brw-rw----. 1 root disk 8, 2 3月 25 17:03 /dev/sda2
|
||||
brw-rw----. 1 root disk 8, 16 3月 25 17:03 /dev/sdb
|
||||
brw-rw----. 1 root disk 8, 32 3月 25 17:03 /dev/sdc
|
||||
brw-rw----. 1 root disk 8, 48 3月 25 17:03 /dev/sdd
|
||||
brw-rw----. 1 root disk 8, 64 3月 25 17:03 /dev/sde
|
||||
|
||||
# 创建RAID
|
||||
[root@wxin ~]# mdadm -C /dev/md0 -l5 -n3 -x1 /dev/sd{b,c,d,e}
|
||||
mdadm: Defaulting to version 1.2 metadata
|
||||
mdadm: array /dev/md0 started.
|
||||
|
||||
# 格式化
|
||||
[root@wxin ~]# mkfs.ext4 /dev/md0
|
||||
mke2fs 1.42.9 (28-Dec-2013)
|
||||
文件系统标签=
|
||||
OS type: Linux
|
||||
块大小=4096 (log=2)
|
||||
分块大小=4096 (log=2)
|
||||
Stride=128 blocks, Stripe width=256 blocks
|
||||
655360 inodes, 2618880 blocks
|
||||
130944 blocks (5.00%) reserved for the super user
|
||||
第一个数据块=0
|
||||
Maximum filesystem blocks=2151677952
|
||||
80 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, 1605632
|
||||
|
||||
Allocating group tables: 完成
|
||||
正在写入inode表: 完成
|
||||
Creating journal (32768 blocks): 完成
|
||||
Writing superblocks and filesystem accounting information: 完成
|
||||
|
||||
# 挂载
|
||||
[root@wxin ~]# mkdir /mnt/raid5
|
||||
[root@wxin ~]# mount /dev/md0 /mnt/raid5/
|
||||
|
||||
# 查看RAID 信息
|
||||
[root@wxin ~]# mdadm -D /dev/md0
|
||||
/dev/md0:
|
||||
Version : 1.2
|
||||
Creation Time : Tue Mar 25 20:04:36 2025
|
||||
Raid Level : raid5
|
||||
Array Size : 10475520 (9.99 GiB 10.73 GB)
|
||||
Used Dev Size : 5237760 (5.00 GiB 5.36 GB)
|
||||
Raid Devices : 3
|
||||
Total Devices : 4
|
||||
Persistence : Superblock is persistent
|
||||
|
||||
Update Time : Tue Mar 25 20:05:53 2025
|
||||
State : clean
|
||||
Active Devices : 3
|
||||
Working Devices : 4
|
||||
Failed Devices : 0
|
||||
Spare Devices : 1
|
||||
|
||||
Layout : left-symmetric
|
||||
Chunk Size : 512K
|
||||
|
||||
Consistency Policy : resync
|
||||
|
||||
Name : wxin:0 (local to host wxin)
|
||||
UUID : 2cd876a1:70d7ba7a:efd30b04:a91143c0
|
||||
Events : 20
|
||||
|
||||
Number Major Minor RaidDevice State
|
||||
0 8 16 0 active sync /dev/sdb
|
||||
1 8 32 1 active sync /dev/sdc
|
||||
4 8 48 2 active sync /dev/sdd
|
||||
|
||||
3 8 64 - spare /dev/sde
|
||||
```
|
||||
|
Loading…
x
Reference in New Issue
Block a user