cloning a raid 1 array

Preconditions: These instructions are for NON GPT disks, ie configured with dos legacy partition styles. sfdisk style partitioning is the default method for ubuntu 16.04.2 LTS server, this is what we want. If you want the clone to be bootable, you must use sfdisk if the source devices are NOT configured as GPT, do NOT use sgdisk, if you do this for a non GPT device you won't be able to get it to boot because of a missing bios boot partition.

For this to work, the partition tables must be made using sfdisk (non GPT, dos compatible partitions). Ubuntu server 16.04.2 default settings for RAID1 are sufficient. debian based distributions would be easiest to do this on. For my tests I used Ubuntu server 16.04.2

Description of clone steps. You're going to clone the partition settings, THEN you're actually going to add the new device to the RAID1 mirror, mdadm is going to automatically "recover" the array, basically making your the new device on the array a clone. Then, after its totally synced into the RAID1 array, in my example , 41 minutes later, you install grub that creates a master boot record (MBR) and makes your device bootable. You can then shut down the computer, take the new single drive that you just added, put it into a new computer, and it will boot up , then you can add raid devices to that computer as desired.

say /dev/sdb and /dev/sdc are your new disks, check by doing ls /dev/disks/by-id to locate your device
restore /dev/sda
copy the partition table from /dev/sda to /dev/sdb:

sfdisk -d /dev/sda | sfdisk /dev/sdb

AND for the other device /dev/sdc

sfdisk -d /dev/sda | sfdisk /dev/sdc

Don't confuse the drives (you still have a backup, though)
re-add partitions on /dev/sdb to the array, for example

mdadm --add /dev/md0 /dev/sdb1
mdadm --add /dev/md1 /dev/sdb2
mdadm --add /dev/md2 /dev/sdb3

Install grub on both disks after you've restored them. The procedure depends on whether you have grub legacy or grub2

For grub legacy do

root (hd0,0)
setup (hd0)
root (hd1,0)
setup (hd1)

For grub2 (ubuntu 16.04.2 LTS server) do

`grub-install /dev/sdb`
`grub-install /dev/sdc`

after this, either one of these new devices will be bootable on its own. Just simply power off the computer or shutdown the computer, take the device out and power them up in a new computer.

investigating this

Need to clone a disk of a RAID1 array to another machine remotely: Fail, Remove, clone drive, then re-add it to the array


md0 : active raid1 sdb1[2] sda1[0]
      488253440 blocks super 1.2 [2/2] [UU]
      bitmap: 4/4 pages [16KB], 65536KB chunk

1) Fail it: mdadm --fail /dev/md0 /dev/sdb1
mdadm: set /dev/sdb1 faulty in /dev/md0
2) Remove it:

mdadm --manage /dev/md0 --re-add /dev/sdb1

mdadm: Cannot open /dev/sdb1: Device or resource busy

mdadm --remove /dev/md0 /dev/sdb1

mdadm: hot removed /dev/sdb1 from /dev/md0
3) clone ce disque:

dd if=/dev/sdb1 bs=4096 conv=sync,noerror | gzip -c > ~/sdb1_image.img.gz

4) view progress of clone in new shell: kill -USR1 <pid> #(ps aux | grep dd finds pid)
13126009+0 enregistrements lus
13126008+0 enregistrements écrits

watch -n 10 kill -USR1 {pid of your DD process}

53764128768 bytes (54 GB, 50 GiB) copied, 326,995 s, 164 MB/s
5) clone mbr (master boot record)

dd if=/dev/sdb2 bs=512 of=~/sdb2_mbr.img count=1

6) copy the clone files over to another server scp -P52224 ~/sdb1_* user@server:/home/user
   a) Howto resume broken download using rsync rsync --partial --progress -e "ssh -p 52224" user@host:~/sdb1_image.img.gz sdb1_image.img.gz
7) restore sdb1_mbr.img to another disk dd if=~/sdb1_mbr.img bs=512 of=/dev/sdb1 count=1
8) restore sdb1_image.img.gz to another disk gunzip -c ~/sdb1_image.img.gz | dd of=/dev/sdb1 bs=4096 conv=sync,noerror
9) mdadm --manage /dev/md0 --re-add /dev/sdb1
mdadm: re-added /dev/sdb1

10) cat /proc/mdstat

md0 : active raid1 sdb1[2] sda1[0]
      488253440 blocks super 1.2 [2/1] [U_]
      [=========>...........]  recovery = 47.9% (234116736/488253440) finish=17.6min speed=240358K/sec
      bitmap: 4/4 pages [16KB], 65536KB chunk