From a7cdfccf38caed80c08abca1d3bd2d54f3c840fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pavel=20Czern=C3=BD?= Date: Tue, 26 Jan 2021 11:28:07 +0100 Subject: [PATCH] F #1048: Unique VM identification (for KVM) (#700) --- include/NebulaUtil.h | 5 +++++ src/common/NebulaUtil.cc | 22 ++++++++++++++++++++++ src/vm/VirtualMachine.cc | 2 +- src/vm/VirtualMachineParser.cc | 17 ++++++++++++++--- src/vmm/LibVirtDriverKVM.cc | 8 ++++++++ 5 files changed, 50 insertions(+), 4 deletions(-) diff --git a/include/NebulaUtil.h b/include/NebulaUtil.h index ba724f50d1..0407e7c40d 100644 --- a/include/NebulaUtil.h +++ b/include/NebulaUtil.h @@ -327,6 +327,11 @@ namespace one_util return output; } + /** + * Generates a new uuid + */ + std::string uuid(); + } // namespace one_util #endif /* _NEBULA_UTIL_H_ */ diff --git a/src/common/NebulaUtil.cc b/src/common/NebulaUtil.cc index 9310a6abf1..c319b5eadf 100644 --- a/src/common/NebulaUtil.cc +++ b/src/common/NebulaUtil.cc @@ -428,3 +428,25 @@ void one_util::split_unique(const string& st, char delim, set& res) res.insert(str); } } + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +string one_util::uuid() +{ + // Generate from random numbers to avoid adding a dependency + ostringstream oss; + + oss.fill('0'); + oss << hex + << setw(4) << random() + << setw(4) << random() + << "-" << setw(4) << random() + << "-" << setw(4) << ((random() & 0x0fff) | 0x4000) + << "-" << setw(4) << random() % 0x3fff + 0x8000 + << "-" << setw(4) << random() + << setw(4) << random() + << setw(4) << random(); + + return oss.str(); +} \ No newline at end of file diff --git a/src/vm/VirtualMachine.cc b/src/vm/VirtualMachine.cc index cb1612c343..e9354946a1 100644 --- a/src/vm/VirtualMachine.cc +++ b/src/vm/VirtualMachine.cc @@ -2767,7 +2767,7 @@ void VirtualMachine::get_public_clouds(const string& pname, set &clouds) /* -------------------------------------------------------------------------- */ static std::map> UPDATECONF_ATTRS = { - {"OS", {"ARCH", "MACHINE", "KERNEL", "INITRD", "BOOTLOADER", "BOOT", "KERNEL_CMD", "ROOT", "SD_DISK_BUS"} }, + {"OS", {"ARCH", "MACHINE", "KERNEL", "INITRD", "BOOTLOADER", "BOOT", "KERNEL_CMD", "ROOT", "SD_DISK_BUS", "UUID"} }, {"FEATURES", {"PAE", "ACPI", "APIC", "LOCALTIME", "HYPERV", "GUEST_AGENT", "VIRTIO_SCSI_QUEUES"} }, {"INPUT", {"TYPE", "BUS"} }, diff --git a/src/vm/VirtualMachineParser.cc b/src/vm/VirtualMachineParser.cc index 9d4babc3a8..5cbdb9ca63 100644 --- a/src/vm/VirtualMachineParser.cc +++ b/src/vm/VirtualMachineParser.cc @@ -187,15 +187,18 @@ int VirtualMachine::parse_os(string& error_str) if ( num == 0 ) { - return 0; + os = new VectorAttribute("OS"); + obj_template->set(os); } else if ( num > 1 ) { error_str = "Only one OS attribute can be defined."; return -1; } - - os = dynamic_cast(os_attr[0]); + else + { + os = dynamic_cast(os_attr[0]); + } if ( os == 0 ) { @@ -212,6 +215,14 @@ int VirtualMachine::parse_os(string& error_str) rc = set_os_file(os, "INITRD", Image::RAMDISK, error_str); + string uuid = os->vector_value("UUID"); + + if (uuid.empty()) + { + uuid = one_util::uuid(); + os->replace("UUID", uuid); + } + if ( rc != 0 ) { return -1; diff --git a/src/vmm/LibVirtDriverKVM.cc b/src/vmm/LibVirtDriverKVM.cc index 5fbc54db6d..1274ca18cf 100644 --- a/src/vmm/LibVirtDriverKVM.cc +++ b/src/vmm/LibVirtDriverKVM.cc @@ -614,6 +614,14 @@ int LibVirtDriver::deployment_description_kvm( file << "\t" << vm->get_name() << "" << endl; + auto os = vm->get_template_attribute("OS"); + auto uuid = os->vector_value("UUID"); + + if (!uuid.empty()) + { + file << "\t" << uuid << "" << endl; + } + // ------------------------------------------------------------------------ // CPU & Memory // ------------------------------------------------------------------------