]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
ceph-disk-activate: Unmount on errors (if it did the mount).
authorTommi Virtanen <tv@inktank.com>
Tue, 2 Oct 2012 23:37:07 +0000 (16:37 -0700)
committerTommi Virtanen <tv@inktank.com>
Fri, 5 Oct 2012 22:41:35 +0000 (15:41 -0700)
This cleans up the error handling to not leave disks mounted
in /var/lib/ceph/tmp/mnt.* when something fails, e.g. when
the ceph command line tool can't talk to mons.

Signed-off-by: Tommi Virtanen <tv@inktank.com>
src/ceph-disk-activate

index 225e2a664ea9b36a5c3ddda592b01d8cf972d88b..e0bfb0f6c39d010ac471c4ed7d36d0be9367cd2f 100755 (executable)
@@ -57,6 +57,12 @@ class MountError(ActivateError):
     """
 
 
+class UnmountError(ActivateError):
+    """
+    Unmounting filesystem failed
+    """
+
+
 def maybe_mkdir(*a, **kw):
     try:
         os.mkdir(*a, **kw)
@@ -354,74 +360,91 @@ def mount(
     return path
 
 
+def unmount(
+    path,
+    ):
+    try:
+        subprocess.check_call(
+            args=[
+                'umount',
+                '--',
+                path,
+                ],
+            )
+    except subprocess.CalledProcessError as e:
+        raise UnmountError(e)
+
+
 def activate(
     path,
     activate_key_template,
     do_mount,
     ):
+
     if do_mount:
         path = mount(dev=path)
 
-    # TODO unmount on errors?
-
-    check_osd_magic(path)
-
-    ceph_fsid = read_one_line(path, 'ceph_fsid')
-    if ceph_fsid is None:
-        raise ActivateError('No cluster uuid assigned.')
-    log.debug('Cluster uuid is %s', ceph_fsid)
-
-    # TODO use ceph_fsid to find the right cluster
-    cluster = 'ceph'
-    log.debug('Cluster name is %s', cluster)
-
-    fsid = read_one_line(path, 'fsid')
-    if fsid is None:
-        raise ActivateError('No OSD uuid assigned.')
-    log.debug('OSD uuid is %s', fsid)
-
-    keyring = activate_key_template.format(cluster=cluster)
-
-    osd_id = get_osd_id(path)
-    if osd_id is None:
-        osd_id = allocate_osd_id(
-            cluster=cluster,
-            fsid=fsid,
-            keyring=keyring,
-            )
-        write_one_line(path, 'whoami', osd_id)
-    log.debug('OSD id is %s', osd_id)
-
-    if not os.path.exists(os.path.join(path, 'ready')):
-        log.debug('Initializing OSD...')
-        # re-running mkfs is safe, so just run until it completes
-        mkfs(
-            path=path,
-            cluster=cluster,
-            osd_id=osd_id,
-            fsid=fsid,
-            keyring=keyring,
-            )
+    try:
+        check_osd_magic(path)
+
+        ceph_fsid = read_one_line(path, 'ceph_fsid')
+        if ceph_fsid is None:
+            raise ActivateError('No cluster uuid assigned.')
+        log.debug('Cluster uuid is %s', ceph_fsid)
+
+        # TODO use ceph_fsid to find the right cluster
+        cluster = 'ceph'
+        log.debug('Cluster name is %s', cluster)
+
+        fsid = read_one_line(path, 'fsid')
+        if fsid is None:
+            raise ActivateError('No OSD uuid assigned.')
+        log.debug('OSD uuid is %s', fsid)
+
+        keyring = activate_key_template.format(cluster=cluster)
+
+        osd_id = get_osd_id(path)
+        if osd_id is None:
+            osd_id = allocate_osd_id(
+                cluster=cluster,
+                fsid=fsid,
+                keyring=keyring,
+                )
+            write_one_line(path, 'whoami', osd_id)
+        log.debug('OSD id is %s', osd_id)
+
+        if not os.path.exists(os.path.join(path, 'ready')):
+            log.debug('Initializing OSD...')
+            # re-running mkfs is safe, so just run until it completes
+            mkfs(
+                path=path,
+                cluster=cluster,
+                osd_id=osd_id,
+                fsid=fsid,
+                keyring=keyring,
+                )
+
+        if not os.path.exists(os.path.join(path, 'active')):
+            log.debug('Authorizing OSD key...')
+            auth_key(
+                path=path,
+                cluster=cluster,
+                osd_id=osd_id,
+                keyring=keyring,
+                )
+            write_one_line(path, 'active', 'ok')
 
-    if not os.path.exists(os.path.join(path, 'active')):
-        log.debug('Authorizing OSD key...')
-        auth_key(
+        move_mount(
             path=path,
             cluster=cluster,
             osd_id=osd_id,
-            keyring=keyring,
             )
-        write_one_line(path, 'active', 'ok')
-
-    move_mount(
-        path=path,
-        cluster=cluster,
-        osd_id=osd_id,
-        )
-
-    if do_mount:
-        # if we created a temp dir to mount it, remove it
-        os.rmdir(path)
+    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,