/* * QEMU Guest Agent Linux-specific command implementations * * Copyright IBM Corp. 2011 * * Authors: * Michael Roth * Michal Privoznik * * This work is licensed under the terms of the GNU GPL, version 2 or later. * See the COPYING file in the top-level directory. */ #include "qemu/osdep.h" #include "qapi/error.h" #include "qga-qapi-commands.h" #include "commands-common.h" #include "cutils.h" #include #include #if defined(CONFIG_FSFREEZE) || defined(CONFIG_FSTRIM) static int dev_major_minor(const char *devpath, unsigned int *devmajor, unsigned int *devminor) { struct stat st; *devmajor = 0; *devminor = 0; if (stat(devpath, &st) < 0) { slog("failed to stat device file '%s': %s", devpath, strerror(errno)); return -1; } if (S_ISDIR(st.st_mode)) { /* It is bind mount */ return -2; } if (S_ISBLK(st.st_mode)) { *devmajor = major(st.st_rdev); *devminor = minor(st.st_rdev); return 0; } return -1; } static bool build_fs_mount_list_from_mtab(FsMountList *mounts, Error **errp) { struct mntent *ment; FsMount *mount; char const *mtab = "/proc/self/mounts"; FILE *fp; unsigned int devmajor, devminor; fp = setmntent(mtab, "r"); if (!fp) { error_setg(errp, "failed to open mtab file: '%s'", mtab); return false; } while ((ment = getmntent(fp))) { /* * An entry which device name doesn't start with a '/' is * either a dummy file system or a network file system. * Add special handling for smbfs and cifs as is done by * coreutils as well. */ if ((ment->mnt_fsname[0] != '/') || (strcmp(ment->mnt_type, "smbfs") == 0) || (strcmp(ment->mnt_type, "cifs") == 0)) { continue; } if (dev_major_minor(ment->mnt_fsname, &devmajor, &devminor) == -2) { /* Skip bind mounts */ continue; } mount = g_new0(FsMount, 1); mount->dirname = g_strdup(ment->mnt_dir); mount->devtype = g_strdup(ment->mnt_type); mount->devmajor = devmajor; mount->devminor = devminor; QTAILQ_INSERT_TAIL(mounts, mount, next); } endmntent(fp); return true; } static void decode_mntname(char *name, int len) { int i, j = 0; for (i = 0; i <= len; i++) { if (name[i] != '\\') { name[j++] = name[i]; } else if (name[i + 1] == '\\') { name[j++] = '\\'; i++; } else if (name[i + 1] >= '0' && name[i + 1] <= '3' && name[i + 2] >= '0' && name[i + 2] <= '7' && name[i + 3] >= '0' && name[i + 3] <= '7') { name[j++] = (name[i + 1] - '0') * 64 + (name[i + 2] - '0') * 8 + (name[i + 3] - '0'); i += 3; } else { name[j++] = name[i]; } } } /* * Walk the mount table and build a list of local file systems */ bool build_fs_mount_list(FsMountList *mounts, Error **errp) { FsMount *mount; char const *mountinfo = "/proc/self/mountinfo"; FILE *fp; char *line = NULL, *dash; size_t n; char check; unsigned int devmajor, devminor; int ret, dir_s, dir_e, type_s, type_e, dev_s, dev_e; fp = fopen(mountinfo, "r"); if (!fp) { return build_fs_mount_list_from_mtab(mounts, errp); } while (getline(&line, &n, fp) != -1) { ret = sscanf(line, "%*u %*u %u:%u %*s %n%*s%n%c", &devmajor, &devminor, &dir_s, &dir_e, &check); if (ret < 3) { continue; } dash = strstr(line + dir_e, " - "); if (!dash) { continue; } ret = sscanf(dash, " - %n%*s%n %n%*s%n%c", &type_s, &type_e, &dev_s, &dev_e, &check); if (ret < 1) { continue; } line[dir_e] = 0; dash[type_e] = 0; dash[dev_e] = 0; decode_mntname(line + dir_s, dir_e - dir_s); decode_mntname(dash + dev_s, dev_e - dev_s); if (devmajor == 0) { /* btrfs reports major number = 0 */ if (strcmp("btrfs", dash + type_s) != 0 || dev_major_minor(dash + dev_s, &devmajor, &devminor) < 0) { continue; } } mount = g_new0(FsMount, 1); mount->dirname = g_strdup(line + dir_s); mount->devtype = g_strdup(dash + type_s); mount->devmajor = devmajor; mount->devminor = devminor; QTAILQ_INSERT_TAIL(mounts, mount, next); } free(line); fclose(fp); return true; } #endif /* CONFIG_FSFREEZE || CONFIG_FSTRIM */ #ifdef CONFIG_FSFREEZE /* * Walk list of mounted file systems in the guest, and freeze the ones which * are real local file systems. */ int64_t qmp_guest_fsfreeze_do_freeze_list(bool has_mountpoints, strList *mountpoints, FsMountList mounts, Error **errp) { struct FsMount *mount; strList *list; int fd, ret, i = 0; QTAILQ_FOREACH_REVERSE(mount, &mounts, next) { /* To issue fsfreeze in the reverse order of mounts, check if the * mount is listed in the list here */ if (has_mountpoints) { for (list = mountpoints; list; list = list->next) { if (strcmp(list->value, mount->dirname) == 0) { break; } } if (!list) { continue; } } fd = qga_open_cloexec(mount->dirname, O_RDONLY, 0); if (fd == -1) { error_setg_errno(errp, errno, "failed to open %s", mount->dirname); return -1; } /* we try to cull filesystems we know won't work in advance, but other * filesystems may not implement fsfreeze for less obvious reasons. * these will report EOPNOTSUPP. we simply ignore these when tallying * the number of frozen filesystems. * if a filesystem is mounted more than once (aka bind mount) a * consecutive attempt to freeze an already frozen filesystem will * return EBUSY. * * any other error means a failure to freeze a filesystem we * expect to be freezable, so return an error in those cases * and return system to thawed state. */ ret = ioctl(fd, FIFREEZE); if (ret == -1) { if (errno != EOPNOTSUPP && errno != EBUSY) { error_setg_errno(errp, errno, "failed to freeze %s", mount->dirname); close(fd); return -1; } } else { i++; } close(fd); } return i; } int qmp_guest_fsfreeze_do_thaw(Error **errp) { int ret; FsMountList mounts; FsMount *mount; int fd, i = 0, logged; Error *local_err = NULL; QTAILQ_INIT(&mounts); if (!build_fs_mount_list(&mounts, &local_err)) { error_propagate(errp, local_err); return -1; } QTAILQ_FOREACH(mount, &mounts, next) { logged = false; fd = qga_open_cloexec(mount->dirname, O_RDONLY, 0); if (fd == -1) { continue; } /* we have no way of knowing whether a filesystem was actually unfrozen * as a result of a successful call to FITHAW, only that if an error * was returned the filesystem was *not* unfrozen by that particular * call. * * since multiple preceding FIFREEZEs require multiple calls to FITHAW * to unfreeze, continuing issuing FITHAW until an error is returned, * in which case either the filesystem is in an unfreezable state, or, * more likely, it was thawed previously (and remains so afterward). * * also, since the most recent successful call is the one that did * the actual unfreeze, we can use this to provide an accurate count * of the number of filesystems unfrozen by guest-fsfreeze-thaw, which * may * be useful for determining whether a filesystem was unfrozen * during the freeze/thaw phase by a process other than qemu-ga. */ do { ret = ioctl(fd, FITHAW); if (ret == 0 && !logged) { i++; logged = true; } } while (ret == 0); close(fd); } free_fs_mount_list(&mounts); return i; } #endif /* CONFIG_FSFREEZE */ #define LINUX_SYS_STATE_FILE "/sys/power/state" #define SUSPEND_SUPPORTED 0 #define SUSPEND_NOT_SUPPORTED 1 typedef enum { SUSPEND_MODE_DISK = 0, SUSPEND_MODE_RAM = 1, SUSPEND_MODE_HYBRID = 2, } SuspendMode; /* * Executes a command in a child process using g_spawn_sync, * returning an int >= 0 representing the exit status of the * process. * * If the program wasn't found in path, returns -1. * * If a problem happened when creating the child process, * returns -1 and errp is set. */ static int run_process_child(const char *command[], Error **errp) { int exit_status, spawn_flag; GError *g_err = NULL; bool success; spawn_flag = G_SPAWN_SEARCH_PATH | G_SPAWN_STDOUT_TO_DEV_NULL | G_SPAWN_STDERR_TO_DEV_NULL; success = g_spawn_sync(NULL, (char **)command, NULL, spawn_flag, NULL, NULL, NULL, NULL, &exit_status, &g_err); if (success) { return WEXITSTATUS(exit_status); } if (g_err && (g_err->code != G_SPAWN_ERROR_NOENT)) { error_setg(errp, "failed to create child process, error '%s'", g_err->message); } g_error_free(g_err); return -1; } static bool systemd_supports_mode(SuspendMode mode, Error **errp) { const char *systemctl_args[3] = {"systemd-hibernate", "systemd-suspend", "systemd-hybrid-sleep"}; const char *cmd[4] = {"systemctl", "status", systemctl_args[mode], NULL}; int status; status = run_process_child(cmd, errp); /* * systemctl status uses LSB return codes so we can expect * status > 0 and be ok. To assert if the guest has support * for the selected suspend mode, status should be < 4. 4 is * the code for unknown service status, the return value when * the service does not exist. A common value is status = 3 * (program is not running). */ if (status > 0 && status < 4) { return true; } return false; } static void systemd_suspend(SuspendMode mode, Error **errp) { Error *local_err = NULL; const char *systemctl_args[3] = {"hibernate", "suspend", "hybrid-sleep"}; const char *cmd[3] = {"systemctl", systemctl_args[mode], NULL}; int status; status = run_process_child(cmd, &local_err); if (status == 0) { return; } if ((status == -1) && !local_err) { error_setg(errp, "the helper program 'systemctl %s' was not found", systemctl_args[mode]); return; } if (local_err) { error_propagate(errp, local_err); } else { error_setg(errp, "the helper program 'systemctl %s' returned an " "unexpected exit status code (%d)", systemctl_args[mode], status); } } static bool pmutils_supports_mode(SuspendMode mode, Error **errp) { Error *local_err = NULL; const char *pmutils_args[3] = {"--hibernate", "--suspend", "--suspend-hybrid"}; const char *cmd[3] = {"pm-is-supported", pmutils_args[mode], NULL}; int status; status = run_process_child(cmd, &local_err); if (status == SUSPEND_SUPPORTED) { return true; } if ((status == -1) && !local_err) { return false; } if (local_err) { error_propagate(errp, local_err); } else { error_setg(errp, "the helper program '%s' returned an unexpected exit" " status code (%d)", "pm-is-supported", status); } return false; } static void pmutils_suspend(SuspendMode mode, Error **errp) { Error *local_err = NULL; const char *pmutils_binaries[3] = {"pm-hibernate", "pm-suspend", "pm-suspend-hybrid"}; const char *cmd[2] = {pmutils_binaries[mode], NULL}; int status; status = run_process_child(cmd, &local_err); if (status == 0) { return; } if ((status == -1) && !local_err) { error_setg(errp, "the helper program '%s' was not found", pmutils_binaries[mode]); return; } if (local_err) { error_propagate(errp, local_err); } else { error_setg(errp, "the helper program '%s' returned an unexpected exit" " status code (%d)", pmutils_binaries[mode], status); } } static bool linux_sys_state_supports_mode(SuspendMode mode, Error **errp) { const char *sysfile_strs[3] = {"disk", "mem", NULL}; const char *sysfile_str = sysfile_strs[mode]; char buf[32]; /* hopefully big enough */ int fd; ssize_t ret; if (!sysfile_str) { error_setg(errp, "unknown guest suspend mode"); return false; } fd = open(LINUX_SYS_STATE_FILE, O_RDONLY); if (fd < 0) { return false; } ret = read(fd, buf, sizeof(buf) - 1); close(fd); if (ret <= 0) { return false; } buf[ret] = '\0'; if (strstr(buf, sysfile_str)) { return true; } return false; } static void linux_sys_state_suspend(SuspendMode mode, Error **errp) { g_autoptr(GError) local_gerr = NULL; const char *sysfile_strs[3] = {"disk", "mem", NULL}; const char *sysfile_str = sysfile_strs[mode]; if (!sysfile_str) { error_setg(errp, "unknown guest suspend mode"); return; } if (!g_file_set_contents(LINUX_SYS_STATE_FILE, sysfile_str, -1, &local_gerr)) { error_setg(errp, "suspend: cannot write to '%s': %s", LINUX_SYS_STATE_FILE, local_gerr->message); return; } } static void guest_suspend(SuspendMode mode, Error **errp) { Error *local_err = NULL; bool mode_supported = false; if (systemd_supports_mode(mode, &local_err)) { mode_supported = true; systemd_suspend(mode, &local_err); if (!local_err) { return; } } error_free(local_err); local_err = NULL; if (pmutils_supports_mode(mode, &local_err)) { mode_supported = true; pmutils_suspend(mode, &local_err); if (!local_err) { return; } } error_free(local_err); local_err = NULL; if (linux_sys_state_supports_mode(mode, &local_err)) { mode_supported = true; linux_sys_state_suspend(mode, &local_err); } if (!mode_supported) { error_free(local_err); error_setg(errp, "the requested suspend mode is not supported by the guest"); } else { error_propagate(errp, local_err); } } void qmp_guest_suspend_disk(Error **errp) { guest_suspend(SUSPEND_MODE_DISK, errp); } void qmp_guest_suspend_ram(Error **errp) { guest_suspend(SUSPEND_MODE_RAM, errp); } void qmp_guest_suspend_hybrid(Error **errp) { guest_suspend(SUSPEND_MODE_HYBRID, errp); } /* Transfer online/offline status between @vcpu and the guest system. * * On input either @errp or *@errp must be NULL. * * In system-to-@vcpu direction, the following @vcpu fields are accessed: * - R: vcpu->logical_id * - W: vcpu->online * - W: vcpu->can_offline * * In @vcpu-to-system direction, the following @vcpu fields are accessed: * - R: vcpu->logical_id * - R: vcpu->online * * Written members remain unmodified on error. */ static void transfer_vcpu(GuestLogicalProcessor *vcpu, bool sys2vcpu, char *dirpath, Error **errp) { int fd; int res; int dirfd; static const char fn[] = "online"; dirfd = open(dirpath, O_RDONLY | O_DIRECTORY); if (dirfd == -1) { error_setg_errno(errp, errno, "open(\"%s\")", dirpath); return; } fd = openat(dirfd, fn, sys2vcpu ? O_RDONLY : O_RDWR); if (fd == -1) { if (errno != ENOENT) { error_setg_errno(errp, errno, "open(\"%s/%s\")", dirpath, fn); } else if (sys2vcpu) { vcpu->online = true; vcpu->can_offline = false; } else if (!vcpu->online) { error_setg(errp, "logical processor #%" PRId64 " can't be " "offlined", vcpu->logical_id); } /* otherwise pretend successful re-onlining */ } else { unsigned char status; res = pread(fd, &status, 1, 0); if (res == -1) { error_setg_errno(errp, errno, "pread(\"%s/%s\")", dirpath, fn); } else if (res == 0) { error_setg(errp, "pread(\"%s/%s\"): unexpected EOF", dirpath, fn); } else if (sys2vcpu) { vcpu->online = (status != '0'); vcpu->can_offline = true; } else if (vcpu->online != (status != '0')) { status = '0' + vcpu->online; if (pwrite(fd, &status, 1, 0) == -1) { error_setg_errno(errp, errno, "pwrite(\"%s/%s\")", dirpath, fn); } } /* otherwise pretend successful re-(on|off)-lining */ res = close(fd); g_assert(res == 0); } res = close(dirfd); g_assert(res == 0); } GuestLogicalProcessorList *qmp_guest_get_vcpus(Error **errp) { GuestLogicalProcessorList *head, **tail; const char *cpu_dir = "/sys/devices/system/cpu"; const gchar *line; g_autoptr(GDir) cpu_gdir = NULL; Error *local_err = NULL; head = NULL; tail = &head; cpu_gdir = g_dir_open(cpu_dir, 0, NULL); if (cpu_gdir == NULL) { error_setg_errno(errp, errno, "failed to list entries: %s", cpu_dir); return NULL; } while (local_err == NULL && (line = g_dir_read_name(cpu_gdir)) != NULL) { GuestLogicalProcessor *vcpu; int64_t id; if (sscanf(line, "cpu%" PRId64, &id)) { g_autofree char *path = g_strdup_printf("/sys/devices/system/cpu/" "cpu%" PRId64 "/", id); vcpu = g_malloc0(sizeof *vcpu); vcpu->logical_id = id; vcpu->has_can_offline = true; /* lolspeak ftw */ transfer_vcpu(vcpu, true, path, &local_err); QAPI_LIST_APPEND(tail, vcpu); } } if (local_err == NULL) { /* there's no guest with zero VCPUs */ g_assert(head != NULL); return head; } qapi_free_GuestLogicalProcessorList(head); error_propagate(errp, local_err); return NULL; } int64_t qmp_guest_set_vcpus(GuestLogicalProcessorList *vcpus, Error **errp) { int64_t processed; Error *local_err = NULL; processed = 0; while (vcpus != NULL) { char *path = g_strdup_printf("/sys/devices/system/cpu/cpu%" PRId64 "/", vcpus->value->logical_id); transfer_vcpu(vcpus->value, false, path, &local_err); g_free(path); if (local_err != NULL) { break; } ++processed; vcpus = vcpus->next; } if (local_err != NULL) { if (processed == 0) { error_propagate(errp, local_err); } else { error_free(local_err); } } return processed; }