1. 先要建立分区,分区类型为fdLinux raid autodetect。不做这步后面会无法开机自动启动软件raid

    sudo fdisk /dev/sdb
    
    Welcome to fdisk (util-linux 2.39.3).
    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.
    Created a new DOS (MBR) disklabel with disk identifier 0xfab081e6.
    
    Command (m for help): o
    Created a new DOS (MBR) disklabel with disk identifier 0xc3896bcc.
    
    Command (m for help): n
    Partition type
    p   primary (0 primary, 0 extended, 4 free)
    e   extended (container for logical partitions)
    Select (default p): p
    Partition number (1-4, default 1):
    First sector (2048-41943039, default 2048):
    Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-41943039, default 41943039):
    
    Created a new partition 1 of type 'Linux' and of size 20 GiB.
    
    Command (m for help): t
    Selected partition 1
    Hex code or alias (type L to list all): fd
    Changed type of partition 'Linux' to 'Linux raid autodetect'.
    
    Command (m for help): w
    The partition table has been altered.
    Calling ioctl() to re-read partition table.
    Syncing disks.
  2. 安装mdadmraid工具

    sudo apt install mdadm
  3. 创建磁盘阵列

    sudo mdadm --create /dev/md0 --level=1 --raid-devices=2 /dev/sdb1 /dev/sdc1

    查看同步状态,完成同步后可以进行后续步骤

    cat /proc/mdstat
Personalities : [raid0] [raid1] [raid6] [raid5] [raid4] [raid10]
md0 : active raid1 sdc[1] sdb[0]
      976630464 blocks super 1.2 [2/2] [UU]
      [>....................]  resync =  0.2% (2607936/976630464) finish=80.9min speed=200610K/sec
      bitmap: 8/8 pages [32KB], 65536KB chunk

unused devices: <none>
  1. 格式化磁盘阵列

    sudo mkfs.ext4 /dev/md0
  2. 挂载到目录

    sudo mkdir /data
    sudo mount /dev/md0 /data
  3. 将配置写入到文件设置开机生效

    sudo mdadm --detail --scan | sudo tee -a /etc/mdadm/mdadm.conf
    sudo update-initramfs -u
  4. 如果想要开机自动挂载还需要把配置写进fstab

检查软raid的方法

sudo mdadm --detail /dev/md0

删除raid的步骤

如果不需要软raid了,其删除步骤如下

  1. 卸载磁盘分区

    sudo umount /dev/md0
  2. 停止raid设备

    sudo mdadm --stop /dev/md0
  3. 删除raid的配置文件并更新initramfs

    # delete
    # ARRAY /dev/md0 metadata=1.2 UUID=XXXXXXXXXXXXX
    
    sudo update-initramfs -u
  4. 删除raid的元信息

    sudo mdadm --zero-superblock /dev/sdb
    sudo mdadm --zero-superblock /dev/sdc

参考链接:
https://developer.aliyun.com/article/1353755
https://blog.csdn.net/qq_44673299/article/details/113771436
https://www.digitalocean.com/community/tutorials/how-to-create-raid-arrays-with-mdadm-on-ubuntu
https://www.cnblogs.com/zhaojiedi1992/p/zhaojiedi_linux_041_raid.html