Skip to content

Commit

Permalink
Merge pull request #3695 from canonical/default-mount-target-in-daemon
Browse files Browse the repository at this point in the history
[daemon] let daemon choose mount target if not specified
  • Loading branch information
ricab authored Oct 21, 2024
2 parents ea024df + a7b3f43 commit 316af44
Show file tree
Hide file tree
Showing 4 changed files with 13 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
7 changes: 5 additions & 2 deletions src/daemon/daemon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1924,7 +1924,10 @@ 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 q_target_path = path_entry.target_path().empty()
? MP_UTILS.default_mount_target(QString::fromStdString(request->source_path()))
: QDir::cleanPath(QString::fromStdString(path_entry.target_path()));
const auto target_path = q_target_path.toStdString();

auto it = operative_instances.find(name);
if (it == operative_instances.end())
Expand All @@ -1934,7 +1937,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 316af44

Please sign in to comment.