FreeBSD Handbook
Prev | Chapter 20 GEOM: Modular Disk Transformation Framework | Next

# 20.4 RAID1 - Mirroring

Mirroring is a technology used by many corporations and home users to back up data without interruption. When a mirror exists, it simply means that diskB replicates diskA. Or, perhaps diskC+D replicates diskA+B. Regardless of the disk configuration, the important aspect is that information on one disk or partition is being replicated. Later, that information could be more easily restored, backed up without causing service or access interruption, and even be physically stored in a data safe.

To begin, ensure the system has two disk drives of equal size, these exercises assume they are direct access (da(4)) SCSI disks.

## 20.4.1 Mirroring Primary Disks

Assuming FreeBSD has been installed on the first, `da0` disk device, gmirror(8) should be told to store its primary data there.

Before building the mirror, enable additional debugging information and opening access to the device by setting the `kern.geom.debugflags` sysctl(8) option to the following value:

    # sysctl kern.geom.debugflags=17

Now create the mirror. Begin the process by storing meta-data information on the primary disk device, effectively creating the `/dev/mirror/gm` device using the following command:

> Warning: Creating a mirror out of the boot drive may result in data loss if any data has been stored on the last sector of the disk. This risk is reduced if creating the mirror is done promptly after a fresh install of FreeBSD. The following procedure is also incompatible with the default installation settings of FreeBSD 9.`X` which use the new GPT partition scheme. GEOM will overwrite GPT metadata, causing data loss and possibly an unbootable system.

    # gmirror label -vb round-robin gm0 /dev/da0

The system should respond with:

    Metadata value stored on /dev/da0.
    Done.

Initialize GEOM, this will load the `/boot/kernel/geom_mirror.ko` kernel module:

    # gmirror load

> Note: When this command completes successfully, it creates the `gm0` device node under the `/dev/mirror` directory.

Enable loading of the `geom_mirror.ko` kernel module during system initialization:

    # echo 'geom_mirror_load="YES"' >> /boot/loader.conf

Edit the `/etc/fstab` file, replacing references to the old `da0` with the new device nodes of the `gm0` mirror device.

> Note: If vi(1) is your preferred editor, the following is an easy way to accomplish this task:

>     # vi /etc/fstab

> In vi(1) back up the current contents of `fstab` by typing `:w /etc/fstab.bak`. Then replace all old `da0` references with `gm0` by typing `:%s/da/mirror\/gm/g`.

The resulting `fstab` file should look similar to the following. It does not matter if the disk drives are SCSI or ATA, the RAID device will be `gm` regardless.

    # Device		Mountpoint	FStype	Options		Dump	Pass#
    /dev/mirror/gm0s1b	none		swap	sw		0	0
    /dev/mirror/gm0s1a	/		ufs	rw		1	1
    /dev/mirror/gm0s1d	/usr		ufs	rw		0	0
    /dev/mirror/gm0s1f	/home		ufs	rw		2	2
    #/dev/mirror/gm0s2d	/store		ufs	rw		2	2
    /dev/mirror/gm0s1e	/var		ufs	rw		2       2
    /dev/acd0		/cdrom		cd9660	ro,noauto	0	0

Reboot the system:

    # shutdown -r now

During system initialization, the `gm0` should be used in place of the `da0` device. Once fully initialized, this may be checked by visually inspecting the output from the `mount` command:

    # mount
    Filesystem         1K-blocks    Used    Avail Capacity  Mounted on
    /dev/mirror/gm0s1a   1012974  224604   707334    24%    /
    devfs                      1       1        0   100%    /dev
    /dev/mirror/gm0s1f  45970182   28596 42263972     0%    /home
    /dev/mirror/gm0s1d   6090094 1348356  4254532    24%    /usr
    /dev/mirror/gm0s1e   3045006 2241420   559986    80%    /var
    devfs                      1       1        0   100%    /var/named/dev

The output looks good, as expected. Finally, to begin synchronization, insert the `da1` disk into the mirror using the following command:

    # gmirror insert gm0 /dev/da1

As the mirror is built the status may be checked using the following command:

    # gmirror status

Once the mirror has been built and all current data has been synchronized, the output from the above command should look like:

          Name    Status  Components
    mirror/gm0  COMPLETE  da0
                          da1

If there are any issues, or the mirror is still completing the build process, the example will show `DEGRADED` in place of `COMPLETE`.

## 20.4.2 Troubleshooting

### 20.4.2.1 System Refuses to Boot

If the system boots up to a prompt similar to:

    ffs_mountroot: can't find rootvp
    Root mount failed: 6
    mountroot>

Reboot the machine using the power or reset button. At the boot menu, select option six (6). This will drop the system to a loader(8) prompt. Load the kernel module manually:

    OK? load geom_mirror
    OK? boot

If this works then for whatever reason the module was not being loaded properly. Check whether the relevant entry in `/boot/loader.conf` is correct. If the problem persists, place:

    options	GEOM_MIRROR

in the kernel configuration file, rebuild and reinstall. That should remedy this issue.

## 20.4.3 Recovering from Disk Failure

The wonderful part about disk mirroring is that when a disk fails, it may be replaced, presumably, without losing any data.

Considering the previous RAID1 configuration, assume that `da1` has failed and now needs to be replaced. To replace it, determine which disk has failed and power down the system. At this point, the disk may be swapped with a new one and the system brought back up. After the system has restarted, the following commands may be used to replace the disk:

    # gmirror forget gm0

    # gmirror insert gm0 /dev/da1

Use the `gmirror` `status` command to monitor the progress of the rebuild. It is that simple.

Prev | Home | Next
RAID0 - Striping | Up | RAID3 - Byte-level Striping with Dedicated Parity