Linux disk and file system (3) ---- RAID array

RAID array

Problem introduction: What should I do if the disk capacity is not enough?
                     In case of data damage or loss caused by natural or human factors, how can you recover?

RAID: Redundant Arrays of Inexpensive Disks

RAID is a technique that combines multiple smaller disks into a larger disk unit. (Not only with storage function, but also with data protection function)
RAID has different functions due to the different levels selected, and the integrated disks have different functions.
a) RAID-0 (equal mode, stripe): best performance
b) RAID-1 (mapping mode, mirror): full backup
c) RAID 0 + 1 , RAID 1 + 0
d) RAID 5: Balanced consideration of performance and data backup
e) Spare Disk: spare disk


RAID-0 (equivalent mode): Best performance
This mode works better if it is composed of disks of the same type and capacity.
In this mode of RAID, the disk will be cut into equal blocks (such as 4KB) first, and then when a file is to be written to the RAID, the file will be cut according to the size of the block, and then placed on each disk in sequence Go inside.
Since each disk will store data interleaved, when your data is written to the RAID, the data will be placed on each disk equally.
For example, if you have two disks in RAID-0, when you have 100MB of data to write, each disk will be allocated 50MB of storage.

RAID-0 Features
1. When forming RAID-0, each disk (Disk A and Disk B) will first be divided into small blocks (chunk).
2. When there is data to be written to the RAID, the data will be cut into small blocks first, and then placed on different disks one by one in sequence.
3. Since the data has been cut first and placed on different disks in sequence, the amount of data each disk is responsible for is reduced.
4. According to this situation, the RAID-0 composed of more disks will have better performance, because the amount of data each is responsible for is lower, which means that my data can be scattered and stored on multiple disks. Of course, Efficiency will get better.
5. The total disk capacity has also increased, because the capacity of each disk will eventually add up to the total capacity of RAID-0.

shortcoming
1. You must bear the risk of data damage by yourself. From the above figure, we know that the file is cut into a block size suitable for each disk partition, and then placed in each disk in sequence. If a certain disk is damaged, the file data will be missing a piece, and the file will be damaged at this time . Since each file is stored in this way, as long as any disk in RAID-0 is damaged, all data on the RAID will be lost and cannot be read.
2. If disks of different capacities are used to form RAID-0, since the data is always placed on different disks in equal order, when the blocks of the small-capacity disks are used up, all the data will be written. to the largest disk.

Example: I use 200G and 500G to form RAID-0, then the initial 400GB data can be written to two disks at the same time (each consumes 200G capacity), and the data added later can only be written to the 500G disk. At this time, the performance becomes worse, because only one is left to store data.


RAID-1 (mapped mode, mirror): full backup

RAID-1 requires identical disk capacities, preferably identical disks. If disks of different capacities form RAID-1, the total capacity will be based on the smallest disk.

RAID-1 mainly allows the same data to be completely stored on two disks.

Example: If I have a 100MB file and I only have two disks in RAID-1, then the two disks will simultaneously write 100MB to their storage space. As a result, the overall RAID capacity is almost 50% less. Since the contents of the two hard drives are exactly the same, as if they are reflected in a mirror, we also call it mirror mode.


RAID-1 Features
1. After a piece of data is sent to RAID-1, it will be divided into two shares and written to each disk respectively.
2. Since the same data will be written to other different disks, if 100MB is to be written, the data will be copied to each disk multiple times, resulting in a larger amount of data.
3. Therefore, in the case of a large number of writes to RAID-1, the write performance may become very poor (because we only have one south bridge).

advantage:
Since the data in the two disks are exactly the same, when any one of the hard disks is damaged, your data can still be completely preserved ! So we can say that the biggest advantage of RAID-1 is probably the backup of data! The read performance is ok, because there are two copies of the data on different disks. If multiple processes are reading the same data, the RAID will achieve the best read balance by itself.
shortcoming:
However, since half of the disk capacity is used for backup, the total capacity will be only half of the total disk capacity . RAID-1 has poor write performance.

RAID 0 + 1 , RAID 1 + 0

(1) Let two disks form RAID 0 first, and there are two groups in this configuration;
(2) Combine the two groups of RAID 0 into a group of RAID 1. This is RAID 0+1.
Conversely, RAID 1+0 means that RAID-1 is formed first and then RAID-0 is formed.

RAID5: Balanced Consideration of Performance and Data Backup
RAID-5 requires at least three or more disks to form this type of disk array. The data writing of this disk array is somewhat similar to RAID-0, but during the writing process of each cycle, a parity , which records the backup data of other disks for use. Rescue when a disk is damaged.


That is, assuming that the A0 data disappears, I can also calculate the value of A0 based on B0 and parity0.

Advantages and disadvantages:
In the comparison of read and write performance, the read performance is similar to that of RAID-0, but the write performance is average, because the data to be written to RAID 5 has to be calculated according to the parity. Due to the addition of this calculation action, the performance of writing has a great relationship with the hardware of the system.


Spare Disk: The function of spare disk

The so-called spare disk is one or more disks that are not included in the original disk array level. This disk is not usually used by the disk array. When any disk in the disk array is damaged, the spare disk will be used. Actively pull it into the disk array and move the broken hard disk out of the disk array! Then rebuild the data system immediately.




RAID array

Advantages of Disk Arrays
If your system needs a disk array, the focus is actually on:
Data security and reliability: It does not refer to information security, but to whether the data can be safely rescued or used when the hardware (referring to the disk) is damaged;
Read and write performance: For example, RAID 0 can enhance the read and write performance, so that your system I/O part can be improved;
Capacity: Multiple disks can be combined, so a single file system can have a considerable capacity.
In particular, the reliability and integrity of data is the key consideration when using RAID! After all, if the hardware is damaged and replaced, it is not a joke if the software data is damaged!

software disk array
The hardware disk array card is more expensive, and the function of using software to simulate the disk array is developed, which is the so-called software disk array (software RAID). At present, the computer CPU has strong performance, and the speed limit caused by insufficient processing power of the CPU in the past no longer exists!
The software disk array provided by CentOS is the software mdadm, which will use partition or disk as the disk unit
mdadm supports RAID0/RAID1/RAID5/spare disk, etc., and the management mechanism provided can also achieve functions similar to hot swap



Next demonstrate the creation of RAID0, RATD1, RAID5

(Eight hard disks are created here) RAID0 requires two hard disks, RAID1 requires two hard disks, and RAID5 requires four hard disks




fdisk -l to see . indeed both exist from sdc - sdj



Create RAID0
(Use sdc, sdd to demonstrate)

fdisk  /dev/sdc
fdisk  /dev/sdd




Modify partition type
The default type of new partition is Linux, codenamed 83, we need to modify it to raid type. Enter "t", then enter "L" to list all file formats, here we select "fdLinux raid auto", enter "fd", then enter "p" to view the partition status, this is the partition format has become Linux raid autodetect.




Type "w" to save the partition.


Set sdd in the same way


In the next operation, first fdisk -l /dev/sdc /dev/sdd   


Start creating RAID0



-C create; create an array
The device name of the /dev/md0 array
-a auto Agree to create the device. It is recommended to use the -ayes parameter to create a one-time
-l array mode The supported array modes are linear, raid0, raid1, raid4, raid5, raid6, raid10, multipath, faulty, container
-n --raid-devices The number of active disks in the array, the number plus the number of spare disks should be equal to the total number of disks in the array;
/dev/sd{c,d}1 The name of the disk involved in creating the array


View raid0 status




You also need to create a RAID profile. Otherwise it will fail at startup

Create /etc/mdadm.conf
# echo DEVICE /dev/sd{b,c}1 >> /etc/mdadm.conf
# mdadm–Ds >> /etc/mdadm.conf



Format the disk array first
mkfs.ext4  /dev/md0
Create a mount point and mount it
mkdir   /mnt/raid0
mount /dev/md0   /mnt/raid0


At this point, we can see that our /dev/md0 is 2.0G

As a final step, write to /etc/fstab. The purpose is that we can use our RAID device normally next time we boot up.





Create RAID1 (using /dev/sde,/dev/sdf)

In fact, the steps are similar to creating RAID0

1. Create a new partition and modify the partition type. fdisk /dev/sde fdisk /dev/sdf
2. Start creating RAID1 mdadm -C /dev/md1 -ayes -l1 -n2 /dev/sd[e,f]1
3. Check the status of the raid cat /proc/mdstat or mdadm -D /dev/md1
4. Add raid1 to the RAID configuration file /etc/mdadm.conf and modify   
 echo  DEVICE /dev/sd{e,f}1 >>/etc/mdadm.conf
mdadm -Ds >> /etc/mdadm.conf
5. Format the disk array mkfs.ext4 /dev/md1
6, create a mount point and mount it     
#mkdir/mnt/raid1
# mount /dev/md1 /mnt/raid1/
7. Write to /etc/fstab
8, reboot



Create RAID5

RAID5 requires at least three hard disks. We use four hard disks /dev/sdg, /dev/sdh, /dev/sdi, /dev/sdj for experiments, three are used as active disks, and the other is used as a hot spare disk .


1. Create a new partition and modify the partition type. fdisk /dev/sdg fdisk /dev/sdh fdisk /dev/sdi fdisk /dev/sdj
2. Start creating RAID5 mdadm -C /dev/md5 -ayes -l5 -n2 -x1 /dev/sd[g,h,i,j]1
3. Check the status of the raid cat /proc/mdstat or mdadm -D /dev/md5
4. Add raid5 to the RAID configuration file /etc/mdadm.conf and modify   
 echo  DEVICE /dev/sd{g,h,i,j}1 >>/etc/mdadm.conf
mdadm -Ds >> /etc/mdadm.conf (The content of md0 will be overwritten here, so it needs to be deleted manually, because we are in a virtual machine.)
5, format the disk array mkfs.ext4 /dev/md5
6, create a mount point and mount it     
#mkdir/mnt/raid5
# mount /dev/md5     /mnt/raid5/
7. Write to /etc/fstab
8,reboot


RAID maintenance

1, enable array
mdadm   -As   /dev/md0
-A enables an existing array
-s based on /etc/mdadm.conf
The mdadm.conf file is not created (or the file is incorrect), you can use the following methods to enable
#mdadm      –A   /dev/md0    /dev/sd[bc]1
mdadm-Ds
2. Stop the array
#mdadm   –S   /dev/md0
3. Display array details
#mdadm   –D   /dev/md0


Below we will simulate the disk damage in raid5 to explain the maintenance operation of soft RAID


1. Write test data to /raid5 to verify whether the data is lost after the disk is damaged:
   # touch test_raid5.failed# touch test_raid5.fail
   # ls


2, simulate disk damage     
         When RAID detects that a disk is faulty, it will automatically mark the disk as a faulty disk and stop reading and writing operations to the faulty disk.
We simulate /dev/sdh1 as a failed disk

#mdadm  /dev/md5  -f  /dev/sdh1



3. Check the rebuild status.
In the process of creating RAID 5 above, we set up a hot spare disk, so when there is a disk marked as failed, the hot spare disk will automatically replace the failed disk, and the array can also be rebuilt in a short time. The current array status can be seen by looking at the "/proc/mdstat" file.

Here, mine has been rebuilt due to the small files I created.



#mdadm -D /dev/md5


4. Check if the previous test data is still there


Data is normal, no loss

5. Remove the damaged disk


show it



Add hot spare disk




Adding storage hard disks to RAID

First, add a hard disk to the virtual machine. The specific operation is exactly the same as the previous steps.

Add a new hard disk to the RAID (at this time, the hard disk is used as a hot spare disk)


We need to add the hot spare disk to the active disk in the RAID.



Explanation: If it is -n5, then both sdh1 and sdk1 disks will be placed on the active disk. If you just want to add one, -n4 will do.



Then expand the file system.
After the RAID is built, the array capacity has increased, but the file system has not increased, then
We also need to expand the file system.
# df–TH
# resize2fs /dev/md5
# df–TH


Modify the RAID configuration file /etc/mdadm.conf

reboot






Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325818809&siteId=291194637