04-18-2019, 01:51 AM
Managing RAID arrays with mdadm
<div><p>Mdadm stands for <u>M</u>ultiple <u>D</u>isk and Device <u>Adm</u>inistration. It is a command line tool that can be used to manage software <a rel="noreferrer noopener" aria-label=" (opens in a new tab)" href="https://en.wikipedia.org/wiki/RAID" target="_blank">RAID</a> arrays on your Linux PC. This article outlines the basics you need to get started with it.</p>
<p> <span id="more-27034"></span> </p>
<p>The following five commands allow you to make use of mdadm’s most basic features:</p>
<ol>
<li><strong>Create a RAID array</strong>:<br />
<div class="codecolorer-container text default" style="overflow:auto;border:1px solid #9F9F9F;width:435px">
<div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace"># mdadm –create /dev/md/test –homehost=any –metadata=1.0 –level=1 –raid-devices=2 /dev/sda1 /dev/sdb1</div>
</div>
</li>
<li><strong>Assemble (and start) a RAID array</strong>:<br />
<div class="codecolorer-container text default" style="overflow:auto;border:1px solid #9F9F9F;width:435px">
<div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace"># mdadm –assemble /dev/md/test /dev/sda1 /dev/sdb1</div>
</div>
</li>
<li><strong>Stop a RAID array</strong>:<br />
<div class="codecolorer-container text default" style="overflow:auto;border:1px solid #9F9F9F;width:435px">
<div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace"># mdadm –stop /dev/md/test</div>
</div>
</li>
<li><strong>Delete a RAID array</strong>:<br />
<div class="codecolorer-container text default" style="overflow:auto;border:1px solid #9F9F9F;width:435px">
<div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace"># mdadm –zero-superblock /dev/sda1 /dev/sdb1</div>
</div>
</li>
<li><strong>Check the status of all assembled RAID arrays</strong>:<br />
<div class="codecolorer-container text default" style="overflow:auto;border:1px solid #9F9F9F;width:435px">
<div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace"># cat /proc/mdstat</div>
</div>
</li>
</ol>
<h2>Notes on features</h2>
<h3><tt>mdadm --create</tt></h3>
<p>The <em>create</em> command shown above includes the following four parameters in addition to the create parameter itself and the device names:</p>
<ol>
<li><strong>–homehost</strong>:<br />By default, mdadm stores your computer’s name as an attribute of the RAID array. If your computer name does not match the stored name, the array will not automatically assemble. This feature is useful in server clusters that share hard drives because file system corruption usually occurs if multiple servers attempt to access the same drive at the same time. The name <em>any</em> is reserved and disables the <em>homehost</em> restriction.</li>
<li><strong>–metadata</strong>:<br /><em>mdadm</em> reserves a small portion of each RAID device to store information about the RAID array itself. The <em>metadata</em> parameter specifies the format and location of the information. The value <em>1.0</em> indicates to use version-1 formatting and store the metadata at the end of the device.</li>
<li><strong>–level</strong>:<br />The <em>level</em> parameter specifies how the data should be distributed among the underlying devices. Level <em>1</em> indicates each device should contain a complete copy of all the data. This level is also known as <a rel="noreferrer noopener" aria-label=" (opens in a new tab)" href="https://en.wikipedia.org/wiki/Disk_mirroring" target="_blank">disk mirroring</a>.</li>
<li><strong>–raid-devices</strong>:<br />The <em>raid-devices</em> parameter specifies the number of devices that will be used to create the RAID array.</li>
</ol>
<p>By using <em>level=1</em> (mirroring) in combination with <em>metadata=1.0</em> (store the metadata at the end of the device), you create a RAID1 array whose underlying devices appear normal if accessed without the aid of the mdadm driver. This is useful in the case of disaster recovery, because you can access the device even if the new system doesn’t support mdadm arrays. It’s also useful in case a program needs <em>read-only</em> access to the underlying device before mdadm is available. For example, the <a rel="noreferrer noopener" aria-label=" (opens in a new tab)" href="https://en.wikipedia.org/wiki/Unified_Extensible_Firmware_Interface" target="_blank">UEFI</a> firmware in a computer may need to read the bootloader from the <a rel="noreferrer noopener" aria-label=" (opens in a new tab)" href="https://en.wikipedia.org/wiki/EFI_system_partition" target="_blank">ESP</a> before mdadm is started.</p>
<h3><tt>mdadm --assemble</tt></h3>
<p>The <em>assemble</em> command above fails if a member device is missing or corrupt. To force the RAID array to assemble and start when one of its members is missing, use the following command:</p>
<pre class="wp-block-preformatted"># mdadm --assemble --run /dev/md/test /dev/sda1</pre>
<h2>Other important notes</h2>
<p>Avoid writing directly to any devices that underlay a mdadm RAID1 array. That causes the devices to become out-of-sync and mdadm won’t know that they are out-of-sync. If you access a RAID1 array with a device that’s been modified out-of-band, you can cause file system corruption. If you modify a RAID1 device out-of-band and need to force the array to re-synchronize, delete the mdadm metadata from the device to be overwritten and then re-add it to the array as demonstrated below:</p>
<pre class="wp-block-preformatted"># mdadm --zero-superblock /dev/sdb1<br /># mdadm --assemble --run /dev/md/test /dev/sda1 <br /># mdadm /dev/md/test --add /dev/sdb1</pre>
<p>These commands completely overwrite the contents of sdb1 with the contents of sda1.</p>
<p>To specify any RAID arrays to automatically activate when your computer starts, create an <em>/etc/mdadm.conf</em> configuration file.</p>
<p>For the most up-to-date and detailed information, check the man pages:</p>
<pre class="wp-block-preformatted">$ man mdadm <br />$ man mdadm.conf</pre>
<p>The next article of this series will show a step-by-step guide on how to convert an existing single-disk Linux installation to a mirrored-disk installation, that will continue running even if one of its hard drives suddenly stops working!</p>
</div>
<div><p>Mdadm stands for <u>M</u>ultiple <u>D</u>isk and Device <u>Adm</u>inistration. It is a command line tool that can be used to manage software <a rel="noreferrer noopener" aria-label=" (opens in a new tab)" href="https://en.wikipedia.org/wiki/RAID" target="_blank">RAID</a> arrays on your Linux PC. This article outlines the basics you need to get started with it.</p>
<p> <span id="more-27034"></span> </p>
<p>The following five commands allow you to make use of mdadm’s most basic features:</p>
<ol>
<li><strong>Create a RAID array</strong>:<br />
<div class="codecolorer-container text default" style="overflow:auto;border:1px solid #9F9F9F;width:435px">
<div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace"># mdadm –create /dev/md/test –homehost=any –metadata=1.0 –level=1 –raid-devices=2 /dev/sda1 /dev/sdb1</div>
</div>
</li>
<li><strong>Assemble (and start) a RAID array</strong>:<br />
<div class="codecolorer-container text default" style="overflow:auto;border:1px solid #9F9F9F;width:435px">
<div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace"># mdadm –assemble /dev/md/test /dev/sda1 /dev/sdb1</div>
</div>
</li>
<li><strong>Stop a RAID array</strong>:<br />
<div class="codecolorer-container text default" style="overflow:auto;border:1px solid #9F9F9F;width:435px">
<div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace"># mdadm –stop /dev/md/test</div>
</div>
</li>
<li><strong>Delete a RAID array</strong>:<br />
<div class="codecolorer-container text default" style="overflow:auto;border:1px solid #9F9F9F;width:435px">
<div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace"># mdadm –zero-superblock /dev/sda1 /dev/sdb1</div>
</div>
</li>
<li><strong>Check the status of all assembled RAID arrays</strong>:<br />
<div class="codecolorer-container text default" style="overflow:auto;border:1px solid #9F9F9F;width:435px">
<div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace"># cat /proc/mdstat</div>
</div>
</li>
</ol>
<h2>Notes on features</h2>
<h3><tt>mdadm --create</tt></h3>
<p>The <em>create</em> command shown above includes the following four parameters in addition to the create parameter itself and the device names:</p>
<ol>
<li><strong>–homehost</strong>:<br />By default, mdadm stores your computer’s name as an attribute of the RAID array. If your computer name does not match the stored name, the array will not automatically assemble. This feature is useful in server clusters that share hard drives because file system corruption usually occurs if multiple servers attempt to access the same drive at the same time. The name <em>any</em> is reserved and disables the <em>homehost</em> restriction.</li>
<li><strong>–metadata</strong>:<br /><em>mdadm</em> reserves a small portion of each RAID device to store information about the RAID array itself. The <em>metadata</em> parameter specifies the format and location of the information. The value <em>1.0</em> indicates to use version-1 formatting and store the metadata at the end of the device.</li>
<li><strong>–level</strong>:<br />The <em>level</em> parameter specifies how the data should be distributed among the underlying devices. Level <em>1</em> indicates each device should contain a complete copy of all the data. This level is also known as <a rel="noreferrer noopener" aria-label=" (opens in a new tab)" href="https://en.wikipedia.org/wiki/Disk_mirroring" target="_blank">disk mirroring</a>.</li>
<li><strong>–raid-devices</strong>:<br />The <em>raid-devices</em> parameter specifies the number of devices that will be used to create the RAID array.</li>
</ol>
<p>By using <em>level=1</em> (mirroring) in combination with <em>metadata=1.0</em> (store the metadata at the end of the device), you create a RAID1 array whose underlying devices appear normal if accessed without the aid of the mdadm driver. This is useful in the case of disaster recovery, because you can access the device even if the new system doesn’t support mdadm arrays. It’s also useful in case a program needs <em>read-only</em> access to the underlying device before mdadm is available. For example, the <a rel="noreferrer noopener" aria-label=" (opens in a new tab)" href="https://en.wikipedia.org/wiki/Unified_Extensible_Firmware_Interface" target="_blank">UEFI</a> firmware in a computer may need to read the bootloader from the <a rel="noreferrer noopener" aria-label=" (opens in a new tab)" href="https://en.wikipedia.org/wiki/EFI_system_partition" target="_blank">ESP</a> before mdadm is started.</p>
<h3><tt>mdadm --assemble</tt></h3>
<p>The <em>assemble</em> command above fails if a member device is missing or corrupt. To force the RAID array to assemble and start when one of its members is missing, use the following command:</p>
<pre class="wp-block-preformatted"># mdadm --assemble --run /dev/md/test /dev/sda1</pre>
<h2>Other important notes</h2>
<p>Avoid writing directly to any devices that underlay a mdadm RAID1 array. That causes the devices to become out-of-sync and mdadm won’t know that they are out-of-sync. If you access a RAID1 array with a device that’s been modified out-of-band, you can cause file system corruption. If you modify a RAID1 device out-of-band and need to force the array to re-synchronize, delete the mdadm metadata from the device to be overwritten and then re-add it to the array as demonstrated below:</p>
<pre class="wp-block-preformatted"># mdadm --zero-superblock /dev/sdb1<br /># mdadm --assemble --run /dev/md/test /dev/sda1 <br /># mdadm /dev/md/test --add /dev/sdb1</pre>
<p>These commands completely overwrite the contents of sdb1 with the contents of sda1.</p>
<p>To specify any RAID arrays to automatically activate when your computer starts, create an <em>/etc/mdadm.conf</em> configuration file.</p>
<p>For the most up-to-date and detailed information, check the man pages:</p>
<pre class="wp-block-preformatted">$ man mdadm <br />$ man mdadm.conf</pre>
<p>The next article of this series will show a step-by-step guide on how to convert an existing single-disk Linux installation to a mirrored-disk installation, that will continue running even if one of its hard drives suddenly stops working!</p>
</div>