Mdadm это утилита предназначенная для управления софтовыми raid-массивами в Linux.
С помощью mdadm можно сделать:
Есть задача сделать софтовый raid1 из двух дисков.
Находим диски которые планируется объединить в raid1, это /dev/sdb и /dev/sdс.
root@blackpc:~# fdisk -l Disk /dev/sda: 111,8 GiB, 120034123776 bytes, 234441648 sectors ... Disk /dev/sdb: 298,1 GiB, 320072933376 bytes, 625142448 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/sdc: 298,1 GiB, 320072933376 bytes, 625142448 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
С помощью программы fdisk меняем тип дисков /dev/sdb и /dev/sdс на FD (Linux raid auto).
root@blackpc:~# fdisk /dev/sdb Welcome to fdisk (util-linux 2.27.1). Changes will remain in memory only, until you decide to write them. Be careful before using the write command. Command (m for help): m Help: DOS (MBR) a toggle a bootable flag b edit nested BSD disklabel c toggle the dos compatibility flag Generic d delete a partition F list free unpartitioned space l list known partition types n add a new partition p print the partition table t change a partition type v verify the partition table i print information about a partition Misc m print this menu u change display/entry units x extra functionality (experts only) Script I load disk layout from sfdisk script file O dump disk layout to sfdisk script file Save & Exit w write table to disk and exit q quit without saving changes Create a new label g create a new empty GPT partition table G create a new empty SGI (IRIX) partition table o create a new empty DOS partition table s create a new empty Sun partition table 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-625142447, default 2048): Last sector, +sectors or +size{K,M,G,T,P} (2048-625142447, default 625142447): 625140000 Created a new partition 1 of type 'Linux' and of size 298,1 GiB. Command (m for help): 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 or c6 DRDOS/sec (FAT- 4 FAT16 <32M 40 Venix 80286 85 Linux extended c7 Syrinx 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 ea Rufus alignment e W95 FAT16 (LBA) 53 OnTrack DM6 Aux a5 FreeBSD eb BeOS fs f W95 Ext'd (LBA) 54 OnTrackDM6 a6 OpenBSD ee GPT 10 OPUS 55 EZ-Drive a7 NeXTSTEP ef EFI (FAT-12/16/ 11 Hidden FAT12 56 Golden Bow a8 Darwin UFS f0 Linux/PA-RISC b 12 Compaq diagnost 5c Priam Edisk a9 NetBSD f1 SpeedStor 14 Hidden FAT16 <3 61 SpeedStor ab Darwin boot f4 SpeedStor 16 Hidden FAT16 63 GNU HURD or Sys af HFS / HFS+ f2 DOS secondary 17 Hidden HPFS/NTF 64 Novell Netware b7 BSDI fs fb VMware VMFS 18 AST SmartSleep 65 Novell Netware b8 BSDI swap fc VMware VMKCORE 1b Hidden W95 FAT3 70 DiskSecure Mult bb Boot Wizard hid fd Linux raid auto 1c Hidden W95 FAT3 75 PC/IX bc Acronis FAT32 L fe LANstep 1e Hidden W95 FAT1 80 Old Minix be Solaris boot ff BBT Command (m for help): t Selected partition 1 Partition type (type L to list all types): fd Changed type of partition 'Linux' to 'Linux raid autodetect'. Command (m for help): p Disk /dev/sdb: 298,1 GiB, 320072933376 bytes, 625142448 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 Disklabel type: dos Disk identifier: 0xfed3067a Device Boot Start End Sectors Size Id Type /dev/sdb1 2048 625140000 625137953 298,1G fd Linux raid autodetect Command (m for help): w The partition table has been altered. Calling ioctl() to re-read partition table. Syncing disks.
Повторяем действия для диска /dev/sdc.
В результате вывод fdisk будет примерно таким:
root@blackpc:~# fdisk -l Disk /dev/sda: 111,8 GiB, 120034123776 bytes, 234441648 sectors ... Disk /dev/sdb: 298,1 GiB, 320072933376 bytes, 625142448 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 Disklabel type: dos Disk identifier: 0xfed3067a Device Boot Start End Sectors Size Id Type /dev/sdb1 2048 625140000 625137953 298,1G fd Linux raid autodetect Disk /dev/sdc: 298,1 GiB, 320072933376 bytes, 625142448 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 Disklabel type: dos Disk identifier: 0xf813c784 Device Boot Start End Sectors Size Id Type /dev/sdc1 2048 625140000 625137953 298,1G fd Linux raid autodetect
Для создание raid1 даем команду:
mdadm --create --verbose /dev/md0 --level=1 --raid-devices=2 /dev/sdb1 /dev/sdc1, где:
где:
Вывод команды.
root@blackpc:~# mdadm --create --verbose /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: size set to 312437888K mdadm: automatically enabling write-intent bitmap on large array Continue creating array? Continue creating array? (y/n) y mdadm: Defaulting to version 1.2 metadata mdadm: array /dev/md0 started.
Проверяем состояние массива и ждем когда он соберется.
root@blackpc:~# cat /proc/mdstat Personalities : [raid1] md0 : active raid1 sdc1[1] sdb1[0] 312437888 blocks super 1.2 [2/2] [UU] [>....................] resync = 3.3% (10455168/312437888) finish=54.6min speed=92170K/sec bitmap: 3/3 pages [12KB], 65536KB chunk
Что бы удобнее было смотреть за изменениями, можно использовать команду watch.
root@blackpc:~# watch -n 5 cat /proc/mdstat
В итоге примерно за час рейд собрался, все хорошо.
root@blackpc:~# cat /proc/mdstat Personalities : [raid1] md0 : active raid1 sdc1[1] sdb1[0] 312437888 blocks super 1.2 [2/2] [UU] bitmap: 0/3 pages [0KB], 65536KB chunk unused devices: <none>
Далее с /dev/md0 действуем как с обычным диском, сначала создаем раздел на нем, потом форматируем, потом монтируем.
Сначала смотрим вывод fdisk.
root@blackpc:~# fdisk -l Disk /dev/sda: 111,8 GiB, 120034123776 bytes, 234441648 sectors ... Disk /dev/md0: 298 GiB, 319936397312 bytes, 624875776 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
Создаем на /dev/md0 новый праймари раздел.
root@blackpc:~# fdisk /dev/md0 Welcome to fdisk (util-linux 2.27.1). 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 disklabel with disk identifier 0xe4109a7a. 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): 1 First sector (2048-624875775, default 2048): Last sector, +sectors or +size{K,M,G,T,P} (2048-624875775, default 624875775): Created a new partition 1 of type 'Linux' and of size 298 GiB. Command (m for help): p Disk /dev/md0: 298 GiB, 319936397312 bytes, 624875776 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 Disklabel type: dos Disk identifier: 0xe4109a7a Device Boot Start End Sectors Size Id Type /dev/md0p1 2048 624875775 624873728 298G 83 Linux Command (m for help): w The partition table has been altered. Calling ioctl() to re-read partition table. Syncing disks.
Форматируем /dev/md0p1 в ext4.
root@blackpc:~# mkfs.ext4 -v /dev/md0p1 mke2fs 1.42.13 (17-May-2015) fs_types for mke2fs.conf resolution: 'ext4' Filesystem label= OS type: Linux Block size=4096 (log=2) Fragment size=4096 (log=2) Stride=0 blocks, Stripe width=0 blocks 19529728 inodes, 78109216 blocks 3905460 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=4294967296 2384 block groups 32768 blocks per group, 32768 fragments per group 8192 inodes per group Filesystem UUID: deae21b6-b555-461a-a8b7-f684361cc671 Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 4096000, 7962624, 11239424, 20480000, 23887872, 71663616 Allocating group tables: done Writing inode tables: done Creating journal (32768 blocks): done Writing superblocks and filesystem accounting information: done
Правим fstab для автомонтирования.
# <file system> <mount point> <type> <options> <dump> <pass> ... /dev/md0p1 /mnt/data1 ext4 defaults 0 0
Тестово выдернем один диск из компьтера и посмотрим что будет.
Диски стоят в корзинах, но они не хотсвап.
После того как достали диск, через некоторое время, в выводе mdstat диск sdc1 пропал.
root@blackpc:~# cat /proc/mdstat Personalities : [raid1] [linear] [multipath] [raid0] [raid6] [raid5] [raid4] [raid10] md0 : active raid1 sdc1[1] 312437888 blocks super 1.2 [2/1] [_U] bitmap: 2/3 pages [8KB], 65536KB chunk
Вставлем диск обратно, но т.к. он не хотсват, до перезагрузки диск в системе не появится.
После презагрузки добавляем диск в массив и массив начинает ребилдиться.
root@blackpc:~# mdadm /dev/md0 --re-add /dev/sdb1 mdadm: re-added /dev/sdb1 root@blackpc:~# cat /proc/mdstat Personalities : [raid1] [linear] [multipath] [raid0] [raid6] [raid5] [raid4] [raid10] md0 : active raid1 sdb1[0] sdc1[1] 312437888 blocks super 1.2 [2/1] [_U] [>....................] recovery = 0.0% (177344/312437888) finish=139.6min speed=37269K/sec bitmap: 2/3 pages [8KB], 65536KB chunk unused devices: <none>
Состояние массива после ребилда. все хорошо.
root@blackpc:~# cat /proc/mdstat Personalities : [raid1] [linear] [multipath] [raid0] [raid6] [raid5] [raid4] [raid10] md0 : active raid1 sdb1[0] sdc1[1] 312437888 blocks super 1.2 [2/2] [UU] bitmap: 2/3 pages [8KB], 65536KB chunk unused devices: <none>