Skip to content

Commit f72fb2b

Browse files
committed
examples/nitro: Specify vCPUs and RAM for enclave
Add command line arguments to specify the number of vCPUs and amount of RAM (in MiB) allocated to an enclave. Signed-off-by: Tyler Fanelli <tfanelli@redhat.com>
1 parent 30a94a7 commit f72fb2b

1 file changed

Lines changed: 18 additions & 9 deletions

File tree

examples/nitro.c

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
/*
22
* This is an example implementing running an example AWS nitro enclave with
33
* libkrun.
4-
*
5-
* Given a nitro enclave image, run the image in a nitro enclave with 1 vCPU and
6-
* 256 MiB of memory allocated.
74
*/
85

96
#include <errno.h>
@@ -33,11 +30,13 @@
3330
static void print_help(char *const name)
3431
{
3532
fprintf(stderr,
36-
"Usage: %s EIF_FILE [COMMAND_ARGS...]\n"
33+
"Usage: %s ENCLAVE_IMAGE NVCPUS RAM_MIB\n"
3734
"OPTIONS: \n"
3835
" -h --help Show help\n"
3936
"\n"
4037
"ENCLAVE_IMAGE: The enclave image to run\n",
38+
"NVCPUS: The amount of vCPUs for running the enclave\n",
39+
"RAM_MIB: The amount of RAM (MiB) allocated for enclave\n",
4140
name
4241
);
4342
}
@@ -50,6 +49,8 @@ static const struct option long_options[] = {
5049
struct cmdline {
5150
bool show_help;
5251
const char *eif_path;
52+
unsigned int nvcpus;
53+
unsigned int ram_mib;
5354
};
5455

5556
bool parse_cmdline(int argc, char *const argv[], struct cmdline *cmdline)
@@ -78,11 +79,19 @@ bool parse_cmdline(int argc, char *const argv[], struct cmdline *cmdline)
7879
}
7980
}
8081

81-
if (optind < argc) {
82+
if (optind < argc - 2) {
8283
cmdline->eif_path = argv[optind];
84+
cmdline->nvcpus = strtoul(argv[optind + 1], NULL, 10);
85+
cmdline->ram_mib = strtoul(argv[optind + 2], NULL, 10);
8386
return true;
84-
} else
85-
fprintf(stderr, "Missing EIF_FILE argument");
87+
}
88+
89+
if (optind >= argc - 2)
90+
fprintf(stderr, "Missing RAM_MIB argument\n");
91+
if (optind >= argc - 1)
92+
fprintf(stderr, "Missing VCPUS argument\n");
93+
if (optind == argc)
94+
fprintf(stderr, "Missing ENCLAVE_IMAGE argument\n");
8695

8796
return false;
8897
}
@@ -168,8 +177,8 @@ int main(int argc, char *const argv[])
168177
return -1;
169178
}
170179

171-
// Configure the number of vCPUs (1) and the amount of RAM (512 MiB).
172-
if (err = krun_set_vm_config(ctx_id, 1, 512)) {
180+
// Configure the number of vCPUs and amount of RAM.
181+
if (err = krun_set_vm_config(ctx_id, cmdline.nvcpus, cmdline.ram_mib)) {
173182
errno = -err;
174183
perror("Error configuring the number of vCPUs and/or the amount of RAM");
175184
return -1;

0 commit comments

Comments
 (0)