]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
ceph-disk-activate: detect whether PATH is mount or dir
authorSage Weil <sage@inktank.com>
Sun, 27 Jan 2013 04:33:16 +0000 (20:33 -0800)
committerSage Weil <sage@inktank.com>
Wed, 13 Feb 2013 20:35:43 +0000 (12:35 -0800)
remove in-the-way symlinks in /var/lib/ceph/osd

This is simpler.  Just detect what the path is and Do The Right Thing.

Closes #3341 (which wanted to make --mount the default)

Signed-off-by: Sage Weil <sage@inktank.com>
src/ceph-disk-activate

index cf3c23692e0d8b203dcf365788da5b584834f245..18c33ef3d3d29a08738bb4f2b3944270324e3a25 100755 (executable)
@@ -7,6 +7,7 @@ import os
 import os.path
 import re
 import subprocess
+import stat
 import sys
 import tempfile
 
@@ -69,6 +70,10 @@ class UnmountError(ActivateError):
 
 
 def maybe_mkdir(*a, **kw):
+    # remove any symlink, if it is there..
+    if os.path.exists(*a) and stat.S_ISLNK(os.lstat(*a).st_mode):
+        log.debug('Removing old symlink at %s', *a)
+        os.unlink(*a)
     try:
         os.mkdir(*a, **kw)
     except OSError, e:
@@ -491,9 +496,44 @@ def mount_activate(
         # remove out temp dir
         os.rmdir(path)
 
+
+def activate_dir(
+    path,
+    activate_key_template,
+    init,
+    ):
+
+    if not os.path.exists(path):
+        raise ActivateError(
+            'directory %s does not exist' % path
             )
 
-        path = mount(dev=path, fstype=fstype, options=mount_options)
+    (osd_id, cluster) = activate(path, activate_key_template, init)
+    canonical = '/var/lib/ceph/osd/{cluster}-{osd_id}'.format(
+        cluster=cluster,
+        osd_id=osd_id)
+    if path != canonical:
+        # symlink it from the proper location
+        create = True
+        if os.path.lexists(canonical):
+            old = os.readlink(canonical)
+            if old != path:
+                log.debug('Removing old symlink %s -> %s', canonical, old)
+                try:
+                    os.unlink(canonical)
+                except:
+                    raise ActivateError('unable to remove old symlink %s', canonical)
+            else:
+                create = False
+        if create:
+            log.debug('Creating symlink %s -> %s', canonical, path)
+            try:
+                os.symlink(path, canonical)
+            except:
+                raise ActivateError('unable to create symlink %s -> %s', canonical, path)
+
+    return (cluster, osd_id)
+
 
 def activate(
     path,
@@ -591,7 +631,8 @@ def parse_args():
     parser.add_argument(
         'path',
         metavar='PATH',
-        help='path to block device when using --mount',
+        nargs='?',
+        help='path to block device or directory',
         )
     parser.add_argument(
         '--mark-init',
@@ -622,12 +663,25 @@ def main():
     try:
         cluster = None
         osd_id = None
-        if args.mount:
+
+        if not os.path.exists(args.path):
+            raise ActivateError('%s does not exist', args.path)
+
+        mode = os.stat(args.path).st_mode
+        if stat.S_ISBLK(mode):
             (cluster, osd_id) = mount_activate(
                 dev=args.path,
                 activate_key_template=args.activate_key_template,
                 init=args.mark_init,
                 )
+        elif stat.S_ISDIR(mode):
+            (cluster, osd_id) = activate_dir(
+                path=args.path,
+                activate_key_template=args.activate_key_template,
+                init=args.mark_init,
+                )
+        else:
+            raise ActivateError('%s is not a directory or block device', args.path)
 
         start_daemon(
             cluster=cluster,