]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
ceph-disk-activate: factor mounting out of activate
authorSage Weil <sage@inktank.com>
Sat, 26 Jan 2013 22:44:26 +0000 (14:44 -0800)
committerSage Weil <sage@inktank.com>
Fri, 26 Apr 2013 20:40:01 +0000 (13:40 -0700)
The activate stuff is generic for any OSD, regardless of whether we want
to mount it or not.  Pull that part out.

Signed-off-by: Sage Weil <sage@inktank.com>
(cherry picked from commit 07655288281c9c6f691f87352dc26b7c11ae07e8)

src/ceph-disk-activate

index f78ae17ce889d744477e846c56dcc91c403ada6a..981f4fac70e548cca500052256309393a9b6971e 100755 (executable)
@@ -278,7 +278,7 @@ def upstart_start(
     cluster,
     osd_id,
     ):
-    log.debug('Starting service...')
+    log.debug('Starting %s osd.%s...', cluster, osd_id)
     subprocess.check_call(
         args=[
             'initctl',
@@ -405,35 +405,80 @@ def unmount(
     except subprocess.CalledProcessError as e:
         raise UnmountError(e)
 
-
-def activate(
-    path,
+def mount_activate(
+    dev,
     activate_key_template,
-    do_mount,
     ):
 
-    if do_mount:
+    try:
+        fstype = detect_fstype(dev=dev)
+    except (subprocess.CalledProcessError,
+            TruncatedLineError,
+            TooManyLinesError) as e:
+        raise FilesystemTypeError(
+            'device {dev}'.format(dev=dev),
+            e,
+            )
+
+    mount_options = get_conf(
+        # TODO always using mount options from cluster=ceph for
+        # now; see http://tracker.newdream.net/issues/3253
+        cluster='ceph',
+        variable='osd_fs_mount_options_{fstype}'.format(
+            fstype=fstype,
+            ),
+        )
+
+    path = mount(dev=dev, fstype=fstype, options=mount_options)
+
+    osd_id = None
+    cluster = None
+    try:
+        (osd_id, cluster) = activate(path, activate_key_template)
+
+        # check if the disk is already active
+        active = False
+        src_dev = os.stat(path).st_dev
         try:
-            fstype = detect_fstype(dev=path)
-        except (subprocess.CalledProcessError,
-                TruncatedLineError,
-                TooManyLinesError) as e:
-            raise FilesystemTypeError(
-                'device {dev}'.format(dev=path),
-                e,
+            dst_dev = os.stat('/var/lib/ceph/osd/{cluster}-{osd_id}'.format(
+                    cluster=cluster,
+                    osd_id=osd_id)).st_dev
+            if src_dev == dst_dev:
+                active = True
+        except:
+            pass
+        if active:
+            log.info('%s osd.%s already mounted in position; unmounting ours.' % (cluster, osd_id))
+            unmount(path)
+        else:
+            move_mount(
+                path=path,
+                cluster=cluster,
+                osd_id=osd_id,
                 )
 
-        mount_options = get_conf(
-            # TODO always using mount options from cluster=ceph for
-            # now; see http://tracker.newdream.net/issues/3253
-            cluster='ceph',
-            variable='osd_fs_mount_options_{fstype}'.format(
-                fstype=fstype,
-                ),
+        upstart_start(
+            cluster=cluster,
+            osd_id=osd_id,
+            )
+
+    except:
+        log.error('Failed to activate')
+        unmount(path)
+        raise
+    finally:
+        # if we created a temp dir to mount it, remove it
+        os.rmdir(path)
+
             )
 
         path = mount(dev=path, fstype=fstype, options=mount_options)
 
+def activate(
+    path,
+    activate_key_template,
+    ):
+
     try:
         check_osd_magic(path)
 
@@ -476,6 +521,7 @@ def activate(
 
         # indicate this daemon is managed by upstart
         if not os.path.exists(os.path.join(path, 'upstart')):
+            log.debug('Marking osd as managed by upstart...')
             with file(os.path.join(path, 'upstart'), 'w'):
                 pass
 
@@ -488,39 +534,10 @@ def activate(
                 keyring=keyring,
                 )
             write_one_line(path, 'active', 'ok')
-
-        # check if the disk is already active
-        active = False
-        src_dev = os.stat(path).st_dev
-        try:
-            dst_dev = os.stat('/var/lib/ceph/osd/{cluster}-{osd_id}'.format(
-                    cluster=cluster,
-                    osd_id=osd_id)).st_dev
-            if src_dev == dst_dev:
-                active = True
-        except:
-            pass
-        if active:
-            log.debug('OSD already mounted')
-            unmount(path)
-        else:
-            move_mount(
-                path=path,
-                cluster=cluster,
-                osd_id=osd_id,
-                )
+        log.debug('%s osd.%s data dir is ready at %s', cluster, osd_id, path)
+        return (osd_id, cluster)
     except:
-        unmount(path)
-    finally:
-        if do_mount:
-            # if we created a temp dir to mount it, remove it
-            os.rmdir(path)
-
-    upstart_start(
-        cluster=cluster,
-        osd_id=osd_id,
-        )
-
+        raise
 
 def parse_args():
     parser = argparse.ArgumentParser(
@@ -534,7 +551,7 @@ def parse_args():
     parser.add_argument(
         '--mount',
         action='store_true', default=None,
-        help='mount the device first',
+        help='mount a block device; path must follow',
         )
     parser.add_argument(
         '--activate-key',
@@ -545,7 +562,7 @@ def parse_args():
     parser.add_argument(
         'path',
         metavar='PATH',
-        help='path to OSD data directory, or block device if using --mount',
+        help='path to block device when using --mount',
         )
     parser.set_defaults(
         activate_key_template='/var/lib/ceph/bootstrap-osd/{cluster}.keyring',
@@ -568,11 +585,11 @@ def main():
         )
 
     try:
-        activate(
-            path=args.path,
-            activate_key_template=args.activate_key_template,
-            do_mount=args.mount,
-            )
+        if args.mount:
+            mount_activate(
+                dev=args.path,
+                activate_key_template=args.activate_key_template,
+                )
     except ActivateError as e:
         print >>sys.stderr, '{prog}: {msg}'.format(
             prog=args.prog,