云计算百科
云计算领域专业知识百科平台

Linux 存储全解:磁盘分区、软 RAID、LVM 逻辑卷从入门到实操

Linux 硬盘分区、RAID 与逻辑卷管理

摘要:本文全面介绍了 Linux 系统中硬盘分区、RAID 存储技术和逻辑卷管理(LVM)三大核心存储管理技术。首先详细讲解了 MBR 和 GPT 两种分区方案的区别,以及 fdisk、gdisk、parted 等分区工具的使用方法。接着深入探讨了 RAID 0、1、5、6、10、50、60 等不同级别的原理、优缺点和适用场景,并提供了软件 RAID 的创建与管理实践。最后系统阐述了逻辑卷管理的概念、创建、扩展、缩减、快照以及 RAID 与逻辑卷结合的实现,帮助读者掌握灵活、高效的存储管理方案。

1. Linux 硬盘分区管理

1.1 硬盘为什么要分区?

  • 将操作系统文件与用户文件分隔开,避免应用数据过多占满操作系统盘。
  • 限制应用或用户的可用空间。
  • 如果一个分区出现逻辑损坏,仅损坏该分区数据而不影响硬盘上的其他分区。
  • 用于创建交换分区。
  • 限制磁盘空间使用,以提高诊断工具和备份操作的性能。
  • 便于针对不同用途定制文件系统,例如有的文件系统适合存放大量小文件,有的则适合存放大量大文件。

1.2 MBR 分区方案

自 1982 年以来,主引导记录(MBR,Master Boot Record)分区方案规定了在运行 BIOS 固件 的系统上如何对磁盘进行分区:

image-20221225080226221

  • 该方案支持最多4个主分区。
  • 在 Linux系统上,管理员可以使用扩展分区和逻辑分区来创建最多 15个分区。

逻辑分区可以格式化(format),而扩展分区则不能格式化。

  • MBR 记录使用 4 个字节(1 byte = 8 bit)存储分区的总扇区数,最多可表示 2³² 个扇区。按每扇区 512 字节计算,每个分区最大不能超过 2 TiB。

类比为:笔记本只有4个usb接口,如果不够用,则将第四个usb接口外接扩展坞,由扩展坞提供多个usb接口。
通常,我们将磁盘的第一个扇区称为主引导扇区,它位于硬盘的柱面 0、磁头 0、扇区 1 的位置。该扇区包含 MBR 引导代码,承担系统启动的职能。它不属于磁盘上的任何分区,因此分区空间内的格式化命令无法清除主引导记录的任何信息。

主引导扇区由三个部分组成:

image-20221225080625691

  • 引导程序(占446个字节),硬盘启动时将系统控制权转给分区表中的某个操作系统。
  • 磁盘分区表项(DPT,Disk Partition Table),由四个分区表项构成(每个16个字节)。
  • 结束标志(占2个字节),其值为AA55(十六进制)。

1.3 fdisk 工具

fdisk 工具可用于管理采用 MBR 分区方案的磁盘,用户可以根据实际情况划分分区。

1.3.1 fdisk 命令语法

[root@cserver~ 20:12:44]# fdisk -h
Usage:
fdisk [options] <disk> change partition table
fdisk [options] -l <disk> list partition table(s)
fdisk -s <partition> give partition size(s) in blocks

Options:
-b <size> sector size (512, 1024, 2048 or 4096)
-c[=<mode>] compatible mode: 'dos' or 'nondos' (default)
-h print this help text
-u[=<unit>] display units: 'cylinders' or 'sectors' (default)
-v print program version
-C <number> specify the number of cylinders
-H <number> specify the number of heads
-S <number> specify the number of sectors per track

除了使用 -l 选项查看分区表外,其他选项暂时无需使用。

1.3.2 查看分区

fdisk工具的大部分操作通过交互式完成,除了查看分区表。

DOS disklabel 指的是硬盘管理方式MBR。
我们使用上一章准备好的硬盘 /dev/sdb。

# 方法1:
[root@cserver~ 20:12:58]# fdisk -l /dev/sdb

Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 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
Disk label type: dos
Disk identifier: 0x78ceaffe

Device Boot Start End Blocks Id System
/dev/sdb1 2048 41943039 20970496 83 Linux

# 方法2:
[root@cserver~ 20:13:09]# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

# 输入m,查看帮助信息
Command (m for help): `m`
Command action
a toggle a bootable flag
b edit bsd disklabel
c toggle the dos compatibility flag
d delete a partition
g create a new empty GPT partition table
G create an IRIX (SGI) partition table
l list known partition types
m print this menu
n add a new partition
o create a new empty DOS partition table
p print the partition table
q quit without saving changes
s create a new empty Sun disklabel
t change a partition's system id
u change display/entry units
v verify the partition table
w write table to disk and exit
x extra functionality (experts only)

# 输入q,退出管理
Command (m for help): q

1.3.3 创建分区

# 输入n,创建一个新分区
Command (m for help): `n`

# 选择分区类型
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
# 直接回车,选择默认分区类型:primary
Select (default p): `回车`
Using default response p

# 直接回车,分区号,使用默认值1
Partition number (1-4, default 1):

# 直接回车,设置分区起始位置为:默认值2048扇区,也就是1M位置。
First sector (2048-41943039, default 2048):
Using default value 2048

# 设置分区结束位置,输入+2G,也就是起始位置之后2G
Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): +2G
Partition 1 of type Linux and of size 2 GiB is set

Command (m for help): p

Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 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
Disk label type: dos
Disk identifier: 0x78ceaffe

Device Boot Start End Blocks Id System
/dev/sdb1 2048 4196351 2097152 83 Linux

# 输入w,保存更改并退出
# 输入q,不保存更改并退出
Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

# 再次验证分区表变化
[root@cserver~ 20:13:17]# lsblk /dev/sdb
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sdb 8:16 0 20G 0 disk
`-sdb1 8:17 0 2G 0 part

注意:如果此时分区表未生成,执行以下命令,通知内核重新读取分区表。有时需要重启系统才会生成最新的分区表。

[root@cserver~ 20:13:30]# partprobe

1.3.4 删除分区

[root@cserver~ 20:13:42]# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

# 输入p,打印分区表
Command (m for help): p

Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 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
Disk label type: dos
Disk identifier: 0x78ceaffe

Device Boot Start End Blocks Id System
/dev/sdb1 2048 41943039 20970496 83 Linux

# 输入d,删除分区,因为只有1个分区,所以自动删除了第一个分区
Command (m for help): `d`
Selected partition 1
Partition 1 is deleted

Command (m for help): p

Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 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
Disk label type: dos
Disk identifier: 0x78ceaffe

Device Boot Start End Blocks Id System

如果硬盘空间较大,且需要的文件系统数量超过4个,那么就需要借助扩展分区来创建逻辑分区。

如下示例,创建第4个分区的时候,类型选择扩展分区;创建第5个分区的时候,类型选择逻辑分区。

[root@cserver~ 20:14:00]# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

# 再次创建一个容量为3G的分区
Command (m for help): n
Partition type:
p primary (1 primary, 0 extended, 3 free)
e extended
Select (default p):
Using default response p
Partition number (2-4, default 2):
First sector (4196352-41943039, default 4196352):
Using default value 4196352
Last sector, +sectors or +size{K,M,G} (4196352-41943039, default 41943039): +3G
Partition 2 of type Linux and of size 3 GiB is set

# 再次创建一个容量为4G的分区
Command (m for help): n
Partition type:
p primary (2 primary, 0 extended, 2 free)
e extended
Select (default p):
Using default response p
Partition number (3,4, default 3):
First sector (10487808-41943039, default 10487808):
Using default value 10487808
Last sector, +sectors or +size{K,M,G} (10487808-41943039, default 41943039): +4G
Partition 3 of type Linux and of size 4 GiB is set

# 创建第4个分区的时候,选择扩展分区,并且使用剩余所有容量
Command (m for help): n
Partition type:
p primary (3 primary, 0 extended, 1 free)
e extended
Select (default e):
Using default response e
Selected partition 4
First sector (18876416-41943039, default 18876416):
Using default value 18876416
Last sector, +sectors or +size{K,M,G} (18876416-41943039, default 41943039):
Using default value 41943039
Partition 4 of type Extended and of size 11 GiB is set

# 创建第5个分区:在扩展分区中,创建逻辑分区,分配5G容量
Command (m for help): n
All primary partitions are in use
Adding logical partition 5
First sector (18878464-41943039, default 18878464):
Using default value 18878464
Last sector, +sectors or +size{K,M,G} (18878464-41943039, default 41943039): +5G
Partition 5 of type Linux and of size 5 GiB is set

Command (m for help): p

Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 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
Disk label type: dos
Disk identifier: 0x78ceaffe

Device Boot Start End Blocks Id System
/dev/sdb1 2048 4196351 2097152 83 Linux
/dev/sdb2 4196352 10487807 3145728 83 Linux
/dev/sdb3 10487808 18876415 4194304 83 Linux
/dev/sdb4 18876416 41943039 11533312 5 Extended
/dev/sdb5 18878464 29364223 5242880 83 Linux

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

# 查看分区表
[root@cserver~ 20:14:18]# lsblk /dev/sdb
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sdb 8:16 0 20G 0 disk
|-sdb1 8:17 0 2G 0 part
|-sdb2 8:18 0 3G 0 part
|-sdb3 8:19 0 4G 0 part
|-sdb4 8:20 0 1K 0 part
|-sdb5 8:21 0 5G 0 part

分区创建演示完成,接下来只保留1个分区,删除多余的分区。

[root@cserver~ 20:14:34]# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Command (m for help): d
Partition number (1-5, default 5):
Partition 5 is deleted

Command (m for help): d
Partition number (1-4, default 4):
Partition 4 is deleted

Command (m for help): d
Partition number (1-3, default 3):
Partition 3 is deleted

Command (m for help): d
Partition number (1,2, default 2):
Partition 2 is deleted

Command (m for help): d
Selected partition 1
Partition 1 is deleted

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

1.3.5 非交互方式管理(自学)

示例1:创建一个分区

# 将要执行的fdisk命令行写入到一个文本文件
[root@cserver~ 20:14:48]# vim fdisk-create.txt
n
p
1
2048
+2G
p
w

# 执行
[root@cserver~ 20:14:58]# fdisk /dev/sdb < fdisk-create.txt
[root@cserver~ 20:15:13]# lsblk /dev/sdb
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sdb 8:16 0 20G 0 disk
|-sdb1 8:17 0 2G 0 part

示例2:删除一个分区

# 将要执行的fdisk命令行写入到一个文本文件
[root@cserver~ 20:15:23]# vim fdisk-delete.txt
d
p
w

# 执行
[root@cserver~ 20:15:35]# fdisk /dev/sdb < fdisk-delete.txt
[root@cserver~ 20:15:48]# lsblk /dev/sdb
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sdb 8:16 0 20G 0 disk

1.4 GPT 分区方案

GPT是运行在统一可扩展固件接口(UEFI)固件系统上的硬盘分区表标准。

  • MBR分区方案只能管理最大2TiB分区和磁盘。全局唯一标识分区表(GPT,GUID Partition Table)使用8个字节(1 byte = 8 bit)存储分区的总扇区数,可支持最多18 EiB(= 2^64 * 512 Byte),即18艾字节的分区和磁盘。
  • MBR分区方案支持最多15个分区。GPT分区方案最多可提供128个分区。
  • GPT 提供分区表信息的冗余备份。

GPT分区表结构如下:

image-20221225080840512

  • LBA0(保护性 MBR)分为两个部分:
  • 第一部分是与 MBR 中446字节相似的区块,用于存放第一阶段的启动引导程序。
  • 第二部分是与MBR分区表记录区对应,该区域存储一个特殊标识符0xEE,用于表示该磁盘为GPT格式。若磁盘管理程序(比较老的软件)无法识别该磁盘,不能修改这个分区信息,进一步保护磁盘。
  • LBA1(GPT表头记录),记录了分区表自身的位置和大小,同时也记录了前面提到备份用的GPT分区所在位置(最后34个LBA),还放置了分区表的校验码(CRC32),校验码的作用是让操作系统判断GPT的正确与否,倘若发现错误则可以从备份的GPT中恢复正常运行。
  • LBA2-33,记录分区信息,每个LBA可以提供4组的分区记录,默认情况下可以有4×32=128组分区记录。因为每个LBA都有512字节,所以每组分区记录所占128字节,除去每组记录需要的标识符和相关记录信息外,GPT在每组记录中提供了64位记载分区的扇区总数。
  • 虽然GPT最大支持128个分区,但是实际使用过程中分区超过第120个会出现无法格式化使用的情况。

1.5 gdisk 工具

gdisk 工具用于管理采用 GPT 分区方案的磁盘,尤其适用于管理容量超过 2 TB 的磁盘。

1.5.1 gdisk命令语法

gdisk [-l] device

1.5.2 查看分区表

[chenxin@cserver~ 20:15:59]$ gdisk -l /dev/sdb
GPT fdisk (gdisk) version 0.8.10

# 此处显示识别到了 MBR 分区方案
Partition table scan:
MBR: MBR only
BSD: not present
APM: not present
GPT: not present

***************************************************************
Found invalid GPT and valid MBR; converting MBR to GPT format in memory.
***************************************************************

Disk /dev/sdb: 41943040 sectors, 20.0 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): 1038836E-C6BD-48D3-A490-8FFF2133146B
Partition table holds up to 128 entries
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

1.5.3 转换分区表方案

gdisk 工具用于管理 GPT 分区,因此我们需要将磁盘的分区表方案由 MBR 转换为 GPT。

[chenxin@cserver~ 20:16:09]$ gdisk /dev/sdb
GPT fdisk (gdisk) version 0.8.10

Partition table scan:
MBR: MBR only
BSD: not present
APM: not present
GPT: not present

***************************************************************
Found invalid GPT and valid MBR; converting MBR to GPT format
in memory. THIS OPERATION IS POTENTIALLY DESTRUCTIVE! Exit by
typing 'q' if you don't want to convert your MBR partitions
to GPT format!
***************************************************************

# 输入 ? 查看帮助信息。gdisk 的管理命令与 fdisk 很相似。
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

# 输入 o,将磁盘的分区表方案由 MBR 转换为 GPT
Command (? for help): o
This option deletes all partitions and creates a new protective MBR.
Proceed? (Y/N): Y

# 输入 p,查看分区表
Command (? for help): p
Disk /dev/sdb: 41943040 sectors, 20.0 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): 99FE6E5F-8316-428D-BA81-D824C786CC43
Partition table holds up to 128 entries
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

# 输入 w,保存更改并退出
# 输入 q,不保存更改并退出
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.

1.5.4 创建分区

[chenxin@cserver~ 20:16:25]$ gdisk /dev/sdb
GPT fdisk (gdisk) version 0.8.10

# 这里识别到 GPT 分区方案
Partition table scan:
MBR: protective
BSD: not present
APM: not present
GPT: present

Found valid GPT with protective MBR; using GPT.

# 输入 n,创建新分区
Command (? for help): `n`

# 设置分区编号
Partition number (1-128, default 1): `回车`

# 设置分区起始扇区
First sector (34-41943006, default = 2048) or {+-}size{KMGTP}: `回车`

# 设置分区结束扇区
Last sector (2048-41943006, default = 41943006) or {+-}size{KMGTP}: `+2G`
Current type is 'Linux filesystem'

# 设置分区类型,输入L查看所有分区类型
Hex code or GUID (L to show codes, Enter = 8300): `L`
0700 Microsoft basic data 0c01 Microsoft reserved 2700 Windows RE
3000 ONIE boot 3001 ONIE config 4100 PowerPC PReP boot
4200 Windows LDM data 4201 Windows LDM metadata 7501 IBM GPFS
7f00 ChromeOS kernel 7f01 ChromeOS root 7f02 ChromeOS reserved
8200 Linux swap 8300 Linux filesystem 8301 Linux reserved
8302 Linux /home 8400 Intel Rapid Start 8e00 Linux LVM
a500 FreeBSD disklabel a501 FreeBSD boot a502 FreeBSD swap
a503 FreeBSD UFS a504 FreeBSD ZFS a505 FreeBSD Vinum/RAID
a580 Midnight BSD data a581 Midnight BSD boot a582 Midnight BSD swap
a583 Midnight BSD UFS a584 Midnight BSD ZFS a585 Midnight BSD Vinum
a800 Apple UFS a901 NetBSD swap a902 NetBSD FFS
a903 NetBSD LFS a904 NetBSD concatenated a905 NetBSD encrypted
a906 NetBSD RAID ab00 Apple boot af00 Apple HFS/HFS+
af01 Apple RAID af02 Apple RAID offline af03 Apple label
af04 AppleTV recovery af05 Apple Core Storage be00 Solaris boot
bf00 Solaris root bf01 Solaris /usr & Mac Z bf02 Solaris swap
bf03 Solaris backup bf04 Solaris /var bf05 Solaris /home
bf06 Solaris alternate se bf07 Solaris Reserved 1 bf08 Solaris Reserved 2
bf09 Solaris Reserved 3 bf0a Solaris Reserved 4 bf0b Solaris Reserved 5
c001 HP-UX data c002 HP-UX service ea00 Freedesktop $BOOT
eb00 Haiku BFS ed00 Sony system partitio ed01 Lenovo system partit

# 输入回车继续查看
Press the <Enter> key to see more codes:`回车`
ef00 EFI System ef01 MBR partition scheme ef02 BIOS boot partition
fb00 VMWare VMFS fb01 VMWare reserved fc00 VMWare kcore crash p
fd00 Linux RAID

# 输入回车,选择默认分区类型
Hex code or GUID (L to show codes, Enter = 8300):
Changed type of partition to 'Linux filesystem'

# 输入p,查看分区表
Command (? for help): p
Disk /dev/sdb: 41943040 sectors, 20.0 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): 1A3D170A-56FC-4655-8B9C-01E1B89294FE
Partition table holds up to 128 entries
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

1.5.5 更改分区名称

# 输入 c,修改分区 ID 为 1 的分区名称
Command (? for help): c
Using 1
# 输入新名称
Enter name: data01

# 查看分区表
Command (? for help): p
Disk /dev/sdb: 41943040 sectors, 20.0 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): 1A3D170A-56FC-4655-8B9C-01E1B89294FE
Partition table holds up to 128 entries
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 data01

1.5.6 查看分区详细信息

Command (? for help): i
Selected partition 1
Partition GUID code: 0FC63DAF-8483-4772-8E79-3D69D8477DE4 (Linux filesystem)
Partition unique GUID: 19AE5CDC-1AB2-4EBA-B9E2-AE9E7BF42653
First sector: 2048 (at 1024.0 KiB)
Last sector: 4196351 (at 2.0 GiB)
Partition size: 4194304 sectors (2.0 GiB)
Attribute flags: 0000000000000000
Partition name: 'data01'

1.5.7 删除分区

# 输入 d,删除分区 ID 为 1 的分区
Command (? for help): d
Using 1

Command (? for help): p
Disk /dev/sdb: 41943040 sectors, 20.0 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): 1A3D170A-56FC-4655-8B9C-01E1B89294FE
Partition table holds up to 128 entries
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

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.

1.6 wipefs 工具

作用:查看和擦除磁盘上的文件系统、RAID 或分区表签名等管理信息。

注意:数据无价,操作前务必谨慎,建议提前备份。

# 查看磁盘上的签名信息
[chenxin@cserver~ 20:16:39]$ wipefs /dev/sdb

# 擦除磁盘上的所有签名(适用于未挂载的磁盘)
[chenxin@cserver~ 20:16:51]$ wipefs -a /dev/sdb

# 强制擦除所有签名(谨慎使用 -f 选项)
[chenxin@cserver~ 20:17:07]$ wipefs -fa /dev/sdb

1.7 parted 工具

parted 工具既可以管理采用 MBR 分区方案的磁盘,也可以管理采用 GPT 分区方案的磁盘。

parted 命令同时支持交互式操作和非交互式操作(适用于脚本编写)。

我们先来看交互式操作。

操作流程如下:

  • 查看分区表。如果是未初始化的硬盘,则需创建分区。
  • 设置单位(MiB)
  • 创建分区
  • 调整分区大小
  • 调整分区类型
  • 删除分区
  • 1.7.1 查看分区表

    [chenxin@cserver~ 20:17:20]$ parted /dev/sdb
    GNU Parted 3.1
    Using /dev/sdb

    # 查看帮助,输入 help
    Welcome to GNU Parted! Type 'help' to view a list of commands.
    [chenxin@cserver~ 20:17:29]$ `help`
    align-check TYPE N check partition N for TYPE(min|opt) alignment
    help [COMMAND] print general help, or help on COMMAND
    mklabel,mktable LABEL-TYPE create a new disklabel (partition table)
    mkpart PART-TYPE [FS-TYPE] START END make a partition
    name NUMBER NAME name partition NUMBER as NAME
    print [devices|free|list,all|NUMBER] display the partition table, available devices, free space, all found partitions, or a particular partition
    quit exit program
    rescue START END rescue a lost partition near START and END

    resizepart NUMBER END resize partition NUMBER
    rm NUMBER delete partition NUMBER
    select DEVICE choose the device to edit
    disk_set FLAG STATE change the FLAG on selected device
    disk_toggle [FLAG] toggle the state of FLAG on selected device
    set NUMBER FLAG STATE change the FLAG on partition NUMBER
    toggle [NUMBER [FLAG]] toggle the state of FLAG on partition NUMBER
    unit UNIT set the default unit to UNIT
    version display the version number and copyright
    information of GNU Parted

    # 查看分区表,输入 print
    [chenxin@cserver~ 20:17:37]$ `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.7.2 设置单位

    parted 工具默认单位是 MB(10^3 KB),可设置为 MiB(2^10 KiB)

    [chenxin@cserver ~ 20:17:52]$ unit MiB

    1.7.3 管理 MBR 磁盘
    1.7.3.1 设置磁盘分区管理方案

    # 使用 mklabel 或 mktable 设置磁盘分区管理方案
    # 设置分区方案为 msdos(即 MBR),输入 mklabel msdos
    [chenxin@cserver~ 20:18:00]$ `mklabel msdos`
    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?
    # 确认更改,输入y
    Yes/No? `y`
    [chenxin@cserver~ 20:18:09]$ print
    Model: VMware, VMware Virtual S (scsi)
    Disk /dev/sdb: 21.5GB
    Sector size (logical/physical): 512B/512B
    # 分区表已改为 msdos
    Partition Table: `msdos`
    Disk Flags:

    Number Start End Size Type File system Flags

    注意: parted 命令所做的更改会立即生效。

    1.7.3.2 创建分区

    # 创建分区,输入 mkpart
    [chenxin@cserver~ 20:18:23]$ `mkpart`

    # 设置分区类型,输入 primary
    Partition type? primary/extended? `primary`

    # 设置分区文件系统类型,输入 xfs(此设置仅标记类型,实际格式化仍需后续手动操作)
    File system type? [ext2]? `xfs`

    # 设置分区起始位置,输入 1
    Start? `1`

    # 设置分区结束位置,输入 2049
    End? `2049`
    [chenxin@cserver~ 20:18:40]$ `print`
    Model: VMware, VMware Virtual S (scsi)
    Disk /dev/sdb: 20480MiB
    Sector size (logical/physical): 512B/512B
    Partition Table: msdos
    Disk Flags:

    Number Start End Size Type File system Flags
    1 1.00MiB 2049MiB 2048MiB primary xfs

    1.7.3.3 扩展分区容量

    # 1 代表分区号,4097 代表分区结束位置
    [chenxin@cserver ~ 20:18:52]$ resizepart 1 4097
    [chenxin@cserver ~ 20:19:03]$ print
    Model: VMware, VMware Virtual S (scsi)
    Disk /dev/sdb: 20480MiB
    Sector size (logical/physical): 512B/512B
    Partition Table: msdos
    Disk Flags:

    Number Start End Size Type File system Flags
    1 1.00MiB 4097MiB 4096MiB primary xfs

    1.7.3.4 删除分区

    # 其中 1 代表分区号
    [chenxin@cserver~ 20:19:14]$ rm 1
    [chenxin@cserver~ 20:19:32]$ print
    Model: VMware Virtual S (scsi)
    Disk /dev/sdb: 20480MiB
    Sector size (logical/physical): 512B/512B
    Partition Table: msdos
    Disk Flags:

    Number Start End Size Type File system Flags

    1.7.3.5 免交互操作

    # 设置磁盘分区表类型
    [chenxin@cserver~ 20:19:43]$ parted /dev/sdb mklabel msdos
    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
    Information: You may need to update /etc/fstab.

    # 查看分区信息
    [chenxin@cserver~ 20:20:01]$ parted /dev/sdb unit MiB print
    Model: VMware, VMware Virtual S (scsi)
    Disk /dev/sdb: 20480MiB
    Sector size (logical/physical): 512B/512B
    Partition Table: msdos
    Disk Flags:

    Number Start End Size Type File system Flags

    # 创建主分区
    [chenxin@cserver~ 20:20:15]$ parted /dev/sdb unit MiB mkpart primary 1 2049
    Information: You may need to update /etc/fstab.

    [chenxin@cserver~ 20:20:31]$ parted /dev/sdb unit MiB print
    Model: VMware, VMware Virtual S (scsi)
    Disk /dev/sdb: 20480MiB
    Sector size (logical/physical): 512B/512B
    Partition Table: msdos
    Disk Flags:

    Number Start End Size Type File system Flags
    1 1.00MiB 2049MiB 2048MiB primary xfs

    # 调整分区大小
    [chenxin@cserver~ 20:20:42]$ parted /dev/sdb unit MiB resizepart 1 5121
    Information: You may need to update /etc/fstab.

    [chenxin@cserver~ 20:21:00]$ parted /dev/sdb unit MiB print
    Model: VMware, VMware Virtual S (scsi)
    Disk /dev/sdb: 20480MiB
    Sector size (logical/physical): 512B/512B
    Partition Table: msdos
    Disk Flags:

    Number Start End Size Type File system Flags
    1 1.00MiB 5121MiB 5120MiB primary xfs

    # 删除分区
    [chenxin@cserver~ 20:21:11]$ parted /dev/sdb rm 1
    Information: You may need to update /etc/fstab.

    [chenxin@cserver~ 20:21:26]$ parted /dev/sdb unit MiB print
    Model: VMware, VMware Virtual S (scsi)
    Disk /dev/sdb: 20480MiB
    Sector size (logical/physical): 512B/512B
    Partition Table: msdos
    Disk Flags:

    Number Start End Size Type File system Flags

    1.7.4 管理 GPT 磁盘

    管理GPT磁盘与管理MBR磁盘基本一致,主要区别在于创建分区。

    1.7.4.1 设置磁盘分区管理方案

    # 设置分区方案为gpt,输入mklabel gpt
    [chenxin@cserver~ 20:21:34]$ `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
    [chenxin@cserver~ 20:21:47]$ print
    Model: VMware, VMware Virtual S (scsi)
    Disk /dev/sdb: 21.5GB
    Sector size (logical/physical): 512B/512B
    # 分区表已更改为 GPT
    Partition Table: `gpt`
    Disk Flags:

    Number Start End Size File system Name Flags

    1.7.4.2 创建分区

    [chenxin@cserver~ 20:21:56]$ unit MiB
    [chenxin@cserver~ 20:22:11]$ mkpart

    # 设置分区名
    Partition name? []? data01
    File system type? [ext2]? xfs
    Start? 1
    End? 2049
    [chenxin@cserver~ 20:22:26]$ print
    Model: VMware, VMware Virtual S (scsi)
    Disk /dev/sdb: 20480MiB
    Sector size (logical/physical): 512B/512B
    Partition Table: gpt
    Disk Flags:

    Number Start End Size File system Name Flags
    1 1.00MiB 2049MiB 2048MiB xfs data01

    1.7.4.3 调整分区大小

    [chenxin@cserver~ 20:22:35]$ resizepart 1 5121
    [chenxin@cserver~ 20:22:47]$ print
    Model: VMware, VMware Virtual S (scsi)
    Disk /dev/sdb: 20480MiB
    Sector size (logical/physical): 512B/512B
    Partition Table: gpt
    Disk Flags:

    Number Start End Size File system Name Flags
    1 1.00MiB 5121MiB 5120MiB xfs data01

    1.7.4.4 删除分区

    [chenxin@cserver~ 20:22:55]$ rm 1
    [chenxin@cserver~ 20:23:03]$ parted /dev/sdb print
    Model: VMware, VMware Virtual S (scsi)
    Disk /dev/sdb: 20480MiB
    Sector size (logical/physical): 512B/512B
    Partition Table: gpt
    Disk Flags:

    Number Start End Size File system Name Flags

    1.7.4.5 免交互操作

    # 设置磁盘分区表方案
    [chenxin@cserver~ 20:23:18]$ parted /dev/sdb 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
    Information: You may need to update /etc/fstab.

    # 查看分区信息
    [chenxin@cserver~ 20:23:32]$ 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

    # 创建新分区
    [chenxin@cserver~ 20:23:44]$ parted /dev/sdb unit MiB mkpart data01 xfs 1 2049
    Information: You may need to update /etc/fstab.

    [chenxin@cserver~ 20:23:52]$ parted /dev/sdb unit MiB print
    Model: VMware, VMware Virtual S (scsi)
    Disk /dev/sdb: 20480MiB
    Sector size (logical/physical): 512B/512B
    Partition Table: gpt
    Disk Flags:

    Number Start End Size File system Name Flags
    1 1.00MiB 2049MiB 2048MiB xfs data01

    # 调整分区大小
    [chenxin@cserver~ 20:24:01]$ parted /dev/sdb unit MiB resizepart 1 5121
    Information: You may need to update /etc/fstab.

    [chenxin@cserver~ 20:24:17]$ parted /dev/sdb unit MiB print
    Model: VMware, VMware Virtual S (scsi)
    Disk /dev/sdb: 20480MiB
    Sector size (logical/physical): 512B/512B
    Partition Table: gpt
    Disk Flags:

    Number Start End Size File system Name Flags
    1 1.00MiB 5121MiB 5120MiB xfs data01

    # 删除指定分区
    [chenxin@cserver~ 20:24:30]$ parted /dev/sdb rm 1
    Information: You may need to update /etc/fstab.

    [chenxin@cserver~ 20:24:41]$ parted /dev/sdb unit MiB print
    Model: VMware, VMware Virtual S (scsi)
    Disk /dev/sdb: 20480MiB
    Sector size (logical/physical): 512B/512B
    Partition Table: gpt
    Disk Flags:

    Number Start End Size File system Name Flags

    1.8 文件系统持久化挂载

    1.8.1 环境准备

    使用 parted 创建一个分区,并将其格式化为 XFS 文件系统。

    [chenxin@cserver~ 20:24:52]$ parted /dev/sdb 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
    Information: You may need to update /etc/fstab.

    [chenxin@cserver~ 20:25:05]$ parted /dev/sdb unit MiB mkpart data01 xfs 1 2049
    Information: You may need to update /etc/fstab.

    [chenxin@cserver~ 20:25:15]$ mkfs.xfs /dev/sdb1
    meta-data=/dev/sdb1 isize=512 agcount=4, agsize=131072 blks
    = sectsz=512 attr=2, projid32bit=1
    = crc=1 finobt=0, sparse=0
    data = bsize=4096 blocks=524288, 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

    # 如果格式化时提示已存在文件系统,则需要使用 -f 选项强制格式化
    [chenxin@cserver~ 20:25:26]$ mkfs.xfs /dev/sdb1
    mkfs.xfs: /dev/sdb1 appears to contain an existing filesystem (xfs).
    mkfs.xfs: Use the -f option to force overwrite.
    [chenxin@cserver~ 20:25:34]$ mkfs.xfs -f /dev/sdb1

    1.8.2 持久化挂载

    当服务器重启时,系统不会自动将文件系统再次挂载到目录树上,导致用户无法访问。为了确保系统在启动时自动挂载文件系统,需要在 /etc/fstab 文件中添加一个条目。

    /etc/fstab 是一个以空格分隔的文件,每行包含六个字段。

    • 第一个字段指定设备。 可以使用 UUID 或设备路径(如 /dev/sdb1)来指定设备。
    • 第二个字段是挂载点目录。 通过该目录可以访问块设备上的文件系统。挂载点目录必须存在;如果不存在,请使用 mkdir 命令创建。
    • 第三个字段包含文件系统类型,如xfs或ext4 。
    • 第四个字段是挂载选项,以逗号分隔的。 defaults是一组常用选项。详细信息参考mount(8) 。
    • 第五个字段指定dump命令是否备份设备。
    • 第六个字段指定 fsck 顺序,决定系统启动时是否运行 fsck 检查文件系统。XFS 设为 0(XFS 不使用 fsck);ext4 根文件系统设为 1,其他 ext4 文件系统设为 2,使 fsck 先处理根文件系统,再并行检查不同磁盘、顺序检查同一磁盘。

    示例:

    [chenxin@cserver~ 20:25:46]$ blkid /dev/sdb1
    /dev/sdb1: UUID="f5c35f10-0274-45af-b044-73694989fe01" TYPE="xfs" PARTLABEL="data01" PARTUUID="432d7d79-1a08-4429-bf38-7ab1444a0ab9"
    [chenxin@cserver~ 20:25:56]$ mkdir /data01

    [chenxin@cserver~ 20:26:14]$ vim /etc/fstab
    # 最后一行增加一个条目
    UUID="f5c35f10-0274-45af-b044-73694989fe01" /data01 xfs defaults 0 0

    # 使用如下命令立刻挂载
    [chenxin@cserver~ 20:26:29]$ mount /data01
    # 或者
    [chenxin@cserver~ 20:26:46]$ mount /dev/sdb1

    # 验证
    [chenxin@cserver~ 20:26:59]$ df -h /data01
    Filesystem Size Used Avail Use% Mounted on
    /dev/sdb1 2.0G 33M 2.0G 2% /data01

    # 重启系统验证
    [chenxin@cserver~ 20:27:16]$ reboot
    [chenxin@cserver~ 20:27:30]$ df -h /data01
    Filesystem Size Used Avail Use% Mounted on
    /dev/sdb1 2.0G 33M 2.0G 2% /data01

    1.8.3 取消持久化挂载

    删除 /etc/fstab 中对应的条目即可。

    1.9 综合案例:文件系统空间不足

    1.9.1 情况1:大量的大文件占用空间

    准备环境

    [chenxin@cserver~ 20:27:48]$ parted /dev/sdb mklabel gpt
    [chenxin@cserver~ 20:28:04]$ parted /dev/sdb unit MiB mkpart data01 xfs 1 2049
    [chenxin@cserver~ 20:28:14]$ mkfs.xfs -f /dev/sdb1
    [chenxin@cserver~ 20:28:29]$ mkdir /myapp-1
    [chenxin@cserver~ 20:28:44]$ mount /dev/sdb1 /myapp-1
    [chenxin@cserver~ 20:28:52]$ cp -r /etc/ /myapp-1/

    # 创建一个大文件
    [chenxin@cserver~ 20:29:00]$ dd if=/dev/zero of=/myapp-1/etc/bigfile bs=1M count=2000

    原因:大文件占用大量空间。

    解决方法:找到文件后删除。

    [chenxin@cserver~ 20:29:11]$ df -h /myapp-1/
    文件系统 容量 已用 可用 已用% 挂载点
    /dev/sdb1 2.0G 2.0G 436K 100% /myapp-1

    # 方法1:find 查找
    [chenxin@cserver~ 20:29:19]$ find /myapp-1/ -size +100M
    /myapp-1/etc/bigfile

    # 方法2:du 查找
    [chenxin@cserver~ 20:29:36]$ du -s /myapp-1/* |sort -n |tail -2
    2051628 /myapp-1/etc
    [chenxin@cserver~ 20:29:51]$ du -s /myapp-1/etc/* |sort -n |tail -2
    23220 /myapp-1/etc/selinux
    2008512 /myapp-1/etc/bigfile
    [chenxin@cserver~ 20:30:05]$ du -s /myapp-1/etc/bigfile/* |sort -n |tail -2
    du: 无法访问"/myapp-1/etc/bigfile/*": 不是目录

    # 删除大文件
    [chenxin@cserver~ 20:30:22]$ rm -f /myapp-1/etc/bigfile

    [chenxin@cserver~ 20:30:37]$ df -h /myapp-1/
    文件系统 容量 已用 可用 已用% 挂载点
    /dev/sdb1 2.0G 77M 2.0G 4% /myapp-1

    找到文件后,删除即可。

    1.9.2 情况2:删除文件后,空间没有释放

    准备环境

    [chenxin@cserver~ 20:30:53]$ dd if=/dev/zero of=/myapp-1/etc/bigfile bs=1M count=2000
    [chenxin@cserver~ 20:31:11]$ tail -f /myapp-1/etc/bigfile &

    # 删除文件后,空间没有释放
    [chenxin@cserver~ 20:31:29]$ rm -f /myapp-1/etc/bigfile
    [chenxin@cserver~ 20:31:39]$ df -h /myapp-1/
    文件系统 容量 已用 可用 已用% 挂载点
    /dev/sdb1 2.0G 2.0G 772K 100% /myapp-1

    原因:被删除的文件,仍然有程序在使用。

    解决方法:找到像一个的程序并,终止程序。

    [chenxin@cserver~ 20:31:48]$ lsof |grep delete |grep /myapp-1
    tail 1654 root 3r REG 8,17 2061565952 444 /myapp-1/etc/bigfile (deleted)

    [chenxin@cserver~ 20:31:57]$ kill 1654

    [chenxin@cserver~ 20:32:09]$ df -h /myapp-1/
    文件系统 容量 已用 可用 已用% 挂载点
    /dev/sdb1 2.0G 72M 2.0G 4% /myapp-1

    建议:清理大文件,先使用重定向清空文件内容,再删除文件。

    1.9.3 情况3:大量的小文件占用空间

    准备环境

    [chenxin@cserver~ 20:32:27]$ parted /dev/sdb unit MiB mkpart data02 ext4 2049 3073
    [chenxin@cserver~ 20:32:40]$ mkfs.ext4 /dev/sdb2
    [chenxin@cserver~ 20:32:49]$ mkdir /myapp-2
    [chenxin@cserver~ 20:33:00]$ mount /dev/sdb2 /myapp-2
    [chenxin@cserver~ 20:33:18]$ df -i /myapp-2
    文件系统 Inode 已用(I) 可用(I) 已用(I)% 挂载点
    /dev/sdb2 65536 11 65525 1% /myapp-2

    [chenxin@cserver~ 20:33:26]$ touch /myapp-2/file-{00001..65530}
    touch: 无法创建"/myapp-2/file-65526": 设备上没有空间
    touch: 无法创建"/myapp-2/file-65527": 设备上没有空间
    touch: 无法创建"/myapp-2/file-65528": 设备上没有空间
    touch: 无法创建"/myapp-2/file-65529": 设备上没有空间
    touch: 无法创建"/myapp-2/file-65530": 设备上没有空间

    [chenxin@cserver~ 20:33:43]$ df -h /myapp-2/
    Filesystem Size Used Avail Use% Mounted on
    /dev/sdb2 974M 1.8M 905M 1% /myapp-2

    原因:文件系统中inode使用完了。

    解决方法:删除大量的小文件或者将这些小文件备份到其他地方。

    [chenxin@cserver~ 20:33:59]$ df -hi /myapp-2
    Filesystem Inodes IUsed IFree IUse% Mounted on
    /dev/sdb2 64K 64K 0 100% /myapp-2

    # 思路1:直接将这些小文件移走或删除
    [chenxin@cserver~ 20:34:17]$ rm -f /myapp-2/file-0*

    # 思路2:合并大量小文件为单个文件
    [chenxin@cserver~ 20:34:28]$ cat /myapp-2/file-{00001..10000} > file-00001

    1.10 思考:如何彻底擦除硬盘上数据

    解答:

  • 逻辑破坏:用0填充所有数据,例如 dd if=/dev/zero of=/dev/sdb 。
  • 物理破坏,例如消磁。
  • 2. Linux RAID 存储技术

    2.1 环境准备

    虚拟机添加6块20G 硬盘,sdb sdc sdd sde sdf sdg

    2.2 RAID 技术历史

    1988 年,美国加州大学伯克利分校 D. A. Patterson 教授等人在论文 A Case of Redundant Array of Inexpensive Disks 中首次提出 RAID 概念,即廉价磁盘冗余阵列(Redundant Array of Inexpensive Disks)。其核心思想是将多个容量较小、相对廉价的磁盘有机组合,以较低成本获得与大容量磁盘相当的容量、性能和可靠性。随着磁盘成本不断下降,"廉价"一词已无实际意义,RAID 咨询委员会(RAB)将其更名为独立磁盘冗余阵列(Redundant Array of Independent Disks),实质内容未变。

    2.3 RAID 实现方式

    从实现角度看, RAID 主要分为:

    • 软 RAID:所有功能均有操作系统和 CPU 来完成,没有独立的 RAID 控制 / 处理芯片和 I/O 处理芯片,效率最低。
    • 硬 RAID :配备了专门的 RAID 控制 / 处理芯片和 I/O 处理芯片以及阵列缓冲,不占用 CPU 资源,成本很高。
    • 软硬混合 RAID:具备 RAID 控制 / 处理芯片,但缺乏 I/O 处理芯片,需要 CPU 和驱动程序来完成,性能和成本在软 RAID 和硬 RAID 之间。

    2.4 RAID 级别

    RAID 主要利用数据条带(striping)、镜像(mirroring)和数据校验三种技术来获取高性能、可靠性、容错能力和扩展性,根据三者的不同组合策略可划分为不同等级。Patterson 等的论文定义了 RAID1~RAID5 原始等级,后又扩展出 RAID0 和 RAID6。厂商陆续推出的 RAID7、RAID10/01、RAID50 等等级并无统一标准,实际应用中最常用的是 RAID0、RAID1、RAID5、RAID6 和 RAID10。各等级之间无高低之分,应根据可用性、性能和成本综合选择。

    raid级别

    下面详细介绍 RAID 的各个级别。

    2.4.1 RAID 0
    2.4.1.1 原理

    RAID 0使用数据条带化(striping)的方式将数据分散存储在多个磁盘驱动器上,而不进行冗余备份。数据被分成固定大小的块,并依次存储在每个磁盘上。例如,如果有两个驱动器(驱动器A和驱动器B),一块数据的第一个部分存储在驱动器A上,第二个部分存储在驱动器B上,以此类推。这种条带化的方式可以同时从多个驱动器读取或写入数据,从而提高系统的性能。

    raid0

    2.4.1.2 适用场景

    RAID 0适用于需要高性能而不关心数据冗余的场景。以下是几种适合使用RAID 0的场景:

  • 视频编辑和处理:在视频编辑中,需要快速读取和写入大量数据。RAID 0可以通过并行读写操作提高数据传输速度,加快视频编辑和处理的速度。
  • 大型数据库应用:对于需要频繁访问和查询数据库的应用程序,RAID 0可以提供更快的数据访问速度,加快数据库操作的响应时间。
  • 实时流媒体:对于需要实时传输和处理大量数据的流媒体应用,RAID 0可以提供足够的带宽和吞吐量,确保流媒体内容的平滑播放。
  • 2.4.1.3 优点

    RAID 0具有以下优点:

  • 高性能:通过数据条带化和并行读写操作,RAID 0可以提供更快的数据传输速度和更高的系统性能。
  • 成本效益:相对于其他RAID级别(如RAID 1或RAID 5),RAID 0不需要额外的磁盘用于冗余备份,因此在成本上更具竞争力。
  • 2.4.1.4 缺点

    RAID 0也存在一些缺点:

  • 缺乏冗余:由于RAID 0不提供数据冗余,如果任何一个驱动器发生故障,所有数据都可能丢失。因此,RAID 0不适合存储关键数据。
  • 可靠性降低:由于没有冗余备份,RAID 0的可靠性相对较低。如果任何一个驱动器发生故障,整个阵列的可用性将受到影响。
  • 2.4.2 RAID 1
    2.4.2.1 原理

    RAID 1使用数据镜像(mirroring)的方式将数据完全复制到两个或多个磁盘驱动器上。当写入数据时,数据同时写入所有驱动器。这样,每个驱动器都具有相同的数据副本,从而实现数据的冗余备份。如果其中一个驱动器发生故障,系统可以继续从剩余的驱动器中读取数据,确保数据的可用性和完整性。

    raid1

    2.4.2.2 适用场景

    RAID 1适用于对数据冗余和高可用性要求较高的场景。以下是几种适合使用RAID 1的场景:

  • 关键数据存储:对于关键数据的存储,如企业的财务数据、客户信息等,RAID 1可以提供数据冗余备份,以防止数据丢失。
  • 数据库服务器:对于需要高可用性和容错性的数据库服务器,RAID 1可以确保数据的持久性和可用性,即使一个驱动器发生故障,也可以从其他驱动器中读取数据。
  • 文件服务器:对于共享文件的服务器,RAID 1可以提供冗余备份,确保文件的可靠性和高可用性。
  • 2.4.2.3 优点

    RAID 1具有以下优点:

  • 数据冗余备份:RAID 1通过数据镜像将数据完全复制到多个驱动器上,提供冗余备份,保护数据免受驱动器故障的影响。
  • 高可用性:由于数据的冗余备份,即使一个驱动器发生故障,系统仍然可以从其他驱动器中读取数据,保证数据的可用性和连续性。
  • 读取性能提升:RAID 1可以通过并行读取数据的方式提升读取性能,从而加快数据访问速度。
  • 2.4.2.4 缺点

    RAID 1也存在一些缺点:

  • 成本增加:由于需要额外的磁盘用于数据冗余备份,RAID 1的成本相对较高。需要考虑额外的硬件成本。
  • 写入性能略低:由于数据需要同时写入多个驱动器,相对于单个驱动器的写入性能,RAID 1的写入性能可能略低。
  • 2.4.3 RAID 5
    2.4.3.1 原理

    RAID 5使用数据条带化(striping)的方式将数据分散存储在多个磁盘驱动器上,并通过分布式奇偶校验实现数据的冗余备份。数据和奇偶校验信息被组织成数据块,其中奇偶校验信息被分布式存储在不同的驱动器上。当写入数据时,奇偶校验信息也会被更新。如果其中一个驱动器发生故障,系统可以通过重新计算奇偶校验信息来恢复丢失的数据。这种方式可以同时提供性能增强和数据冗余。

    raid5

    2.4.3.2 适用场景

    RAID 5适用于需要性能增强和数据冗余的场景。以下是几种适合使用RAID 5的场景:

  • 文件服务器:对于文件服务器,RAID 5可以提供高性能的数据访问和数据冗余备份,确保文件的安全性和可用性。
  • 数据库服务器:对于需要高性能和数据冗余的数据库服务器,RAID 5可以提供快速的数据读取和写入,同时保护数据免受驱动器故障的影响。
  • 小型企业环境:对于小型企业,RAID 5提供了经济实惠的解决方案,同时提供了性能和数据冗余的好处。
  • 2.4.3.3 优点

    RAID 5具有以下优点:

  • 性能增强:通过数据条带化和并行读写操作,RAID 5可以提供较高的数据传输速度和系统性能。
  • 数据冗余备份:通过分布式奇偶校验,RAID 5可以提供数据的冗余备份,保护数据免受驱动器故障的影响。
  • 成本效益:相对于其他RAID级别(如RAID 1),RAID 5只需要额外一个驱动器用于奇偶校验信息,从而在成本上更具竞争力。
  • 2.4.3.4 缺点

    RAID 5也存在一些缺点:

  • 写入性能受限:由于写入数据时需要重新计算奇偶校验信息,相对于读取操作,RAID 5的写入性能较低。
  • 驱动器故障期间的数据完整性:如果一个驱动器发生故障,系统在恢复数据时需要进行计算,这可能导致数据访问速度较慢,并且在此期间可能会有数据完整性的风险。
  • 2.4.4 RAID 6
    2.4.4.1 原理

    RAID 6使用数据条带化(striping)的方式将数据分散存储在多个磁盘驱动器上,并通过分布式奇偶校验和双重奇偶校验实现数据的冗余备份。数据和奇偶校验信息被组织成数据块,其中奇偶校验信息被分布式存储在不同的驱动器上,并通过双重奇偶校验提供更高的数据冗余性。当写入数据时,奇偶校验信息也会被更新。如果其中两个驱动器发生故障,系统可以通过重新计算奇偶校验信息来恢复丢失的数据。这种方式可以同时提供性能增强和更高级别的数据冗余。

    raid6

    2.4.4.2 适用场景

    RAID 6适用于需要更高级别的数据冗余和性能增强的场景。以下是几种适合使用RAID 6的场景:

  • 大容量存储系统:对于需要大容量存储和数据冗余备份的系统,如大型文件服务器或存档系统,RAID 6可以提供更高级别的数据冗余性。
  • 长时间运行的应用程序:对于需要长时间运行的关键应用程序,如数据库服务器,RAID 6可以提供更高级别的数据冗余和故障容忍性。
  • 虚拟化环境:在虚拟化环境中,需要高性能和更高级别的数据冗余来支持多个虚拟机的运行。RAID 6可以满足这些要求。
  • 2.4.4.3 优点

    RAID 6具有以下优点:

  • 更高级别的数据冗余:通过分布式奇偶校验和双重奇偶校验,RAID 6可以提供更高级别的数据冗余性,即使同时发生两个驱动器故障,仍能恢复丢失的数据。
  • 性能增强:通过数据条带化和并行读写操作,RAID 6可以提供较高的数据传输速度和系统性能。
  • 2.4.4.4 缺点

    RAID 6也存在一些缺点:

  • 写入性能略低:由于数据需要同时写入多个驱动器,并进行双重奇偶校验计算,相对于读取操作,RAID 6的写入性能较低。
  • 较高的成本:由于需要额外的磁盘用于奇偶校验信息和更复杂的计算,RAID 6的成本相对较高。需要考虑额外的硬件成本。
  • 2.4.5 RAID 10
    2.4.5.1 原理

    RAID 10使用条带化(striping)的方式将数据分散存储在多个磁盘驱动器上,并通过镜像(mirroring)实现数据的冗余备份。数据被分成固定大小的块,并依次存储在不同的驱动器上,类似于RAID 0。然而,每个数据块都会被完全复制到另一个驱动器上,实现数据的冗余备份,类似于RAID 1。这样,RAID 10在提供性能增强的同时,也提供了数据的冗余保护。

    raid10

    2.4.5.2 适用场景

    RAID 10适用于需要高性能和数据冗余的场景。以下是几种适合使用RAID 10的场景:

  • 数据库服务器:对于需要高可用性和性能的数据库服务器,RAID 10可以提供快速的数据读取和写入,同时保护数据免受驱动器故障的影响。
  • 虚拟化环境:在虚拟化环境中,需要高性能和数据冗余来支持多个虚拟机的运行。RAID 10可以满足这些要求,提供性能增强和数据保护。
  • 关键业务应用:对于关键业务应用,如金融交易系统或在线电子商务平台,RAID 10可以提供高可用性和快速的数据访问,确保业务的连续性和稳定性。
  • 2.4.5.3 优点

    RAID 10具有以下优点:

  • 高性能:通过数据条带化和并行读写操作,RAID 10可以提供较高的数据传输速度和系统性能。
  • 数据冗余备份:通过数据镜像将数据完全复制到另一个驱动器上,RAID 10提供了数据的冗余备份,保护数据免受驱动器故障的影响。
  • 较高的可靠性:由于RAID 10采用镜像的方式进行数据冗余备份,即使一个驱动器发生故障,仍然可以从其他驱动器中读取数据,确保数据的可用性和连续性。
  • 快速的故障恢复:在RAID 10中,如果一个驱动器发生故障,系统可以直接从镜像驱动器中恢复数据,而无需进行复杂的计算,从而加快故障恢复的速度。
  • 2.4.5.4 缺点

    RAID 10也存在一些缺点:

  • 较高的成本:相对于其他RAID级别,RAID 10需要更多的驱动器用于数据镜像,从而增加了硬件成本。
  • 低效的空间利用:由于RAID 10的数据镜像特性,有效的存储容量只等于所有驱动器中一半的容量,因此空间利用率较低。
  • 2.4.6 RAID 50
    2.4.6.1 原理

    RAID 50使用条带化(striping)的方式将数据分散存储在多个RAID 5组中,并通过RAID 0的条带化方式对这些RAID 5组进行条带化。每个RAID 5组由多个磁盘驱动器组成,并使用分布式奇偶校验来提供数据冗余备份。RAID 0则通过将数据划分为固定大小的块,并将这些块依次存储在多个驱动器上,提供了更高的性能。这样,RAID 50既提供了数据冗余备份,又提供了性能增强。

    raid50

    2.4.6.2 适用场景

    RAID 50适用于需要高性能和更高级别的数据冗余的场景。以下是几种适合使用RAID 50的场景:

  • 大规模数据存储:对于需要大规模数据存储和数据冗余备份的系统,如视频编辑、数据分析或大型数据库,RAID 50可以提供高性能和较高级别的数据冗余性。
  • 图形渲染和动画制作:在图形渲染和动画制作领域,需要高性能的存储系统来处理大型文件和复杂的渲染任务。RAID 50可以满足这些要求,提供快速的数据读取和写入速度。
  • 虚拟化环境:在虚拟化环境中,需要高性能和更高级别的数据冗余来支持多个虚拟机的运行。RAID 50可以满足这些要求,提供性能增强和数据保护。
  • 2.4.6.3 优点

    RAID 50具有以下优点:

  • 高性能:通过数据条带化和并行读写操作,RAID 50可以提供较高的数据传输速度和系统性能。
  • 更高级别的数据冗余:由于采用了多个RAID 5组的方式,RAID 50提供了更高级别的数据冗余备份,即使同时发生多个驱动器故障,仍能恢复丢失的数据。
  • 2.4.6.4 缺点

    RAID 50也存在一些缺点:

  • 较高的成本:由于需要更多的驱动器用于数据条带化和数据冗余备份,RAID 50的硬件成本相对较高。
  • 配置和管理复杂性:由于涉及多个RAID 5组和驱动器,RAID 50的配置和管理相对复杂,需要更多的注意和维护。
  • 2.4.7 RAID 60
    2.4.7.1 原理

    RAID 60采用条带化(striping)的方式将数据分散存储在多个RAID 6组中,并通过RAID 0的条带化方式对这些RAID 6组进行条带化。每个RAID 6组由多个磁盘驱动器组成,并使用分布式奇偶校验来提供数据的冗余备份。RAID 0则通过将数据划分为固定大小的块,并将这些块依次存储在多个驱动器上,提供了更高的性能。这样,RAID 60既提供了更高级别的数据冗余备份,又提供了性能增强。

    raid60

    2.4.7.2 适用场景

    RAID 60适用于需要更高级别的数据冗余和更高性能的场景。以下是几种适合使用RAID 60的场景:

  • 大型数据库系统:对于大型数据库系统,需要高可用性、高性能和更高级别的数据冗余来确保数据的完整性和可靠性。RAID 60可以提供这些要求。
  • 大规模数据分析:在大规模数据分析领域,需要高性能的存储系统来处理大量数据的读取和写入。RAID 60可以满足这些要求,提供较高的数据传输速度和系统性能。
  • 视频流****媒体处理:对于视频流媒体处理应用,需要快速的数据读取和写入,以确保流畅的视频播放和高质量的媒体处理。RAID 60可以满足这些要求。
  • 2.4.7.3 优点

    RAID 60具有以下优点:

  • 更高级别的数据冗余:由于采用了多个RAID 6组的方式,RAID 60提供了更高级别的数据冗余备份,即使同时发生多个驱动器故障,仍能恢复丢失的数据。
  • 高性能:通过数据条带化和并行读写操作,RAID 60可以提供较高的数据传输速度和系统性能。
  • 2.4.7.4 缺点

    RAID 60也存在一些缺点:

  • 较高的成本:由于需要更多的驱动器用于数据条带化和数据冗余备份,RAID 60的硬件成本相对较高。
  • 配置和管理复杂性:由于涉及多个RAID 6组和驱动器,RAID 60的配置和管理相对复杂,需要更多的注意和维护。
  • 2.4.8 RAID 级别总结
    RAID级别最小磁盘数容错能力磁盘空间开销读取速度写入速度硬件成本
    RAID 0 2 0%
    RAID 1 2 单个磁盘 50%
    RAID 5 3 单个磁盘 1 / N
    RAID 6 4 两个磁盘 2 / N
    RAID 10 4 多个磁盘 50%
    RAID 50 6 多个磁盘 1 / N
    RAID 60 8 多个磁盘 50%

    2.5 管理软 RAID

    Linux 提供 mdadm 实用程序来创建和管理软件RAID。

    2.5.1 管理 RAID0
    2.5.1.1 创建 RAID

    [chenxin@cserver~ 20:34:46]$ yum install -y mdadm

    # 创建一个包含2个块设备的raid0设备/dev/md0
    [chenxin@cserver~ 20:34:55]$ mdadm –create /dev/md0 –level 0 –raid-devices 2 /dev/sd{b,c}
    mdadm: Defaulting to version 1.2 metadata
    mdadm: array /dev/md0 started.

    2.5.1.2 查看 RAID

    # 查看 raid 概要信息
    [chenxin@cserver~ 20:35:07]$ cat /proc/mdstat
    Personalities : [raid0] [raid1] [raid6] [raid5] [raid4]
    md0 : active raid0 sdc[1] sdb[0]
    41908224 blocks super 1.2 512k chunks

    unused devices: <none>

    # 查看 raid 设备详细信息
    [chenxin@cserver~ 20:35:23]$ mdadm –detail /dev/md0
    /dev/md0:
    Version : 1.2
    Creation Time : Fri Aug 2 17:03:58 2024
    Raid Level : raid0
    Array Size : 41908224 (39.97 GiB 42.91 GB)
    Raid Devices : 2
    Total Devices : 2
    Persistence : Superblock is persistent

    Update Time : Fri Aug 2 17:03:58 2024
    State : clean
    Active Devices : 2
    Working Devices : 2
    Failed Devices : 0
    Spare Devices : 0

    Layout : original
    Chunk Size : 512K

    Consistency Policy : none

    Name : centos7.linux.fun:0 (local to host centos7.linux.fun)
    UUID : afe03287:7cfb8b2e:82844a2e:8f721e04
    Events : 0

    Number Major Minor RaidDevice State
    0 8 16 0 active sync /dev/sdb
    1 8 32 1 active sync /dev/sdc

    需要关注的属性:

    • Raid Level : raid0
    • State : clean
    • Chunk Size : 512K
    • 设备清单

    [chenxin@cserver~ 20:35:39]$ lsblk /dev/md0
    NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
    md0 9:0 0 40G 0 raid0

    [chenxin@cserver~ 20:35:48]$ lsblk /dev/sdb /dev/sdc
    NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
    sdb 8:16 0 20G 0 disk
    └─md0 9:1 0 20G 0 raid0
    sdc 8:32 0 20G 0 disk
    └─md0 9:1 0 20G 0 raid0

    2.5.1.3 格式化和挂载

    [chenxin@cserver~ 20:35:59]$ mkfs.xfs /dev/md0
    [chenxin@cserver~ 20:36:11]$ mkdir -p /raid/raid0
    [chenxin@cserver~ 20:36:27]$ mount /dev/md0 /raid/raid0
    [chenxin@cserver~ 20:36:40]$ df -h /raid/raid0
    Filesystem Size Used Avail Use% Mounted on
    /dev/md0 40G 319M 40G 1% /raid/raid0

    # 创建数据
    [chenxin@cserver~ 20:36:58]$ cp /etc/ho* /raid/raid0
    [chenxin@cserver~ 20:37:08]$ ls /raid/raid0/
    host.conf hostname hosts

    2.5.1.4 删除 RAID

    # 卸载
    [chenxin@cserver~ 20:37:22]$ umount /dev/md0

    # stop RAID阵列,将删除阵列
    [chenxin@cserver~ 20:37:35]$ mdadm –stop /dev/md0
    mdadm: stopped /dev/md0

    # 清除原先设备上的 md superblock
    [chenxin@cserver~ 20:37:51]$ mdadm –zero-superblock /dev/sd{b,c}

    2.5.1.5 补充说明
    • raid0 条带不能增加新成员盘。

    [chenxin@cserver~ 20:38:01]$ mdadm –add /dev/md0 /dev/sdd
    mdadm: add new device failed for /dev/sdd as 2: Invalid argument

    • raid0 条带不能强制故障成员盘。

    [chenxin@cserver~ 20:38:17]$ mdadm –fail /dev/md0 /dev/sdc
    mdadm: Cannot remove /dev/sdc from /dev/md0, array will be failed.

    2.5.2 管理 RAID1
    2.5.2.1 创建 RAID

    # 创建一个包含2个块设备的raid1设备/dev/md1
    [chenxin@cserver~ 20:38:30]$ mdadm –create /dev/md1 –level 1 –raid-devices 2 /dev/sd{b,c}
    mdadm: Note: this array has metadata at the start and
    may not be suitable as a boot device. If you plan to
    store '/boot' on this device please ensure that
    your boot-loader understands md/v1.x metadata, or use
    –metadata=0.90
    Continue creating array? yes
    mdadm: Defaulting to version 1.2 metadata
    mdadm: array /dev/md1 started.

    2.5.2.2 查看 RAID

    [chenxin@cserver~ 20:38:38]$ mdadm –detail /dev/md1
    /dev/md1:
    Version : 1.2
    Creation Time : Fri Aug 2 17:41:25 2024
    Raid Level : raid1
    Array Size : 20954112 (19.98 GiB 21.46 GB)
    Used Dev Size : 20954112 (19.98 GiB 21.46 GB)
    Raid Devices : 2
    Total Devices : 2
    Persistence : Superblock is persistent

    Update Time : Fri Aug 2 17:41:57 2024
    State : clean, resyncing
    Active Devices : 2
    Working Devices : 2
    Failed Devices : 0
    Spare Devices : 0

    Consistency Policy : resync

    Resync Status : 33% complete

    Name : centos7.linux.fun:1 (local to host centos7.linux.fun)
    UUID : f024b6e0:d2a5793c:f8bdebc6:6bcc7027
    Events : 5

    Number Major Minor RaidDevice State
    0 8 16 0 active sync /dev/sdb
    1 8 32 1 active sync /dev/sdc

    需要关注的属性:

    • Raid Level : raid1
    • State : clean, resyncing,正在同步。
    • Consistency Policy : resync
    • Resync Status : 33% complete,同步进度。
    • 设备清单

    [chenxin@cserver~ 20:38:54]$ lsblk /dev/md1
    NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
    md1 9:1 0 20G 0 raid1

    [chenxin@cserver~ 20:39:09]$ lsblk /dev/sdb /dev/sdc
    NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
    sdb 8:16 0 20G 0 disk
    └─md1 9:1 0 20G 0 raid1
    sdc 8:32 0 20G 0 disk
    └─md1 9:1 0 20G 0 raid1

    2.5.2.3 格式化和挂载

    等待同步完成:直到同步进度达到100%,然后进行格式化和挂载。

    [chenxin@cserver~ 20:39:20]$ mkfs.xfs /dev/md1
    [chenxin@cserver~ 20:39:36]$ mkdir /raid/raid1
    [chenxin@cserver~ 20:39:45]$ mount /dev/md1 /raid/raid1
    [chenxin@cserver~ 20:40:03]$ df -h /raid/raid1
    Filesystem Size Used Avail Use% Mounted on
    /dev/md1 20G 175M 20G 1% /raid/raid1

    # 创建数据
    [chenxin@cserver~ 20:40:13]$ cp /etc/ho* /raid/raid1
    [chenxin@cserver~ 20:40:22]$ ls /raid/raid1/
    host.conf hostname hosts

    2.5.2.4 增加热备盘

    [chenxin@cserver~ 20:40:33]$ mdadm –add /dev/md1 /dev/sdd
    mdadm: added /dev/sdd

    [chenxin@cserver~ 20:40:48]$ mdadm –detail /dev/md1 |tail -5
    Number Major Minor RaidDevice State
    0 8 16 0 active sync /dev/sdb
    1 8 32 1 active sync /dev/sdc

    2 8 48 – spare /dev/sdd
    # /dev/sdd的状态为spare(备用)

    2.5.2.5 模拟故障

    # 强制成员盘故障
    [chenxin@cserver~ 20:41:06]$ mdadm –fail /dev/md1 /dev/sdc
    mdadm: set /dev/sdc faulty in /dev/md1

    # 查看成员状态
    [chenxin@cserver~ 20:41:15]$ mdadm –detail /dev/md1 |tail -5
    Number Major Minor RaidDevice State
    0 8 16 0 active sync /dev/sdb
    2 8 48 1 spare rebuilding /dev/sdd

    1 8 32 – faulty /dev/sdc
    # /dev/sdd立刻顶替故障磁盘,并进行同步

    # 数据依然正常访问
    [chenxin@cserver~ 20:41:29]$ ls /raid/raid1/
    host.conf hostname hosts
    [chenxin@cserver~ 20:41:38]$ cat /raid/raid1/hostname
    centos7.linux.fun

    2.5.2.6 删除故障磁盘

    [chenxin@cserver~ 20:41:52]$ mdadm –remove /dev/md1 /dev/sdc
    mdadm: hot removed /dev/sdc from /dev/md1

    [chenxin@cserver~ 20:42:08]$ mdadm –detail /dev/md1 |tail -5
    Events : 43

    Number Major Minor RaidDevice State
    0 8 16 0 active sync /dev/sdb
    2 8 48 1 active sync /dev/sdd

    2.5.2.7 删除 RAID

    # 卸载
    [chenxin@cserver~ 20:42:20]$ umount /dev/md1

    # stop RAID 阵列,将删除阵列
    [chenxin@cserver~ 20:42:32]$ mdadm –stop /dev/md1
    mdadm: stopped /dev/md1

    # 清除原先设备上的 md superblock
    [chenxin@cserver~ 20:42:50]$ mdadm –zero-superblock /dev/sd{b..d}

    2.5.2.8 补充说明

    RAID1的设计初衷是数据冗余和可靠性,‌而不是为了增加存储容量。‌因此,‌即使添加了新的硬盘并进行了扩容操作,‌由于RAID1的工作方式,‌其总容量是不会增加的。

    2.5.3 管理 RAID5
    2.5.3.1 创建 RAID

    # 创建一个包含4个块设备的raid5设备/dev/md5
    [chenxin@cserver~ 20:43:08]$ mdadm –create /dev/md5 –level 5 –raid-devices 4 /dev/sd{b..e}
    mdadm: Defaulting to version 1.2 metadata
    mdadm: array /dev/md5 started.

    2.5.3.2 查看 RAID

    [chenxin@cserver~ 20:43:24]$ mdadm –detail /dev/md5
    /dev/md5:
    Version : 1.2
    Creation Time : Sat Aug 3 17:16:10 2024
    Raid Level : raid5
    Array Size : 62862336 (59.95 GiB 64.37 GB)
    Used Dev Size : 20954112 (19.98 GiB 21.46 GB)
    Raid Devices : 4
    Total Devices : 4
    Persistence : Superblock is persistent

    Update Time : Sat Aug 3 17:16:24 2024
    State : clean, degraded, recovering
    Active Devices : 3
    Working Devices : 4
    Failed Devices : 0
    Spare Devices : 1

    Layout : left-symmetric
    Chunk Size : 512K

    Consistency Policy : resync

    Rebuild Status : 17% complete

    Name : centos7.linux.fun:5 (local to host centos7.linux.fun)
    UUID : b1661262:66062c09:76e887ce:fecb1340
    Events : 3

    Number Major Minor RaidDevice State
    0 8 16 0 active sync /dev/sdb
    1 8 32 1 active sync /dev/sdc
    2 8 48 2 active sync /dev/sdd
    4 8 64 3 spare rebuilding /dev/sde

    需要关注的属性:

    • Raid Level : raid5
    • State : clean, resyncing,正在同步。
    • Consistency Policy : resync
    • Resync Status : 17% complete,同步进度。
    • 设备清单

    [chenxin@cserver~ 20:43:38]$ lsblk /dev/md5
    NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
    md5 9:5 0 60G 0 raid5 /raid/raid5

    [chenxin@cserver~ 20:43:55]$ lsblk /dev/sd{b..e}
    NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
    sdb 8:16 0 20G 0 disk
    └─md5 9:5 0 60G 0 raid5
    sdc 8:32 0 20G 0 disk
    └─md5 9:5 0 60G 0 raid5
    sdd 8:48 0 20G 0 disk
    └─md5 9:5 0 60G 0 raid5
    sde 8:64 0 20G 0 disk
    └─md5 9:5 0 60G 0 raid5

    2.5.3.3 格式化和挂载

    注意:格式化前,等待 raid 构建完成。

    [chenxin@cserver~ 20:44:07]$ mkfs.xfs /dev/md5
    [chenxin@cserver~ 20:44:22]$ mkdir /raid/raid5
    [chenxin@cserver~ 20:44:35]$ mount /dev/md5 /raid/raid5
    [chenxin@cserver~ 20:44:51]$ df -h /raid/raid5/
    Filesystem Size Used Avail Use% Mounted on
    /dev/md5 60G 461M 60G 1% /raid/raid5

    # 创建数据
    [chenxin@cserver~ 20:45:09]$ cp /etc/ho* /raid/raid5
    [chenxin@cserver~ 20:45:23]$ ls /raid/raid5/
    host.conf hostname hosts

    2.5.3.4 增加热备盘

    # RAID5 阵列增加一个块热备盘
    [chenxin@cserver~ 20:45:37]$ mdadm –add /dev/md5 /dev/sdf
    mdadm: added /dev/sdf

    [chenxin@cserver~ 20:45:53]$ mdadm –detail /dev/md5 |tail -7
    Number Major Minor RaidDevice State
    0 8 16 0 active sync /dev/sdb
    1 8 32 1 active sync /dev/sdc
    2 8 48 2 active sync /dev/sdd
    4 8 64 3 active sync /dev/sde

    5 8 80 – spare /dev/sdf

    2.5.3.5 模拟故障

    # 模拟磁盘故障,手动标记/dev/sdb为fail
    [chenxin@cserver~ 20:46:04]$ mdadm –fail /dev/md5 /dev/sdb
    mdadm: set /dev/sdb faulty in /dev/md5

    # 查看成员状态
    [chenxin@cserver~ 20:46:15]$ mdadm –detail /dev/md5 |tail -7
    Number Major Minor RaidDevice State
    5 8 80 0 spare rebuilding /dev/sdf
    1 8 32 1 active sync /dev/sdc
    2 8 48 2 active sync /dev/sdd
    4 8 64 3 active sync /dev/sde

    0 8 16 – faulty /dev/sdb
    # /dev/sdf立刻顶替故障磁盘,并进行同步

    # 数据依然正常访问
    [chenxin@cserver~ 20:46:31]$ ls /raid/raid5/
    host.conf hostname hosts
    [chenxin@cserver~ 20:46:40]$ cat /raid/raid5/hostname
    centos7.linux.fun

    # 监控磁盘的读写情况
    # 重点关注 %util(磁盘繁忙程度) rd_sec/s(每秒读扇区数量) wr_sec/s(每秒写扇区数量)
    [chenxin@cserver~ 20:46:54]$ sar -dp 1
    15时09分04秒 DEV tps rd_sec/s wr_sec/s avgrq-sz avgqu-sz await svctm %util
    15时09分05秒 sda 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
    15时09分05秒 sdb 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
    15时09分05秒 sr0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
    15时09分05秒 sdf 1718.18 0.00 439854.55 256.00 0.33 0.19 0.19 32.73
    15时09分05秒 sde 1718.18 439854.55 0.00 256.00 0.38 0.22 0.22 38.18
    15时09分05秒 sdd 1718.18 439854.55 0.00 256.00 0.62 0.36 0.36 61.82
    15时09分05秒 sdg 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
    15时09分05秒 sdc 1718.18 439854.55 0.00 256.00 0.51 0.30 0.30 50.91
    15时09分05秒 centos-root 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
    15时09分05秒 centos-swap 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
    15时09分05秒 centos-home 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
    15时09分05秒 md5 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00

    平均时间: DEV tps rd_sec/s wr_sec/s avgrq-sz avgqu-sz await svctm %util
    平均时间: sda 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
    平均时间: sdb 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
    平均时间: sr0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
    平均时间: sdf 1561.70 0.00 398832.24 255.38 0.24 0.15 0.15 23.96
    平均时间: sde 1561.70 398790.18 0.16 255.36 0.28 0.18 0.18 27.64
    平均时间: sdd 1561.70 398790.18 0.16 255.36 0.41 0.26 0.26 41.05
    平均时间: sdg 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
    平均时间: sdc 1561.70 398790.18 0.16 255.36 0.33 0.21 0.21 32.82
    平均时间: centos-root 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
    平均时间: centos-swap 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
    平均时间: centos-home 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
    平均时间: md5 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00

    2.5.3.6 删除故障磁盘

    [chenxin@cserver~ 20:47:08]$ mdadm –remove /dev/md5 /dev/sdb
    mdadm: hot removed /dev/sdb from /dev/md5

    [chenxin@cserver~ 20:47:20]$ mdadm –detail /dev/md5 |tail -5
    Number Major Minor RaidDevice State
    5 8 80 0 active sync /dev/sdf
    1 8 32 1 active sync /dev/sdc
    2 8 48 2 active sync /dev/sdd
    4 8 64 3 active sync /dev/sde

    2.5.3.7 扩容 RAID

    对于raid5,只能扩容,不能减容。

    注意:阵列只有在正常状态下,才能扩容,降级及重构时不允许扩容。

    [chenxin@cserver~ 20:47:30]$ mdadm –add /dev/md5 /dev/sdb /dev/sdg
    mdadm: added /dev/sdb
    mdadm: added /dev/sdg

    [chenxin@cserver~ 20:47:46]$ mdadm –detail /dev/md5 |tail -8
    Number Major Minor RaidDevice State
    5 8 80 0 active sync /dev/sdf
    1 8 32 1 active sync /dev/sdc
    2 8 48 2 active sync /dev/sdd
    4 8 64 3 active sync /dev/sde

    6 8 16 – spare /dev/sdb
    7 8 96 – spare /dev/sdg

    # 设置成员数量为5,-G是grow(扩展)
    [chenxin@cserver~ 20:48:00]$ mdadm –grow /dev/md5 –raid-devices 5

    # 等待重组完成
    [chenxin@cserver~ 20:48:08]$ mdadm –detail /dev/md5
    ......
    Reshape Status : 16% complete
    Delta Devices : 1, (4>5)

    Name : centos7.linux.fun:5 (local to host centos7.linux.fun)
    UUID : 1d4e3f21:1178a4fd:db214497:593e3353
    Events : 79

    Number Major Minor RaidDevice State
    5 8 80 0 active sync /dev/sdf
    1 8 32 1 active sync /dev/sdc
    2 8 48 2 active sync /dev/sdd
    4 8 64 3 active sync /dev/sde
    7 8 96 4 active sync /dev/sdg

    6 8 16 – spare /dev/sdb

    # 确认 raid 容量:增加了20G
    [chenxin@cserver~ 20:48:26]$ lsblk /dev/md5
    NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
    md5 9:5 0 80G 0 raid5 /raid/raid5

    # 扩展文件系统
    [chenxin@cserver~ 20:48:41]$ xfs_growfs /raid/raid5
    [chenxin@cserver~ 20:48:57]$ df -h /raid/raid5/
    Filesystem Size Used Avail Use% Mounted on
    /dev/md5 80G 604M 80G 1% /raid/raid5

    2.5.3.8 删除 RAID

    # 卸载
    [chenxin@cserver~ 20:49:08]$ umount /dev/md5

    # stop RAID 阵列,将删除阵列
    [chenxin@cserver~ 20:49:19]$ mdadm –stop /dev/md5
    mdadm: stopped /dev/md5

    # 清除原先设备上的 md superblock
    [chenxin@cserver~ 20:49:28]$ mdadm –zero-superblock /dev/sd{b..g}

    2.5.3.9 重构 RAID

    # stop之后、清除superblock之前,如果想构建原先的md5设备,使用以下命令,数据不会丢失
    [chenxin@cserver~ 20:49:46]$ mdadm –assemble /dev/md5 /dev/sd{b..g}

    3. Linux 逻辑卷管理

    3.1 环境准备

    虚拟机添加3块20G硬盘,sdb sdc sdd

    [chenxin@cserver~ 20:50:02]$ lsblk /dev/sd{b..d}
    NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
    sdb 8:16 0 20G 0 disk
    sdc 8:32 0 20G 0 disk
    sdd 8:48 0 20G 0 disk

    3.2 逻辑卷基本管理

    3.2.1 硬盘分区管理方案缺点
    • 硬盘分区空间必须使用连续空间,所以硬盘分区扩容很麻烦,特别是要扩容的分区后没有空间。
    • 使用硬盘分区创建的文件系统不同跨多个硬盘,受单个硬盘容量限制。
    • 如果硬盘损坏,则文件系统中数据也会丢失。
    3.2.2 逻辑卷管理硬盘方案优点
    • 使用逻辑卷可以更加灵活地扩展和缩减文件系统空间。
    • 使用逻辑卷创建的文件系统可以跨多个硬盘,适合创建超大容量文件系统。
    • 使用逻辑卷可以创建镜像卷,保证数据的冗余性,即使单个硬盘损坏,文件系统中的数据也不会丢失。
    • 使用逻辑卷还可以创建快照,保留某一时刻数据,就像虚拟机快照一样。
    3.2.3 逻辑卷基本概念

    LVM是 Logical Volume Manager(逻辑卷管理)的简写,LVM将若干个磁盘或者磁盘分区连接为一个整块的卷组,形成一个存储池。管理员可以在卷组上任意创建逻辑卷,并进一步在逻辑卷上创建文件系统。

    image-20221226104759787

    • 物理卷(PV, Physical Volume),是LVM的基本存储逻辑块,通过块设备创建。块设备是指磁盘、磁盘分区或从逻辑上和磁盘分区具有同样功能的设备(如RAID)。与基本的物理存储介质(如分区、磁盘等)相比,物理卷包含有和LVM相关的管理参数。
    • 卷组(VG, Volume Group),是一个逻辑的存储空间,由一个或多个物理卷组成。
    • 逻辑卷(LV, Logical Volume),是从卷组中创建出来的一个逻辑空间。如果把一个卷组类比为一个逻辑硬盘,那么逻辑卷则可以类比为一个硬盘分区,所以也可以在逻辑卷之上创建文件系统。

    简言之,多个物理卷组成卷组,卷组中可创建多个逻辑卷,再在逻辑卷上创建文件系统供用户使用。

    LVM管理基本流程:

  • 创建物理卷
  • 创建卷组
  • 创建逻辑卷
  • image-20221226141838663

    3.2.4 创建物理卷

    # 创建单个PV
    [chenxin@cserver~ 20:50:14]$ pvcreate /dev/sdb
    Physical volume "/dev/sdb" successfully created.

    # 此次创建多个PV
    [chenxin@cserver~ 20:50:31]$ pvcreate /dev/sd{c,d}
    Physical volume "/dev/sdc" successfully created.
    Physical volume "/dev/sdd" successfully created.

    # 查看PV列表
    [chenxin@cserver~ 20:50:47]$ pvs
    PV VG Fmt Attr PSize PFree
    /dev/sdb lvm2 — 20.00g 20.00g
    /dev/sdc lvm2 — 20.00g 20.00g
    /dev/sdd lvm2 — 20.00g 20.00g

    # 查看单个PV详细信息
    [chenxin@cserver~ 20:51:01]$ pvdisplay /dev/sdb
    "/dev/sdb" is a new physical volume of "20.00 GiB"
    — NEW Physical volume —
    PV Name /dev/sdb
    VG Name
    PV Size 20.00 GiB
    Allocatable NO
    PE Size 0
    Total PE 0
    Free PE 0
    Allocated PE 0
    PV UUID MBHW0w-OkJL-CasO-PYRR-sduS-U2m5-zdkQ5d

    3.2.5 创建卷组

    # 创建包涵单个PV的VG
    [chenxin@cserver~ 20:51:11]$ vgcreate webapp /dev/sdb
    Volume group "webapp" successfully created

    # 创建包涵多个PV的VG
    [chenxin@cserver~ 20:51:21]$ vgcreate dbapp /dev/sd{c,d}
    Volume group "dbapp" successfully created

    [chenxin@cserver~ 20:51:37]$ pvs
    PV VG Fmt Attr PSize PFree
    /dev/sdb webapp lvm2 a– <20.00g <20.00g
    /dev/sdc dbapp lvm2 a– <20.00g <20.00g
    /dev/sdd dbapp lvm2 a– <20.00g <20.00g

    # 查看VG列表
    [chenxin@cserver~ 20:51:54]$ vgs
    VG #PV #LV #SN Attr VSize VFree
    dbapp 2 0 0 wz–n- 39.99g 39.99g
    webapp 1 0 0 wz–n- <20.00g <20.00g

    # 查看单个VG详细信息
    [chenxin@cserver~ 20:52:03]$ vgdisplay dbapp
    — Volume group —
    VG Name dbapp
    System ID
    Format lvm2
    Metadata Areas 2
    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 2
    Act PV 2
    VG Size 39.99 GiB
    # PE,Physical Extend,是PV的最小存储单元。
    # 在创建VG时,可通过-s选项设置。
    PE Size 4.00 MiB
    Total PE 10238
    Alloc PE / Size 0 / 0
    Free PE / Size 10238 / 39.99 GiB
    VG UUID kjY26m-D0ax-WmMq-fMJv-1Tnb-imqh-GOAWkG

    3.2.6 创建逻辑卷

    # 在卷组webapp中创建一个逻辑卷:名称为webapp01,大小为5G。
    [chenxin@cserver~ 20:52:18]$ lvcreate -n webapp01 -L 5G webapp
    Logical volume "webapp01" created.

    # 在卷组dbapp中创建一个跨硬盘逻辑卷:名称为data01,大小为25G。
    [chenxin@cserver~ 20:52:28]$ lvcreate -n data01 -L 25G dbapp
    Logical volume "data01" created.

    # 查看LV列表
    [chenxin@cserver~ 20:52:38]$ lvs
    LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
    data01 dbapp -wi-a—– 25.00g
    webapp01 webapp -wi-a—– 5.00g

    逻辑卷设备名支持以下三种格式:

    • /dev/vname/lvame
    • /dev/mapper/vname-lvame
    • /dev/dm-N

    [chenxin@cserver~ 20:52:51]$ ls -l /dev/dbapp/data01 /dev/mapper/dbapp-data01
    lrwxrwxrwx 1 root root 7 617 18:26 /dev/dbapp/data01 –> ../dm-4
    lrwxrwxrwx 1 root root 7 617 18:26 /dev/mapper/dbapp-data01 –> ../dm-4

    # 查看单个LV详细信息
    [chenxin@cserver~ 20:53:07]$ lvdisplay /dev/dbapp/data01
    — Logical volume —
    LV Path /dev/dbapp/data01
    LV Name data02
    VG Name dbapp
    LV UUID W8UafB-A7RJ-dBjM-bsA2-8Obc-p8gE-bMZgmm
    LV Write Access read/write
    LV Creation host, time centos7.linux.com, 2022-12-26 11:42:40 +0800
    LV Status available
    # open 0
    LV Size 25.00 GiB
    # LE,Logical Extend,是LV的最小存储单元。
    # 通常一个LE对应于一个PE。
    Current LE 6400
    Segments 2
    Allocation inherit
    Read ahead sectors auto
    – currently set to 8192
    Block device 253:4

    # 可以看到:物理卷/dev/sdc空间已使用完,物理卷/dev/sdd空间已使用5G
    [chenxin@cserver~ 20:53:25]$ pvs
    PV VG Fmt Attr PSize PFree
    /dev/sdb webapp lvm2 a– <20.00g <15.00g
    /dev/sdc dbapp lvm2 a– <20.00g 0
    /dev/sdd dbapp lvm2 a– <20.00g 14.99g

    # 可以看到逻辑卷/dev/dbapp/data01空间横跨2个硬盘
    [chenxin@cserver~ 20:53:39]$ lsblk /dev/sd{b..d}
    NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
    sdb 8:16 0 20G 0 disk
    └─webapp-webapp01 253:3 0 5G 0 lvm
    sdc 8:32 0 20G 0 disk
    └─dbapp-data01 253:4 0 25G 0 lvm
    sdd 8:48 0 20G 0 disk
    └─dbapp-data01 253:4 0 25G 0 lvm

    3.2.7 创建文件系统

    [chenxin@cserver~ 20:53:56]$ mkfs.xfs /dev/webapp/webapp01
    [chenxin@cserver~ 20:54:13]$ mount /dev/webapp/webapp01 /usr/share/nginx/html
    # 基于lvm的文件系统持久化挂载,需要修改/etc/fstab文件。

    3.2.8 清理

    # 卸载文件系统
    [chenxin@cserver~ 20:54:21]$ umount /dev/webapp/webapp01

    # 删除LV
    [chenxin@cserver~ 20:54:39]$ lvremove /dev/webapp/webapp01 /dev/dbapp/data01
    Do you really want to remove active logical volume webapp/webapp01? [y/n]: y
    Logical volume "webapp01" successfully removed
    Do you really want to remove active logical volume dbapp/data01? [y/n]: y
    Logical volume "data01" successfully removed

    # 删除VG
    [chenxin@cserver~ 20:54:48]$ vgremove webapp dbapp
    Volume group "webapp" successfully removed
    Volume group "dbapp" successfully removed

    # 删除PV
    [chenxin@cserver~ 20:55:06]$ pvremove /dev/sd{b..d}
    Labels on physical volume "/dev/sdb" successfully wiped.
    Labels on physical volume "/dev/sdc" successfully wiped.
    Labels on physical volume "/dev/sdd" successfully wiped.

    3.3 扩展和缩减卷组

    3.3.1 环境准备

    # 创建卷组
    [chenxin@cserver~ 20:55:15]$ vgcreate webapp /dev/sdb
    Physical volume "/dev/sdb" successfully created.
    Volume group "webapp" successfully created
    # 创建卷组的时候,如果指定的块设备不是物理卷,则会先将块设备创建为物理卷。

    # 创建逻辑卷
    [chenxin@cserver~ 20:55:24]$ lvcreate -n webapp01 -L 10G webapp
    Logical volume "webapp01" created.

    3.3.2 扩展卷组

    如果卷组空间不足,可以通过添加新的物理卷到卷组中,以扩展卷组容量。

    [chenxin@cserver~ 20:55:35]$ vgextend webapp /dev/sd{c,d}
    Physical volume "/dev/sdc" successfully created.
    Physical volume "/dev/sdd" successfully created.
    Volume group "webapp" successfully extended

    3.3.3 缩减卷组

    如果卷组空间中有多余的物理卷或者物理卷容量小需要更换,则可以将物理卷从卷组中剔除。

    删除物理卷前确保物理卷不在使用中。

    # 查看物理卷使用状态
    [chenxin@cserver~ 20:55:51]$ pvs
    PV VG Fmt Attr PSize PFree
    /dev/sdb webapp lvm2 a– <20.00g <10.00g # /dev/sdb在使用
    /dev/sdc webapp lvm2 a– <20.00g <20.00g
    /dev/sdd webapp lvm2 a– <20.00g <20.00g

    # 将物理卷/dev/sdb从卷组webapp中剔除,则会报错
    [chenxin@cserver~ 20:56:06]$ vgreduce webapp /dev/sdb
    Physical volume "/dev/sdb" still in use

    # 解决方法:将物理卷/dev/sdb中数据移动到卷组中其他物理卷
    [chenxin@cserver~ 20:56:15]$ pvmove /dev/sdb
    # 或者移动到卷组中特定物理卷
    [chenxin@cserver~ 20:56:29]$ pvmove /dev/sdb /dev/sdd
    /dev/sdb: Moved: 0.51%
    /dev/sdb: Moved: 100.00%

    # 查看物理卷使用状态
    [chenxin@cserver~ 20:56:40]$ pvs
    PV VG Fmt Attr PSize PFree
    /dev/sdb webapp lvm2 a– <20.00g <20.00g # /dev/sdb不在使用
    /dev/sdc webapp lvm2 a– <20.00g <20.00g
    /dev/sdd webapp lvm2 a– <20.00g <10.00g # /dev/sdd在使用

    # 再次剔除,成功
    [chenxin@cserver~ 20:56:50]$ vgreduce webapp /dev/sdb
    Removed "/dev/sdb" from volume group "webapp"
    [chenxin@cserver~ 20:57:04]$ pvs
    PV VG Fmt Attr PSize PFree
    /dev/sdb lvm2 — 20.00g 20.00g
    /dev/sdc webapp lvm2 a– <20.00g <20.00g
    /dev/sdd webapp lvm2 a– <20.00g <10.00g

    3.4 扩展和缩减逻辑卷

    3.4.1 扩展逻辑卷

    如果逻辑卷空间不足,在卷组空间充足的情况下,也可以扩展逻辑卷空间。

    # 逻辑卷增加2G空间
    [chenxin@cserver~ 20:57:22]$ lvextend -L +2G /dev/webapp/webapp01
    Size of logical volume webapp/webapp01 changed from 10.00 GiB (2560 extents) to 12.00 GiB (3072 extents).
    Logical volume webapp/webapp01 successfully resized.

    [chenxin@cserver~ 20:57:39]$ lvs /dev/webapp/webapp01
    LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
    webapp01 webapp -wi-a—– 12.00g

    3.4.2 缩减逻辑卷

    如果逻辑卷空间充足,希望将多余的空间缩减出来它用,也可以缩减逻辑卷空间。

    [chenxin@cserver~ 20:57:49]$ lvreduce -L -2G /dev/webapp/webapp01
    WARNING: Reducing active logical volume to 10.00 GiB.
    THIS MAY DESTROY YOUR DATA (filesystem etc.)
    Do you really want to reduce webapp/webapp01? [y/n]: `y`
    Size of logical volume webapp/webapp01 changed from 12.00 GiB (3072 extents) to 10.00 GiB (2560 extents).
    Logical volume webapp/webapp01 successfully resized.

    [chenxin@cserver~ 20:58:07]$ lvs /dev/webapp/webapp01
    LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
    webapp01 webapp -wi-a—– 10.00g

    3.5 扩展和缩减文件系统

    3.5.1 扩展 XFS 文件系统

    XFS文件系统只支持扩展,不支持缩减。

    环境准备:格式化文件系统,并创建数据

    [chenxin@cserver~ 20:58:20]$ mkfs.xfs /dev/webapp/webapp01
    [chenxin@cserver~ 20:58:35]$ mkdir -p /usr/share/nginx/html
    [chenxin@cserver~ 20:58:48]$ mount /dev/webapp/webapp01 /usr/share/nginx/html
    [chenxin@cserver~ 20:59:06]$ cp /etc/host* /usr/share/nginx/html
    [chenxin@cserver~ 20:59:23]$ ls /usr/share/nginx/html
    host.conf hostname hosts hosts.allow hosts.deny

    # 第一步:扩展逻辑卷
    [chenxin@cserver~ 20:59:38]$ lvextend -L 15G /dev/webapp/webapp01
    [chenxin@cserver~ 20:59:49]$ lvs /dev/webapp/webapp01
    LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
    webapp01 webapp -wi-ao—- 15.00g

    # 第二步:扩展文件系统
    [chenxin@cserver~ 21:00:03]$ xfs_growfs /usr/share/nginx/html

    [chenxin@cserver~ 21:00:17]$ df -h /usr/share/nginx/html
    文件系统 容量 已用 可用 已用% 挂载点
    /dev/mapper/webapp-webapp01 15G 140M 15G 1% /usr/share/nginx/html
    [chenxin@cserver~ 21:00:28]$ ls /usr/share/nginx/html
    host.conf hostname hosts hosts.allow hosts.deny

    # 块设备和文件系统一并扩展
    [chenxin@cserver~ 21:00:41]$ lvextend -rL 20G /dev/webapp/webapp01

    [chenxin@cserver~ 21:00:50]$ lvs /dev/webapp/webapp01
    LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
    webapp01 webapp -wi-ao—- 20.00g
    [chenxin@cserver~ 21:01:05]$ df -h /usr/share/nginx/html
    文件系统 容量 已用 可用 已用% 挂载点
    /dev/mapper/webapp-webapp01 20G 176M 20G 1% /usr/share/nginx/html

    3.5.2 扩展 EXT4 文件系统

    EXT4文件系统即支持扩展,也支持缩减。

    环境准备:格式化文件系统,并创建数据

    [chenxin@cserver~ 21:01:14]$ umount /usr/share/nginx/html
    [chenxin@cserver~ 21:01:26]$ mkfs.ext4 /dev/webapp/webapp01

    [chenxin@cserver~ 21:01:38]$ mount /dev/webapp/webapp01 /usr/share/nginx/html
    [chenxin@cserver~ 21:01:47]$ df -h /usr/share/nginx/html
    文件系统 容量 已用 可用 已用% 挂载点
    /dev/mapper/webapp-webapp01 20G 24K 19G 1% /usr/share/nginx/html

    [chenxin@cserver~ 21:01:55]$ cp /etc/host* /usr/share/nginx/html
    [chenxin@cserver~ 21:02:09]$ ls /usr/share/nginx/html
    host.conf hostname hosts hosts.allow hosts.deny

    # 第一步:扩展逻辑卷
    [chenxin@cserver~ 21:02:21]$ lvextend -L 25G /dev/webapp/webapp01
    [chenxin@cserver~ 21:02:36]$ lvs /dev/webapp/webapp01
    LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
    webapp01 webapp -wi-ao—- 25.00g

    # 第二步:扩展文件系统
    [chenxin@cserver~ 21:02:50]$ resize2fs /dev/webapp/webapp01
    [chenxin@cserver~ 21:03:00]$ df -h /usr/share/nginx/html
    文件系统 容量 已用 可用 已用% 挂载点
    /dev/mapper/webapp-webapp01 25G 36K 24G 1% /usr/share/nginx/html
    [chenxin@cserver~ 21:03:13]$ ls /usr/share/nginx/html
    host.conf hostname hosts hosts.allow hosts.deny

    # 块设备和文件系统一并扩展
    [chenxin@cserver~ 21:03:26]$ lvextend -rL 30G /dev/webapp/webapp01

    [chenxin@cserver~ 21:03:40]$ lvs /dev/webapp/webapp01
    LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
    webapp01 webapp -wi-ao—- 30.00g
    [chenxin@cserver~ 21:03:50]$ df -h /usr/share/nginx/html
    文件系统 容量 已用 可用 已用% 挂载点
    /dev/mapper/webapp-webapp01 30G 36K 28G 1% /usr/share/nginx/html

    3.5.3 缩减 EXT4 文件系统

    缩减EXT4文件系统注意事项:

    • 不支持在线缩减,必须卸载后缩减。
    • 缩减后的容量,不能小于当前使用的容量。

    ext4 减容流程:

  • 卸载文件系统
  • e2fsck 文件系统
  • 缩减文件系统
  • 缩减 lv
  • 挂载测试
  • 具体操作如下:

    # 第一步:卸载文件系统
    [chenxin@cserver~ 21:04:06]$ umount /usr/share/nginx/html

    # 第二步:检测文件系统
    [chenxin@cserver~ 21:04:19]$ e2fsck -f /dev/webapp/webapp01
    e2fsck 1.42.9 (28-Dec-2013)
    第一步: 检查inode,块,和大小
    第二步: 检查目录结构
    第3步: 检查目录连接性
    Pass 4: Checking reference counts
    第5步: 检查簇概要信息
    /dev/webapp/webapp01:14/1966080 文件(0.0% 为非连续的), 167445/7864320 块

    # 第三步:缩减文件系统
    [chenxin@cserver~ 21:04:33]$ resize2fs /dev/webapp/webapp01 10G
    resize2fs 1.45.6 (20-Mar-2020)
    将 /dev/webapp/webapp01 上的文件系统调整为 2621440 个块(每块 4k)。
    /dev/webapp/webapp01 上的文件系统现在为 2621440 个块(每块 4k)。

    # 第四步:缩减逻辑卷
    [chenxin@cserver~ 21:04:44]$ lvreduce -L 10G /dev/webapp/webapp01
    WARNING: Reducing active logical volume to 10.00 GiB.
    THIS MAY DESTROY YOUR DATA (filesystem etc.)
    Do you really want to reduce webapp/webapp01? [y/n]: `y`
    Size of logical volume webapp/webapp01 changed from 30.00 GiB (7680 extents) to 10.00 GiB (2560 extents).
    Logical volume webapp/webapp01 successfully resized.

    # 第五步:挂载文件系统验证
    [chenxin@cserver~ 21:04:58]$ mount /dev/webapp/webapp01 /usr/share/nginx/html

    [chenxin@cserver~ 21:05:12]$ df -h /usr/share/nginx/html
    文件系统 容量 已用 可用 已用% 挂载点
    /dev/mapper/webapp-webapp01 9.7G 36K 9.3G 1% /usr/share/nginx/html

    [chenxin@cserver~ 21:05:20]$ ls /usr/share/nginx/html
    host.conf hostname hosts lost+found

    补充说明: 如果文件系统是构建在磁盘分区上,扩展文件系统容量和缩减文件系统容量操作相同。

    3.6 逻辑卷快照

    我们可以使用快照记录了lv中数据,后续可以用来恢复数据。

    # 创建快照:快照的容量不能小于lv容量
    [chenxin@cserver~ 21:05:28]$ lvcreate -s -n webapp01-snap1 -L 10G /dev/webapp/webapp01
    Logical volume "webapp01-snap1" created.

    # 挂载快照
    [chenxin@cserver~ 21:05:37]$ mount /dev/webapp/webapp01-snap1 /webapp/webapp01/

    # 查看数据
    [chenxin@cserver~ 21:05:48]$ ls /webapp/webapp01/
    host.conf hostname hosts hosts.allow hosts.deny

    # 创建新数据
    [chenxin@cserver~ 21:05:57]$ echo hello world > /webapp/webapp01/hello.txt
    [chenxin@cserver~ 21:06:06]$ cat /webapp/webapp01/hello.txt
    hello world

    3.7 raid 逻辑卷

    准备环境

    [chenxin@cserver~ 21:06:18]$ umount /webapp/webapp0
    [chenxin@cserver~ 21:06:33]$ umount /usr/share/nginx/html
    [chenxin@cserver~ 21:06:47]$ lvremove /dev/webapp/webapp01*

    创建 raid1 逻辑卷

    [chenxin@cserver~ 21:07:01]$ lvcreate –type raid1 -n webapp01 -L 15G webapp
    [chenxin@cserver~ 21:07:09]$ mkfs.xfs /dev/webapp/webapp01
    [chenxin@cserver~ 21:07:19]$ mount /dev/webapp/webapp01 /usr/share/nginx/html/
    [chenxin@cserver~ 21:07:32]$ cp /etc/ho* /usr/share/nginx/html/

    模拟故障

    [chenxin@cserver~ 21:07:45]$ dd if=/dev/zero of=/dev/sdd bs=1M count=256

    此时文件系统仍可以正方访问

    [chenxin@cserver~ 21:07:57]$ ls /usr/share/nginx/html/
    host.conf hostname hosts hosts.allow hosts.deny
    [chenxin@cserver~ 21:08:15]$ umount /usr/share/nginx/html
    [chenxin@cserver~ 21:08:31]$ mount /dev/webapp/webapp01 /usr/share/nginx/html/

    修复raid

    # 删除丢失的物理卷
    [chenxin@cserver~ 21:08:44]$ vgreduce –removemissing webapp –force

    # 扩容回来
    [chenxin@cserver~ 21:09:02]$ vgextend webapp /dev/sdd

    # 修复raid1 逻辑卷
    [chenxin@cserver~ 21:09:13]$ lvconvert –repair /dev/webapp/webapp01

    # 扫描物理卷变化
    [chenxin@cserver~ 21:09:29]$ pvscan

    # 验证结果
    [chenxin@cserver~ 21:09:39]$ pvs|grep webap
    /dev/sdb webapp lvm2 a– <20.00g 4.99g
    /dev/sdc webapp lvm2 a– <20.00g 4.99g
    /dev/sdd webapp lvm2 a– <20.00g <20.00g

    其他示例:创建 raid5 逻辑卷

    # Create a RAID5 LV with a usable size of 5GiB, 3 stripes, a stripe size of 64KiB, using a total of 4 devices (including one for parity).
    lvcreate –type raid5 -L 5G -i 3 -I 64 -n mylv vg00

    henxin@cserver~ 21:04:58]$ mount /dev/webapp/webapp01 /usr/share/nginx/html

    [chenxin@cserver~ 21:05:12]$ df -h /usr/share/nginx/html
    文件系统 容量 已用 可用 已用% 挂载点
    /dev/mapper/webapp-webapp01 9.7G 36K 9.3G 1% /usr/share/nginx/html

    [chenxin@cserver~ 21:05:20]$ ls /usr/share/nginx/html
    host.conf hostname hosts lost+found

    **==补充说明:==** 如果文件系统是构建在磁盘分区上,扩展文件系统容量和缩减文件系统容量操作相同。

    ### 3.6 逻辑卷快照

    我们可以使用快照记录了lv中数据,后续可以用来恢复数据。

    ```bash
    # 创建快照:快照的容量不能小于lv容量
    [chenxin@cserver~ 21:05:28]$ lvcreate -s -n webapp01-snap1 -L 10G /dev/webapp/webapp01
    Logical volume "webapp01-snap1" created.

    # 挂载快照
    [chenxin@cserver~ 21:05:37]$ mount /dev/webapp/webapp01-snap1 /webapp/webapp01/

    # 查看数据
    [chenxin@cserver~ 21:05:48]$ ls /webapp/webapp01/
    host.conf hostname hosts hosts.allow hosts.deny

    # 创建新数据
    [chenxin@cserver~ 21:05:57]$ echo hello world > /webapp/webapp01/hello.txt
    [chenxin@cserver~ 21:06:06]$ cat /webapp/webapp01/hello.txt
    hello world

    3.7 raid 逻辑卷

    准备环境

    [chenxin@cserver~ 21:06:18]$ umount /webapp/webapp0
    [chenxin@cserver~ 21:06:33]$ umount /usr/share/nginx/html
    [chenxin@cserver~ 21:06:47]$ lvremove /dev/webapp/webapp01*

    创建 raid1 逻辑卷

    [chenxin@cserver~ 21:07:01]$ lvcreate –type raid1 -n webapp01 -L 15G webapp
    [chenxin@cserver~ 21:07:09]$ mkfs.xfs /dev/webapp/webapp01
    [chenxin@cserver~ 21:07:19]$ mount /dev/webapp/webapp01 /usr/share/nginx/html/
    [chenxin@cserver~ 21:07:32]$ cp /etc/ho* /usr/share/nginx/html/

    模拟故障

    [chenxin@cserver~ 21:07:45]$ dd if=/dev/zero of=/dev/sdd bs=1M count=256

    此时文件系统仍可以正方访问

    [chenxin@cserver~ 21:07:57]$ ls /usr/share/nginx/html/
    host.conf hostname hosts hosts.allow hosts.deny
    [chenxin@cserver~ 21:08:15]$ umount /usr/share/nginx/html
    [chenxin@cserver~ 21:08:31]$ mount /dev/webapp/webapp01 /usr/share/nginx/html/

    修复raid

    # 删除丢失的物理卷
    [chenxin@cserver~ 21:08:44]$ vgreduce –removemissing webapp –force

    # 扩容回来
    [chenxin@cserver~ 21:09:02]$ vgextend webapp /dev/sdd

    # 修复raid1 逻辑卷
    [chenxin@cserver~ 21:09:13]$ lvconvert –repair /dev/webapp/webapp01

    # 扫描物理卷变化
    [chenxin@cserver~ 21:09:29]$ pvscan

    # 验证结果
    [chenxin@cserver~ 21:09:39]$ pvs|grep webap
    /dev/sdb webapp lvm2 a– <20.00g 4.99g
    /dev/sdc webapp lvm2 a– <20.00g 4.99g
    /dev/sdd webapp lvm2 a– <20.00g <20.00g

    其他示例:创建 raid5 逻辑卷

    # Create a RAID5 LV with a usable size of 5GiB, 3 stripes, a stripe size of 64KiB, using a total of 4 devices (including one for parity).
    lvcreate –type raid5 -L 5G -i 3 -I 64 -n mylv vg00

    赞(0)
    未经允许不得转载:网硕互联帮助中心 » Linux 存储全解:磁盘分区、软 RAID、LVM 逻辑卷从入门到实操
    分享到: 更多 (0)

    评论 抢沙发

    评论前必须登录!