Software raid on linux

This post is borrowed from linuxaria.com please check out the original written by Ricardo

First, what’s a RAID ?

From Wikipedia

rtable

RAID (redundant array of independent disks, originally redundant array of inexpensive disks) is a storage technology that combines multiple disk drive components into a logical unit. Data is distributed across the drives in one of several ways called “RAID levels”, depending on what level of redundancy and performance (via parallel communication) is required.

This is a good definition for the original RAID concept, the hardware implementation, where 2 or more disks are connected to a controller that uses some dedicated CPU and RAM to perform all the tasks needed by your raid level, while on Linux this is traditionally done via a software RAID.
This means that in Linux we’ll use some of our CPU and RAM resources to perform all the tasks needed to keep our disks on RAID up and running, this naturally cost to our system some resource, but also means that we don’t have to buy any extra RAID controller for our computers, and with today CPU and RAM this is usually a negligible cost in terms of CPU cycles.

In this article I’ll setup the 2 disks in a RAID 1 configuration also known as “disk mirroring”, data written to one disk drive is simultaneously written to another disk drive. During writes, there will be a minor performance penalty when compared to writing to a single disk. If one drive fails, all data are preserved on the other drive.
RAID 1 offers extremely high data reliability, but at the cost of doubling the required data storage capacity.

Installation of the software

mdadm is the standard RAID management tool available on Linux and should be found in any modern distribution, so to install it on our system we can use the command:

sudo apt-get install mdadm

This will probably install also the package postfix, configuring it is useful to send emails if the system found that the RAID array is degraded, but if you are installing it on your desktop is safe enough to don’t configure it, while on any server i suggest to configure it to be able to send emails in case of problems.

Disk partitioning

We could use the whole disks and build the raid on top of them, but the best practice tell that is better to do 1 partition taking the whole disk and use that instead, no problem, we always follow the best practice :)

Also, I’ve Mint installed on a 500GB Disk, that in my system is mapped as sda, the new 2 disks are sdb and sdc and I’ll use them to make 1 new filesystem that I’ll mount under /data, so let’s go partitioning, for this task I’ve always used fdisk, if you prefer something more graphical you can use cfdisk.

As first thing you should identify your new disks, for this you can use the command dmesg or from a terminal as root use the command fdisk -l that in my case prints this:

Disk /dev/sda: 500.1 GB, 500107862016 bytes
255 heads, 63 sectors/track, 60801 cylinders, total 976773168 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk identifier: 0x000d3020
 
   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048    97656831    48827392   83  Linux
/dev/sda2        97658878   976771071   439556097    5  Extended
Partition 2 does not start on physical sector boundary.
/dev/sda5        97658880   105469951     3905536   82  Linux swap / Solaris
/dev/sda6       105472000   976771071   435649536   83  Linux
 
Disk /dev/sdb: 500.1 GB, 500107862016 bytes
255 heads, 63 sectors/track, 60801 cylinders, total 976773168 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: 0x00000000
 
Disk /dev/sdb doesn't contain a valid partition table
 
Disk /dev/sdc: 500.1 GB, 500107862016 bytes
255 heads, 63 sectors/track, 60801 cylinders, total 976773168 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: 0x00000000
 
Disk /dev/sdc doesn't contain a valid partition table

You should look for disks with doesn’t contain a valid partition table, they are the disks that you want to partition.

With fdisk just follow these simple instructions:

fdisk /dev/sdb

This will open a dialog, just use the following sequence to create a new primary partition that will take the whole disk and mark it as Linux Raid:

n          ; for a new partition
enter
p          ; for a primary partition
enter
1          ; number of partition
enter    ; accept the default
enter    ; accept the default
t          ; to change the type
fd        ; sets the type to be “Linux raid auto detect” (83h)
w         ; write changes to disk and exit

Repeat for the second disk, this time start with

fdisk /dev/sdc

and use the same sequence, now we can use /dev/sdb1 and /dev/sdc1

Create the RAID

Now we can finally create our RAID, the syntax is:

mdadm --create /dev/md0  --level=mirror --raid-devices=2 /dev/sdb1 /dev/sdc1
mdadm: Defaulting to version 1.2 metadata
mdadm: array /dev/md0 started.

This will create the /dev/md0 virtual device created from the two real block devices /dev/sdb1 and /dev/sdc1, configuring it in mirror mode.

Ok, now we’re all set to start initializing the RAID. The mirror must be constructed, eg. the contents (however unimportant now, since the device is still not formatted) of the two devices must be synchronized.
Check out the /proc/mdstat file. It should tell you that the /dev/md0 device has been started, that the mirror is being reconstructed, and an ETA of the completion of the reconstruction.

cat /proc/mdstat
Personalities : [raid1] 
md0 : active raid1 sdc1[1] sdb1[0]
      488253440 blocks super 1.2 512K chunks 2 far-copies [2/2] [UU]
      [>....................]  resync =  0.3% (1625024/488253440) finish=169.9min speed=47730K/sec

Reconstruction is done using idle I/O bandwidth. So, your system should still be fairly responsive, although your disk LEDs should be glowing nicely.
The reconstruction process is transparent, so you can actually use the device even though the mirror is currently under reconstruction.
We’ll now format the device, while the reconstruction is running. It will work.
Also you can mount it and use it while reconstruction is running.
Of course, if the disk breaks while the reconstruction is running, you’re out of luck.

Create the mdadm.conf file

In Ubuntu the system scan all devices during the boot and automatically activates the raids, so it’s not necessary to have an mdadm.conf file configured, but as best practice i suggest to write your Raid configuration there, in Ubuntu this file is located in /etc/mdadm/mdadm.conf and the easiest way to configure it is to use the following command:

sudo mdadm --detail --scan --verbose > /etc/mdadm/mdadm.conf

In my case this file contains:

ARRAY /dev/md0 level=raid1 num-devices=2 metadata=1.2 name=mint-desktop:0 UUID=048c40d4:16d71dda:1c3ba4ca:8babae51
   devices=/dev/sdb1,/dev/sdc1

Create a filesystem on the Raid metadevice

I’ll format the raid with an ext4 filesystem so to do this I use the command:

sudo mkfs.ext4 /dev/md0

As expected this has finished without any error in few second also if the mirror is still in reconstruction.
Now I create the mountpoint and I mount the new filesystem on it with:

sudo mkdir /data
sudo mount /dev/md0 /data
sudo chmod 777 /data # I want all users to be able to work there
sudo chmod +t /data  #But i want that only the owner can delete their contents.
tune2fs -m 1 /dev/md0 #reserve just 1% of the disk for maintenance

And as last thing I add a line to the file /etc/fstab, so the array is automatically mounted in /data during the boot process

echo "/dev/md0 /data/ ext4 defaults 1 2" >> /etc/fstab

SMALL UPDATE

After a reboot the system told me that /dev/md0 was not available, and so the mount of /media was skipped in the boot phase.
I checked how the autoscan process named my meta device and for me this is:

mint-desktop dev # ls -l /dev/md/mint-desktop\:0 
lrwxrwxrwx 1 root root 8 Dec 30 12:12 /dev/md/mint-desktop:0 -> ../md127
/dev/md/mint-desktop:0 -> ../md127

mint-desktop is the hostname of my desktop, so I’ve changed the line in fstab with:

/dev/md/mint-desktop:0 	/data/ 		ext4 			defaults 		1 2

And now /data is mounted automatically in the boot phase.

This post is borrowed from linuxaria.com please check out the original written by Ricardo

sjaak