struct config * copy_config (struct config *old) { struct config *c = new_config (); memcpy (c, old, sizeof *c); /* Need to deep copy strings and string lists. */ if (c->server) c->server = strdup (c->server); if (c->username) c->username = strdup (c->username); if (c->password) c->password = strdup (c->password); if (c->identity_url) c->identity_url = strdup (c->identity_url); if (c->identity_file) c->identity_file = strdup (c->identity_file); if (c->guestname) c->guestname = strdup (c->guestname); if (c->disks) c->disks = guestfs_int_copy_string_list (c->disks); if (c->removable) c->removable = guestfs_int_copy_string_list (c->removable); if (c->interfaces) c->interfaces = guestfs_int_copy_string_list (c->interfaces); if (c->network_map) c->network_map = guestfs_int_copy_string_list (c->network_map); if (c->output) c->output = strdup (c->output); if (c->output_connection) c->output_connection = strdup (c->output_connection); if (c->output_format) c->output_format = strdup (c->output_format); if (c->output_storage) c->output_storage = strdup (c->output_storage); return c; }
int guestfs_impl_set_backend_settings (guestfs_h *g, char *const *settings) { char **copy; copy = guestfs_int_copy_string_list (settings); if (copy == NULL) { perrorf (g, "copy: malloc"); return -1; } guestfs_int_free_string_list (g->backend_settings); g->backend_settings = copy; return 0; }
static void set_config_defaults (struct config *config) { long i; char hostname[257]; int flags; /* Default guest name is derived from the source hostname. If we * assume that the p2v ISO gets its IP address and hostname from * DHCP, then there is at better than average chance that * gethostname will return the real hostname here. It's better than * trying to fish around in the guest filesystem anyway. */ if (gethostname (hostname, sizeof hostname) == -1) { perror ("gethostname"); /* Generate a simple random name. */ if (guestfs_int_random_string (hostname, 8) == -1) { perror ("/dev/urandom"); exit (EXIT_FAILURE); } } else { char *p; /* If the hostname is an FQDN, truncate before the first dot. */ p = strchr (hostname, '.'); if (p && p > hostname) *p = '\0'; } config->guestname = strdup (hostname); /* Defaults for #vcpus and memory are taken from the physical machine. */ i = sysconf (_SC_NPROCESSORS_ONLN); if (i == -1) { perror ("sysconf: _SC_NPROCESSORS_ONLN"); config->vcpus = 1; } else if (i == 0) config->vcpus = 1; else config->vcpus = i; i = sysconf (_SC_PHYS_PAGES); if (i == -1) { perror ("sysconf: _SC_PHYS_PAGES"); config->memory = 1024 * 1024 * 1024; } else config->memory = i; i = sysconf (_SC_PAGESIZE); if (i == -1) { perror ("sysconf: _SC_PAGESIZE"); config->memory *= 4096; } else config->memory *= i; /* Round up the default memory to a power of 2, since the kernel * memory is not included in the total physical pages returned * above. * http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2 */ config->memory--; config->memory |= config->memory >> 1; config->memory |= config->memory >> 2; config->memory |= config->memory >> 4; config->memory |= config->memory >> 8; config->memory |= config->memory >> 16; config->memory |= config->memory >> 32; config->memory++; flags = cpuinfo_flags (); if (flags >= 0) config->flags = flags; else config->flags = 0; find_all_disks (); if (all_disks) config->disks = guestfs_int_copy_string_list (all_disks); if (all_removable) config->removable = guestfs_int_copy_string_list (all_removable); find_all_interfaces (); if (all_interfaces) config->interfaces = guestfs_int_copy_string_list (all_interfaces); /* Default output drops the guest onto /var/tmp on the conversion * server, a hopefully safe default. */ config->output = strdup ("local"); config->output_storage = strdup ("/var/tmp"); }