From ddf87ed6460df738b0f3d555a26c3ab49c4c6c47 Mon Sep 17 00:00:00 2001 From: "Richard W.M. Jones" Date: Fri, 9 May 2014 13:34:00 +0100 Subject: [PATCH] When building a guest, give it a fixed (but random) MAC address. Otherwise libvirt generates a new random MAC address on every guest restart. --- libvirt_xml.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) 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: -- 1.8.3.1