{"id":373,"date":"2018-06-24T18:32:47","date_gmt":"2018-06-24T16:32:47","guid":{"rendered":"http:\/\/hentati.org\/?p=373"},"modified":"2020-12-06T20:45:02","modified_gmt":"2020-12-06T19:45:02","slug":"373","status":"publish","type":"post","link":"https:\/\/hentati.org\/index.php\/2018\/06\/24\/373\/","title":{"rendered":"Linux RAID Software : Set Up RAID 1"},"content":{"rendered":"<p><span style=\"color: #000000;\">In this tutorial, we\u2019ll be talking about\u00a0<strong>RAID<\/strong>, specifically we will set up\u00a0<strong>software RAID 1<\/strong>\u00a0on a running Linux distribution.<\/span><\/p>\n<h2><span style=\"color: #000000;\">What is RAID?<\/span><\/h2>\n<p><span style=\"color: #000000;\">RAID stands for\u00a0<strong>R<\/strong>edundant\u00a0<strong>A<\/strong>rray of\u00a0<strong>I<\/strong>nexpensive\u00a0<strong>D<\/strong>isks. RAID allows you to turn multiple physical hard drives into a single logical hard drive. There are many RAID levels such as RAID 0, RAID 1, RAID 5, RAID 10 etc.<\/span><\/p>\n<p><span style=\"color: #000000;\">Here we will discuss about\u00a0<strong>RAID 1<\/strong>\u00a0which is also known as\u00a0<strong>disk mirroring<\/strong>. RAID 1 creates identical copies of data. If you have two hard drives in RAID 1, then data will be written to both drives. The two hard drives have the same data.<\/span><\/p>\n<p><span style=\"color: #000000;\">The nice part about RAID 1 is that if one of your hard drive fails, your computer or server would still be up and running because you have a complete, intact copy of the data on the other hard drive. You can pull the failed hard drive out while the computer is running, insert a new hard drive and it will automatically rebuilds the mirror.<\/span><\/p>\n<p><span style=\"color: #000000;\">The downside of RAID 1 is that you don\u2019t get any extra disk space. If your two hard drives are both 1TB, then the total usable volume is 1TB instead of 2TB.<\/span><\/p>\n<h2><span style=\"color: #000000;\">Hardware RAID vs Software RAID<\/span><\/h2>\n<p><span style=\"color: #000000;\">To set up RAID, you can either use a hard drive controller, or use a piece of software to create it. A hard drive controller is a PCIe card that you put into a computer. Then you connect your hard drives to this card. When you boot up the computer, you are going to see an option that allows you to configure the RAID. You can install an operating system on top of hardware RAID which can increase uptime.<\/span><\/p>\n<p><span style=\"color: #000000;\">Software RAID requires you already installed an operating system. It\u2019s good for storing data.<\/span><\/p>\n<h2><span style=\"color: #000000;\">Basic Steps to Create Software RAID 1 on Linux<\/span><\/h2>\n<ul>\n<li><span style=\"color: #000000;\">First you need to have a Linux distribution installed on your hard drive. In this tutorial we will name it\u00a0<code>\/dev\/sda<\/code>.<\/span><\/li>\n<li><span style=\"color: #000000;\">Then you are going to grab two hard drives which will be named\u00a0<code>\/dev\/sdb<\/code>\u00a0and\u00a0<code>\/dev\/sdc<\/code>\u00a0in this post. These two hard drives can be of different sizes. Remember to back up your existing data before formating your hard drives.<\/span><\/li>\n<li><span style=\"color: #000000;\">Next, we will create special file systems on\u00a0<code>\/dev\/sdb<\/code>\u00a0and\u00a0<code>\/dev\/sdc<\/code>.<\/span><\/li>\n<li><span style=\"color: #000000;\">And finally create the\u00a0<strong>RAID 1 array<\/strong>\u00a0using the\u00a0<code>mdadm<\/code>\u00a0utility.<\/span><\/li>\n<\/ul>\n<h2><span style=\"color: #000000;\">Step 1: Format Hard Drive<\/span><\/h2>\n<p><span style=\"color: #000000;\">Insert two hard drives into your Linux computer, then open up a terminal window. Run the following command to check the device name.<\/span><\/p>\n<pre><span style=\"color: #000000;\">sudo fdisk -l<\/span><\/pre>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #000000;\">You can see mine is\u00a0<code>\/dev\/sdb<\/code>\u00a0and\u00a0<code>\/dev\/sdc<\/code>.<\/span><\/p>\n<p><span style=\"color: #000000;\">Then run the following 2 commands to make new MBR partition table on the two hard drives. (Note: this is going to wipe out all existing partitions and data from these two hard drives. Make sure your data is backed up.)<\/span><\/p>\n<pre><span style=\"color: #000000;\">sudo parted \/dev\/sdb mklabel msdos\r\n\r\nsudo parted \/dev\/sdc mklabel msdos<\/span><\/pre>\n<p><span style=\"color: #000000;\">You can create\u00a0GPT partition table\u00a0by replacing\u00a0<code>msdos<\/code>\u00a0with\u00a0<code>gpt<\/code>, but for the sake of compatibility, this tutorial will create MBR partition table.<\/span><\/p>\n<p><span style=\"color: #000000;\">Next, use the\u00a0<code>fdisk<\/code>\u00a0command to create a new partition on each drive and format them as a\u00a0<strong>Linux raid autodetect<\/strong>\u00a0file system. First do this on\u00a0<code>\/dev\/sdb<\/code>.<\/span><\/p>\n<pre><span style=\"color: #000000;\">sudo fdisk \/dev\/sdb<\/span><\/pre>\n<p><span style=\"color: #000000;\">Follow these instructions.<\/span><\/p>\n<ol>\n<li><span style=\"color: #000000;\">Type\u00a0<strong>n<\/strong>\u00a0to create a new partition.<\/span><\/li>\n<li><span style=\"color: #000000;\">Type\u00a0<strong>p<\/strong>\u00a0to select primary partition.<\/span><\/li>\n<li><span style=\"color: #000000;\">Type\u00a0<strong>1<\/strong>\u00a0to create \/dev\/sdb1.<\/span><\/li>\n<li><span style=\"color: #000000;\"><strong>Press Enter<\/strong>\u00a0to choose the default first sector<\/span><\/li>\n<li><span style=\"color: #000000;\"><strong>Press Enter<\/strong>\u00a0to choose the default last sector. This partition will span across the entire drive.<\/span><\/li>\n<li><span style=\"color: #000000;\">Typing\u00a0<strong>p<\/strong>\u00a0will print information about the newly created partition. By default the partition type is Linux.<\/span><\/li>\n<li><span style=\"color: #000000;\">We need to change the partition type, so type\u00a0<strong>t<\/strong>.<\/span><\/li>\n<li><span style=\"color: #000000;\">Enter\u00a0<strong>fd<\/strong>\u00a0to set partition type to\u00a0<code>Linux raid autodetect<\/code>.<\/span><\/li>\n<li><span style=\"color: #000000;\">Type<strong>\u00a0p<\/strong>\u00a0again to check the partition type.<\/span><\/li>\n<li><span style=\"color: #000000;\">Type\u00a0<strong>w<\/strong>\u00a0to apply the above changes.<\/span><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #000000;\">Follow the same instruction to create a Linux raid autodetect partition on\u00a0<code>\/dev\/sdc<\/code>.<\/span><\/p>\n<p><span style=\"color: #000000;\">Now we have two raid devices\u00a0<code>\/dev\/sdb1<\/code>\u00a0and\u00a0<code>\/dev\/sdc1<\/code>.<\/span><\/p>\n<h2><span style=\"color: #000000;\">Step 2: Install mdadm<\/span><\/h2>\n<p><span style=\"color: #000000;\"><code>mdadm<\/code>\u00a0is used for managing MD (multiple devices) devices, also known as Linux software RAID.<\/span><\/p>\n<pre><span style=\"color: #000000;\">Debian\/Ubuntu: \u00a0 \u00a0 sudo apt install mdadm\r\n\r\nCentOS\/Redhat: \u00a0 \u00a0 sudo yum install mdadm\r\n\r\nSUSE: \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0sudo zypper install mdadm\r\n\r\nArch Linux \u00a0 \u00a0 \u00a0 \u00a0 sudo pacman -S mdadm<\/span><\/pre>\n<p><span style=\"color: #000000;\">Let\u2019s examine the two devices.<\/span><\/p>\n<pre><span style=\"color: #000000;\">sudo mdadm --examine \/dev\/sdb \/dev\/sdc<\/span><\/pre>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #000000;\">You can see that both are the type\u00a0<strong>fd<\/strong>\u00a0(Linux raid autodetect). At this stage, there\u2019s no RAID setup on\u00a0<code>\/dev\/sdb1<\/code>\u00a0and\u00a0<code>\/dev\/sdc1<\/code>\u00a0which can be inferred with this command.<\/span><\/p>\n<pre><span style=\"color: #000000;\">sudo mdadm --examine \/dev\/sdb1 \/dev\/sdc1<\/span><\/pre>\n<p>&nbsp;<\/p>\n<h2><span style=\"color: #000000;\">Step 3: Create RAID 1 Logical Drive<\/span><\/h2>\n<p><span style=\"color: #000000;\">Execute the following command to create RAID 1. The logical drive will be named\u00a0<code>\/dev\/md0<\/code>.<\/span><\/p>\n<pre><span style=\"color: #000000;\">sudo mdadm --create \/dev\/md0 --level=mirror --raid-devices=2 \/dev\/sdb1 \/dev\/sdc1<\/span><\/pre>\n<p><span style=\"color: #000000;\">Now we can check it with:<\/span><\/p>\n<pre><span style=\"color: #000000;\">cat \/proc\/mdstat<\/span><\/pre>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #000000;\">You can see that md0 is active and is a RAID 1 setup. To get more detailed information about\u00a0<code>\/dev\/md0<\/code>, we can use the below commands:<\/span><\/p>\n<pre><span style=\"color: #000000;\">sudo mdadm --detail \/dev\/md0<\/span><\/pre>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #000000;\">To obtain detailed information about each raid device, run this command:<\/span><\/p>\n<pre><span style=\"color: #000000;\">sudo mdadm --examine \/dev\/sdb1 \/dev\/sdc1<\/span><\/pre>\n<h2><span style=\"color: #000000;\">Step 4: Create File System on the RAID 1 Logical Drive<\/span><\/h2>\n<p><span style=\"color: #000000;\">Let\u2019s format it to ext4 file system.<\/span><\/p>\n<pre><span style=\"color: #000000;\">sudo mkfs.ext4 \/dev\/md0<\/span><\/pre>\n<p><span style=\"color: #000000;\">Then create a mount point\u00a0<code>\/mnt\/raid1<\/code>\u00a0and mount the RAID 1 drive.<\/span><\/p>\n<pre><span style=\"color: #000000;\">sudo mkdir \/mnt\/raid1\r\n\r\nsudo mount \/dev\/md0 \/mnt\/raid1<\/span><\/pre>\n<p><span style=\"color: #000000;\">You can use this command to check how much disk space you have.<\/span><\/p>\n<pre><span style=\"color: #000000;\">df -h \/mnt\/raid1<\/span><\/pre>\n<p>&nbsp;<\/p>\n<h2><span style=\"color: #000000;\">Step 5: Test<\/span><\/h2>\n<p><span style=\"color: #000000;\">Now let\u2019s go to\u00a0<code>\/mnt\/raid1<\/code>\u00a0and create a text file.<\/span><\/p>\n<pre><span style=\"color: #000000;\">cd \/mnt\/raid1\r\n\r\nsudo nano raid.txt<\/span><\/pre>\n<p><span style=\"color: #000000;\">Write something like<\/span><\/p>\n<pre><span style=\"color: #000000;\">This is raid 1 device.<\/span><\/pre>\n<p><span style=\"color: #000000;\">Save and close the file. Next, remove one of your drive out from your computer and check the status RAID 1 device again.<\/span><\/p>\n<pre><span style=\"color: #000000;\">sudo mdadm --examine \/dev\/sdb1 \/dev\/sdc1<\/span><\/pre>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #000000;\">You can see that\u00a0<code>\/dev\/sdc1<\/code>\u00a0is not available. If we check\u00a0<code>\/dev\/md0<\/code>, we can see that one RAID device is removed.<\/span><\/p>\n<pre><span style=\"color: #000000;\">sudo mdadm --detail \/dev\/md0<\/span><\/pre>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #000000;\">However, the text file is still there.<\/span><\/p>\n<pre><span style=\"color: #000000;\">cat \/mnt\/raid1\/raid1.txt<\/span><\/pre>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #000000;\">To add the failed drive (in this case\u00a0<code>\/dev\/sdc1<\/code>) back to the RAID, run the following command.<\/span><\/p>\n<pre><span style=\"color: #000000;\">sudo mdadm --manage \/dev\/md0 --add \/dev\/sdc1<\/span><\/pre>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #000000;\">It\u2019s very important to save our RAID1 configuration with the below command.<\/span><\/p>\n<pre><span style=\"color: #000000;\">sudo mdadm --detail --scan --verbose | sudo tee \/etc\/mdadm\/mdadm.conf<\/span><\/pre>\n<p><span style=\"color: #000000;\">Output:<\/span><\/p>\n<pre><span style=\"color: #000000;\">ARRAY \/dev\/md\/0 level=raid1 num-devices=2 metadata=1.2 spares=1 name=xenial:0 UUID=c7a2743d:f1e0d872:b2ad29cd:e2bee48c\r\n\u00a0 \u00a0 \u00a0 devices=\/dev\/sdb1,\/dev\/sdc1<\/span><\/pre>\n<p><span style=\"color: #000000;\">On some Linux distribution such as CentOS, the config file for\u00a0<code>mdadm<\/code>\u00a0is\u00a0<code>\/etc\/mdadm.conf<\/code>.<\/span><\/p>\n<p><span style=\"color: #000000;\">To automatically mount the RAID 1 logical drive on boot time, add an entry in\u00a0<code>\/etc\/fstab<\/code>\u00a0file like below.<\/span><\/p>\n<pre><span style=\"color: #000000;\">\/dev\/md0  \/mnt\/raid1  ext4  defaults  0  0<\/span><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, we\u2019ll be talking about\u00a0RAID, specifically we will set up\u00a0software RAID 1\u00a0on a running Linux distribution. What is RAID? RAID stands for\u00a0Redundant\u00a0Array of\u00a0Inexpensive\u00a0Disks. RAID allows you to turn &#8230;<\/p>\n","protected":false},"author":1,"featured_media":378,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[8],"tags":[],"_links":{"self":[{"href":"https:\/\/hentati.org\/index.php\/wp-json\/wp\/v2\/posts\/373"}],"collection":[{"href":"https:\/\/hentati.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hentati.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hentati.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/hentati.org\/index.php\/wp-json\/wp\/v2\/comments?post=373"}],"version-history":[{"count":8,"href":"https:\/\/hentati.org\/index.php\/wp-json\/wp\/v2\/posts\/373\/revisions"}],"predecessor-version":[{"id":646,"href":"https:\/\/hentati.org\/index.php\/wp-json\/wp\/v2\/posts\/373\/revisions\/646"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/hentati.org\/index.php\/wp-json\/wp\/v2\/media\/378"}],"wp:attachment":[{"href":"https:\/\/hentati.org\/index.php\/wp-json\/wp\/v2\/media?parent=373"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hentati.org\/index.php\/wp-json\/wp\/v2\/categories?post=373"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hentati.org\/index.php\/wp-json\/wp\/v2\/tags?post=373"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}