From 2acbf52ce3b513ba087bbd9ca7883d1a3428bb97 Mon Sep 17 00:00:00 2001 From: Cole Robinson Date: Fri, 5 Dec 2014 19:52:25 -0500 Subject: [PATCH] cli: If creating storage, always build vol_install This is the start of moving this logic out of devicedisk --- tests/clitest.py | 2 +- virtinst/cli.py | 32 ++++++++++++++++++-------------- virtinst/devicedisk.py | 31 ++++++++++++++++++------------- virtinst/diskbackend.py | 11 ++++++++++- 4 files changed, 47 insertions(+), 29 deletions(-) diff --git a/tests/clitest.py b/tests/clitest.py index 9106488bf..feaf4138a 100644 --- a/tests/clitest.py +++ b/tests/clitest.py @@ -512,7 +512,7 @@ c.add_valid("--disk none --boot hd --paravirt --arch i686") # 32 on 64 xen c = vinst.add_category("kvm", "--connect %(KVMURI)s --noautoconsole") c.add_compare("--os-variant fedora20 --file %(EXISTIMG1)s --location %(TREEDIR)s --extra-args console=ttyS0 --cpu host --channel none --console none --sound none --redirdev none", "kvm-f14-url") # F14 Directory tree URL install with extra-args c.add_compare("--test-media-detection %(TREEDIR)s", "test-url-detection") -c.add_compare("--os-variant fedora20 --disk %(NEWIMG1)s,size=.01 --location %(TREEDIR)s --extra-args console=ttyS0 --quiet", "quiet-url") # Quiet URL install should make no noise +c.add_compare("--os-variant fedora20 --disk %(NEWIMG1)s,size=.01,format=vmdk --location %(TREEDIR)s --extra-args console=ttyS0 --quiet", "quiet-url") # Quiet URL install should make no noise c.add_compare("--cdrom %(EXISTIMG2)s --file %(EXISTIMG1)s --os-variant win2k3 --wait 0 --sound --controller usb", "kvm-win2k3-cdrom") # HVM windows install with disk c.add_compare("--os-variant fedora20 --nodisks --boot hd --paravirt --cpu pentium2", "kvm-xenner") # xenner c.add_compare("--os-variant ubuntusaucy --nodisks --boot cdrom --virt-type qemu --cpu Penryn", "qemu-plain") # plain qemu diff --git a/virtinst/cli.py b/virtinst/cli.py index bb948281a..48108e8e6 100644 --- a/virtinst/cli.py +++ b/virtinst/cli.py @@ -1558,30 +1558,34 @@ class ParserDisk(VirtCLIParser): logging.debug("Parsed volume: as pool='%s' vol='%s'", poolname, volname) + inst = VirtCLIParser._parse(self, opts, inst) + + # Generate and fill in the disk source info + newvolname = None + poolobj = None if poolname: if poolname == "default": StoragePool.build_default_pool(self.guest.conn) poolobj = self.guest.conn.storagePoolLookupByName(poolname) - vol_install = None - vol_object = None - if volname: + if inst.path and not inst.source_exists(): + newvolname = os.path.basename(inst.path) + poolobj = inst.get_parent_pool() + elif volname: vol_object = poolobj.storageVolLookupByName(volname) - elif poolname: + inst.set_vol_object(vol_object) + poolobj = None + + if poolobj and (fmt or size or sparse or backing_store): if not fmt: fmt = _get_default_image_format(self.guest.conn, poolobj) - vname = _generate_new_volume_name(self.guest, poolobj, fmt) + if newvolname is None: + newvolname = _generate_new_volume_name(self.guest, poolobj, + fmt) vol_install = VirtualDisk.build_vol_install( - self.guest.conn, vname, poolobj, size, sparse, + self.guest.conn, newvolname, poolobj, size, sparse, fmt=fmt, backing_store=backing_store) - - inst = VirtCLIParser._parse(self, opts, inst) - - if vol_object: - inst.set_vol_object(vol_object) - elif size or fmt or sparse or vol_install: - inst.set_create_storage(size=size, fmt=fmt, - vol_install=vol_install, sparse=sparse) + inst.set_create_storage(vol_install=vol_install) if not inst.target: skip_targets = [d.target for d in self.guest.get_devices("disk")] diff --git a/virtinst/devicedisk.py b/virtinst/devicedisk.py index d9d1025e8..ef19802e9 100644 --- a/virtinst/devicedisk.py +++ b/virtinst/devicedisk.py @@ -99,17 +99,16 @@ def _is_dir_searchable(uid, username, path): def _make_storage_backend(conn, nomanaged, path, vol_object): parent_pool = None - if (conn.check_support(conn.SUPPORT_CONN_STORAGE) and - not vol_object and path and not nomanaged): - path = os.path.abspath(path) + if (not vol_object and path and not nomanaged): (vol_object, parent_pool) = diskbackend.manage_path(conn, path) - backend = diskbackend.StorageBackend(conn, path, vol_object) - return backend, parent_pool + backend = diskbackend.StorageBackend(conn, path, vol_object, parent_pool) + return backend -def _make_storage_creator(conn, backend, - parent_pool, vol_install, clone_path, *creator_args): +def _make_storage_creator(conn, backend, vol_install, clone_path, + *creator_args): + parent_pool = backend.get_parent_pool() if backend.exists(auto_check=False) and backend.path is not None: if not clone_path: return @@ -516,6 +515,10 @@ class VirtualDisk(VirtualDevice): if not self._storage_creator: return None return self._storage_creator.get_vol_install() + def get_parent_pool(self): + if self.get_vol_install(): + return self.get_vol_install().pool + return self._storage_backend.get_parent_pool() def get_size(self): if self._storage_creator: @@ -642,7 +645,7 @@ class VirtualDisk(VirtualDevice): def _get_storage_backend(self): if self.__storage_backend is None: self.__storage_backend = diskbackend.StorageBackend( - self.conn, self._get_xmlpath(), None) + self.conn, self._get_xmlpath(), None, None) return self.__storage_backend def _set_storage_backend(self, val): self.__storage_backend = val @@ -693,10 +696,10 @@ class VirtualDisk(VirtualDevice): if fake and size is None: size = .000001 - backend, parent_pool = _make_storage_backend(self.conn, + backend = _make_storage_backend(self.conn, self.nomanaged, path, None) creator_args = (backing_store, size, sparse, fmt) - creator = _make_storage_creator(self.conn, backend, parent_pool, + creator = _make_storage_creator(self.conn, backend, vol_install, clone_path, *creator_args) @@ -722,9 +725,8 @@ class VirtualDisk(VirtualDevice): return self.is_floppy() or self.is_cdrom() def _change_backend(self, path, vol_object): - backend, pool = _make_storage_backend(self.conn, self.nomanaged, - path, vol_object) - ignore = pool + backend = _make_storage_backend(self.conn, self.nomanaged, + path, vol_object) self._storage_backend = backend def sync_path_props(self): @@ -742,6 +744,9 @@ class VirtualDisk(VirtualDevice): # Need to retrigger this if self.type changed self._set_xmlpath(path) + def source_exists(self): + return self._storage_backend.exists() + def __managed_storage(self): """ Return bool representing if managed storage parameters have diff --git a/virtinst/diskbackend.py b/virtinst/diskbackend.py index 468c12fc5..762520407 100644 --- a/virtinst/diskbackend.py +++ b/virtinst/diskbackend.py @@ -125,6 +125,10 @@ def manage_path(conn, path): """ If path is not managed, try to create a storage pool to probe the path """ + if not conn.check_support(conn.SUPPORT_CONN_STORAGE): + return None, None + + path = os.path.abspath(path) vol, pool = check_if_path_managed(conn, path) if vol or pool or not _can_auto_manage(path): return vol, pool @@ -413,11 +417,12 @@ class StorageBackend(_StorageBase): Class that carries all the info about any existing storage that the disk references """ - def __init__(self, conn, path, vol_object): + def __init__(self, conn, path, vol_object, parent_pool): _StorageBase.__init__(self) self._conn = conn self._vol_object = vol_object + self._parent_pool = parent_pool self._path = path if self._vol_object is not None: @@ -453,6 +458,10 @@ class StorageBackend(_StorageBase): def get_vol_object(self): return self._vol_object + def get_parent_pool(self): + if not self._parent_pool and self._vol_object: + self._parent_pool = self._vol_object.storagePoolLookupByVolume() + return self._parent_pool def get_size(self): """