centos服务器中,通过mdadm工具来创建维护软RAID,具体参数如下:
-C:新建 -c:指定chunk -a {yes|no}:是否为新建的raid设备自动创建设备文件 -A:装配 -l # :指定raid级别 -F :监控 -n # :用于raid的块设备的个数,不包括备盘 -x # :指定备盘的块设备个数
操作:
添加三块硬盘
[root@node ~]# fdisk -l Disk /dev/sdb: 21.5 GB, 21474836480 bytes 255 heads, 63 sectors/track, 2610 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: 0x00000000 Disk /dev/sdc: 32.2 GB, 32212254720 bytes 255 heads, 63 sectors/track, 3916 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: 0x00000000 Disk /dev/sdd: 21.5 GB, 21474836480 bytes 255 heads, 63 sectors/track, 2610 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: 0x00000000 Disk /dev/sda: 21.5 GB, 21474836480 bytes 255 heads, 63 sectors/track, 2610 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: 0x0000c75b Device Boot Start End Blocks Id System /dev/sda1 * 1 64 512000 83 Linux Partition 1 does not end on cylinder boundary. /dev/sda2 64 2611 20458496 8e Linux LVM
1.磁盘分区:
[root@node ~]# fdisk /dev/sdb Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel Building a new DOS disklabel with disk identifier 0x7e9d2c67. 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-2610, default 1): Using default value 1 Last cylinder, +cylinders or +size{K,M,G} (1-2610, default 2610): +5G Command (m for help): p Disk /dev/sdb: 21.5 GB, 21474836480 bytes 255 heads, 63 sectors/track, 2610 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: 0x7e9d2c67 Device Boot Start End Blocks Id System /dev/sdb1 1 654 5253223+ 83 Linux Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 2 First cylinder (655-2610, default 655): Using default value 655 Last cylinder, +cylinders or +size{K,M,G} (655-2610, default 2610): +5G Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks. #/sdb分了2块大小为5G的/sdb1,/sdb2,余下的几块都按照这样分配 [root@node ~]# fdisk -l Disk /dev/sdb: 21.5 GB, 21474836480 bytes 255 heads, 63 sectors/track, 2610 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: 0x7e9d2c67 Device Boot Start End Blocks Id System /dev/sdb1 1 654 5253223+ 83 Linux /dev/sdb2 655 1308 5253255 83 Linux Disk /dev/sdc: 32.2 GB, 32212254720 bytes 255 heads, 63 sectors/track, 3916 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: 0x0166fc81 Device Boot Start End Blocks Id System /dev/sdc1 1 654 5253223+ 83 Linux /dev/sdc2 655 1308 5253255 83 Linux Disk /dev/sdd: 21.5 GB, 21474836480 bytes 255 heads, 63 sectors/track, 2610 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: 0x7c530055 Device Boot Start End Blocks Id System /dev/sdd1 1 654 5253223+ 83 Linux /dev/sdd2 655 1308 5253255 83 Linux Disk /dev/sda: 21.5 GB, 21474836480 bytes 255 heads, 63 sectors/track, 2610 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: 0x0000c75b Device Boot Start End Blocks Id System /dev/sda1 * 1 64 512000 83 Linux Partition 1 does not end on cylinder boundary. /dev/sda2 64 2611 20458496 8e Linux LVM Disk /dev/mapper/vg_node-lv_root: 18.9 GB, 18865979392 bytes 255 heads, 63 sectors/track, 2293 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: 0x00000000 Disk /dev/mapper/vg_node-lv_swap: 2080 MB, 2080374784 bytes 255 heads, 63 sectors/track, 252 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: 0x00000000
mdadm配置前,先查看本地是否有raid:
[root@node ~]# cat /proc/partitions major minor #blocks name 8 16 20971520 sdb 8 17 5253223 sdb1 8 18 5253255 sdb2 8 32 31457280 sdc 8 33 5253223 sdc1 8 34 5253255 sdc2 8 48 20971520 sdd 8 49 5253223 sdd1 8 50 5253255 sdd2 8 0 20971520 sda 8 1 512000 sda1 8 2 20458496 sda2 253 0 18423808 dm-0 253 1 2031616 dm-1 [root@node ~]# cat /proc/mdstat Personalities : unused devices: <none> [root@node ~]# ls /dev/md* ls: cannot access /dev/md*: No such file or directory
[root@node ~]# mdadm -C /dev/md0 -a yes -l 0 -n 2 /dev/sdb1 /dev/sdd1 #创建raid 0,磁盘用/sdb1,/sdd1 mdadm: Defaulting to version 1.2 metadata mdadm: array /dev/md0 started. [root@node ~]# cat /proc/mdstat #查看是否创建 Personalities : [raid0] md0 : active raid0 sdd1[1] sdb1[0] 10506240 blocks super 1.2 512k chunks unused devices: <none> [root@node ~]# mke2fs -t 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=128 blocks, Stripe blocks 657072 inodes, 2626560 blocks 131328 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=2692743168 81 block groups 32768 blocks per group, 32768 fragments per group 8112 inodes per group Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632 Writing inode tables: done Creating journal (32768 blocks): done Writing superblocks and filesystem accounting information: done This filesystem will be automatically checked every 27 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override. [root@node ~]# mount /dev/md0 /backup/ #挂载 mount: mount point /backup/ does not exist [root@node ~]# mkdir /backup [root@node ~]# mount /dev/md0 /backup/ [root@node ~]# cp /etc/fstab /backup/ [root@node ~]# cd /backup/ [root@node backup]# cat fstab # # /etc/fstab # Created by anaconda on Wed Apr 15 01:35:08 2015 # # 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/vg_node-lv_root / ext4 defaults 1 1 UUID=f8e87dfa-7f58-4396-94e6-5caf9b46eca5 /boot ext4 defaults 1 2 /dev/mapper/vg_node-lv_swap swap swap defaults 0 0 tmpfs /dev/shm tmpfs defaults 0 0 devpts /dev/pts devpts gid=5,mode=620 0 0 sysfs /sys sysfs defaults 0 0 proc /proc proc defaults 0 0 [root@node backup]# mdadm -D /dev/md md/ md0 [root@node backup]# mdadm -D /dev/md0 /dev/md0: Version : 1.2 Creation Time : Tue Apr 21 23:43:44 2015 Raid Level : raid0 Array Size : 10506240 (10.02 GiB 10.76 GB) Raid Devices : 2 Total Devices : 2 Persistence : Superblock is persistent Update Time : Tue Apr 21 23:43:44 2015 State : clean Active Devices : 2 Working Devices : 2 Failed Devices : 0 Spare Devices : 0 Chunk Size : 512K Name : node.magine356wj.com:0 (local to host node.magine356wj.com) UUID : 9e0ed1ff:4079aca5:9c9b9f6d:31b2519a Events : 0 Number Major Minor RaidDevice State 0 8 17 0 active sync /dev/sdb1 1 8 49 1 active sync /dev/sdd1
#Major:主设备号,用于区分设备类别
#Minor:次设备号,用于区分同一种类别下不同的具体设备
下面来创建raid1:
[root@node backup]# mdadm -C /dev/md1 -a yes -l 1 -n 2 -x 1 /dev/sd{b,c,d}2 ##创建raid1,并指定2块是raid,一块是备盘 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 Continue creating array? Continue creating array? (y/n) y mdadm: Defaulting to version 1.2 metadata mdadm: array /dev/md1 started. [root@node backup]# cat /proc/mdstat Personalities : [raid0] [raid1] md1 : active raid1 sdd2[2](S) sdc2[1] sdb2[0] 5249088 blocks super 1.2 [2/2] [UU] md0 : active raid0 sdd1[1] sdb1[0] 10506240 blocks super 1.2 512k chunks unused devices: <none> [root@node backup]# mke2fs -t ext4 /dev/md1 #格式化 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 blocks 328656 inodes, 1312272 blocks 65613 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=1346371584 41 block groups 32768 blocks per group, 32768 fragments per group 8016 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 This filesystem will be automatically checked every 21 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override. [root@node backup]# mount /dev/md1 /mnt ##挂载到/mnt [root@node backup]# cd [root@node ~]# cp /etc/fstab /mnt [root@node ~]# cd /mnt/ [root@node mnt]# cat fstab # # /etc/fstab # Created by anaconda on Wed Apr 15 01:35:08 2015 # # 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/vg_node-lv_root / ext4 defaults 1 1 UUID=f8e87dfa-7f58-4396-94e6-5caf9b46eca5 /boot ext4 defaults 1 2 /dev/mapper/vg_node-lv_swap swap swap defaults 0 0 tmpfs /dev/shm tmpfs defaults 0 0 devpts /dev/pts devpts gid=5,mode=620 0 0 sysfs /sys sysfs defaults 0 0 proc /proc proc defaults 0 0 [root@node mnt]# cd ###mdadm -f:把指定设备模拟损坏 -r:把损坏设备移除 -a:新增一个设备到阵列 [root@node ~]# mdadm /dev/md1 -f /dev/sdb2 # 损坏sdb2 mdadm: set /dev/sdb2 faulty in /dev/md1 [root@node ~]# mdadm -D /dev/md1 ##可以看到/sdd2从备盘状态转换,并正在同步,可以用watch -n 1 "mdadm -D /dev/md1" 监控同步进程 /dev/md1: Version : 1.2 Creation Time : Wed Apr 22 00:03:30 2015 Raid Level : raid1 Array Size : 5249088 (5.01 GiB 5.38 GB) Used Dev Size : 5249088 (5.01 GiB 5.38 GB) Raid Devices : 2 Total Devices : 3 Persistence : Superblock is persistent Update Time : Wed Apr 22 00:07:13 2015 State : clean, degraded, recovering Active Devices : 1 Working Devices : 2 Failed Devices : 1 Spare Devices : 1 Rebuild Status : 95% complete Name : node.magine356wj.com:1 (local to host node.magine356wj.com) UUID : d48c5751:40098eb5:9dfb0f26:a7732603 Events : 34 Number Major Minor RaidDevice State 2 8 50 0 spare rebuilding /dev/sdd2 1 8 34 1 active sync /dev/sdc2 0 8 18 - faulty /dev/sdb2 [root@node ~]# cat /mnt/fstab ##查看文件,没有损坏 # # /etc/fstab # Created by anaconda on Wed Apr 15 01:35:08 2015 # # 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/vg_node-lv_root / ext4 defaults 1 1 UUID=f8e87dfa-7f58-4396-94e6-5caf9b46eca5 /boot ext4 defaults 1 2 /dev/mapper/vg_node-lv_swap swap swap defaults 0 0 tmpfs /dev/shm tmpfs defaults 0 0 devpts /dev/pts devpts gid=5,mode=620 0 0 sysfs /sys sysfs defaults 0 0 proc /proc proc defaults 0 0 [root@node ~]# mdadm /dev/md1 -a /dev/sdc1 ##添加一块设备 mdadm: added /dev/sdc1 [root@node ~]# mdadm -D /dev/md1 ##成为备盘 /dev/md1: Version : 1.2 Creation Time : Wed Apr 22 00:03:30 2015 Raid Level : raid1 Array Size : 5249088 (5.01 GiB 5.38 GB) Used Dev Size : 5249088 (5.01 GiB 5.38 GB) Raid Devices : 2 Total Devices : 4 Persistence : Superblock is persistent Update Time : Wed Apr 22 00:09:53 2015 State : clean Active Devices : 2 Working Devices : 3 Failed Devices : 1 Spare Devices : 1 Name : node.magine356wj.com:1 (local to host node.magine356wj.com) UUID : d48c5751:40098eb5:9dfb0f26:a7732603 Events : 39 Number Major Minor RaidDevice State 2 8 50 0 active sync /dev/sdd2 1 8 34 1 active sync /dev/sdc2 0 8 18 - faulty /dev/sdb2 3 8 33 - spare /dev/sdc1 root@node ~]# mdadm /dev/md1 -r /dev/sdb2 ##移除sdb2 mdadm: hot removed /dev/sdb2 from /dev/md1 [root@node ~]# mdadm -D /dev/md1 /dev/md1: Version : 1.2 Creation Time : Wed Apr 22 00:03:30 2015 Raid Level : raid1 Array Size : 5249088 (5.01 GiB 5.38 GB) Used Dev Size : 5249088 (5.01 GiB 5.38 GB) Raid Devices : 2 Total Devices : 3 Persistence : Superblock is persistent Update Time : Wed Apr 22 00:21:39 2015 State : clean Active Devices : 2 Working Devices : 2 Failed Devices : 1 Spare Devices : 0 Name : node.magine356wj.com:1 (local to host node.magine356wj.com) UUID : d48c5751:40098eb5:9dfb0f26:a7732603 Events : 85 Number Major Minor RaidDevice State 2 8 50 0 active sync /dev/sdd2 3 8 33 1 active sync /dev/sdc1 1 8 34 - faulty /dev/sdc2
本文出自 “wj” 博客,请务必保留此出处http://magine356wj.blog.51cto.com/2664956/1636573
原文地址:http://magine356wj.blog.51cto.com/2664956/1636573