From: Eric Harney <eharney@redhat.com>
Date: Sat, 17 Aug 2013 17:05:38 +0000 (-0400)
Subject: Extend volume for GlusterFS
X-Git-Url: https://review.fuel-infra.org/gitweb?a=commitdiff_plain;h=38b48dd704fa648223bc98f7f5c60a1df666ae87;p=openstack-build%2Fcinder-build.git

Extend volume for GlusterFS

Add support for extend volume.

Change-Id: Ie9ba67d91de2da43e6b857b4256a445c5269c5cd
---

diff --git a/cinder/tests/test_glusterfs.py b/cinder/tests/test_glusterfs.py
index 2715172cc..c08bcce81 100644
--- a/cinder/tests/test_glusterfs.py
+++ b/cinder/tests/test_glusterfs.py
@@ -989,3 +989,34 @@ class GlusterFsDriverTestCase(test.TestCase):
                           'volume-%s' % self.VOLUME_UUID)
 
         mox.VerifyAll()
+
+    def test_extend_volume(self):
+        (mox, drv) = self._mox, self._driver
+
+        volume = self._simple_volume()
+
+        mox.StubOutWithMock(drv, '_execute')
+        mox.StubOutWithMock(drv, 'get_active_image_from_info')
+
+        drv.get_active_image_from_info(volume).AndReturn(volume['name'])
+
+        qemu_img_info_output = """image: volume-%s
+        file format: qcow2
+        virtual size: 1.0G (1073741824 bytes)
+        disk size: 473K
+        """ % self.VOLUME_UUID
+
+        volume_path = '%s/%s/volume-%s' % (self.TEST_MNT_POINT_BASE,
+                                           drv._get_hash_str(
+                                               self.TEST_EXPORT1),
+                                           self.VOLUME_UUID)
+        drv._execute('qemu-img', 'info', volume_path).\
+            AndReturn((qemu_img_info_output, ''))
+
+        drv._execute('qemu-img', 'resize', volume_path, '3G', run_as_root=True)
+
+        mox.ReplayAll()
+
+        drv.extend_volume(volume, 3)
+
+        mox.VerifyAll()
diff --git a/cinder/volume/drivers/glusterfs.py b/cinder/volume/drivers/glusterfs.py
index 0236a027b..ddc4fdb40 100644
--- a/cinder/volume/drivers/glusterfs.py
+++ b/cinder/volume/drivers/glusterfs.py
@@ -701,6 +701,28 @@ class GlusterfsDriver(nfs.RemoteFsDriver):
             if temp_path is not None:
                 self._execute('rm', '-f', temp_path)
 
+    def extend_volume(self, volume, size_gb):
+        volume_path = self.local_path(volume)
+        volume_filename = os.path.basename(volume_path)
+
+        # Ensure no snapshots exist for the volume
+        active_image = self.get_active_image_from_info(volume)
+        if volume_filename != active_image:
+            msg = _('Extend volume is only supported for this'
+                    ' driver when no snapshots exist.')
+            raise exception.InvalidVolume(msg)
+
+        (out, err) = self._execute('qemu-img', 'info', volume_path)
+        backing_fmt = self._get_file_format(out)
+
+        if backing_fmt not in ['raw', 'qcow2']:
+            msg = _('Unrecognized backing format: %s')
+            raise exception.InvalidVolume(msg % backing_fmt)
+
+        # qemu-img can resize both raw and qcow2 files
+        cmd = ['qemu-img', 'resize', volume_path, '%sG' % size_gb]
+        self._execute(*cmd, run_as_root=True)
+
     def _do_create_volume(self, volume):
         """Create a volume on given glusterfs_share.