Skip to content

Commit

Permalink
9pfs: Dynamically add 9pfs options to qemu.
Browse files Browse the repository at this point in the history
Instead of having static options for the 9pfs directory
in hypervisor.args file, pass them dynamically.
We want to have the ability to change the storage options.

Signed-off-by: Archana Shinde <[email protected]>
  • Loading branch information
amshinde committed Jun 19, 2017
1 parent 7b3a743 commit 5249cb4
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 68 deletions.
4 changes: 0 additions & 4 deletions data/hypervisor.args.in
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ memory-backend-file,id=mem0,mem-path=@IMAGE@,size=@SIZE@
@KERNEL@
-append
@KERNEL_PARAMS@ @KERNEL_NET_PARAMS@
-device
virtio-9p-pci,fsdev=workload9p,mount_tag=rootfs
-fsdev
local,id=workload9p,path=@WORKLOAD_DIR@,security_model=none
-smp
2,sockets=1,cores=2,threads=1
-cpu
Expand Down
46 changes: 32 additions & 14 deletions src/hypervisor.c
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,37 @@ cc_oci_append_network_args(struct cc_oci_config *config,
}
}

static gboolean
cc_oci_append_storage_args(struct cc_oci_config *config,
GPtrArray *additional_args)
{
gchar *workload_dir;

if (! (config && additional_args)) {
return false;
}

workload_dir = cc_oci_get_workload_dir(config);
if (! workload_dir) {
g_critical ("No workload");
return false;
}

if (!(workload_dir[0]
&& g_file_test (workload_dir, G_FILE_TEST_IS_DIR))) {
g_critical ("workload directory: %s does not exist",
workload_dir);
return false;
}

g_ptr_array_add(additional_args, g_strdup("-device"));
g_ptr_array_add(additional_args, g_strdup("virtio-9p-pci,fsdev=workload9p,mount_tag=rootfs"));
g_ptr_array_add(additional_args, g_strdup("-fsdev"));
g_ptr_array_add(additional_args, g_strdup_printf("local,id=workload9p,path=%s,security_model=none", workload_dir));

return true;
}

/*!
* Replace any special tokens found in \p args with their expanded
* values.
Expand All @@ -158,7 +189,6 @@ cc_oci_expand_cmdline (struct cc_oci_config *config,
gchar **arg;
gchar *bytes = NULL;
gchar *console_device = NULL;
gchar *workload_dir;
gchar *hypervisor_console = NULL;
g_autofree gchar *procsock_device = NULL;

Expand Down Expand Up @@ -194,11 +224,6 @@ cc_oci_expand_cmdline (struct cc_oci_config *config,

/* We're about to launch the hypervisor so validate paths.*/

workload_dir = cc_oci_get_workload_dir(config);
if (! workload_dir) {
g_critical ("No workload");
goto out;
}

if ((!config->vm->image_path[0])
|| stat (config->vm->image_path, &st) < 0) {
Expand All @@ -214,13 +239,6 @@ cc_oci_expand_cmdline (struct cc_oci_config *config,
return false;
}

if (!(workload_dir[0]
&& g_file_test (workload_dir, G_FILE_TEST_IS_DIR))) {
g_critical ("workload directory: %s does not exist",
workload_dir);
return false;
}

uuid_generate_random(uuid);
for(size_t i=0; i<sizeof(uuid_t) && uuid_index < sizeof(uuid_pattern); ++i) {
/* hex to char */
Expand Down Expand Up @@ -266,7 +284,6 @@ cc_oci_expand_cmdline (struct cc_oci_config *config,
const gchar* name;
const gchar* value;
} special_tags[] = {
{ "@WORKLOAD_DIR@" , workload_dir },
{ "@KERNEL@" , config->vm->kernel_path },
{ "@KERNEL_PARAMS@" , config->vm->kernel_params },
{ "@KERNEL_NET_PARAMS@" , kernel_net_params },
Expand Down Expand Up @@ -497,6 +514,7 @@ cc_oci_populate_extra_args(struct cc_oci_config *config ,
//g_ptr_array_add(additional_args, g_strdup("-device testdevice"));

cc_oci_append_network_args(config, additional_args);
cc_oci_append_storage_args(config, additional_args);

return;
}
71 changes: 21 additions & 50 deletions tests/hypervisor_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,23 +47,21 @@ check_full_expansion (struct cc_oci_config *config,
return false;
}

if (g_strcmp0 (args[0], config->oci.root.path)) {
return false;
}
int i = 0;

if (g_strcmp0 (args[1], config->vm->kernel_path)) {
if (g_strcmp0 (args[i++], config->vm->kernel_path)) {
return false;
}
if (g_strcmp0 (args[2], "param1=foo param2=bar")) {
if (g_strcmp0 (args[i++], "param1=foo param2=bar")) {
return false;
}
if (g_strcmp0 (args[3], config->vm->image_path)) {
if (g_strcmp0 (args[i++], config->vm->image_path)) {
return false;
}
if (g_strcmp0 (args[4], image_size)) {
if (g_strcmp0 (args[i++], image_size)) {
return false;
}
if (g_strcmp0 (args[5], "comms-path")) {
if (g_strcmp0 (args[i++], "comms-path")) {
return false;
}

Expand All @@ -74,19 +72,19 @@ check_full_expansion (struct cc_oci_config *config,
gchar *console = g_strdup_printf ("socket,path=%s,server,nowait,id=charconsole0,signal=off",
hypervisor_console_socket);

gboolean ok = g_strcmp0 (args[6], console) == 0;
gboolean ok = g_strcmp0 (args[i++], console) == 0;
g_free (console);
if (!ok) {
return false;
}

/* NAME is comprised of purely alpha-numeric chars */
if (! g_regex_match_simple ("^[\\S\\D]*$", args[7], 0, 0)) {
if (! g_regex_match_simple ("^[\\S\\D]*$", args[i++], 0, 0)) {
return false;
}

/* UUID is like NAME, but longer and contains dashes */
if (! g_regex_match_simple ("^[\\S\\D-]*$", args[8], 0, 0)) {
if (! g_regex_match_simple ("^[\\S\\D-]*$", args[i], 0, 0)) {
return false;
}

Expand All @@ -101,13 +99,13 @@ check_full_expansion (struct cc_oci_config *config,
* Length is therefore: A + B + C + 4 (for the dashes).
*/
#define EXPECTED_UUID_LEN (8 + 4 + 4 + 4 + 12 + 4)
if (! (g_utf8_strlen (args[8], EXPECTED_UUID_LEN) == EXPECTED_UUID_LEN)) {
if (! (g_utf8_strlen (args[i++], EXPECTED_UUID_LEN) == EXPECTED_UUID_LEN)) {
return false;
}

#undef EXPECTED_UUID_LEN

if (args[9] != NULL) {
if (args[i++] != NULL) {
return false;
}

Expand Down Expand Up @@ -302,17 +300,6 @@ START_TEST(test_cc_oci_expand_cmdline) {
ret = g_file_set_contents (config->vm->kernel_path, "", -1, NULL);
ck_assert (ret);

/* no root.path */
ck_assert (! cc_oci_expand_cmdline (config, args));

path = g_build_path ("/", tmpdir, "workload", NULL);
ck_assert (path);
g_strlcpy (config->oci.root.path, path,
sizeof (config->oci.root.path));
g_free (path);

ck_assert (! g_mkdir (config->oci.root.path, 0750));

g_strlcpy (config->state.comms_path, "comms-path",
sizeof (config->state.comms_path));
g_strlcpy (config->state.runtime_path, "runtime-path",
Expand Down Expand Up @@ -374,18 +361,17 @@ START_TEST(test_cc_oci_expand_cmdline) {
ck_assert (! args[2]);
g_strfreev (args);

args = g_new0 (gchar *, 10);
args = g_new0 (gchar *, 9);
ck_assert (args);
args[0] = g_strdup ("@WORKLOAD_DIR@");
args[1] = g_strdup ("@KERNEL@");
args[2] = g_strdup ("@KERNEL_PARAMS@");
args[3] = g_strdup ("@IMAGE@");
args[4] = g_strdup ("@SIZE@");
args[5] = g_strdup ("@COMMS_SOCKET@");
args[6] = g_strdup ("@CONSOLE_DEVICE@");
args[7] = g_strdup ("@NAME@");
args[8] = g_strdup ("@UUID@");
args[9] = NULL;
args[0] = g_strdup ("@KERNEL@");
args[1] = g_strdup ("@KERNEL_PARAMS@");
args[2] = g_strdup ("@IMAGE@");
args[3] = g_strdup ("@SIZE@");
args[4] = g_strdup ("@COMMS_SOCKET@");
args[5] = g_strdup ("@CONSOLE_DEVICE@");
args[6] = g_strdup ("@NAME@");
args[7] = g_strdup ("@UUID@");
args[8] = NULL;

/* clean up ready for another call */
cc_proxy_free (config->proxy);
Expand Down Expand Up @@ -433,7 +419,6 @@ START_TEST(test_cc_oci_expand_cmdline) {
/* clean up */
ck_assert (! g_remove (config->vm->image_path));
ck_assert (! g_remove (config->vm->kernel_path));
ck_assert (! g_remove (config->oci.root.path));

ck_assert (! g_remove (tmpdir));
g_free (tmpdir);
Expand Down Expand Up @@ -519,18 +504,6 @@ START_TEST(test_cc_oci_vm_args_get) {
ret = g_file_set_contents (config->vm->kernel_path, "", -1, NULL);
ck_assert (ret);

/* no root.path */
ck_assert (! cc_oci_expand_cmdline (config, args));

path = g_build_path ("/", tmpdir, "workload", NULL);
ck_assert (path);
g_strlcpy (config->oci.root.path, path,
sizeof (config->oci.root.path));
g_free (path);

/* create root path */
ck_assert (! g_mkdir (config->oci.root.path, 0750));

/* bundle path is now set, but no args file exists there.
*/
ck_assert (! cc_oci_vm_args_get (config, &args, NULL));
Expand Down Expand Up @@ -707,7 +680,6 @@ START_TEST(test_cc_oci_vm_args_get) {

/* recreate the args file with expandable lines */
ret = g_file_set_contents (args_file,
"@WORKLOAD_DIR@\n"
"@KERNEL@\n"
"@KERNEL_PARAMS@\n"
"@IMAGE@\n"
Expand All @@ -733,7 +705,6 @@ START_TEST(test_cc_oci_vm_args_get) {
ck_assert (! g_remove (args_file));
ck_assert (! g_remove (config->vm->image_path));
ck_assert (! g_remove (config->vm->kernel_path));
ck_assert (! g_remove (config->oci.root.path));

ck_assert (! g_remove (tmpdir));

Expand Down

0 comments on commit 5249cb4

Please sign in to comment.