Skip to content

Commit 0a4c757

Browse files
committed
krun: Determine flavor from VM config tree
To support other krun flavors in the future, parse which flavor (and by extension, which handle) should be used before making any libkrun API calls past libkrun_create_ctx. The correct handle to be used can be determined before any other subsequent API calls. Signed-off-by: Tyler Fanelli <[email protected]>
1 parent 150f7f8 commit 0a4c757

File tree

1 file changed

+52
-16
lines changed

1 file changed

+52
-16
lines changed

src/libcrun/handlers/krun.c

Lines changed: 52 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@
5959
*/
6060
#define KRUN_VM_FILE "/.krun_vm.json"
6161

62+
#define KRUN_FLAVOR_SEV "sev"
63+
6264
struct krun_config
6365
{
6466
void *handle;
@@ -199,6 +201,50 @@ libkrun_configure_vm (uint32_t ctx_id, void *handle, bool *configured, yajl_val
199201
return 0;
200202
}
201203

204+
static int
205+
libkrun_configure_flavor (void *cookie, yajl_val *config_tree, libcrun_error_t *err)
206+
{
207+
int sev_indicated = 0;
208+
const char *path_flavor[] = { "flavor", (const char *) 0 };
209+
struct krun_config *kconf = (struct krun_config *) cookie;
210+
yajl_val val_flavor = NULL;
211+
char *flavor = NULL;
212+
213+
// Read if the SEV flavor was indicated in the krun VM config.
214+
val_flavor = yajl_tree_get (*config_tree, path_flavor, yajl_t_string);
215+
if (val_flavor != NULL && YAJL_IS_STRING (val_flavor))
216+
{
217+
flavor = YAJL_GET_STRING (val_flavor);
218+
219+
// The SEV flavor will be used if the krun VM config indicates to use SEV
220+
// within the "flavor" field.
221+
sev_indicated |= strcmp (flavor, KRUN_FLAVOR_SEV) == 0;
222+
}
223+
224+
// To maintain backward compatibility, also use the SEV flavor if the
225+
// KRUN_SEV_FILE was found.
226+
sev_indicated |= access (KRUN_SEV_FILE, F_OK) == 0;
227+
228+
if (sev_indicated)
229+
{
230+
if (kconf->handle_sev == NULL)
231+
error (EXIT_FAILURE, 0, "the container requires libkrun-sev but it's not available");
232+
233+
kconf->handle = kconf->handle_sev;
234+
kconf->ctx_id = kconf->ctx_id_sev;
235+
kconf->sev = true;
236+
}
237+
else
238+
{
239+
if (kconf->handle == NULL)
240+
error (EXIT_FAILURE, 0, "the container requires libkrun but it's not available");
241+
242+
kconf->sev = false;
243+
}
244+
245+
return 0;
246+
}
247+
202248
static int
203249
libkrun_exec (void *cookie, libcrun_container_t *container, const char *pathname, char *const argv[])
204250
{
@@ -222,22 +268,12 @@ libkrun_exec (void *cookie, libcrun_container_t *container, const char *pathname
222268
if (UNLIKELY (ret < 0))
223269
error (EXIT_FAILURE, -ret, "libkrun VM config exists, but unable to parse");
224270

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-
}
271+
ret = libkrun_configure_flavor (cookie, &config_tree, &err);
272+
if (UNLIKELY (ret < 0))
273+
error (EXIT_FAILURE, -ret, "unable to configure libkrun flavor");
274+
275+
handle = kconf->handle;
276+
ctx_id = kconf->ctx_id;
241277

242278
krun_set_log_level = dlsym (handle, "krun_set_log_level");
243279
krun_start_enter = dlsym (handle, "krun_start_enter");

0 commit comments

Comments
 (0)