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

runInLinuxVM, test-driver: disable vmx cpu feature for guest vm #83920

Closed

Conversation

offlinehacker
Copy link
Contributor

Passing vmx (virtualization extensions) to guest vm can cause
issues if build is running inside nested vm.

Motivation for this change

When i was trying to run build inside VM with enabled nested virtualization, build failed as there was error with passing vmx extensions. This patch disables virtualization extension in qemu based tests. As far as i am aware no nixos tests require vmx extensions, as before we did not have CPU set to host.

The other concern i have is if changing cpu to host is good, as it can make build non deterministic and result in crashes like i had.

Things done
  • Tested using sandboxing (nix.useSandbox on NixOS, or option sandbox in nix.conf on non-NixOS linux)
  • Built on platform(s)
    • NixOS
    • macOS
    • other Linux distributions
  • Tested via one or more NixOS test(s) if existing and applicable for the change (look inside nixos/tests)
  • Tested compilation of all pkgs that depend on this change using nix-shell -p nixpkgs-review --run "nixpkgs-review wip"
  • Tested execution of all binary files (usually in ./result/bin/)
  • Determined the impact on package closure size (by running nix path-info -S before and after)
  • Ensured that relevant documentation is up to date
  • Fits CONTRIBUTING.md.

Passing vmx (virtualization extensions) to guest vm can cause
issues if build is running inside nested vm.
Copy link
Member

@Mic92 Mic92 left a comment

Choose a reason for hiding this comment

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

Our builds also run with native CPU features so that runInLinuxVM is not that much of a regression. Otherwise the change looks good to me.

@@ -74,7 +74,7 @@ let
throw "Non-EFI boot methods are only supported on i686 / x86_64"
else ''
def assemble_qemu_flags():
flags = "-cpu host"
flags = "-cpu host,-vmx"
Copy link
Contributor

Choose a reason for hiding this comment

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

not sure if this is a problem, but this passes -vmx also on non-x86 platforms. Does that even work then?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Have to check that one, thanks!

@offlinehacker
Copy link
Contributor Author

@volth I am thinking enabling CPU features individually might be better, or that maybe tests can redefine CPU it requires. This way may achieve greater compatibility and reproducibility.

@PhDyellow
Copy link

PhDyellow commented Apr 17, 2020

I came across this issue while trying to build a singularity container under nixos-unstable.
I am running NixOS in a Virtualbox VM, with Windows as the host. To build the singularity container, I used nix build on a .nix file with a pinned version of nixpkgs.

I had the issue described in issuse #85394
I cherry picked commits with "host,-vmx", and had the same issue.
Reverting to "kvm64" worked.

@offlinehacker
Copy link
Contributor Author

But as I see you had issues, as your build requires nested virtualization? I think CPU should default to qemu default, as this is most compatible, and then passing additional required cpu features. Looks like what we need is avx and vmx extensions. I was thinking to make improved pull request with support for these optional CPU features. @volth What do you think would be sane default and which additional CPU features are needed?

@PhDyellow
Copy link

Yes, my build required nested virtualization. singularity-tools uses a VM to build the container, so calling nix build was creating a guest within a guest.

@nixos-discourse
Copy link

This pull request has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/new-constraint-between-release-19-09-and-20-03-in-package-qemu/6297/4

@mrschyte
Copy link

mrschyte commented May 4, 2020

In my opinion the presence of vmx should be autodetected. The check can be as simple as grep 'flags.*vmx' /proc/cpuinfo. It is possible to enable nested virtualization with kvm and vmx may be needed for some use cases.

@offlinehacker
Copy link
Contributor Author

It is already auto detected by qemu, but the thing is it can crash on different qemu/kvm (kernel) versions in some circumstances, like I had. I think extended CPU features should be explicitly enabled, as host CPU can vary and can result in unexpected behavior.

cransom pushed a commit to dailykos/nixpkgs that referenced this pull request May 6, 2020
matthewbauer added a commit to matthewbauer/nixiosk that referenced this pull request Aug 20, 2020
matthewbauer added a commit to matthewbauer/nixpkgs that referenced this pull request Aug 22, 2020
This appears to avoid requiring KVM when it’s not available. This is
what I originally though -cpu host did. Unfortunately not much
documentation available from the QEMU side on this, but this appears
to square with help:

$ qemu-system-x86 -cpu help
...
x86 host                  KVM processor with all supported host features
x86 max                   Enables all features supported by the accelerator in the current host
...

Whether we actually want to support this not clear, since this only
happens when your CPU doesn’t have full KVM support. Some Nix builders
are lying about kvm support though. Things aren’t too slow without it
though.

Fixes NixOS#85394

Alternative to NixOS#83920
matthewbauer added a commit to matthewbauer/nixpkgs that referenced this pull request Aug 22, 2020
This appears to avoid requiring KVM when it’s not available. This is
what I originally though -cpu host did. Unfortunately not much
documentation available from the QEMU side on this, but this appears
to square with help:

$ qemu-system-x86 -cpu help
...
x86 host                  KVM processor with all supported host features
x86 max                   Enables all features supported by the accelerator in the current host
...

Whether we actually want to support this not clear, since this only
happens when your CPU doesn’t have full KVM support. Some Nix builders
are lying about kvm support though. Things aren’t too slow without it
though.

Fixes NixOS#85394

Alternative to NixOS#83920
matthewbauer added a commit to matthewbauer/nixpkgs that referenced this pull request Feb 25, 2021
This appears to avoid requiring KVM when it’s not available. This is
what I originally though -cpu host did. Unfortunately not much
documentation available from the QEMU side on this, but this appears
to square with help:

$ qemu-system-x86 -cpu help
...
x86 host                  KVM processor with all supported host features
x86 max                   Enables all features supported by the accelerator in the current host
...

Whether we actually want to support this not clear, since this only
happens when your CPU doesn’t have full KVM support. Some Nix builders
are lying about kvm support though. Things aren’t too slow without it
though.

Fixes NixOS#85394

Alternative to NixOS#83920

(cherry picked from commit 47b56e7)
@stale
Copy link

stale bot commented Apr 7, 2021

I marked this as stale due to inactivity. → More info

@stale stale bot added the 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md label Apr 7, 2021
@kvtb
Copy link
Contributor

kvtb commented May 17, 2021

Since #95956 is has no sense: -cpu max implies vmx=on

Also

virtualisation.qemu.options =
if useKvmNestedVirt then ["-cpu" "kvm64,vmx=on"] else [];

is meaningless, vmx=on whenever useKvmNestedVirt is true or false

@stale stale bot removed the 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md label May 17, 2021
@stale
Copy link

stale bot commented Nov 16, 2021

I marked this as stale due to inactivity. → More info

@stale stale bot added the 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md label Nov 16, 2021
@anmonteiro
Copy link
Member

Has this been fixed by #95956 ?

@stale stale bot removed the 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md label Dec 15, 2021
@stale stale bot added the 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md label Jun 19, 2022
@stale stale bot removed the 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md label Jun 26, 2022
@Artturin Artturin closed this Jan 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet