Skip to content

Commit 8ec4e58

Browse files
committed
Changes
1 parent 689fee5 commit 8ec4e58

2 files changed

Lines changed: 30 additions & 11 deletions

File tree

plugins/module_utils/vm.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
__metaclass__ = type
1313

14+
import yaml
1415
import base64
1516
from time import sleep
1617
from time import time
@@ -423,6 +424,24 @@ def create_export_or_import_vm_payload(ansible_dict, cloud_init, is_export):
423424
payload["template"]["cloudInitData"] = cloud_init
424425
return payload
425426

427+
def clone_add_user_data_to_cloud_init(cloud_init):
428+
# Task 370 - the generated cloud-init should include a runcmd to fix the grub UUID issue at first boot
429+
if not cloud_init.get("user_data"):
430+
return cloud_init
431+
432+
config = yaml.safe_load(cloud_init["user_data"]) or {}
433+
434+
runcmd = config.setdefault("runcmd", [])
435+
runcmd.extend(
436+
[
437+
"sed -i 's/^GRUB_DISABLE_LINUX_UUID=true/#GRUB_DISABLE_LINUX_UUID=true/' /etc/default/grub",
438+
"update-grub",
439+
]
440+
)
441+
# Keeps the header, safe_dump removes it
442+
cloud_init["user_data"] = "#cloud-config\n" + yaml.safe_dump(config)
443+
return cloud_init
444+
426445
@classmethod
427446
def create_clone_vm_payload(
428447
cls,
@@ -446,6 +465,7 @@ def create_clone_vm_payload(
446465
hypercore_tags.append(tag)
447466
data["template"]["tags"] = ",".join(hypercore_tags)
448467
if cloud_init:
468+
cloud_init = clone_add_user_data_to_cloud_init(cloud_init)
449469
data["template"]["cloudInitData"] = cloud_init
450470
if preserve_mac_address:
451471
data["template"]["netDevs"] = [
@@ -612,7 +632,10 @@ def find_disk(self, slot):
612632

613633
# primary disk is the largest Virtio disk
614634
def get_primary_disk(self):
615-
return max(self.disk_list, key=lambda disk: disk.size)
635+
virtio_disks = [disk for disk in self.disk_list if disk.disk_type == "virtio_disk"]
636+
if not virtio_disks:
637+
return None
638+
return max(virtio_disks, key=lambda disk: disk.size)
616639

617640
def post_vm_payload(self, rest_client, ansible_dict):
618641
# The rest of the keys from VM_PAYLOAD_KEYS will get set properly automatically

plugins/modules/vm_clone.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -167,10 +167,13 @@ def run(module, rest_client):
167167
task_status = TaskTag.get_task_status(rest_client, task)
168168
if task_status and task_status.get("state", "") == "COMPLETE":
169169
# Get cloned VM
170-
virtual_machine_cloned_obj = VM.get_or_fail(query={"name": module.params["vm_name"]}, rest_client=rest_client)
170+
virtual_machine_cloned_obj = VM.get_or_fail(query={"name": module.params["vm_name"]}, rest_client=rest_client)[
171+
0
172+
]
171173
# Set boot devices after cloning Issue-370 (VM starts failing as soon as another disk is attahed if boot is not specified)
172174
# By default we always set the largest Virtio disk which is the "primary disk"
173-
boot_items = [virtual_machine_cloned_obj.get_primary_disk.uuid]
175+
primary_disk = virtual_machine_cloned_obj.get_primary_disk()
176+
boot_items = [primary_disk.uuid] if primary_disk else []
174177
# previous boot order after cloning is always empty
175178
previous_boot_order = []
176179
changed = virtual_machine_cloned_obj.set_boot_devices(boot_items, module, rest_client, previous_boot_order)
@@ -209,14 +212,7 @@ def main():
209212
type="dict",
210213
default={},
211214
options=dict(
212-
user_data=dict(
213-
type="str",
214-
default=(
215-
"runcmd:\n"
216-
" - sed -i 's/^GRUB_DISABLE_LINUX_UUID=true/#GRUB_DISABLE_LINUX_UUID=true/' /etc/default/grub\n"
217-
" - update-grub\n"
218-
),
219-
),
215+
user_data=dict(type="str"),
220216
meta_data=dict(type="str"),
221217
),
222218
),

0 commit comments

Comments
 (0)