Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[snapshots] resolve instance dir in vm #3130

Merged
merged 35 commits into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
0da1f3d
[image-vault] drive by fix pass by value
sharder996 Jun 1, 2023
6cc1de0
[image-vault] drive by move functions in unnamed namespace together
sharder996 Jun 1, 2023
0180540
[vm] have vms be aware of where they are stored
sharder996 Jun 16, 2023
44746a7
[vm] reference internally stored instance directory location
sharder996 Jun 16, 2023
9fd4194
[vm factory] drive by fix add const modifier
sharder996 Jun 16, 2023
1f1b9e9
[vm factory] give responsibility of instances dir to vm factory
sharder996 Jun 16, 2023
fe9634f
[vm factory] provide external access to specific vm instance directory
sharder996 Jun 16, 2023
05f9156
[vm factory] populate vm instance directory on creation
sharder996 Jun 16, 2023
7d79236
[vm factory] give responsibility of removing instance dir on deletion…
sharder996 Jun 16, 2023
e68df45
[image vault] have image vault reference factory for instance directo…
sharder996 Jun 19, 2023
f167348
[tests] Adapt LXD mount tests to instance_dir
ricab Jun 27, 2023
a03e950
[image vault] rename parameter
sharder996 Aug 31, 2023
111f021
[vm factory] move common code to base class
sharder996 Aug 31, 2023
7552d76
[vm] remove extra constructor
sharder996 Aug 31, 2023
57868c2
[vm image vault] only create vm directory when required
sharder996 Aug 31, 2023
b15cc69
[vm] make instance_dir protected
sharder996 Aug 31, 2023
dec0b4d
[vm factory] use mp::Path for clarity on return parameter usage
sharder996 Aug 31, 2023
54faf5f
[vm factory] make constructor explicit
sharder996 Aug 31, 2023
30cbc96
[vm factory] use virtual{,-impl} architecture to remove duplicate fun…
sharder996 Sep 13, 2023
35c466c
[qemu] Fix circular dependency in initialization
ricab Sep 14, 2023
9ff29e0
[qemu] Remove unnecessary `explicit`
ricab Sep 14, 2023
cf88eef
[qemu] Make QemuPlatform::get_directory_name const
ricab Sep 14, 2023
84ac031
[tests] update tests to properly reference an instance's directory an…
sharder996 Sep 14, 2023
ff13fbc
[vm factory] make instance variable private
sharder996 Sep 14, 2023
04a4d7b
[lxd vm image vault] label unused parameter
sharder996 Sep 14, 2023
2020333
linting changes
sharder996 Sep 14, 2023
8e50791
[vms] Remove artificial constructors for tests
ricab Sep 18, 2023
3029016
[tests] Use a temporary dir in remaining stub VMs
ricab Sep 18, 2023
4788cf3
rename param to be more descriptive
sharder996 Sep 19, 2023
8a3f6a0
[lxd] edit log string to be more descriptive
sharder996 Sep 19, 2023
373a23d
remove unnecessary comment
sharder996 Sep 19, 2023
feb86ea
[vm factory] rename function
sharder996 Sep 19, 2023
33af0f6
[vm factory] extract instance directory derivation into util function
sharder996 Sep 20, 2023
400fbb3
[vm image vault] correct instance directory creation location
sharder996 Sep 20, 2023
463ef1c
[utils] avoid hardcoded directory separators
sharder996 Sep 20, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions include/multipass/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@ class Utils : public Singleton<Utils>
// virtual machine helpers
virtual void wait_for_cloud_init(VirtualMachine* virtual_machine, std::chrono::milliseconds timeout,
const SSHKeyProvider& key_provider) const;
virtual Path derive_instances_dir(const Path& data_dir, const Path& backend_directory_name,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect, well done adding this!

const Path& instances_subdir) const;

// system info helpers
virtual std::string get_kernel_version() const;
Expand Down
3 changes: 1 addition & 2 deletions include/multipass/virtual_machine_factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ class VirtualMachineFactory : private DisabledCopyMove
virtual void prepare_instance_image(const VMImage& instance_image, const VirtualMachineDescription& desc) = 0;
virtual void hypervisor_health_check() = 0;
virtual QString get_backend_directory_name() const = 0;
virtual Path
get_instance_directory_name(const std::string& name) const = 0; // postcondition: return directory exists
virtual Path get_instance_directory(const std::string& name) const = 0;
virtual QString get_backend_version_string() const = 0;
virtual VMImageVault::UPtr create_image_vault(std::vector<VMImageHost*> image_hosts, URLDownloader* downloader,
const Path& cache_dir_path, const Path& data_dir_path,
Expand Down
4 changes: 2 additions & 2 deletions src/daemon/daemon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ auto fetch_image_for(const std::string& name, mp::VirtualMachineFactory& factory
mp::Query query{name, "", false, "", mp::Query::Type::Alias, false};

return vault.fetch_image(factory.fetch_type(), query, stub_prepare, stub_progress, false, std::nullopt,
factory.get_instance_directory_name(name));
factory.get_instance_directory(name));
}

auto try_mem_size(const std::string& val) -> std::optional<mp::MemorySize>
Expand Down Expand Up @@ -2917,7 +2917,7 @@ void mp::Daemon::create_vm(const CreateRequest* request,

auto vm_image =
config->vault->fetch_image(fetch_type, query, prepare_action, progress_monitor, launch_from_blueprint,
checksum, config->factory->get_instance_directory_name(name));
checksum, config->factory->get_instance_directory(name));

const auto image_size = config->vault->minimum_image_size_for(vm_image.id);
vm_desc.disk_space = compute_final_image_size(
Expand Down
12 changes: 6 additions & 6 deletions src/daemon/default_vm_image_vault.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ mp::DefaultVMImageVault::~DefaultVMImageVault()
mp::VMImage mp::DefaultVMImageVault::fetch_image(const FetchType& fetch_type, const Query& query,
const PrepareAction& prepare, const ProgressMonitor& monitor,
const bool unlock, const std::optional<std::string>& checksum,
const mp::Path& download_dir)
const mp::Path& save_dir)
{
{
std::lock_guard<decltype(fetch_mutex)> lock{fetch_mutex};
Expand Down Expand Up @@ -282,11 +282,11 @@ mp::VMImage mp::DefaultVMImageVault::fetch_image(const FetchType& fetch_type, co

if (source_image.image_path.endsWith(".xz"))
{
source_image.image_path = extract_image_from(query.name, source_image, monitor, download_dir);
source_image.image_path = extract_image_from(query.name, source_image, monitor, save_dir);
}
else
{
source_image = image_instance_from(query.name, source_image, download_dir);
source_image = image_instance_from(query.name, source_image, save_dir);
}

vm_image = prepare(source_image);
Expand Down Expand Up @@ -327,7 +327,7 @@ mp::VMImage mp::DefaultVMImageVault::fetch_image(const FetchType& fetch_type, co

if (last_modified.isValid() && (last_modified.toString().toStdString() == record.image.release_date))
{
return finalize_image_records(query, record.image, id, download_dir);
return finalize_image_records(query, record.image, id, save_dir);
}
}

Expand Down Expand Up @@ -389,7 +389,7 @@ mp::VMImage mp::DefaultVMImageVault::fetch_image(const FetchType& fetch_type, co
const auto prepared_image = record.second.image;
try
{
return finalize_image_records(query, prepared_image, record.first, download_dir);
return finalize_image_records(query, prepared_image, record.first, save_dir);
}
catch (const std::exception& e)
{
Expand Down Expand Up @@ -427,7 +427,7 @@ mp::VMImage mp::DefaultVMImageVault::fetch_image(const FetchType& fetch_type, co
auto prepared_image = future.result();
std::lock_guard<decltype(fetch_mutex)> lock{fetch_mutex};
in_progress_image_fetches.erase(id);
return finalize_image_records(query, prepared_image, id, download_dir);
return finalize_image_records(query, prepared_image, id, save_dir);
}
catch (const std::exception&)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ auto make_libvirt_wrapper(const std::string& libvirt_object_path)

mp::LibVirtVirtualMachineFactory::LibVirtVirtualMachineFactory(const mp::Path& data_dir,
const std::string& libvirt_object_path)
: BaseVirtualMachineFactory(QDir(data_dir, get_backend_directory_name()).filePath("vault/instances")),
: BaseVirtualMachineFactory(
MP_UTILS.derive_instances_dir(data_dir, get_backend_directory_name(), instances_subdir)),
libvirt_wrapper{make_libvirt_wrapper(libvirt_object_path)},
data_dir{data_dir},
bridge_name{enable_libvirt_network(data_dir, libvirt_wrapper)},
Expand All @@ -128,7 +129,7 @@ mp::VirtualMachine::UPtr mp::LibVirtVirtualMachineFactory::create_virtual_machin
bridge_name = enable_libvirt_network(data_dir, libvirt_wrapper);

return std::make_unique<mp::LibVirtVirtualMachine>(desc, bridge_name, monitor, libvirt_wrapper,
get_instance_directory_name(desc.vm_name));
get_instance_directory(desc.vm_name));
}

mp::LibVirtVirtualMachineFactory::~LibVirtVirtualMachineFactory()
Expand Down
8 changes: 4 additions & 4 deletions src/platform/backends/lxd/lxd_virtual_machine_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,12 @@ mp::NetworkInterfaceInfo munch_network(std::map<std::string, mp::NetworkInterfac

return ret;
}

} // namespace

mp::LXDVirtualMachineFactory::LXDVirtualMachineFactory(NetworkAccessManager::UPtr manager, const mp::Path& data_dir,
const QUrl& base_url)
: BaseVirtualMachineFactory(MP_UTILS.make_dir(data_dir, get_backend_directory_name())),
: BaseVirtualMachineFactory(
MP_UTILS.derive_instances_dir(data_dir, get_backend_directory_name(), instances_subdir)),
manager{std::move(manager)},
base_url{base_url}
{
Expand All @@ -103,12 +103,12 @@ mp::VirtualMachine::UPtr mp::LXDVirtualMachineFactory::create_virtual_machine(co
{
return std::make_unique<mp::LXDVirtualMachine>(desc, monitor, manager.get(), base_url, multipass_bridge_name,
storage_pool,
MP_UTILS.make_dir(get_instance_directory_name(desc.vm_name)));
MP_UTILS.make_dir(get_instance_directory(desc.vm_name)));
}

void mp::LXDVirtualMachineFactory::remove_resources_for_impl(const std::string& name)
{
mpl::log(mpl::Level::trace, category, fmt::format("No resources to remove for \"{}\"", name));
mpl::log(mpl::Level::trace, category, fmt::format("No further resources to remove for \"{}\"", name));
}

auto mp::LXDVirtualMachineFactory::prepare_source_image(const VMImage& source_image) -> VMImage
Expand Down
2 changes: 0 additions & 2 deletions src/platform/backends/lxd/lxd_virtual_machine_factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@ class LXDVirtualMachineFactory : public BaseVirtualMachineFactory

protected:
void remove_resources_for_impl(const std::string& name) override;

protected:
std::string create_bridge_with(const NetworkInterfaceInfo& interface) override;

private:
Expand Down
10 changes: 4 additions & 6 deletions src/platform/backends/qemu/qemu_virtual_machine_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,6 @@ namespace mpl = multipass::logging;
namespace
{
constexpr auto category = "qemu factory";
mp::Path derive_instances_dir(const mp::QemuPlatform& qemu_platform, const mp::Path& data_dir)
{
return QDir(data_dir, qemu_platform.get_directory_name()).filePath("vault/instances");
}
} // namespace

mp::QemuVirtualMachineFactory::QemuVirtualMachineFactory(const mp::Path& data_dir)
Expand All @@ -46,15 +42,17 @@ mp::QemuVirtualMachineFactory::QemuVirtualMachineFactory(const mp::Path& data_di
}

mp::QemuVirtualMachineFactory::QemuVirtualMachineFactory(QemuPlatform::UPtr qemu_platform, const mp::Path& data_dir)
: BaseVirtualMachineFactory(derive_instances_dir(*qemu_platform, data_dir)), qemu_platform{std::move(qemu_platform)}
: BaseVirtualMachineFactory(
MP_UTILS.derive_instances_dir(data_dir, qemu_platform->get_directory_name(), instances_subdir)),
qemu_platform{std::move(qemu_platform)}
{
}

mp::VirtualMachine::UPtr mp::QemuVirtualMachineFactory::create_virtual_machine(const VirtualMachineDescription& desc,
VMStatusMonitor& monitor)
{
return std::make_unique<mp::QemuVirtualMachine>(desc, qemu_platform.get(), monitor,
MP_UTILS.make_dir(get_instance_directory_name(desc.vm_name)));
get_instance_directory(desc.vm_name));
}

void mp::QemuVirtualMachineFactory::remove_resources_for_impl(const std::string& name)
Expand Down
2 changes: 2 additions & 0 deletions src/platform/backends/shared/base_virtual_machine_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ auto find_bridge_with(const NetworkContainer& networks, const std::string& membe
}
} // namespace

const mp::Path mp::BaseVirtualMachineFactory::instances_subdir = "vault/instances";

mp::BaseVirtualMachineFactory::BaseVirtualMachineFactory(const Path& instances_dir) : instances_dir{instances_dir} {};

void mp::BaseVirtualMachineFactory::configure(VirtualMachineDescription& vm_desc)
Expand Down
7 changes: 5 additions & 2 deletions src/platform/backends/shared/base_virtual_machine_factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class BaseVirtualMachineFactory : public VirtualMachineFactory
return {};
};

Path get_instance_directory_name(const std::string& name) const override
Path get_instance_directory(const std::string& name) const override
{
return multipass::utils::backend_directory_path(instances_dir, QString::fromStdString(name));
}
Expand All @@ -73,6 +73,9 @@ class BaseVirtualMachineFactory : public VirtualMachineFactory
throw NotImplementedOnThisBackendException("networks");
};

protected:
static const Path instances_subdir;

protected:
std::string create_bridge_with(const NetworkInterfaceInfo& interface) override
{
Expand All @@ -95,7 +98,7 @@ class BaseVirtualMachineFactory : public VirtualMachineFactory
inline void multipass::BaseVirtualMachineFactory::remove_resources_for(const std::string& name)
{
remove_resources_for_impl(name);
QDir instance_dir{get_instance_directory_name(name)};
QDir instance_dir{get_instance_directory(name)};
instance_dir.removeRecursively();
}

Expand Down
9 changes: 9 additions & 0 deletions src/utils/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -674,3 +674,12 @@ void mp::utils::set_owner_for(mp::SSHSession& session, const std::string& root,
fmt::format("sudo /bin/bash -c 'cd \"{}\" && chown -R {}:{} \"{}\"'", root, vm_user,
vm_group, relative_target.substr(0, relative_target.find_first_of('/'))));
}

mp::Path mp::Utils::derive_instances_dir(const mp::Path& data_dir, const mp::Path& backend_directory_name,
const mp::Path& instances_subdir) const
{
if (backend_directory_name.isEmpty())
return QDir(data_dir).filePath(instances_subdir);
else
return QDir(data_dir).filePath(QString("%1/%2").arg(backend_directory_name).arg(instances_subdir));
ricab marked this conversation as resolved.
Show resolved Hide resolved
}
4 changes: 2 additions & 2 deletions tests/blueprint_test_lambdas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ mpt::fetch_image_lambda(const std::string& release, const std::string& remote, c
return [&release, &remote, must_have_checksum](
const mp::FetchType& fetch_type, const mp::Query& query, const mp::VMImageVault::PrepareAction& prepare,
const mp::ProgressMonitor& monitor, const bool unlock, const std::optional<std::string>& checksum,
const mp::Path& download_dir) {
const mp::Path& save_dir) {
EXPECT_EQ(query.release, release);
if (remote.empty())
{
Expand All @@ -57,7 +57,7 @@ mpt::fetch_image_lambda(const std::string& release, const std::string& remote, c
EXPECT_NE(checksum, std::nullopt);
}

return mpt::StubVMImageVault().fetch_image(fetch_type, query, prepare, monitor, unlock, checksum, download_dir);
return mpt::StubVMImageVault().fetch_image(fetch_type, query, prepare, monitor, unlock, checksum, save_dir);
};
}

Expand Down
8 changes: 4 additions & 4 deletions tests/lxd/test_lxd_backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -958,10 +958,10 @@ TEST_F(LXDBackend, unimplemented_functions_logs_trace_message)

const std::string name{"foo"};

EXPECT_CALL(
*logger_scope.mock_logger,
log(Eq(mpl::Level::trace), mpt::MockLogger::make_cstring_matcher(StrEq("lxd factory")),
mpt::MockLogger::make_cstring_matcher(StrEq(fmt::format("No resources to remove for \"{}\"", name)))));
EXPECT_CALL(*logger_scope.mock_logger,
log(Eq(mpl::Level::trace), mpt::MockLogger::make_cstring_matcher(StrEq("lxd factory")),
mpt::MockLogger::make_cstring_matcher(
StrEq(fmt::format("No further resources to remove for \"{}\"", name)))));

EXPECT_CALL(*logger_scope.mock_logger,
log(Eq(mpl::Level::trace), mpt::MockLogger::make_cstring_matcher(StrEq("lxd factory")),
Expand Down
Loading