mdadm creating a new array


This is a short guide on using mdadm, and other command line tools to create a RAID 1 mirrored array from 2 harddrives in Linux (Ubuntu 14.04)


Firstly, lets get a list of the harddrives in the system:

$ lsblk
sda                       8:0    0 447.1G  0 disk 
sdb                       8:16   0 447.1G  0 disk 
sdc                       8:32   0   1.8T  0 disk 
├─sdc1                    8:33   0   512M  0 part /boot
└─sdc2                    8:34   0   1.8T  0 part 

We’re going to use sda, and sdb to create the array of devices. I am going to be creating a partition that nearly fills the harddrives (440G), but not completely. This allows us to rebuild from other drives in the future that may not be exactly the same size.

Creating the partitions

Note: We are deleting and modifying partitions here, please make sure you have backed up data, and that you are using the correct drive numbers

$ fdisk /dev/sda
Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
Partition number (1-4, default 1): 
Using default value 1
First sector (2048-937703087, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-937703087, default 937703087): +440G

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

Command (m for help): p
Disk /dev/sda: 480.1 GB, 480103981056 bytes
255 heads, 63 sectors/track, 58369 cylinders, total 937703088 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 identifier: 0x000071b7

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1            2048   922748927   461373440   fd  Linux raid autodetect
Command (m for help): w
The partition table has been altered!

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

Do the same on /dev/sdb, and afterwards, check the partitions are OK:

$ lsblk
sda                       8:0    0 447.1G  0 disk 
└─sda1                    8:1    0   440G  0 part 
sdb                       8:16   0 447.1G  0 disk 
└─sdb1                    8:17   0   440G  0 part 
sdc                       8:32   0   1.8T  0 disk 

OK we have the two partitions. Make sure there no superblocks set on these two drives (i.e. make sure they’re clean):

$ mdadm --zero-superblock /dev/sda /dev/sdb
mdadm: Unrecognised md component device - /dev/sda
mdadm: Unrecognised md component device - /dev/sdb

This shows that these were clean drives, but it’s worth doing. Next we’re going to create the array itself

$ mdadm --create /dev/md0 --level=1 --raid-devices=2 /dev/sda1 /dev/sdb1
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
Continue creating array? y       
mdadm: Defaulting to version 1.2 metadata
mdadm: array /dev/md0 started.

Check the state of the array:

$ cat /proc/mdstat
Personalities : [linear] [multipath] [raid0] [raid1] [raid6] [raid5] [raid4] [raid10] 
md0 : active raid1 sdb1[1] sda1[0]
      461242176 blocks super 1.2 [2/2] [UU]
      [>....................]  resync =  0.1% (893248/461242176) finish=34.3min speed=223312K/sec

Add the information from the mdadm --detail --scan to the /etc/mdadm/mdadm.conf file, so quicker boots, etc and the device is found. Remove the name field from the output of the mdadm command.

ARRAY /dev/md0 metadata=1.2 name=hogg:0 UUID=23529206:b91dd393:10786167:71e5ce60

Also you need to update the boot image with the following:

$ update-initramfs -u

And that’s it for creating a mirrored RAID 1 array. We’re not building file systems on this device, as I will be using it as the basis of a LVM with snapshots. In a follow up guide here, I’ll be creating a logical volume on top of this raid array.


Leave a Reply

Your email address will not be published. Required fields are marked *