Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions lib/runner.nix
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
let
inherit (pkgs) lib;

inherit (microvmConfig) hostName vmHostPackages;
inherit (microvmConfig) fqdnOrHostName vmHostPackages;

inherit (import ./. { inherit lib; }) makeMacvtap withDriveLetters extractOptValues extractParamValue;
inherit (import ./volumes.nix { pkgs = microvmConfig.vmHostPackages; }) createVolumesScript;
Expand All @@ -26,7 +26,8 @@ let
tapMultiQueue = hypervisorConfig.tapMultiQueue or false;
setBalloonScript = hypervisorConfig.setBalloonScript or null;

execArg = lib.optionalString microvmConfig.prettyProcnames ''-a "microvm@${hostName}"'';
execArg = lib.optionalString microvmConfig.prettyProcnames
''-a "microvm@${fqdnOrHostName}"'';

# TAP interface names for machined registration
tapInterfaces = lib.filter (i: i.type == "tap" && i ? id) microvmConfig.interfaces;
Expand All @@ -40,14 +41,14 @@ let
else
builtins.readFile (
vmHostPackages.runCommand "machine-uuid" { } ''
${vmHostPackages.python3}/bin/python3 -c 'import uuid; print(uuid.uuid5(uuid.NAMESPACE_DNS, "${hostName}"), end="")' > $out
${vmHostPackages.python3}/bin/python3 -c 'import uuid; print(uuid.uuid5(uuid.NAMESPACE_DNS, "${fqdnOrHostName}"), end="")' > $out
''
);

# Script to unregister from systemd-machined
unregisterMachineScript = ''
set -euo pipefail
MACHINE_NAME="${hostName}"
MACHINE_NAME="${fqdnOrHostName}"

# Terminate the machine registration (ignore errors if already gone)
${vmHostPackages.systemd}/bin/busctl call \
Expand All @@ -65,7 +66,7 @@ let
set -euo pipefail

LEADER_PID="''${1:-$$}"
MACHINE_NAME="${hostName}"
MACHINE_NAME="${fqdnOrHostName}"
UUID="${machineUuid}"

# Convert UUID to decimal bytes for busctl array arguments
Expand Down Expand Up @@ -207,11 +208,11 @@ let
};

binScriptPkgs = lib.mapAttrs (
scriptName: lines: vmHostPackages.writeShellScript "microvm-${hostName}-${scriptName}" lines
scriptName: lines: vmHostPackages.writeShellScript "microvm-${fqdnOrHostName}-${scriptName}" lines
) binScripts;
in

vmHostPackages.buildPackages.runCommand "microvm-${microvmConfig.hypervisor}-${hostName}"
vmHostPackages.buildPackages.runCommand "microvm-${microvmConfig.hypervisor}-${fqdnOrHostName}"
{
# for `nix run`
meta.mainProgram = "microvm-run";
Expand Down
4 changes: 2 additions & 2 deletions lib/runners/firecracker.nix
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ let
inherit (pkgs) lib;
inherit (pkgs.stdenv.hostPlatform) system;
inherit (microvmConfig)
hostName user socket preStart
fqdnOrHostName user socket preStart
vcpu mem balloon initialBalloonMem hotplugMem hotpluggedMem
interfaces volumes shares devices
kernel initrdPath
Expand Down Expand Up @@ -76,7 +76,7 @@ let
};
config = lib.recursiveUpdate baseConfig microvmConfig.firecracker.extraConfig;

configFile = pkgs.writers.writeJSON "firecracker-${hostName}.json" config;
configFile = pkgs.writers.writeJSON "firecracker-${fqdnOrHostName}.json" config;

firecrackerPkg = microvmConfig.firecracker.package;

Expand Down
6 changes: 3 additions & 3 deletions lib/runners/kvmtool.nix
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ let
kvmtoolPkg = microvmConfig.kvmtool.package;

inherit (microvmConfig)
hostName preStart user
fqdnOrHostName preStart user
vcpu mem balloon initialBalloonMem hotplugMem hotpluggedMem interfaces volumes shares devices vsock
kernel initrdPath credentialFiles
storeDisk storeOnDisk;
Expand All @@ -34,7 +34,7 @@ in {
else builtins.concatStringsSep " " (
[
"${kvmtoolPkg}/bin/lkvm" "run"
"--name" (lib.escapeShellArg hostName)
"--name" (lib.escapeShellArg fqdnOrHostName)
"-m" (toString mem)
"-c" (toString vcpu)
"--console" "serial"
Expand Down Expand Up @@ -103,6 +103,6 @@ in {
else
ARGS="-i $SIZE"
fi
HOME=$PWD ${kvmtoolPkg}/bin/lkvm balloon $ARGS -n ${hostName}
HOME=$PWD ${kvmtoolPkg}/bin/lkvm balloon $ARGS -n ${fqdnOrHostName}
'';
}
6 changes: 3 additions & 3 deletions lib/runners/qemu.nix
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ let
then "io_uring"
else "threads";

inherit (microvmConfig) hostName machineId vcpu mem balloon initialBalloonMem deflateOnOOM hotplugMem hotpluggedMem user interfaces shares socket forwardPorts devices vsock graphics storeOnDisk kernel initrdPath storeDisk credentialFiles;
inherit (microvmConfig) fqdnOrHostName machineId vcpu mem balloon initialBalloonMem deflateOnOOM hotplugMem hotpluggedMem user interfaces shares socket forwardPorts devices vsock graphics storeOnDisk kernel initrdPath storeDisk credentialFiles;
inherit (microvmConfig.qemu) machine extraArgs serialConsole pcieRootPorts;

volumes = withDriveLetters microvmConfig;
Expand Down Expand Up @@ -174,7 +174,7 @@ lib.warnIf (mem == 2048) ''
else lib.escapeShellArgs (
[
"${qemu}/bin/qemu-system-${arch}"
"-name" hostName
"-name" fqdnOrHostName
"-M" machineConfig
"-m" "${toString mem}M${lib.optionalString useHotPlugMemory ",maxmem=${toString (mem + hotplugMem)}M"}"
"-smp" (toString vcpu)
Expand Down Expand Up @@ -297,7 +297,7 @@ lib.warnIf (mem == 2048) ''
lib.warnIf (
forwardPorts != [] &&
! builtins.any ({ type, ... }: type == "user") interfaces
) "${hostName}: forwardPortsOptions only running with user network" (
) "${fqdnOrHostName}: forwardPortsOptions only running with user network" (
builtins.concatMap ({ type, id, mac, bridge, tap ? {}, ... }: [
"-netdev" (
lib.concatStringsSep "," (
Expand Down
6 changes: 3 additions & 3 deletions lib/runners/stratovirt.nix
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ let
stratovirtPkg = microvmConfig.stratovirt.package;

inherit (microvmConfig)
hostName
fqdnOrHostName
vcpu mem balloon initialBalloonMem hotplugMem hotpluggedMem interfaces shares socket forwardPorts devices
kernel initrdPath credentialFiles
storeOnDisk storeDisk;
Expand Down Expand Up @@ -89,7 +89,7 @@ in {
[
"${pkgs.expect}/bin/unbuffer"
"${stratovirtPkg}/bin/stratovirt"
"-name" hostName
"-name" fqdnOrHostName
"-machine" machine
"-m" (toString mem)
"-smp" (toString vcpu)
Expand Down Expand Up @@ -143,7 +143,7 @@ in {
lib.warnIf (
forwardPorts != [] &&
! builtins.any ({ type, ... }: type == "user") interfaces
) "${hostName}: forwardPortsOptions only running with user network" (
) "${fqdnOrHostName}: forwardPortsOptions only running with user network" (
builtins.concatMap ({ type, id, mac, bridge, ... }: [
"-netdev" (
lib.concatStringsSep "," (
Expand Down
2 changes: 1 addition & 1 deletion nixos-modules/host/options.nix
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
extraConfig = ({ lib, ... }: {
_file = "module at ${__curPos.file}:${toString __curPos.line}";
config = {
networking.hostName = lib.mkDefault name;
networking.fqdnOrHostName = lib.mkDefault name;
};
});
in [
Expand Down
24 changes: 12 additions & 12 deletions nixos-modules/microvm/asserts.nix
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{ config, lib, ... }:
let
inherit (config.networking) hostName;
inherit (config.networking) fqdnOrHostName;

in
lib.mkIf config.microvm.guest.enable {
Expand All @@ -9,7 +9,7 @@ lib.mkIf config.microvm.guest.enable {
map (volumes: {
assertion = builtins.length volumes == 1;
message = ''
MicroVM ${hostName}: volume image "${(builtins.head volumes).image}" is used ${toString (builtins.length volumes)} > 1 times.
MicroVM ${fqdnOrHostName}: volume image "${(builtins.head volumes).image}" is used ${toString (builtins.length volumes)} > 1 times.
'';
}) (
builtins.attrValues (
Expand All @@ -21,7 +21,7 @@ lib.mkIf config.microvm.guest.enable {
map (interfaces: {
assertion = builtins.length interfaces == 1;
message = ''
MicroVM ${hostName}: interface id "${(builtins.head interfaces).id}" is used ${toString (builtins.length interfaces)} > 1 times.
MicroVM ${fqdnOrHostName}: interface id "${(builtins.head interfaces).id}" is used ${toString (builtins.length interfaces)} > 1 times.
'';
}) (
builtins.attrValues (
Expand All @@ -35,14 +35,14 @@ lib.mkIf config.microvm.guest.enable {
then {
assertion = bridge != null;
message = ''
MicroVM ${hostName}: interface ${id} is of type "bridge"
MicroVM ${fqdnOrHostName}: interface ${id} is of type "bridge"
but doesn't have a bridge to attach to defined.
'';
}
else {
assertion = bridge == null;
message = ''
MicroVM ${hostName}: interface ${id} is not of type "bridge"
MicroVM ${fqdnOrHostName}: interface ${id} is not of type "bridge"
and therefore shouldn't have a "bridge" option defined.
'';
}
Expand All @@ -52,7 +52,7 @@ lib.mkIf config.microvm.guest.enable {
map ({ id, ... }: {
assertion = builtins.stringLength id <= 15;
message = ''
MicroVM ${hostName}: interface name ${id} is longer than the
MicroVM ${fqdnOrHostName}: interface name ${id} is longer than the
the maximum length of 15 characters on Linux.
'';
}) config.microvm.interfaces
Expand All @@ -61,7 +61,7 @@ lib.mkIf config.microvm.guest.enable {
map (shares: {
assertion = builtins.length shares == 1;
message = ''
MicroVM ${hostName}: share tag "${(builtins.head shares).tag}" is used ${toString (builtins.length shares)} > 1 times.
MicroVM ${fqdnOrHostName}: share tag "${(builtins.head shares).tag}" is used ${toString (builtins.length shares)} > 1 times.
'';
}) (
builtins.attrValues (
Expand All @@ -73,7 +73,7 @@ lib.mkIf config.microvm.guest.enable {
map (shares: {
assertion = builtins.length shares == 1;
message = ''
MicroVM ${hostName}: share socket "${(builtins.head shares).socket}" is used ${toString (builtins.length shares)} > 1 times.
MicroVM ${fqdnOrHostName}: share socket "${(builtins.head shares).socket}" is used ${toString (builtins.length shares)} > 1 times.
'';
}) (
builtins.attrValues (
Expand All @@ -88,7 +88,7 @@ lib.mkIf config.microvm.guest.enable {
map ({ tag, socket, ... }: {
assertion = socket != null;
message = ''
MicroVM ${hostName}: virtiofs share with tag "${tag}" is missing a `socket` path.
MicroVM ${fqdnOrHostName}: virtiofs share with tag "${tag}" is missing a `socket` path.
'';
}) (
builtins.filter ({ proto, ... }: proto == "virtiofs")
Expand All @@ -103,23 +103,23 @@ lib.mkIf config.microvm.guest.enable {
builtins.any ({ type, ... }: type == "user") config.microvm.interfaces
);
message = ''
MicroVM ${hostName}: `config.microvm.forwardPorts` works only with qemu and one network interface with `type = "user"`
MicroVM ${fqdnOrHostName}: `config.microvm.forwardPorts` works only with qemu and one network interface with `type = "user"`
'';
} ]
++
# cloud-hypervisor specific asserts
lib.optionals (config.microvm.hypervisor == "cloud-hypervisor") [ {
assertion = ! (lib.any (str: lib.hasInfix "oem_strings" str) config.microvm.cloud-hypervisor.platformOEMStrings);
message = ''
MicroVM ${hostName}: `config.microvm.cloud-hypervisor.platformOEMStrings` items must not contain `oem_strings`
MicroVM ${fqdnOrHostName}: `config.microvm.cloud-hypervisor.platformOEMStrings` items must not contain `oem_strings`
'';
} ];


warnings =
# 32 MB is just an optimistic guess, not based on experience
lib.optional (config.microvm.mem < 32) ''
MicroVM ${hostName}: ${toString config.microvm.mem} MB of RAM is uncomfortably narrow.
MicroVM ${fqdnOrHostName}: ${toString config.microvm.mem} MB of RAM is uncomfortably narrow.
''
++ lib.optional config.nix.optimise.automatic ''
Optimising the nix store is not recommended as it either uses lots of file handles with virtiofsd or as it doesn't do what you expect with a block device.
Expand Down
2 changes: 1 addition & 1 deletion nixos-modules/microvm/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ in
microvm-lib.buildRunner {
inherit pkgs;
microvmConfig = config.microvm // {
inherit (config.networking) hostName;
inherit (config.networking) fqdnOrHostName;
inherit hypervisor;
};
inherit (config.system.build) toplevel;
Expand Down
12 changes: 6 additions & 6 deletions nixos-modules/microvm/options.nix
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ let
};

cfg = config.microvm;
hostName = config.networking.hostName or "$HOSTNAME";
fqdnOrHostName = config.networking.fqdnOrHostName or "$HOSTNAME";
kernelAtLeast = lib.versionAtLeast config.boot.kernelPackages.kernel.version;
in
{
Expand Down Expand Up @@ -76,8 +76,8 @@ in

socket = mkOption {
description = "Hypervisor control socket path";
default = "${hostName}.sock";
defaultText = literalExpression ''"''${hostName}.sock"'';
default = "${fqdnOrHostName}.sock";
defaultText = literalExpression ''"''${fqdnOrHostName}.sock"'';
type = with types; nullOr str;
};

Expand Down Expand Up @@ -383,7 +383,7 @@ in
type = nullOr str;
default =
if config.proto == "virtiofs"
then "${hostName}-virtiofs-${config.tag}.sock"
then "${fqdnOrHostName}-virtiofs-${config.tag}.sock"
else null;
description = "Socket for communication with virtiofs daemon";
};
Expand Down Expand Up @@ -514,7 +514,7 @@ in
type = with types; nullOr str;
default =
let
hash = builtins.hashString "sha256" "microvm.nix:${hostName}";
hash = builtins.hashString "sha256" "microvm.nix:${fqdnOrHostName}";
hs = offset: len:
builtins.substring offset len hash;
in builtins.concatStringsSep "-" [
Expand Down Expand Up @@ -611,7 +611,7 @@ in

socket = mkOption {
type = types.str;
default = "${hostName}-gpu.sock";
default = "${fqdnOrHostName}-gpu.sock";
description = ''
Path of vhost-user socket
'';
Expand Down
Loading