Skip to content

Commit da98ec0

Browse files
committed
Added default logic after cloning
- cloud init's user data not has a default value - boot devices by default set as the primary Virtio disk
1 parent 34308d2 commit da98ec0

2 files changed

Lines changed: 53 additions & 5 deletions

File tree

plugins/module_utils/vm.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ def __init__(
218218
power_state=None,
219219
power_action=None,
220220
nics=None, # nics represents a list of type Nic
221-
disks=None, # disks represents a list of type Nic
221+
disks=None, # disks represents a list of type Disk
222222
# boot_devices are stored as list of nics and/or disks internally.
223223
boot_devices=None,
224224
attach_guest_tools_iso=False,
@@ -423,6 +423,27 @@ def create_export_or_import_vm_payload(ansible_dict, cloud_init, is_export):
423423
payload["template"]["cloudInitData"] = cloud_init
424424
return payload
425425

426+
@staticmethod
427+
def clone_add_user_data_to_cloud_init(cloud_init):
428+
# Task 370 - the generated cloud-init should include a runcmd
429+
# to fix the grub UUID issue at first boot
430+
431+
user_data = cloud_init.get("user_data")
432+
if not user_data:
433+
return cloud_init
434+
435+
cloud_init["user_data"] = (
436+
user_data.rstrip()
437+
+ """
438+
439+
runcmd:
440+
- sed -i 's/^GRUB_DISABLE_LINUX_UUID=true/#GRUB_DISABLE_LINUX_UUID=true/' /etc/default/grub
441+
- update-grub
442+
"""
443+
)
444+
445+
return cloud_init
446+
426447
@classmethod
427448
def create_clone_vm_payload(
428449
cls,
@@ -446,6 +467,7 @@ def create_clone_vm_payload(
446467
hypercore_tags.append(tag)
447468
data["template"]["tags"] = ",".join(hypercore_tags)
448469
if cloud_init:
470+
cloud_init = cls.clone_add_user_data_to_cloud_init(cloud_init)
449471
data["template"]["cloudInitData"] = cloud_init
450472
if preserve_mac_address:
451473
data["template"]["netDevs"] = [
@@ -610,6 +632,13 @@ def find_disk(self, slot):
610632
if disk.slot == slot:
611633
return disk
612634

635+
# primary disk is the largest Virtio disk
636+
def get_primary_disk(self):
637+
virtio_disks = [disk for disk in self.disk_list if disk.disk_type == "virtio_disk"]
638+
if not virtio_disks:
639+
return None
640+
return max(virtio_disks, key=lambda disk: disk.size)
641+
613642
def post_vm_payload(self, rest_client, ansible_dict):
614643
# The rest of the keys from VM_PAYLOAD_KEYS will get set properly automatically
615644
# Cloud init will be obtained through ansible_dict - If method will be reused outside of vm module,

plugins/modules/vm_clone.py

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,29 @@ def run(module, rest_client):
166166
TaskTag.wait_task(rest_client, task)
167167
task_status = TaskTag.get_task_status(rest_client, task)
168168
if task_status and task_status.get("state", "") == "COMPLETE":
169-
return (
170-
True,
171-
f"Virtual machine - {module.params['source_vm_name']} - cloning complete to - {module.params['vm_name']}.",
172-
)
169+
# Get cloned VM
170+
virtual_machine_cloned_obj = VM.get_or_fail(query={"name": module.params["vm_name"]}, rest_client=rest_client)[
171+
0
172+
]
173+
# Set boot devices after cloning Issue-370 (VM starts failing as soon as another disk is attahed if boot is not specified)
174+
# By default we always set the largest Virtio disk which is the "primary disk"
175+
primary_disk = virtual_machine_cloned_obj.get_primary_disk()
176+
boot_items = [primary_disk.uuid] if primary_disk else []
177+
# previous boot order after cloning is always empty
178+
previous_boot_order = []
179+
changed = virtual_machine_cloned_obj.set_boot_devices(boot_items, module, rest_client, previous_boot_order)
180+
if changed:
181+
msg = "and boot order was set - you can change it with vm_boot_devices module"
182+
return (
183+
True,
184+
f"Virtual machine - {module.params['source_vm_name']} - cloning complete to - {module.params['vm_name']} {msg}.",
185+
)
186+
else:
187+
msg = "and boot order was not set - you can set it with vm_boot_devices module"
188+
return (
189+
True,
190+
f"Virtual machine - {module.params['source_vm_name']} - cloning complete to - {module.params['vm_name']} {msg}.",
191+
)
173192
raise errors.ScaleComputingError(
174193
f"There was a problem during cloning of {module.params['source_vm_name']}, cloning failed."
175194
)

0 commit comments

Comments
 (0)