Please be advised converting a single disk to RAID-1 on Ubuntu Linux is a dangerous operation, and you may lose your data or destroy your Operating System in this process
Prepare the new disk by using following command (assuming your second drive is /dev/sdb):
$ sfdisk -d /dev/sda | sfdisk –force /dev/sdb
This will copy partition scheme from old drive to new one.
Assuming your drive have one partition / (sda1), please create array:
$ mdadm –create /dev/md0 –level=1 –raid-devices=2 missing /dev/sdb1
Edit now /etc/mdadm/mdadm.conf and change DEVICE line:
DEVICE /dev/sda* /dev/sdb*
Update mdadm.conf by typing following command:
$ mdadm –detail –scan >> /etc/mdadm/mdadm.conf
$ dpkg-reconfigure mdadm
Now update grub by selecting /dev/sda and /dev/sdb, except /dev/md0:
$ dpkg-reconfigure grub-pc
Now create exact the same filesystem like on your sda drive on your RAID device:
$ mkfs.ext4 /dev/md0
Mount your /dev/md0 inside /mnt directory:
$ mkdir /mnt/md0
$ mount -t auto /dev/md0 /mnt/md0
Now copy your existing data to the RAID mountpoints by using rsync:
$ rsync -vazHAX –numeric-ids –exclude=”/dev/*” –exclude=”/proc/*” –exclude=”/sys/*” –exclude=”/mnt/*” / /mnt/md0
Edit fstab file located in /mnt/md0/etc/fstab and change root / mount point:
/dev/md0 / ext4 errors=remount-ro 0 1
Now edit /boot/grub/grub.cfg, and change root device from (UUID will be different):
linux /boot/vmlinuz-4.15.0-151-generic root=UUID=12bdcc43-9553-4043-a892-fba9ceb31d93 ro
To:
linux /boot/vmlinuz-4.15.0-151-generic root=/dev/md0 ro
And reboot your server.
After reboot, please add your first drive to the RAID-1 array:
$ mdadm /dev/md0 -a /dev/sda1
Now wait until RAID-1 will be fully synchronized by typing:
$ watch -n1 cat /proc/mdstat
Now we need to change /dev/md0 to UUID inside /etc/fstab. Please type following command and copy the UUID number:
$ blkid | grep md0
Change now /dev/md0 mount point inside /etc/fstab to (UUID will be different):
UUID=3eb8f1ee-88bf-4a0b-90a9-1f622e88a1b9 / ext4 errors=remount-ro 0 1
Now, last time, reinstall and update grub and reboot your server:
$ grub-install /dev/sda
$ grub-install /dev/sdb
$ update-grub
$ reboot