Skip to content

Commit 3bbb732

Browse files
committed
cloud-hypervisor: add platformOEMStrings option
Cloud-hypervisor only supports a single --platform argument with comma-separated options. This commit adds proper handling for platform configuration: - Add platformOEMStrings option for passing OEM strings in a structured way - Introduce extractOptValues helper function to extract and remove command-line options from argument lists - Parse any --platform arguments from extraArgs and merge them with configured platform options The extractOptValues function enables proper handling of --platform arguments in extraArgs while avoiding conflicts with the internally generated --platform option.
1 parent e068a39 commit 3bbb732

File tree

3 files changed

+70
-19
lines changed

3 files changed

+70
-19
lines changed

lib/default.nix

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,59 @@ rec {
7878
import ./macvtap.nix {
7979
inherit microvmConfig hypervisorConfig lib;
8080
};
81+
82+
/*
83+
extractOptValues - Extract and remove all occurrences of a command-line option and its values from a list of arguments.
84+
85+
Description:
86+
This function searches for a specified option flag in a list of command-line arguments,
87+
extracts ALL associated values, and returns both the values and a filtered list with
88+
all occurrences of the option flag and its values removed. The order of all other
89+
arguments is preserved. Uses tail recursion to process the argument list.
90+
91+
Parameters:
92+
optFlag :: String | [String] - The option flag(s) to search for. Can be:
93+
- A single string (e.g., "-platform")
94+
- A list of strings (e.g., ["-p" "-platform"])
95+
All matching flags and their values are extracted
96+
extraArgs :: [String] - A list of command-line arguments
97+
98+
Returns:
99+
{
100+
values :: [String] - List of all values associated with matching flags (empty list if none found)
101+
args :: [String] - The input list with all matched flags and their values removed
102+
}
103+
104+
Examples:
105+
# Extract single occurrence:
106+
extractOptValues "-platform" ["-vnc" ":0" "-platform" "linux" "-usb"]
107+
=> { values = ["linux"]; args = ["-vnc" ":0" "-usb"]; }
108+
109+
# Extract multiple occurrences:
110+
extractOptValues "-platform" ["-a" "a" "-b" "b" "-c" "c" "-b" "b2"]
111+
=> { values = ["b" "b2"]; args = ["-b" "b" "-c" "c"]; }
112+
113+
# Extract with multiple flag aliases:
114+
extractOptValues ["-p" "-platform"] ["-p" "short" "-vnc" ":0" "-platform" "long" "-usb"]
115+
=> { values = ["short" "long"]; args = ["-vnc" ":0" "-usb"]; }
116+
117+
# Degenerate case with no matches:
118+
extractOptValues ["-p" "-platform"] ["-vnc" ":0" "-usb"]
119+
=> { values = []; args = ["-vnc" ":0" "-usb"]; }
120+
*/
121+
extractOptValues = optFlag: extraArgs:
122+
let
123+
flags = if builtins.isList optFlag then optFlag else [optFlag];
124+
125+
processArgs = args: values: acc:
126+
if args == [] then
127+
{ values = values; args = acc; }
128+
else if (builtins.elem (builtins.head args) flags) && (builtins.length args) > 1 then
129+
# Found one of the option flags, skip it and its value
130+
processArgs (builtins.tail (builtins.tail args)) (values ++ [(builtins.elemAt args 1)]) acc
131+
else
132+
# Not the option we're looking for, keep this element
133+
processArgs (builtins.tail args) values (acc ++ [(builtins.head args)]);
134+
in
135+
processArgs extraArgs [] [];
81136
}

lib/runners/cloud-hypervisor.nix

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66

77
let
88
inherit (pkgs) lib;
9+
inherit (import ../. { inherit (pkgs) lib; }) extractOptValues;
910
inherit (microvmConfig) vcpu mem balloon initialBalloonMem deflateOnOOM hotplugMem hotpluggedMem user interfaces volumes shares socket devices hugepageMem graphics storeDisk storeOnDisk kernel initrdPath;
10-
inherit (microvmConfig.cloud-hypervisor) platformOEMStrings extraPlatformOpts extraArgs;
11+
inherit (microvmConfig.cloud-hypervisor) platformOEMStrings extraArgs;
1112

1213
kernelPath = {
1314
x86_64-linux = "${kernel.dev}/vmlinux";
@@ -96,7 +97,9 @@ let
9697

9798
oemStringValues = (lib.optionals supportsNotifySocket ["io.systemd.credential:vmm.notify_socket=vsock-stream:2:8888"]) ++ platformOEMStrings;
9899
oemStringOptions = lib.optionals (oemStringValues != []) ["oem_strings=[${lib.concatStringsSep "," oemStringValues}]"];
99-
platformOps = lib.concatStringsSep "," (oemStringOptions ++ extraPlatformOpts);
100+
extraArgsWithoutPlatform = (extractOptValues "--platform" extraArgs).args;
101+
userPlatformOpts = (extractOptValues "--platform" extraArgs).values;
102+
platformOps = lib.concatStringsSep "," (oemStringOptions ++ userPlatformOpts);
100103
in {
101104
inherit tapMultiQueue;
102105

@@ -226,7 +229,7 @@ in {
226229
usb = throw "USB passthrough is not supported on cloud-hypervisor";
227230
}.${bus}) devices
228231
)
229-
) + " " + lib.escapeShellArgs extraArgs;
232+
) + " " + lib.escapeShellArgs extraArgsWithoutPlatform;
230233

231234
canShutdown = socket != null;
232235

nixos-modules/microvm/options.nix

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -519,24 +519,17 @@ in
519519
type = with types; listOf str;
520520
default = [];
521521
description = ''
522-
Extra arguments to pass to cloud-hypervisor's --platform oem_strings= argument.
522+
Extra arguments to pass to cloud-hypervisor's --platform oem_strings= argument.
523523
524-
All the oem strings will be concatenated with a comma (,) and wrapped in oem_string=[].
525-
'';
526-
example = literalExpression /* nix */ ''
527-
[ "io.systemd.credential:APIKEY=supersecret" ]
528-
'';
529-
};
530-
cloud-hypervisor.extraPlatformOpts = mkOption {
531-
type = with types; listOf str;
532-
default = [];
533-
description = ''
534-
Extra arguments to pass to cloud-hypervisor's --platform argument.
535-
All --platform args will be concatended with a comma (,).
536-
'';
537-
example = literalExpression /* nix */ ''
538-
[ "uuid=<dmi_device_uuid>" ]
524+
All the oem strings will be concatenated with a comma (,) and wrapped in oem_string=[].
525+
526+
The resulting string will be combined with any --platform options in
527+
`config.microvm.cloud-hypervisor.extraArgs` and passed as a single
528+
--platform option to cloud-hypervisor
539529
'';
530+
example = lib.literalExpression /* nix */ ''
531+
[ "io.systemd.credential:APIKEY=supersecret" ]
532+
'';
540533
};
541534

542535
cloud-hypervisor.extraArgs = mkOption {

0 commit comments

Comments
 (0)