在Linux CentOS 7 创建软RAID 0

logo-linux

在Linux CentOS 7 创建软RAID 0

01 fdisk 命令介绍

命令 参数 描述
fdisk a 切换可引导标志
fdisk b 编辑bsd磁盘标签
fdisk c 切换dos兼容性标志
fdisk d 删除分区
fdisk g 创建一个新的空GPT分区表
fdisk G 创建一个IRIX (SGI)分区表
fdisk l 列出已知的分区类型
fdisk m 打印这个菜单
fdisk n 添加一个新的分区
fdisk o 创建一个新的空DOS分区表
fdisk p 打印分区表
fdisk q 退出而不保存更改
fdisk s 创建一个新的空的Sun磁盘标签
fdisk t 改变分区的系统id
fdisk u 更改显示/输入单元
fdisk v 验证分区表
fdisk w 将表写入磁盘并保存退出
fdisk x 额外功能(仅限专家)
mdadm -C 创建RAID
mdadm -a 后接yes或者no,自动创建设备文件
mdadm -l RAID级别 0:为 raid0 1:为raid1
mdadm -n 干活的磁盘数

02 环境信息

OS: centos 7 2009 x64

VMware Version: VMware Workstation Pro 14

03 开始配置

给虚拟机添加两块硬盘

添加两块硬盘

执行fdisk -l命令查看已挂载的硬盘信息。

1
$ fdisk -l

查看已挂载的磁盘信息

3.1 使用fdisk命令将两块硬盘创建分区,设置分区类型为 fd(Linux raid autodetect)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# 开始创建第一块硬盘分区;/dev/sdb硬盘
$ fdisk /dev/sdb # 对接/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.

Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0x2fcde05c.

Command (m for help): n # 新建一个分区
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p # 设置当前分区为主分区
Partition number (1-4, default 1): # 保持默认;选择第一个分区
First sector (2048-104857599, default 2048): # 保持默认;磁盘空间从2048开始
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-104857599, default 104857599): # 保持默认;磁盘空间至104857599结束
Using default value 104857599
Partition 1 of type Linux and of size 50 GiB is set

Command (m for help): p # 显示/dev/sdb硬盘分区信息

Disk /dev/sdd: 53.7 GB, 53687091200 bytes, 104857600 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: 0x2fcde05c

Device Boot Start End Blocks Id System
/dev/sdd1 2048 104857599 52427776 83 Linux

Command (m for help): t # 更改硬盘分区类型为fd(Linux raid autodetect)
Selected partition 1
Hex code (type L to list all codes): fd # Linux raid autodetect
Changed type of partition 'Linux' to 'Linux raid autodetect'

Command (m for help): p # 显示/dev/sdb硬盘分区信息

Disk /dev/sdd: 53.7 GB, 53687091200 bytes, 104857600 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: 0x2fcde05c

Device Boot Start End Blocks Id System
/dev/sdd1 2048 104857599 52427776 fd Linux raid autodetect

Command (m for help): w # 保存分区信息

# 开始创建第二块硬盘分区;/dev/sdc硬盘
$ fdisk /dev/sdc # 对接/dev/sdc硬盘进行分区操作
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.

Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0x2fcde05c.

Command (m for help): n # 新建一个分区
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p # 设置当前分区为主分区
Partition number (1-4, default 1): # 保持默认;选择第一个分区
First sector (2048-104857599, default 2048): # 保持默认;磁盘空间从2048开始
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-104857599, default 104857599): # 保持默认;磁盘空间至104857599结束
Using default value 104857599
Partition 1 of type Linux and of size 50 GiB is set

Command (m for help): p # 显示/dev/sdc硬盘分区信息

Disk /dev/sdc: 53.7 GB, 53687091200 bytes, 104857600 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: 0x2fcde05c

Device Boot Start End Blocks Id System
/dev/sdd1 2048 104857599 52427776 83 Linux

Command (m for help): t # 更改硬盘分区类型为fd(Linux raid autodetect)
Selected partition 1
Hex code (type L to list all codes): fd # Linux raid autodetect
Changed type of partition 'Linux' to 'Linux raid autodetect'

Command (m for help): p # 显示/dev/sdc硬盘分区信息

Disk /dev/sdc: 53.7 GB, 53687091200 bytes, 104857600 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: 0x2fcde05c

Device Boot Start End Blocks Id System
/dev/sdd1 2048 104857599 52427776 fd Linux raid autodetect

Command (m for help): w # 保存分区信息

3.2 创建raid 0

如果没有mdamd命令使用yum或者apt-get进行安装

1
2
CentOS:yum install mdadm -y
Ubuntu:apt-get install mdadm -y
1
2
3
4
5
6
7
$ mdamd -Ca yes /dev/md0 -l0 -n2 /dev/sd{b,c}1

-C:创建RAID
-a:后接yes或者no,自动创建设备文件
-l:RAID级别
-n:干活的磁盘数

3.3 检查

  • 查看md0详细信息
    1
    $ mdadm -D /dev/md0
    md0-info

04 Linux系统配置

4.1 系统配置

创建好的md0文件信息需要保存到/etc/mdadm.conf配置文件中。后续为了避免系统重启后md0变成md127;

注意:没有创建md0配置文件并且 /etc/fstab 设置自动挂载会导致系统启动失败。
fstab文件写的是md0,重启后会将md0改成md127,导致内核找不到相应的文件 从而导致系统启动失败。

1
$ mdadm -Ds /dev/md0 >> /etc/mdadm.conf

mdadm.conf内容:

1
2
$ cat /etc/mdadm.conf
ARRAY /dev/md0 metadata=1.2 name=localhost.localdomain:0 UuID=bc20b561:a7e376fc:f92cd5c8:85e129b1

4.2 配置挂载

1
2
3
4
5
6
7
8
9
10
11
12
13
$ mkfs.ext4 /dev/md0  # 将/dev/md0设置为mkfs.ext4文件系统
$ mkdir /raid-data # 创建挂载目录
$ mount /dev/md0 /raid-data # 将/dev/md0挂载到/raid-data目录
$ df -h # 查看挂载情况
Filesystem Size Used Avail Use% Mounted on
devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs 1.9G 12M 1.9G 1% /run
tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/mapper/centos-root 77G 56G 22G 73% /
/dev/sda1 1014M 321M 694M 32% /boot
tmpfs 378M 0 378M 0% /run/user/0
/dev/md0 100G 45M 99G 0% /raid-data

编辑/etc/fstab配置文件设置自动挂载

1
2
3
4
5
6
7
8
9
10
11
#
# /etc/fstab
# Created by anaconda on Sun Oct 20 05:25:35 2019
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/centos-root / xfs defaults 0 0
UUID=a95eac58-77cf-4b6f-a7dd-78cef0e915e9 /boot xfs defaults 0 0
/dev/mapper/centos-swap swap swap defaults 0 0
/dev/md0 ext4 /raid-data defaults 0 0

在Linux CentOS 7 创建软RAID 0
https://hesc.info/db62c1bfc21e/
作者
需要哈气的纸飞机
发布于
2020年5月19日
许可协议