Skip to content

Commit 67695d0

Browse files
committed
virt-xml: Don't regenerate defaults if both hotplugging and defining
For example, if both hotplugging and defining a new NIC, where we generate the mac address, we need to use the initial generated device XML for both operations, and not generate different MAC addresses for each stage. Resolves: virt-manager#305 Signed-off-by: Cole Robinson <[email protected]>
1 parent c0f8da6 commit 67695d0

File tree

3 files changed

+32
-17
lines changed

3 files changed

+32
-17
lines changed

tests/data/cli/compare/virt-xml-update-nodefine-succeed.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55
<on_crash>destroy</on_crash>
66
<devices>
77
+ <interface type="user">
8-
+ <mac address="00:11:22:33:44:57"/>
8+
+ <mac address="00:11:22:33:44:56"/>
99
+ <model type="e1000"/>
1010
+ </interface>
1111
</devices>
1212
</domain>
1313

1414
<interface type="user">
15-
<mac address="00:11:22:33:44:57"/>
15+
<mac address="00:11:22:33:44:56"/>
1616
<model type="e1000"/>
1717
</interface>
1818

tests/data/cli/compare/virt-xml-update-succeed.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ Hotplug this device to the guest 'test'? (y/n): Device hotplug successful.
2626
<on_crash>destroy</on_crash>
2727
<devices>
2828
+ <interface type="user">
29-
+ <mac address="00:11:22:33:44:56"/>
29+
+ <mac address="00:11:22:33:44:55"/>
3030
+ <model type="e1000"/>
3131
+ </interface>
3232
</devices>

virtinst/virtxml.py

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -159,14 +159,20 @@ def action_edit(guest, options, parserclass):
159159
return cli.parse_option_strings(options, guest, inst, editing=True)
160160

161161

162-
def action_add_device(guest, options, parserclass):
162+
def action_add_device(guest, options, parserclass, devs):
163163
if not parserclass.prop_is_list(guest):
164164
fail(_("Cannot use --add-device with --%s") % parserclass.cli_arg_name)
165165
set_os_variant(options, guest)
166-
devs = cli.parse_option_strings(options, guest, None)
167-
devs = xmlutil.listify(devs)
168-
for dev in devs:
169-
dev.set_defaults(guest)
166+
167+
if devs:
168+
for dev in devs:
169+
guest.add_device(dev)
170+
else:
171+
devs = cli.parse_option_strings(options, guest, None)
172+
devs = xmlutil.listify(devs)
173+
for dev in devs:
174+
dev.set_defaults(guest)
175+
170176
return devs
171177

172178

@@ -305,8 +311,15 @@ def update_changes(domain, devs, action, confirm):
305311
print_stdout("")
306312

307313

308-
def prepare_changes(xmlobj, options, parserclass):
309-
origxml = xmlobj.get_xml()
314+
def prepare_changes(orig_xmlobj, options, parserclass, devs=None):
315+
"""
316+
Parse the command line device/XML arguments, and apply the changes to
317+
a copy of the passed in xmlobj.
318+
319+
:returns: (list of device objects, action string, altered xmlobj)
320+
"""
321+
origxml = orig_xmlobj.get_xml()
322+
xmlobj = orig_xmlobj.__class__(conn=orig_xmlobj.conn, parsexml=origxml)
310323
has_edit = options.edit != -1
311324
is_xmlcli = parserclass is cli.ParserXML
312325

@@ -322,7 +335,7 @@ def prepare_changes(xmlobj, options, parserclass):
322335
action = "update"
323336

324337
elif options.add_device:
325-
devs = action_add_device(xmlobj, options, parserclass)
338+
devs = action_add_device(xmlobj, options, parserclass, devs)
326339
action = "hotplug"
327340

328341
elif options.remove_device:
@@ -342,7 +355,7 @@ def prepare_changes(xmlobj, options, parserclass):
342355
elif options.print_xml:
343356
print_stdout(newxml)
344357

345-
return devs, action
358+
return devs, action, xmlobj
346359

347360

348361
#######################
@@ -498,13 +511,14 @@ def main(conn=None):
498511
print_stdout(dev.get_xml())
499512
return 0
500513

514+
devs = None
501515
performed_update = False
502516
if options.update:
503517
if options.update and options.start:
504518
fail_conflicting("--update", "--start")
505-
506519
if vm_is_running:
507-
devs, action = prepare_changes(active_xmlobj, options, parserclass)
520+
devs, action, dummy = prepare_changes(
521+
active_xmlobj, options, parserclass)
508522
update_changes(domain, devs, action, options.confirm)
509523
performed_update = True
510524
else:
@@ -515,14 +529,15 @@ def main(conn=None):
515529
return 0
516530

517531
original_xml = inactive_xmlobj.get_xml()
518-
devs, action = prepare_changes(inactive_xmlobj, options, parserclass)
532+
devs, action, xmlobj_to_define = prepare_changes(
533+
inactive_xmlobj, options, parserclass, devs=devs)
519534
if not options.define:
520535
if options.start:
521-
start_domain_transient(conn, inactive_xmlobj, devs,
536+
start_domain_transient(conn, xmlobj_to_define, devs,
522537
action, options.confirm)
523538
return 0
524539

525-
dom = define_changes(conn, inactive_xmlobj,
540+
dom = define_changes(conn, xmlobj_to_define,
526541
devs, action, options.confirm)
527542
if not dom:
528543
# --confirm user said 'no'

0 commit comments

Comments
 (0)