NixOS / nixpkgs

Nix Packages collection & NixOS
MIT License
18.18k stars 14.19k forks source link

LXD Virtual-Machine (qemu) mode is not working #114194

Closed mkg20001 closed 1 year ago

mkg20001 commented 3 years ago

Describe the bug Since lxd 4.0 lxd is able to run qemu virtual machines besides just containers https://discuss.linuxcontainers.org/t/running-virtual-machines-with-lxd-4-0/7519/1

There seem to be hardcoded locations to some /usr/share foloders that are missing, etc

Additionally not sure about qemu inclusion into lxd

A variant of lxd could be built with a flag "useQemu ? false" in the package and a configuration option to toggle it could be added

To Reproduce Steps to reproduce the behavior:

  1. lxc init win10 --empty --vm -c security.secureboot=false -c limits.cpu=4 -c limits.memory=4GB
  2. lxc start win10

Expected behavior It tries to boot

Screenshots

# lxc start win10
Error: lstat /usr/share: no such file or directory
Try `lxc info --show-log win10` for more info

Additional context /usr/share hardcoded stuff

Notify maintainers

@fpletz @wucke13

Metadata

Maintainer information:

# a list of nixpkgs attributes affected by the problem
attribute:
# a list of nixos modules affected by the problem
module:
mkg20001 commented 3 years ago

Here's my quick-and-dirty patch that works

diff --git a/pkgs/tools/admin/lxd/default.nix b/pkgs/tools/admin/lxd/default.nix
index ba6f9d074b2..666d2a34dbe 100644
--- a/pkgs/tools/admin/lxd/default.nix
+++ b/pkgs/tools/admin/lxd/default.nix
@@ -7,8 +7,12 @@
 , bash
 , installShellFiles
 , nftablesSupport ? false
+, useQemu ? true
+, OVMF, qemu_kvm, seabios, gptfdisk
 }:

+# TODO: patch code so without useQemu it throws an error about useQemu=false
+
 let
   networkPkgs = if nftablesSupport then
     [ iptables-nftables-compat ]
@@ -30,6 +34,12 @@ buildGoPackage rec {
   postPatch = ''
     substituteInPlace shared/usbid/load.go \
       --replace "/usr/share/misc/usb.ids" "${hwdata}/share/hwdata/usb.ids"
+    # Nuke from orbit.
+    find . -type f -exec sed -i \
+      -e s,/usr/share/OVMF,${OVMF.fd}/FV,g \
+      -e s,/usr/share/qemu,${qemu_kvm}/share/qemu,g \
+      -e s,/usr/share/seabios,${seabios},g \
+      {} +
   '';

   preBuild = ''
@@ -48,6 +58,7 @@ buildGoPackage rec {

     wrapProgram $out/bin/lxd --prefix PATH : ${lib.makeBinPath (
       networkPkgs
+      ++ (lib.optionals useQemu [ qemu_kvm gptfdisk ])
       ++ [ acl rsync gnutar xz btrfs-progs gzip dnsmasq squashfsTools iproute bash criu ]
       ++ [ (writeShellScriptBin "apparmor_parser" ''
              exec '${apparmor-parser}/bin/apparmor_parser' -I '${apparmor-profiles}/etc/apparmor.d' "$@"
@@ -60,7 +71,7 @@ buildGoPackage rec {

   nativeBuildInputs = [ installShellFiles pkg-config makeWrapper ];
   buildInputs = [ lxc acl libcap libco-canonical.dev dqlite.dev
-                  raft-canonical.dev sqlite-replication udev.dev ];
+                  raft-canonical.dev sqlite-replication udev.dev qemu_kvm ];

   meta = with lib; {
     description = "Daemon based on liblxc offering a REST API to manage containers";
marsam commented 3 years ago

@mkg20001 would you mind creating a PR with that patch?

mkg20001 commented 3 years ago

I've noticed there's this PR which tries to achive the same thing https://github.com/NixOS/nixpkgs/pull/105651

mkg20001 commented 3 years ago

Tbe other PR is missing some things, like the seabios path patch or gptfdisk in path.

Prob the best is to take the best of both and put it into one PR.

stale[bot] commented 3 years ago

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

Artturin commented 2 years ago

LXD has the ability to use env vars for many of the paths https://github.com/lxc/lxd/search?q=%2Fusr%2Fshare

func (d *qemu) ovmfPath() string {
    if os.Getenv("LXD_OVMF_PATH") != "" {
            return os.Getenv("LXD_OVMF_PATH")
    }

    return "/usr/share/OVMF"
}