From 18e4dee6616b197a215de6c676800da328836e48 Mon Sep 17 00:00:00 2001
From: Iustin Pop <iustin@google.com>
Date: Fri, 5 Aug 2011 15:41:28 +0200
Subject: [PATCH] Zero DRBD metadata before creation
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

The docstring of the DRBD8 class says:

  … The meta device is checked for valid size and is zeroed on create.

which is not done today, hence we have
http://code.google.com/p/ganeti/issues/detail?id=182:

  node1# mkreiserfs -f /dev/xenvg/t8
  …
  ReiserFS is successfully created on /dev/xenvg/t8.
  node1# drbdmeta --force /dev/drbd256 v08 /dev/xenvg/t8 0 create-md
  md_offset 0
  al_offset 4096
  bm_offset 36864

  Found reiser filesystem

  This would corrupt existing data.
  If you want me to do this, you need to zero out the first part
  of the device (destroy the content).
  You should be very sure that you mean it.
  Operation refused.

I've tested and even just 1MB is enough to wipe the meta, but let's be
safer and pass a 'clean' meta to drbd.

Note: I didn't copy _WipeDevice from backend.py since it seemed more
complex than needed here.

Signed-off-by: Iustin Pop <iustin@google.com>
Reviewed-by: Michael Hanselmann <hansmi@google.com>
---
 lib/bdev.py | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/lib/bdev.py b/lib/bdev.py
index 70470f006..5cf45c9a8 100644
--- a/lib/bdev.py
+++ b/lib/bdev.py
@@ -1131,6 +1131,17 @@ class DRBD8(BaseDRBD):
     This will not work if the given minor is in use.
 
     """
+    # Zero the metadata first, in order to make sure drbdmeta doesn't
+    # try to auto-detect existing filesystems or similar (see
+    # http://code.google.com/p/ganeti/issues/detail?id=182); we only
+    # care about the first 128MB of data in the device, even though it
+    # can be bigger
+    result = utils.RunCmd([constants.DD_CMD,
+                           "if=/dev/zero", "of=%s" % dev_path,
+                           "bs=1048576", "count=128", "oflag=direct"])
+    if result.failed:
+      _ThrowError("Can't wipe the meta device: %s", result.output)
+
     result = utils.RunCmd(["drbdmeta", "--force", cls._DevPath(minor),
                            "v08", dev_path, "0", "create-md"])
     if result.failed:
-- 
GitLab