The cool thing here is that we only need 1 drive to make a RAID 10 or RAID 1 array, we just tell the Linux mdadm utility that the other drive is "missing" and we can then add our original drive to the array after booting into our new RAID array.
Step#1 Install tools we need
yum -y install mdadm rsync
Step #2 Create your partitions on the drive that will be our RAID array
Here I assume it is /dev/sdb
fdisk /dev/sdb
#I find that mdadm works fine with the default partition type Linux although the fd flag will make them easier to find (fd means Software RAID)
/dev/sdb1 (md0) = Partition #1=/boot size=1G
/dev/sdb2 (md1) = Partition #2=swap size=30G (or whatever is suitable for your RAM and disk space)
/dev/sdb3 (md2) = Partition #3=/ size = the remainder of the disk (unless you have other plans/requirements).
Step #3 - Make our RAID arrays
To make sure your RAID array is bootable we need to ALWAYS make our md0 or /boot this way.
#md0 /boot
#we use level = 1 and metadata=0.90 to ensure /boot is readable by grub otherwise boot will fail
mdadm --create /dev/md0 --level 1 --raid-devices 2 /dev/sdb1 missing --metadata=0.90
#md1 swap
mdadm --create /dev/md1 --level 10 --raid-devices 2 /dev/sdb2 missing
#md2 /
mdadm --create /dev/md2 --level 10 --raid-devices 2 /dev/sdb3 missing
Notice that we specified the second drive as "missing". We will re-add it after we are all done and have rebooted into our RAID array. Still, with the degraded array and only a single drive you can convert a live system into RAID without reinstalling anything.
Step #4 - Make filesystems on RAID arrays
mkfs.ext4 /dev/md0
mkswap /dev/md1
mkfs.ext4 /dev/md2
Step #5 - Mount and stage our current system into new mdadm RAID arrays
We will use /mnt/md2 as out staging point but it could be anything technically.
#make our staging point
mkdir /mnt/md2
# mount our root into our staging point
mount /dev/md2 /mnt/md2
#we need to make our boot inside our staging point before we copy things over
mkdir /mnt/md2/boot
# mount our boot into our staging point
mount /dev/md0 /mnt/md2/boot
Step #6 - Copy our current environment to our new RAID
#we exclude /mnt/so we don't double copy what is in /mnt including our staging environment
# we also exclude the contents of proc, sys because it slows things down and proc and sys will be populated once our new array environment actually gets booted from
rsync -Phaz --exclude=/mnt/* --exclude=/sys/* --exclude=/proc/* / /mnt/md2
Step #7 - chroot into and configure our new environment
Here is how we chroot properly:
#remember I assume your staging point ins in /mnt/md2 change that part if yours is different
for mount in dev sys proc; do
mount -o bind /$mount /mnt/md2/$mount
done
#now let's chroot
chroot /mnt/md2
Step #8 - Disable SELinux
#1 Let's disable selinux it causes lots of problems and if you don't update the selinux attributes you will not be able to login after you boot!
#you would get this error "Failed to create session: Start job for unit user@0.service failed with 'failed'"
sed -i s#SELINUX=enforcing#SELINUX=disabled# /etc/selinux/config
#double check that /etc/selinux/config has SELINUX=disabled just to be sure
Step #9 - Modify grub default config
#2 Let's fix our default grub config, it will often have references to lvm and other hard coded partitions that we no longer have. We also have to add "rd.auto" or grub will not assemble and boot from our array
vi /etc/default/grub
Find this line:
GRUB_CMDLINE_LINUX="crashkernel=auto resume=/dev/mapper/cl-swap rd.lvm.lv=cl/root rd.lvm.lv=cl/swap rhgb quiet"
change to
GRUB_CMDLINE_LINUX="crashkernel=auto rd.auto rhgb quiet"
rd.auto will automatically assemble our raid array otherwise if it's not assembled we can't mount and boot from it.
update grub
grub2-mkconfig > /etc/grub2.cfg
Make sure your grub entries are correct:
Centos grub would not boot because it was relative to /boot but that is wrong since we changed to an actual partition for /boot
cd /boot/loader/entries
ls
02bcb1988e6940a1bed64c61df98716a-0-rescue.conf
02bcb1988e6940a1bed64c61df98716a-4.18.0-147.5.1.el8_1.x86_64.conf
02bcb1988e6940a1bed64c61df98716a-4.18.0-80.el8.x86_64.conf
[root@localhost entries]# vi 02bcb1988e6940a1bed64c61df98716a-4.18.0-147.5.1.el8_1.x86_64.conf
title CentOS Linux (4.18.0-147.5.1.el8_1.x86_64) 8 (Core)
version 4.18.0-147.5.1.el8_1.x86_64
linux /boot/vmlinuz-4.18.0-147.5.1.el8_1.x86_64
initrd /boot/initramfs-4.18.0-147.5.1.el8_1.x86_64.img $tuned_initrd
options $kernelopts $tuned_params
id centos-20200205020746-4.18.0-147.5.1.el8_1.x86_64
grub_users $grub_users
grub_arg --unrestricted
grub_class kernel
Fix the lines in bold and remove the /boot because that will cause your system not to boot. If you have the /boot above it means that your current system has no separate boot partition.
Fixing them would like this:
title CentOS Linux (4.18.0-147.5.1.el8_1.x86_64) 8 (Core)
version 4.18.0-147.5.1.el8_1.x86_64
linux /vmlinuz-4.18.0-147.5.1.el8_1.x86_64
initrd /initramfs-4.18.0-147.5.1.el8_1.x86_64.img $tuned_initrd
options $kernelopts $tuned_params
id centos-20200205020746-4.18.0-147.5.1.el8_1.x86_64
grub_users $grub_users
grub_arg --unrestricted
grub_class kernel
Step #10 - Update /etc/fstab
Modify /etc/fstab and give the UUID for /, boot and swap of your md devices.
md0=/boot
md1=swap
md2=/
#Let's get their block IDs/UUID
blkid /dev/md0
/dev/md0: UUID="f4dc88f5-90ea-4916-97d7-8d627935118" TYPE="ext4"
blkid /dev/md1
/dev/md1: UUID="3adf88f5-90ea-4916-97d7-8d6279871f18" TYPE="swap"
blkid /dev/md2
/dev/md2: UUID="45aa90ea-4916-97d7-8d6279871f18" TYPE="ext4"
vi /etc/fstab
It should look something like this with ONLY the RAID arrays we have and the old stuff commented out
UUID=45aa90ea-4916-97d7-8d6279871f18 / ext4 defaults 0 0
UUID=f4dc88f5-90ea-4916-97d7-8d627935118 /boot ext4 defaults 1 2
UUID=3adf88f5-90ea-4916-97d7-8d6279871f18 swap swap defaults 0 0
Step #11 - Use dracut to update our initramfs otherwise we don't be able to boot still!
#the first part below after -f is the full path name to the initramfs that you will be booting. The second part is just the raw kernel version
dracut -f /boot/initramfs-4.18.0-147.5.1.el8_1.x86_64.img 4.18.0-147.5.1.el8_1.x86_64
dracut -f
alone will work IF you are on the same OS and kernel that is installed
Step#12 - Install grub to all bootable drives
This depends on how many drives you have but let's assume 2 then they are /dev/sda and /dev/sdb
grub2-install /dev/sda
grub2-install /dev/sdb
Step#13 - Cross fingers and reboot
It would be a good idea to go back through the steps and make sure everything is right, including your grub default conf, UUIDs in /etc/fstab etc..
I also recommend NOT doing this on a production machine and at least not without backups. If you want to practice it is best to run through the steps on a Virtual Machine first to identify any mistakes you've made.
reboot
centos, convert, bootable, mdadm, raid, software, arraystep, install, yum, partitions, dev, sdb, fdisk, default, partition, linux, fd, md, swap, suitable, ram, disk, remainder, requirements, arrays, array, metadata, ensure, readable, grub, devices, filesystems, mkfs, ext, mkswap, mount, mnt, staging, mkdir, exclude, contents, proc, sys, slows, populated, booted, rsync, phaz, chroot, configure, ins, bind, disable, selinux, update, attributes, login, quot, user, sed, enforcing, disabled, etc, config, references, lvm, coded, rd, auto, assemble, ll, mkconfig, cfg, doesn, vi, default_kernelopts, uuid, fa, aaa, eb, aa, ro, crashkernel, resume, mapper, cl, lv, rhgb, automatically, updated, fstab, modify, ids, blkid, dc, ea, adf, commented, defaults, dracut, initramfs, booting, kernel, _, img, sda, reboot,