From: Richard W.M. Jones Date: Fri, 9 May 2014 12:34:00 +0000 (+0100) Subject: When building a guest, give it a fixed (but random) MAC address. X-Git-Url: http://git.annexia.org/?p=mclu.git;a=commitdiff_plain;h=ddf87ed6460df738b0f3d555a26c3ab49c4c6c47 When building a guest, give it a fixed (but random) MAC address. Otherwise libvirt generates a new random MAC address on every guest restart. --- diff --git a/libvirt_xml.py b/libvirt_xml.py index a2f082d..ceb5ab7 100644 --- a/libvirt_xml.py +++ b/libvirt_xml.py @@ -17,6 +17,7 @@ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. import os +import random import sys # Generate libvirt XML. Would be nice to use virt-install here, but @@ -26,6 +27,12 @@ def generate_libvirt_xml (vm_name, memory, vcpus, virtio, output): if not virtio: network_model = "e1000" + # Give the network a fixed MAC address, otherwise libvirt will + # generate a random one on every guest restart. + network_mac = "52:54:00:%02x:%02x:%02x" % (random.randint (0, 0xff), + random.randint (0, 0xff), + random.randint (0, 0xff)) + # XXX Quoting, and we should use a real XML generator. xml = """ @@ -53,6 +60,7 @@ def generate_libvirt_xml (vm_name, memory, vcpus, virtio, output): restart + @@ -66,7 +74,7 @@ def generate_libvirt_xml (vm_name, memory, vcpus, virtio, output): -""" % (vm_name, memory, memory, vcpus, network_model) +""" % (vm_name, memory, memory, vcpus, network_mac, network_model) # virtio-scsi or IDE disk: if virtio: