2023-01-29 15:55:56 +01:00
/*
# Qemu Exec VM
This module configures a "VM Variant" of the system that allows executing (pretty) arbitrary commands in a very lightweight qemu VM, with full access to the host's Nix store, and in the context of the systems Kernel and it's modules.
This is, for example, used to install the system to images without requiring root access (on the host).
## Usage
```bash
nix run .../nixos-config'#'nixosConfigurations.${hostName}.config.system.build.vmExec -- [--quiet] [--initrd-console] [--shared=/host/path/to/shared] "bash commands to run in VM" [-- ...extra-qemu-options]
```
* `--initrd-console` shows NixOS'es stage 1 boot output on the console (otherwise it is silenced).
* `--quiet` aims to suppress the terminal (re-)setting and all non-command output. Note that this filters the VM output.
* `--shared=` specifies an optional path to a host path that is read-write mounted at `/tmp/shared` in the VM.
* The value of the first positional argument is executed as a bash script in the VM and (if nothing else goes wrong) its exit status becomes that of the overall command.
* Any other positional arguments (that aren't parsed as named arguments, so put them after the `--` marker) are passed verbatim to the `qemu` launch command. These could for example attach disk or network devices.
## Notes
* The host's `/nix/var/nix/db` is read-only shared into the VM and then overlayed with a tmpfs. Modifying the Nix DBs on the host may have funny effects, esp. when also doing writing operations in the VM.
## Implementation
```nix
#*/# end of MarkDown, beginning of NixOS module:
2023-06-27 12:53:04 +02:00
dirname: inputs: moduleArgs@{ config, options, pkgs, lib, modulesPath, extendModules, ... }: let lib = inputs.self.lib.__internal__; in let mainModule = (suffix: extraModule: let
2023-01-29 15:55:56 +01:00
prefix = inputs.config.prefix;
2023-06-27 12:53:04 +02:00
cfg = config.virtualisation."vmVariant${suffix}";
2023-02-05 22:59:39 +01:00
in let hostModule = {
2023-01-29 15:55:56 +01:00
2023-06-27 12:53:04 +02:00
options = { virtualisation."vmVariant${suffix}" = lib.mkOption {
2023-01-29 15:55:56 +01:00
description = lib.mdDoc ''Machine configuration to be added to the system's qemu exec VM.'';
2023-06-27 12:53:04 +02:00
inherit (extendModules { modules = [ "${modulesPath}/virtualisation/qemu-vm.nix" vmModule extraModule ]; }) type;
2023-01-29 15:55:56 +01:00
default = { }; visible = "shallow";
}; };
config = {
2023-06-27 12:53:04 +02:00
system.build."vm${suffix}" = (let hostPkgs = pkgs; in let
2023-01-29 15:55:56 +01:00
name = "run-${config.system.name}-vm-exec";
launch = "${cfg.system.build.vm}/bin/${cfg.system.build.vm.meta.mainProgram}";
2023-02-05 22:59:39 +01:00
pkgs = if cfg.virtualisation?host.pkgs then cfg.virtualisation.host.pkgs else hostPkgs;
2023-01-29 15:55:56 +01:00
in pkgs.runCommand "nixos-vm" {
preferLocalBuild = true; meta.mainProgram = name;
} ''
mkdir -p $out/bin
ln -s ${cfg.system.build.toplevel} $out/system
ln -s ${pkgs.writeShellScript name ''
2023-12-04 19:30:01 +01:00
source ${lib.fun.bash.generic-arg-parse}
2023-01-29 15:55:56 +01:00
generic-arg-parse "$@" ; set -- ; set -o pipefail -u #; set -x
script=''${argv[0]:?'The first positional argument must be the script to execute in the VM'} ; argv=( "''${argv[@]:1}" )
tmp=$( mktemp -d nix-vm.XXXXXXXXXX --tmpdir ) & & trap "rm -rf '$tmp'" EXIT || exit
mkdir -p $tmp/{xchg,shared} && printf '%s\n' "$script" >$tmp/xchg/script && chmod +x $tmp/xchg/script || exit
if [[ ! ''${args[initrd-console]:-} ]] ; then noConsole=1 ; fi
if [[ ''${args[initrd-console]:-} ]] ; then touch $tmp/xchg/initrd-console ; fi
if [[ ''${args[quiet]:-} ]] ; then touch $tmp/xchg/quiet ; fi
2024-05-31 22:41:35 +02:00
< /etc/hosts grep -oP '127.0.0.[12] (?!localhost)\K.*' >$tmp/xchg/host
2023-01-29 15:55:56 +01:00
${cfg.virtualisation.qemu.package}/bin/qemu-img create -f qcow2 $tmp/dummyImage 4M & >/dev/null # do this silently
export NIX_DISK_IMAGE=$tmp/dummyImage
export TMPDIR=$tmp USE_TMPDIR=1 SHARED_DIR=''${args[shared]:-$tmp/shared}
export QEMU_KERNEL_PARAMS="init=${config.system.build.toplevel}/init ''${noConsole:+console=tty1} edd=off boot.shell_on_fail"
export QEMU_NET_OPTS= QEMU_OPTS=
if [[ ''${args[quiet]:-} ]] ; then
2024-05-31 22:41:35 +02:00
${launch} "''${argv[@]}" & > >( ${pkgs.coreutils}/bin/tr -dc '[[:print:]]\r\n\t' | {
while IFS= read line ; do if [[ $line == magic:cm4alv0wly79p6i4aq32hy36i* ]] ; then break ; fi ; done ; cat ;
} ) || { e=$? ; echo "Execution of VM failed!" 1>& 2 ; exit $e ; }
2023-01-29 15:55:56 +01:00
else
${launch} "''${argv[@]}" || exit
fi
if [[ -e $tmp/xchg/exit ]] ; then \exit "$( cat $tmp/xchg/exit )" ; fi
echo "Execution in VM failed!" 1>& 2 ; \exit 1
''} $out/bin/${name}
'');
2023-02-05 22:59:39 +01:00
};
2023-01-29 15:55:56 +01:00
2023-02-05 22:59:39 +01:00
}; vmModule = { config, ... }: {
_file = "${dirname}/vm-exec.nix.md#vmModule";
imports = [ ({
2023-01-29 15:55:56 +01:00
2023-06-16 02:14:51 +02:00
virtualisation.graphics = false;
2023-01-29 15:55:56 +01:00
# Instead of tearing down the initrd environment, adjust some mounts and run the »command« in the initrd:
2024-02-27 16:36:01 +01:00
boot.initrd.systemd.enable = lib.mkVMOverride false;
2023-01-29 15:55:56 +01:00
boot.initrd.postMountCommands = ''
2024-05-31 22:41:35 +02:00
set -x
2023-01-29 15:55:56 +01:00
for fs in tmp/shared tmp/xchg nix/store.lower nix/var/nix/db.lower ; do
mkdir -p /$fs & & mount --move $targetRoot/$fs /$fs || fail
done
chmod 1777 /tmp
# Nix want's to create lock files, even on read-only operations:
mkdir -p -m 755 /nix/var/nix/db.work /nix/var/nix/db.upper /nix/var/nix/db
mount -t overlay overlay -o lowerdir=/nix/var/nix/db.lower,workdir=/nix/var/nix/db.work,upperdir=/nix/var/nix/db.upper /nix/var/nix/db
2023-06-16 02:14:51 +02:00
# Nix insists on setting the ownership of »/nix/store« to »0:30000« (if run as root(?) and the current ownership is something else, e.g. when using »nix-user-chroot«):
2023-01-29 15:55:56 +01:00
mkdir -p -m 755 /nix/store.work /nix/store.upper /nix/store
mount -t overlay overlay -o lowerdir=/nix/store.lower,workdir=/nix/store.work,upperdir=/nix/store.upper /nix/store
# »/run/{booted,current}-system« is more for debugging than anything else, but changing »/lib/modules« makes modprobe use the full system's modules, instead of only the initrd ones:
toplevel=$(dirname $stage2Init)
ln -sfT $toplevel /run/current-system
ln -sfT $toplevel /run/booted-system
2024-05-31 22:41:35 +02:00
rm -rf /lib/modules ; ln -sfT $toplevel/kernel-modules/lib/modules /lib/modules
2023-01-29 15:55:56 +01:00
2024-05-31 22:41:35 +02:00
# Set up /etc:
2023-01-29 15:55:56 +01:00
mv /etc /etc.initrd
mkdir -p -m 755 /etc.work /etc.upper /etc
mount -t overlay overlay -o lowerdir=$toplevel/etc,workdir=/etc.work,upperdir=/etc.upper /etc
( cd /etc.initrd ; cp -a mtab udev /etc/ ) # (keep these)
2024-05-31 22:41:35 +02:00
# Set up NATed networking:
cat /etc/hosts >/etc/hosts.cp ; rm /etc/hosts ; mv /etc/hosts.cp /etc/hosts
perl -pe 's/127.0.0.[12](?! localhost)/# /' -i /etc/hosts
perl -pe 's/::1(?! localhost)/# /' -i /etc/hosts
{ printf '10.0.2.2 ' ; cat /tmp/xchg/host ; } >>/etc/hosts
ip addr add 10.0.2.15/24 dev eth0
ip link set dev eth0 up
ip route add default via 10.0.2.2 dev eth0
echo nameserver 1.1.1.1 >/etc/resolv.conf # 10.0.2.3 doesn't reply
2023-01-29 15:55:56 +01:00
# »nix copy« complains without »nixbld« group:
rm -f /etc/passwd /etc/group
printf '%s\n' 'root:x:0:0:root:/root:/bin/bash' >/etc/passwd
printf '%s\n' 'root:x:0:' 'nixbld:x:30000:' >/etc/group
export HOME=/root USER=root ; mkdir -p -m 700 $HOME
PATH=/run/current-system/sw/bin ; rm -f /bin /sbin ; unset LD_LIBRARY_PATH
console=/dev/ttyS0 ; if [[ -e /tmp/xchg/initrd-console ]] ; then console=/dev/console ; fi # (does this even make a difference?)
if [[ -e /tmp/xchg/quiet ]] ; then printf '\n%s\n' 'magic:cm4alv0wly79p6i4aq32hy36i...' >$console ; fi
2024-05-31 22:41:35 +02:00
set +x ; exit=0 ; bash /tmp/xchg/script < $console >$console 2>$console || exit=$?
2023-01-29 15:55:56 +01:00
echo $exit >/tmp/xchg/exit
sync ; sync
echo 1 > /proc/sys/kernel/sysrq
echo o > /proc/sysrq-trigger
sleep infinity # the VM will halt very soon
'';
boot.initrd.kernelModules = [ "overlay" ]; # for writable »/etc«, chown of »/nix/store« and locks in »/nix/var/nix/db«
2024-05-31 22:41:35 +02:00
#boot .initrd.extraUtilsCommands = ''copy_bin_and_libs ${pkgs.perl}/bin/perl'';
2023-01-29 15:55:56 +01:00
}) ({
2023-06-16 02:14:51 +02:00
virtualisation.writableStore = false;
2023-01-29 15:55:56 +01:00
fileSystems = lib.mkVMOverride {
"/nix/var/nix/db.lower" = {
fsType = "9p"; device = "nix-var-nix-db"; neededForBoot = true;
2023-05-02 02:13:24 +02:00
options = [ "trans=virtio" "version=9p2000.L" "msize=4194304" "ro" ];
2023-01-29 15:55:56 +01:00
};
2023-05-02 02:13:24 +02:00
"/nix/store".options = lib.mkAfter [ "ro" "msize=4194304" ];
2023-06-16 02:14:51 +02:00
"/nix/store".mountPoint = lib.mkForce "/nix/store.lower";
2023-05-02 02:13:24 +02:00
}; # mount -t 9p -o trans=virtio -o version=9p2000.L -o msize=4194304 nix-var-nix-db /nix/var/nix/db
2023-01-29 15:55:56 +01:00
virtualisation.qemu.options = [ "-virtfs local,path=/nix/var/nix/db,security_model=none,mount_tag=nix-var-nix-db,readonly=on" ]; # (doing this manually to pass »readonly«, to not ever corrupt the host's Nix DBs)
2024-05-31 22:41:35 +02:00
boot.resumeDevice = lib.mkVMOverride "";
2023-01-29 15:55:56 +01:00
}) ({
fileSystems = lib.mkVMOverride { "/" = lib.mkForce {
fsType = "tmpfs"; device = "tmpfs"; neededForBoot = true;
options = [ "mode=1777" "noatime" "nosuid" "nodev" "size=50%" ];
}; };
virtualisation.diskSize = 4; #MB , not needed at all
2023-02-05 22:59:39 +01:00
}) ({
2024-05-31 22:41:35 +02:00
virtualisation.host.pkgs = lib.mkDefault pkgs.buildPackages;
2023-05-02 02:13:24 +02:00
virtualisation.qemu.package = lib.mkIf (pkgs.buildPackages.system != pkgs.system) (cfg.virtualisation.host or { pkgs = pkgs.buildPackages; }).pkgs.qemu_full;
2023-02-05 22:59:39 +01:00
2024-05-31 22:41:35 +02:00
}) ({
#virtualisation .qemu.options = [ "-nic user,model=virtio-net-pci" ]; # NAT
2023-02-05 22:59:39 +01:00
}) ({
specialisation = lib.mkForce { };
services.qemuGuest.enable = lib.mkForce false;
2023-01-29 15:55:56 +01:00
# tag this to make clearer what's what
2023-06-27 12:53:04 +02:00
system.nixos.tags = [ "vm${suffix}" ];
system.build."isVm${suffix}" = true;
2023-01-29 15:55:56 +01:00
2023-02-05 22:59:39 +01:00
}) ];
2023-06-27 12:53:04 +02:00
}; in hostModule); in { imports = [ (mainModule "Exec" { }) ] ++ (map (system: (
mainModule "Exec-${system}" {
virtualisation.host.pkgs = import (moduleArgs.inputs.nixpkgs or inputs.nixpkgs).outPath { inherit (pkgs) overlays config; inherit system; };
}
)) [ "aarch64-linux" "x86_64-linux" ]); }