Cannot build Azure image on Ubuntu with `azure-new`: qemu-system-x86_64: CPU model 'host' requires KVM

This is almost the exact mirror of the Cannot build Docker image topic, but this is about building an Azure image with azure-new, and I also didn’t want to hijack that conversation if turns out to be unrelated.

Issue 1 (resolved)

On Ubuntu 18.04.4 LTS, the build would fail initially with

error: a 'x86_64-linux' with features {kvm} is required to build 
[store-path] but I am a 'x86_64-linux' 
with features {benchmark, big-parallel, nixos-test}

which got resolved by creating nix.conf as the following:

$ cat ~/.config/nix/nix.conf 
system-features = kvm

(As per Nix issue #2964.)

Issue 2: qemu-system-x86_64: CPU model 'host' requires KVM

The new error message is the same as in the mentioned topic,

WARNING: Image   format  was   not  specified   for
         'nixos.raw'   and  probing   guessed  raw.
         Automatically  detecting   the  format  is
         dangerous for raw images, write operations
         on block 0 will be restricted.

         Specify the 'raw' format explicitly to remove
         the restrictions.

qemu-system-x86_64: CPU model 'host' requires KVM

builder for '/nix/store/g5jz6wvsrmzgngrsgkn9c06c0ha95639-azure-image.drv' failed with exit code 1
error: build of '/nix/store/g5jz6wvsrmzgngrsgkn9c06c0ha95639-azure-image.drv' failed

but the solution there (i.e., don’t use docker-tools’s runAsRoot and exportImage) doesn’t seem to apply to azure-new. qemu is only called in nixos/modules/virtualisation/azure-image.nix (what I could find at least), but not sure what needs to be added, or if the problem is entirely somewhere else.

This usually means that you have cpu virtualization turned off in your BIOS settings.
https://wiki.archlinux.org/index.php/KVM#Checking_support_for_KVM

1 Like

Thank you, you were right, just turned virtualization on in the BIOS, and it checks out:

$ kvm-ok
INFO: /dev/kvm exists
KVM acceleration can be used

Got into a permission issue however:

Could not access KVM kernel module: Permission denied
qemu-system-x86_64: failed to initialize KVM: Permission denied

Found out that on Ubuntu KVM needs some setup for non-privileged users, but still no luck after that. My first though was that the Nix build process uses another user that needs to be authorized, but not sure if this true in single-user Nix installs. I also couldn’t find any Nix-related users/groups in /etc/passwd or in /etc/group. Will give all this a try on a NixOS machine later on.

You may need to run the command as root, or in your configuration.nix add yourself to the kvm group (assuming that /dev/kvm is part of that group).

  user.users.toraritte.extraGroups = [ "wheel" "kvm" ];
1 Like

@jonringer Sorry for the late reply - in the end I ditched Ubuntu and switched to NixOS, and the image got built without any issues with an almost-untouched configuration.nix.

Thanks for the help and for your time!

Note: Marking this as solution, but it was @jonringer who basically solved this. The real issue was that the Nix package manager (with all the installed packages) was running on a system (in this case, Ubuntu) where the tools used presumed that many settings/pre-requisites are there but they weren’t (who knows what settings I’ve messed up since it was installed). On NixOS this became a non-issue.

1 Like

@jonringer, I am hitting a wall of same problem when I tried to build a docker image using: dockerTools.buildImage with runAsRoot inside of docker using nixos/nix from github

Is it even possible that this could be done using a nixos docker to do so? or what sort of setup steps required to enable it - the kvm so that I could use runAsRoot option?

Thanks

I don’t think this is related, original post is about creating a cloud “azure.vhd” image, not a docker image.

not familiar with this. But if you’re getting issues with kvm not enabled, that a BIOS+kernel module issue; not a permission issue