util: Fix generate_name start_num logic

We would start at start_num+1 which was confusing, fix it to do
the right thing.
This commit is contained in:
Cole Robinson 2013-10-05 10:22:27 -04:00
parent 20dfebeda3
commit 98187eb4b7
3 changed files with 13 additions and 26 deletions

View File

@ -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:

View File

@ -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)

View File

@ -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