/** * Test C<guestfs_int_split_string>. */ static void test_split (void) { char **ret; ret = guestfs_int_split_string (':', ""); assert (ret); assert (guestfs_int_count_strings (ret) == 0); guestfs_int_free_string_list (ret); ret = guestfs_int_split_string (':', "a"); assert (ret); assert (guestfs_int_count_strings (ret) == 1); assert (STREQ (ret[0], "a")); guestfs_int_free_string_list (ret); ret = guestfs_int_split_string (':', ":"); assert (ret); assert (guestfs_int_count_strings (ret) == 2); assert (STREQ (ret[0], "")); assert (STREQ (ret[1], "")); guestfs_int_free_string_list (ret); ret = guestfs_int_split_string (':', "::"); assert (ret); assert (guestfs_int_count_strings (ret) == 3); assert (STREQ (ret[0], "")); assert (STREQ (ret[1], "")); assert (STREQ (ret[2], "")); guestfs_int_free_string_list (ret); ret = guestfs_int_split_string (':', ":a"); assert (ret); assert (guestfs_int_count_strings (ret) == 2); assert (STREQ (ret[0], "")); assert (STREQ (ret[1], "a")); guestfs_int_free_string_list (ret); ret = guestfs_int_split_string (':', "a:"); assert (ret); assert (guestfs_int_count_strings (ret) == 2); assert (STREQ (ret[0], "a")); assert (STREQ (ret[1], "")); guestfs_int_free_string_list (ret); ret = guestfs_int_split_string (':', "a:b:c"); assert (ret); assert (guestfs_int_count_strings (ret) == 3); assert (STREQ (ret[0], "a")); assert (STREQ (ret[1], "b")); assert (STREQ (ret[2], "c")); guestfs_int_free_string_list (ret); }
static int parse_environment (guestfs_h *g, char *(*do_getenv) (const void *data, const char *), const void *data) { int memsize, b; char *str; /* Don't bother checking the return values of functions * that cannot return errors. */ str = do_getenv (data, "LIBGUESTFS_TRACE"); if (str) { b = guestfs_int_is_true (str); if (b == -1) { error (g, _("%s=%s: non-boolean value"), "LIBGUESTFS_TRACE", str); return -1; } guestfs_set_trace (g, b); } str = do_getenv (data, "LIBGUESTFS_DEBUG"); if (str) { b = guestfs_int_is_true (str); if (b == -1) { error (g, _("%s=%s: non-boolean value"), "LIBGUESTFS_TRACE", str); return -1; } guestfs_set_verbose (g, b); } str = do_getenv (data, "LIBGUESTFS_TMPDIR"); if (str && STRNEQ (str, "")) { if (guestfs_set_tmpdir (g, str) == -1) return -1; } str = do_getenv (data, "LIBGUESTFS_CACHEDIR"); if (str && STRNEQ (str, "")) { if (guestfs_set_cachedir (g, str) == -1) return -1; } str = do_getenv (data, "TMPDIR"); if (guestfs_int_set_env_tmpdir (g, str) == -1) return -1; str = do_getenv (data, "LIBGUESTFS_PATH"); if (str && STRNEQ (str, "")) guestfs_set_path (g, str); str = do_getenv (data, "LIBGUESTFS_HV"); if (str && STRNEQ (str, "")) guestfs_set_hv (g, str); else { str = do_getenv (data, "LIBGUESTFS_QEMU"); if (str && STRNEQ (str, "")) guestfs_set_hv (g, str); } str = do_getenv (data, "LIBGUESTFS_APPEND"); if (str) guestfs_set_append (g, str); str = do_getenv (data, "LIBGUESTFS_MEMSIZE"); if (str && STRNEQ (str, "")) { if (sscanf (str, "%d", &memsize) != 1) { error (g, _("non-numeric value for LIBGUESTFS_MEMSIZE")); return -1; } if (guestfs_set_memsize (g, memsize) == -1) { /* set_memsize produces an error message already. */ return -1; } } str = do_getenv (data, "LIBGUESTFS_BACKEND"); if (str && STRNEQ (str, "")) { if (guestfs_set_backend (g, str) == -1) return -1; } else { str = do_getenv (data, "LIBGUESTFS_ATTACH_METHOD"); if (str && STRNEQ (str, "")) { if (guestfs_set_backend (g, str) == -1) return -1; } } str = do_getenv (data, "LIBGUESTFS_BACKEND_SETTINGS"); if (str) { CLEANUP_FREE_STRING_LIST char **settings = guestfs_int_split_string (':', str); if (settings == NULL) { perrorf (g, "split_string: malloc"); return -1; } if (guestfs_set_backend_settings (g, settings) == -1) return -1; } return 0; }
void kernel_configuration (struct config *config, char **cmdline, int cmdline_source) { const char *p; p = get_cmdline_key (cmdline, "p2v.pre"); if (p) run_command (config->verbose, "p2v.pre", p); p = get_cmdline_key (cmdline, "p2v.server"); assert (p); /* checked by caller */ free (config->server); config->server = strdup (p); p = get_cmdline_key (cmdline, "p2v.port"); if (p) { if (sscanf (p, "%d", &config->port) != 1) { fprintf (stderr, "%s: cannot parse p2v.port from kernel command line", guestfs_int_program_name); exit (EXIT_FAILURE); } } p = get_cmdline_key (cmdline, "p2v.username"); if (p) { free (config->username); config->username = strdup (p); } p = get_cmdline_key (cmdline, "p2v.password"); if (p) { free (config->password); config->password = strdup (p); } p = get_cmdline_key (cmdline, "p2v.identity"); if (p) { free (config->identity_url); config->identity_url = strdup (p); config->identity_file_needs_update = 1; } p = get_cmdline_key (cmdline, "p2v.sudo"); if (p) config->sudo = 1; /* We should now be able to connect and interrogate virt-v2v * on the conversion server. */ p = get_cmdline_key (cmdline, "p2v.skip_test_connection"); if (!p) { wait_network_online (config); if (test_connection (config) == -1) { const char *err = get_ssh_error (); fprintf (stderr, "%s: error opening control connection to %s:%d: %s\n", guestfs_int_program_name, config->server, config->port, err); exit (EXIT_FAILURE); } } p = get_cmdline_key (cmdline, "p2v.name"); if (p) { free (config->guestname); config->guestname = strdup (p); } p = get_cmdline_key (cmdline, "p2v.vcpus"); if (p) { if (sscanf (p, "%d", &config->vcpus) != 1) { fprintf (stderr, "%s: cannot parse p2v.vcpus from kernel command line\n", guestfs_int_program_name); exit (EXIT_FAILURE); } } p = get_cmdline_key (cmdline, "p2v.memory"); if (p) { char mem_code; if (sscanf (p, "%" SCNu64 "%c", &config->memory, &mem_code) != 2) { fprintf (stderr, "%s: cannot parse p2v.memory from kernel command line\n", guestfs_int_program_name); exit (EXIT_FAILURE); } config->memory *= 1024; if (mem_code == 'M' || mem_code == 'm' || mem_code == 'G' || mem_code == 'g') config->memory *= 1024; if (mem_code == 'G' || mem_code == 'g') config->memory *= 1024; if (mem_code != 'M' && mem_code != 'm' && mem_code != 'G' && mem_code != 'g') { fprintf (stderr, "%s: p2v.memory on kernel command line must be followed by 'G' or 'M'\n", guestfs_int_program_name); exit (EXIT_FAILURE); } } p = get_cmdline_key (cmdline, "p2v.disks"); if (p) { CLEANUP_FREE char *t; t = strdup (p); guestfs_int_free_string_list (config->disks); config->disks = guestfs_int_split_string (',', t); } p = get_cmdline_key (cmdline, "p2v.removable"); if (p) { CLEANUP_FREE char *t; t = strdup (p); guestfs_int_free_string_list (config->removable); config->removable = guestfs_int_split_string (',', t); } p = get_cmdline_key (cmdline, "p2v.interfaces"); if (p) { CLEANUP_FREE char *t; t = strdup (p); guestfs_int_free_string_list (config->interfaces); config->interfaces = guestfs_int_split_string (',', t); } p = get_cmdline_key (cmdline, "p2v.network"); if (p) { CLEANUP_FREE char *t; t = strdup (p); guestfs_int_free_string_list (config->network_map); config->network_map = guestfs_int_split_string (',', t); } p = get_cmdline_key (cmdline, "p2v.o"); if (p) { free (config->output); config->output = strdup (p); } p = get_cmdline_key (cmdline, "p2v.oa"); if (p) { if (STREQ (p, "sparse")) config->output_allocation = OUTPUT_ALLOCATION_SPARSE; else if (STREQ (p, "preallocated")) config->output_allocation = OUTPUT_ALLOCATION_PREALLOCATED; else fprintf (stderr, "%s: warning: don't know what p2v.oa=%s means\n", guestfs_int_program_name, p); } p = get_cmdline_key (cmdline, "p2v.oc"); if (p) { free (config->output_connection); config->output_connection = strdup (p); } p = get_cmdline_key (cmdline, "p2v.of"); if (p) { free (config->output_format); config->output_format = strdup (p); } p = get_cmdline_key (cmdline, "p2v.os"); if (p) { free (config->output_storage); config->output_storage = strdup (p); } /* Undocumented command line tool used for testing command line parsing. */ p = get_cmdline_key (cmdline, "p2v.dump_config_and_exit"); if (p) { print_config (config, stdout); exit (EXIT_SUCCESS); } /* Some disks must have been specified for conversion. */ if (config->disks == NULL || guestfs_int_count_strings (config->disks) == 0) { fprintf (stderr, "%s: error: no non-removable disks were discovered on this machine.\n", guestfs_int_program_name); fprintf (stderr, "virt-p2v looked in /sys/block and in p2v.disks on the kernel command line.\n"); fprintf (stderr, "This is a fatal error and virt-p2v cannot continue.\n"); exit (EXIT_FAILURE); } /* Perform the conversion in text mode. */ if (start_conversion (config, notify_ui_callback) == -1) { const char *err = get_conversion_error (); fprintf (stderr, "%s: error during conversion: %s\n", guestfs_int_program_name, err); p = get_cmdline_key (cmdline, "p2v.fail"); if (p) run_command (config->verbose, "p2v.fail", p); exit (EXIT_FAILURE); } p = get_cmdline_key (cmdline, "p2v.post"); if (!p) { if (geteuid () == 0 && cmdline_source == CMDLINE_SOURCE_PROC_CMDLINE) p = "poweroff"; } if (p) run_command (config->verbose, "p2v.post", p); }
void parse_config (void) { const char *home; /* Try the global configuration first. */ read_config_from_file (etc_filename); { /* Then read the configuration from XDG system paths. */ const char *xdg_env, *var; CLEANUP_FREE_STRING_LIST char **xdg_config_dirs = NULL; size_t xdg_config_dirs_count; xdg_env = getenv ("XDG_CONFIG_DIRS"); var = xdg_env != NULL && xdg_env[0] != 0 ? xdg_env : "/etc/xdg"; xdg_config_dirs = guestfs_int_split_string (':', var); xdg_config_dirs_count = guestfs_int_count_strings (xdg_config_dirs); for (size_t i = xdg_config_dirs_count; i > 0; --i) { CLEANUP_FREE char *path = NULL; const char *dir = xdg_config_dirs[i - 1]; if (asprintf (&path, "%s/libguestfs/" GLOBAL_CONFIG_FILENAME, dir) == -1) { perror ("asprintf"); exit (EXIT_FAILURE); } read_config_from_file (path); } } /* Read the configuration from $HOME, to override system settings. */ home = getenv ("HOME"); if (home != NULL) { { /* Old-style configuration file first. */ CLEANUP_FREE char *path = NULL; if (asprintf (&path, "%s/%s", home, home_filename) == -1) { perror ("asprintf"); exit (EXIT_FAILURE); } read_config_from_file (path); } { /* Then, XDG_CONFIG_HOME path. */ CLEANUP_FREE char *path = NULL; CLEANUP_FREE char *home_copy = strdup (home); const char *xdg_env; if (home_copy == NULL) { perror ("strdup"); exit (EXIT_FAILURE); } xdg_env = getenv ("XDG_CONFIG_HOME"); if (xdg_env == NULL) { if (asprintf (&path, "%s/.config/libguestfs/" GLOBAL_CONFIG_FILENAME, home_copy) == -1) { perror ("asprintf"); exit (EXIT_FAILURE); } } else { if (asprintf (&path, "%s/libguestfs/" GLOBAL_CONFIG_FILENAME, xdg_env) == -1) { perror ("asprintf"); exit (EXIT_FAILURE); } } read_config_from_file (path); } } }