Skip to content

Commit 33f0626

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 33f0626

2 files changed

Lines changed: 52 additions & 5 deletions

File tree

plugins/module_utils/vm.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
from typing import List
2020
from typing import Optional
2121

22+
import yaml
23+
2224
from ..module_utils import errors
2325
from ..module_utils.disk import Disk
2426
from ..module_utils.errors import DeviceNotUnique
@@ -218,7 +220,7 @@ def __init__(
218220
power_state=None,
219221
power_action=None,
220222
nics=None, # nics represents a list of type Nic
221-
disks=None, # disks represents a list of type Nic
223+
disks=None, # disks represents a list of type Disk
222224
# boot_devices are stored as list of nics and/or disks internally.
223225
boot_devices=None,
224226
attach_guest_tools_iso=False,
@@ -423,6 +425,24 @@ def create_export_or_import_vm_payload(ansible_dict, cloud_init, is_export):
423425
payload["template"]["cloudInitData"] = cloud_init
424426
return payload
425427

428+
@staticmethod
429+
def clone_add_user_data_to_cloud_init(cloud_init):
430+
# Task 370 - the generated cloud-init should include a runcmd
431+
# to fix the grub UUID issue at first boot
432+
433+
user_data = cloud_init.get("user_data")
434+
if not user_data:
435+
return cloud_init
436+
437+
cloud_init["user_data"] = user_data.rstrip() + """
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+
return cloud_init
445+
426446
@classmethod
427447
def create_clone_vm_payload(
428448
cls,
@@ -446,6 +466,7 @@ def create_clone_vm_payload(
446466
hypercore_tags.append(tag)
447467
data["template"]["tags"] = ",".join(hypercore_tags)
448468
if cloud_init:
469+
cloud_init = cls.clone_add_user_data_to_cloud_init(cloud_init)
449470
data["template"]["cloudInitData"] = cloud_init
450471
if preserve_mac_address:
451472
data["template"]["netDevs"] = [
@@ -610,6 +631,13 @@ def find_disk(self, slot):
610631
if disk.slot == slot:
611632
return disk
612633

634+
# primary disk is the largest Virtio disk
635+
def get_primary_disk(self):
636+
virtio_disks = [disk for disk in self.disk_list if disk.disk_type == "virtio_disk"]
637+
if not virtio_disks:
638+
return None
639+
return max(virtio_disks, key=lambda disk: disk.size)
640+
613641
def post_vm_payload(self, rest_client, ansible_dict):
614642
# The rest of the keys from VM_PAYLOAD_KEYS will get set properly automatically
615643
# 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)