virt-manager

changeset 1601:a42fc22785af

create: Offer to reuse an existing disk image

If the disk image has the expected name /var/lib/libvirt/images/VMNAME.img,
and is unused by any guests, ask the user if they want to use it.
author Cole Robinson <crobinso@redhat.com>
date Wed Mar 03 18:16:08 2010 -0500 (2010-03-03)
parents 0c381de3d89a
children 78c31a870744
files src/virtManager/create.py
line diff
     1.1 --- a/src/virtManager/create.py	Wed Mar 03 17:48:22 2010 -0500
     1.2 +++ b/src/virtManager/create.py	Wed Mar 03 18:16:08 2010 -0500
     1.3 @@ -823,11 +823,14 @@
     1.4  
     1.5          return (media.strip(), extra.strip(), ks.strip())
     1.6  
     1.7 +    def is_default_storage(self):
     1.8 +        return self.window.get_widget("config-storage-create").get_active()
     1.9 +
    1.10      def get_storage_info(self):
    1.11          path = None
    1.12          size = self.window.get_widget("config-storage-size").get_value()
    1.13          sparse = not self.window.get_widget("config-storage-nosparse").get_active()
    1.14 -        if self.window.get_widget("config-storage-create").get_active():
    1.15 +        if self.is_default_storage():
    1.16              path = self.get_default_path(self.guest.name)
    1.17              logging.debug("Default storage path is: %s" % path)
    1.18          else:
    1.19 @@ -835,6 +838,28 @@
    1.20  
    1.21          return (path, size, sparse)
    1.22  
    1.23 +    def get_default_pool(self):
    1.24 +        pool = None
    1.25 +        for uuid in self.conn.list_pool_uuids():
    1.26 +            p = self.conn.get_pool(uuid)
    1.27 +            if p.get_name() == util.DEFAULT_POOL_NAME:
    1.28 +                pool = p
    1.29 +
    1.30 +        if not pool:
    1.31 +            raise RuntimeError(_("Did not find pool '%s'") %
    1.32 +                               util.DEFAULT_POOL_NAME)
    1.33 +
    1.34 +        return pool
    1.35 +
    1.36 +    def get_ideal_path_info(self, name):
    1.37 +        pool = self.get_default_pool()
    1.38 +        suffix = ".img"
    1.39 +        return (pool.get_target_path(), name, suffix)
    1.40 +
    1.41 +    def get_ideal_path(self, name):
    1.42 +        target, name, suffix = self.get_ideal_path_info(name)
    1.43 +        return os.path.join(target, name) + suffix
    1.44 +
    1.45      def get_default_path(self, name):
    1.46          path = ""
    1.47  
    1.48 @@ -860,20 +885,13 @@
    1.49              path = f
    1.50  
    1.51          else:
    1.52 -            pool = None
    1.53 -            for uuid in self.conn.list_pool_uuids():
    1.54 -                p = self.conn.get_pool(uuid)
    1.55 -                if p.get_name() == util.DEFAULT_POOL_NAME:
    1.56 -                    pool = p
    1.57 -
    1.58 -            if not pool:
    1.59 -                raise RuntimeError(_("Did not find pool '%s'") %
    1.60 -                                   util.DEFAULT_POOL_NAME)
    1.61 +            pool = self.get_default_pool()
    1.62 +            target, ignore, suffix = self.get_ideal_path_info(name)
    1.63  
    1.64              path = virtinst.Storage.StorageVolume.find_free_name(name,
    1.65 -                            pool_object=pool.pool, suffix=".img")
    1.66 +                            pool_object=pool.pool, suffix=suffix)
    1.67  
    1.68 -            path = os.path.join(pool.get_target_path(), path)
    1.69 +            path = os.path.join(target, path)
    1.70  
    1.71          return path
    1.72  
    1.73 @@ -1353,6 +1371,31 @@
    1.74              # This can error out
    1.75              diskpath, disksize, sparse = self.get_storage_info()
    1.76  
    1.77 +            if self.is_default_storage() and not revalidate:
    1.78 +                # See if the ideal disk path (/default/pool/vmname.img)
    1.79 +                # exists, and if unused, prompt the use for using it
    1.80 +                ideal = self.get_ideal_path(self.guest.name)
    1.81 +                do_exist = False
    1.82 +                ret = True
    1.83 +
    1.84 +                try:
    1.85 +                    do_exist = virtinst.VirtualDisk.path_exists(
    1.86 +                                                        self.conn.vmm, ideal)
    1.87 +
    1.88 +                    ret = virtinst.VirtualDisk.path_in_use_by(self.conn.vmm,
    1.89 +                                                              ideal)
    1.90 +                except:
    1.91 +                    logging.exception("Error checking default path usage")
    1.92 +
    1.93 +                if do_exist and not ret:
    1.94 +                    do_use = self.err.yes_no(
    1.95 +                        _("The following path already exists, but is not\n"
    1.96 +                          "in use by any virtual machine:\n\n%s\n\n"
    1.97 +                          "Would you like to use this path?") % ideal)
    1.98 +
    1.99 +                    if do_use:
   1.100 +                        diskpath = ideal
   1.101 +
   1.102              if not diskpath:
   1.103                  return self.verr(_("A storage path must be specified."))
   1.104