Merge "disk/vfs: introduce new option to setup"
This commit is contained in:
@@ -14,6 +14,8 @@
|
|||||||
|
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
import mock
|
||||||
|
|
||||||
from nova import exception
|
from nova import exception
|
||||||
from nova import test
|
from nova import test
|
||||||
from nova.tests.unit.virt.disk.vfs import fakeguestfs
|
from nova.tests.unit.virt.disk.vfs import fakeguestfs
|
||||||
@@ -269,3 +271,15 @@ class VirtDiskVFSGuestFSTest(test.NoDBTestCase):
|
|||||||
self.assertIsNotNone(vfs.handle)
|
self.assertIsNotNone(vfs.handle)
|
||||||
self.assertTrue('ext3', vfs.get_image_fs())
|
self.assertTrue('ext3', vfs.get_image_fs())
|
||||||
vfs.teardown()
|
vfs.teardown()
|
||||||
|
|
||||||
|
@mock.patch.object(vfsimpl.VFSGuestFS, 'setup_os')
|
||||||
|
def test_setup_mount(self, setup_os):
|
||||||
|
vfs = vfsimpl.VFSGuestFS("img.qcow2", imgfmt='qcow2')
|
||||||
|
vfs.setup()
|
||||||
|
self.assertTrue(setup_os.called)
|
||||||
|
|
||||||
|
@mock.patch.object(vfsimpl.VFSGuestFS, 'setup_os')
|
||||||
|
def test_setup_mount_false(self, setup_os):
|
||||||
|
vfs = vfsimpl.VFSGuestFS("img.qcow2", imgfmt='qcow2')
|
||||||
|
vfs.setup(mount=False)
|
||||||
|
self.assertFalse(setup_os.called)
|
||||||
|
@@ -12,6 +12,8 @@
|
|||||||
# License for the specific language governing permissions and limitations
|
# License for the specific language governing permissions and limitations
|
||||||
# under the License.
|
# under the License.
|
||||||
|
|
||||||
|
import tempfile
|
||||||
|
|
||||||
import mock
|
import mock
|
||||||
from oslo.config import cfg
|
from oslo.config import cfg
|
||||||
from oslo_concurrency import processutils
|
from oslo_concurrency import processutils
|
||||||
@@ -415,3 +417,35 @@ class VirtDiskVFSLocalFSTest(test.NoDBTestCase):
|
|||||||
'value', '-s',
|
'value', '-s',
|
||||||
'TYPE', '/dev/xyz',
|
'TYPE', '/dev/xyz',
|
||||||
run_as_root=True)
|
run_as_root=True)
|
||||||
|
|
||||||
|
@mock.patch.object(tempfile, 'mkdtemp')
|
||||||
|
@mock.patch.object(nova.virt.disk.mount.nbd, 'NbdMount')
|
||||||
|
def test_setup_mount(self, NbdMount, mkdtemp):
|
||||||
|
vfs = vfsimpl.VFSLocalFS("img.qcow2", imgfmt='qcow2')
|
||||||
|
|
||||||
|
mounter = mock.MagicMock()
|
||||||
|
mkdtemp.return_value = 'tmp/'
|
||||||
|
NbdMount.return_value = mounter
|
||||||
|
|
||||||
|
vfs.setup()
|
||||||
|
|
||||||
|
self.assertTrue(mkdtemp.called)
|
||||||
|
NbdMount.assert_called_once_with(
|
||||||
|
'img.qcow2', 'tmp/', None)
|
||||||
|
mounter.do_mount.assert_called_once_with()
|
||||||
|
|
||||||
|
@mock.patch.object(tempfile, 'mkdtemp')
|
||||||
|
@mock.patch.object(nova.virt.disk.mount.nbd, 'NbdMount')
|
||||||
|
def test_setup_mount_false(self, NbdMount, mkdtemp):
|
||||||
|
vfs = vfsimpl.VFSLocalFS("img.qcow2", imgfmt='qcow2')
|
||||||
|
|
||||||
|
mounter = mock.MagicMock()
|
||||||
|
mkdtemp.return_value = 'tmp/'
|
||||||
|
NbdMount.return_value = mounter
|
||||||
|
|
||||||
|
vfs.setup(mount=False)
|
||||||
|
|
||||||
|
self.assertTrue(mkdtemp.called)
|
||||||
|
NbdMount.assert_called_once_with(
|
||||||
|
'img.qcow2', 'tmp/', None)
|
||||||
|
self.assertFalse(mounter.do_mount.called)
|
||||||
|
@@ -76,7 +76,7 @@ class VFS(object):
|
|||||||
self.imgfmt = imgfmt
|
self.imgfmt = imgfmt
|
||||||
self.partition = partition
|
self.partition = partition
|
||||||
|
|
||||||
def setup(self):
|
def setup(self, mount=True):
|
||||||
"""Performs any one-time setup.
|
"""Performs any one-time setup.
|
||||||
|
|
||||||
Perform any one-time setup tasks to make the virtual filesystem
|
Perform any one-time setup tasks to make the virtual filesystem
|
||||||
|
@@ -69,6 +69,7 @@ class VFSGuestFS(vfs.VFS):
|
|||||||
_("libguestfs is not installed (%s)") % e)
|
_("libguestfs is not installed (%s)") % e)
|
||||||
|
|
||||||
self.handle = None
|
self.handle = None
|
||||||
|
self.mount = False
|
||||||
|
|
||||||
def inspect_capabilities(self):
|
def inspect_capabilities(self):
|
||||||
"""Determines whether guestfs is well configured."""
|
"""Determines whether guestfs is well configured."""
|
||||||
@@ -163,7 +164,7 @@ class VFSGuestFS(vfs.VFS):
|
|||||||
else:
|
else:
|
||||||
raise exception.NovaException(msg)
|
raise exception.NovaException(msg)
|
||||||
|
|
||||||
def setup(self):
|
def setup(self, mount=True):
|
||||||
LOG.debug("Setting up appliance for %(imgfile)s %(imgfmt)s",
|
LOG.debug("Setting up appliance for %(imgfile)s %(imgfmt)s",
|
||||||
{'imgfile': self.imgfile, 'imgfmt': self.imgfmt})
|
{'imgfile': self.imgfile, 'imgfmt': self.imgfmt})
|
||||||
try:
|
try:
|
||||||
@@ -197,9 +198,10 @@ class VFSGuestFS(vfs.VFS):
|
|||||||
self.handle.add_drive_opts(self.imgfile, format=self.imgfmt)
|
self.handle.add_drive_opts(self.imgfile, format=self.imgfmt)
|
||||||
self.handle.launch()
|
self.handle.launch()
|
||||||
|
|
||||||
self.setup_os()
|
if mount:
|
||||||
|
self.setup_os()
|
||||||
self.handle.aug_init("/", 0)
|
self.handle.aug_init("/", 0)
|
||||||
|
self.mount = True
|
||||||
except RuntimeError as e:
|
except RuntimeError as e:
|
||||||
# explicitly teardown instead of implicit close()
|
# explicitly teardown instead of implicit close()
|
||||||
# to prevent orphaned VMs in cases when an implicit
|
# to prevent orphaned VMs in cases when an implicit
|
||||||
@@ -220,7 +222,8 @@ class VFSGuestFS(vfs.VFS):
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
try:
|
try:
|
||||||
self.handle.aug_close()
|
if self.mount:
|
||||||
|
self.handle.aug_close()
|
||||||
except RuntimeError as e:
|
except RuntimeError as e:
|
||||||
LOG.warning(_LW("Failed to close augeas %s"), e)
|
LOG.warning(_LW("Failed to close augeas %s"), e)
|
||||||
|
|
||||||
|
@@ -60,22 +60,23 @@ class VFSLocalFS(vfs.VFS):
|
|||||||
self.imgdir = imgdir
|
self.imgdir = imgdir
|
||||||
self.mount = None
|
self.mount = None
|
||||||
|
|
||||||
def setup(self):
|
def setup(self, mount=True):
|
||||||
self.imgdir = tempfile.mkdtemp(prefix="openstack-vfs-localfs")
|
self.imgdir = tempfile.mkdtemp(prefix="openstack-vfs-localfs")
|
||||||
try:
|
try:
|
||||||
if self.imgfmt == "raw":
|
if self.imgfmt == "raw":
|
||||||
LOG.debug("Using LoopMount")
|
LOG.debug("Using LoopMount")
|
||||||
mount = loop.LoopMount(self.imgfile,
|
mnt = loop.LoopMount(self.imgfile,
|
||||||
self.imgdir,
|
|
||||||
self.partition)
|
|
||||||
else:
|
|
||||||
LOG.debug("Using NbdMount")
|
|
||||||
mount = nbd.NbdMount(self.imgfile,
|
|
||||||
self.imgdir,
|
self.imgdir,
|
||||||
self.partition)
|
self.partition)
|
||||||
if not mount.do_mount():
|
else:
|
||||||
raise exception.NovaException(mount.error)
|
LOG.debug("Using NbdMount")
|
||||||
self.mount = mount
|
mnt = nbd.NbdMount(self.imgfile,
|
||||||
|
self.imgdir,
|
||||||
|
self.partition)
|
||||||
|
if mount:
|
||||||
|
if not mnt.do_mount():
|
||||||
|
raise exception.NovaException(mnt.error)
|
||||||
|
self.mount = mnt
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
with excutils.save_and_reraise_exception():
|
with excutils.save_and_reraise_exception():
|
||||||
LOG.debug("Failed to mount image %(ex)s)", {'ex': e})
|
LOG.debug("Failed to mount image %(ex)s)", {'ex': e})
|
||||||
|
Reference in New Issue
Block a user