static void test_stringsbuf (void) { guestfs_h *g; DECLARE_STRINGSBUF (sb); g = guestfs_create (); assert (g); guestfs_int_add_string (g, &sb, "aaa"); guestfs_int_add_string (g, &sb, "bbb"); guestfs_int_add_string (g, &sb, "ccc"); guestfs_int_add_string (g, &sb, ""); guestfs_int_end_stringsbuf (g, &sb); assert (sb.size == 5 /* 4 strings + terminating NULL */); assert (STREQ (sb.argv[0], "aaa")); assert (STREQ (sb.argv[1], "bbb")); assert (STREQ (sb.argv[2], "ccc")); assert (STREQ (sb.argv[3], "")); assert (sb.argv[4] == NULL); assert (guestfs_int_count_strings (sb.argv) == 4); guestfs_int_free_stringsbuf (&sb); guestfs_close (g); }
/* Use vfs-type to look for a filesystem of some sort on 'dev'. * Apart from some types which we ignore, add the result to the * 'ret' string list. */ static int check_with_vfs_type (guestfs_h *g, const char *device, struct stringsbuf *sb) { const char *v; CLEANUP_FREE char *vfs_type = NULL; guestfs_push_error_handler (g, NULL, NULL); vfs_type = guestfs_vfs_type (g, device); guestfs_pop_error_handler (g); if (!vfs_type) v = "unknown"; else if (STREQ (vfs_type, "")) v = "unknown"; else if (STREQ (vfs_type, "btrfs")) { CLEANUP_FREE_BTRFSSUBVOLUME_LIST struct guestfs_btrfssubvolume_list *vols = guestfs_btrfs_subvolume_list (g, device); if (vols == NULL) return -1; for (size_t i = 0; i < vols->len; i++) { struct guestfs_btrfssubvolume *this = &vols->val[i]; guestfs_int_add_sprintf (g, sb, "btrfsvol:%s/%s", device, this->btrfssubvolume_path); guestfs_int_add_string (g, sb, "btrfs"); } v = vfs_type; }
/* Use vfs-type to look for a filesystem of some sort on 'dev'. * Apart from some types which we ignore, add the result to the * 'ret' string list. */ static int check_with_vfs_type (guestfs_h *g, const char *device, struct stringsbuf *sb) { const char *v; CLEANUP_FREE char *vfs_type = NULL; guestfs_push_error_handler (g, NULL, NULL); vfs_type = guestfs_vfs_type (g, device); guestfs_pop_error_handler (g); if (!vfs_type) v = "unknown"; else if (STREQ (vfs_type, "")) v = "unknown"; else if (STREQ (vfs_type, "btrfs")) { CLEANUP_FREE_BTRFSSUBVOLUME_LIST struct guestfs_btrfssubvolume_list *vols = guestfs_btrfs_subvolume_list (g, device); if (vols == NULL) return -1; int64_t default_volume = guestfs_btrfs_subvolume_get_default (g, device); for (size_t i = 0; i < vols->len; i++) { struct guestfs_btrfssubvolume *this = &vols->val[i]; /* Ignore the default subvolume. We get it by simply mounting * the whole device of this btrfs filesystem. */ if (this->btrfssubvolume_id == (uint64_t) default_volume) continue; guestfs_int_add_sprintf (g, sb, "btrfsvol:%s/%s", device, this->btrfssubvolume_path); guestfs_int_add_string (g, sb, "btrfs"); } v = vfs_type; }
/** * Construct the Linux command line passed to the appliance. This is * used by the C<direct> and C<libvirt> backends, and is simply * located in this file because it's a convenient place for this * common code. * * The C<appliance_dev> parameter must be the full device name of the * appliance disk and must have already been adjusted to take into * account virtio-blk or virtio-scsi; eg C</dev/sdb>. * * The C<flags> parameter can contain the following flags logically * or'd together (or 0): * * =over 4 * * =item C<APPLIANCE_COMMAND_LINE_IS_TCG> * * If we are launching a qemu TCG guest (ie. KVM is known to be * disabled or unavailable). If you don't know, don't pass this flag. * * =back * * Note that this function returns a newly allocated buffer which must * be freed by the caller. */ char * guestfs_int_appliance_command_line (guestfs_h *g, const char *appliance_dev, int flags) { CLEANUP_FREE_STRINGSBUF DECLARE_STRINGSBUF (argv); char *term = getenv ("TERM"); bool tcg = flags & APPLIANCE_COMMAND_LINE_IS_TCG; char *ret; /* We assemble the kernel command line by simply joining the final * list of strings with spaces. This means (a) the strings are not * quoted (it's not clear if the kernel can handle quoting in any * case), and (b) we can append multiple parameters in a single * argument, as we must do for the g->append parameter. */ /* Force kernel to panic if daemon exits. */ guestfs_int_add_string (g, &argv, "panic=1"); #ifdef __arm__ guestfs_int_add_sprintf (g, &argv, "mem=%dM", g->memsize); #endif #ifdef __i386__ /* Workaround for RHBZ#857026. */ guestfs_int_add_string (g, &argv, "noapic"); #endif /* Serial console. */ guestfs_int_add_string (g, &argv, SERIAL_CONSOLE); #ifdef EARLYPRINTK /* Get messages from early boot. */ guestfs_int_add_string (g, &argv, EARLYPRINTK); #endif #ifdef __aarch64__ guestfs_int_add_string (g, &argv, "ignore_loglevel"); /* This option turns off the EFI RTC device. QEMU VMs don't * currently provide EFI, and if the device is compiled in it * will try to call the EFI function GetTime unconditionally * (causing a call to NULL). However this option requires a * non-upstream patch. */ guestfs_int_add_string (g, &argv, "efi-rtc=noprobe"); #endif /* RHBZ#1404287 */ guestfs_int_add_string (g, &argv, "edd=off"); /* For slow systems (RHBZ#480319, RHBZ#1096579). */ guestfs_int_add_string (g, &argv, "udevtimeout=6000"); /* Same as above, for newer udevd. */ guestfs_int_add_string (g, &argv, "udev.event-timeout=6000"); /* Fix for RHBZ#502058. */ guestfs_int_add_string (g, &argv, "no_timer_check"); if (tcg) { const int lpj = guestfs_int_get_lpj (g); if (lpj > 0) guestfs_int_add_sprintf (g, &argv, "lpj=%d", lpj); } /* Display timestamp before kernel messages. */ guestfs_int_add_string (g, &argv, "printk.time=1"); /* Saves us about 5 MB of RAM. */ guestfs_int_add_string (g, &argv, "cgroup_disable=memory"); /* Disable USB, only saves about 1ms. */ guestfs_int_add_string (g, &argv, "usbcore.nousb"); /* Disable crypto tests, saves 28ms. */ guestfs_int_add_string (g, &argv, "cryptomgr.notests"); /* Don't synch TSCs when using SMP. Saves 21ms for each secondary vCPU. */ guestfs_int_add_string (g, &argv, "tsc=reliable"); /* Don't scan all 8250 UARTS. */ guestfs_int_add_string (g, &argv, "8250.nr_uarts=1"); /* Tell supermin about the appliance device. */ if (appliance_dev) guestfs_int_add_sprintf (g, &argv, "root=%s", appliance_dev); /* SELinux - deprecated setting, never worked and should not be enabled. */ if (g->selinux) guestfs_int_add_string (g, &argv, "selinux=1 enforcing=0"); else guestfs_int_add_string (g, &argv, "selinux=0"); /* Quiet/verbose. */ if (g->verbose) guestfs_int_add_string (g, &argv, "guestfs_verbose=1"); else guestfs_int_add_string (g, &argv, "quiet"); /* Network. */ if (g->enable_network) guestfs_int_add_string (g, &argv, "guestfs_network=1"); /* TERM environment variable. */ if (term && valid_term (term)) guestfs_int_add_sprintf (g, &argv, "TERM=%s", term); else guestfs_int_add_string (g, &argv, "TERM=linux"); /* Handle identifier. */ if (STRNEQ (g->identifier, "")) guestfs_int_add_sprintf (g, &argv, "guestfs_identifier=%s", g->identifier); /* Append extra arguments. */ if (g->append) guestfs_int_add_string (g, &argv, g->append); guestfs_int_end_stringsbuf (g, &argv); /* Caller frees. */ ret = guestfs_int_join_strings (" ", argv.argv); if (ret == NULL) g->abort_cb (); return ret; }