Soft Mirror a Hard Drive in FreeBSD with gmirror

If you have a hard drive and you want to make sure your data will be intact but for different reasons you don't want to use hardware raid (you do not have support on your motherboard to hardware raid) you can use FreeBSD's gmirror feature to mirror that drive to a new one.

Case 1. Completely mirroring a hard drive that is not used to boot FreeBSD

You have your FreeBSD OS on a hard drive and you have another hard drive (in that system) that you use only for data, and you want to mirror this second drive.

You need to to this:

Add support for Gmirror at boot by adding the following line to /boot/loader.conf:

/boot/loader.conf: geom_mirror_load="YES"

Load the gmirror module (this is useful so you will not need to reboot right now):

kldload geom_mirror

If your FreeBSD main drive is /dev/ada0 and your drives you want to mirror ar /dev/ada0 and /dev/ada1, then use the following command to mirror /dev/ada0 and /dev/ada1:
1. Create the mirror:

gmirror label -v -b round-robin gm0 /dev/ada0

(Please note that /dev/ada0 must be previously newfs-ed hard drive, if not before doing that do a
newfs  to your ada0 drive  - Warning: this command will distroy your previous data)

After last command if you get an error like this:

gmirror: Can't store metadata on /dev/ada0: Operation not permitted.

you must modify kern.geom.debugflags sysctl variable:

sysctl kern.geom.debugflags=16

Now reboot the server.

2.Add the second disc to the mirror:

gmirror insert gm0 /dev/ada1

After you did that you must wait for drives to be syncronized. 
Please note that until drives are syncronized the gmirror system will appear as DIRTY.
Also after you copy/write something to gm0, the system will appear as dirty until drives complete mirroring the file you've copied.

After issuing gmirror insert command (previously described) you will be able to access the mirror system using /dev/mirror/gm0 device name.

And then mount it manualy or by adding it in /etc/fstab to be mounted at next reboot.

If you did not created a file system on /dev/ada0 before ading it to the gmirror system you can newfs the mirror with the following command:

# Warning ! the following command willl delete your data, do it only if you need to:

newfs /dev/mirror/gm0                      #    Warning: this will delete all your previous data!

Case 2. Mirroring a hard drive with a FreeBSD OS installed on it

In this example we have two drives: /dev/ada0 and /dev/ada1. 
/dev/ada0 is your FreeBSD existing installation with all data on it. 
/dev/ada1 is your new drive with no data on it.

In order to add /dev/ada1 to your gmirror system and mirror /dev/ada0 to /dev/ada1 you must do the following:

Step 2.1 Boot to your FreeBSD system on /dev/ada0 and add the following option to /boot/loader.conf load gmirror module at boot:

edit /boot/loader.conf

and add:


Step 2.2 Boot from FreeBSD USB stick or CD and choose Fixit

After you did that add your master drive (the one with FreeBSD installed on it) to the mirror system:

gmirror label -v -b round-robin gm0 /dev/ada0 

We mount  the mirrored system and edit /etc/fstab to replace /dev/ada0 with /dev/mirror/gm0

mount /dev/mirror/gm0s1a /mnt

We edit /mnt/etc/fstab file and replace /dev/ada0 with /dev/mirror/gm0

edit /mnt/etc/fstab

Your fstab might look like this:
# Device                Mountpoint      FStype  Options         Dump    Pass#
/dev/mirror/gm0s1b              none            swap    sw              0       0
/dev/mirror/gm0s1a              /               ufs     rw              1       1
/dev/mirror/gm0s1d              /tmp            ufs     rw              2       2
/dev/mirror/gm0s1f              /usr            ufs     rw              2       2
/dev/mirror/gm0s1e              /var            ufs     rw              2       2

2.4 Reboot your Fixit system and then after the system boots, add the second hard drive to the gmirror system:

gmirror insert gm0 /dev/ada1

Then wait for the hard drives to be syncronized. Use:

gmirror list gm0 

to see the syncronization status.

Notes/tips for gmirror

- To list your gmirror drives: gmirror list gm0  or gmirror status gm0
- If you use encryption like geli is better first to apply gmirror and then encrypt the gm device. That way encription/decryption will happend only once, and not twice, and it's optimal.
- If one of mirrored drive has failed you must remove it from the gmirror system using gmirror command. Then add a new hard drive. 
- If you move your whole system to a new machine and your hard drive order changes the mirror system will still work, you don't have to do anything regarding gmirror system, you only have to modify /etc/fstab.

- If one of your drive was disconnected from the mirror (for example it could happend if you removed SATA cable by mistake) you can add the drive back to the mirror with:

  gmirror forget gm0
  gmirror insert gm0 /dev/ada1

- to remove a drive from gmirror pool:
  gmirror remove gm0  ada1

 Be carefull when issuing those commands. You might lose data. Read the gmirror man page and know what you are doing!