Dec 282012

I’ve just recovered 2x 500 GB sata disks from an old installation, so I opened up my new Desktop and connected them to my main Linux machine, these 2 disks have been used for around 3 years on the the other installation, so I prefer to use them in a mirrored configuration, or RAID 1, so everything that is wrote on a disk is copyed automatically also on the the other, and there is no loss of information if 1 of 2 disks broke up.

At the moment I use a Mint 14 XFCE edition, that is totally compatible with Ubuntu 12.10, and in my point of view for some aspects much better, so in this guide I’ll use commands that are compatible for Mint , Ubuntu and Debian, for other distributions you’ll have to search for your packages, but the configurations and commands will be the same.

First, what’s a RAID ?

From Wikipedia


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
p          ; for a primary partition
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

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


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.


With these simple instructions you can set up a software Raid on Linux, this will help in save your data if something bad happen to one of your hard disk, I also suggest to take a look at this article about RAID10, a good alternative to the Raid 1 that I’ve used in this article.


The Software-RAID HOWTO
Raid Setup
Non-standard RAID levels

Popular Posts:

Flattr this!

  10 Responses to “Configuring 2 mirrored disks on Ubuntu Linux”

  1. To fix the problem you mention in your small update (if your mdadm.conf file still refers to md0) you need to update your initramfs.

    sudo update-initramfs -u

  2. Salve io ho un dubbio, ho seguito la tua guida e alla fine sul mio kubuntu 11.10 sono riuscito s creare un Raid1 e tutto funziona, questo Raid contiene solo dati di lavoro. Ilmio quesito è il seguente adesso ho neccessità di formattare il sistema e reinstallare da zero la distro, metterò la 12.04 per questioni di supporto, il Raid non contiene il sistema ma sono altri HHD, quindi se io reinstallo non lo tocco ma come faccio a reimpostarlo nel nuovo sistema senza formattare o rifarlo? Non voglio e non devo perdere i dati penso che si la procedura per farlo basta rifare la parte finale della guida. Grazie per la risposta Drox

    • Ciao,

      Si basta rifare la parte in cui ridefinisci il raid, anche io ho reinstallato il SO su un certo disco, senza problemi sui due dischi in Raid.
      Le distribuzioni più nuove dovrebbero fare autodiscovery, segnati comunque la conf che vedi in : /etc/mdadm/mdadm.conf

      • Creare il file mdadm.conf

        In Ubuntu il sistema esegue una scansione di tutti i dispositivi durante l’avvio e attiva automaticamente i raid, quindi non è necessario avere un file mdadm.conf configurato, ma come best practice vi suggerisco di scrivere al suo interno la configurazione Raid, in Ubuntu questo file si trova in /etc/mdadm/mdadm.conf e il modo più semplice per configurarlo è quello di utilizzare il seguente comando:

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

        Nel mio caso questo file contiene:

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

        Quindi è questa parte qui? Dopo aver ovviamente intallato mdamd!

  3. Scusa, forse ti faccio una domanda banale, ma trattando di dati di lavoro vorrei essere sicuro di non perdere nulla.

    ho questo sistema
    /dev/sdc1 montato su /home da 250 GB
    /dev/sdb1 montato su /media/mirror da 250 GB

    /dev/sdb1 è una partizione vuota.

    nel comando mdadm per creare il RAID devo indicare prima il device origine e poi quello destinazione? cioè nel caso mio sarebbe
    madm –create /dev/md0 –level=mirror –raid-devices=2 /dev/sdc1 /dev/sdb1
    o sbaglio? Non vorrei che la sincronizzazione avvenisse dalla partizione vuota a quella “buona” cancellandomi tutto…

    La partizione /dev/sdb1 deve essere necessariamente montata all’avvio da fstab (per esempio sulla dir /mirror) ?

    • Ciao,

      In tutti i test che ho fatto il mirror veniva creato sui due device vuoti, e poi montando il device in raid ci scrivevo sopra i dati.
      Non so se il caso che ipotizzi sia fattibile, anzi se lo hai testato con successo..fammelo sapere.


  4. Ciao,

    Ho un server con due dischi in Raid, che funziona con Ubuntu. Vorrei fare un upgrade di dischi e allo stesso tempo anche un aggiornamento del sistema a una versione piu’ nuova. Sapete indicarmi un metodo per fare questa cosa? Io avevo pensato di mettere un disco nuovo e lasciare che faccia la ricostruzione e poi ripetere con il secondo disco, e in fine aggiornare il sistema. Ma penso che dovro’ riconfigurare i dischi Raid. Per fare il tutto posso utilizzare questa guida? Si puo’ applicare al mio caso?

    Ciao e Grazie

  5. Volevo fare il RAID 1 con due dischi da 3 TB, ho dato il comando fdisk /dev/sdb
    ed ho ottenuto questo come risposta:

    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.

    /dev/sdb: device contains a valid ‘ext4′ signature; it is strongly recommended to wipe the device with wipefs(8) if this is unexpected, in order to avoid possible collisions

    Device does not contain a recognized partition table.
    The size of this disk is 2,7 TiB (3000592982016 bytes). DOS partition table format can not be used on drives for volumes larger than 2199023255040 bytes for 512-byte sectors. Use GUID partition table format (GPT).

    Created a new DOS disklabel with disk identifier 0x2aa6102f.

    Comando (m per richiamare la guida):

    E’ possibile risolvere il problema? Ubuntu si trova su un SSD da 250 GB.

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>