Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Fedora - Mirror your System Drive using Software RAID

#1
Mirror your System Drive using Software RAID

<div><p>Nothing lasts forever. When it comes to the hardware in your PC, most of it can easily be replaced. There is, however, one special-case hardware component in your PC that is not as easy to replace as the rest — your hard disk drive.</p>
<p> <span id="more-25485"></span> </p>
<h2>Drive Mirroring</h2>
<p>Your hard drive stores your personal data. Some of your data can be backed up automatically by scheduled backup jobs. But those jobs scan the files to be backed up for changes and trying to scan an entire drive would be very resource intensive. Also, anything that you’ve changed since your last backup will be lost if your drive fails. <a href="https://en.wikipedia.org/wiki/Disk_mirroring" target="_blank" rel="noreferrer noopener" aria-label=" (opens in a new tab)">Drive mirroring</a> is a better way to maintain a secondary copy of your entire hard drive. With drive mirroring, a secondary copy of <em>all the data</em> on your hard drive is maintained <em>in real time</em>.</p>
<p>An added benefit of live mirroring your hard drive to a secondary hard drive is that it can <a href="https://en.wikipedia.org/wiki/Disk_mirroring#Additional_benefits" target="_blank" rel="noreferrer noopener" aria-label=" (opens in a new tab)">increase your computer’s performance</a>. Because disk I/O is one of your computer’s main performance <a rel="noreferrer noopener" aria-label=" (opens in a new tab)" href="https://en.wikipedia.org/wiki/Bottleneck_(software)" target="_blank">bottlenecks</a>, the performance improvement can be quite significant.</p>
<p>Note that a mirror is not a backup. It only protects your data from being lost if one of your physical drives fail. Types of failures that drive mirroring, by itself, does not protect against include:</p>
<ul>
<li><a rel="noreferrer noopener" aria-label=" (opens in a new tab)" href="https://en.wikipedia.org/wiki/Data_corruption" target="_blank">File System Corruption</a></li>
<li><a href="https://en.wikipedia.org/wiki/Data_degradation" target="_blank" rel="noreferrer noopener" aria-label=" (opens in a new tab)">Bit Rot</a></li>
<li>Accidental File Deletion</li>
<li>Simultaneous Failure of all Mirrored Drives (highly unlikely)</li>
</ul>
<p>Some of the above can be addressed by other file system features that can be used in conjunction with drive mirroring. File system features that address the above types of failures include:</p>
<ul>
<li>Using a <a rel="noreferrer noopener" aria-label=" (opens in a new tab)" href="https://en.wikipedia.org/wiki/Journaling_file_system" target="_blank">Journaling</a> or <a rel="noreferrer noopener" aria-label=" (opens in a new tab)" href="https://www.quora.com/What-is-the-difference-between-a-journaling-vs-a-log-structured-file-system" target="_blank">Log-Structured</a> file system</li>
<li>Using <a rel="noreferrer noopener" aria-label=" (opens in a new tab)" href="https://en.wikipedia.org/wiki/File_verification" target="_blank">Checksums</a> (<a rel="noreferrer noopener" aria-label=" (opens in a new tab)" href="https://en.wikipedia.org/wiki/ZFS#Summary_of_key_differentiating_features" target="_blank">ZFS</a> , for example, does this automatically and transparently)</li>
<li>Using <a rel="noreferrer noopener" aria-label=" (opens in a new tab)" href="https://en.wikipedia.org/wiki/Snapshot_(computer_storage)#File_systems" target="_blank">Snapshots</a></li>
<li>Using <a href="https://en.wikipedia.org/wiki/Business_continuance_volume" target="_blank" rel="noreferrer noopener" aria-label=" (opens in a new tab)">BCVs</a></li>
</ul>
<p>This guide will demonstrate one method of mirroring your system drive using the Multiple Disk and Device Administration (mdadm) toolset. Just for fun, this guide will show how to do the conversion without using any extra boot media (CDs, USB drives, etc). For more about the concepts and terminology related to the multiple device driver, you can skim the <em>md</em> man page:</p>
<pre class="wp-block-preformatted">$ man md</pre>
<h2>The Procedure</h2>
<ol>
<li><strong>Use </strong><a href="https://fedoramagazine.org/managing-partitions-with-sgdisk/" target="_blank" rel="noreferrer noopener" aria-label=" (opens in a new tab)"><strong>sgdisk</strong></a><strong> to (re)partition the <u><em>extra</em></u> drive that you have added to your computer</strong>:
<pre>$ sudo -i
# MY_DISK_1=/dev/sdb
# sgdisk --zap-all $MY_DISK_1
# test -d /sys/firmware/efi/efivars || sgdisk -n 0:0:+1MiB -t 0:ef02 -c 0:grub_1 $MY_DISK_1
# sgdisk -n 0:0:+1GiB -t 0:ea00 -c 0:boot_1 $MY_DISK_1
# sgdisk -n 0:0:+4GiB -t 0:fd00 -c 0Confusedwap_1 $MY_DISK_1
# sgdisk -n 0:0:0 -t 0:fd00 -c 0:root_1 $MY_DISK_1</pre>
<p>– If the drive that you will be using for the second half of the mirror in step 12 is smaller than this drive, then you will need to adjust down the size of the last partition so that the total size of all the partitions is not greater than the size of your second drive.<br />– A few of the commands in this guide are prefixed with a test for the existence of an <em>efivars</em> directory. This is necessary because those commands are slightly different depending on whether your computer is BIOS-based or UEFI-based.</p>
<p></li>
<li><strong>Use </strong><a href="https://fedoramagazine.org/managing-raid-arrays-with-mdadm/" target="_blank" rel="noreferrer noopener" aria-label=" (opens in a new tab)"><strong>mdadm</strong></a><strong> to create RAID devices that use the new partitions to store their data</strong>:
<pre># mdadm --create /dev/md/boot --homehost=any --metadata=1.0 --level=1 --raid-devices=2 /dev/disk/by-partlabel/boot_1 missing
# mdadm --create /dev/md/swap --homehost=any --metadata=1.0 --level=1 --raid-devices=2 /dev/disk/by-partlabel/swap_1 missing
# mdadm --create /dev/md/root --homehost=any --metadata=1.0 --level=1 --raid-devices=2 /dev/disk/by-partlabel/root_1 missing</pre>
<pre># cat &lt;&lt; END &gt; /etc/mdadm.conf
MAILADDR root
AUTO +all
DEVICE partitions
END</pre>
<pre># mdadm --detail --scan &gt;&gt; /etc/mdadm.conf</pre>
<p>– The <em>missing</em> parameter tells mdadm to create an array with a missing member. You will add the other half of the mirror in step 14.<br />– You should configure <a href="https://fedoraproject.org/wiki/QA:Testcase_Sendmail" target="_blank" rel="noreferrer noopener" aria-label=" (opens in a new tab)">sendmail</a> so you will be notified if a drive fails.<br />– You can configure <a href="https://en.wikipedia.org/wiki/Evolution_(software)" target="_blank" rel="noreferrer noopener" aria-label=" (opens in a new tab)">Evolution</a> to <a href="https://dotancohen.com/howto/root_email.html" target="_blank" rel="noreferrer noopener" aria-label=" (opens in a new tab)">monitor a local mail spool</a>.</p>
<p></li>
<li><strong>Use </strong><a href="https://fedoramagazine.org/initramfs-dracut-and-the-dracut-emergency-shell/" target="_blank" rel="noreferrer noopener" aria-label=" (opens in a new tab)"><strong>dracut</strong></a><strong> to update the initramfs</strong>:
<pre># dracut -f --add mdraid --add-drivers xfs</pre>
<p>– Dracut will include the /etc/mdadm.conf file you created in the previous section in your initramfs <em>unless</em> you build your initramfs with the <em>hostonly</em> option set to <em>no</em>. If you build your initramfs with the hostonly option set to no, then you should either manually include the /etc/mdadm.conf file, manually specify the UUID’s of the RAID arrays to assemble at boot time with the <em>rd.md.uuid</em> kernel parameter, or specify the <em>rd.auto</em> kernel parameter to have all RAID arrays automatically assembled and started at boot time. This guide will demonstrate the <em>rd.auto</em> option since it is the most generic.</p>
<p></li>
<li><strong>Format the RAID devices</strong>:
<pre># mkfs -t vfat /dev/md/boot
# mkswap /dev/md/swap
# mkfs -t xfs /dev/md/root</pre>
<p>– The new <a href="https://systemd.io/BOOT_LOADER_SPECIFICATION#technical-details" target="_blank" rel="noreferrer noopener" aria-label=" (opens in a new tab)">Boot Loader Specification</a> states “if the OS is installed on a disk with GPT disk label, and no ESP partition exists yet, a new suitably sized (let’s say 500MB) ESP should be created and should be used as $BOOT” and “$BOOT must be a VFAT (16 or 32) file system”.</p>
<p></li>
<li><strong>Reboot and set the <u><em>rd.auto</em></u>, <u><em>rd.break</em></u> and <u><em>single</em></u> kernel parameters</strong>:
<pre># reboot</pre>
<p>– You may need to <a href="https://docs.fedoraproject.org/en-US/Fedora/26/html/System_Administrators_Guide/sec-Changing_and_Resetting_the_Root_Password.html" target="_blank" rel="noreferrer noopener" aria-label=" (opens in a new tab)">set your root password</a> before rebooting so that you can get into <em>single-user mode</em> in step 7.<br />– See “<a href="https://docs.fedoraproject.org/en-US/fedora/rawhide/system-administrators-guide/kernel-module-driver-configuration/Working_with_the_GRUB_2_Boot_Loader/#sec-Making_Temporary_Changes_to_a_GRUB_2_Menu" target="_blank" rel="noreferrer noopener" aria-label=" (opens in a new tab)">Making Temporary Changes to a GRUB 2 Menu</a>” for directions on how to set kernel parameters on compters that use the GRUB 2 boot loader.</p>
<p></li>
<li><strong>Use </strong><a href="https://fedoramagazine.org/initramfs-dracut-and-the-dracut-emergency-shell/" target="_blank" rel="noreferrer noopener" aria-label=" (opens in a new tab)"><strong>the dracut shell</strong></a><strong> to copy the root file system</strong>:
<pre># mkdir /newroot
# mount /dev/md/root /newroot
# shopt -s dotglob
# cp -ax /sysroot/* /newroot
# rm -rf /newroot/boot/*
# umount /newroot
# exit</pre>
<p>– The <em>dotglob</em> flag is set for this bash session so that the <a href="https://en.wikipedia.org/wiki/Wildcard_character#File_and_directory_patterns" target="_blank" rel="noreferrer noopener" aria-label=" (opens in a new tab)">wildcard character</a> will match hidden files.<br />– Files are removed from the <em>boot</em> directory because they will be copied to a separate partition in the next step.<br />– This copy operation is being done from the dracut shell to insure that no processes are accessing the files while they are being copied.</p>
<p></li>
<li><strong>Use <u><em>single-user mode</em></u> to copy the non-root file systems</strong>:
<pre># mkdir /newroot
# mount /dev/md/root /newroot
# mount /dev/md/boot /newroot/boot
# shopt -s dotglob
# cp -Lr /boot/* /newroot/boot
# test -d /newroot/boot/efi/EFI &amp;&amp; mv /newroot/boot/efi/EFI/* /newroot/boot/efi &amp;&amp; rmdir /newroot/boot/efi/EFI
# test -d /sys/firmware/efi/efivars &amp;&amp; ln -sfr /newroot/boot/efi/fedora/grub.cfg /newroot/etc/grub2-efi.cfg
# cp -ax /home/* /newroot/home
# exit</pre>
<p>– It is OK to run these commands in the dracut shell shown in the previous section instead of doing it from single-user mode. I’ve demonstrated using single-user mode to avoid having to explain how to mount the non-root partitions from the dracut shell.<br />– The parameters being past to the <em>cp</em> command for the <em>boot</em> directory are a little different because the VFAT file system doesn’t support symbolic links or Unix-style file permissions.<br />– In rare cases, the <em>rd.auto</em> parameter is known to cause LVM to fail to assemble due to a <a href="https://en.wikipedia.org/wiki/Race_condition" target="_blank" rel="noreferrer noopener" aria-label=" (opens in a new tab)">race condition</a>. If you see errors about your <em>swap</em> or <em>home</em> partition failing to mount when entering single-user mode, simply try again by repeating step 5 but omiting the <em>rd.break</em> paramenter so that you will go directly to single-user mode.</p>
<p></li>
<li><strong>Update <u><em>fstab</em></u> on the new drive</strong>:
<pre># cat &lt;&lt; END &gt; /newroot/etc/fstab
/dev/md/root / xfs defaults 0 0
/dev/md/boot /boot vfat defaults 0 0
/dev/md/swap swap swap defaults 0 0
END</pre>
<p></li>
<li><strong>Configure the boot loader on the new drive</strong>:
<pre># NEW_GRUB_CMDLINE_LINUX=$(cat /etc/default/grub | sed -n 's/^GRUB_CMDLINE_LINUX="\(.*\)"/\1/ p')
# NEW_GRUB_CMDLINE_LINUX=${NEW_GRUB_CMDLINE_LINUX//rd.lvm.*([^ ])}
# NEW_GRUB_CMDLINE_LINUX=${NEW_GRUB_CMDLINE_LINUX//resume=*([^ ])}
# NEW_GRUB_CMDLINE_LINUX+=" selinux=0 rd.auto"
# sed -i "/^GRUB_CMDLINE_LINUX=/s/=.*/=\"$NEW_GRUB_CMDLINE_LINUX\"/" /newroot/etc/default/grub</pre>
<p>– You can re-enable selinux after this procedure is complete. But you will have to <a href="https://wiki.centos.org/HowTos/SELinux#head-867ca18a09f3103705cdb04b7d2581b69cd74c55" target="_blank" rel="noreferrer noopener" aria-label=" (opens in a new tab)">relabel your file system</a> first.</p>
<p></li>
<li><strong>Install the boot loader on the new drive</strong>:
<pre># sed -i '/^GRUB_DISABLE_OS_PROBER=.*/d' /newroot/etc/default/grub
# echo "GRUB_DISABLE_OS_PROBER=true" &gt;&gt; /newroot/etc/default/grub
# MY_DISK_1=$(mdadm --detail /dev/md/boot | grep active | grep -m 1 -o "/dev/sd.")
# for i in dev dev/pts proc sys run; do mount -o bind /$i /newroot/$i; done
# chroot /newroot env MY_DISK_1=$MY_DISK_1 bash --login
# test -d /sys/firmware/efi/efivars || MY_GRUB_DIR=/boot/grub2
# test -d /sys/firmware/efi/efivars &amp;&amp; MY_GRUB_DIR=$(find /boot/efi -type d -name 'fedora' -print -quit)
# test -e /usr/sbin/grub2-switch-to-blscfg &amp;&amp; grub2-switch-to-blscfg --grub-directory=$MY_GRUB_DIR
# grub2-mkconfig -o $MY_GRUB_DIR/grub.cfg \;
# test -d /sys/firmware/efi/efivars &amp;&amp; test /boot/grub2/grubenv -nt $MY_GRUB_DIR/grubenv &amp;&amp; cp /boot/grub2/grubenv $MY_GRUB_DIR/grubenv
# test -d /sys/firmware/efi/efivars || grub2-install "$MY_DISK_1"
# logout
# for i in run sys proc dev/pts dev; do umount /newroot/$i; done
# test -d /sys/firmware/efi/efivars &amp;&amp; efibootmgr -c -d "$MY_DISK_1" -p 1 -l "$(find /newroot/boot -name shimx64.efi -printf '/%P\n' -quit | sed 's!/!\\!g')" -L "Fedora RAID Disk 1"</pre>
<p>– The <em>grub2-switch-to-blscfg</em> command is optional. It is only supported on Fedora 29+.<br />– The <em>cp</em> command above should not be necessary, but there appears to be a bug in the current version of grub which causes it to write to $BOOT/grub2/grubenv instead of $BOOT/efi/fedora/grubenv on UEFI systems.<br />– You can use the following command to verify the contents of the <em>grub.cfg</em> file right after running the <em>grub2-mkconfig</em> command above:</p>
<pre># sed -n '/BEGIN .*10_linux/,/END .*10_linux/ p' $MY_GRUB_DIR/grub.cfg</pre>
<p>– You should see references to <em>mdraid</em> and <em>mduuid</em> in the output from the above command if the RAID array was detected properly.</p>
<p></li>
<li><strong>Boot off of the new drive</strong>:
<pre># reboot</pre>
<p>– How to select the new drive is system-dependent. It usually requires pressing one of the <strong>F12</strong><strong></strong>, <strong>F10</strong>, <strong>Esc</strong> or <strong>Del</strong> keys when you hear the <a href="https://en.wikipedia.org/wiki/Power-on_self-test#Original_IBM_POST_beep_codes" target="_blank" rel="noreferrer noopener" aria-label=" (opens in a new tab)">System OK BIOS beep code</a>.<br />– On UEFI systems the boot loader on the new drive should be labeled “Fedora RAID Disk 1”.</p>
<p></li>
<li><strong>Remove all the volume groups and partitions from your old drive</strong>:
<pre># MY_DISK_2=/dev/sda
# MY_VOLUMES=$(pvs | grep $MY_DISK_2 | awk '{print $2}' | tr "\n" " ")
# test -n "$MY_VOLUMES" &amp;&amp; vgremove $MY_VOLUMES
# sgdisk --zap-all $MY_DISK_2</pre>
<p>– <strong>WARNING</strong>: You want to make certain that everything is working properly on your new drive before you do this. A good way to verify that your old drive is no longer being used is to try booting your computer once without the old drive connected.<br />– You can add another new drive to your computer instead of erasing your old one if you prefer.</p>
<p></li>
<li><strong>Create new partitions on your old drive to match the ones on your new drive</strong>:
<pre># test -d /sys/firmware/efi/efivars || sgdisk -n 0:0:+1MiB -t 0:ef02 -c 0:grub_2 $MY_DISK_2
# sgdisk -n 0:0:+1GiB -t 0:ea00 -c 0:boot_2 $MY_DISK_2
# sgdisk -n 0:0:+4GiB -t 0:fd00 -c 0Confusedwap_2 $MY_DISK_2
# sgdisk -n 0:0:0 -t 0:fd00 -c 0:root_2 $MY_DISK_2</pre>
<p>– It is important that the partitions match in size and type. I prefer to use the <em>parted</em> command to display the partition table because it supports setting the display unit:</p>
<pre># parted /dev/sda unit MiB print
# parted /dev/sdb unit MiB print</pre>
<p></li>
<li><strong>Use mdadm to add the new partitions to the RAID devices</strong>:
<pre># mdadm --manage /dev/md/boot --add /dev/disk/by-partlabel/boot_2
# mdadm --manage /dev/md/swap --add /dev/disk/by-partlabel/swap_2
# mdadm --manage /dev/md/root --add /dev/disk/by-partlabel/root_2</pre>
<p></li>
<li><strong>Install the boot loader on your old drive</strong>:
<pre># test -d /sys/firmware/efi/efivars || grub2-install "$MY_DISK_2"
# test -d /sys/firmware/efi/efivars &amp;&amp; efibootmgr -c -d "$MY_DISK_2" -p 1 -l "$(find /boot -name shimx64.efi -printf "/%P\n" -quit | sed 's!/!\\!g')" -L "Fedora RAID Disk 2"</pre>
<p></li>
<li><strong>Use mdadm to test that email notifications are working</strong>:
<pre># mdadm --monitor --scan --oneshot --test</pre>
<p></li>
</ol>
<p>As soon as your drives have finished synchronizing, you should be able to select either drive when restarting your computer and you will receive the same live-mirrored operating system. If either drive fails, mdmonitor will send an email notification. Recovering from a drive failure is now simply a matter of swapping out the bad drive with a new one and running a few <em>sgdisk</em> and <em>mdadm</em> commands to re-create the mirrors (steps 13 through 15). You will no longer have to worry about losing any data if a drive fails!</p>
<h2>Video Demonstrations</h2>
<figure class="wp-block-embed-youtube aligncenter wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio">
<div class="wp-block-embed__wrapper">
<iframe class='youtube-player' type='text/html' width='616' height='347' src='https://www.youtube.com/embed/62RViGv4Nfg?version=3&rel=1&fs=1&autohide=2&showsearch=0&showinfo=1&iv_load_policy=1&wmode=transparent' allowfullscreen='true' style='border:0;'></iframe>
</div><figcaption>Converting a UEFI PC to RAID1</figcaption></figure>
<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>
<figure class="wp-block-embed-youtube aligncenter wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio">
<div class="wp-block-embed__wrapper">
<iframe class='youtube-player' type='text/html' width='616' height='347' src='https://www.youtube.com/embed/4S02tBPHcsk?version=3&rel=1&fs=1&autohide=2&showsearch=0&showinfo=1&iv_load_policy=1&wmode=transparent' allowfullscreen='true' style='border:0;'></iframe>
</div><figcaption>Converting a BIOS PC to RAID1</figcaption></figure>
<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>
<ul>
<li>TIP: Set the the quality to 720p on the above videos for best viewing.</li>
</ul>
</div>
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016