Isaac Handcrafted.codes // RAID-1
… a mentor of mine … once observed that anything with "science" in its name isn't really a science, whether social, political, or computer. A true science like physics or chemistry studies some aspect of physical reality. It is not concerned with how to build things; that is the province of engineering. Some parts of computer science lie within mathematics, but mathematics is not a science and is rarely claimed to be one.Communications of the Association for Computing Machinery (ACM), commenting on computology

Mirrored Redundant Array of Independent Disks (RAID-1) on Linux

The probablility of complete data loss goes like $\sigma^n$, where $\sigma$ is the probability of failure of an individual disk, and $n$ is the total number of disks. Given the reliability of most hard drives these days, $n$ = 2 should be sufficient for most folks. Note that as $n$ increases in a mirrored array, the probablility of complete loss goes down substantially since you are essentially multiplying fractions. This method is a good solution for data redundancy (but not backup—that's different).

Objective


Create a RAID-1 (mirrored) array /dev/md0 using two identical disks /dev/sdb and /dev/sdc. The process of creating the array will go something like this:

  1. Partition the disks
  2. Use the partitioned disks to create the array
  3. Create a filesystem for the array
  4. Format the array
Back-up your data before proceeding.
Tools like gnome-disk-utility or similar can help you decide which disks to use. Also, before starting, make sure the disks you plan to use are not mounted.

Install mdadm

sudo apt-get install mdadm

If mdadm is already installed, make sure the service is currently running and active on boot.
Graphical tools like BootUp-Manager are useful here.

Examining Arrays Using /proc/mdstat

The pseudofile /proc/mdstat will provide information about the current state of RAID devices and the md driver:
cat /proc/mdstat

Existing Arrays

You will not be able to create a new array using partitions that are members of an existing array. Therefore, in order to reuse disks, you will need to stop any active arrays of which they are members:
sudo mdadm -S /dev/md0

Reusing the partitions on these disks also means destroying the data on them (along with the array). Whenever you attempt to create a new array using mdadm, you will be warned about using member disks that already have a RAID superblock on them, even if they're not part of an array that's currently in use. You can suppress this prompt by using mdadm to remove the RAID superblock from the disks that were part of an array. This command allows you to remove the superblock from more than one disk at a time:
Back-up your data before proceeding!
sudo mdadm --zero-superblock /dev/sd{b,c}1

Creating an Array

It's time to partition disks and create the array. We can partition a disk for auto-detection like so:
sudo 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): n Partition number (2-128, default 2): 2 First sector (34-1953525134, default 2048): 34 Last sector, +sectors or +size{K,M,G,T,P} (34-2047, default 2047): 2047 Created a new partition 2 of type 'Linux filesystem' and of size 1007 KiB.
Now change the drive type of /dev/sdb1 to Linux RAID (0xFD) so that it can be detected automatically at boot time:
Command (m for help): t Partition number (1,2, default 2): 2 Hex code (type L to list all codes): 28 Changed type of partition 'Linux filesystem' to 'Linux RAID'. Command (m for help): w
Entering w writes the partition table to disk and exits.

Now do the same for /dev/sdc. After that, use mdadm to create a RAID-1 (mirrored) array:
You will probably have to unmount both disks again before proceeding to the next step.
sudo mdadm -Cv -l1 -n2 /dev/md0 /dev/sd{b,c}1

Whenever a new mirror is created, resynchronization occurs. Check this by issuing the command cat /proc/mdstat.
The initial synchronization is complete when the progress bar no longer appears.
Now create a filesystem for your new array:
sudo mkfs.ext4 /dev/md0

Finishing Touches

You aren't obligated to create an /etc/mdadm/mdadm.conf file, but it will make managing arrays using mdadm much easier. Let's start by looking at the output of sudo mdadm --detail /dev/md0:
/dev/md0: Version : 1.2 Creation Time : Mon Jun 13 15:26:59 2016 Raid Level : raid1 Array Size : 976629760 (931.39 GiB 1000.07 GB) Used Dev Size : 976629760 (931.39 GiB 1000.07 GB) Raid Devices : 2 Total Devices : 2 Persistence : Superblock is persistent Intent Bitmap : Internal Update Time : Mon Jun 13 18:54:15 2016 State : clean Active Devices : 2 Working Devices : 2 Failed Devices : 0 Spare Devices : 0 Name : DeWitt:0 (local to host DeWitt) UUID : 147987ca:fe805146:e6fc86f1:1ed7dbdb Events : 2520 Number Major Minor RaidDevice State 0 8 17 0 active sync /dev/sdb1 1 8 33 1 active sync /dev/sdc1
Note the value of UUID. You should put the line ARRAY /dev/md0 UUID=147987ca:fe805146:e6fc86f1:1ed7dbdb in your configuration file by doing sudo gedit /etc/mdadm/mdadm.conf (make sure to hash any other ARRAY lines corresponding to /dev/md0). Next, let's create a mount point for the array:
sudo mkdir /media/joel/Linux
Now tell your system to automatically mount the array at boot time by putting the line /dev/md0 /media/joel/Linux auto defaults 0 0 in your file systems table via the sudo gedit /etc/fstab command. As a one-off, you should tell the kernel take a look at the system again to figure out how to boot properly:
sudo update-initramfs -u

After rebooting, don't forget to format the array, or you won't be able to use it.
You're done!

References

Vadala, Derek. Managing RAID on Linux. Sebastopol: O'Reilly Media, 2002.
Leurs, Laurens. "RAID level 1 – Mirroring." Prepressure.com.
"Advantages / Disadvantages of RAID 0 vs. RAID 1." M-Tech Laptops.
back-to-top DANGER