terça-feira, 15 de maio de 2018

How To Linux software RAID1 Tutorial



Linux Software RAID1 (Mirror)
Simple step by step Tutorial with fake disks

In this tutorial I'll show how to create a RAID1 (MIRROR) in Linux using files (non block device) to simulate hard disks (block devices).


INSTALLING AND CONFIGURING RAID1 ON LINUX

1) Install MADM

Debian/Ubuntu: 
# apt-get install mdadm 

CentOS/RedHat
# yum install mdadm 

2) Create an empty files with 1GB size
# dd if=/dev/zero of=fakedisk1 bs=1M count=1000 

# fdisk fakedisk1
fdisk fakedisk1 Command (m for help): n
Select (default p): p
Partition number (1-4, default 1):  (press enter)
First sector (2048-2047999, default 2048): (press enter)
Last sector, +sectors or +size{K,M,G,T,P} (2048-2047999, default 2047999):  (press enter)
Command (m for help): t
Hex code (type L to list all codes): fd
Command (m for help): p
Device      Boot Start     End Sectors  Size Id Type
fakedisk1p1       2048 2047999 2045952  999M fd Linux raid autodetect
Command (m for help): w

3) Repeat step 2 and do the same with fakedisk2

4) Cheating Linux to think the files are harddisks
# losetup /dev/loop1 fakedisk1 
# losetup /dev/loop2 fakedisk2 

Now the block devices /dev/loop1 and /dev/loop2 works like a harddisk

5) Creating RAID1 devices:
# mdadm --create /dev/md0 --level=mirror --raid-devices=2 /dev/loop1 /dev/loop2
Continue creating array? y
mdadm: Defaulting to version 1.2 metadata
mdadm: array /dev/md0 started.

6) Checking RAID1

Show Raid1 status:
# cat /proc/mdstat

Check Raid devices type
# mdadm -E /dev/loop1 /dev/loop2
# mdadm --detail /dev/md0

7) Creating Filesystem on the RAID Device
# mkfs.ext4 /dev/md0

You can change mkfs to any desired filesystem like mkfs.ext2, mkfs.ext3, mkfs.xfs, mkfs.ntfs

8) Mount the newly created filesystem under /mnt/raid1
# mkdir /mnt/raid1disk
# mount /dev/md0 /mnt/raid1

Now you can save your files under /mnt/raid1/ and the data will be mirrored to fakedisk1 and fakedisk2.

Try writing something to the directory:
# cd /mnt/raid1disk
# echo "Hello World" > sample.txt


9) To auto-mount RAID1 on system reboot, you need to make an entry in fstab file:
# nano /etc/fstab 
Insert at the end of the file:
/dev/md0        /mnt/raid1disk     ext4    defaults        0 0


Run 'mount -a' to check errors in fstab:
# mount -av


10) Save the Raid configuration manually to 'mdadm.conf' file:
# mdadm --detail --scan --verbose >> /etc/mdadm/mdadm.conf

SIMULATING A DISK FAILURE

Show the disks in the array
# mdadm --detail /dev/md0
...
   State : clean
...
    Number   Major   Minor   RaidDevice State
       0       7        1        0      active sync   /dev/loop1
       1       7        2        1      active sync   /dev/loop2


Simulate a disk fail
# mdadm --fail /dev/md0 /dev/loop1

Verify the array
#  mdadm --detail /dev/md0
...
         State : clean, degraded
...
    Number   Major   Minor   RaidDevice State
       -       0        0        0      removed
       1       7        2        1      active sync   /dev/loop2
       0       7        1        -      faulty        /dev/loop1


Verity the test file, is still acessible: 
# cat /mnt/raid1disk/sample.txt
Hello World

Re-add device to array:
Remove from array and add it again:
# mdadm --remove /dev/md0 /dev/loop1

Create a new fake harddisk:
# dd if=/dev/zero of=fakedisk3 bs=1M count=1000
# losetup /dev/loop3 fakedisk3

Add the new disk to the array: 
# mdadm --add /dev/md0 /dev/loop3

Show the Array status:
# mdadm --detail /dev/md0
...

        State : clean, degraded, recovering
...
    Number   Major   Minor   RaidDevice State
       2       7        3        0      spare rebuilding   /dev/loop3
       1       7        2        1      active sync        /dev/loop2



Wait the rebuilding, after some time it will show active again:
# mdadm --detail /dev/md0
    Number   Major   Minor   RaidDevice State
       2       7        3        0      active sync   /dev/loop3
       1       7        2        1      active sync   /dev/loop2




REMOVE AND MOUNT A SINGLE DISK FROM ARRAY

You cannot remove a disk directly from the array, unless it is failed, so we first have to fail it (if the drive it is failed this is normally already in failed state and this step is not needed):
# mdadm --fail /dev/md0 /dev/loop1
and now we can remove it:
# mdadm --remove /dev/md0 /dev/loop1
Then mount the disk as a normal disk:
# mkdir /mnt/disk1
# mount /dev/loop1 /mnt/disk1
REMOVE THE RAID ARRAY

Find out your arrays:
# fdisk -l | grep md

Query your arrays to find what disks are contained
# mdadm --detail /dev/md0
Disk /dev/md0: 999.4 MiB, 1047986176 bytes, 2046848 sectors

# Unmount the array
# umount /mnt/raid1disk

Shut down the array
# mdadm --stop /dev/md0


REFERENCES





Nenhum comentário:

Postar um comentário

Atualização DELL INSPIRON 15R com Update para Windows 10 22H2 travado em 61% ou 99%

 Como atualizar notebook DELL INSPIRON 15R com Windows 10 que está com a atualização travada em 61% ou 99%: Processo de atualização manual d...