CentOS/Redhat:创建软件RAID 1阵列

时间:2020-01-09 10:34:15  来源:igfitidea点击:

最近,我在安装后向Linux服务器添加了另一个73GB SAS磁盘。
我在旧硬盘上有30GB的空分区。
如何将旧的/dev/sda3和新的/dev/sdb1(均为30GB)转换为RAID 1,以提高NFS服务器的速度和可靠性?

RAID设备是由两个或多个实际块设备创建的虚拟设备。

Linux支持RAID1和其他级别。
您需要在两个磁盘上具有相同大小的分区,即在第二个磁盘上创建与第一个磁盘完全相同的分区,并将类型设置为fd(Linux raid autodetect)。
您需要使用以下命令创建RAID 1。

警告!如果不正确执行这些示例,可能会导致计算机崩溃或丢失数据。
这些示例涉及使用fdisk命令处理磁盘分区以及使用mkfs.ext3命令构建(格式化)文件系统。
因此,在键入以下任一命令之前,请确保"您已备份所有数据"。

我们的示例RAID -1设置具有两个分区:

+------------+
|            |
| /dev/sda3  | ===============+
|            |                |
+------------+                |
  37GB                        | =======> RAID-1 /dev/md0 
                              |  
+------------+                |
|            |                |
| /dev/sdb1  | ===============+
|            |
+------------+
  37GB

步骤1:列出分区

键入以下命令:

# fdisk -l

# fdisk -l /dev/sda

输出示例:

Disk /dev/sda: 73.2 GB, 73295462400 bytes
255 heads, 63 sectors/track, 8910 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          13      104391   83  Linux
/dev/sda2              14        3929    31455270   83  Linux
/dev/sda3            3930        8779    38957625   83  Linux
/dev/sda4            8780        8910     1052257+   5  Extended
/dev/sda5            8780        8910     1052226   82  Linux swap / Solaris

例如,有目的地使用/dev/sda3(大小为37G)。
现在,列出第二个磁盘分区,输入:

# fdisk -l /dev/sdb

输出示例:

Disk /dev/sdb: 73.2 GB, 73295462400 bytes
255 heads, 63 sectors/track, 8910 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System

步骤2:为/dev/sdb创建37G分区

键入以下命令:

# fdisk /dev/sdb

输出示例:

The number of cylinders for this disk is set to 8910.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
   (e.g., DOS FDISK, OS/2 FDISK)

Command (m for help):

要添加新分区,请键入n命令,然后键入p命令,如下所示:

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-8910, default 1): 
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-8910, default 8910): +37G

要打印分区表类型p命令,请输入:

Command (m for help): p

Disk /dev/sdb: 73.2 GB, 73295462400 bytes
255 heads, 63 sectors/track, 8910 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1          37      297171   83  Linux

最后,将分区类型设置为Software Raid,即键入0xfd \ *(类型t命令后跟fd代码以将分区类型更改为Software RAID):

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

Command (m for help): p

Disk /dev/sdb: 73.2 GB, 73295462400 bytes
255 heads, 63 sectors/track, 8910 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1          37      297171   fd  Linux raid autodetect

保存更改并通过键入w命令退出到shell提示符:

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

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

步骤3:为/dev/sda创建/更新37G分区

您需要使用fdisk命令删除/dev/sda3(确保备份了所有数据)和/或将其设置为raid,如下所示:

# fdisk /dev/sda

输出示例:

The number of cylinders for this disk is set to 8910.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
   (e.g., DOS FDISK, OS/2 FDISK)

Command (m for help): p

Disk /dev/sda: 73.2 GB, 73295462400 bytes
255 heads, 63 sectors/track, 8910 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          13      104391   83  Linux
/dev/sda2              14        3929    31455270   83  Linux
/dev/sda3            3930        8779    38957625   83  Linux
/dev/sda4            8780        8910     1052257+   5  Extended
/dev/sda5            8780        8910     1052226   82  Linux swap / Solaris

Command (m for help): t
Partition number (1-5): 3
Hex code (type L to list codes): fd
Changed system type of partition 3 to fd (Linux raid autodetect)

Command (m for help): p

Disk /dev/sda: 73.2 GB, 73295462400 bytes
255 heads, 63 sectors/track, 8910 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          13      104391   83  Linux
/dev/sda2              14        3929    31455270   83  Linux
/dev/sda3            3930        8779    38957625   fd  Linux raid autodetect
/dev/sda4            8780        8910     1052257+   5  Extended
/dev/sda5            8780        8910     1052226   82  Linux swap / Solaris

Command (m for help): 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.
Syncing disks.

重新加载分区表更改

使用partprobe命令无需重新启动该框即可重新加载更改:

# partprobe  /dev/sda

或者

# echo 1 > /sys/block/sdb/device/rescan

步骤3:创建RAID 1阵列

要使用/dev/sda3和/dev/sdb1创建RAID1,请输入:

# mdadm --create /dev/md0 --level=1 --raid-devices=2 /dev/sda3 /dev/sdb1

要查看RAID构建进度,请输入:

# watch cat /proc/mdstat

输出示例:

RAID 1镜像

构建完成后,在新的软件RAID设备上创建文件系统,输入:

# mkfs.ext3 /dev/md0
mke2fs 1.39 (29-May-2006)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
4872896 inodes, 9737360 blocks
486868 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=4294967296
298 block groups
32768 blocks per group, 32768 fragments per group
16352 inodes per group
Superblock backups stored on blocks: 
	32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 
	4096000, 7962624

Writing inode tables: done                            
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 39 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.

将RAID1挂载到/raid1

键入以下命令:

# mkdir /raid1

# mount /dev/md0 /raid1/

# df -H /raid1/

输出示例:

Filesystem             Size   Used  Avail Use% Mounted on
/dev/md0                40G   185M    38G   1% /raid1

如何测试RAID 1阵列速度?

只需使用dd命令,如下所示:

# cd /raid1/

# dd if=/dev/zero of=output bs=8k count=256k

# rm output
262144+0 records in
262144+0 records out
2147483648 bytes (2.1 GB) copied, 6.01068 seconds, 357 MB/s

更新/etc/fstab

重新引导系统时,请确保已安装RAID1(注意>>附加重定向):

# echo '/dev/md0 /raid1 ext3 noatime,rw 0 0' >> /etc/fstab

如何获取有关RAID 1的详细信息?

键入以下命令:

# mdadm --query --detail /dev/md0

输出示例:

/dev/md0:
        Version : 0.90
  Creation Time : Wed May 26 01:58:53 2010
     Raid Level : raid1
     Array Size : 38949440 (37.15 GiB 39.88 GB)
  Used Dev Size : 38949440 (37.15 GiB 39.88 GB)
   Raid Devices : 2
  Total Devices : 2
Preferred Minor : 0
    Persistence : Superblock is persistent

    Update Time : Wed May 26 02:18:22 2010
          State : clean
Active Devices : 2
Working Devices : 2
Failed Devices : 0
  Spare Devices : 0

           UUID : b6f207e4:7f73484f:53f64bfd:e70b77d8
         Events : 0.2

    Number   Major   Minor   RaidDevice State
       0       8        3        0      active sync   /dev/sda3
       1       8       17        1      active sync   /dev/sdb1