Skip to content

Commit

Permalink
[daemon] let daemon choose mount target if not specified
Browse files Browse the repository at this point in the history
  • Loading branch information
andrei-toterman committed Sep 25, 2024
1 parent a8725ac commit e056ee9
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 2 deletions.
2 changes: 2 additions & 0 deletions include/multipass/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,8 @@ class Utils : public Singleton<Utils>
virtual QString make_uuid(const std::optional<std::string>& seed = std::nullopt) const;
virtual void sleep_for(const std::chrono::milliseconds& ms) const;
virtual bool is_ipv4_valid(const std::string& ipv4) const;

virtual Path default_mount_target(const Path& source) const;
};
} // namespace multipass

Expand Down
8 changes: 6 additions & 2 deletions src/daemon/daemon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1912,7 +1912,11 @@ try // clang-format on
for (const auto& path_entry : request->target_paths())
{
const auto& name = path_entry.instance_name();
const auto target_path = QDir::cleanPath(QString::fromStdString(path_entry.target_path())).toStdString();
const auto requested_target_path = QDir::cleanPath(QString::fromStdString(path_entry.target_path()));
const auto q_target_path = requested_target_path.isEmpty()
? MP_UTILS.default_mount_target(QString::fromStdString(request->source_path()))
: requested_target_path;
const auto target_path = q_target_path.toStdString();

auto it = operative_instances.find(name);
if (it == operative_instances.end())
Expand All @@ -1922,7 +1926,7 @@ try // clang-format on
}
auto& vm = it->second;

if (mp::utils::invalid_target_path(QString::fromStdString(target_path)))
if (mp::utils::invalid_target_path(q_target_path))
{
add_fmt_to(errors, "unable to mount to \"{}\"", target_path);
continue;
Expand Down
5 changes: 5 additions & 0 deletions src/utils/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,11 @@ bool mp::Utils::is_ipv4_valid(const std::string& ipv4) const
return true;
}

mp::Path mp::Utils::default_mount_target(const Path& source) const
{
return source.isEmpty() ? "" : QDir{QDir::cleanPath(source)}.dirName().prepend("/home/ubuntu/");
}

auto mp::utils::find_bridge_with(const std::vector<mp::NetworkInterfaceInfo>& networks,
const std::string& target_network,
const std::string& bridge_type) -> std::optional<mp::NetworkInterfaceInfo>
Expand Down
1 change: 1 addition & 0 deletions tests/mock_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class MockUtils : public Utils
MOCK_METHOD(QString, make_uuid, (const std::optional<std::string>&), (const, override));
MOCK_METHOD(void, sleep_for, (const std::chrono::milliseconds&), (const, override));
MOCK_METHOD(bool, is_ipv4_valid, (const std::string& ipv4), (const, override));
MOCK_METHOD(Path, default_mount_target, (const Path& source), (const, override));

MP_MOCK_SINGLETON_BOILERPLATE(MockUtils, Utils);
};
Expand Down

0 comments on commit e056ee9

Please sign in to comment.