Skip to content

Commit 5707e3a

Browse files
committed
[hyperv-hcs] replace resize_memory with modify_compute_system
1 parent 1c410f5 commit 5707e3a

11 files changed

+91
-56
lines changed

src/platform/backends/hyperv_api/hcs/hyperv_hcs_api_wrapper.cpp

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -310,25 +310,6 @@ OperationResult HCSWrapper::resume_compute_system(const std::string& compute_sys
310310

311311
// ---------------------------------------------------------
312312

313-
OperationResult HCSWrapper::resize_memory(const std::string& compute_system_name, std::uint32_t new_size_mib) const
314-
{
315-
// Machine must be booted up.
316-
mpl::debug(kLogCategory, "resize_memory(...) > name: ({}), new_size_mb: ({})", compute_system_name, new_size_mib);
317-
// https://learn.microsoft.com/en-us/virtualization/api/hcs/reference/hcsmodifycomputesystem#remarks
318-
constexpr auto resize_memory_settings_template = LR"(
319-
{{
320-
"ResourcePath": "VirtualMachine/ComputeTopology/Memory/SizeInMB",
321-
"RequestType": "Update",
322-
"Settings": {0}
323-
}})";
324-
325-
const auto settings = fmt::format(resize_memory_settings_template, new_size_mib);
326-
327-
return perform_hcs_operation(api, api.ModifyComputeSystem, compute_system_name, settings.c_str(), nullptr);
328-
}
329-
330-
// ---------------------------------------------------------
331-
332313
OperationResult HCSWrapper::update_cpu_count(const std::string& compute_system_name, std::uint32_t new_vcpu_count) const
333314
{
334315
return OperationResult{E_NOTIMPL, L"Not implemented yet!"};

src/platform/backends/hyperv_api/hcs/hyperv_hcs_api_wrapper.h

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -160,20 +160,6 @@ struct HCSWrapper : public HCSWrapperInterface
160160

161161
// ---------------------------------------------------------
162162

163-
/**
164-
* Resize the amount of memory the compute system has.
165-
*
166-
* @param compute_system_name Target compute system name
167-
* @param new_size_mib New memory size, in megabytes
168-
*
169-
* @return An object that evaluates to true on success, false otherwise.
170-
* message() may contain details of failure when result is false.
171-
*/
172-
[[nodiscard]] OperationResult resize_memory(const std::string& compute_system_name,
173-
std::uint32_t new_size_mib) const override;
174-
175-
// ---------------------------------------------------------
176-
177163
/**
178164
* Change the amount of available vCPUs in the compute system
179165
*
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright (C) Canonical, Ltd.
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation; version 3.
7+
*
8+
* This program is distributed in the hope that it will be useful,
9+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
* GNU General Public License for more details.
12+
*
13+
* You should have received a copy of the GNU General Public License
14+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
15+
*
16+
*/
17+
18+
#ifndef MULTIPASS_HYPERV_API_HCS_MODIFY_MEMORY_SETTINGS_H
19+
#define MULTIPASS_HYPERV_API_HCS_MODIFY_MEMORY_SETTINGS_H
20+
21+
#include <fmt/format.h>
22+
23+
#include <cstdint>
24+
25+
namespace multipass::hyperv::hcs
26+
{
27+
28+
struct HcsModifyMemorySettings
29+
{
30+
std::uint32_t size_in_mb{0};
31+
};
32+
33+
} // namespace multipass::hyperv::hcs
34+
35+
#endif

src/platform/backends/hyperv_api/hcs/hyperv_hcs_request.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <fmt/std.h>
2222

2323
using multipass::hyperv::maybe_widen;
24+
using multipass::hyperv::hcs::HcsModifyMemorySettings;
2425
using multipass::hyperv::hcs::HcsNetworkAdapter;
2526
using multipass::hyperv::hcs::HcsRequest;
2627

@@ -47,6 +48,15 @@ struct HcsRequestSettingsFormatters
4748
maybe_widen{params.endpoint_guid},
4849
maybe_widen{params.mac_address});
4950
}
51+
52+
auto operator()(const HcsModifyMemorySettings& params)
53+
{
54+
constexpr static auto json_template = MULTIPASS_UNIVERSAL_LITERAL(R"json(
55+
{0}
56+
)json");
57+
58+
return fmt::format(json_template.as<Char>(), params.size_in_mb);
59+
}
5060
};
5161

5262
template <typename Char>

src/platform/backends/hyperv_api/hcs/hyperv_hcs_request.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#ifndef MULTIPASS_HYPERV_API_HCS_REQUEST_H
1919
#define MULTIPASS_HYPERV_API_HCS_REQUEST_H
2020

21+
#include <hyperv_api/hcs/hyperv_hcs_modify_memory_settings.h>
2122
#include <hyperv_api/hcs/hyperv_hcs_network_adapter.h>
2223
#include <hyperv_api/hcs/hyperv_hcs_request_type.h>
2324
#include <hyperv_api/hcs/hyperv_hcs_resource_path.h>
@@ -36,7 +37,7 @@ struct HcsRequest
3637
{
3738
HcsResourcePath resource_path;
3839
HcsRequestType request_type;
39-
std::variant<std::monostate, HcsNetworkAdapter> settings;
40+
std::variant<std::monostate, HcsNetworkAdapter, HcsModifyMemorySettings> settings{std::monostate{}};
4041
};
4142

4243
} // namespace multipass::hyperv::hcs

src/platform/backends/hyperv_api/hcs/hyperv_hcs_request_type.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ struct HcsRequestType
4646
return HcsRequestType{"Remove"};
4747
}
4848

49+
constexpr static auto Update(){
50+
return HcsRequestType{"Update"};
51+
}
52+
4953
private:
5054
constexpr HcsRequestType(std::string_view v) : value(v)
5155
{

src/platform/backends/hyperv_api/hcs/hyperv_hcs_resource_path.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
#include <fmt/format.h>
2222

23-
#include <filesystem>
2423
#include <string>
2524
#include <string_view>
2625

@@ -41,8 +40,12 @@ struct HcsResourcePath
4140

4241
static HcsResourcePath NetworkAdapters(const std::string& network_adapter_id)
4342
{
44-
std::filesystem::path result{network_adapters / fmt::format("{{{0}}}", network_adapter_id)};
45-
return HcsResourcePath(result.generic_string());
43+
return fmt::format("VirtualMachine/Devices/NetworkAdapters/{{{0}}}", network_adapter_id);
44+
}
45+
46+
static HcsResourcePath Memory()
47+
{
48+
return std::string{"VirtualMachine/ComputeTopology/Memory/SizeInMB"};
4649
}
4750

4851
private:
@@ -51,9 +54,6 @@ struct HcsResourcePath
5154
}
5255

5356
std::string value{};
54-
inline static const std::filesystem::path root{"VirtualMachine"};
55-
inline static const std::filesystem::path devices{root / "Devices"};
56-
inline static const std::filesystem::path network_adapters{devices / "NetworkAdapters"};
5757
};
5858

5959
} // namespace multipass::hyperv::hcs

src/platform/backends/hyperv_api/hcs/hyperv_hcs_wrapper_interface.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,6 @@ struct HCSWrapperInterface
4747
const std::filesystem::path& file_path) const = 0;
4848
virtual OperationResult revoke_vm_access(const std::string& compute_system_name,
4949
const std::filesystem::path& file_path) const = 0;
50-
virtual OperationResult resize_memory(const std::string& compute_system_name,
51-
const std::uint32_t new_size_mib) const = 0;
5250
virtual OperationResult update_cpu_count(const std::string& compute_system_name,
5351
const std::uint32_t new_core_count) const = 0;
5452
virtual OperationResult get_compute_system_state(const std::string& compute_system_name,

src/platform/backends/hyperv_api/hcs_virtual_machine.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -520,15 +520,20 @@ void HCSVirtualMachine::update_cpus(int num_cores)
520520

521521
throw std::runtime_error{"Not yet implemented"};
522522
}
523+
523524
void HCSVirtualMachine::resize_memory(const MemorySize& new_size)
524525
{
525526
mpl::debug(kLogCategory,
526527
"resize_memory() -> called for VM `{}`, new_size `{}` MiB",
527528
vm_name,
528529
new_size.in_megabytes());
529-
530-
const auto& [status, status_msg] = hcs->resize_memory(vm_name, new_size.in_megabytes());
530+
hcs::HcsRequest req{hcs::HcsResourcePath::Memory(),
531+
hcs::HcsRequestType::Update(),
532+
hcs::HcsModifyMemorySettings{static_cast<std::uint32_t>(new_size.in_megabytes())}};
533+
hcs->modify_compute_system(vm_name, req);
534+
// FIXME: Log the result.
531535
}
536+
532537
void HCSVirtualMachine::resize_disk(const MemorySize& new_size)
533538
{
534539
mpl::debug(kLogCategory,

src/platform/backends/hyperv_api/hyperv_api_string_conversion.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,4 @@ struct fmt::formatter<multipass::hyperv::maybe_widen, Char>
9797
"" X, L"" X \
9898
}
9999

100-
#endif // MULTIPASS_HYPERV_API_STRING_CONVERSION_H
100+
#endif // MULTIPASS_HYPERV_API_STRING_CONVERSION_H

tests/hyperv_api/test_ut_hyperv_hcs_api.cpp

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2517,9 +2517,12 @@ TEST_F(HyperVHCSAPI_UnitTests, resize_memory_of_compute_system_happy_path)
25172517
generic_operation_happy_path<decltype(HcsModifyComputeSystem)>(
25182518
mock_api_table.ModifyComputeSystem,
25192519
[&](hyperv::hcs::HCSWrapper& wrapper) {
2520-
logger_scope.mock_logger->expect_log(mpl::Level::debug,
2521-
"resize_memory(...) > name: (test_vm), new_size_mb: (16384)");
2522-
return wrapper.resize_memory("test_vm", 16384);
2520+
logger_scope.mock_logger->expect_log(mpl::Level::debug, "modify_compute_system(...) > params");
2521+
hyperv::hcs::HcsRequest req{hyperv::hcs::HcsResourcePath::Memory(),
2522+
hyperv::hcs::HcsRequestType::Update(),
2523+
hyperv::hcs::HcsModifyMemorySettings{16384}};
2524+
2525+
return wrapper.modify_compute_system("test_vm", req);
25232526
},
25242527
[](HCS_SYSTEM computeSystem, HCS_OPERATION operation, PCWSTR configuration, HANDLE identity) {
25252528
ASSERT_EQ(mock_compute_system_object, computeSystem);
@@ -2537,8 +2540,11 @@ TEST_F(HyperVHCSAPI_UnitTests, resize_memory_of_compute_system_hcs_open_fail)
25372540
generic_operation_hcs_open_fail<decltype(HcsModifyComputeSystem)>(
25382541
mock_api_table.ModifyComputeSystem,
25392542
[&](hyperv::hcs::HCSWrapper& wrapper) {
2540-
logger_scope.mock_logger->expect_log(mpl::Level::debug, "resize_memory(...)");
2541-
return wrapper.resize_memory("test_vm", 16384);
2543+
logger_scope.mock_logger->expect_log(mpl::Level::debug, "modify_compute_system(...)");
2544+
hyperv::hcs::HcsRequest req{hyperv::hcs::HcsResourcePath::Memory(),
2545+
hyperv::hcs::HcsRequestType::Update(),
2546+
hyperv::hcs::HcsModifyMemorySettings{16384}};
2547+
return wrapper.modify_compute_system("test_vm", req);
25422548
});
25432549
}
25442550

@@ -2549,8 +2555,11 @@ TEST_F(HyperVHCSAPI_UnitTests, resize_memory_of_compute_system_create_operation_
25492555
generic_operation_create_operation_fail<decltype(HcsModifyComputeSystem)>(
25502556
mock_api_table.ModifyComputeSystem,
25512557
[&](hyperv::hcs::HCSWrapper& wrapper) {
2552-
logger_scope.mock_logger->expect_log(mpl::Level::debug, "resize_memory(...)");
2553-
return wrapper.resize_memory("test_vm", 16384);
2558+
logger_scope.mock_logger->expect_log(mpl::Level::debug, "modify_compute_system(...)");
2559+
hyperv::hcs::HcsRequest req{hyperv::hcs::HcsResourcePath::Memory(),
2560+
hyperv::hcs::HcsRequestType::Update(),
2561+
hyperv::hcs::HcsModifyMemorySettings{16384}};
2562+
return wrapper.modify_compute_system("test_vm", req);
25542563
});
25552564
}
25562565

@@ -2568,8 +2577,11 @@ TEST_F(HyperVHCSAPI_UnitTests, resize_memory_of_compute_system_fail)
25682577
generic_operation_fail<decltype(HcsModifyComputeSystem)>(
25692578
mock_api_table.ModifyComputeSystem,
25702579
[&](hyperv::hcs::HCSWrapper& wrapper) {
2571-
logger_scope.mock_logger->expect_log(mpl::Level::debug, "resize_memory(...)");
2572-
return wrapper.resize_memory("test_vm", 16384);
2580+
logger_scope.mock_logger->expect_log(mpl::Level::debug, "modify_compute_system(...)");
2581+
hyperv::hcs::HcsRequest req{hyperv::hcs::HcsResourcePath::Memory(),
2582+
hyperv::hcs::HcsRequestType::Update(),
2583+
hyperv::hcs::HcsModifyMemorySettings{16384}};
2584+
return wrapper.modify_compute_system("test_vm", req);
25732585
},
25742586
[](HCS_SYSTEM computeSystem, HCS_OPERATION operation, PCWSTR configuration, HANDLE identity) {
25752587
ASSERT_EQ(mock_compute_system_object, computeSystem);
@@ -2594,8 +2606,11 @@ TEST_F(HyperVHCSAPI_UnitTests, resize_memory_of_compute_system_wait_for_operatio
25942606
generic_operation_wait_for_operation_fail<decltype(HcsModifyComputeSystem)>(
25952607
mock_api_table.ModifyComputeSystem,
25962608
[&](hyperv::hcs::HCSWrapper& wrapper) {
2597-
logger_scope.mock_logger->expect_log(mpl::Level::debug, "resize_memory(...)");
2598-
return wrapper.resize_memory("test_vm", 16384);
2609+
logger_scope.mock_logger->expect_log(mpl::Level::debug, "modify_compute_system(...)");
2610+
hyperv::hcs::HcsRequest req{hyperv::hcs::HcsResourcePath::Memory(),
2611+
hyperv::hcs::HcsRequestType::Update(),
2612+
hyperv::hcs::HcsModifyMemorySettings{16384}};
2613+
return wrapper.modify_compute_system("test_vm", req);
25992614
},
26002615
[](HCS_SYSTEM computeSystem, HCS_OPERATION operation, PCWSTR configuration, HANDLE identity) {
26012616
ASSERT_EQ(mock_compute_system_object, computeSystem);

0 commit comments

Comments
 (0)