diff --git a/virtinst/cloner.py b/virtinst/cloner.py index 21a41c1d..d8c26143 100644 --- a/virtinst/cloner.py +++ b/virtinst/cloner.py @@ -62,7 +62,7 @@ class Cloner(object): self.CLONE_POLICY_NO_EMPTYMEDIA] # Generate a random UUID at the start - self.clone_uuid = util.generate_uuid(conn) + self.clone_uuid = Guest.generate_uuid(conn) ############## diff --git a/virtinst/guest.py b/virtinst/guest.py index 98893fa6..82a2711a 100644 --- a/virtinst/guest.py +++ b/virtinst/guest.py @@ -7,6 +7,7 @@ # See the COPYING file in the top-level directory. import logging +import random import libvirt @@ -114,6 +115,27 @@ class Guest(XMLBuilder): return raise ValueError(_("Guest name '%s' is already in use.") % name) + @staticmethod + def generate_uuid(conn): + def _randomUUID(): + if conn.fake_conn_predictable(): + # Testing hack + return "00000000-1111-2222-3333-444444444444" + + u = [random.randint(0, 255) for ignore in range(0, 16)] + u[6] = (u[6] & 0x0F) | (4 << 4) + u[8] = (u[8] & 0x3F) | (2 << 6) + + return "-".join(["%02x" * 4, "%02x" * 2, "%02x" * 2, "%02x" * 2, + "%02x" * 6]) % tuple(u) + + for ignore in range(256): + uuid = _randomUUID() + if not util.libvirt_collision(conn.lookupByUUID, uuid): + return uuid + + logging.error("Failed to generate non-conflicting UUID") + @staticmethod def get_recommended_machine(capsinfo): """ @@ -634,7 +656,7 @@ class Guest(XMLBuilder): def set_defaults(self, _guest): if not self.uuid: - self.uuid = util.generate_uuid(self.conn) + self.uuid = Guest.generate_uuid(self.conn) if not self.vcpus and self.cpu.has_topology(): self.vcpus = self.cpu.vcpus_from_topology() diff --git a/virtinst/util.py b/virtinst/util.py index afee0479..51409da2 100644 --- a/virtinst/util.py +++ b/virtinst/util.py @@ -5,9 +5,7 @@ # See the COPYING file in the top-level directory. # -import logging import os -import random import sys import libvirt @@ -22,14 +20,6 @@ def listify(l): return l -def vm_uuid_collision(conn, uuid): - """ - Check if passed UUID string is in use by another guest of the connection - Returns true/false - """ - return libvirt_collision(conn.lookupByUUIDString, uuid) - - def libvirt_collision(collision_cb, val): """ Run the passed collision function with val as the only argument: @@ -114,29 +104,6 @@ def generate_name(base, collision_cb, suffix="", lib_collision=True, raise ValueError(_("Name generation range exceeded.")) - -def generate_uuid(conn): - for ignore in range(256): - uuid = randomUUID(conn) - if not vm_uuid_collision(conn, uuid): - return uuid - - logging.error("Failed to generate non-conflicting UUID") - - - -def randomUUID(conn): - if conn.fake_conn_predictable(): - # Testing hack - return "00000000-1111-2222-3333-444444444444" - - u = [random.randint(0, 255) for ignore in range(0, 16)] - u[6] = (u[6] & 0x0F) | (4 << 4) - u[8] = (u[8] & 0x3F) | (2 << 6) - return "-".join(["%02x" * 4, "%02x" * 2, "%02x" * 2, "%02x" * 2, - "%02x" * 6]) % tuple(u) - - def xml_escape(xml): """ Replaces chars ' " < > & with xml safe counterparts