22, microvmConfig
33, macvtapFds
44, extractOptValues
5+ , extractParamValue
56, ...
67} :
78
89let
910 inherit ( pkgs ) lib ;
10- inherit ( microvmConfig ) vcpu mem balloon initialBalloonMem deflateOnOOM hotplugMem hotpluggedMem user interfaces volumes shares socket devices hugepageMem graphics storeDisk storeOnDisk kernel initrdPath credentialFiles ;
11+ inherit ( microvmConfig ) vcpu mem balloon initialBalloonMem deflateOnOOM hotplugMem hotpluggedMem user interfaces volumes shares socket devices hugepageMem graphics storeDisk storeOnDisk kernel initrdPath credentialFiles vsock ;
1112 inherit ( microvmConfig . cloud-hypervisor ) platformOEMStrings extraArgs ;
1213
14+ # extract all the extra args that we merge with up front
15+ processedExtraArgs = builtins . foldl'
16+ ( args : opt : ( extractOptValues opt args ) . args )
17+ extraArgs
18+ [ "--vsock" "--platform" ] ;
19+
1320 hasUserConsole = ( extractOptValues "--console" extraArgs ) . values != [ ] ;
1421 hasUserSerial = ( extractOptValues "--serial" extraArgs ) . values != [ ] ;
1522 userSerial = lib . optionalString hasUserSerial ( extractOptValues "--serial" extraArgs ) . values ;
3037
3138 kernelCmdLine = "${ kernelConsole } reboot=t panic=-1 ${ builtins . unsafeDiscardStringContext ( toString microvmConfig . kernelParams ) } " ;
3239
40+
41+ userVSockOpts = ( extractOptValues "--vsock" extraArgs ) . values ;
42+ userVSockStr = if userVSockOpts == [ ] then null else builtins . head userVSockOpts ;
43+ userVSockPath = extractParamValue "socket" userVSockStr ;
44+ userVSockCID = extractParamValue "cid" userVSockStr ;
45+ vsockCID = if vsock . cid != null && userVSockCID != null
46+ then throw "Cannot set `microvm.vsock.cid` and --vsock 'cid=${ userVSockCID } ...' via `microvm.cloud-hypervisor.extraArgs` at the same time"
47+ else if vsock . cid != null
48+ then vsock . cid
49+ else userVSockCID ;
50+ supportsNotifySocket = vsockCID != null ;
51+ vsockPath = if userVSockPath != null then userVSockPath else "notify.vsock" ;
52+ vsockOpts =
53+ if vsockCID == null then
54+ lib . warn "cloud-hypervisor supports systemd-notify via vsock, but `microvm.vsock.cid` must be set to enable this." ""
55+ else
56+ "cid=${ toString vsockCID } ,socket=${ vsockPath } " ;
57+
3358 useHotPlugMemory = hotplugMem > 0 ;
3459
3560 useVirtiofs = builtins . any ( { proto , ... } : proto == "virtiofs" ) shares ;
@@ -101,14 +126,18 @@ let
101126 vulkan = true ;
102127 } ;
103128
104- supportsNotifySocket = true ;
105-
106129 oemStringValues = platformOEMStrings ++ lib . optional supportsNotifySocket "io.systemd.credential:vmm.notify_socket=vsock-stream:2:8888" ;
107130 oemStringOptions = lib . optional ( oemStringValues != [ ] ) "oem_strings=[${ lib . concatStringsSep "," oemStringValues } ]" ;
108131 platformExtracted = extractOptValues "--platform" extraArgs ;
109132 extraArgsWithoutPlatform = platformExtracted . args ;
110133 userPlatformOpts = platformExtracted . values ;
111- platformOps = lib . concatStringsSep "," ( oemStringOptions ++ userPlatformOpts ) ;
134+ userPlatformStr = lib . optionalString ( userPlatformOpts != [ ] ) ( builtins . head userPlatformOpts ) ;
135+ userHasOemStrings = ( extractParamValue "oem_strings" userPlatformStr ) != null ;
136+ platformOps =
137+ if userHasOemStrings then
138+ throw "Use `microvm.cloud-hypervisor.platformOEMStrings` instead of passing oem_strings via --platform"
139+ else
140+ lib . concatStringsSep "," ( oemStringOptions ++ userPlatformOpts ) ;
112141in {
113142 inherit tapMultiQueue ;
114143
@@ -122,13 +151,13 @@ in {
122151
123152 '' + lib . optionalString supportsNotifySocket ''
124153 # Ensure notify sockets are removed if cloud-hypervisor didn't exit cleanly the last time
125- rm -f notify.vsock notify.vsock_8888
154+ rm -f ${ vsockPath } ${ vsockPath } _8888
126155
127156 # Start socat to forward systemd notify socket over vsock
128157 if [ -n "'' ${NOTIFY_SOCKET:-}" ]; then
129158 # -T2 is required because cloud-hypervisor does not handle partial
130159 # shutdown of the stream, like systemd v256+ does.
131- ${ pkgs . socat } /bin/socat -T2 UNIX-LISTEN:notify.vsock_8888 ,fork UNIX-SENDTO:$NOTIFY_SOCKET &
160+ ${ pkgs . socat } /bin/socat -T2 UNIX-LISTEN:${ vsockPath } _8888 ,fork UNIX-SENDTO:$NOTIFY_SOCKET &
132161 fi
133162 '' + lib . optionalString graphics . enable ''
134163 rm -f ${ graphics . socket }
142171 done
143172 '' ;
144173
145- inherit supportsNotifySocket ;
146174
147175 command =
148176 if user != null
169197 ++
170198 lib . optionals ( ! hasUserSerial ) [ "--serial" "tty" ]
171199 ++
172- lib . optionals supportsNotifySocket [
173- "--vsock" "cid=3,socket=notify.vsock"
174- ]
200+ lib . optionals ( vsockOpts != "" ) [ "--vsock" vsockOpts ]
175201 ++
176202 lib . optionals graphics . enable [
177203 "--gpu" "socket=${ graphics . socket } "
242268 usb = throw "USB passthrough is not supported on cloud-hypervisor" ;
243269 } . ${ bus } ) devices
244270 )
245- ) + " " + lib . escapeShellArgs extraArgsWithoutPlatform ;
271+ ) + " " + lib . escapeShellArgs processedExtraArgs ;
246272
247273 canShutdown = socket != null ;
248274
0 commit comments