Linux · 2015年2月6日 0

centos下创建软raid0。

今天带来的是软raid0的创建
(环境centos7)
1、首先
# fdisk –l查看当前磁盘列表
其中这四块是今天要用来做raid0的
Disk /dev/sdc: 1098.4 GB, 1098437885952 bytes, 2145386496 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: 0xd56f8810

Device Boot Start End Blocks Id System
/dev/sdc1 2048 2145386495 1072692224 83 Linux

Disk /dev/sdd: 1098.4 GB, 1098437885952 bytes, 2145386496 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 /dev/sde: 1098.4 GB, 1098437885952 bytes, 2145386496 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 /dev/sdf: 1098.4 GB, 1098437885952 bytes, 2145386496 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
2、安装mdadm
查看当前系统有没有安装mdadm
# rpm -qa|grep mdadm
如果没有安装
请执行
# yum install mdadm –y
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Resolving Dependencies
–> Running transaction check
—> Package mdadm.x86_64 0:3.2.6-31.el7_0.2 will be installed
–> Processing Dependency: libreport-filesystem for package: mdadm-3.2.6-31.el7_0.2.x86_64
–> Running transaction check
—> Package libreport-filesystem.x86_64 0:2.1.11-10.el7.centos.0.1 will be installed
–> Finished Dependency Resolution

Dependencies Resolved

=============================================================================================================================================================================================
Package Arch Version Repository Size
=============================================================================================================================================================================================
Installing:
mdadm x86_64 3.2.6-31.el7_0.2 updates 358 k
Installing for dependencies:
libreport-filesystem x86_64 2.1.11-10.el7.centos.0.1 updates 32 k

Transaction Summary
=============================================================================================================================================================================================
Install 1 Package (+1 Dependent package)

Total download size: 391 k
Installed size: 870 k
Is this ok [y/d/N]: y
Downloading packages:
(1/2): libreport-filesystem-2.1.11-10.el7.centos.0.1.x86_64.rpm | 32 kB 00:00:00
(2/2): mdadm-3.2.6-31.el7_0.2.x86_64.rpm | 358 kB 00:00:05
———————————————————————————————————————————————————————————————
Total 74 kB/s | 391 kB 00:00:05
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : libreport-filesystem-2.1.11-10.el7.centos.0.1.x86_64 1/2
Installing : mdadm-3.2.6-31.el7_0.2.x86_64 2/2
Verifying : mdadm-3.2.6-31.el7_0.2.x86_64 1/2
Verifying : libreport-filesystem-2.1.11-10.el7.centos.0.1.x86_64 2/2

Installed:
mdadm.x86_64 0:3.2.6-31.el7_0.2

Dependency Installed:
libreport-filesystem.x86_64 0:2.1.11-10.el7.centos.0.1

Complete!
3、创建raid0前提
在此我们创建raid0用的四块盘分别为/dev/sdc、/dev/sdd、/dev/sde、/dev/sdf
执行
# fdisk /dev/sdc
输入m可获取帮助,输入p可查看分区前磁盘状态,不多列举了。
# fdisk /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.

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): 1(第一个主分区)
First sector (2048-2145386495, default 2048): (默认直接回车)
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-2145386495, default 2145386495):(默认直接回车)
Using default value 2145386495
Partition 1 of type Linux and of size 1023 GiB is set

Command (m for help): p(查看当前分区状态,默认新分区的类型是Linux,代号83,需要将其修改为raid 类型。输入t ,然后输入L列出所有的文件格式,这里选择fd Linux raid auto, 输入fd,然后再输入p 查看分区情况,分区格式已经变成了Linux raid autodetect.)

Disk /dev/sdc: 1098.4 GB, 1098437885952 bytes, 2145386496 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: 0xd56f8810

Device Boot Start End Blocks Id System
/dev/sdc1 2048 2145386495 1072692224 83 Linux

Command (m for help): t
Selected partition 1
Hex code (type L to list all codes): L

0 Empty 24 NEC DOS 81 Minix / old Lin bf Solaris
1 FAT12 27 Hidden NTFS Win 82 Linux swap / So c1 DRDOS/sec (FAT-
2 XENIX root 39 Plan 9 83 Linux c4 DRDOS/sec (FAT-
3 XENIX usr 3c PartitionMagic 84 OS/2 hidden C: c6 DRDOS/sec (FAT-
4 FAT16 5 Extended 41 PPC PReP Boot 86 NTFS volume set da Non-FS data
6 FAT16 42 SFS 87 NTFS volume set db CP/M / CTOS / .
7 HPFS/NTFS/exFAT 4d QNX4.x 88 Linux plaintext de Dell Utility
8 AIX 4e QNX4.x 2nd part 8e Linux LVM df BootIt
9 AIX bootable 4f QNX4.x 3rd part 93 Amoeba e1 DOS access
a OS/2 Boot Manag 50 OnTrack DM 94 Amoeba BBT e3 DOS R/O
b W95 FAT32 51 OnTrack DM6 Aux 9f BSD/OS e4 SpeedStor
c W95 FAT32 (LBA) 52 CP/M a0 IBM Thinkpad hi eb BeOS fs
e W95 FAT16 (LBA) 53 OnTrack DM6 Aux a5 FreeBSD ee GPT
f W95 Ext’d (LBA) 54 OnTrackDM6 a6 OpenBSD ef EFI (FAT-12/16/
10 OPUS 55 EZ-Drive a7 NeXTSTEP f0 Linux/PA-RISC b
11 Hidden FAT12 56 Golden Bow a8 Darwin UFS f1 SpeedStor
12 Compaq diagnost 5c Priam Edisk a9 NetBSD f4 SpeedStor
14 Hidden FAT16 16 Hidden FAT16 63 GNU HURD or Sys af HFS / HFS+ fb VMware VMFS
17 Hidden HPFS/NTF 64 Novell Netware b7 BSDI fs fc VMware VMKCORE
18 AST SmartSleep 65 Novell Netware b8 BSDI swap fd Linux raid auto
1b Hidden W95 FAT3 70 DiskSecure Mult bb Boot Wizard hid fe LANstep
1c Hidden W95 FAT3 75 PC/IX be Solaris boot ff BBT
1e Hidden W95 FAT1 80 Old Minix
Hex code (type L to list all codes): fd
Changed type of partition ‘Linux’ to ‘Linux raid autodetect’

Command (m for help): p

Disk /dev/sdc: 1098.4 GB, 1098437885952 bytes, 2145386496 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: 0xd56f8810

Device Boot Start End Blocks Id System
/dev/sdc1 2048 2145386495 1072692224 fd Linux raid autodetect

Command (m for help): w(最后w保存)
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.
使用相同的方法对其他三块盘进行操作。
4、同步一下分区情况
# partx /dev/sdc
NR START END SECTORS SIZE NAME UUID
1 2048 2145386495 2145384448 1023G
# partx /dev/sdd
NR START END SECTORS SIZE NAME UUID
1 2048 2145386495 2145384448 1023G
# partx /dev/sde
NR START END SECTORS SIZE NAME UUID
1 2048 2145386495 2145384448 1023G
# partx /dev/sdf
NR START END SECTORS SIZE NAME UUID
1 2048 2145386495 2145384448 1023G
5、查看现在的状态
# fdisk -l /dev/sdc /dev/sdd /dev/sde /dev/sdf
Disk /dev/sdc: 1098.4 GB, 1098437885952 bytes, 2145386496 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: 0xd56f8810

Device Boot Start End Blocks Id System
/dev/sdc1 2048 2145386495 1072692224 fd Linux raid autodetect

Disk /dev/sdd: 1098.4 GB, 1098437885952 bytes, 2145386496 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: 0xc98cf858

Device Boot Start End Blocks Id System
/dev/sdd1 2048 2145386495 1072692224 fd Linux raid autodetect

Disk /dev/sde: 1098.4 GB, 1098437885952 bytes, 2145386496 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: 0x30484bf1

Device Boot Start End Blocks Id System
/dev/sde1 2048 2145386495 1072692224 fd Linux raid autodetect

Disk /dev/sdf: 1098.4 GB, 1098437885952 bytes, 2145386496 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: 0xb1d05b49

Device Boot Start End Blocks Id System
/dev/sdf1 2048 2145386495 1072692224 fd Linux raid autodetect
6、开始创建raid0
# mdadm -C /dev/md0 -a yes -l0 -n4 /dev/sd[c,d,e,f]1
mdadm: /dev/sdc1 appears to contain an ext2fs file system
size=1072692224K mtime=Fri Feb 6 06:44:10 2015
Continue creating array? yes
mdadm: Defaulting to version 1.2 metadata
mdadm: array /dev/md0 started.
-C  –create  创建阵列
-a  –auto   同意创建设备,如不加此参数时必须先使用mknod 命令来创建一个RAID设备,不过推荐使用-a yes参数一次性创建
-l   –level   阵列模式,支持的阵列模式有 linear, raid0, raid1, raid4, raid5, raid6, raid10, multipath, faulty, container
-n –raid-devices 阵列中活动磁盘的数目,该数目加上备用磁盘的数目应该等于阵列中总的磁盘数目
/dev/md0     阵列的设备名称;
/dev/sd{c,d,e,f}1  参与创建阵列的磁盘名称;
7、查看raid0状态
# cat /proc/mdstat
Personalities : [raid0]
md0 : active raid0 sdf1[3] sde1[2] sdd1[1] sdc1[0]
4290766848 blocks super 1.2 512k chunks

unused devices:
# mdadm -D /dev/md0
/dev/md0:
Version : 1.2
Creation Time : Fri Feb 6 06:46:11 2015
Raid Level : raid0
Array Size : 4290766848 (4091.99 GiB 4393.75 GB)
Raid Devices : 4
Total Devices : 4
Persistence : Superblock is persistent

Update Time : Fri Feb 6 06:46:11 2015
State : clean
Active Devices : 4
Working Devices : 4
Failed Devices : 0
Spare Devices : 0

Chunk Size : 512K

Name : v-as-db001:0 (local to host v-as-db001)
UUID : e330d461:5464a508:560fa5fd:ad8ef634
Events : 0

Number Major Minor RaidDevice State
0 8 33 0 active sync /dev/sdc1
1 8 49 1 active sync /dev/sdd1
2 8 65 2 active sync /dev/sde1
3 8 81 3 active sync /dev/sdf1
说明:Raid Level : 阵列级别;
Array Size : 阵列容量大小;
Raid Devices : RAID成员的个数;
Total Devices : RAID中下属成员的总计个数,因为还有冗余硬盘或分区,也就是spare,为了RAID的正常运珩,随时可以推上去加入RAID的;
State : clean, degraded, recovering 状态,包括三个状态,clean 表示正常,degraded 表示有问题,recovering 表示正在恢复或构建;
Active Devices : 被激活的RAID成员个数;
Working Devices : 正常的工作的RAID成员个数;
Failed Devices : 出问题的RAID成员;
Spare Devices : 备用RAID成员个数,当一个RAID的成员出问题时,用其它硬盘或分区来顶替时,RAID要进行构建,在没构建完成时,这个成员也会被认为是spare设备;
UUID : RAID的UUID值,在系统中是唯一的;
8、
创建raid配置文件,默认不存在自己创建。
此配置文件的主要作用是系统启动的时候能够自动加载软RAID,同时也方便日后管理。但不是必须的,推荐对该文件进行配置。
这里需要创建这个文件,测试中发现,如果没有这个文件,则reboot 后,已经创建好的md0 会自动变成md127。
/etc/mdadm.conf 文件内容包括:
由DEVICE 选项指定用于软RAID的所有设备,和ARRAY 选项所指定阵列的设备名、RAID级别、阵列中活动设备的数目以及设备的UUID号。
创建/etc/mdadm.conf
# echo DEVICE /dev/sd{c,d,e,f}1 >> /etc/mdadm.conf
# mdadm –Ds >> /etc/mdadm.conf
9、格式化存储阵列
# mkfs.ext4 /dev/md0
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=128 blocks, Stripe width=512 blocks
268173312 inodes, 1072691712 blocks
53634585 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=3221225472
32736 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, 2654208,
4096000, 7962624, 11239424, 20480000, 23887872, 71663616, 78675968,
102400000, 214990848, 512000000, 550731776, 644972544

Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
10、创建挂载点并挂载到相应的目录下
# mkdir /data
# mount /dev/md0 /data/
# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 29G 1.7G 26G 7% /
devtmpfs 6.9G 0 6.9G 0% /dev
tmpfs 6.9G 0 6.9G 0% /dev/shm
tmpfs 6.9G 8.3M 6.9G 1% /run
tmpfs 6.9G 0 6.9G 0% /sys/fs/cgroup
/dev/sdb1 133G 61M 126G 1% /mnt/resource
/dev/md0 4.0T 89M 3.8T 1% /data
11、修改/etc/fstab(开机自动挂载)
添加如下内容
/dev/md0 /data ext4 defaults 0 0
12、reboot重启查看开机是否能正常。创建完成。