How to Make Software RAID in Linux

How to Make Software RAID in Linux

Add a new partition on the first physical disc:

# fdisk /dev/sda

Using the same option add a new partition on the second physical disc:

# fdisk /dev/sdb

Create a new RAID volume md0 type of RAID1 (raid level1):

# mdadm --create /dev/md0 --level=1 --raiddevices=2 /dev/sda7 /dev/sdb2

Check and verify addition:

# watch cat /proc/mdstat

Format the new RAID volume to ext3 filesystem:

# mkfs -t ext3 /dev/md0

Verify created RAID:

# mdadm --query /dev/md0

For more details enter:

# mdadm --query --detail /dev/md0

Create a directory for mounting RAID volume:

# mkdir /mirror

Mount RAID volume md0:

# mount /dev/md0 /mirror

Verify:

# df -hT

Let’s try to create directory /etc and file numbers.txt:

# cp -ar /etc /mirror
# seq 10000000 > /mirror/cisla.txt

Verify:

# ls -ltr /mirror
# tail /mirror/subor.txt

You can anytime monitor volume [UU]:

# cat /proc/mdstat

We need to configure automatic mounting volume md0 after the system starts:

# cat /etc/mtab | grep md0
# joe /etc/fstab

Append next lines at the end of file /etc/fstab:

/dev/md0 /mirror ext3 defaults 0 0

Append next lines at the end of file /etc/mdadm.conf. If any error occurred, the system will send an email about it.

DEVICE /dev/sd[a-z]*
ARRAY /dev/md0 level=raid1 devices=/dev/sda7,/dev/sdb2
MAILADDR root@localhost

Turn the monitoring service on:

# service mdmonitor start

Try to make disc down:

# yes 'We love linux' > /mirror/linux.txt

Caution: yes command will repeatedly output the string until killed.
If you pull out the disc during the yes command you should get an email with the error message:

# mutt

We can see one disc is not available [_U]:

# cat /proc/mdstat

Removing partitions that physically don’t exist:

# mdadm /dev/md0 --detached

Removing partitions that physically exist:

# mdadm --manage /dev/md0 --remove /dev/sdb2

Connect physical disc and monitor how it is being synchronized:

# mdadm --manage /dev/md0 --add /dev/sdb2
# watch cat /proc/mdstat

Test RAID functionality. We should see “active raid1”:

# cat /proc/mdstat

verify RAID:

# mdadm --detail /dev/md0

Disable RAID:

# umount /dev/md0
# mdadm --manage --stop /dev/md0

Test RAID functionality again, we should not see “active raid1”:

# cat /proc/mdstat

RAID: removing

Unmount mounted partition:

# umount /mirror

Stop RAID (to start it again use –assemble –scan):

# mdadm --stop /dev/md0

Remove RAID:

# mdadm --remove /dev/md0

RAID partitions are readable, we can mount it:

# mount /dev/sdb2 /mnt

We should see details about RAID:

# ls -ltrh /mnt

Unmount RAID partition:

# umount /dev/sdb2

Leave a Comment