Merge tag 'qga-pull-2022-10-26' of https://github.com/kostyanf14/qemu into staging

qga-pull-2022-10-26

# -----BEGIN PGP SIGNATURE-----
#
# iQIzBAABCgAdFiEEwsLBCepDxjwUI+uE711egWG6hOcFAmNZcDMACgkQ711egWG6
# hOdRPQ/8D5T9GpkC+Ar9GLlx/DZFZx0+FxdolV06TcuSXTz3SXYtVKJUSaLjW3SF
# ZRMbBYxqO1NKXWfe8kPKzSR7lJY9fBdn75AUbN88iYW7xu2x+A8LNskVOanu8vbo
# eqcjO1x3sStzdZHfA4uFUsF7J9A2zqflmZxTMWm1UDt1HTklAJLPkb/E6DnVc3Rp
# WhI89JWvAZBOZoVBq7MyfBPEyf3KibHHMWENVY7vGmmXaw9EJQYpXNEMTeBP1VI3
# tTLxrr8WoGr5w2K4a3Kku2ixD+IOPPWXbZXmSjAGgOwiVSkORwMbUVHnN/A11O3O
# b8XOGZ5LkFjgORTsm9ePxXJvcHlsxWZIb80ZnZA9oGF/33S7RbW1Kcl6OAVGdm0S
# ZzysqPLPJStxuUvesqmFfGkvZ29EHNlWjrIfXQz5mnlDsnOi/0Bus0vNjc/kBNPF
# KfHW6MNw4A2gmVcrNg2f2rlYveHa6e+4XsS5xJJ74WDMHWxMZRaFXyO+qU6p7lEJ
# I5pLD9oEM856y2CpQJpoJnZ6ddLEag652x08WpCFMI76x5XLN8D0lszlgJghR7M7
# 84TKSF6HBvrMS+jQc3xZZlK7ELSluKtUFnSTkNZg9WiuQC/FS+7XRcC6lek1qBAL
# WVsKL5UwM/Eyq+O/B8R7w6TEWFxnEFmjddq1rD6kH8LYx1Lmt4M=
# =h41f
# -----END PGP SIGNATURE-----
# gpg: Signature made Wed 26 Oct 2022 13:36:51 EDT
# gpg:                using RSA key C2C2C109EA43C63C1423EB84EF5D5E8161BA84E7
# gpg: Good signature from "Kostiantyn Kostiuk (Upstream PR sign) <kkostiuk@redhat.com>" [unknown]
# gpg: WARNING: This key is not certified with a trusted signature!
# gpg:          There is no indication that the signature belongs to the owner.
# Primary key fingerprint: C2C2 C109 EA43 C63C 1423  EB84 EF5D 5E81 61BA 84E7

* tag 'qga-pull-2022-10-26' of https://github.com/kostyanf14/qemu:
  qga: add channel path to error messages
  qga: Add HW address getting for FreeBSD
  qga: Move HW address getting to a separate function
  qga: Add support for user password setting in FreeBSD
  qga: Add shutdown/halt/reboot support for FreeBSD
  qga: Add UFS freeze/thaw support for FreeBSD
  qga: Move Linux-specific FS freeze/thaw code to a separate file
  qga: Add initial FreeBSD support

Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
This commit is contained in:
Stefan Hajnoczi
2022-10-30 15:07:14 -04:00
8 changed files with 784 additions and 444 deletions
+1 -1
View File
@@ -75,7 +75,7 @@ have_tools = get_option('tools') \
.allowed()
have_ga = get_option('guest_agent') \
.disable_auto_if(not have_system and not have_tools) \
.require(targetos in ['sunos', 'linux', 'windows'],
.require(targetos in ['sunos', 'linux', 'windows', 'freebsd'],
error_message: 'unsupported OS for QEMU guest agent') \
.allowed()
have_block = have_system or have_tools
+21 -2
View File
@@ -138,7 +138,7 @@ static gboolean ga_channel_open(GAChannel *c, const gchar *path,
0
);
if (fd == -1) {
error_setg_errno(errp, errno, "error opening channel");
error_setg_errno(errp, errno, "error opening channel '%s'", path);
return false;
}
#ifdef CONFIG_SOLARIS
@@ -149,6 +149,25 @@ static gboolean ga_channel_open(GAChannel *c, const gchar *path,
return false;
}
#endif
#ifdef __FreeBSD__
/*
* In the default state channel sends echo of every command to a
* client. The client programm doesn't expect this and raises an
* error. Suppress echo by resetting ECHO terminal flag.
*/
struct termios tio;
if (tcgetattr(fd, &tio) < 0) {
error_setg_errno(errp, errno, "error getting channel termios attrs");
close(fd);
return false;
}
tio.c_lflag &= ~ECHO;
if (tcsetattr(fd, TCSAFLUSH, &tio) < 0) {
error_setg_errno(errp, errno, "error setting channel termios attrs");
close(fd);
return false;
}
#endif /* __FreeBSD__ */
ret = ga_channel_client_add(c, fd);
if (ret) {
error_setg(errp, "error adding channel to main loop");
@@ -163,7 +182,7 @@ static gboolean ga_channel_open(GAChannel *c, const gchar *path,
assert(fd < 0);
fd = qga_open_cloexec(path, O_RDWR | O_NOCTTY | O_NONBLOCK, 0);
if (fd == -1) {
error_setg_errno(errp, errno, "error opening channel");
error_setg_errno(errp, errno, "error opening channel '%s'", path);
return false;
}
tcgetattr(fd, &tio);
+200
View File
@@ -0,0 +1,200 @@
/*
* QEMU Guest Agent BSD-specific command implementations
*
* Copyright (c) Virtuozzo International GmbH.
*
* Authors:
* Alexander Ivanov <alexander.ivanov@virtuozzo.com>
*
* 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 "qga-qapi-commands.h"
#include "qapi/qmp/qerror.h"
#include "qapi/error.h"
#include "qemu/queue.h"
#include "commands-common.h"
#include <sys/ioctl.h>
#include <sys/param.h>
#include <sys/ucred.h>
#include <sys/mount.h>
#include <net/if_dl.h>
#include <net/ethernet.h>
#include <paths.h>
#if defined(CONFIG_FSFREEZE) || defined(CONFIG_FSTRIM)
bool build_fs_mount_list(FsMountList *mounts, Error **errp)
{
FsMount *mount;
struct statfs *mntbuf, *mntp;
struct stat statbuf;
int i, count, ret;
count = getmntinfo(&mntbuf, MNT_NOWAIT);
if (count == 0) {
error_setg_errno(errp, errno, "getmntinfo failed");
return false;
}
for (i = 0; i < count; i++) {
mntp = &mntbuf[i];
ret = stat(mntp->f_mntonname, &statbuf);
if (ret != 0) {
error_setg_errno(errp, errno, "stat failed on %s",
mntp->f_mntonname);
return false;
}
mount = g_new0(FsMount, 1);
mount->dirname = g_strdup(mntp->f_mntonname);
mount->devtype = g_strdup(mntp->f_fstypename);
mount->devmajor = major(mount->dev);
mount->devminor = minor(mount->dev);
mount->fsid = mntp->f_fsid;
mount->dev = statbuf.st_dev;
QTAILQ_INSERT_TAIL(mounts, mount, next);
}
return true;
}
#endif /* CONFIG_FSFREEZE || CONFIG_FSTRIM */
#if defined(CONFIG_FSFREEZE)
static int ufssuspend_fd = -1;
static int ufssuspend_cnt;
int64_t qmp_guest_fsfreeze_do_freeze_list(bool has_mountpoints,
strList *mountpoints,
FsMountList mounts,
Error **errp)
{
int ret;
strList *list;
struct FsMount *mount;
if (ufssuspend_fd != -1) {
error_setg(errp, "filesystems have already frozen");
return -1;
}
ufssuspend_cnt = 0;
ufssuspend_fd = qemu_open(_PATH_UFSSUSPEND, O_RDWR, errp);
if (ufssuspend_fd == -1) {
return -1;
}
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 (g_str_equal(list->value, mount->dirname)) {
break;
}
}
if (!list) {
continue;
}
}
/* Only UFS supports suspend */
if (!g_str_equal(mount->devtype, "ufs")) {
continue;
}
ret = ioctl(ufssuspend_fd, UFSSUSPEND, &mount->fsid);
if (ret == -1) {
/*
* ioctl returns EBUSY for all the FS except the first one
* that was suspended
*/
if (errno == EBUSY) {
continue;
}
error_setg_errno(errp, errno, "failed to freeze %s",
mount->dirname);
goto error;
}
ufssuspend_cnt++;
}
return ufssuspend_cnt;
error:
close(ufssuspend_fd);
ufssuspend_fd = -1;
return -1;
}
/*
* We don't need to call UFSRESUME ioctl because all the frozen FS
* are thawed on /dev/ufssuspend closing.
*/
int qmp_guest_fsfreeze_do_thaw(Error **errp)
{
int ret = ufssuspend_cnt;
ufssuspend_cnt = 0;
if (ufssuspend_fd != -1) {
close(ufssuspend_fd);
ufssuspend_fd = -1;
}
return ret;
}
GuestFilesystemInfoList *qmp_guest_get_fsinfo(Error **errp)
{
error_setg(errp, QERR_UNSUPPORTED);
return NULL;
}
GuestDiskInfoList *qmp_guest_get_disks(Error **errp)
{
error_setg(errp, QERR_UNSUPPORTED);
return NULL;
}
GuestDiskStatsInfoList *qmp_guest_get_diskstats(Error **errp)
{
error_setg(errp, QERR_UNSUPPORTED);
return NULL;
}
GuestCpuStatsList *qmp_guest_get_cpustats(Error **errp)
{
error_setg(errp, QERR_UNSUPPORTED);
return NULL;
}
#endif /* CONFIG_FSFREEZE */
#ifdef HAVE_GETIFADDRS
/*
* Fill "buf" with MAC address by ifaddrs. Pointer buf must point to a
* buffer with ETHER_ADDR_LEN length at least.
*
* Returns false in case of an error, otherwise true. "obtained" arguument
* is true if a MAC address was obtained successful, otherwise false.
*/
bool guest_get_hw_addr(struct ifaddrs *ifa, unsigned char *buf,
bool *obtained, Error **errp)
{
struct sockaddr_dl *sdp;
*obtained = false;
if (ifa->ifa_addr->sa_family != AF_LINK) {
/* We can get HW address only for AF_LINK family. */
g_debug("failed to get MAC address of %s", ifa->ifa_name);
return true;
}
sdp = (struct sockaddr_dl *)ifa->ifa_addr;
memcpy(buf, sdp->sdl_data + sdp->sdl_nlen, ETHER_ADDR_LEN);
*obtained = true;
return true;
}
#endif /* HAVE_GETIFADDRS */
+51
View File
@@ -10,6 +10,57 @@
#define QGA_COMMANDS_COMMON_H
#include "qga-qapi-types.h"
#include "guest-agent-core.h"
#include "qemu/queue.h"
#if defined(__linux__)
#include <linux/fs.h>
#ifdef FIFREEZE
#define CONFIG_FSFREEZE
#endif
#ifdef FITRIM
#define CONFIG_FSTRIM
#endif
#endif /* __linux__ */
#ifdef __FreeBSD__
#include <ufs/ffs/fs.h>
#ifdef UFSSUSPEND
#define CONFIG_FSFREEZE
#endif
#endif /* __FreeBSD__ */
#if defined(CONFIG_FSFREEZE) || defined(CONFIG_FSTRIM)
typedef struct FsMount {
char *dirname;
char *devtype;
unsigned int devmajor, devminor;
#if defined(__FreeBSD__)
dev_t dev;
fsid_t fsid;
#endif
QTAILQ_ENTRY(FsMount) next;
} FsMount;
typedef QTAILQ_HEAD(FsMountList, FsMount) FsMountList;
bool build_fs_mount_list(FsMountList *mounts, Error **errp);
void free_fs_mount_list(FsMountList *mounts);
#endif /* CONFIG_FSFREEZE || CONFIG_FSTRIM */
#if defined(CONFIG_FSFREEZE)
int64_t qmp_guest_fsfreeze_do_freeze_list(bool has_mountpoints,
strList *mountpoints,
FsMountList mounts,
Error **errp);
int qmp_guest_fsfreeze_do_thaw(Error **errp);
#endif /* CONFIG_FSFREEZE */
#ifdef HAVE_GETIFADDRS
#include <ifaddrs.h>
bool guest_get_hw_addr(struct ifaddrs *ifa, unsigned char *buf,
bool *obtained, Error **errp);
#endif
typedef struct GuestFileHandle GuestFileHandle;
+286
View File
@@ -0,0 +1,286 @@
/*
* QEMU Guest Agent Linux-specific command implementations
*
* Copyright IBM Corp. 2011
*
* Authors:
* Michael Roth <mdroth@linux.vnet.ibm.com>
* Michal Privoznik <mprivozn@redhat.com>
*
* 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 "commands-common.h"
#include "cutils.h"
#include <mntent.h>
#include <sys/ioctl.h>
#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 */
+213 -434
View File
File diff suppressed because it is too large Load Diff
+6 -7
View File
@@ -37,17 +37,16 @@
#include "qga/service-win32.h"
#include "qga/vss-win32.h"
#endif
#ifdef __linux__
#include <linux/fs.h>
#ifdef FIFREEZE
#define CONFIG_FSFREEZE
#endif
#endif
#include "commands-common.h"
#ifndef _WIN32
#ifdef __FreeBSD__
#define QGA_VIRTIO_PATH_DEFAULT "/dev/vtcon/org.qemu.guest_agent.0"
#else /* __FreeBSD__ */
#define QGA_VIRTIO_PATH_DEFAULT "/dev/virtio-ports/org.qemu.guest_agent.0"
#define QGA_STATE_RELATIVE_DIR "run"
#endif /* __FreeBSD__ */
#define QGA_SERIAL_PATH_DEFAULT "/dev/ttyS0"
#define QGA_STATE_RELATIVE_DIR "run"
#else
#define QGA_VIRTIO_PATH_DEFAULT "\\\\.\\Global\\org.qemu.guest_agent.0"
#define QGA_STATE_RELATIVE_DIR "qemu-ga"
+6
View File
@@ -72,6 +72,12 @@ qga_ss.add(when: 'CONFIG_POSIX', if_true: files(
'commands-posix.c',
'commands-posix-ssh.c',
))
qga_ss.add(when: 'CONFIG_LINUX', if_true: files(
'commands-linux.c',
))
qga_ss.add(when: 'CONFIG_BSD', if_true: files(
'commands-bsd.c',
))
qga_ss.add(when: 'CONFIG_WIN32', if_true: files(
'channel-win32.c',
'commands-win32.c',