@@ -199,6 +199,50 @@ libkrun_configure_vm (uint32_t ctx_id, void *handle, bool *configured, yajl_val
199199 return 0 ;
200200}
201201
202+ static int
203+ libkrun_configure_flavor (void * cookie , yajl_val * config_tree , libcrun_error_t * err )
204+ {
205+ int sev_indicated = 0 ;
206+ const char * path_flavor [] = { "flavor" , (const char * ) 0 };
207+ struct krun_config * kconf = (struct krun_config * ) cookie ;
208+ yajl_val val_flavor = NULL ;
209+ char * flavor = NULL ;
210+
211+ // Read if the SEV flavor was indicated in the krun VM config.
212+ val_flavor = yajl_tree_get (* config_tree , path_flavor , yajl_t_string );
213+ if (val_flavor != NULL && YAJL_IS_STRING (val_flavor ))
214+ {
215+ flavor = YAJL_GET_STRING (val_flavor );
216+
217+ // The SEV flavor will be used if the krun VM config indicates to use SEV
218+ // within the "flavor" field.
219+ sev_indicated |= (strcmp (flavor , "sev" ) == 0 );
220+ }
221+
222+ // To maintain backward compatibility, also use the SEV flavor if the
223+ // KRUN_SEV_FILE was found.
224+ sev_indicated |= (access (KRUN_SEV_FILE , F_OK ) == 0 );
225+
226+ if (sev_indicated )
227+ {
228+ if (kconf -> handle_sev == NULL )
229+ error (EXIT_FAILURE , 0 , "the container requires libkrun-sev but it's not available" );
230+
231+ kconf -> handle = kconf -> handle_sev ;
232+ kconf -> ctx_id = kconf -> ctx_id_sev ;
233+ kconf -> sev = true;
234+ }
235+ else
236+ {
237+ if (kconf -> handle == NULL )
238+ error (EXIT_FAILURE , 0 , "the container requires libkrun but it's not available" );
239+
240+ kconf -> sev = false;
241+ }
242+
243+ return 0 ;
244+ }
245+
202246static int
203247libkrun_exec (void * cookie , libcrun_container_t * container , const char * pathname , char * const argv [])
204248{
@@ -222,22 +266,12 @@ libkrun_exec (void *cookie, libcrun_container_t *container, const char *pathname
222266 if (UNLIKELY (ret < 0 ))
223267 error (EXIT_FAILURE , - ret , "libkrun VM config exists, but unable to parse" );
224268
225- if (access (KRUN_SEV_FILE , F_OK ) == 0 )
226- {
227- if (kconf -> handle_sev == NULL )
228- error (EXIT_FAILURE , 0 , "the container requires libkrun-sev but it's not available" );
229- handle = kconf -> handle_sev ;
230- ctx_id = kconf -> ctx_id_sev ;
231- kconf -> sev = true;
232- }
233- else
234- {
235- if (kconf -> handle == NULL )
236- error (EXIT_FAILURE , 0 , "the container requires libkrun but it's not available" );
237- handle = kconf -> handle ;
238- ctx_id = kconf -> ctx_id ;
239- kconf -> sev = false;
240- }
269+ ret = libkrun_configure_flavor (cookie , & config_tree , & err );
270+ if (UNLIKELY (ret < 0 ))
271+ error (EXIT_FAILURE , - ret , "unable to configure libkrun flavor" );
272+
273+ handle = kconf -> handle ;
274+ ctx_id = kconf -> ctx_id ;
241275
242276 krun_set_log_level = dlsym (handle , "krun_set_log_level" );
243277 krun_start_enter = dlsym (handle , "krun_start_enter" );
0 commit comments