def find_free_name(name, pool_object=None, pool_name=None, conn=None, suffix=""): """ Finds a name similar (or equal) to passed 'name' 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 @returns: A free name @rtype: C{str} """ pool_object = StorageVolume.lookup_pool_by_name(pool_object=pool_object, pool_name=pool_name, conn=conn) pool_object.refresh(0) return _util.generate_name(name, pool_object.storageVolLookupByName, suffix)
def generate_clone_disk_path(origpath, design, newname=None): origname = design.original_guest newname = newname or design.clone_name path = origpath suffix = "" # Try to split the suffix off the existing disk name. Ex. # foobar.img -> foobar-clone.img # # If the suffix is greater than 7 characters, assume it isn't # a file extension and is part of the disk name, at which point # just stick '-clone' on the end. if origpath.count(".") and len(origpath.rsplit(".", 1)[1]) <= 7: path, suffix = origpath.rsplit(".", 1) suffix = "." + suffix dirname = os.path.dirname(path) basename = os.path.basename(path) clonebase = basename + "-clone" if origname and basename == origname: clonebase = newname clonebase = os.path.join(dirname, clonebase) return _util.generate_name( clonebase, lambda p: VirtualDisk.path_exists(design.original_conn, p), suffix, lib_collision=False)
def find_free_name(name, pool_object=None, pool_name=None, conn=None, suffix="", collidelist=None, start_num=0): """ Finds a name similar (or equal) to passed 'name' 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} """ collidelist = collidelist or [] pool_object = StorageVolume.lookup_pool_by_name( pool_object=pool_object, pool_name=pool_name, conn=conn) pool_object.refresh(0) return _util.generate_name(name, pool_object.storageVolLookupByName, suffix, collidelist=collidelist, start_num=start_num)
def find_free_name(conn, prefix): """ Generate an unused interface name based on prefix. For example, if prefix="br", we find the first unused name such as "br0", "br1", etc. """ return _util.generate_name(prefix, conn.interfaceLookupByName, sep="", force_num=True)
def _build_pool(conn, meter, path): pool = _util.lookup_pool_by_path(conn, path) if pool: logging.debug("Existing pool '%s' found for %s", pool.name(), path) pool.refresh(0) return pool name = _util.generate_name("boot-scratch", conn.storagePoolLookupByName) logging.debug("Building storage pool: path=%s name=%s", path, name) poolbuild = Storage.DirectoryPool(conn=conn, name=name, target_path=path) # Explicitly don't build? since if we are creating this directory # we probably don't have correct perms return poolbuild.install(meter=meter, create=True, build=False, autostart=True)
def generate_clone_name(design): # If the orig name is "foo-clone", we don't want the clone to be # "foo-clone-clone", we want "foo-clone1" basename = design.original_guest match = re.search("-clone[1-9]*$", basename) start_num = 0 if match: num_match = re.search("[1-9]+$", match.group()) if num_match: start_num = int(str(num_match.group())) basename = basename.replace(match.group(), "") basename = basename + "-clone" return _util.generate_name(basename, design.original_conn.lookupByName, sep="", start_num=start_num)
def _build_pool(conn, meter, path): pool = _util.lookup_pool_by_path(conn, path) if pool: logging.debug("Existing pool '%s' found for %s" % (pool.name(), path)) pool.refresh(0) return pool name = _util.generate_name("boot-scratch", conn.storagePoolLookupByName) logging.debug("Building storage pool: path=%s name=%s" % (path, name)) poolbuild = Storage.DirectoryPool(conn=conn, name=name, target_path=path) # Explicitly don't build? since if we are creating this directory # we probably don't have correct perms return poolbuild.install(meter=meter, create=True, build=False, autostart=True)
def generate_clone_disk_path(origpath, design): basename = origpath suffix = "" # Try to split the suffix off the existing disk name. Ex. # foobar.img -> foobar-clone.img # # If the suffix is greater than 7 characters, assume it isn't # a file extension and is part of the disk name, at which point # just stick '-clone' on the end. if basename.count(".") and len(basename.rsplit(".", 1)[1]) <= 7: basename, suffix = basename.rsplit(".", 1) suffix = "." + suffix return _util.generate_name(basename + "-clone", lambda p: VirtualDisk.path_exists(design.original_conn, p), suffix, lib_collision=False)