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"
16 #include <sys/ioctl.h>
19 #include "qga/guest-agent-core.h"
20 #include "qga-qmp-commands.h"
21 #include "qapi/qmp/qerror.h"
22 #include "qemu/queue.h"
23 #include "qemu/host-utils.h"
24 #include "qemu/sockets.h"
25 #include "qemu/base64.h"
27 #ifndef CONFIG_HAS_ENVIRON
29 #include <crt_externs.h>
30 #define environ (*_NSGetEnviron())
32 extern char **environ
;
36 #if defined(__linux__)
40 #include <arpa/inet.h>
41 #include <sys/socket.h>
45 #define CONFIG_FSFREEZE
52 static void ga_wait_child(pid_t pid
, int *status
, Error
**errp
)
59 rpid
= waitpid(pid
, status
, 0);
60 } while (rpid
== -1 && errno
== EINTR
);
63 error_setg_errno(errp
, errno
, "failed to wait for child (pid: %d)",
68 g_assert(rpid
== pid
);
71 void qmp_guest_shutdown(bool has_mode
, const char *mode
, Error
**errp
)
73 const char *shutdown_flag
;
74 Error
*local_err
= NULL
;
78 slog("guest-shutdown called, mode: %s", mode
);
79 if (!has_mode
|| strcmp(mode
, "powerdown") == 0) {
81 } else if (strcmp(mode
, "halt") == 0) {
83 } else if (strcmp(mode
, "reboot") == 0) {
87 "mode is invalid (valid values are: halt|powerdown|reboot");
93 /* child, start the shutdown */
99 execle("/sbin/shutdown", "shutdown", "-h", shutdown_flag
, "+0",
100 "hypervisor initiated shutdown", (char*)NULL
, environ
);
102 } else if (pid
< 0) {
103 error_setg_errno(errp
, errno
, "failed to create child process");
107 ga_wait_child(pid
, &status
, &local_err
);
109 error_propagate(errp
, local_err
);
113 if (!WIFEXITED(status
)) {
114 error_setg(errp
, "child process has terminated abnormally");
118 if (WEXITSTATUS(status
)) {
119 error_setg(errp
, "child process has failed to shutdown");
126 int64_t qmp_guest_get_time(Error
**errp
)
132 ret
= qemu_gettimeofday(&tq
);
134 error_setg_errno(errp
, errno
, "Failed to get time");
138 time_ns
= tq
.tv_sec
* 1000000000LL + tq
.tv_usec
* 1000;
142 void qmp_guest_set_time(bool has_time
, int64_t time_ns
, Error
**errp
)
147 Error
*local_err
= NULL
;
150 /* If user has passed a time, validate and set it. */
154 /* year-2038 will overflow in case time_t is 32bit */
155 if (time_ns
/ 1000000000 != (time_t)(time_ns
/ 1000000000)) {
156 error_setg(errp
, "Time %" PRId64
" is too large", time_ns
);
160 tv
.tv_sec
= time_ns
/ 1000000000;
161 tv
.tv_usec
= (time_ns
% 1000000000) / 1000;
162 g_date_set_time_t(&date
, tv
.tv_sec
);
163 if (date
.year
< 1970 || date
.year
>= 2070) {
164 error_setg_errno(errp
, errno
, "Invalid time");
168 ret
= settimeofday(&tv
, NULL
);
170 error_setg_errno(errp
, errno
, "Failed to set time to guest");
175 /* Now, if user has passed a time to set and the system time is set, we
176 * just need to synchronize the hardware clock. However, if no time was
177 * passed, user is requesting the opposite: set the system time from the
178 * hardware clock (RTC). */
182 reopen_fd_to_null(0);
183 reopen_fd_to_null(1);
184 reopen_fd_to_null(2);
186 /* Use '/sbin/hwclock -w' to set RTC from the system time,
187 * or '/sbin/hwclock -s' to set the system time from RTC. */
188 execle("/sbin/hwclock", "hwclock", has_time
? "-w" : "-s",
191 } else if (pid
< 0) {
192 error_setg_errno(errp
, errno
, "failed to create child process");
196 ga_wait_child(pid
, &status
, &local_err
);
198 error_propagate(errp
, local_err
);
202 if (!WIFEXITED(status
)) {
203 error_setg(errp
, "child process has terminated abnormally");
207 if (WEXITSTATUS(status
)) {
208 error_setg(errp
, "hwclock failed to set hardware clock to system time");
219 typedef struct GuestFileHandle
{
223 QTAILQ_ENTRY(GuestFileHandle
) next
;
227 QTAILQ_HEAD(, GuestFileHandle
) filehandles
;
228 } guest_file_state
= {
229 .filehandles
= QTAILQ_HEAD_INITIALIZER(guest_file_state
.filehandles
),
232 static int64_t guest_file_handle_add(FILE *fh
, Error
**errp
)
234 GuestFileHandle
*gfh
;
237 handle
= ga_get_fd_handle(ga_state
, errp
);
242 gfh
= g_new0(GuestFileHandle
, 1);
245 QTAILQ_INSERT_TAIL(&guest_file_state
.filehandles
, gfh
, next
);
250 static GuestFileHandle
*guest_file_handle_find(int64_t id
, Error
**errp
)
252 GuestFileHandle
*gfh
;
254 QTAILQ_FOREACH(gfh
, &guest_file_state
.filehandles
, next
)
261 error_setg(errp
, "handle '%" PRId64
"' has not been found", id
);
265 typedef const char * const ccpc
;
271 /* http://pubs.opengroup.org/onlinepubs/9699919799/functions/fopen.html */
272 static const struct {
275 } guest_file_open_modes
[] = {
276 { (ccpc
[]){ "r", NULL
}, O_RDONLY
},
277 { (ccpc
[]){ "rb", NULL
}, O_RDONLY
| O_BINARY
},
278 { (ccpc
[]){ "w", NULL
}, O_WRONLY
| O_CREAT
| O_TRUNC
},
279 { (ccpc
[]){ "wb", NULL
}, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
},
280 { (ccpc
[]){ "a", NULL
}, O_WRONLY
| O_CREAT
| O_APPEND
},
281 { (ccpc
[]){ "ab", NULL
}, O_WRONLY
| O_CREAT
| O_APPEND
| O_BINARY
},
282 { (ccpc
[]){ "r+", NULL
}, O_RDWR
},
283 { (ccpc
[]){ "rb+", "r+b", NULL
}, O_RDWR
| O_BINARY
},
284 { (ccpc
[]){ "w+", NULL
}, O_RDWR
| O_CREAT
| O_TRUNC
},
285 { (ccpc
[]){ "wb+", "w+b", NULL
}, O_RDWR
| O_CREAT
| O_TRUNC
| O_BINARY
},
286 { (ccpc
[]){ "a+", NULL
}, O_RDWR
| O_CREAT
| O_APPEND
},
287 { (ccpc
[]){ "ab+", "a+b", NULL
}, O_RDWR
| O_CREAT
| O_APPEND
| O_BINARY
}
291 find_open_flag(const char *mode_str
, Error
**errp
)
295 for (mode
= 0; mode
< ARRAY_SIZE(guest_file_open_modes
); ++mode
) {
298 form
= guest_file_open_modes
[mode
].forms
;
299 while (*form
!= NULL
&& strcmp(*form
, mode_str
) != 0) {
307 if (mode
== ARRAY_SIZE(guest_file_open_modes
)) {
308 error_setg(errp
, "invalid file open mode '%s'", mode_str
);
311 return guest_file_open_modes
[mode
].oflag_base
| O_NOCTTY
| O_NONBLOCK
;
314 #define DEFAULT_NEW_FILE_MODE (S_IRUSR | S_IWUSR | \
315 S_IRGRP | S_IWGRP | \
319 safe_open_or_create(const char *path
, const char *mode
, Error
**errp
)
321 Error
*local_err
= NULL
;
324 oflag
= find_open_flag(mode
, &local_err
);
325 if (local_err
== NULL
) {
328 /* If the caller wants / allows creation of a new file, we implement it
329 * with a two step process: open() + (open() / fchmod()).
331 * First we insist on creating the file exclusively as a new file. If
332 * that succeeds, we're free to set any file-mode bits on it. (The
333 * motivation is that we want to set those file-mode bits independently
334 * of the current umask.)
336 * If the exclusive creation fails because the file already exists
337 * (EEXIST is not possible for any other reason), we just attempt to
338 * open the file, but in this case we won't be allowed to change the
339 * file-mode bits on the preexistent file.
341 * The pathname should never disappear between the two open()s in
342 * practice. If it happens, then someone very likely tried to race us.
343 * In this case just go ahead and report the ENOENT from the second
344 * open() to the caller.
346 * If the caller wants to open a preexistent file, then the first
347 * open() is decisive and its third argument is ignored, and the second
348 * open() and the fchmod() are never called.
350 fd
= open(path
, oflag
| ((oflag
& O_CREAT
) ? O_EXCL
: 0), 0);
351 if (fd
== -1 && errno
== EEXIST
) {
352 oflag
&= ~(unsigned)O_CREAT
;
353 fd
= open(path
, oflag
);
357 error_setg_errno(&local_err
, errno
, "failed to open file '%s' "
358 "(mode: '%s')", path
, mode
);
360 qemu_set_cloexec(fd
);
362 if ((oflag
& O_CREAT
) && fchmod(fd
, DEFAULT_NEW_FILE_MODE
) == -1) {
363 error_setg_errno(&local_err
, errno
, "failed to set permission "
364 "0%03o on new file '%s' (mode: '%s')",
365 (unsigned)DEFAULT_NEW_FILE_MODE
, path
, mode
);
369 f
= fdopen(fd
, mode
);
371 error_setg_errno(&local_err
, errno
, "failed to associate "
372 "stdio stream with file descriptor %d, "
373 "file '%s' (mode: '%s')", fd
, path
, mode
);
380 if (oflag
& O_CREAT
) {
386 error_propagate(errp
, local_err
);
390 int64_t qmp_guest_file_open(const char *path
, bool has_mode
, const char *mode
,
394 Error
*local_err
= NULL
;
400 slog("guest-file-open called, filepath: %s, mode: %s", path
, mode
);
401 fh
= safe_open_or_create(path
, mode
, &local_err
);
402 if (local_err
!= NULL
) {
403 error_propagate(errp
, local_err
);
407 /* set fd non-blocking to avoid common use cases (like reading from a
408 * named pipe) from hanging the agent
410 qemu_set_nonblock(fileno(fh
));
412 handle
= guest_file_handle_add(fh
, errp
);
418 slog("guest-file-open, handle: %" PRId64
, handle
);
422 void qmp_guest_file_close(int64_t handle
, Error
**errp
)
424 GuestFileHandle
*gfh
= guest_file_handle_find(handle
, errp
);
427 slog("guest-file-close called, handle: %" PRId64
, handle
);
432 ret
= fclose(gfh
->fh
);
434 error_setg_errno(errp
, errno
, "failed to close handle");
438 QTAILQ_REMOVE(&guest_file_state
.filehandles
, gfh
, next
);
442 struct GuestFileRead
*qmp_guest_file_read(int64_t handle
, bool has_count
,
443 int64_t count
, Error
**errp
)
445 GuestFileHandle
*gfh
= guest_file_handle_find(handle
, errp
);
446 GuestFileRead
*read_data
= NULL
;
456 count
= QGA_READ_COUNT_DEFAULT
;
457 } else if (count
< 0) {
458 error_setg(errp
, "value '%" PRId64
"' is invalid for argument count",
465 /* explicitly flush when switching from writing to reading */
466 if (gfh
->state
== RW_STATE_WRITING
) {
467 int ret
= fflush(fh
);
469 error_setg_errno(errp
, errno
, "failed to flush file");
472 gfh
->state
= RW_STATE_NEW
;
475 buf
= g_malloc0(count
+1);
476 read_count
= fread(buf
, 1, count
, fh
);
478 error_setg_errno(errp
, errno
, "failed to read file");
479 slog("guest-file-read failed, handle: %" PRId64
, handle
);
482 read_data
= g_new0(GuestFileRead
, 1);
483 read_data
->count
= read_count
;
484 read_data
->eof
= feof(fh
);
486 read_data
->buf_b64
= g_base64_encode(buf
, read_count
);
488 gfh
->state
= RW_STATE_READING
;
496 GuestFileWrite
*qmp_guest_file_write(int64_t handle
, const char *buf_b64
,
497 bool has_count
, int64_t count
,
500 GuestFileWrite
*write_data
= NULL
;
504 GuestFileHandle
*gfh
= guest_file_handle_find(handle
, errp
);
513 if (gfh
->state
== RW_STATE_READING
) {
514 int ret
= fseek(fh
, 0, SEEK_CUR
);
516 error_setg_errno(errp
, errno
, "failed to seek file");
519 gfh
->state
= RW_STATE_NEW
;
522 buf
= qbase64_decode(buf_b64
, -1, &buf_len
, errp
);
529 } else if (count
< 0 || count
> buf_len
) {
530 error_setg(errp
, "value '%" PRId64
"' is invalid for argument count",
536 write_count
= fwrite(buf
, 1, count
, fh
);
538 error_setg_errno(errp
, errno
, "failed to write to file");
539 slog("guest-file-write failed, handle: %" PRId64
, handle
);
541 write_data
= g_new0(GuestFileWrite
, 1);
542 write_data
->count
= write_count
;
543 write_data
->eof
= feof(fh
);
544 gfh
->state
= RW_STATE_WRITING
;
552 struct GuestFileSeek
*qmp_guest_file_seek(int64_t handle
, int64_t offset
,
553 GuestFileWhence
*whence_code
,
556 GuestFileHandle
*gfh
= guest_file_handle_find(handle
, errp
);
557 GuestFileSeek
*seek_data
= NULL
;
567 /* We stupidly exposed 'whence':'int' in our qapi */
568 whence
= ga_parse_whence(whence_code
, &err
);
570 error_propagate(errp
, err
);
575 ret
= fseek(fh
, offset
, whence
);
577 error_setg_errno(errp
, errno
, "failed to seek file");
578 if (errno
== ESPIPE
) {
579 /* file is non-seekable, stdio shouldn't be buffering anyways */
580 gfh
->state
= RW_STATE_NEW
;
583 seek_data
= g_new0(GuestFileSeek
, 1);
584 seek_data
->position
= ftell(fh
);
585 seek_data
->eof
= feof(fh
);
586 gfh
->state
= RW_STATE_NEW
;
593 void qmp_guest_file_flush(int64_t handle
, Error
**errp
)
595 GuestFileHandle
*gfh
= guest_file_handle_find(handle
, errp
);
606 error_setg_errno(errp
, errno
, "failed to flush file");
608 gfh
->state
= RW_STATE_NEW
;
612 /* linux-specific implementations. avoid this if at all possible. */
613 #if defined(__linux__)
615 #if defined(CONFIG_FSFREEZE) || defined(CONFIG_FSTRIM)
616 typedef struct FsMount
{
619 unsigned int devmajor
, devminor
;
620 QTAILQ_ENTRY(FsMount
) next
;
623 typedef QTAILQ_HEAD(FsMountList
, FsMount
) FsMountList
;
625 static void free_fs_mount_list(FsMountList
*mounts
)
627 FsMount
*mount
, *temp
;
633 QTAILQ_FOREACH_SAFE(mount
, mounts
, next
, temp
) {
634 QTAILQ_REMOVE(mounts
, mount
, next
);
635 g_free(mount
->dirname
);
636 g_free(mount
->devtype
);
641 static int dev_major_minor(const char *devpath
,
642 unsigned int *devmajor
, unsigned int *devminor
)
649 if (stat(devpath
, &st
) < 0) {
650 slog("failed to stat device file '%s': %s", devpath
, strerror(errno
));
653 if (S_ISDIR(st
.st_mode
)) {
654 /* It is bind mount */
657 if (S_ISBLK(st
.st_mode
)) {
658 *devmajor
= major(st
.st_rdev
);
659 *devminor
= minor(st
.st_rdev
);
666 * Walk the mount table and build a list of local file systems
668 static void build_fs_mount_list_from_mtab(FsMountList
*mounts
, Error
**errp
)
672 char const *mtab
= "/proc/self/mounts";
674 unsigned int devmajor
, devminor
;
676 fp
= setmntent(mtab
, "r");
678 error_setg(errp
, "failed to open mtab file: '%s'", mtab
);
682 while ((ment
= getmntent(fp
))) {
684 * An entry which device name doesn't start with a '/' is
685 * either a dummy file system or a network file system.
686 * Add special handling for smbfs and cifs as is done by
689 if ((ment
->mnt_fsname
[0] != '/') ||
690 (strcmp(ment
->mnt_type
, "smbfs") == 0) ||
691 (strcmp(ment
->mnt_type
, "cifs") == 0)) {
694 if (dev_major_minor(ment
->mnt_fsname
, &devmajor
, &devminor
) == -2) {
695 /* Skip bind mounts */
699 mount
= g_new0(FsMount
, 1);
700 mount
->dirname
= g_strdup(ment
->mnt_dir
);
701 mount
->devtype
= g_strdup(ment
->mnt_type
);
702 mount
->devmajor
= devmajor
;
703 mount
->devminor
= devminor
;
705 QTAILQ_INSERT_TAIL(mounts
, mount
, next
);
711 static void decode_mntname(char *name
, int len
)
714 for (i
= 0; i
<= len
; i
++) {
715 if (name
[i
] != '\\') {
717 } else if (name
[i
+ 1] == '\\') {
720 } else if (name
[i
+ 1] >= '0' && name
[i
+ 1] <= '3' &&
721 name
[i
+ 2] >= '0' && name
[i
+ 2] <= '7' &&
722 name
[i
+ 3] >= '0' && name
[i
+ 3] <= '7') {
723 name
[j
++] = (name
[i
+ 1] - '0') * 64 +
724 (name
[i
+ 2] - '0') * 8 +
733 static void build_fs_mount_list(FsMountList
*mounts
, Error
**errp
)
736 char const *mountinfo
= "/proc/self/mountinfo";
738 char *line
= NULL
, *dash
;
741 unsigned int devmajor
, devminor
;
742 int ret
, dir_s
, dir_e
, type_s
, type_e
, dev_s
, dev_e
;
744 fp
= fopen(mountinfo
, "r");
746 build_fs_mount_list_from_mtab(mounts
, errp
);
750 while (getline(&line
, &n
, fp
) != -1) {
751 ret
= sscanf(line
, "%*u %*u %u:%u %*s %n%*s%n%c",
752 &devmajor
, &devminor
, &dir_s
, &dir_e
, &check
);
756 dash
= strstr(line
+ dir_e
, " - ");
760 ret
= sscanf(dash
, " - %n%*s%n %n%*s%n%c",
761 &type_s
, &type_e
, &dev_s
, &dev_e
, &check
);
768 decode_mntname(line
+ dir_s
, dir_e
- dir_s
);
769 decode_mntname(dash
+ dev_s
, dev_e
- dev_s
);
771 /* btrfs reports major number = 0 */
772 if (strcmp("btrfs", dash
+ type_s
) != 0 ||
773 dev_major_minor(dash
+ dev_s
, &devmajor
, &devminor
) < 0) {
778 mount
= g_new0(FsMount
, 1);
779 mount
->dirname
= g_strdup(line
+ dir_s
);
780 mount
->devtype
= g_strdup(dash
+ type_s
);
781 mount
->devmajor
= devmajor
;
782 mount
->devminor
= devminor
;
784 QTAILQ_INSERT_TAIL(mounts
, mount
, next
);
792 #if defined(CONFIG_FSFREEZE)
794 static char *get_pci_driver(char const *syspath
, int pathlen
, Error
**errp
)
802 path
= g_strndup(syspath
, pathlen
);
803 dpath
= g_strdup_printf("%s/driver", path
);
804 len
= readlink(dpath
, buf
, sizeof(buf
) - 1);
807 driver
= g_strdup(basename(buf
));
814 static int compare_uint(const void *_a
, const void *_b
)
816 unsigned int a
= *(unsigned int *)_a
;
817 unsigned int b
= *(unsigned int *)_b
;
819 return a
< b
? -1 : a
> b
? 1 : 0;
822 /* Walk the specified sysfs and build a sorted list of host or ata numbers */
823 static int build_hosts(char const *syspath
, char const *host
, bool ata
,
824 unsigned int *hosts
, int hosts_max
, Error
**errp
)
828 struct dirent
*entry
;
831 path
= g_strndup(syspath
, host
- syspath
);
834 error_setg_errno(errp
, errno
, "opendir(\"%s\")", path
);
839 while (i
< hosts_max
) {
840 entry
= readdir(dir
);
844 if (ata
&& sscanf(entry
->d_name
, "ata%d", hosts
+ i
) == 1) {
846 } else if (!ata
&& sscanf(entry
->d_name
, "host%d", hosts
+ i
) == 1) {
851 qsort(hosts
, i
, sizeof(hosts
[0]), compare_uint
);
858 /* Store disk device info specified by @sysfs into @fs */
859 static void build_guest_fsinfo_for_real_device(char const *syspath
,
860 GuestFilesystemInfo
*fs
,
863 unsigned int pci
[4], host
, hosts
[8], tgt
[3];
864 int i
, nhosts
= 0, pcilen
;
865 GuestDiskAddress
*disk
;
866 GuestPCIAddress
*pciaddr
;
867 GuestDiskAddressList
*list
= NULL
;
868 bool has_ata
= false, has_host
= false, has_tgt
= false;
869 char *p
, *q
, *driver
= NULL
;
871 p
= strstr(syspath
, "/devices/pci");
872 if (!p
|| sscanf(p
+ 12, "%*x:%*x/%x:%x:%x.%x%n",
873 pci
, pci
+ 1, pci
+ 2, pci
+ 3, &pcilen
) < 4) {
874 g_debug("only pci device is supported: sysfs path \"%s\"", syspath
);
878 driver
= get_pci_driver(syspath
, (p
+ 12 + pcilen
) - syspath
, errp
);
883 p
= strstr(syspath
, "/target");
884 if (p
&& sscanf(p
+ 7, "%*u:%*u:%*u/%*u:%u:%u:%u",
885 tgt
, tgt
+ 1, tgt
+ 2) == 3) {
889 p
= strstr(syspath
, "/ata");
894 p
= strstr(syspath
, "/host");
897 if (p
&& sscanf(q
, "%u", &host
) == 1) {
899 nhosts
= build_hosts(syspath
, p
, has_ata
, hosts
,
900 sizeof(hosts
) / sizeof(hosts
[0]), errp
);
906 pciaddr
= g_malloc0(sizeof(*pciaddr
));
907 pciaddr
->domain
= pci
[0];
908 pciaddr
->bus
= pci
[1];
909 pciaddr
->slot
= pci
[2];
910 pciaddr
->function
= pci
[3];
912 disk
= g_malloc0(sizeof(*disk
));
913 disk
->pci_controller
= pciaddr
;
915 list
= g_malloc0(sizeof(*list
));
918 if (strcmp(driver
, "ata_piix") == 0) {
919 /* a host per ide bus, target*:0:<unit>:0 */
920 if (!has_host
|| !has_tgt
) {
921 g_debug("invalid sysfs path '%s' (driver '%s')", syspath
, driver
);
924 for (i
= 0; i
< nhosts
; i
++) {
925 if (host
== hosts
[i
]) {
926 disk
->bus_type
= GUEST_DISK_BUS_TYPE_IDE
;
933 g_debug("no host for '%s' (driver '%s')", syspath
, driver
);
936 } else if (strcmp(driver
, "sym53c8xx") == 0) {
937 /* scsi(LSI Logic): target*:0:<unit>:0 */
939 g_debug("invalid sysfs path '%s' (driver '%s')", syspath
, driver
);
942 disk
->bus_type
= GUEST_DISK_BUS_TYPE_SCSI
;
944 } else if (strcmp(driver
, "virtio-pci") == 0) {
946 /* virtio-scsi: target*:0:0:<unit> */
947 disk
->bus_type
= GUEST_DISK_BUS_TYPE_SCSI
;
950 /* virtio-blk: 1 disk per 1 device */
951 disk
->bus_type
= GUEST_DISK_BUS_TYPE_VIRTIO
;
953 } else if (strcmp(driver
, "ahci") == 0) {
954 /* ahci: 1 host per 1 unit */
955 if (!has_host
|| !has_tgt
) {
956 g_debug("invalid sysfs path '%s' (driver '%s')", syspath
, driver
);
959 for (i
= 0; i
< nhosts
; i
++) {
960 if (host
== hosts
[i
]) {
962 disk
->bus_type
= GUEST_DISK_BUS_TYPE_SATA
;
967 g_debug("no host for '%s' (driver '%s')", syspath
, driver
);
971 g_debug("unknown driver '%s' (sysfs path '%s')", driver
, syspath
);
975 list
->next
= fs
->disk
;
982 qapi_free_GuestDiskAddressList(list
);
987 static void build_guest_fsinfo_for_device(char const *devpath
,
988 GuestFilesystemInfo
*fs
,
991 /* Store a list of slave devices of virtual volume specified by @syspath into
993 static void build_guest_fsinfo_for_virtual_device(char const *syspath
,
994 GuestFilesystemInfo
*fs
,
999 struct dirent
*entry
;
1001 dirpath
= g_strdup_printf("%s/slaves", syspath
);
1002 dir
= opendir(dirpath
);
1004 error_setg_errno(errp
, errno
, "opendir(\"%s\")", dirpath
);
1011 entry
= readdir(dir
);
1012 if (entry
== NULL
) {
1014 error_setg_errno(errp
, errno
, "readdir(\"%s\")", dirpath
);
1019 if (entry
->d_type
== DT_LNK
) {
1022 g_debug(" slave device '%s'", entry
->d_name
);
1023 path
= g_strdup_printf("%s/slaves/%s", syspath
, entry
->d_name
);
1024 build_guest_fsinfo_for_device(path
, fs
, errp
);
1037 /* Dispatch to functions for virtual/real device */
1038 static void build_guest_fsinfo_for_device(char const *devpath
,
1039 GuestFilesystemInfo
*fs
,
1042 char *syspath
= realpath(devpath
, NULL
);
1045 error_setg_errno(errp
, errno
, "realpath(\"%s\")", devpath
);
1050 fs
->name
= g_strdup(basename(syspath
));
1053 g_debug(" parse sysfs path '%s'", syspath
);
1055 if (strstr(syspath
, "/devices/virtual/block/")) {
1056 build_guest_fsinfo_for_virtual_device(syspath
, fs
, errp
);
1058 build_guest_fsinfo_for_real_device(syspath
, fs
, errp
);
1064 /* Return a list of the disk device(s)' info which @mount lies on */
1065 static GuestFilesystemInfo
*build_guest_fsinfo(struct FsMount
*mount
,
1068 GuestFilesystemInfo
*fs
= g_malloc0(sizeof(*fs
));
1069 char *devpath
= g_strdup_printf("/sys/dev/block/%u:%u",
1070 mount
->devmajor
, mount
->devminor
);
1072 fs
->mountpoint
= g_strdup(mount
->dirname
);
1073 fs
->type
= g_strdup(mount
->devtype
);
1074 build_guest_fsinfo_for_device(devpath
, fs
, errp
);
1080 GuestFilesystemInfoList
*qmp_guest_get_fsinfo(Error
**errp
)
1083 struct FsMount
*mount
;
1084 GuestFilesystemInfoList
*new, *ret
= NULL
;
1085 Error
*local_err
= NULL
;
1087 QTAILQ_INIT(&mounts
);
1088 build_fs_mount_list(&mounts
, &local_err
);
1090 error_propagate(errp
, local_err
);
1094 QTAILQ_FOREACH(mount
, &mounts
, next
) {
1095 g_debug("Building guest fsinfo for '%s'", mount
->dirname
);
1097 new = g_malloc0(sizeof(*ret
));
1098 new->value
= build_guest_fsinfo(mount
, &local_err
);
1102 error_propagate(errp
, local_err
);
1103 qapi_free_GuestFilesystemInfoList(ret
);
1109 free_fs_mount_list(&mounts
);
1115 FSFREEZE_HOOK_THAW
= 0,
1116 FSFREEZE_HOOK_FREEZE
,
1119 static const char *fsfreeze_hook_arg_string
[] = {
1124 static void execute_fsfreeze_hook(FsfreezeHookArg arg
, Error
**errp
)
1129 const char *arg_str
= fsfreeze_hook_arg_string
[arg
];
1130 Error
*local_err
= NULL
;
1132 hook
= ga_fsfreeze_hook(ga_state
);
1136 if (access(hook
, X_OK
) != 0) {
1137 error_setg_errno(errp
, errno
, "can't access fsfreeze hook '%s'", hook
);
1141 slog("executing fsfreeze hook with arg '%s'", arg_str
);
1145 reopen_fd_to_null(0);
1146 reopen_fd_to_null(1);
1147 reopen_fd_to_null(2);
1149 execle(hook
, hook
, arg_str
, NULL
, environ
);
1150 _exit(EXIT_FAILURE
);
1151 } else if (pid
< 0) {
1152 error_setg_errno(errp
, errno
, "failed to create child process");
1156 ga_wait_child(pid
, &status
, &local_err
);
1158 error_propagate(errp
, local_err
);
1162 if (!WIFEXITED(status
)) {
1163 error_setg(errp
, "fsfreeze hook has terminated abnormally");
1167 status
= WEXITSTATUS(status
);
1169 error_setg(errp
, "fsfreeze hook has failed with status %d", status
);
1175 * Return status of freeze/thaw
1177 GuestFsfreezeStatus
qmp_guest_fsfreeze_status(Error
**errp
)
1179 if (ga_is_frozen(ga_state
)) {
1180 return GUEST_FSFREEZE_STATUS_FROZEN
;
1183 return GUEST_FSFREEZE_STATUS_THAWED
;
1186 int64_t qmp_guest_fsfreeze_freeze(Error
**errp
)
1188 return qmp_guest_fsfreeze_freeze_list(false, NULL
, errp
);
1192 * Walk list of mounted file systems in the guest, and freeze the ones which
1193 * are real local file systems.
1195 int64_t qmp_guest_fsfreeze_freeze_list(bool has_mountpoints
,
1196 strList
*mountpoints
,
1202 struct FsMount
*mount
;
1203 Error
*local_err
= NULL
;
1206 slog("guest-fsfreeze called");
1208 execute_fsfreeze_hook(FSFREEZE_HOOK_FREEZE
, &local_err
);
1210 error_propagate(errp
, local_err
);
1214 QTAILQ_INIT(&mounts
);
1215 build_fs_mount_list(&mounts
, &local_err
);
1217 error_propagate(errp
, local_err
);
1221 /* cannot risk guest agent blocking itself on a write in this state */
1222 ga_set_frozen(ga_state
);
1224 QTAILQ_FOREACH_REVERSE(mount
, &mounts
, FsMountList
, next
) {
1225 /* To issue fsfreeze in the reverse order of mounts, check if the
1226 * mount is listed in the list here */
1227 if (has_mountpoints
) {
1228 for (list
= mountpoints
; list
; list
= list
->next
) {
1229 if (strcmp(list
->value
, mount
->dirname
) == 0) {
1238 fd
= qemu_open(mount
->dirname
, O_RDONLY
);
1240 error_setg_errno(errp
, errno
, "failed to open %s", mount
->dirname
);
1244 /* we try to cull filesytems we know won't work in advance, but other
1245 * filesytems may not implement fsfreeze for less obvious reasons.
1246 * these will report EOPNOTSUPP. we simply ignore these when tallying
1247 * the number of frozen filesystems.
1249 * any other error means a failure to freeze a filesystem we
1250 * expect to be freezable, so return an error in those cases
1251 * and return system to thawed state.
1253 ret
= ioctl(fd
, FIFREEZE
);
1255 if (errno
!= EOPNOTSUPP
) {
1256 error_setg_errno(errp
, errno
, "failed to freeze %s",
1267 free_fs_mount_list(&mounts
);
1271 free_fs_mount_list(&mounts
);
1272 qmp_guest_fsfreeze_thaw(NULL
);
1277 * Walk list of frozen file systems in the guest, and thaw them.
1279 int64_t qmp_guest_fsfreeze_thaw(Error
**errp
)
1284 int fd
, i
= 0, logged
;
1285 Error
*local_err
= NULL
;
1287 QTAILQ_INIT(&mounts
);
1288 build_fs_mount_list(&mounts
, &local_err
);
1290 error_propagate(errp
, local_err
);
1294 QTAILQ_FOREACH(mount
, &mounts
, next
) {
1296 fd
= qemu_open(mount
->dirname
, O_RDONLY
);
1300 /* we have no way of knowing whether a filesystem was actually unfrozen
1301 * as a result of a successful call to FITHAW, only that if an error
1302 * was returned the filesystem was *not* unfrozen by that particular
1305 * since multiple preceding FIFREEZEs require multiple calls to FITHAW
1306 * to unfreeze, continuing issuing FITHAW until an error is returned,
1307 * in which case either the filesystem is in an unfreezable state, or,
1308 * more likely, it was thawed previously (and remains so afterward).
1310 * also, since the most recent successful call is the one that did
1311 * the actual unfreeze, we can use this to provide an accurate count
1312 * of the number of filesystems unfrozen by guest-fsfreeze-thaw, which
1313 * may * be useful for determining whether a filesystem was unfrozen
1314 * during the freeze/thaw phase by a process other than qemu-ga.
1317 ret
= ioctl(fd
, FITHAW
);
1318 if (ret
== 0 && !logged
) {
1326 ga_unset_frozen(ga_state
);
1327 free_fs_mount_list(&mounts
);
1329 execute_fsfreeze_hook(FSFREEZE_HOOK_THAW
, errp
);
1334 static void guest_fsfreeze_cleanup(void)
1338 if (ga_is_frozen(ga_state
) == GUEST_FSFREEZE_STATUS_FROZEN
) {
1339 qmp_guest_fsfreeze_thaw(&err
);
1341 slog("failed to clean up frozen filesystems: %s",
1342 error_get_pretty(err
));
1347 #endif /* CONFIG_FSFREEZE */
1349 #if defined(CONFIG_FSTRIM)
1351 * Walk list of mounted file systems in the guest, and trim them.
1353 GuestFilesystemTrimResponse
*
1354 qmp_guest_fstrim(bool has_minimum
, int64_t minimum
, Error
**errp
)
1356 GuestFilesystemTrimResponse
*response
;
1357 GuestFilesystemTrimResultList
*list
;
1358 GuestFilesystemTrimResult
*result
;
1361 struct FsMount
*mount
;
1363 Error
*local_err
= NULL
;
1364 struct fstrim_range r
;
1366 slog("guest-fstrim called");
1368 QTAILQ_INIT(&mounts
);
1369 build_fs_mount_list(&mounts
, &local_err
);
1371 error_propagate(errp
, local_err
);
1375 response
= g_malloc0(sizeof(*response
));
1377 QTAILQ_FOREACH(mount
, &mounts
, next
) {
1378 result
= g_malloc0(sizeof(*result
));
1379 result
->path
= g_strdup(mount
->dirname
);
1381 list
= g_malloc0(sizeof(*list
));
1382 list
->value
= result
;
1383 list
->next
= response
->paths
;
1384 response
->paths
= list
;
1386 fd
= qemu_open(mount
->dirname
, O_RDONLY
);
1388 result
->error
= g_strdup_printf("failed to open: %s",
1390 result
->has_error
= true;
1394 /* We try to cull filesytems we know won't work in advance, but other
1395 * filesytems may not implement fstrim for less obvious reasons. These
1396 * will report EOPNOTSUPP; while in some other cases ENOTTY will be
1397 * reported (e.g. CD-ROMs).
1398 * Any other error means an unexpected error.
1402 r
.minlen
= has_minimum
? minimum
: 0;
1403 ret
= ioctl(fd
, FITRIM
, &r
);
1405 result
->has_error
= true;
1406 if (errno
== ENOTTY
|| errno
== EOPNOTSUPP
) {
1407 result
->error
= g_strdup("trim not supported");
1409 result
->error
= g_strdup_printf("failed to trim: %s",
1416 result
->has_minimum
= true;
1417 result
->minimum
= r
.minlen
;
1418 result
->has_trimmed
= true;
1419 result
->trimmed
= r
.len
;
1423 free_fs_mount_list(&mounts
);
1426 #endif /* CONFIG_FSTRIM */
1429 #define LINUX_SYS_STATE_FILE "/sys/power/state"
1430 #define SUSPEND_SUPPORTED 0
1431 #define SUSPEND_NOT_SUPPORTED 1
1433 static void bios_supports_mode(const char *pmutils_bin
, const char *pmutils_arg
,
1434 const char *sysfile_str
, Error
**errp
)
1436 Error
*local_err
= NULL
;
1441 pmutils_path
= g_find_program_in_path(pmutils_bin
);
1445 char buf
[32]; /* hopefully big enough */
1450 reopen_fd_to_null(0);
1451 reopen_fd_to_null(1);
1452 reopen_fd_to_null(2);
1455 execle(pmutils_path
, pmutils_bin
, pmutils_arg
, NULL
, environ
);
1459 * If we get here either pm-utils is not installed or execle() has
1460 * failed. Let's try the manual method if the caller wants it.
1464 _exit(SUSPEND_NOT_SUPPORTED
);
1467 fd
= open(LINUX_SYS_STATE_FILE
, O_RDONLY
);
1469 _exit(SUSPEND_NOT_SUPPORTED
);
1472 ret
= read(fd
, buf
, sizeof(buf
)-1);
1474 _exit(SUSPEND_NOT_SUPPORTED
);
1478 if (strstr(buf
, sysfile_str
)) {
1479 _exit(SUSPEND_SUPPORTED
);
1482 _exit(SUSPEND_NOT_SUPPORTED
);
1483 } else if (pid
< 0) {
1484 error_setg_errno(errp
, errno
, "failed to create child process");
1488 ga_wait_child(pid
, &status
, &local_err
);
1490 error_propagate(errp
, local_err
);
1494 if (!WIFEXITED(status
)) {
1495 error_setg(errp
, "child process has terminated abnormally");
1499 switch (WEXITSTATUS(status
)) {
1500 case SUSPEND_SUPPORTED
:
1502 case SUSPEND_NOT_SUPPORTED
:
1504 "the requested suspend mode is not supported by the guest");
1508 "the helper program '%s' returned an unexpected exit status"
1509 " code (%d)", pmutils_path
, WEXITSTATUS(status
));
1514 g_free(pmutils_path
);
1517 static void guest_suspend(const char *pmutils_bin
, const char *sysfile_str
,
1520 Error
*local_err
= NULL
;
1525 pmutils_path
= g_find_program_in_path(pmutils_bin
);
1533 reopen_fd_to_null(0);
1534 reopen_fd_to_null(1);
1535 reopen_fd_to_null(2);
1538 execle(pmutils_path
, pmutils_bin
, NULL
, environ
);
1542 * If we get here either pm-utils is not installed or execle() has
1543 * failed. Let's try the manual method if the caller wants it.
1547 _exit(EXIT_FAILURE
);
1550 fd
= open(LINUX_SYS_STATE_FILE
, O_WRONLY
);
1552 _exit(EXIT_FAILURE
);
1555 if (write(fd
, sysfile_str
, strlen(sysfile_str
)) < 0) {
1556 _exit(EXIT_FAILURE
);
1559 _exit(EXIT_SUCCESS
);
1560 } else if (pid
< 0) {
1561 error_setg_errno(errp
, errno
, "failed to create child process");
1565 ga_wait_child(pid
, &status
, &local_err
);
1567 error_propagate(errp
, local_err
);
1571 if (!WIFEXITED(status
)) {
1572 error_setg(errp
, "child process has terminated abnormally");
1576 if (WEXITSTATUS(status
)) {
1577 error_setg(errp
, "child process has failed to suspend");
1582 g_free(pmutils_path
);
1585 void qmp_guest_suspend_disk(Error
**errp
)
1587 Error
*local_err
= NULL
;
1589 bios_supports_mode("pm-is-supported", "--hibernate", "disk", &local_err
);
1591 error_propagate(errp
, local_err
);
1595 guest_suspend("pm-hibernate", "disk", errp
);
1598 void qmp_guest_suspend_ram(Error
**errp
)
1600 Error
*local_err
= NULL
;
1602 bios_supports_mode("pm-is-supported", "--suspend", "mem", &local_err
);
1604 error_propagate(errp
, local_err
);
1608 guest_suspend("pm-suspend", "mem", errp
);
1611 void qmp_guest_suspend_hybrid(Error
**errp
)
1613 Error
*local_err
= NULL
;
1615 bios_supports_mode("pm-is-supported", "--suspend-hybrid", NULL
,
1618 error_propagate(errp
, local_err
);
1622 guest_suspend("pm-suspend-hybrid", NULL
, errp
);
1625 static GuestNetworkInterfaceList
*
1626 guest_find_interface(GuestNetworkInterfaceList
*head
,
1629 for (; head
; head
= head
->next
) {
1630 if (strcmp(head
->value
->name
, name
) == 0) {
1639 * Build information about guest interfaces
1641 GuestNetworkInterfaceList
*qmp_guest_network_get_interfaces(Error
**errp
)
1643 GuestNetworkInterfaceList
*head
= NULL
, *cur_item
= NULL
;
1644 struct ifaddrs
*ifap
, *ifa
;
1646 if (getifaddrs(&ifap
) < 0) {
1647 error_setg_errno(errp
, errno
, "getifaddrs failed");
1651 for (ifa
= ifap
; ifa
; ifa
= ifa
->ifa_next
) {
1652 GuestNetworkInterfaceList
*info
;
1653 GuestIpAddressList
**address_list
= NULL
, *address_item
= NULL
;
1654 char addr4
[INET_ADDRSTRLEN
];
1655 char addr6
[INET6_ADDRSTRLEN
];
1658 unsigned char *mac_addr
;
1661 g_debug("Processing %s interface", ifa
->ifa_name
);
1663 info
= guest_find_interface(head
, ifa
->ifa_name
);
1666 info
= g_malloc0(sizeof(*info
));
1667 info
->value
= g_malloc0(sizeof(*info
->value
));
1668 info
->value
->name
= g_strdup(ifa
->ifa_name
);
1671 head
= cur_item
= info
;
1673 cur_item
->next
= info
;
1678 if (!info
->value
->has_hardware_address
&&
1679 ifa
->ifa_flags
& SIOCGIFHWADDR
) {
1680 /* we haven't obtained HW address yet */
1681 sock
= socket(PF_INET
, SOCK_STREAM
, 0);
1683 error_setg_errno(errp
, errno
, "failed to create socket");
1687 memset(&ifr
, 0, sizeof(ifr
));
1688 pstrcpy(ifr
.ifr_name
, IF_NAMESIZE
, info
->value
->name
);
1689 if (ioctl(sock
, SIOCGIFHWADDR
, &ifr
) == -1) {
1690 error_setg_errno(errp
, errno
,
1691 "failed to get MAC address of %s",
1698 mac_addr
= (unsigned char *) &ifr
.ifr_hwaddr
.sa_data
;
1700 info
->value
->hardware_address
=
1701 g_strdup_printf("%02x:%02x:%02x:%02x:%02x:%02x",
1702 (int) mac_addr
[0], (int) mac_addr
[1],
1703 (int) mac_addr
[2], (int) mac_addr
[3],
1704 (int) mac_addr
[4], (int) mac_addr
[5]);
1706 info
->value
->has_hardware_address
= true;
1709 if (ifa
->ifa_addr
&&
1710 ifa
->ifa_addr
->sa_family
== AF_INET
) {
1711 /* interface with IPv4 address */
1712 p
= &((struct sockaddr_in
*)ifa
->ifa_addr
)->sin_addr
;
1713 if (!inet_ntop(AF_INET
, p
, addr4
, sizeof(addr4
))) {
1714 error_setg_errno(errp
, errno
, "inet_ntop failed");
1718 address_item
= g_malloc0(sizeof(*address_item
));
1719 address_item
->value
= g_malloc0(sizeof(*address_item
->value
));
1720 address_item
->value
->ip_address
= g_strdup(addr4
);
1721 address_item
->value
->ip_address_type
= GUEST_IP_ADDRESS_TYPE_IPV4
;
1723 if (ifa
->ifa_netmask
) {
1724 /* Count the number of set bits in netmask.
1725 * This is safe as '1' and '0' cannot be shuffled in netmask. */
1726 p
= &((struct sockaddr_in
*)ifa
->ifa_netmask
)->sin_addr
;
1727 address_item
->value
->prefix
= ctpop32(((uint32_t *) p
)[0]);
1729 } else if (ifa
->ifa_addr
&&
1730 ifa
->ifa_addr
->sa_family
== AF_INET6
) {
1731 /* interface with IPv6 address */
1732 p
= &((struct sockaddr_in6
*)ifa
->ifa_addr
)->sin6_addr
;
1733 if (!inet_ntop(AF_INET6
, p
, addr6
, sizeof(addr6
))) {
1734 error_setg_errno(errp
, errno
, "inet_ntop failed");
1738 address_item
= g_malloc0(sizeof(*address_item
));
1739 address_item
->value
= g_malloc0(sizeof(*address_item
->value
));
1740 address_item
->value
->ip_address
= g_strdup(addr6
);
1741 address_item
->value
->ip_address_type
= GUEST_IP_ADDRESS_TYPE_IPV6
;
1743 if (ifa
->ifa_netmask
) {
1744 /* Count the number of set bits in netmask.
1745 * This is safe as '1' and '0' cannot be shuffled in netmask. */
1746 p
= &((struct sockaddr_in6
*)ifa
->ifa_netmask
)->sin6_addr
;
1747 address_item
->value
->prefix
=
1748 ctpop32(((uint32_t *) p
)[0]) +
1749 ctpop32(((uint32_t *) p
)[1]) +
1750 ctpop32(((uint32_t *) p
)[2]) +
1751 ctpop32(((uint32_t *) p
)[3]);
1755 if (!address_item
) {
1759 address_list
= &info
->value
->ip_addresses
;
1761 while (*address_list
&& (*address_list
)->next
) {
1762 address_list
= &(*address_list
)->next
;
1765 if (!*address_list
) {
1766 *address_list
= address_item
;
1768 (*address_list
)->next
= address_item
;
1771 info
->value
->has_ip_addresses
= true;
1781 qapi_free_GuestNetworkInterfaceList(head
);
1785 #define SYSCONF_EXACT(name, errp) sysconf_exact((name), #name, (errp))
1787 static long sysconf_exact(int name
, const char *name_str
, Error
**errp
)
1792 ret
= sysconf(name
);
1795 error_setg(errp
, "sysconf(%s): value indefinite", name_str
);
1797 error_setg_errno(errp
, errno
, "sysconf(%s)", name_str
);
1803 /* Transfer online/offline status between @vcpu and the guest system.
1805 * On input either @errp or *@errp must be NULL.
1807 * In system-to-@vcpu direction, the following @vcpu fields are accessed:
1808 * - R: vcpu->logical_id
1810 * - W: vcpu->can_offline
1812 * In @vcpu-to-system direction, the following @vcpu fields are accessed:
1813 * - R: vcpu->logical_id
1816 * Written members remain unmodified on error.
1818 static void transfer_vcpu(GuestLogicalProcessor
*vcpu
, bool sys2vcpu
,
1824 dirpath
= g_strdup_printf("/sys/devices/system/cpu/cpu%" PRId64
"/",
1826 dirfd
= open(dirpath
, O_RDONLY
| O_DIRECTORY
);
1828 error_setg_errno(errp
, errno
, "open(\"%s\")", dirpath
);
1830 static const char fn
[] = "online";
1834 fd
= openat(dirfd
, fn
, sys2vcpu
? O_RDONLY
: O_RDWR
);
1836 if (errno
!= ENOENT
) {
1837 error_setg_errno(errp
, errno
, "open(\"%s/%s\")", dirpath
, fn
);
1838 } else if (sys2vcpu
) {
1839 vcpu
->online
= true;
1840 vcpu
->can_offline
= false;
1841 } else if (!vcpu
->online
) {
1842 error_setg(errp
, "logical processor #%" PRId64
" can't be "
1843 "offlined", vcpu
->logical_id
);
1844 } /* otherwise pretend successful re-onlining */
1846 unsigned char status
;
1848 res
= pread(fd
, &status
, 1, 0);
1850 error_setg_errno(errp
, errno
, "pread(\"%s/%s\")", dirpath
, fn
);
1851 } else if (res
== 0) {
1852 error_setg(errp
, "pread(\"%s/%s\"): unexpected EOF", dirpath
,
1854 } else if (sys2vcpu
) {
1855 vcpu
->online
= (status
!= '0');
1856 vcpu
->can_offline
= true;
1857 } else if (vcpu
->online
!= (status
!= '0')) {
1858 status
= '0' + vcpu
->online
;
1859 if (pwrite(fd
, &status
, 1, 0) == -1) {
1860 error_setg_errno(errp
, errno
, "pwrite(\"%s/%s\")", dirpath
,
1863 } /* otherwise pretend successful re-(on|off)-lining */
1876 GuestLogicalProcessorList
*qmp_guest_get_vcpus(Error
**errp
)
1879 GuestLogicalProcessorList
*head
, **link
;
1881 Error
*local_err
= NULL
;
1886 sc_max
= SYSCONF_EXACT(_SC_NPROCESSORS_CONF
, &local_err
);
1888 while (local_err
== NULL
&& current
< sc_max
) {
1889 GuestLogicalProcessor
*vcpu
;
1890 GuestLogicalProcessorList
*entry
;
1892 vcpu
= g_malloc0(sizeof *vcpu
);
1893 vcpu
->logical_id
= current
++;
1894 vcpu
->has_can_offline
= true; /* lolspeak ftw */
1895 transfer_vcpu(vcpu
, true, &local_err
);
1897 entry
= g_malloc0(sizeof *entry
);
1898 entry
->value
= vcpu
;
1901 link
= &entry
->next
;
1904 if (local_err
== NULL
) {
1905 /* there's no guest with zero VCPUs */
1906 g_assert(head
!= NULL
);
1910 qapi_free_GuestLogicalProcessorList(head
);
1911 error_propagate(errp
, local_err
);
1915 int64_t qmp_guest_set_vcpus(GuestLogicalProcessorList
*vcpus
, Error
**errp
)
1918 Error
*local_err
= NULL
;
1921 while (vcpus
!= NULL
) {
1922 transfer_vcpu(vcpus
->value
, false, &local_err
);
1923 if (local_err
!= NULL
) {
1927 vcpus
= vcpus
->next
;
1930 if (local_err
!= NULL
) {
1931 if (processed
== 0) {
1932 error_propagate(errp
, local_err
);
1934 error_free(local_err
);
1941 void qmp_guest_set_user_password(const char *username
,
1942 const char *password
,
1946 Error
*local_err
= NULL
;
1947 char *passwd_path
= NULL
;
1950 int datafd
[2] = { -1, -1 };
1951 char *rawpasswddata
= NULL
;
1952 size_t rawpasswdlen
;
1953 char *chpasswddata
= NULL
;
1956 rawpasswddata
= (char *)qbase64_decode(password
, -1, &rawpasswdlen
, errp
);
1957 if (!rawpasswddata
) {
1960 rawpasswddata
= g_renew(char, rawpasswddata
, rawpasswdlen
+ 1);
1961 rawpasswddata
[rawpasswdlen
] = '\0';
1963 if (strchr(rawpasswddata
, '\n')) {
1964 error_setg(errp
, "forbidden characters in raw password");
1968 if (strchr(username
, '\n') ||
1969 strchr(username
, ':')) {
1970 error_setg(errp
, "forbidden characters in username");
1974 chpasswddata
= g_strdup_printf("%s:%s\n", username
, rawpasswddata
);
1975 chpasswdlen
= strlen(chpasswddata
);
1977 passwd_path
= g_find_program_in_path("chpasswd");
1980 error_setg(errp
, "cannot find 'passwd' program in PATH");
1984 if (pipe(datafd
) < 0) {
1985 error_setg(errp
, "cannot create pipe FDs");
1995 reopen_fd_to_null(1);
1996 reopen_fd_to_null(2);
1999 execle(passwd_path
, "chpasswd", "-e", NULL
, environ
);
2001 execle(passwd_path
, "chpasswd", NULL
, environ
);
2003 _exit(EXIT_FAILURE
);
2004 } else if (pid
< 0) {
2005 error_setg_errno(errp
, errno
, "failed to create child process");
2011 if (qemu_write_full(datafd
[1], chpasswddata
, chpasswdlen
) != chpasswdlen
) {
2012 error_setg_errno(errp
, errno
, "cannot write new account password");
2018 ga_wait_child(pid
, &status
, &local_err
);
2020 error_propagate(errp
, local_err
);
2024 if (!WIFEXITED(status
)) {
2025 error_setg(errp
, "child process has terminated abnormally");
2029 if (WEXITSTATUS(status
)) {
2030 error_setg(errp
, "child process has failed to set user password");
2035 g_free(chpasswddata
);
2036 g_free(rawpasswddata
);
2037 g_free(passwd_path
);
2038 if (datafd
[0] != -1) {
2041 if (datafd
[1] != -1) {
2046 static void ga_read_sysfs_file(int dirfd
, const char *pathname
, char *buf
,
2047 int size
, Error
**errp
)
2053 fd
= openat(dirfd
, pathname
, O_RDONLY
);
2055 error_setg_errno(errp
, errno
, "open sysfs file \"%s\"", pathname
);
2059 res
= pread(fd
, buf
, size
, 0);
2061 error_setg_errno(errp
, errno
, "pread sysfs file \"%s\"", pathname
);
2062 } else if (res
== 0) {
2063 error_setg(errp
, "pread sysfs file \"%s\": unexpected EOF", pathname
);
2068 static void ga_write_sysfs_file(int dirfd
, const char *pathname
,
2069 const char *buf
, int size
, Error
**errp
)
2074 fd
= openat(dirfd
, pathname
, O_WRONLY
);
2076 error_setg_errno(errp
, errno
, "open sysfs file \"%s\"", pathname
);
2080 if (pwrite(fd
, buf
, size
, 0) == -1) {
2081 error_setg_errno(errp
, errno
, "pwrite sysfs file \"%s\"", pathname
);
2087 /* Transfer online/offline status between @mem_blk and the guest system.
2089 * On input either @errp or *@errp must be NULL.
2091 * In system-to-@mem_blk direction, the following @mem_blk fields are accessed:
2092 * - R: mem_blk->phys_index
2093 * - W: mem_blk->online
2094 * - W: mem_blk->can_offline
2096 * In @mem_blk-to-system direction, the following @mem_blk fields are accessed:
2097 * - R: mem_blk->phys_index
2098 * - R: mem_blk->online
2099 *- R: mem_blk->can_offline
2100 * Written members remain unmodified on error.
2102 static void transfer_memory_block(GuestMemoryBlock
*mem_blk
, bool sys2memblk
,
2103 GuestMemoryBlockResponse
*result
,
2109 Error
*local_err
= NULL
;
2115 error_setg(errp
, "Internal error, 'result' should not be NULL");
2119 dp
= opendir("/sys/devices/system/memory/");
2120 /* if there is no 'memory' directory in sysfs,
2121 * we think this VM does not support online/offline memory block,
2122 * any other solution?
2124 if (!dp
&& errno
== ENOENT
) {
2126 GUEST_MEMORY_BLOCK_RESPONSE_TYPE_OPERATION_NOT_SUPPORTED
;
2132 dirpath
= g_strdup_printf("/sys/devices/system/memory/memory%" PRId64
"/",
2133 mem_blk
->phys_index
);
2134 dirfd
= open(dirpath
, O_RDONLY
| O_DIRECTORY
);
2137 error_setg_errno(errp
, errno
, "open(\"%s\")", dirpath
);
2139 if (errno
== ENOENT
) {
2140 result
->response
= GUEST_MEMORY_BLOCK_RESPONSE_TYPE_NOT_FOUND
;
2143 GUEST_MEMORY_BLOCK_RESPONSE_TYPE_OPERATION_FAILED
;
2151 status
= g_malloc0(10);
2152 ga_read_sysfs_file(dirfd
, "state", status
, 10, &local_err
);
2154 /* treat with sysfs file that not exist in old kernel */
2155 if (errno
== ENOENT
) {
2156 error_free(local_err
);
2158 mem_blk
->online
= true;
2159 mem_blk
->can_offline
= false;
2160 } else if (!mem_blk
->online
) {
2162 GUEST_MEMORY_BLOCK_RESPONSE_TYPE_OPERATION_NOT_SUPPORTED
;
2166 error_propagate(errp
, local_err
);
2169 GUEST_MEMORY_BLOCK_RESPONSE_TYPE_OPERATION_FAILED
;
2176 char removable
= '0';
2178 mem_blk
->online
= (strncmp(status
, "online", 6) == 0);
2180 ga_read_sysfs_file(dirfd
, "removable", &removable
, 1, &local_err
);
2182 /* if no 'removable' file, it doesn't support offline mem blk */
2183 if (errno
== ENOENT
) {
2184 error_free(local_err
);
2185 mem_blk
->can_offline
= false;
2187 error_propagate(errp
, local_err
);
2190 mem_blk
->can_offline
= (removable
!= '0');
2193 if (mem_blk
->online
!= (strncmp(status
, "online", 6) == 0)) {
2194 char *new_state
= mem_blk
->online
? g_strdup("online") :
2195 g_strdup("offline");
2197 ga_write_sysfs_file(dirfd
, "state", new_state
, strlen(new_state
),
2201 error_free(local_err
);
2203 GUEST_MEMORY_BLOCK_RESPONSE_TYPE_OPERATION_FAILED
;
2207 result
->response
= GUEST_MEMORY_BLOCK_RESPONSE_TYPE_SUCCESS
;
2208 result
->has_error_code
= false;
2209 } /* otherwise pretend successful re-(on|off)-lining */
2220 result
->has_error_code
= true;
2221 result
->error_code
= errno
;
2225 GuestMemoryBlockList
*qmp_guest_get_memory_blocks(Error
**errp
)
2227 GuestMemoryBlockList
*head
, **link
;
2228 Error
*local_err
= NULL
;
2235 dp
= opendir("/sys/devices/system/memory/");
2237 /* it's ok if this happens to be a system that doesn't expose
2238 * memory blocks via sysfs, but otherwise we should report
2241 if (errno
!= ENOENT
) {
2242 error_setg_errno(errp
, errno
, "Can't open directory"
2243 "\"/sys/devices/system/memory/\"");
2248 /* Note: the phys_index of memory block may be discontinuous,
2249 * this is because a memblk is the unit of the Sparse Memory design, which
2250 * allows discontinuous memory ranges (ex. NUMA), so here we should
2251 * traverse the memory block directory.
2253 while ((de
= readdir(dp
)) != NULL
) {
2254 GuestMemoryBlock
*mem_blk
;
2255 GuestMemoryBlockList
*entry
;
2257 if ((strncmp(de
->d_name
, "memory", 6) != 0) ||
2258 !(de
->d_type
& DT_DIR
)) {
2262 mem_blk
= g_malloc0(sizeof *mem_blk
);
2263 /* The d_name is "memoryXXX", phys_index is block id, same as XXX */
2264 mem_blk
->phys_index
= strtoul(&de
->d_name
[6], NULL
, 10);
2265 mem_blk
->has_can_offline
= true; /* lolspeak ftw */
2266 transfer_memory_block(mem_blk
, true, NULL
, &local_err
);
2268 entry
= g_malloc0(sizeof *entry
);
2269 entry
->value
= mem_blk
;
2272 link
= &entry
->next
;
2276 if (local_err
== NULL
) {
2277 /* there's no guest with zero memory blocks */
2279 error_setg(errp
, "guest reported zero memory blocks!");
2284 qapi_free_GuestMemoryBlockList(head
);
2285 error_propagate(errp
, local_err
);
2289 GuestMemoryBlockResponseList
*
2290 qmp_guest_set_memory_blocks(GuestMemoryBlockList
*mem_blks
, Error
**errp
)
2292 GuestMemoryBlockResponseList
*head
, **link
;
2293 Error
*local_err
= NULL
;
2298 while (mem_blks
!= NULL
) {
2299 GuestMemoryBlockResponse
*result
;
2300 GuestMemoryBlockResponseList
*entry
;
2301 GuestMemoryBlock
*current_mem_blk
= mem_blks
->value
;
2303 result
= g_malloc0(sizeof(*result
));
2304 result
->phys_index
= current_mem_blk
->phys_index
;
2305 transfer_memory_block(current_mem_blk
, false, result
, &local_err
);
2306 if (local_err
) { /* should never happen */
2309 entry
= g_malloc0(sizeof *entry
);
2310 entry
->value
= result
;
2313 link
= &entry
->next
;
2314 mem_blks
= mem_blks
->next
;
2319 qapi_free_GuestMemoryBlockResponseList(head
);
2320 error_propagate(errp
, local_err
);
2324 GuestMemoryBlockInfo
*qmp_guest_get_memory_block_info(Error
**errp
)
2326 Error
*local_err
= NULL
;
2330 GuestMemoryBlockInfo
*info
;
2332 dirpath
= g_strdup_printf("/sys/devices/system/memory/");
2333 dirfd
= open(dirpath
, O_RDONLY
| O_DIRECTORY
);
2335 error_setg_errno(errp
, errno
, "open(\"%s\")", dirpath
);
2341 buf
= g_malloc0(20);
2342 ga_read_sysfs_file(dirfd
, "block_size_bytes", buf
, 20, &local_err
);
2346 error_propagate(errp
, local_err
);
2350 info
= g_new0(GuestMemoryBlockInfo
, 1);
2351 info
->size
= strtol(buf
, NULL
, 16); /* the unit is bytes */
2358 #else /* defined(__linux__) */
2360 void qmp_guest_suspend_disk(Error
**errp
)
2362 error_setg(errp
, QERR_UNSUPPORTED
);
2365 void qmp_guest_suspend_ram(Error
**errp
)
2367 error_setg(errp
, QERR_UNSUPPORTED
);
2370 void qmp_guest_suspend_hybrid(Error
**errp
)
2372 error_setg(errp
, QERR_UNSUPPORTED
);
2375 GuestNetworkInterfaceList
*qmp_guest_network_get_interfaces(Error
**errp
)
2377 error_setg(errp
, QERR_UNSUPPORTED
);
2381 GuestLogicalProcessorList
*qmp_guest_get_vcpus(Error
**errp
)
2383 error_setg(errp
, QERR_UNSUPPORTED
);
2387 int64_t qmp_guest_set_vcpus(GuestLogicalProcessorList
*vcpus
, Error
**errp
)
2389 error_setg(errp
, QERR_UNSUPPORTED
);
2393 void qmp_guest_set_user_password(const char *username
,
2394 const char *password
,
2398 error_setg(errp
, QERR_UNSUPPORTED
);
2401 GuestMemoryBlockList
*qmp_guest_get_memory_blocks(Error
**errp
)
2403 error_setg(errp
, QERR_UNSUPPORTED
);
2407 GuestMemoryBlockResponseList
*
2408 qmp_guest_set_memory_blocks(GuestMemoryBlockList
*mem_blks
, Error
**errp
)
2410 error_setg(errp
, QERR_UNSUPPORTED
);
2414 GuestMemoryBlockInfo
*qmp_guest_get_memory_block_info(Error
**errp
)
2416 error_setg(errp
, QERR_UNSUPPORTED
);
2422 #if !defined(CONFIG_FSFREEZE)
2424 GuestFilesystemInfoList
*qmp_guest_get_fsinfo(Error
**errp
)
2426 error_setg(errp
, QERR_UNSUPPORTED
);
2430 GuestFsfreezeStatus
qmp_guest_fsfreeze_status(Error
**errp
)
2432 error_setg(errp
, QERR_UNSUPPORTED
);
2437 int64_t qmp_guest_fsfreeze_freeze(Error
**errp
)
2439 error_setg(errp
, QERR_UNSUPPORTED
);
2444 int64_t qmp_guest_fsfreeze_freeze_list(bool has_mountpoints
,
2445 strList
*mountpoints
,
2448 error_setg(errp
, QERR_UNSUPPORTED
);
2453 int64_t qmp_guest_fsfreeze_thaw(Error
**errp
)
2455 error_setg(errp
, QERR_UNSUPPORTED
);
2459 #endif /* CONFIG_FSFREEZE */
2461 #if !defined(CONFIG_FSTRIM)
2462 GuestFilesystemTrimResponse
*
2463 qmp_guest_fstrim(bool has_minimum
, int64_t minimum
, Error
**errp
)
2465 error_setg(errp
, QERR_UNSUPPORTED
);
2470 /* add unsupported commands to the blacklist */
2471 GList
*ga_command_blacklist_init(GList
*blacklist
)
2473 #if !defined(__linux__)
2475 const char *list
[] = {
2476 "guest-suspend-disk", "guest-suspend-ram",
2477 "guest-suspend-hybrid", "guest-network-get-interfaces",
2478 "guest-get-vcpus", "guest-set-vcpus",
2479 "guest-get-memory-blocks", "guest-set-memory-blocks",
2480 "guest-get-memory-block-size", NULL
};
2481 char **p
= (char **)list
;
2484 blacklist
= g_list_append(blacklist
, g_strdup(*p
++));
2489 #if !defined(CONFIG_FSFREEZE)
2491 const char *list
[] = {
2492 "guest-get-fsinfo", "guest-fsfreeze-status",
2493 "guest-fsfreeze-freeze", "guest-fsfreeze-freeze-list",
2494 "guest-fsfreeze-thaw", "guest-get-fsinfo", NULL
};
2495 char **p
= (char **)list
;
2498 blacklist
= g_list_append(blacklist
, g_strdup(*p
++));
2503 #if !defined(CONFIG_FSTRIM)
2504 blacklist
= g_list_append(blacklist
, g_strdup("guest-fstrim"));
2510 /* register init/cleanup routines for stateful command groups */
2511 void ga_command_state_init(GAState
*s
, GACommandState
*cs
)
2513 #if defined(CONFIG_FSFREEZE)
2514 ga_command_state_add(cs
, NULL
, guest_fsfreeze_cleanup
);