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

[snapshots] resolve instance dir in vm #3130

merged 35 commits into from
Sep 22, 2023

Conversation

sharder996
Copy link
Contributor

@sharder996 sharder996 commented Jun 19, 2023

The motivation behind this change is to allow a vm to resolve the directory in which it and all it's relevant files are stored, which is needed for snapshots and retrospectively, native mounts on hyperv instances.

In order to achieve this, the idea was to take away the responsibility of creating/deleting instance directories from the vm image vault and give it to the vm factory. This conceptually makes more sense as the factory is the class responsible for creating/removing vm resources in the first place and is also the place where platform/backend specific directories locations are resolved. The vm image vault then queries the factory for a instance specific directories and using it as a download directory.

As this is not a small architectural change, I want to get some feedback on this and improve it.

Public side of #549

@sharder996 sharder996 changed the title [snapshots] Resolve instance dir [snapshots] resolve instance dir in vm Jun 19, 2023
@ricab ricab self-requested a review June 20, 2023 10:33
@ricab
Copy link
Collaborator

ricab commented Jun 20, 2023

Hey @sharder996, it looks like this needs a rebase and conflict fix.

@sharder996 sharder996 force-pushed the resolve-instance-dir branch from 9365e06 to 7af15d3 Compare June 21, 2023 17:33
@ricab ricab force-pushed the resolve-instance-dir branch 2 times, most recently from 8757363 to 5220596 Compare June 23, 2023 23:05
@ricab
Copy link
Collaborator

ricab commented Jun 27, 2023

Hey @sharder996, I pushed a new commit to account for new tests that are now present after rebasing. However, there are still a few other tests that failed when I tried them locally:

  • ImageVault
    • returned_image_contains_instance_name
    • caches_prepared_images
    • remembers_prepared_images
  • QemuBackend
    • get_backend_directory_name_calls_qemu_platform

I am not sure if they broke with my rebase or if they were already failing. Would you be able to take a look, please? Thank you!

@sharder996
Copy link
Contributor Author

Ho @ricab, thanks for fixing those tests! When I created this PR, I had not prioritized fixing tests since I'm anticipating there being review changes which would require me to fix tests again.

@ricab
Copy link
Collaborator

ricab commented Jun 27, 2023

Ah OK, if it's not caused by my rebase I am happy.

@sharder996 sharder996 force-pushed the resolve-instance-dir branch 4 times, most recently from 1adce11 to fa3de91 Compare June 30, 2023 19:17
@ricab ricab force-pushed the resolve-instance-dir branch from fa3de91 to d9926b8 Compare July 10, 2023 18:31
Copy link
Collaborator

@ricab ricab left a comment

Choose a reason for hiding this comment

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

Alright, thanks @sharder996! It took me a little to wrap my head around this one, but I think I finally formed a cohesive opinion...

This is tricky stuff but you made good progress here. I understand the option for creating the directory where it is derived (in get_instance_directory_name()), but I am not sure it's worth all the additional trips to disk (even to remove resources, the code tries to create the directory before removing it). We've been operating without that safety net for a long time and if we failed to create the directory now, that would be quickly noticed.

IIUC, the instance directory needs to be created only before it is used, i.e.:

  1. when fetching, just before placing the instance image there
  2. when launching with LXD (which does not place the image there), just before calling the instance constructor
  3. when starting up the daemon with LXD, on creation of pre-existing (LXD) VMs that still do not have a directory, just before calling the instance constructor (only needed the first time after upgrading)

Additionally, I don't see a need to create parent directories separately:

  • .../vault is the vault's business
  • .../vault/instances is not needed until there is an instance
  • creating .../vault/instances/<instance> with MP_UTILS.make_dir creates those parents

Therefore, it seems to me that, strictly speaking, the only new place we need to create a directory is in LXDVMFactory::create_vm(). Other backends already require the image to be in place when creating the VM, whichever way it got there (DefaultVMImageVault today, clone tomorrow, etc.). So there is no need to create the directory in those cases.

If this all makes sense to you, I think you could still keep the instance directory derivation, parameters, and fields where you added them, but actually create the directory only in DefaultVMImageVault, as before (image_instance_from and extract_image_from) and in the LXD factory.

WDYT?

include/multipass/virtual_machine.h Outdated Show resolved Hide resolved
include/multipass/vm_image_vault.h Outdated Show resolved Hide resolved
include/multipass/virtual_machine_factory.h Outdated Show resolved Hide resolved
include/multipass/virtual_machine.h Outdated Show resolved Hide resolved
src/platform/backends/shared/base_virtual_machine.h Outdated Show resolved Hide resolved
src/platform/backends/lxd/lxd_vm_image_vault.cpp Outdated Show resolved Hide resolved
src/platform/backends/lxd/lxd_virtual_machine_factory.cpp Outdated Show resolved Hide resolved
@sharder996 sharder996 force-pushed the resolve-instance-dir branch from b1ef49d to c505a13 Compare August 30, 2023 13:56
@sharder996 sharder996 force-pushed the resolve-instance-dir branch from 557c0ad to 54faf5f Compare August 31, 2023 03:57
@ricab
Copy link
Collaborator

ricab commented Sep 5, 2023

Hey @sharder996, is this ready for another pass?

@codecov
Copy link

codecov bot commented Sep 14, 2023

Codecov Report

Merging #3130 (463ef1c) into snapshots (7546c56) will increase coverage by 0.15%.
Report is 21 commits behind head on snapshots.
The diff coverage is 70.64%.

@@              Coverage Diff              @@
##           snapshots    #3130      +/-   ##
=============================================
+ Coverage      84.61%   84.77%   +0.15%     
=============================================
  Files            247      247              
  Lines          13107    13228     +121     
=============================================
+ Hits           11091    11214     +123     
+ Misses          2016     2014       -2     
Files Changed Coverage Δ
include/multipass/utils.h 100.00% <ø> (ø)
include/multipass/virtual_machine_factory.h 100.00% <ø> (ø)
include/multipass/vm_image_vault.h 100.00% <ø> (ø)
src/platform/backends/lxd/lxd_vm_image_vault.cpp 94.54% <ø> (ø)
...rc/platform/backends/shared/base_virtual_machine.h 0.00% <ø> (ø)
.../platform/backends/shared/base_virtual_machine.cpp 8.39% <18.51%> (+1.06%) ⬆️
src/daemon/daemon.cpp 68.58% <71.42%> (+0.14%) ⬆️
src/daemon/default_vm_image_vault.cpp 87.38% <76.19%> (+0.99%) ⬆️
...ckends/libvirt/libvirt_virtual_machine_factory.cpp 62.66% <83.33%> (+1.02%) ⬆️
include/multipass/virtual_machine.h 100.00% <100.00%> (ø)
... and 11 more

... and 9 files with indirect coverage changes

src/daemon/default_vm_image_vault.cpp Outdated Show resolved Hide resolved
tests/blueprint_test_lambdas.cpp Outdated Show resolved Hide resolved
tests/lxd/test_lxd_image_vault.cpp Outdated Show resolved Hide resolved
tests/test_image_vault.cpp Outdated Show resolved Hide resolved
include/multipass/vm_image_vault.h Outdated Show resolved Hide resolved
Remove artificial constructors from base VM and factory classes. Let
stub classes pass in stub arguments when needed.
Copy link
Collaborator

@ricab ricab left a comment

Choose a reason for hiding this comment

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

OK, this is getting there, only a few loose ends left to tie (please see comments above too).

src/platform/backends/lxd/lxd_virtual_machine_factory.cpp Outdated Show resolved Hide resolved
include/multipass/virtual_machine_factory.h Outdated Show resolved Hide resolved
src/daemon/daemon.cpp Show resolved Hide resolved
src/daemon/daemon.cpp Show resolved Hide resolved
src/platform/backends/lxd/lxd_virtual_machine_factory.cpp Outdated Show resolved Hide resolved
Copy link
Collaborator

@ricab ricab left a comment

Choose a reason for hiding this comment

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

Getting real close, but something is fishy now:

$ multipass launch -n a
Launched: a
$ ll /var/snap/multipass/common/data/multipassd/vault/instances/a/
total 703784
drwxr-xr-x 3 root root      4096 set 20 11:43 .
drwxr-xr-x 3 root root      4096 set 20 11:43 ..
drwxr-xr-x 2 root root      4096 set 20 11:43 a
-rw-r--r-- 1 root root     53248 set 20 11:43 cloud-init-config.iso
-rw-r--r-- 1 root root 720699392 set 20 11:46 ubuntu-22.04-server-cloudimg-amd64.img

Notice the extra "a" subdir.

src/utils/utils.cpp Outdated Show resolved Hide resolved
@@ -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!

Copy link
Collaborator

@ricab ricab left a comment

Choose a reason for hiding this comment

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

Great Scott :} Looking good on this side, so approving but postponing merging until the other side is reviewed too.

@ricab ricab merged commit 7a58568 into snapshots Sep 22, 2023
11 of 13 checks passed
@bors bors bot deleted the resolve-instance-dir branch September 22, 2023 18:29
ricab added a commit that referenced this pull request Oct 25, 2023
[snapshots] resolve instance dir in vm

r=ricab a=sharder996

Co-authored-by: ScottH <[email protected]>
ricab added a commit that referenced this pull request Oct 30, 2023
[snapshots] resolve instance dir in vm

r=ricab a=sharder996

Co-authored-by: ScottH <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants