492 lines
14 KiB
Markdown
492 lines
14 KiB
Markdown
<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
|
||
```
|
||
|