NixOS / nixpkgs

Nix Packages collection & NixOS
MIT License
17.71k stars 13.85k forks source link

libvirt hooks run without bash context #98448

Open adamlwgriffiths opened 4 years ago

adamlwgriffiths commented 4 years ago

Describe the bug This bug is partially related to #51152, in that a declarative method of adding the hooks would solve this.

Hooks (eg /var/lib/libvirt/hooks/qemu) normally start with #!/usr/bin/env bash. It seems this isn't valid within the libvirt context and causes the hook execution - and therefore the vm - to fail.

Instead I have had to work around this by using #!/run/current-system/sw/bin/bash.

To Reproduce Steps to reproduce the behavior:

  1. Copy hook script from here https://github.com/PassthroughPOST/VFIO-Tools/blob/master/libvirt_hooks/qemu to /var/lib/libvirt/hooks/qemu
  2. sudo systemctl restart libvirtd.service
  3. Run a VM
  4. VM fails with an error

Expected behavior VM starts without error

Screenshots image

Additional context

This also applies to all hook scripts that are in turn executed by the hook itself. Eg. qemu.d/<vm>/prepare/begin/start.sh.

Notify maintainers @fpletz

Metadata $ nix-shell -p nix-info --run "nix-info -m"

Maintainer information:

# a list of nixpkgs attributes affected by the problem
attribute: libvirt
# a list of nixos modules affected by the problem
module:
cole-h commented 4 years ago

FWIW, I work around this by simply excluding the shebang(s):

https://github.com/cole-h/nixos-config/blob/69c1a768b210c3b8e29075a8c53ce741f475c0b9/hosts/scadrial/modules/libvirt/default.nix#L4

Not really a solution, but a(nother) workaround for the time being.

stale[bot] commented 3 years ago

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

adamlwgriffiths commented 3 years ago

Still important to me

tgharib commented 3 years ago

I just came across the same issue. Thank you cole-h for the reference nix configuration.

cole-h commented 3 years ago

FWIW, I've slightly changed how I do this: https://github.com/cole-h/nixos-config/blob/61d17a3355288872e4b765ddce1700654a9accc8/hosts/scadrial/modules/libvirt/default.nix#L5-L16

adamlwgriffiths commented 3 years ago

Thanks @cole-h, I didn't realise modprobe would also be unavailable to my hooks. I've now gotten them working with the following mod from your linked nix files. Super awesome, since I've been trying to get this working for over a year.

  systemd.services.libvirtd = {
    path = let
      env = pkgs.buildEnv {
        name = "qemu-hook-env";
        paths = with pkgs; [
          bash
          libvirt
          kmod
          systemd
        ];
      };
    in [ env ];
  };
stale[bot] commented 2 years ago

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

gshpychka commented 1 month ago

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

Still relevant