# gmirror on running system. What partition wins?



## Ben (May 1, 2012)

Hi,

I have a FreeBSD 7 running on harddisk /dev/ad6.

I have a second harddisk which is currently not empty at /dev/ad4. On /dev/ad4 is a former FreeBSD 8 installation. This /dev/ad4 is part of the mirrored geom mirror/gm0.

I want to erase /dev/ad4 and combine /dev/ad6 and /dev/ad4 to one mirrored geom.

When I run [cmd=]gmirror status[/cmd] I can see "COMPLETE" for /dev/ad4 which is correct. But what happens if I add /dev/ad6 to /dev/ad4? It will "sync" the harddisks, but what does it mean? Will it erase the data on /dev/ad6 as /dev/ad4 is the "master" (if this term exists in a RAID) or will it copy /dev/ad6 to /dev/ad4?

The problem is that the system is currently running (not in use) and I dont have physical access at the moment, so I can not do the "normal" way booting from a DVD.

What would you suggest to do? I mustn't lose the data on /dev/ad6 (even backup exists for the worst case).

Can I make sure that /dev/ad6 is copied on /dev/ad4? In the meantime I would activate gmirror on boot and edit my /etc/fstab.

Thanks for recommendations.


----------



## usdmatt (May 1, 2012)

So at the moment you have ad6 which is the active disk you want to keep, and ad4 that has an old FreeBSD install and gmirror config that you want to re-use?

I'm assuming you are using MBR and bsdlabels as gmirror with GPT is a pain.

As ad4 is currently the 'master' mirror device, attaching ad6 to the mirror will sync the contents of ad4 to it, which you don't want. (Although the fact that ad6 is mounted may stop this from succeeding)

I would clear the old disk first which should stop it showing up as a mirror and should remove any /dev/mirror/gmX devices.


```
gmirror clear ad4
```

Then follow the normal instructions to make ad6 into a mirror, attach ad4 to it, and update /etc/fstab.
See: http://www.freebsd.org/doc/handbook/geom-mirror.html

Please note I provide no warranty that you will not end up wiping the system drive by accident, or leave the system in a state that won't boot. Make sure you backup any important data first.


----------



## Ben (May 1, 2012)

No, no GPT here. That's correct.

Yes, this "Master/Slave" thing I assumed already. I tried running "clear" on ad4 but it failed even it is not mounted.

Thanks for confirming this behaviour. I will check what I can do.

Thanks.


----------



## Leander (May 1, 2012)

There is nice tutorials for your case.


```
ROOT_HDD="/dev/ad6"
SECOND_HDD="/dev/ad4"

sysctl kern.geom.debugflags=17

[[ -z $(kldstat | grep -E "geom_mirror.ko$") ]] && \
kldload geom_mirror
```

This will activate gmirror for first / main drive [MASTER]:

```
gmirror label -v -b round-robin gm0 ${ROOT_HDD}
```

This will add a second drive [SLAVE] to existing gm0:

```
gmirror insert gm0 ${SECOND_HDD}
```

Remember that you want to give gmirror the entire drive .. not just a partition or a slice.
so /dev/da6 and NOT /dev/da6s1a

Adjusting /etc/fstab in order to use new mirror device

```
sed -i -- "s%ad6%mirror/gm0%g" /mnt/etc/fstab
```
Adding this to load gmirror module whe nsystem boots:

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

Also remember  may want to use a file based swap instead of a seperate partition. My reasons are following:

I've had issues once that System foze when swap had data while disk failover. After this I even read somethign about issues with mirroring swap - unfortunately I couldn't find it for you.
file based swap is way more flexible in size
turncate is the cherry on top of the cake acording to flexibility in size

In order to do so:

```
truncate -s 4G /var/swap
chmod 0600 /var/swap
mdconfig -a -t vnode -f /var/swap -u 0
swapon /dev/md0

echo '
### SWAP File
swapfile="/var/swap"
' >> /etc/rc.conf
```
The rc.conf entry will mdconfig the swapfile and swap-it-on for you every time you reboot.

... turn debugging off again:

```
sysctl kern.geom.debugflags=0
```


EDIT ... oh I see usdmatt was more quick in answering then I was


----------



## DutchDaemon (May 1, 2012)

@Ben, no unformatted posts, please.


----------



## Leander (May 1, 2012)

Before you can [cmd=]gmirror clear ad4[/cmd]you have to remove the drive from the old mirror first.

There is still another way of clearing metadata from disk by the help of dd:

This will not only delete the very first sector but the first ten in which metadata could be stored:
[cmd=]dd if=/dev/zero of=/dev/da4 bs=512 count=10[/cmd]

This will clear potential metadata such as gpart, gmirror, gconcat etc. ...

```
LASTSECTOR=`diskinfo -v /dev/ad4 | grep "mediasize in sectors" | awk '{print $1}'`
let "DEL=$LASTSECTOR-10"
dd if=/dev/zero of=/dev/ad4 seek=$DEL
```
The $LASTSECTOR-10 makes sure to delete metadata in case you've had up to ten geom layers on top of each other


----------



## Ben (May 1, 2012)

Everything worked fine. Thanks.


----------



## wblock@ (May 1, 2012)

Some notes:

Setting kern.geom.debugflags should be avoided.  If a procedure won't work without it, it means you're overwriting something in use.

The Handbook procedure shown to create a mirror in-place with an existing disk is well-intentioned but creates a mirror with some incorrect structures that will not boot with FreeBSD 9.  The right way to do it is to back up, create a fresh mirror out of the drives, and restore to it.


----------

