diff --git a/virtinst/cloner.py b/virtinst/cloner.py index ca5c9d8e..93e82a80 100644 --- a/virtinst/cloner.py +++ b/virtinst/cloner.py @@ -480,7 +480,7 @@ class Cloner(object): basename = self.original_guest match = re.search("-clone[1-9]*$", basename) - start_num = 0 + start_num = 1 if match: num_match = re.search("[1-9]+$", match.group()) if num_match: diff --git a/virtinst/storage.py b/virtinst/storage.py index f8c4e252..b9e33858 100644 --- a/virtinst/storage.py +++ b/virtinst/storage.py @@ -461,32 +461,15 @@ class StorageVolume(_StorageObject): Base class for building and installing libvirt storage volume xml """ @staticmethod - def find_free_name(pool_object, basename, - suffix="", collidelist=None, start_num=0): + def find_free_name(pool_object, basename, **kwargs): """ Finds a name similar (or equal) to passed 'basename' that is not - in use by another pool - - This function scans the list of existing Volumes on the passed or - looked up pool object for a collision with the passed name. If the - name is in use, it append "-1" to the name and tries again, then "-2", - continuing to 100000 (which will hopefully never be reached.") If - suffix is specified, attach it to the (potentially incremented) name - before checking for collision. - - Ex name="test", suffix=".img" -> name-3.img - - @param collidelist: An extra list of names to check for collision - @type collidelist: C{list} - @returns: A free name - @rtype: C{str} + in use by another pool. Extra params are passed to generate_name """ - collidelist = collidelist or [] pool_object.refresh(0) - - return util.generate_name(basename, pool_object.storageVolLookupByName, - suffix, collidelist=collidelist, - start_num=start_num) + return util.generate_name(basename, + pool_object.storageVolLookupByName, + **kwargs) TYPE_FILE = getattr(libvirt, "VIR_STORAGE_VOL_FILE", 0) TYPE_BLOCK = getattr(libvirt, "VIR_STORAGE_VOL_BLOCK", 1) diff --git a/virtinst/util.py b/virtinst/util.py index 9a689509..7651628d 100644 --- a/virtinst/util.py +++ b/virtinst/util.py @@ -166,7 +166,7 @@ def validate_macaddr(val): def generate_name(base, collision_cb, suffix="", lib_collision=True, - start_num=0, sep="-", force_num=False, collidelist=None): + start_num=1, sep="-", force_num=False, collidelist=None): """ Generate a new name from the passed base string, verifying it doesn't collide with the collision callback. @@ -201,9 +201,13 @@ def generate_name(base, collision_cb, suffix="", lib_collision=True, else: return collision_cb(tryname) - for i in range(start_num, start_num + 100000): + numrange = range(start_num, start_num + 100000) + if not force_num: + numrange = [None] + numrange + + for i in numrange: tryname = base - if i != start_num or force_num: + if i is not None: tryname += ("%s%d" % (sep, i)) tryname += suffix