2 * QEMU Guest Agent POSIX-specific command implementations
4 * Copyright IBM Corp. 2011
7 * Michael Roth <mdroth@linux.vnet.ibm.com>
8 * Michal Privoznik <mprivozn@redhat.com>
10 * This work is licensed under the terms of the GNU GPL, version 2 or later.
11 * See the COPYING file in the top-level directory.
14 #include "qemu/osdep.h"
15 #include <sys/ioctl.h>
16 #include <sys/utsname.h>
19 #include "qga-qapi-commands.h"
20 #include "qapi/error.h"
21 #include "qapi/qmp/qerror.h"
22 #include "qemu/host-utils.h"
23 #include "qemu/sockets.h"
24 #include "qemu/base64.h"
25 #include "qemu/cutils.h"
26 #include "commands-common.h"
27 #include "block/nvme.h"
34 #if defined(__linux__)
36 #include <sys/statvfs.h>
37 #include <linux/nvme_ioctl.h>
44 #ifdef HAVE_GETIFADDRS
45 #include <arpa/inet.h>
46 #include <sys/socket.h>
48 #include <net/ethernet.h>
49 #include <sys/types.h>
51 #include <sys/sockio.h>
55 static void ga_wait_child(pid_t pid
, int *status
, Error
**errp
)
62 rpid
= waitpid(pid
, status
, 0);
63 } while (rpid
== -1 && errno
== EINTR
);
66 error_setg_errno(errp
, errno
, "failed to wait for child (pid: %d)",
71 g_assert(rpid
== pid
);
74 void qmp_guest_shutdown(const char *mode
, Error
**errp
)
76 const char *shutdown_flag
;
77 Error
*local_err
= NULL
;
82 const char *powerdown_flag
= "-i5";
83 const char *halt_flag
= "-i0";
84 const char *reboot_flag
= "-i6";
85 #elif defined(CONFIG_BSD)
86 const char *powerdown_flag
= "-p";
87 const char *halt_flag
= "-h";
88 const char *reboot_flag
= "-r";
90 const char *powerdown_flag
= "-P";
91 const char *halt_flag
= "-H";
92 const char *reboot_flag
= "-r";
95 slog("guest-shutdown called, mode: %s", mode
);
96 if (!mode
|| strcmp(mode
, "powerdown") == 0) {
97 shutdown_flag
= powerdown_flag
;
98 } else if (strcmp(mode
, "halt") == 0) {
99 shutdown_flag
= halt_flag
;
100 } else if (strcmp(mode
, "reboot") == 0) {
101 shutdown_flag
= reboot_flag
;
104 "mode is invalid (valid values are: halt|powerdown|reboot");
110 /* child, start the shutdown */
112 reopen_fd_to_null(0);
113 reopen_fd_to_null(1);
114 reopen_fd_to_null(2);
116 #ifdef CONFIG_SOLARIS
117 execl("/sbin/shutdown", "shutdown", shutdown_flag
, "-g0", "-y",
118 "hypervisor initiated shutdown", (char *)NULL
);
119 #elif defined(CONFIG_BSD)
120 execl("/sbin/shutdown", "shutdown", shutdown_flag
, "+0",
121 "hypervisor initiated shutdown", (char *)NULL
);
123 execl("/sbin/shutdown", "shutdown", "-h", shutdown_flag
, "+0",
124 "hypervisor initiated shutdown", (char *)NULL
);
127 } else if (pid
< 0) {
128 error_setg_errno(errp
, errno
, "failed to create child process");
132 ga_wait_child(pid
, &status
, &local_err
);
134 error_propagate(errp
, local_err
);
138 if (!WIFEXITED(status
)) {
139 error_setg(errp
, "child process has terminated abnormally");
143 if (WEXITSTATUS(status
)) {
144 error_setg(errp
, "child process has failed to shutdown");
151 void qmp_guest_set_time(bool has_time
, int64_t time_ns
, Error
**errp
)
156 Error
*local_err
= NULL
;
158 static const char hwclock_path
[] = "/sbin/hwclock";
159 static int hwclock_available
= -1;
161 if (hwclock_available
< 0) {
162 hwclock_available
= (access(hwclock_path
, X_OK
) == 0);
165 if (!hwclock_available
) {
166 error_setg(errp
, QERR_UNSUPPORTED
);
170 /* If user has passed a time, validate and set it. */
174 /* year-2038 will overflow in case time_t is 32bit */
175 if (time_ns
/ 1000000000 != (time_t)(time_ns
/ 1000000000)) {
176 error_setg(errp
, "Time %" PRId64
" is too large", time_ns
);
180 tv
.tv_sec
= time_ns
/ 1000000000;
181 tv
.tv_usec
= (time_ns
% 1000000000) / 1000;
182 g_date_set_time_t(&date
, tv
.tv_sec
);
183 if (date
.year
< 1970 || date
.year
>= 2070) {
184 error_setg_errno(errp
, errno
, "Invalid time");
188 ret
= settimeofday(&tv
, NULL
);
190 error_setg_errno(errp
, errno
, "Failed to set time to guest");
195 /* Now, if user has passed a time to set and the system time is set, we
196 * just need to synchronize the hardware clock. However, if no time was
197 * passed, user is requesting the opposite: set the system time from the
198 * hardware clock (RTC). */
202 reopen_fd_to_null(0);
203 reopen_fd_to_null(1);
204 reopen_fd_to_null(2);
206 /* Use '/sbin/hwclock -w' to set RTC from the system time,
207 * or '/sbin/hwclock -s' to set the system time from RTC. */
208 execl(hwclock_path
, "hwclock", has_time
? "-w" : "-s", NULL
);
210 } else if (pid
< 0) {
211 error_setg_errno(errp
, errno
, "failed to create child process");
215 ga_wait_child(pid
, &status
, &local_err
);
217 error_propagate(errp
, local_err
);
221 if (!WIFEXITED(status
)) {
222 error_setg(errp
, "child process has terminated abnormally");
226 if (WEXITSTATUS(status
)) {
227 error_setg(errp
, "hwclock failed to set hardware clock to system time");
238 struct GuestFileHandle
{
242 QTAILQ_ENTRY(GuestFileHandle
) next
;
246 QTAILQ_HEAD(, GuestFileHandle
) filehandles
;
247 } guest_file_state
= {
248 .filehandles
= QTAILQ_HEAD_INITIALIZER(guest_file_state
.filehandles
),
251 static int64_t guest_file_handle_add(FILE *fh
, Error
**errp
)
253 GuestFileHandle
*gfh
;
256 handle
= ga_get_fd_handle(ga_state
, errp
);
261 gfh
= g_new0(GuestFileHandle
, 1);
264 QTAILQ_INSERT_TAIL(&guest_file_state
.filehandles
, gfh
, next
);
269 GuestFileHandle
*guest_file_handle_find(int64_t id
, Error
**errp
)
271 GuestFileHandle
*gfh
;
273 QTAILQ_FOREACH(gfh
, &guest_file_state
.filehandles
, next
)
280 error_setg(errp
, "handle '%" PRId64
"' has not been found", id
);
284 typedef const char * const ccpc
;
290 /* http://pubs.opengroup.org/onlinepubs/9699919799/functions/fopen.html */
291 static const struct {
294 } guest_file_open_modes
[] = {
295 { (ccpc
[]){ "r", NULL
}, O_RDONLY
},
296 { (ccpc
[]){ "rb", NULL
}, O_RDONLY
| O_BINARY
},
297 { (ccpc
[]){ "w", NULL
}, O_WRONLY
| O_CREAT
| O_TRUNC
},
298 { (ccpc
[]){ "wb", NULL
}, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
},
299 { (ccpc
[]){ "a", NULL
}, O_WRONLY
| O_CREAT
| O_APPEND
},
300 { (ccpc
[]){ "ab", NULL
}, O_WRONLY
| O_CREAT
| O_APPEND
| O_BINARY
},
301 { (ccpc
[]){ "r+", NULL
}, O_RDWR
},
302 { (ccpc
[]){ "rb+", "r+b", NULL
}, O_RDWR
| O_BINARY
},
303 { (ccpc
[]){ "w+", NULL
}, O_RDWR
| O_CREAT
| O_TRUNC
},
304 { (ccpc
[]){ "wb+", "w+b", NULL
}, O_RDWR
| O_CREAT
| O_TRUNC
| O_BINARY
},
305 { (ccpc
[]){ "a+", NULL
}, O_RDWR
| O_CREAT
| O_APPEND
},
306 { (ccpc
[]){ "ab+", "a+b", NULL
}, O_RDWR
| O_CREAT
| O_APPEND
| O_BINARY
}
310 find_open_flag(const char *mode_str
, Error
**errp
)
314 for (mode
= 0; mode
< ARRAY_SIZE(guest_file_open_modes
); ++mode
) {
317 form
= guest_file_open_modes
[mode
].forms
;
318 while (*form
!= NULL
&& strcmp(*form
, mode_str
) != 0) {
326 if (mode
== ARRAY_SIZE(guest_file_open_modes
)) {
327 error_setg(errp
, "invalid file open mode '%s'", mode_str
);
330 return guest_file_open_modes
[mode
].oflag_base
| O_NOCTTY
| O_NONBLOCK
;
333 #define DEFAULT_NEW_FILE_MODE (S_IRUSR | S_IWUSR | \
334 S_IRGRP | S_IWGRP | \
338 safe_open_or_create(const char *path
, const char *mode
, Error
**errp
)
344 oflag
= find_open_flag(mode
, errp
);
349 /* If the caller wants / allows creation of a new file, we implement it
350 * with a two step process: open() + (open() / fchmod()).
352 * First we insist on creating the file exclusively as a new file. If
353 * that succeeds, we're free to set any file-mode bits on it. (The
354 * motivation is that we want to set those file-mode bits independently
355 * of the current umask.)
357 * If the exclusive creation fails because the file already exists
358 * (EEXIST is not possible for any other reason), we just attempt to
359 * open the file, but in this case we won't be allowed to change the
360 * file-mode bits on the preexistent file.
362 * The pathname should never disappear between the two open()s in
363 * practice. If it happens, then someone very likely tried to race us.
364 * In this case just go ahead and report the ENOENT from the second
365 * open() to the caller.
367 * If the caller wants to open a preexistent file, then the first
368 * open() is decisive and its third argument is ignored, and the second
369 * open() and the fchmod() are never called.
371 fd
= qga_open_cloexec(path
, oflag
| ((oflag
& O_CREAT
) ? O_EXCL
: 0), 0);
372 if (fd
== -1 && errno
== EEXIST
) {
373 oflag
&= ~(unsigned)O_CREAT
;
374 fd
= qga_open_cloexec(path
, oflag
, 0);
377 error_setg_errno(errp
, errno
,
378 "failed to open file '%s' (mode: '%s')",
383 if ((oflag
& O_CREAT
) && fchmod(fd
, DEFAULT_NEW_FILE_MODE
) == -1) {
384 error_setg_errno(errp
, errno
, "failed to set permission "
385 "0%03o on new file '%s' (mode: '%s')",
386 (unsigned)DEFAULT_NEW_FILE_MODE
, path
, mode
);
390 f
= fdopen(fd
, mode
);
392 error_setg_errno(errp
, errno
, "failed to associate stdio stream with "
393 "file descriptor %d, file '%s' (mode: '%s')",
398 if (f
== NULL
&& fd
!= -1) {
400 if (oflag
& O_CREAT
) {
407 int64_t qmp_guest_file_open(const char *path
, const char *mode
,
411 Error
*local_err
= NULL
;
417 slog("guest-file-open called, filepath: %s, mode: %s", path
, mode
);
418 fh
= safe_open_or_create(path
, mode
, &local_err
);
419 if (local_err
!= NULL
) {
420 error_propagate(errp
, local_err
);
424 /* set fd non-blocking to avoid common use cases (like reading from a
425 * named pipe) from hanging the agent
427 if (!g_unix_set_fd_nonblocking(fileno(fh
), true, NULL
)) {
429 error_setg_errno(errp
, errno
, "Failed to set FD nonblocking");
433 handle
= guest_file_handle_add(fh
, errp
);
439 slog("guest-file-open, handle: %" PRId64
, handle
);
443 void qmp_guest_file_close(int64_t handle
, Error
**errp
)
445 GuestFileHandle
*gfh
= guest_file_handle_find(handle
, errp
);
448 slog("guest-file-close called, handle: %" PRId64
, handle
);
453 ret
= fclose(gfh
->fh
);
455 error_setg_errno(errp
, errno
, "failed to close handle");
459 QTAILQ_REMOVE(&guest_file_state
.filehandles
, gfh
, next
);
463 GuestFileRead
*guest_file_read_unsafe(GuestFileHandle
*gfh
,
464 int64_t count
, Error
**errp
)
466 GuestFileRead
*read_data
= NULL
;
471 /* explicitly flush when switching from writing to reading */
472 if (gfh
->state
== RW_STATE_WRITING
) {
473 int ret
= fflush(fh
);
475 error_setg_errno(errp
, errno
, "failed to flush file");
478 gfh
->state
= RW_STATE_NEW
;
481 buf
= g_malloc0(count
+ 1);
482 read_count
= fread(buf
, 1, count
, fh
);
484 error_setg_errno(errp
, errno
, "failed to read file");
487 read_data
= g_new0(GuestFileRead
, 1);
488 read_data
->count
= read_count
;
489 read_data
->eof
= feof(fh
);
491 read_data
->buf_b64
= g_base64_encode(buf
, read_count
);
493 gfh
->state
= RW_STATE_READING
;
501 GuestFileWrite
*qmp_guest_file_write(int64_t handle
, const char *buf_b64
,
502 bool has_count
, int64_t count
,
505 GuestFileWrite
*write_data
= NULL
;
509 GuestFileHandle
*gfh
= guest_file_handle_find(handle
, errp
);
518 if (gfh
->state
== RW_STATE_READING
) {
519 int ret
= fseek(fh
, 0, SEEK_CUR
);
521 error_setg_errno(errp
, errno
, "failed to seek file");
524 gfh
->state
= RW_STATE_NEW
;
527 buf
= qbase64_decode(buf_b64
, -1, &buf_len
, errp
);
534 } else if (count
< 0 || count
> buf_len
) {
535 error_setg(errp
, "value '%" PRId64
"' is invalid for argument count",
541 write_count
= fwrite(buf
, 1, count
, fh
);
543 error_setg_errno(errp
, errno
, "failed to write to file");
544 slog("guest-file-write failed, handle: %" PRId64
, handle
);
546 write_data
= g_new0(GuestFileWrite
, 1);
547 write_data
->count
= write_count
;
548 write_data
->eof
= feof(fh
);
549 gfh
->state
= RW_STATE_WRITING
;
557 struct GuestFileSeek
*qmp_guest_file_seek(int64_t handle
, int64_t offset
,
558 GuestFileWhence
*whence_code
,
561 GuestFileHandle
*gfh
= guest_file_handle_find(handle
, errp
);
562 GuestFileSeek
*seek_data
= NULL
;
572 /* We stupidly exposed 'whence':'int' in our qapi */
573 whence
= ga_parse_whence(whence_code
, &err
);
575 error_propagate(errp
, err
);
580 ret
= fseek(fh
, offset
, whence
);
582 error_setg_errno(errp
, errno
, "failed to seek file");
583 if (errno
== ESPIPE
) {
584 /* file is non-seekable, stdio shouldn't be buffering anyways */
585 gfh
->state
= RW_STATE_NEW
;
588 seek_data
= g_new0(GuestFileSeek
, 1);
589 seek_data
->position
= ftell(fh
);
590 seek_data
->eof
= feof(fh
);
591 gfh
->state
= RW_STATE_NEW
;
598 void qmp_guest_file_flush(int64_t handle
, Error
**errp
)
600 GuestFileHandle
*gfh
= guest_file_handle_find(handle
, errp
);
611 error_setg_errno(errp
, errno
, "failed to flush file");
613 gfh
->state
= RW_STATE_NEW
;
617 #if defined(CONFIG_FSFREEZE) || defined(CONFIG_FSTRIM)
618 void free_fs_mount_list(FsMountList
*mounts
)
620 FsMount
*mount
, *temp
;
626 QTAILQ_FOREACH_SAFE(mount
, mounts
, next
, temp
) {
627 QTAILQ_REMOVE(mounts
, mount
, next
);
628 g_free(mount
->dirname
);
629 g_free(mount
->devtype
);
635 #if defined(CONFIG_FSFREEZE)
637 FSFREEZE_HOOK_THAW
= 0,
638 FSFREEZE_HOOK_FREEZE
,
641 static const char *fsfreeze_hook_arg_string
[] = {
646 static void execute_fsfreeze_hook(FsfreezeHookArg arg
, Error
**errp
)
651 const char *arg_str
= fsfreeze_hook_arg_string
[arg
];
652 Error
*local_err
= NULL
;
654 hook
= ga_fsfreeze_hook(ga_state
);
658 if (access(hook
, X_OK
) != 0) {
659 error_setg_errno(errp
, errno
, "can't access fsfreeze hook '%s'", hook
);
663 slog("executing fsfreeze hook with arg '%s'", arg_str
);
667 reopen_fd_to_null(0);
668 reopen_fd_to_null(1);
669 reopen_fd_to_null(2);
671 execl(hook
, hook
, arg_str
, NULL
);
673 } else if (pid
< 0) {
674 error_setg_errno(errp
, errno
, "failed to create child process");
678 ga_wait_child(pid
, &status
, &local_err
);
680 error_propagate(errp
, local_err
);
684 if (!WIFEXITED(status
)) {
685 error_setg(errp
, "fsfreeze hook has terminated abnormally");
689 status
= WEXITSTATUS(status
);
691 error_setg(errp
, "fsfreeze hook has failed with status %d", status
);
697 * Return status of freeze/thaw
699 GuestFsfreezeStatus
qmp_guest_fsfreeze_status(Error
**errp
)
701 if (ga_is_frozen(ga_state
)) {
702 return GUEST_FSFREEZE_STATUS_FROZEN
;
705 return GUEST_FSFREEZE_STATUS_THAWED
;
708 int64_t qmp_guest_fsfreeze_freeze(Error
**errp
)
710 return qmp_guest_fsfreeze_freeze_list(false, NULL
, errp
);
713 int64_t qmp_guest_fsfreeze_freeze_list(bool has_mountpoints
,
714 strList
*mountpoints
,
719 Error
*local_err
= NULL
;
721 slog("guest-fsfreeze called");
723 execute_fsfreeze_hook(FSFREEZE_HOOK_FREEZE
, &local_err
);
725 error_propagate(errp
, local_err
);
729 QTAILQ_INIT(&mounts
);
730 if (!build_fs_mount_list(&mounts
, &local_err
)) {
731 error_propagate(errp
, local_err
);
735 /* cannot risk guest agent blocking itself on a write in this state */
736 ga_set_frozen(ga_state
);
738 ret
= qmp_guest_fsfreeze_do_freeze_list(has_mountpoints
, mountpoints
,
741 free_fs_mount_list(&mounts
);
742 /* We may not issue any FIFREEZE here.
743 * Just unset ga_state here and ready for the next call.
746 ga_unset_frozen(ga_state
);
747 } else if (ret
< 0) {
748 qmp_guest_fsfreeze_thaw(NULL
);
753 int64_t qmp_guest_fsfreeze_thaw(Error
**errp
)
757 ret
= qmp_guest_fsfreeze_do_thaw(errp
);
759 ga_unset_frozen(ga_state
);
760 execute_fsfreeze_hook(FSFREEZE_HOOK_THAW
, errp
);
768 static void guest_fsfreeze_cleanup(void)
772 if (ga_is_frozen(ga_state
) == GUEST_FSFREEZE_STATUS_FROZEN
) {
773 qmp_guest_fsfreeze_thaw(&err
);
775 slog("failed to clean up frozen filesystems: %s",
776 error_get_pretty(err
));
783 /* linux-specific implementations. avoid this if at all possible. */
784 #if defined(__linux__)
785 #if defined(CONFIG_FSFREEZE)
787 static char *get_pci_driver(char const *syspath
, int pathlen
, Error
**errp
)
795 path
= g_strndup(syspath
, pathlen
);
796 dpath
= g_strdup_printf("%s/driver", path
);
797 len
= readlink(dpath
, buf
, sizeof(buf
) - 1);
800 driver
= g_path_get_basename(buf
);
807 static int compare_uint(const void *_a
, const void *_b
)
809 unsigned int a
= *(unsigned int *)_a
;
810 unsigned int b
= *(unsigned int *)_b
;
812 return a
< b
? -1 : a
> b
? 1 : 0;
815 /* Walk the specified sysfs and build a sorted list of host or ata numbers */
816 static int build_hosts(char const *syspath
, char const *host
, bool ata
,
817 unsigned int *hosts
, int hosts_max
, Error
**errp
)
821 struct dirent
*entry
;
824 path
= g_strndup(syspath
, host
- syspath
);
827 error_setg_errno(errp
, errno
, "opendir(\"%s\")", path
);
832 while (i
< hosts_max
) {
833 entry
= readdir(dir
);
837 if (ata
&& sscanf(entry
->d_name
, "ata%d", hosts
+ i
) == 1) {
839 } else if (!ata
&& sscanf(entry
->d_name
, "host%d", hosts
+ i
) == 1) {
844 qsort(hosts
, i
, sizeof(hosts
[0]), compare_uint
);
852 * Store disk device info for devices on the PCI bus.
853 * Returns true if information has been stored, or false for failure.
855 static bool build_guest_fsinfo_for_pci_dev(char const *syspath
,
856 GuestDiskAddress
*disk
,
859 unsigned int pci
[4], host
, hosts
[8], tgt
[3];
860 int i
, nhosts
= 0, pcilen
;
861 GuestPCIAddress
*pciaddr
= disk
->pci_controller
;
862 bool has_ata
= false, has_host
= false, has_tgt
= false;
863 char *p
, *q
, *driver
= NULL
;
866 p
= strstr(syspath
, "/devices/pci");
867 if (!p
|| sscanf(p
+ 12, "%*x:%*x/%x:%x:%x.%x%n",
868 pci
, pci
+ 1, pci
+ 2, pci
+ 3, &pcilen
) < 4) {
869 g_debug("only pci device is supported: sysfs path '%s'", syspath
);
875 driver
= get_pci_driver(syspath
, p
- syspath
, errp
);
876 if (driver
&& (g_str_equal(driver
, "ata_piix") ||
877 g_str_equal(driver
, "sym53c8xx") ||
878 g_str_equal(driver
, "virtio-pci") ||
879 g_str_equal(driver
, "ahci") ||
880 g_str_equal(driver
, "nvme"))) {
885 if (sscanf(p
, "/%x:%x:%x.%x%n",
886 pci
, pci
+ 1, pci
+ 2, pci
+ 3, &pcilen
) == 4) {
891 g_debug("unsupported driver or sysfs path '%s'", syspath
);
895 p
= strstr(syspath
, "/target");
896 if (p
&& sscanf(p
+ 7, "%*u:%*u:%*u/%*u:%u:%u:%u",
897 tgt
, tgt
+ 1, tgt
+ 2) == 3) {
901 p
= strstr(syspath
, "/ata");
906 p
= strstr(syspath
, "/host");
909 if (p
&& sscanf(q
, "%u", &host
) == 1) {
911 nhosts
= build_hosts(syspath
, p
, has_ata
, hosts
,
912 ARRAY_SIZE(hosts
), errp
);
918 pciaddr
->domain
= pci
[0];
919 pciaddr
->bus
= pci
[1];
920 pciaddr
->slot
= pci
[2];
921 pciaddr
->function
= pci
[3];
923 if (strcmp(driver
, "ata_piix") == 0) {
924 /* a host per ide bus, target*:0:<unit>:0 */
925 if (!has_host
|| !has_tgt
) {
926 g_debug("invalid sysfs path '%s' (driver '%s')", syspath
, driver
);
929 for (i
= 0; i
< nhosts
; i
++) {
930 if (host
== hosts
[i
]) {
931 disk
->bus_type
= GUEST_DISK_BUS_TYPE_IDE
;
938 g_debug("no host for '%s' (driver '%s')", syspath
, driver
);
941 } else if (strcmp(driver
, "sym53c8xx") == 0) {
942 /* scsi(LSI Logic): target*:0:<unit>:0 */
944 g_debug("invalid sysfs path '%s' (driver '%s')", syspath
, driver
);
947 disk
->bus_type
= GUEST_DISK_BUS_TYPE_SCSI
;
949 } else if (strcmp(driver
, "virtio-pci") == 0) {
951 /* virtio-scsi: target*:0:0:<unit> */
952 disk
->bus_type
= GUEST_DISK_BUS_TYPE_SCSI
;
955 /* virtio-blk: 1 disk per 1 device */
956 disk
->bus_type
= GUEST_DISK_BUS_TYPE_VIRTIO
;
958 } else if (strcmp(driver
, "ahci") == 0) {
959 /* ahci: 1 host per 1 unit */
960 if (!has_host
|| !has_tgt
) {
961 g_debug("invalid sysfs path '%s' (driver '%s')", syspath
, driver
);
964 for (i
= 0; i
< nhosts
; i
++) {
965 if (host
== hosts
[i
]) {
967 disk
->bus_type
= GUEST_DISK_BUS_TYPE_SATA
;
972 g_debug("no host for '%s' (driver '%s')", syspath
, driver
);
975 } else if (strcmp(driver
, "nvme") == 0) {
976 disk
->bus_type
= GUEST_DISK_BUS_TYPE_NVME
;
978 g_debug("unknown driver '%s' (sysfs path '%s')", driver
, syspath
);
990 * Store disk device info for non-PCI virtio devices (for example s390x
991 * channel I/O devices). Returns true if information has been stored, or
994 static bool build_guest_fsinfo_for_nonpci_virtio(char const *syspath
,
995 GuestDiskAddress
*disk
,
1001 if (!strstr(syspath
, "/virtio") || !strstr(syspath
, "/block")) {
1002 g_debug("Unsupported virtio device '%s'", syspath
);
1006 p
= strstr(syspath
, "/target");
1007 if (p
&& sscanf(p
+ 7, "%*u:%*u:%*u/%*u:%u:%u:%u",
1008 &tgt
[0], &tgt
[1], &tgt
[2]) == 3) {
1009 /* virtio-scsi: target*:0:<target>:<unit> */
1010 disk
->bus_type
= GUEST_DISK_BUS_TYPE_SCSI
;
1012 disk
->target
= tgt
[1];
1013 disk
->unit
= tgt
[2];
1015 /* virtio-blk: 1 disk per 1 device */
1016 disk
->bus_type
= GUEST_DISK_BUS_TYPE_VIRTIO
;
1023 * Store disk device info for CCW devices (s390x channel I/O devices).
1024 * Returns true if information has been stored, or false for failure.
1026 static bool build_guest_fsinfo_for_ccw_dev(char const *syspath
,
1027 GuestDiskAddress
*disk
,
1030 unsigned int cssid
, ssid
, subchno
, devno
;
1033 p
= strstr(syspath
, "/devices/css");
1034 if (!p
|| sscanf(p
+ 12, "%*x/%x.%x.%x/%*x.%*x.%x/",
1035 &cssid
, &ssid
, &subchno
, &devno
) < 4) {
1036 g_debug("could not parse ccw device sysfs path: %s", syspath
);
1040 disk
->ccw_address
= g_new0(GuestCCWAddress
, 1);
1041 disk
->ccw_address
->cssid
= cssid
;
1042 disk
->ccw_address
->ssid
= ssid
;
1043 disk
->ccw_address
->subchno
= subchno
;
1044 disk
->ccw_address
->devno
= devno
;
1046 if (strstr(p
, "/virtio")) {
1047 build_guest_fsinfo_for_nonpci_virtio(syspath
, disk
, errp
);
1053 /* Store disk device info specified by @sysfs into @fs */
1054 static void build_guest_fsinfo_for_real_device(char const *syspath
,
1055 GuestFilesystemInfo
*fs
,
1058 GuestDiskAddress
*disk
;
1059 GuestPCIAddress
*pciaddr
;
1061 #ifdef CONFIG_LIBUDEV
1062 struct udev
*udev
= NULL
;
1063 struct udev_device
*udevice
= NULL
;
1066 pciaddr
= g_new0(GuestPCIAddress
, 1);
1067 pciaddr
->domain
= -1; /* -1 means field is invalid */
1070 pciaddr
->function
= -1;
1072 disk
= g_new0(GuestDiskAddress
, 1);
1073 disk
->pci_controller
= pciaddr
;
1074 disk
->bus_type
= GUEST_DISK_BUS_TYPE_UNKNOWN
;
1076 #ifdef CONFIG_LIBUDEV
1078 udevice
= udev_device_new_from_syspath(udev
, syspath
);
1079 if (udev
== NULL
|| udevice
== NULL
) {
1080 g_debug("failed to query udev");
1082 const char *devnode
, *serial
;
1083 devnode
= udev_device_get_devnode(udevice
);
1084 if (devnode
!= NULL
) {
1085 disk
->dev
= g_strdup(devnode
);
1087 serial
= udev_device_get_property_value(udevice
, "ID_SERIAL");
1088 if (serial
!= NULL
&& *serial
!= 0) {
1089 disk
->serial
= g_strdup(serial
);
1094 udev_device_unref(udevice
);
1097 if (strstr(syspath
, "/devices/pci")) {
1098 has_hwinf
= build_guest_fsinfo_for_pci_dev(syspath
, disk
, errp
);
1099 } else if (strstr(syspath
, "/devices/css")) {
1100 has_hwinf
= build_guest_fsinfo_for_ccw_dev(syspath
, disk
, errp
);
1101 } else if (strstr(syspath
, "/virtio")) {
1102 has_hwinf
= build_guest_fsinfo_for_nonpci_virtio(syspath
, disk
, errp
);
1104 g_debug("Unsupported device type for '%s'", syspath
);
1108 if (has_hwinf
|| disk
->dev
|| disk
->serial
) {
1109 QAPI_LIST_PREPEND(fs
->disk
, disk
);
1111 qapi_free_GuestDiskAddress(disk
);
1115 static void build_guest_fsinfo_for_device(char const *devpath
,
1116 GuestFilesystemInfo
*fs
,
1119 /* Store a list of slave devices of virtual volume specified by @syspath into
1121 static void build_guest_fsinfo_for_virtual_device(char const *syspath
,
1122 GuestFilesystemInfo
*fs
,
1128 struct dirent
*entry
;
1130 dirpath
= g_strdup_printf("%s/slaves", syspath
);
1131 dir
= opendir(dirpath
);
1133 if (errno
!= ENOENT
) {
1134 error_setg_errno(errp
, errno
, "opendir(\"%s\")", dirpath
);
1142 entry
= readdir(dir
);
1143 if (entry
== NULL
) {
1145 error_setg_errno(errp
, errno
, "readdir(\"%s\")", dirpath
);
1150 if (entry
->d_type
== DT_LNK
) {
1153 g_debug(" slave device '%s'", entry
->d_name
);
1154 path
= g_strdup_printf("%s/slaves/%s", syspath
, entry
->d_name
);
1155 build_guest_fsinfo_for_device(path
, fs
, &err
);
1159 error_propagate(errp
, err
);
1169 static bool is_disk_virtual(const char *devpath
, Error
**errp
)
1171 g_autofree
char *syspath
= realpath(devpath
, NULL
);
1174 error_setg_errno(errp
, errno
, "realpath(\"%s\")", devpath
);
1177 return strstr(syspath
, "/devices/virtual/block/") != NULL
;
1180 /* Dispatch to functions for virtual/real device */
1181 static void build_guest_fsinfo_for_device(char const *devpath
,
1182 GuestFilesystemInfo
*fs
,
1186 g_autofree
char *syspath
= NULL
;
1187 bool is_virtual
= false;
1189 syspath
= realpath(devpath
, NULL
);
1191 if (errno
!= ENOENT
) {
1192 error_setg_errno(errp
, errno
, "realpath(\"%s\")", devpath
);
1196 /* ENOENT: This devpath may not exist because of container config */
1198 fs
->name
= g_path_get_basename(devpath
);
1204 fs
->name
= g_path_get_basename(syspath
);
1207 g_debug(" parse sysfs path '%s'", syspath
);
1208 is_virtual
= is_disk_virtual(syspath
, errp
);
1209 if (*errp
!= NULL
) {
1213 build_guest_fsinfo_for_virtual_device(syspath
, fs
, errp
);
1215 build_guest_fsinfo_for_real_device(syspath
, fs
, errp
);
1219 #ifdef CONFIG_LIBUDEV
1222 * Wrapper around build_guest_fsinfo_for_device() for getting just
1225 static GuestDiskAddress
*get_disk_address(const char *syspath
, Error
**errp
)
1227 g_autoptr(GuestFilesystemInfo
) fs
= NULL
;
1229 fs
= g_new0(GuestFilesystemInfo
, 1);
1230 build_guest_fsinfo_for_device(syspath
, fs
, errp
);
1231 if (fs
->disk
!= NULL
) {
1232 return g_steal_pointer(&fs
->disk
->value
);
1237 static char *get_alias_for_syspath(const char *syspath
)
1239 struct udev
*udev
= NULL
;
1240 struct udev_device
*udevice
= NULL
;
1245 g_debug("failed to query udev");
1248 udevice
= udev_device_new_from_syspath(udev
, syspath
);
1249 if (udevice
== NULL
) {
1250 g_debug("failed to query udev for path: %s", syspath
);
1253 const char *alias
= udev_device_get_property_value(
1254 udevice
, "DM_NAME");
1256 * NULL means there was an error and empty string means there is no
1257 * alias. In case of no alias we return NULL instead of empty string.
1259 if (alias
== NULL
) {
1260 g_debug("failed to query udev for device alias for: %s",
1262 } else if (*alias
!= 0) {
1263 ret
= g_strdup(alias
);
1269 udev_device_unref(udevice
);
1273 static char *get_device_for_syspath(const char *syspath
)
1275 struct udev
*udev
= NULL
;
1276 struct udev_device
*udevice
= NULL
;
1281 g_debug("failed to query udev");
1284 udevice
= udev_device_new_from_syspath(udev
, syspath
);
1285 if (udevice
== NULL
) {
1286 g_debug("failed to query udev for path: %s", syspath
);
1289 ret
= g_strdup(udev_device_get_devnode(udevice
));
1294 udev_device_unref(udevice
);
1298 static void get_disk_deps(const char *disk_dir
, GuestDiskInfo
*disk
)
1300 g_autofree
char *deps_dir
= NULL
;
1302 GDir
*dp_deps
= NULL
;
1304 /* List dependent disks */
1305 deps_dir
= g_strdup_printf("%s/slaves", disk_dir
);
1306 g_debug(" listing entries in: %s", deps_dir
);
1307 dp_deps
= g_dir_open(deps_dir
, 0, NULL
);
1308 if (dp_deps
== NULL
) {
1309 g_debug("failed to list entries in %s", deps_dir
);
1312 disk
->has_dependencies
= true;
1313 while ((dep
= g_dir_read_name(dp_deps
)) != NULL
) {
1314 g_autofree
char *dep_dir
= NULL
;
1317 /* Add dependent disks */
1318 dep_dir
= g_strdup_printf("%s/%s", deps_dir
, dep
);
1319 dev_name
= get_device_for_syspath(dep_dir
);
1320 if (dev_name
!= NULL
) {
1321 g_debug(" adding dependent device: %s", dev_name
);
1322 QAPI_LIST_PREPEND(disk
->dependencies
, dev_name
);
1325 g_dir_close(dp_deps
);
1329 * Detect partitions subdirectory, name is "<disk_name><number>" or
1330 * "<disk_name>p<number>"
1332 * @disk_name -- last component of /sys path (e.g. sda)
1333 * @disk_dir -- sys path of the disk (e.g. /sys/block/sda)
1334 * @disk_dev -- device node of the disk (e.g. /dev/sda)
1336 static GuestDiskInfoList
*get_disk_partitions(
1337 GuestDiskInfoList
*list
,
1338 const char *disk_name
, const char *disk_dir
,
1339 const char *disk_dev
)
1341 GuestDiskInfoList
*ret
= list
;
1342 struct dirent
*de_disk
;
1343 DIR *dp_disk
= NULL
;
1344 size_t len
= strlen(disk_name
);
1346 dp_disk
= opendir(disk_dir
);
1347 while ((de_disk
= readdir(dp_disk
)) != NULL
) {
1348 g_autofree
char *partition_dir
= NULL
;
1350 GuestDiskInfo
*partition
;
1352 if (!(de_disk
->d_type
& DT_DIR
)) {
1356 if (!(strncmp(disk_name
, de_disk
->d_name
, len
) == 0 &&
1357 ((*(de_disk
->d_name
+ len
) == 'p' &&
1358 isdigit(*(de_disk
->d_name
+ len
+ 1))) ||
1359 isdigit(*(de_disk
->d_name
+ len
))))) {
1363 partition_dir
= g_strdup_printf("%s/%s",
1364 disk_dir
, de_disk
->d_name
);
1365 dev_name
= get_device_for_syspath(partition_dir
);
1366 if (dev_name
== NULL
) {
1367 g_debug("Failed to get device name for syspath: %s",
1371 partition
= g_new0(GuestDiskInfo
, 1);
1372 partition
->name
= dev_name
;
1373 partition
->partition
= true;
1374 partition
->has_dependencies
= true;
1375 /* Add parent disk as dependent for easier tracking of hierarchy */
1376 QAPI_LIST_PREPEND(partition
->dependencies
, g_strdup(disk_dev
));
1378 QAPI_LIST_PREPEND(ret
, partition
);
1385 static void get_nvme_smart(GuestDiskInfo
*disk
)
1388 GuestNVMeSmart
*smart
;
1389 NvmeSmartLog log
= {0};
1390 struct nvme_admin_cmd cmd
= {
1391 .opcode
= NVME_ADM_CMD_GET_LOG_PAGE
,
1392 .nsid
= NVME_NSID_BROADCAST
,
1393 .addr
= (uintptr_t)&log
,
1394 .data_len
= sizeof(log
),
1395 .cdw10
= NVME_LOG_SMART_INFO
| (1 << 15) /* RAE bit */
1396 | (((sizeof(log
) >> 2) - 1) << 16)
1399 fd
= qga_open_cloexec(disk
->name
, O_RDONLY
, 0);
1401 g_debug("Failed to open device: %s: %s", disk
->name
, g_strerror(errno
));
1405 if (ioctl(fd
, NVME_IOCTL_ADMIN_CMD
, &cmd
)) {
1406 g_debug("Failed to get smart: %s: %s", disk
->name
, g_strerror(errno
));
1411 disk
->smart
= g_new0(GuestDiskSmart
, 1);
1412 disk
->smart
->type
= GUEST_DISK_BUS_TYPE_NVME
;
1414 smart
= &disk
->smart
->u
.nvme
;
1415 smart
->critical_warning
= log
.critical_warning
;
1416 smart
->temperature
= lduw_le_p(&log
.temperature
); /* unaligned field */
1417 smart
->available_spare
= log
.available_spare
;
1418 smart
->available_spare_threshold
= log
.available_spare_threshold
;
1419 smart
->percentage_used
= log
.percentage_used
;
1420 smart
->data_units_read_lo
= le64_to_cpu(log
.data_units_read
[0]);
1421 smart
->data_units_read_hi
= le64_to_cpu(log
.data_units_read
[1]);
1422 smart
->data_units_written_lo
= le64_to_cpu(log
.data_units_written
[0]);
1423 smart
->data_units_written_hi
= le64_to_cpu(log
.data_units_written
[1]);
1424 smart
->host_read_commands_lo
= le64_to_cpu(log
.host_read_commands
[0]);
1425 smart
->host_read_commands_hi
= le64_to_cpu(log
.host_read_commands
[1]);
1426 smart
->host_write_commands_lo
= le64_to_cpu(log
.host_write_commands
[0]);
1427 smart
->host_write_commands_hi
= le64_to_cpu(log
.host_write_commands
[1]);
1428 smart
->controller_busy_time_lo
= le64_to_cpu(log
.controller_busy_time
[0]);
1429 smart
->controller_busy_time_hi
= le64_to_cpu(log
.controller_busy_time
[1]);
1430 smart
->power_cycles_lo
= le64_to_cpu(log
.power_cycles
[0]);
1431 smart
->power_cycles_hi
= le64_to_cpu(log
.power_cycles
[1]);
1432 smart
->power_on_hours_lo
= le64_to_cpu(log
.power_on_hours
[0]);
1433 smart
->power_on_hours_hi
= le64_to_cpu(log
.power_on_hours
[1]);
1434 smart
->unsafe_shutdowns_lo
= le64_to_cpu(log
.unsafe_shutdowns
[0]);
1435 smart
->unsafe_shutdowns_hi
= le64_to_cpu(log
.unsafe_shutdowns
[1]);
1436 smart
->media_errors_lo
= le64_to_cpu(log
.media_errors
[0]);
1437 smart
->media_errors_hi
= le64_to_cpu(log
.media_errors
[1]);
1438 smart
->number_of_error_log_entries_lo
=
1439 le64_to_cpu(log
.number_of_error_log_entries
[0]);
1440 smart
->number_of_error_log_entries_hi
=
1441 le64_to_cpu(log
.number_of_error_log_entries
[1]);
1446 static void get_disk_smart(GuestDiskInfo
*disk
)
1449 && (disk
->address
->bus_type
== GUEST_DISK_BUS_TYPE_NVME
)) {
1450 get_nvme_smart(disk
);
1454 GuestDiskInfoList
*qmp_guest_get_disks(Error
**errp
)
1456 GuestDiskInfoList
*ret
= NULL
;
1457 GuestDiskInfo
*disk
;
1459 struct dirent
*de
= NULL
;
1461 g_debug("listing /sys/block directory");
1462 dp
= opendir("/sys/block");
1464 error_setg_errno(errp
, errno
, "Can't open directory \"/sys/block\"");
1467 while ((de
= readdir(dp
)) != NULL
) {
1468 g_autofree
char *disk_dir
= NULL
, *line
= NULL
,
1471 Error
*local_err
= NULL
;
1472 if (de
->d_type
!= DT_LNK
) {
1473 g_debug(" skipping entry: %s", de
->d_name
);
1477 /* Check size and skip zero-sized disks */
1478 g_debug(" checking disk size");
1479 size_path
= g_strdup_printf("/sys/block/%s/size", de
->d_name
);
1480 if (!g_file_get_contents(size_path
, &line
, NULL
, NULL
)) {
1481 g_debug(" failed to read disk size");
1484 if (g_strcmp0(line
, "0\n") == 0) {
1485 g_debug(" skipping zero-sized disk");
1489 g_debug(" adding %s", de
->d_name
);
1490 disk_dir
= g_strdup_printf("/sys/block/%s", de
->d_name
);
1491 dev_name
= get_device_for_syspath(disk_dir
);
1492 if (dev_name
== NULL
) {
1493 g_debug("Failed to get device name for syspath: %s",
1497 disk
= g_new0(GuestDiskInfo
, 1);
1498 disk
->name
= dev_name
;
1499 disk
->partition
= false;
1500 disk
->alias
= get_alias_for_syspath(disk_dir
);
1501 QAPI_LIST_PREPEND(ret
, disk
);
1503 /* Get address for non-virtual devices */
1504 bool is_virtual
= is_disk_virtual(disk_dir
, &local_err
);
1505 if (local_err
!= NULL
) {
1506 g_debug(" failed to check disk path, ignoring error: %s",
1507 error_get_pretty(local_err
));
1508 error_free(local_err
);
1510 /* Don't try to get the address */
1514 disk
->address
= get_disk_address(disk_dir
, &local_err
);
1515 if (local_err
!= NULL
) {
1516 g_debug(" failed to get device info, ignoring error: %s",
1517 error_get_pretty(local_err
));
1518 error_free(local_err
);
1523 get_disk_deps(disk_dir
, disk
);
1524 get_disk_smart(disk
);
1525 ret
= get_disk_partitions(ret
, de
->d_name
, disk_dir
, dev_name
);
1535 GuestDiskInfoList
*qmp_guest_get_disks(Error
**errp
)
1537 error_setg(errp
, QERR_UNSUPPORTED
);
1543 /* Return a list of the disk device(s)' info which @mount lies on */
1544 static GuestFilesystemInfo
*build_guest_fsinfo(struct FsMount
*mount
,
1547 GuestFilesystemInfo
*fs
= g_malloc0(sizeof(*fs
));
1549 unsigned long used
, nonroot_total
, fr_size
;
1550 char *devpath
= g_strdup_printf("/sys/dev/block/%u:%u",
1551 mount
->devmajor
, mount
->devminor
);
1553 fs
->mountpoint
= g_strdup(mount
->dirname
);
1554 fs
->type
= g_strdup(mount
->devtype
);
1555 build_guest_fsinfo_for_device(devpath
, fs
, errp
);
1557 if (statvfs(fs
->mountpoint
, &buf
) == 0) {
1558 fr_size
= buf
.f_frsize
;
1559 used
= buf
.f_blocks
- buf
.f_bfree
;
1560 nonroot_total
= used
+ buf
.f_bavail
;
1561 fs
->used_bytes
= used
* fr_size
;
1562 fs
->total_bytes
= nonroot_total
* fr_size
;
1564 fs
->has_total_bytes
= true;
1565 fs
->has_used_bytes
= true;
1573 GuestFilesystemInfoList
*qmp_guest_get_fsinfo(Error
**errp
)
1576 struct FsMount
*mount
;
1577 GuestFilesystemInfoList
*ret
= NULL
;
1578 Error
*local_err
= NULL
;
1580 QTAILQ_INIT(&mounts
);
1581 if (!build_fs_mount_list(&mounts
, &local_err
)) {
1582 error_propagate(errp
, local_err
);
1586 QTAILQ_FOREACH(mount
, &mounts
, next
) {
1587 g_debug("Building guest fsinfo for '%s'", mount
->dirname
);
1589 QAPI_LIST_PREPEND(ret
, build_guest_fsinfo(mount
, &local_err
));
1591 error_propagate(errp
, local_err
);
1592 qapi_free_GuestFilesystemInfoList(ret
);
1598 free_fs_mount_list(&mounts
);
1601 #endif /* CONFIG_FSFREEZE */
1603 #if defined(CONFIG_FSTRIM)
1605 * Walk list of mounted file systems in the guest, and trim them.
1607 GuestFilesystemTrimResponse
*
1608 qmp_guest_fstrim(bool has_minimum
, int64_t minimum
, Error
**errp
)
1610 GuestFilesystemTrimResponse
*response
;
1611 GuestFilesystemTrimResult
*result
;
1614 struct FsMount
*mount
;
1616 struct fstrim_range r
;
1618 slog("guest-fstrim called");
1620 QTAILQ_INIT(&mounts
);
1621 if (!build_fs_mount_list(&mounts
, errp
)) {
1625 response
= g_malloc0(sizeof(*response
));
1627 QTAILQ_FOREACH(mount
, &mounts
, next
) {
1628 result
= g_malloc0(sizeof(*result
));
1629 result
->path
= g_strdup(mount
->dirname
);
1631 QAPI_LIST_PREPEND(response
->paths
, result
);
1633 fd
= qga_open_cloexec(mount
->dirname
, O_RDONLY
, 0);
1635 result
->error
= g_strdup_printf("failed to open: %s",
1640 /* We try to cull filesystems we know won't work in advance, but other
1641 * filesystems may not implement fstrim for less obvious reasons.
1642 * These will report EOPNOTSUPP; while in some other cases ENOTTY
1643 * will be reported (e.g. CD-ROMs).
1644 * Any other error means an unexpected error.
1648 r
.minlen
= has_minimum
? minimum
: 0;
1649 ret
= ioctl(fd
, FITRIM
, &r
);
1651 if (errno
== ENOTTY
|| errno
== EOPNOTSUPP
) {
1652 result
->error
= g_strdup("trim not supported");
1654 result
->error
= g_strdup_printf("failed to trim: %s",
1661 result
->has_minimum
= true;
1662 result
->minimum
= r
.minlen
;
1663 result
->has_trimmed
= true;
1664 result
->trimmed
= r
.len
;
1668 free_fs_mount_list(&mounts
);
1671 #endif /* CONFIG_FSTRIM */
1674 #define LINUX_SYS_STATE_FILE "/sys/power/state"
1675 #define SUSPEND_SUPPORTED 0
1676 #define SUSPEND_NOT_SUPPORTED 1
1679 SUSPEND_MODE_DISK
= 0,
1680 SUSPEND_MODE_RAM
= 1,
1681 SUSPEND_MODE_HYBRID
= 2,
1685 * Executes a command in a child process using g_spawn_sync,
1686 * returning an int >= 0 representing the exit status of the
1689 * If the program wasn't found in path, returns -1.
1691 * If a problem happened when creating the child process,
1692 * returns -1 and errp is set.
1694 static int run_process_child(const char *command
[], Error
**errp
)
1696 int exit_status
, spawn_flag
;
1697 GError
*g_err
= NULL
;
1700 spawn_flag
= G_SPAWN_SEARCH_PATH
| G_SPAWN_STDOUT_TO_DEV_NULL
|
1701 G_SPAWN_STDERR_TO_DEV_NULL
;
1703 success
= g_spawn_sync(NULL
, (char **)command
, NULL
, spawn_flag
,
1704 NULL
, NULL
, NULL
, NULL
,
1705 &exit_status
, &g_err
);
1708 return WEXITSTATUS(exit_status
);
1711 if (g_err
&& (g_err
->code
!= G_SPAWN_ERROR_NOENT
)) {
1712 error_setg(errp
, "failed to create child process, error '%s'",
1716 g_error_free(g_err
);
1720 static bool systemd_supports_mode(SuspendMode mode
, Error
**errp
)
1722 const char *systemctl_args
[3] = {"systemd-hibernate", "systemd-suspend",
1723 "systemd-hybrid-sleep"};
1724 const char *cmd
[4] = {"systemctl", "status", systemctl_args
[mode
], NULL
};
1727 status
= run_process_child(cmd
, errp
);
1730 * systemctl status uses LSB return codes so we can expect
1731 * status > 0 and be ok. To assert if the guest has support
1732 * for the selected suspend mode, status should be < 4. 4 is
1733 * the code for unknown service status, the return value when
1734 * the service does not exist. A common value is status = 3
1735 * (program is not running).
1737 if (status
> 0 && status
< 4) {
1744 static void systemd_suspend(SuspendMode mode
, Error
**errp
)
1746 Error
*local_err
= NULL
;
1747 const char *systemctl_args
[3] = {"hibernate", "suspend", "hybrid-sleep"};
1748 const char *cmd
[3] = {"systemctl", systemctl_args
[mode
], NULL
};
1751 status
= run_process_child(cmd
, &local_err
);
1757 if ((status
== -1) && !local_err
) {
1758 error_setg(errp
, "the helper program 'systemctl %s' was not found",
1759 systemctl_args
[mode
]);
1764 error_propagate(errp
, local_err
);
1766 error_setg(errp
, "the helper program 'systemctl %s' returned an "
1767 "unexpected exit status code (%d)",
1768 systemctl_args
[mode
], status
);
1772 static bool pmutils_supports_mode(SuspendMode mode
, Error
**errp
)
1774 Error
*local_err
= NULL
;
1775 const char *pmutils_args
[3] = {"--hibernate", "--suspend",
1776 "--suspend-hybrid"};
1777 const char *cmd
[3] = {"pm-is-supported", pmutils_args
[mode
], NULL
};
1780 status
= run_process_child(cmd
, &local_err
);
1782 if (status
== SUSPEND_SUPPORTED
) {
1786 if ((status
== -1) && !local_err
) {
1791 error_propagate(errp
, local_err
);
1794 "the helper program '%s' returned an unexpected exit"
1795 " status code (%d)", "pm-is-supported", status
);
1801 static void pmutils_suspend(SuspendMode mode
, Error
**errp
)
1803 Error
*local_err
= NULL
;
1804 const char *pmutils_binaries
[3] = {"pm-hibernate", "pm-suspend",
1805 "pm-suspend-hybrid"};
1806 const char *cmd
[2] = {pmutils_binaries
[mode
], NULL
};
1809 status
= run_process_child(cmd
, &local_err
);
1815 if ((status
== -1) && !local_err
) {
1816 error_setg(errp
, "the helper program '%s' was not found",
1817 pmutils_binaries
[mode
]);
1822 error_propagate(errp
, local_err
);
1825 "the helper program '%s' returned an unexpected exit"
1826 " status code (%d)", pmutils_binaries
[mode
], status
);
1830 static bool linux_sys_state_supports_mode(SuspendMode mode
, Error
**errp
)
1832 const char *sysfile_strs
[3] = {"disk", "mem", NULL
};
1833 const char *sysfile_str
= sysfile_strs
[mode
];
1834 char buf
[32]; /* hopefully big enough */
1839 error_setg(errp
, "unknown guest suspend mode");
1843 fd
= open(LINUX_SYS_STATE_FILE
, O_RDONLY
);
1848 ret
= read(fd
, buf
, sizeof(buf
) - 1);
1855 if (strstr(buf
, sysfile_str
)) {
1861 static void linux_sys_state_suspend(SuspendMode mode
, Error
**errp
)
1863 Error
*local_err
= NULL
;
1864 const char *sysfile_strs
[3] = {"disk", "mem", NULL
};
1865 const char *sysfile_str
= sysfile_strs
[mode
];
1870 error_setg(errp
, "unknown guest suspend mode");
1880 reopen_fd_to_null(0);
1881 reopen_fd_to_null(1);
1882 reopen_fd_to_null(2);
1884 fd
= open(LINUX_SYS_STATE_FILE
, O_WRONLY
);
1886 _exit(EXIT_FAILURE
);
1889 if (write(fd
, sysfile_str
, strlen(sysfile_str
)) < 0) {
1890 _exit(EXIT_FAILURE
);
1893 _exit(EXIT_SUCCESS
);
1894 } else if (pid
< 0) {
1895 error_setg_errno(errp
, errno
, "failed to create child process");
1899 ga_wait_child(pid
, &status
, &local_err
);
1901 error_propagate(errp
, local_err
);
1905 if (WEXITSTATUS(status
)) {
1906 error_setg(errp
, "child process has failed to suspend");
1911 static void guest_suspend(SuspendMode mode
, Error
**errp
)
1913 Error
*local_err
= NULL
;
1914 bool mode_supported
= false;
1916 if (systemd_supports_mode(mode
, &local_err
)) {
1917 mode_supported
= true;
1918 systemd_suspend(mode
, &local_err
);
1925 error_free(local_err
);
1928 if (pmutils_supports_mode(mode
, &local_err
)) {
1929 mode_supported
= true;
1930 pmutils_suspend(mode
, &local_err
);
1937 error_free(local_err
);
1940 if (linux_sys_state_supports_mode(mode
, &local_err
)) {
1941 mode_supported
= true;
1942 linux_sys_state_suspend(mode
, &local_err
);
1945 if (!mode_supported
) {
1946 error_free(local_err
);
1948 "the requested suspend mode is not supported by the guest");
1950 error_propagate(errp
, local_err
);
1954 void qmp_guest_suspend_disk(Error
**errp
)
1956 guest_suspend(SUSPEND_MODE_DISK
, errp
);
1959 void qmp_guest_suspend_ram(Error
**errp
)
1961 guest_suspend(SUSPEND_MODE_RAM
, errp
);
1964 void qmp_guest_suspend_hybrid(Error
**errp
)
1966 guest_suspend(SUSPEND_MODE_HYBRID
, errp
);
1969 /* Transfer online/offline status between @vcpu and the guest system.
1971 * On input either @errp or *@errp must be NULL.
1973 * In system-to-@vcpu direction, the following @vcpu fields are accessed:
1974 * - R: vcpu->logical_id
1976 * - W: vcpu->can_offline
1978 * In @vcpu-to-system direction, the following @vcpu fields are accessed:
1979 * - R: vcpu->logical_id
1982 * Written members remain unmodified on error.
1984 static void transfer_vcpu(GuestLogicalProcessor
*vcpu
, bool sys2vcpu
,
1985 char *dirpath
, Error
**errp
)
1990 static const char fn
[] = "online";
1992 dirfd
= open(dirpath
, O_RDONLY
| O_DIRECTORY
);
1994 error_setg_errno(errp
, errno
, "open(\"%s\")", dirpath
);
1998 fd
= openat(dirfd
, fn
, sys2vcpu
? O_RDONLY
: O_RDWR
);
2000 if (errno
!= ENOENT
) {
2001 error_setg_errno(errp
, errno
, "open(\"%s/%s\")", dirpath
, fn
);
2002 } else if (sys2vcpu
) {
2003 vcpu
->online
= true;
2004 vcpu
->can_offline
= false;
2005 } else if (!vcpu
->online
) {
2006 error_setg(errp
, "logical processor #%" PRId64
" can't be "
2007 "offlined", vcpu
->logical_id
);
2008 } /* otherwise pretend successful re-onlining */
2010 unsigned char status
;
2012 res
= pread(fd
, &status
, 1, 0);
2014 error_setg_errno(errp
, errno
, "pread(\"%s/%s\")", dirpath
, fn
);
2015 } else if (res
== 0) {
2016 error_setg(errp
, "pread(\"%s/%s\"): unexpected EOF", dirpath
,
2018 } else if (sys2vcpu
) {
2019 vcpu
->online
= (status
!= '0');
2020 vcpu
->can_offline
= true;
2021 } else if (vcpu
->online
!= (status
!= '0')) {
2022 status
= '0' + vcpu
->online
;
2023 if (pwrite(fd
, &status
, 1, 0) == -1) {
2024 error_setg_errno(errp
, errno
, "pwrite(\"%s/%s\")", dirpath
,
2027 } /* otherwise pretend successful re-(on|off)-lining */
2037 GuestLogicalProcessorList
*qmp_guest_get_vcpus(Error
**errp
)
2039 GuestLogicalProcessorList
*head
, **tail
;
2040 const char *cpu_dir
= "/sys/devices/system/cpu";
2042 g_autoptr(GDir
) cpu_gdir
= NULL
;
2043 Error
*local_err
= NULL
;
2047 cpu_gdir
= g_dir_open(cpu_dir
, 0, NULL
);
2049 if (cpu_gdir
== NULL
) {
2050 error_setg_errno(errp
, errno
, "failed to list entries: %s", cpu_dir
);
2054 while (local_err
== NULL
&& (line
= g_dir_read_name(cpu_gdir
)) != NULL
) {
2055 GuestLogicalProcessor
*vcpu
;
2057 if (sscanf(line
, "cpu%" PRId64
, &id
)) {
2058 g_autofree
char *path
= g_strdup_printf("/sys/devices/system/cpu/"
2059 "cpu%" PRId64
"/", id
);
2060 vcpu
= g_malloc0(sizeof *vcpu
);
2061 vcpu
->logical_id
= id
;
2062 vcpu
->has_can_offline
= true; /* lolspeak ftw */
2063 transfer_vcpu(vcpu
, true, path
, &local_err
);
2064 QAPI_LIST_APPEND(tail
, vcpu
);
2068 if (local_err
== NULL
) {
2069 /* there's no guest with zero VCPUs */
2070 g_assert(head
!= NULL
);
2074 qapi_free_GuestLogicalProcessorList(head
);
2075 error_propagate(errp
, local_err
);
2079 int64_t qmp_guest_set_vcpus(GuestLogicalProcessorList
*vcpus
, Error
**errp
)
2082 Error
*local_err
= NULL
;
2085 while (vcpus
!= NULL
) {
2086 char *path
= g_strdup_printf("/sys/devices/system/cpu/cpu%" PRId64
"/",
2087 vcpus
->value
->logical_id
);
2089 transfer_vcpu(vcpus
->value
, false, path
, &local_err
);
2091 if (local_err
!= NULL
) {
2095 vcpus
= vcpus
->next
;
2098 if (local_err
!= NULL
) {
2099 if (processed
== 0) {
2100 error_propagate(errp
, local_err
);
2102 error_free(local_err
);
2108 #endif /* __linux__ */
2110 #if defined(__linux__) || defined(__FreeBSD__)
2111 void qmp_guest_set_user_password(const char *username
,
2112 const char *password
,
2116 Error
*local_err
= NULL
;
2117 char *passwd_path
= NULL
;
2120 int datafd
[2] = { -1, -1 };
2121 char *rawpasswddata
= NULL
;
2122 size_t rawpasswdlen
;
2123 char *chpasswddata
= NULL
;
2126 rawpasswddata
= (char *)qbase64_decode(password
, -1, &rawpasswdlen
, errp
);
2127 if (!rawpasswddata
) {
2130 rawpasswddata
= g_renew(char, rawpasswddata
, rawpasswdlen
+ 1);
2131 rawpasswddata
[rawpasswdlen
] = '\0';
2133 if (strchr(rawpasswddata
, '\n')) {
2134 error_setg(errp
, "forbidden characters in raw password");
2138 if (strchr(username
, '\n') ||
2139 strchr(username
, ':')) {
2140 error_setg(errp
, "forbidden characters in username");
2145 chpasswddata
= g_strdup(rawpasswddata
);
2146 passwd_path
= g_find_program_in_path("pw");
2148 chpasswddata
= g_strdup_printf("%s:%s\n", username
, rawpasswddata
);
2149 passwd_path
= g_find_program_in_path("chpasswd");
2152 chpasswdlen
= strlen(chpasswddata
);
2155 error_setg(errp
, "cannot find 'passwd' program in PATH");
2159 if (!g_unix_open_pipe(datafd
, FD_CLOEXEC
, NULL
)) {
2160 error_setg(errp
, "cannot create pipe FDs");
2170 reopen_fd_to_null(1);
2171 reopen_fd_to_null(2);
2175 h_arg
= (crypted
) ? "-H" : "-h";
2176 execl(passwd_path
, "pw", "usermod", "-n", username
, h_arg
, "0", NULL
);
2179 execl(passwd_path
, "chpasswd", "-e", NULL
);
2181 execl(passwd_path
, "chpasswd", NULL
);
2184 _exit(EXIT_FAILURE
);
2185 } else if (pid
< 0) {
2186 error_setg_errno(errp
, errno
, "failed to create child process");
2192 if (qemu_write_full(datafd
[1], chpasswddata
, chpasswdlen
) != chpasswdlen
) {
2193 error_setg_errno(errp
, errno
, "cannot write new account password");
2199 ga_wait_child(pid
, &status
, &local_err
);
2201 error_propagate(errp
, local_err
);
2205 if (!WIFEXITED(status
)) {
2206 error_setg(errp
, "child process has terminated abnormally");
2210 if (WEXITSTATUS(status
)) {
2211 error_setg(errp
, "child process has failed to set user password");
2216 g_free(chpasswddata
);
2217 g_free(rawpasswddata
);
2218 g_free(passwd_path
);
2219 if (datafd
[0] != -1) {
2222 if (datafd
[1] != -1) {
2226 #else /* __linux__ || __FreeBSD__ */
2227 void qmp_guest_set_user_password(const char *username
,
2228 const char *password
,
2232 error_setg(errp
, QERR_UNSUPPORTED
);
2234 #endif /* __linux__ || __FreeBSD__ */
2237 static void ga_read_sysfs_file(int dirfd
, const char *pathname
, char *buf
,
2238 int size
, Error
**errp
)
2244 fd
= openat(dirfd
, pathname
, O_RDONLY
);
2246 error_setg_errno(errp
, errno
, "open sysfs file \"%s\"", pathname
);
2250 res
= pread(fd
, buf
, size
, 0);
2252 error_setg_errno(errp
, errno
, "pread sysfs file \"%s\"", pathname
);
2253 } else if (res
== 0) {
2254 error_setg(errp
, "pread sysfs file \"%s\": unexpected EOF", pathname
);
2259 static void ga_write_sysfs_file(int dirfd
, const char *pathname
,
2260 const char *buf
, int size
, Error
**errp
)
2265 fd
= openat(dirfd
, pathname
, O_WRONLY
);
2267 error_setg_errno(errp
, errno
, "open sysfs file \"%s\"", pathname
);
2271 if (pwrite(fd
, buf
, size
, 0) == -1) {
2272 error_setg_errno(errp
, errno
, "pwrite sysfs file \"%s\"", pathname
);
2278 /* Transfer online/offline status between @mem_blk and the guest system.
2280 * On input either @errp or *@errp must be NULL.
2282 * In system-to-@mem_blk direction, the following @mem_blk fields are accessed:
2283 * - R: mem_blk->phys_index
2284 * - W: mem_blk->online
2285 * - W: mem_blk->can_offline
2287 * In @mem_blk-to-system direction, the following @mem_blk fields are accessed:
2288 * - R: mem_blk->phys_index
2289 * - R: mem_blk->online
2290 *- R: mem_blk->can_offline
2291 * Written members remain unmodified on error.
2293 static void transfer_memory_block(GuestMemoryBlock
*mem_blk
, bool sys2memblk
,
2294 GuestMemoryBlockResponse
*result
,
2300 Error
*local_err
= NULL
;
2306 error_setg(errp
, "Internal error, 'result' should not be NULL");
2310 dp
= opendir("/sys/devices/system/memory/");
2311 /* if there is no 'memory' directory in sysfs,
2312 * we think this VM does not support online/offline memory block,
2313 * any other solution?
2316 if (errno
== ENOENT
) {
2318 GUEST_MEMORY_BLOCK_RESPONSE_TYPE_OPERATION_NOT_SUPPORTED
;
2325 dirpath
= g_strdup_printf("/sys/devices/system/memory/memory%" PRId64
"/",
2326 mem_blk
->phys_index
);
2327 dirfd
= open(dirpath
, O_RDONLY
| O_DIRECTORY
);
2330 error_setg_errno(errp
, errno
, "open(\"%s\")", dirpath
);
2332 if (errno
== ENOENT
) {
2333 result
->response
= GUEST_MEMORY_BLOCK_RESPONSE_TYPE_NOT_FOUND
;
2336 GUEST_MEMORY_BLOCK_RESPONSE_TYPE_OPERATION_FAILED
;
2344 status
= g_malloc0(10);
2345 ga_read_sysfs_file(dirfd
, "state", status
, 10, &local_err
);
2347 /* treat with sysfs file that not exist in old kernel */
2348 if (errno
== ENOENT
) {
2349 error_free(local_err
);
2351 mem_blk
->online
= true;
2352 mem_blk
->can_offline
= false;
2353 } else if (!mem_blk
->online
) {
2355 GUEST_MEMORY_BLOCK_RESPONSE_TYPE_OPERATION_NOT_SUPPORTED
;
2359 error_propagate(errp
, local_err
);
2361 error_free(local_err
);
2363 GUEST_MEMORY_BLOCK_RESPONSE_TYPE_OPERATION_FAILED
;
2370 char removable
= '0';
2372 mem_blk
->online
= (strncmp(status
, "online", 6) == 0);
2374 ga_read_sysfs_file(dirfd
, "removable", &removable
, 1, &local_err
);
2376 /* if no 'removable' file, it doesn't support offline mem blk */
2377 if (errno
== ENOENT
) {
2378 error_free(local_err
);
2379 mem_blk
->can_offline
= false;
2381 error_propagate(errp
, local_err
);
2384 mem_blk
->can_offline
= (removable
!= '0');
2387 if (mem_blk
->online
!= (strncmp(status
, "online", 6) == 0)) {
2388 const char *new_state
= mem_blk
->online
? "online" : "offline";
2390 ga_write_sysfs_file(dirfd
, "state", new_state
, strlen(new_state
),
2393 error_free(local_err
);
2395 GUEST_MEMORY_BLOCK_RESPONSE_TYPE_OPERATION_FAILED
;
2399 result
->response
= GUEST_MEMORY_BLOCK_RESPONSE_TYPE_SUCCESS
;
2400 result
->has_error_code
= false;
2401 } /* otherwise pretend successful re-(on|off)-lining */
2412 result
->has_error_code
= true;
2413 result
->error_code
= errno
;
2417 GuestMemoryBlockList
*qmp_guest_get_memory_blocks(Error
**errp
)
2419 GuestMemoryBlockList
*head
, **tail
;
2420 Error
*local_err
= NULL
;
2427 dp
= opendir("/sys/devices/system/memory/");
2429 /* it's ok if this happens to be a system that doesn't expose
2430 * memory blocks via sysfs, but otherwise we should report
2433 if (errno
!= ENOENT
) {
2434 error_setg_errno(errp
, errno
, "Can't open directory"
2435 "\"/sys/devices/system/memory/\"");
2440 /* Note: the phys_index of memory block may be discontinuous,
2441 * this is because a memblk is the unit of the Sparse Memory design, which
2442 * allows discontinuous memory ranges (ex. NUMA), so here we should
2443 * traverse the memory block directory.
2445 while ((de
= readdir(dp
)) != NULL
) {
2446 GuestMemoryBlock
*mem_blk
;
2448 if ((strncmp(de
->d_name
, "memory", 6) != 0) ||
2449 !(de
->d_type
& DT_DIR
)) {
2453 mem_blk
= g_malloc0(sizeof *mem_blk
);
2454 /* The d_name is "memoryXXX", phys_index is block id, same as XXX */
2455 mem_blk
->phys_index
= strtoul(&de
->d_name
[6], NULL
, 10);
2456 mem_blk
->has_can_offline
= true; /* lolspeak ftw */
2457 transfer_memory_block(mem_blk
, true, NULL
, &local_err
);
2462 QAPI_LIST_APPEND(tail
, mem_blk
);
2466 if (local_err
== NULL
) {
2467 /* there's no guest with zero memory blocks */
2469 error_setg(errp
, "guest reported zero memory blocks!");
2474 qapi_free_GuestMemoryBlockList(head
);
2475 error_propagate(errp
, local_err
);
2479 GuestMemoryBlockResponseList
*
2480 qmp_guest_set_memory_blocks(GuestMemoryBlockList
*mem_blks
, Error
**errp
)
2482 GuestMemoryBlockResponseList
*head
, **tail
;
2483 Error
*local_err
= NULL
;
2488 while (mem_blks
!= NULL
) {
2489 GuestMemoryBlockResponse
*result
;
2490 GuestMemoryBlock
*current_mem_blk
= mem_blks
->value
;
2492 result
= g_malloc0(sizeof(*result
));
2493 result
->phys_index
= current_mem_blk
->phys_index
;
2494 transfer_memory_block(current_mem_blk
, false, result
, &local_err
);
2495 if (local_err
) { /* should never happen */
2499 QAPI_LIST_APPEND(tail
, result
);
2500 mem_blks
= mem_blks
->next
;
2505 qapi_free_GuestMemoryBlockResponseList(head
);
2506 error_propagate(errp
, local_err
);
2510 GuestMemoryBlockInfo
*qmp_guest_get_memory_block_info(Error
**errp
)
2512 Error
*local_err
= NULL
;
2516 GuestMemoryBlockInfo
*info
;
2518 dirpath
= g_strdup_printf("/sys/devices/system/memory/");
2519 dirfd
= open(dirpath
, O_RDONLY
| O_DIRECTORY
);
2521 error_setg_errno(errp
, errno
, "open(\"%s\")", dirpath
);
2527 buf
= g_malloc0(20);
2528 ga_read_sysfs_file(dirfd
, "block_size_bytes", buf
, 20, &local_err
);
2532 error_propagate(errp
, local_err
);
2536 info
= g_new0(GuestMemoryBlockInfo
, 1);
2537 info
->size
= strtol(buf
, NULL
, 16); /* the unit is bytes */
2544 #define MAX_NAME_LEN 128
2545 static GuestDiskStatsInfoList
*guest_get_diskstats(Error
**errp
)
2548 GuestDiskStatsInfoList
*head
= NULL
, **tail
= &head
;
2549 const char *diskstats
= "/proc/diskstats";
2554 fp
= fopen(diskstats
, "r");
2556 error_setg_errno(errp
, errno
, "open(\"%s\")", diskstats
);
2560 while (getline(&line
, &n
, fp
) != -1) {
2561 g_autofree GuestDiskStatsInfo
*diskstatinfo
= NULL
;
2562 g_autofree GuestDiskStats
*diskstat
= NULL
;
2563 char dev_name
[MAX_NAME_LEN
];
2564 unsigned int ios_pgr
, tot_ticks
, rq_ticks
, wr_ticks
, dc_ticks
, fl_ticks
;
2565 unsigned long rd_ios
, rd_merges_or_rd_sec
, rd_ticks_or_wr_sec
, wr_ios
;
2566 unsigned long wr_merges
, rd_sec_or_wr_ios
, wr_sec
;
2567 unsigned long dc_ios
, dc_merges
, dc_sec
, fl_ios
;
2568 unsigned int major
, minor
;
2571 i
= sscanf(line
, "%u %u %s %lu %lu %lu"
2572 "%lu %lu %lu %lu %u %u %u %u"
2573 "%lu %lu %lu %u %lu %u",
2574 &major
, &minor
, dev_name
,
2575 &rd_ios
, &rd_merges_or_rd_sec
, &rd_sec_or_wr_ios
,
2576 &rd_ticks_or_wr_sec
, &wr_ios
, &wr_merges
, &wr_sec
,
2577 &wr_ticks
, &ios_pgr
, &tot_ticks
, &rq_ticks
,
2578 &dc_ios
, &dc_merges
, &dc_sec
, &dc_ticks
,
2579 &fl_ios
, &fl_ticks
);
2585 diskstatinfo
= g_new0(GuestDiskStatsInfo
, 1);
2586 diskstatinfo
->name
= g_strdup(dev_name
);
2587 diskstatinfo
->major
= major
;
2588 diskstatinfo
->minor
= minor
;
2590 diskstat
= g_new0(GuestDiskStats
, 1);
2592 diskstat
->has_read_ios
= true;
2593 diskstat
->read_ios
= rd_ios
;
2594 diskstat
->has_read_sectors
= true;
2595 diskstat
->read_sectors
= rd_merges_or_rd_sec
;
2596 diskstat
->has_write_ios
= true;
2597 diskstat
->write_ios
= rd_sec_or_wr_ios
;
2598 diskstat
->has_write_sectors
= true;
2599 diskstat
->write_sectors
= rd_ticks_or_wr_sec
;
2602 diskstat
->has_read_ios
= true;
2603 diskstat
->read_ios
= rd_ios
;
2604 diskstat
->has_read_sectors
= true;
2605 diskstat
->read_sectors
= rd_sec_or_wr_ios
;
2606 diskstat
->has_read_merges
= true;
2607 diskstat
->read_merges
= rd_merges_or_rd_sec
;
2608 diskstat
->has_read_ticks
= true;
2609 diskstat
->read_ticks
= rd_ticks_or_wr_sec
;
2610 diskstat
->has_write_ios
= true;
2611 diskstat
->write_ios
= wr_ios
;
2612 diskstat
->has_write_sectors
= true;
2613 diskstat
->write_sectors
= wr_sec
;
2614 diskstat
->has_write_merges
= true;
2615 diskstat
->write_merges
= wr_merges
;
2616 diskstat
->has_write_ticks
= true;
2617 diskstat
->write_ticks
= wr_ticks
;
2618 diskstat
->has_ios_pgr
= true;
2619 diskstat
->ios_pgr
= ios_pgr
;
2620 diskstat
->has_total_ticks
= true;
2621 diskstat
->total_ticks
= tot_ticks
;
2622 diskstat
->has_weight_ticks
= true;
2623 diskstat
->weight_ticks
= rq_ticks
;
2626 diskstat
->has_discard_ios
= true;
2627 diskstat
->discard_ios
= dc_ios
;
2628 diskstat
->has_discard_merges
= true;
2629 diskstat
->discard_merges
= dc_merges
;
2630 diskstat
->has_discard_sectors
= true;
2631 diskstat
->discard_sectors
= dc_sec
;
2632 diskstat
->has_discard_ticks
= true;
2633 diskstat
->discard_ticks
= dc_ticks
;
2636 diskstat
->has_flush_ios
= true;
2637 diskstat
->flush_ios
= fl_ios
;
2638 diskstat
->has_flush_ticks
= true;
2639 diskstat
->flush_ticks
= fl_ticks
;
2642 diskstatinfo
->stats
= g_steal_pointer(&diskstat
);
2643 QAPI_LIST_APPEND(tail
, diskstatinfo
);
2644 diskstatinfo
= NULL
;
2650 g_debug("disk stats reporting available only for Linux");
2655 GuestDiskStatsInfoList
*qmp_guest_get_diskstats(Error
**errp
)
2657 return guest_get_diskstats(errp
);
2660 GuestCpuStatsList
*qmp_guest_get_cpustats(Error
**errp
)
2662 GuestCpuStatsList
*head
= NULL
, **tail
= &head
;
2663 const char *cpustats
= "/proc/stat";
2664 int clk_tck
= sysconf(_SC_CLK_TCK
);
2669 fp
= fopen(cpustats
, "r");
2671 error_setg_errno(errp
, errno
, "open(\"%s\")", cpustats
);
2675 while (getline(&line
, &n
, fp
) != -1) {
2676 GuestCpuStats
*cpustat
= NULL
;
2677 GuestLinuxCpuStats
*linuxcpustat
;
2679 unsigned long user
, system
, idle
, iowait
, irq
, softirq
, steal
, guest
;
2680 unsigned long nice
, guest_nice
;
2683 i
= sscanf(line
, "%s %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu",
2684 name
, &user
, &nice
, &system
, &idle
, &iowait
, &irq
, &softirq
,
2685 &steal
, &guest
, &guest_nice
);
2687 /* drop "cpu 1 2 3 ...", get "cpuX 1 2 3 ..." only */
2688 if ((i
== EOF
) || strncmp(name
, "cpu", 3) || (name
[3] == '\0')) {
2693 slog("Parsing cpu stat from %s failed, see \"man proc\"", cpustats
);
2697 cpustat
= g_new0(GuestCpuStats
, 1);
2698 cpustat
->type
= GUEST_CPU_STATS_TYPE_LINUX
;
2700 linuxcpustat
= &cpustat
->u
.q_linux
;
2701 linuxcpustat
->cpu
= atoi(&name
[3]);
2702 linuxcpustat
->user
= user
* 1000 / clk_tck
;
2703 linuxcpustat
->nice
= nice
* 1000 / clk_tck
;
2704 linuxcpustat
->system
= system
* 1000 / clk_tck
;
2705 linuxcpustat
->idle
= idle
* 1000 / clk_tck
;
2708 linuxcpustat
->has_iowait
= true;
2709 linuxcpustat
->iowait
= iowait
* 1000 / clk_tck
;
2713 linuxcpustat
->has_irq
= true;
2714 linuxcpustat
->irq
= irq
* 1000 / clk_tck
;
2715 linuxcpustat
->has_softirq
= true;
2716 linuxcpustat
->softirq
= softirq
* 1000 / clk_tck
;
2720 linuxcpustat
->has_steal
= true;
2721 linuxcpustat
->steal
= steal
* 1000 / clk_tck
;
2725 linuxcpustat
->has_guest
= true;
2726 linuxcpustat
->guest
= guest
* 1000 / clk_tck
;
2730 linuxcpustat
->has_guest
= true;
2731 linuxcpustat
->guest
= guest
* 1000 / clk_tck
;
2732 linuxcpustat
->has_guestnice
= true;
2733 linuxcpustat
->guestnice
= guest_nice
* 1000 / clk_tck
;
2736 QAPI_LIST_APPEND(tail
, cpustat
);
2744 #else /* defined(__linux__) */
2746 void qmp_guest_suspend_disk(Error
**errp
)
2748 error_setg(errp
, QERR_UNSUPPORTED
);
2751 void qmp_guest_suspend_ram(Error
**errp
)
2753 error_setg(errp
, QERR_UNSUPPORTED
);
2756 void qmp_guest_suspend_hybrid(Error
**errp
)
2758 error_setg(errp
, QERR_UNSUPPORTED
);
2761 GuestLogicalProcessorList
*qmp_guest_get_vcpus(Error
**errp
)
2763 error_setg(errp
, QERR_UNSUPPORTED
);
2767 int64_t qmp_guest_set_vcpus(GuestLogicalProcessorList
*vcpus
, Error
**errp
)
2769 error_setg(errp
, QERR_UNSUPPORTED
);
2773 GuestMemoryBlockList
*qmp_guest_get_memory_blocks(Error
**errp
)
2775 error_setg(errp
, QERR_UNSUPPORTED
);
2779 GuestMemoryBlockResponseList
*
2780 qmp_guest_set_memory_blocks(GuestMemoryBlockList
*mem_blks
, Error
**errp
)
2782 error_setg(errp
, QERR_UNSUPPORTED
);
2786 GuestMemoryBlockInfo
*qmp_guest_get_memory_block_info(Error
**errp
)
2788 error_setg(errp
, QERR_UNSUPPORTED
);
2794 #ifdef HAVE_GETIFADDRS
2795 static GuestNetworkInterface
*
2796 guest_find_interface(GuestNetworkInterfaceList
*head
,
2799 for (; head
; head
= head
->next
) {
2800 if (strcmp(head
->value
->name
, name
) == 0) {
2808 static int guest_get_network_stats(const char *name
,
2809 GuestNetworkInterfaceStat
*stats
)
2813 char const *devinfo
= "/proc/net/dev";
2815 char *line
= NULL
, *colon
;
2817 fp
= fopen(devinfo
, "r");
2819 g_debug("failed to open network stats %s: %s", devinfo
,
2823 name_len
= strlen(name
);
2824 while (getline(&line
, &n
, fp
) != -1) {
2827 long long rx_packets
;
2829 long long rx_dropped
;
2831 long long tx_packets
;
2833 long long tx_dropped
;
2835 trim_line
= g_strchug(line
);
2836 if (trim_line
[0] == '\0') {
2839 colon
= strchr(trim_line
, ':');
2843 if (colon
- name_len
== trim_line
&&
2844 strncmp(trim_line
, name
, name_len
) == 0) {
2845 if (sscanf(colon
+ 1,
2846 "%lld %lld %lld %lld %lld %lld %lld %lld %lld %lld %lld %lld %lld %lld %lld %lld",
2847 &rx_bytes
, &rx_packets
, &rx_errs
, &rx_dropped
,
2848 &dummy
, &dummy
, &dummy
, &dummy
,
2849 &tx_bytes
, &tx_packets
, &tx_errs
, &tx_dropped
,
2850 &dummy
, &dummy
, &dummy
, &dummy
) != 16) {
2853 stats
->rx_bytes
= rx_bytes
;
2854 stats
->rx_packets
= rx_packets
;
2855 stats
->rx_errs
= rx_errs
;
2856 stats
->rx_dropped
= rx_dropped
;
2857 stats
->tx_bytes
= tx_bytes
;
2858 stats
->tx_packets
= tx_packets
;
2859 stats
->tx_errs
= tx_errs
;
2860 stats
->tx_dropped
= tx_dropped
;
2868 g_debug("/proc/net/dev: Interface '%s' not found", name
);
2869 #else /* !CONFIG_LINUX */
2870 g_debug("Network stats reporting available only for Linux");
2871 #endif /* !CONFIG_LINUX */
2877 * Fill "buf" with MAC address by ifaddrs. Pointer buf must point to a
2878 * buffer with ETHER_ADDR_LEN length at least.
2880 * Returns false in case of an error, otherwise true. "obtained" argument
2881 * is true if a MAC address was obtained successful, otherwise false.
2883 bool guest_get_hw_addr(struct ifaddrs
*ifa
, unsigned char *buf
,
2884 bool *obtained
, Error
**errp
)
2891 /* we haven't obtained HW address yet */
2892 sock
= socket(PF_INET
, SOCK_STREAM
, 0);
2894 error_setg_errno(errp
, errno
, "failed to create socket");
2898 memset(&ifr
, 0, sizeof(ifr
));
2899 pstrcpy(ifr
.ifr_name
, IF_NAMESIZE
, ifa
->ifa_name
);
2900 if (ioctl(sock
, SIOCGIFHWADDR
, &ifr
) == -1) {
2902 * We can't get the hw addr of this interface, but that's not a
2905 if (errno
== EADDRNOTAVAIL
) {
2906 /* The interface doesn't have a hw addr (e.g. loopback). */
2907 g_debug("failed to get MAC address of %s: %s",
2908 ifa
->ifa_name
, strerror(errno
));
2910 g_warning("failed to get MAC address of %s: %s",
2911 ifa
->ifa_name
, strerror(errno
));
2914 #ifdef CONFIG_SOLARIS
2915 memcpy(buf
, &ifr
.ifr_addr
.sa_data
, ETHER_ADDR_LEN
);
2917 memcpy(buf
, &ifr
.ifr_hwaddr
.sa_data
, ETHER_ADDR_LEN
);
2924 #endif /* __FreeBSD__ */
2927 * Build information about guest interfaces
2929 GuestNetworkInterfaceList
*qmp_guest_network_get_interfaces(Error
**errp
)
2931 GuestNetworkInterfaceList
*head
= NULL
, **tail
= &head
;
2932 struct ifaddrs
*ifap
, *ifa
;
2934 if (getifaddrs(&ifap
) < 0) {
2935 error_setg_errno(errp
, errno
, "getifaddrs failed");
2939 for (ifa
= ifap
; ifa
; ifa
= ifa
->ifa_next
) {
2940 GuestNetworkInterface
*info
;
2941 GuestIpAddressList
**address_tail
;
2942 GuestIpAddress
*address_item
= NULL
;
2943 GuestNetworkInterfaceStat
*interface_stat
= NULL
;
2944 char addr4
[INET_ADDRSTRLEN
];
2945 char addr6
[INET6_ADDRSTRLEN
];
2946 unsigned char mac_addr
[ETHER_ADDR_LEN
];
2950 g_debug("Processing %s interface", ifa
->ifa_name
);
2952 info
= guest_find_interface(head
, ifa
->ifa_name
);
2955 info
= g_malloc0(sizeof(*info
));
2956 info
->name
= g_strdup(ifa
->ifa_name
);
2958 QAPI_LIST_APPEND(tail
, info
);
2961 if (!info
->hardware_address
) {
2962 if (!guest_get_hw_addr(ifa
, mac_addr
, &obtained
, errp
)) {
2966 info
->hardware_address
=
2967 g_strdup_printf("%02x:%02x:%02x:%02x:%02x:%02x",
2968 (int) mac_addr
[0], (int) mac_addr
[1],
2969 (int) mac_addr
[2], (int) mac_addr
[3],
2970 (int) mac_addr
[4], (int) mac_addr
[5]);
2974 if (ifa
->ifa_addr
&&
2975 ifa
->ifa_addr
->sa_family
== AF_INET
) {
2976 /* interface with IPv4 address */
2977 p
= &((struct sockaddr_in
*)ifa
->ifa_addr
)->sin_addr
;
2978 if (!inet_ntop(AF_INET
, p
, addr4
, sizeof(addr4
))) {
2979 error_setg_errno(errp
, errno
, "inet_ntop failed");
2983 address_item
= g_malloc0(sizeof(*address_item
));
2984 address_item
->ip_address
= g_strdup(addr4
);
2985 address_item
->ip_address_type
= GUEST_IP_ADDRESS_TYPE_IPV4
;
2987 if (ifa
->ifa_netmask
) {
2988 /* Count the number of set bits in netmask.
2989 * This is safe as '1' and '0' cannot be shuffled in netmask. */
2990 p
= &((struct sockaddr_in
*)ifa
->ifa_netmask
)->sin_addr
;
2991 address_item
->prefix
= ctpop32(((uint32_t *) p
)[0]);
2993 } else if (ifa
->ifa_addr
&&
2994 ifa
->ifa_addr
->sa_family
== AF_INET6
) {
2995 /* interface with IPv6 address */
2996 p
= &((struct sockaddr_in6
*)ifa
->ifa_addr
)->sin6_addr
;
2997 if (!inet_ntop(AF_INET6
, p
, addr6
, sizeof(addr6
))) {
2998 error_setg_errno(errp
, errno
, "inet_ntop failed");
3002 address_item
= g_malloc0(sizeof(*address_item
));
3003 address_item
->ip_address
= g_strdup(addr6
);
3004 address_item
->ip_address_type
= GUEST_IP_ADDRESS_TYPE_IPV6
;
3006 if (ifa
->ifa_netmask
) {
3007 /* Count the number of set bits in netmask.
3008 * This is safe as '1' and '0' cannot be shuffled in netmask. */
3009 p
= &((struct sockaddr_in6
*)ifa
->ifa_netmask
)->sin6_addr
;
3010 address_item
->prefix
=
3011 ctpop32(((uint32_t *) p
)[0]) +
3012 ctpop32(((uint32_t *) p
)[1]) +
3013 ctpop32(((uint32_t *) p
)[2]) +
3014 ctpop32(((uint32_t *) p
)[3]);
3018 if (!address_item
) {
3022 address_tail
= &info
->ip_addresses
;
3023 while (*address_tail
) {
3024 address_tail
= &(*address_tail
)->next
;
3026 QAPI_LIST_APPEND(address_tail
, address_item
);
3028 info
->has_ip_addresses
= true;
3030 if (!info
->statistics
) {
3031 interface_stat
= g_malloc0(sizeof(*interface_stat
));
3032 if (guest_get_network_stats(info
->name
, interface_stat
) == -1) {
3033 g_free(interface_stat
);
3035 info
->statistics
= interface_stat
;
3045 qapi_free_GuestNetworkInterfaceList(head
);
3051 GuestNetworkInterfaceList
*qmp_guest_network_get_interfaces(Error
**errp
)
3053 error_setg(errp
, QERR_UNSUPPORTED
);
3057 #endif /* HAVE_GETIFADDRS */
3059 #if !defined(CONFIG_FSFREEZE)
3061 GuestFilesystemInfoList
*qmp_guest_get_fsinfo(Error
**errp
)
3063 error_setg(errp
, QERR_UNSUPPORTED
);
3067 GuestFsfreezeStatus
qmp_guest_fsfreeze_status(Error
**errp
)
3069 error_setg(errp
, QERR_UNSUPPORTED
);
3074 int64_t qmp_guest_fsfreeze_freeze(Error
**errp
)
3076 error_setg(errp
, QERR_UNSUPPORTED
);
3081 int64_t qmp_guest_fsfreeze_freeze_list(bool has_mountpoints
,
3082 strList
*mountpoints
,
3085 error_setg(errp
, QERR_UNSUPPORTED
);
3090 int64_t qmp_guest_fsfreeze_thaw(Error
**errp
)
3092 error_setg(errp
, QERR_UNSUPPORTED
);
3097 GuestDiskInfoList
*qmp_guest_get_disks(Error
**errp
)
3099 error_setg(errp
, QERR_UNSUPPORTED
);
3103 GuestDiskStatsInfoList
*qmp_guest_get_diskstats(Error
**errp
)
3105 error_setg(errp
, QERR_UNSUPPORTED
);
3109 GuestCpuStatsList
*qmp_guest_get_cpustats(Error
**errp
)
3111 error_setg(errp
, QERR_UNSUPPORTED
);
3115 #endif /* CONFIG_FSFREEZE */
3117 #if !defined(CONFIG_FSTRIM)
3118 GuestFilesystemTrimResponse
*
3119 qmp_guest_fstrim(bool has_minimum
, int64_t minimum
, Error
**errp
)
3121 error_setg(errp
, QERR_UNSUPPORTED
);
3126 /* add unsupported commands to the list of blocked RPCs */
3127 GList
*ga_command_init_blockedrpcs(GList
*blockedrpcs
)
3129 #if !defined(__linux__)
3131 const char *list
[] = {
3132 "guest-suspend-disk", "guest-suspend-ram",
3133 "guest-suspend-hybrid", "guest-get-vcpus", "guest-set-vcpus",
3134 "guest-get-memory-blocks", "guest-set-memory-blocks",
3135 "guest-get-memory-block-size", "guest-get-memory-block-info",
3137 char **p
= (char **)list
;
3140 blockedrpcs
= g_list_append(blockedrpcs
, g_strdup(*p
++));
3145 #if !defined(HAVE_GETIFADDRS)
3146 blockedrpcs
= g_list_append(blockedrpcs
,
3147 g_strdup("guest-network-get-interfaces"));
3150 #if !defined(CONFIG_FSFREEZE)
3152 const char *list
[] = {
3153 "guest-get-fsinfo", "guest-fsfreeze-status",
3154 "guest-fsfreeze-freeze", "guest-fsfreeze-freeze-list",
3155 "guest-fsfreeze-thaw", "guest-get-fsinfo",
3156 "guest-get-disks", NULL
};
3157 char **p
= (char **)list
;
3160 blockedrpcs
= g_list_append(blockedrpcs
, g_strdup(*p
++));
3165 #if !defined(CONFIG_FSTRIM)
3166 blockedrpcs
= g_list_append(blockedrpcs
, g_strdup("guest-fstrim"));
3169 blockedrpcs
= g_list_append(blockedrpcs
, g_strdup("guest-get-devices"));
3174 /* register init/cleanup routines for stateful command groups */
3175 void ga_command_state_init(GAState
*s
, GACommandState
*cs
)
3177 #if defined(CONFIG_FSFREEZE)
3178 ga_command_state_add(cs
, NULL
, guest_fsfreeze_cleanup
);
3184 #define QGA_MICRO_SECOND_TO_SECOND 1000000
3186 static double ga_get_login_time(struct utmpx
*user_info
)
3188 double seconds
= (double)user_info
->ut_tv
.tv_sec
;
3189 double useconds
= (double)user_info
->ut_tv
.tv_usec
;
3190 useconds
/= QGA_MICRO_SECOND_TO_SECOND
;
3191 return seconds
+ useconds
;
3194 GuestUserList
*qmp_guest_get_users(Error
**errp
)
3196 GHashTable
*cache
= NULL
;
3197 GuestUserList
*head
= NULL
, **tail
= &head
;
3198 struct utmpx
*user_info
= NULL
;
3199 gpointer value
= NULL
;
3200 GuestUser
*user
= NULL
;
3201 double login_time
= 0;
3203 cache
= g_hash_table_new(g_str_hash
, g_str_equal
);
3207 user_info
= getutxent();
3208 if (user_info
== NULL
) {
3210 } else if (user_info
->ut_type
!= USER_PROCESS
) {
3212 } else if (g_hash_table_contains(cache
, user_info
->ut_user
)) {
3213 value
= g_hash_table_lookup(cache
, user_info
->ut_user
);
3214 user
= (GuestUser
*)value
;
3215 login_time
= ga_get_login_time(user_info
);
3216 /* We're ensuring the earliest login time to be sent */
3217 if (login_time
< user
->login_time
) {
3218 user
->login_time
= login_time
;
3223 user
= g_new0(GuestUser
, 1);
3224 user
->user
= g_strdup(user_info
->ut_user
);
3225 user
->login_time
= ga_get_login_time(user_info
);
3227 g_hash_table_insert(cache
, user
->user
, user
);
3229 QAPI_LIST_APPEND(tail
, user
);
3232 g_hash_table_destroy(cache
);
3238 GuestUserList
*qmp_guest_get_users(Error
**errp
)
3240 error_setg(errp
, QERR_UNSUPPORTED
);
3246 /* Replace escaped special characters with theire real values. The replacement
3247 * is done in place -- returned value is in the original string.
3249 static void ga_osrelease_replace_special(gchar
*value
)
3251 gchar
*p
, *p2
, quote
;
3253 /* Trim the string at first space or semicolon if it is not enclosed in
3254 * single or double quotes. */
3255 if ((value
[0] != '"') || (value
[0] == '\'')) {
3256 p
= strchr(value
, ' ');
3260 p
= strchr(value
, ';');
3281 /* Keep literal backslash followed by whatever is there */
3285 } else if (*p
== quote
) {
3293 static GKeyFile
*ga_parse_osrelease(const char *fname
)
3295 gchar
*content
= NULL
;
3296 gchar
*content2
= NULL
;
3298 GKeyFile
*keys
= g_key_file_new();
3299 const char *group
= "[os-release]\n";
3301 if (!g_file_get_contents(fname
, &content
, NULL
, &err
)) {
3302 slog("failed to read '%s', error: %s", fname
, err
->message
);
3306 if (!g_utf8_validate(content
, -1, NULL
)) {
3307 slog("file is not utf-8 encoded: %s", fname
);
3310 content2
= g_strdup_printf("%s%s", group
, content
);
3312 if (!g_key_file_load_from_data(keys
, content2
, -1, G_KEY_FILE_NONE
,
3314 slog("failed to parse file '%s', error: %s", fname
, err
->message
);
3326 g_key_file_free(keys
);
3330 GuestOSInfo
*qmp_guest_get_osinfo(Error
**errp
)
3332 GuestOSInfo
*info
= NULL
;
3333 struct utsname kinfo
;
3334 GKeyFile
*osrelease
= NULL
;
3335 const char *qga_os_release
= g_getenv("QGA_OS_RELEASE");
3337 info
= g_new0(GuestOSInfo
, 1);
3339 if (uname(&kinfo
) != 0) {
3340 error_setg_errno(errp
, errno
, "uname failed");
3342 info
->kernel_version
= g_strdup(kinfo
.version
);
3343 info
->kernel_release
= g_strdup(kinfo
.release
);
3344 info
->machine
= g_strdup(kinfo
.machine
);
3347 if (qga_os_release
!= NULL
) {
3348 osrelease
= ga_parse_osrelease(qga_os_release
);
3350 osrelease
= ga_parse_osrelease("/etc/os-release");
3351 if (osrelease
== NULL
) {
3352 osrelease
= ga_parse_osrelease("/usr/lib/os-release");
3356 if (osrelease
!= NULL
) {
3359 #define GET_FIELD(field, osfield) do { \
3360 value = g_key_file_get_value(osrelease, "os-release", osfield, NULL); \
3361 if (value != NULL) { \
3362 ga_osrelease_replace_special(value); \
3363 info->field = value; \
3366 GET_FIELD(id
, "ID");
3367 GET_FIELD(name
, "NAME");
3368 GET_FIELD(pretty_name
, "PRETTY_NAME");
3369 GET_FIELD(version
, "VERSION");
3370 GET_FIELD(version_id
, "VERSION_ID");
3371 GET_FIELD(variant
, "VARIANT");
3372 GET_FIELD(variant_id
, "VARIANT_ID");
3375 g_key_file_free(osrelease
);
3381 GuestDeviceInfoList
*qmp_guest_get_devices(Error
**errp
)
3383 error_setg(errp
, QERR_UNSUPPORTED
);
3388 #ifndef HOST_NAME_MAX
3389 # ifdef _POSIX_HOST_NAME_MAX
3390 # define HOST_NAME_MAX _POSIX_HOST_NAME_MAX
3392 # define HOST_NAME_MAX 255
3396 char *qga_get_host_name(Error
**errp
)
3399 g_autofree
char *hostname
= NULL
;
3401 #ifdef _SC_HOST_NAME_MAX
3402 len
= sysconf(_SC_HOST_NAME_MAX
);
3403 #endif /* _SC_HOST_NAME_MAX */
3406 len
= HOST_NAME_MAX
;
3409 /* Unfortunately, gethostname() below does not guarantee a
3410 * NULL terminated string. Therefore, allocate one byte more
3412 hostname
= g_new0(char, len
+ 1);
3414 if (gethostname(hostname
, len
) < 0) {
3415 error_setg_errno(errp
, errno
,
3416 "cannot get hostname");
3420 return g_steal_pointer(&hostname
);