创建分区

# ls -l /dev/sd*
 brw-rw----. 1 root disk 8,  0 Sep 19 22:50 /dev/sda
 brw-rw----. 1 root disk 8,  1 Sep 19 22:50 /dev/sda1
 brw-rw----. 1 root disk 8,  2 Sep 19 22:50 /dev/sda2
 brw-rw----. 1 root disk 8, 16 Sep 19 22:54 /dev/sdb
brw-rw----. 1 root disk 8, 32 Sep 19 22:52 /dev/sdc

检查我们的设备还没有被配置为raid

# mdadm --examine /dev/sdb /dev/sdc
mdadm: No md superblock detected on /dev/sdb.
mdadm: No md superblock detected on /dev/sdc. 

可以看到,还没有为磁盘配置 raid

接下来开始创建分区

# fdisk /dev/sdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklab  
                  el
Building a new DOS disklabel with disk identifier 0x37a6235e.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.
Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)
WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').
Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-652, default 1):[Leave blank and hit ENTER for default]
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-652, default 652):[Leave blank and hit ENTER for default]
Using default value 652
Command (m for help): p
Disk /dev/sdb: 5368 MB, 5368709120 bytes
255 heads, 63 sectors/track, 652 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes/512 bytes
I/O size (minimum/optimal): 512 bytes/512 bytes
Disk identifier: 0x37a6235e
  Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         652     5237158+  83  Linux
Command (m for help): t
Selected partition 1
Hex code (type L to list codes): fd
Changed system type of partition 1 to fd (Linux raid autodetect)
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.

第二块硬盘

# fdisk /dev/sdc
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklab                                el
Building a new DOS disklabel with disk identifier 0x31c93154.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.
Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)
WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').
Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-783, default 1):[Leave blank and hit ENTER for default]
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-783, default 783):[Leave blank and hit ENTER for default]
Using default value 783
Command (m for help): t
Selected partition 1
Hex code (type L to list codes): fd
Changed system type of partition 1 to fd (Linux raid autodetect)
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.

通知操作系统我们添加了新的分区,并进行了分区更改。
让系统重新检测分区

# partprobe  /dev/sdb
# partprobe  /dev/sdc 

重新检查我们的磁盘的变化

# mdadm --examine /dev/sdb /dev/sdc
/dev/sdb:
   MBR Magic : aa55
Partition[0] :     10474317 sectors at           63 (type fd)
/dev/sdc:
   MBR Magic : aa55
Partition[0] :     12578832 sectors at           63 (type fd)

创建 RAID 分区

使用mdadm命令创建软 raid1

# mdadm --create /dev/md0 --level=1 --raid-devices=2 /dev/sdb1 /dev/sdc1
 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
 mdadm: largest drive (/dev/sdc1) exceeds size (5233024K) by more than 1%
 Continue creating array? y
 mdadm: Defaulting to version 1.2 metadata
 mdadm: array /dev/md0 started.
说明
--level设置RAID级别。当和--create一起使用时,可以使用的选项有:linear, raid0, 0, stripe, raid1, 1, mirror, raid4, 4, raid5, 5, raid6, 6, raid10, 10, multipath, mp, faulty, container
--raid-devices指定数组中的活动设备的数量。

下面的命令将显示RAID上的活动设备

# cat /proc/mdstat
Personalities : [raid1]
 md0 : active raid1 sdc1[1] sdb1[0]
       5233024 blocks super 1.2 [2/2] [UU]
unused devices: <none> 

查看有关md设备的更多详细信息:

# mdadm --detail /dev/md0
/dev/md0:
       Version : 1.2
  Creation Time : Fri Sep 19 23:02:52 2014
     Raid Level : raid1
     Array Size : 5233024 (4.99 GiB 5.36 GB)
  Used Dev Size : 5233024 (4.99 GiB 5.36 GB)
   Raid Devices : 2
  Total Devices : 2
    Persistence : Superblock is persistent
    Update Time : Fri Sep 19 23:07:39 2014
          State : clean
 Active Devices : 2
Working Devices : 2
 Failed Devices : 0
  Spare Devices : 0
           Name : test2.example:0  (local to host test2.example)
           UUID : 5a463788:9bf2659a:09d1c73a:9adcbbbd
         Events : 17
    Number   Major   Minor   RaidDevice State
       0       8       17        0      active sync   /dev/sdb1
       1       8       33        1      active sync   /dev/sdc1

格式化 mdadm 设备

现在要记住的一件事是,现在我们不使用任何一个分区,而是在一个包含两个分区的 raid 文件系统上工作

我们将使用 ext4 文件系统进行格式化,因为我使用的CentOS 6.4

# mkfs.ext4 /dev/md0
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
327680 inodes, 1308256 blocks
65412 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=1342177280
40 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

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

该文件系统将每 22 次挂载或者每180 天检查一次。
我们可以使用 tune2fs -c 或者 -i 来修改这两个值。

挂载 md 设备

# mkdir /myraid

# mount /dev/md0 /myraid/

检查分区:

# df -h /myraid/
Filesystem      Size  Used Avail Use% Mounted on
/dev/md0        5.0G  138M  4.6G   3% /myraid

如果有一个驱动器(硬盘)停止工作了,会发生什么?

要查看结果如何。

首先让我们在raid中创建一些文件,以便我们可以验证数据是否丢失。

我们把一些文件从 /root 复制到 /myraid/目录。用来测试。

# cp -rvf /root/* /myraid/
`/root/anaconda-ks.cfg' -> `/myraid/anaconda-ks.cfg'
`/root/Desktop' -> `/myraid/Desktop'
`/root/Documents' -> `/myraid/Documents'
`/root/Downloads' -> `/myraid/Downloads'
`/root/install.log' -> `/myraid/install.log'
`/root/install.log.syslog' -> `/myraid/install.log.syslog'
`/root/log' -> `/myraid/log'
`/root/Music' -> `/myraid/Music'
`/root/Pictures' -> `/myraid/Pictures'
`/root/Public' -> `/myraid/Public'
`/root/Templates' -> `/myraid/Templates'
`/root/Videos' -> `/myraid/Videos' 

现在我们有了需要保护的文件,即使raid设备出现任何硬盘故障。

让我们手动把其中一个硬盘弄失效

# mdadm --fail /dev/md0 /dev/sdb1
 mdadm: set /dev/sdb1 faulty in /dev/md0 

检查数据文件:

[root@test2 myraid]# ll
total 120
-rw-------. 1 root root  1629 Sep 19 23:18 anaconda-ks.cfg
drwxr-xr-x. 2 root root  4096 Sep 19 23:18 Desktop
drwxr-xr-x. 2 root root  4096 Sep 19 23:18 Documents
drwxr-xr-x. 2 root root  4096 Sep 19 23:18 Downloads
-rw-r--r--. 1 root root 49565 Sep 19 23:18 install.log
-rw-r--r--. 1 root root 10033 Sep 19 23:18 install.log.syslog
drwxr-xr-x. 2 root root  4096 Sep 19 23:18 log
drwx------. 2 root root 16384 Sep 19 23:14 lost+found
drwxr-xr-x. 2 root root  4096 Sep 19 23:18 Music
drwxr-xr-x. 2 root root  4096 Sep 19 23:18 Pictures
drwxr-xr-x. 2 root root  4096 Sep 19 23:18 Public
drwxr-xr-x. 2 root root  4096 Sep 19 23:18 Templates
drwxr-xr-x. 2 root root  4096 Sep 19 23:18 Videos 

文件都在,没有问题。

重要提示:需要再次记住,RAID 1至少需要2个硬盘用于镜像,因为如果这时另一块硬盘出现故障,您将丢失所有数据.

检查 mdraid 设备下的活动和可用设备

# mdadm --detail /dev/md0
/dev/md0:
        Version : 1.2
  Creation Time : Fri Sep 19 23:02:52 2014
     Raid Level : raid1
     Array Size : 5233024 (4.99 GiB 5.36 GB)
  Used Dev Size : 5233024 (4.99 GiB 5.36 GB)
   Raid Devices : 2
  Total Devices : 2
    Persistence : Superblock is persistent
    Update Time : Fri Sep 19 23:21:54 2014
          State : clean, degraded
 Active Devices : 1
Working Devices : 1
 Failed Devices : 1
  Spare Devices : 0
           Name : test2.example:0  (local to host test2.example)
           UUID : 5a463788:9bf2659a:09d1c73a:9adcbbbd
         Events : 19
    Number   Major   Minor   RaidDevice State
       0       0        0        0      removed
       1       8       33        1      active sync   /dev/sdc1
       0       8       17        -      faulty   /dev/sdb1
在Linux中如何创建软raid 1

在本文中,我们将介绍如何配置具有级别 1 的软件 RAID,即镜像功能。

首先,您需要了解RAID 1的前提条件,即它至少需要2个硬盘,并且由于我们正在配置软件RAID,因此您可以将同一硬盘划分为两个分区进行测试,但始终建议使用两个单独的硬盘,以便在出现任何故障时不会丢失任何数据。

我们将在 VMware Workstation 完成本次实验。

虚拟机已经添加了2块硬盘。
接下来我将从创建分区开始

日期:2020-06-02 22:18:28 来源:oir作者:oir