Skip to content

Commit

Permalink
virt.update: handle changing cdrom images for devices with remote source
Browse files Browse the repository at this point in the history
When a DVD device on a VM has a remote source, virt.update needs to be
able to handle detaching it and attaching a file image live.
  • Loading branch information
cbosdo committed Jun 9, 2020
1 parent fb7bd15 commit a49109c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 13 deletions.
19 changes: 8 additions & 11 deletions salt/modules/virt.py
Original file line number Diff line number Diff line change
Expand Up @@ -2453,16 +2453,15 @@ def update(
new_disks = []
for new_disk in changes["disk"].get("new", []):
device = new_disk.get("device", "disk")
if new_disk.get("type") != "file" and device not in ["cdrom", "floppy"]:
if device not in ["cdrom", "floppy"]:
new_disks.append(new_disk)
continue

target_dev = new_disk.find("target").get("dev")
matching = [
old_disk
for old_disk in changes["disk"].get("deleted", [])
if old_disk.get("type") == "file"
and old_disk.get("device", "disk") == device
if old_disk.get("device", "disk") == device
and old_disk.find("target").get("dev") == target_dev
]
if not matching:
Expand All @@ -2480,15 +2479,13 @@ def update(
else None
)

updated_disk.set("type", "file")
# Detaching device
if source_node is not None:
if not source_file:
# Detaching device
updated_disk.remove(source_node)
else:
# Changing device
source_node.set("file", source_file)
else:
# Attaching device
updated_disk.remove(source_node)

# Attaching device
if source_file:
ElementTree.SubElement(
updated_disk, "source", attrib={"file": source_file}
)
Expand Down
35 changes: 33 additions & 2 deletions tests/unit/modules/test_virt.py
Original file line number Diff line number Diff line change
Expand Up @@ -2207,9 +2207,11 @@ def test_update_removables(self):
<type arch='x86_64' machine='pc-i440fx-2.6'>hvm</type>
</os>
<devices>
<disk type='file' device='cdrom'>
<disk type='network' device='cdrom'>
<driver name='qemu' type='raw' cache='none' io='native'/>
<source file='/srv/dvd-image-1.iso'/>
<source protocol='https' name='/dvd-image-1.iso'>
<host name='test-srv.local' port='80'/>
</source>
<backingStore/>
<target dev='hda' bus='ide'/>
<readonly/>
Expand Down Expand Up @@ -2241,6 +2243,15 @@ def test_update_removables(self):
<alias name='ide0-0-3'/>
<address type='drive' controller='0' bus='0' target='0' unit='3'/>
</disk>
<disk type='network' device='cdrom'>
<driver name='qemu' type='raw' cache='none' io='native'/>
<source protocol='https' name='/dvd-image-6.iso'>
<host name='test-srv.local' port='80'/>
</source>
<backingStore/>
<target dev='hde' bus='ide'/>
<readonly/>
</disk>
</devices>
</domain>
"""
Expand Down Expand Up @@ -2277,6 +2288,12 @@ def test_update_removables(self):
"source_file": "/srv/dvd-image-5.iso",
"model": "ide",
},
{
"name": "dvd5",
"device": "cdrom",
"source_file": "/srv/dvd-image-6.iso",
"model": "ide",
},
],
)

Expand Down Expand Up @@ -2349,6 +2366,20 @@ def test_update_removables(self):
},
"source": {"file": "/srv/dvd-image-5.iso"},
},
{
"type": "file",
"device": "cdrom",
"driver": {
"name": "qemu",
"type": "raw",
"cache": "none",
"io": "native",
},
"backingStore": None,
"target": {"dev": "hde", "bus": "ide"},
"readonly": None,
"source": {"file": "/srv/dvd-image-6.iso"},
},
],
[
salt.utils.xmlutil.to_dict(ET.fromstring(disk), True)
Expand Down

0 comments on commit a49109c

Please sign in to comment.