2 * QEMU Guest Agent commands
4 * Copyright IBM Corp. 2011
7 * Michael Roth <mdroth@linux.vnet.ibm.com>
9 * This work is licensed under the terms of the GNU GPL, version 2 or later.
10 * See the COPYING file in the top-level directory.
15 #if defined(__linux__)
19 #if defined(__linux__) && defined(FIFREEZE)
20 #define CONFIG_FSFREEZE
24 #include <sys/types.h>
25 #include <sys/ioctl.h>
26 #include "qga/guest-agent-core.h"
27 #include "qga-qmp-commands.h"
29 #include "qemu-queue.h"
31 static GAState
*ga_state
;
33 /* Note: in some situations, like with the fsfreeze, logging may be
34 * temporarilly disabled. if it is necessary that a command be able
35 * to log for accounting purposes, check ga_logging_enabled() beforehand,
36 * and use the QERR_QGA_LOGGING_DISABLED to generate an error
38 static void slog(const char *fmt
, ...)
43 g_logv("syslog", G_LOG_LEVEL_INFO
, fmt
, ap
);
47 int64_t qmp_guest_sync(int64_t id
, Error
**errp
)
52 void qmp_guest_ping(Error
**err
)
54 slog("guest-ping called");
57 struct GuestAgentInfo
*qmp_guest_info(Error
**err
)
59 GuestAgentInfo
*info
= g_malloc0(sizeof(GuestAgentInfo
));
60 GuestAgentCommandInfo
*cmd_info
;
61 GuestAgentCommandInfoList
*cmd_info_list
;
62 char **cmd_list_head
, **cmd_list
;
64 info
->version
= g_strdup(QGA_VERSION
);
66 cmd_list_head
= cmd_list
= qmp_get_command_list();
67 if (*cmd_list_head
== NULL
) {
72 cmd_info
= g_malloc0(sizeof(GuestAgentCommandInfo
));
73 cmd_info
->name
= strdup(*cmd_list
);
74 cmd_info
->enabled
= qmp_command_is_enabled(cmd_info
->name
);
76 cmd_info_list
= g_malloc0(sizeof(GuestAgentCommandInfoList
));
77 cmd_info_list
->value
= cmd_info
;
78 cmd_info_list
->next
= info
->supported_commands
;
79 info
->supported_commands
= cmd_info_list
;
86 g_free(cmd_list_head
);
90 void qmp_guest_shutdown(bool has_mode
, const char *mode
, Error
**err
)
93 const char *shutdown_flag
;
95 slog("guest-shutdown called, mode: %s", mode
);
96 if (!has_mode
|| strcmp(mode
, "powerdown") == 0) {
98 } else if (strcmp(mode
, "halt") == 0) {
100 } else if (strcmp(mode
, "reboot") == 0) {
101 shutdown_flag
= "-r";
103 error_set(err
, QERR_INVALID_PARAMETER_VALUE
, "mode",
104 "halt|powerdown|reboot");
110 /* child, start the shutdown */
116 ret
= execl("/sbin/shutdown", "shutdown", shutdown_flag
, "+0",
117 "hypervisor initiated shutdown", (char*)NULL
);
119 slog("guest-shutdown failed: %s", strerror(errno
));
122 } else if (ret
< 0) {
123 error_set(err
, QERR_UNDEFINED_ERROR
);
127 typedef struct GuestFileHandle
{
130 QTAILQ_ENTRY(GuestFileHandle
) next
;
134 QTAILQ_HEAD(, GuestFileHandle
) filehandles
;
137 static void guest_file_handle_add(FILE *fh
)
139 GuestFileHandle
*gfh
;
141 gfh
= g_malloc0(sizeof(GuestFileHandle
));
142 gfh
->id
= fileno(fh
);
144 QTAILQ_INSERT_TAIL(&guest_file_state
.filehandles
, gfh
, next
);
147 static GuestFileHandle
*guest_file_handle_find(int64_t id
)
149 GuestFileHandle
*gfh
;
151 QTAILQ_FOREACH(gfh
, &guest_file_state
.filehandles
, next
)
161 int64_t qmp_guest_file_open(const char *path
, bool has_mode
, const char *mode
, Error
**err
)
170 slog("guest-file-open called, filepath: %s, mode: %s", path
, mode
);
171 fh
= fopen(path
, mode
);
173 error_set(err
, QERR_OPEN_FILE_FAILED
, path
);
177 /* set fd non-blocking to avoid common use cases (like reading from a
178 * named pipe) from hanging the agent
181 ret
= fcntl(fd
, F_GETFL
);
182 ret
= fcntl(fd
, F_SETFL
, ret
| O_NONBLOCK
);
184 error_set(err
, QERR_QGA_COMMAND_FAILED
, "fcntl() failed");
189 guest_file_handle_add(fh
);
190 slog("guest-file-open, handle: %d", fd
);
194 void qmp_guest_file_close(int64_t handle
, Error
**err
)
196 GuestFileHandle
*gfh
= guest_file_handle_find(handle
);
199 slog("guest-file-close called, handle: %ld", handle
);
201 error_set(err
, QERR_FD_NOT_FOUND
, "handle");
205 ret
= fclose(gfh
->fh
);
207 error_set(err
, QERR_QGA_COMMAND_FAILED
, "fclose() failed");
211 QTAILQ_REMOVE(&guest_file_state
.filehandles
, gfh
, next
);
215 struct GuestFileRead
*qmp_guest_file_read(int64_t handle
, bool has_count
,
216 int64_t count
, Error
**err
)
218 GuestFileHandle
*gfh
= guest_file_handle_find(handle
);
219 GuestFileRead
*read_data
= NULL
;
225 error_set(err
, QERR_FD_NOT_FOUND
, "handle");
230 count
= QGA_READ_COUNT_DEFAULT
;
231 } else if (count
< 0) {
232 error_set(err
, QERR_INVALID_PARAMETER
, "count");
237 buf
= g_malloc0(count
+1);
238 read_count
= fread(buf
, 1, count
, fh
);
240 slog("guest-file-read failed, handle: %ld", handle
);
241 error_set(err
, QERR_QGA_COMMAND_FAILED
, "fread() failed");
244 read_data
= g_malloc0(sizeof(GuestFileRead
));
245 read_data
->count
= read_count
;
246 read_data
->eof
= feof(fh
);
248 read_data
->buf_b64
= g_base64_encode(buf
, read_count
);
257 GuestFileWrite
*qmp_guest_file_write(int64_t handle
, const char *buf_b64
,
258 bool has_count
, int64_t count
, Error
**err
)
260 GuestFileWrite
*write_data
= NULL
;
264 GuestFileHandle
*gfh
= guest_file_handle_find(handle
);
268 error_set(err
, QERR_FD_NOT_FOUND
, "handle");
273 buf
= g_base64_decode(buf_b64
, &buf_len
);
277 } else if (count
< 0 || count
> buf_len
) {
279 error_set(err
, QERR_INVALID_PARAMETER
, "count");
283 write_count
= fwrite(buf
, 1, count
, fh
);
285 slog("guest-file-write failed, handle: %ld", handle
);
286 error_set(err
, QERR_QGA_COMMAND_FAILED
, "fwrite() error");
288 write_data
= g_malloc0(sizeof(GuestFileWrite
));
289 write_data
->count
= write_count
;
290 write_data
->eof
= feof(fh
);
298 struct GuestFileSeek
*qmp_guest_file_seek(int64_t handle
, int64_t offset
,
299 int64_t whence
, Error
**err
)
301 GuestFileHandle
*gfh
= guest_file_handle_find(handle
);
302 GuestFileSeek
*seek_data
= NULL
;
307 error_set(err
, QERR_FD_NOT_FOUND
, "handle");
312 ret
= fseek(fh
, offset
, whence
);
314 error_set(err
, QERR_QGA_COMMAND_FAILED
, strerror(errno
));
316 seek_data
= g_malloc0(sizeof(GuestFileRead
));
317 seek_data
->position
= ftell(fh
);
318 seek_data
->eof
= feof(fh
);
325 void qmp_guest_file_flush(int64_t handle
, Error
**err
)
327 GuestFileHandle
*gfh
= guest_file_handle_find(handle
);
332 error_set(err
, QERR_FD_NOT_FOUND
, "handle");
339 error_set(err
, QERR_QGA_COMMAND_FAILED
, strerror(errno
));
343 static void guest_file_init(void)
345 QTAILQ_INIT(&guest_file_state
.filehandles
);
348 #if defined(CONFIG_FSFREEZE)
349 static void disable_logging(void)
351 ga_disable_logging(ga_state
);
354 static void enable_logging(void)
356 ga_enable_logging(ga_state
);
359 typedef struct GuestFsfreezeMount
{
362 QTAILQ_ENTRY(GuestFsfreezeMount
) next
;
363 } GuestFsfreezeMount
;
366 GuestFsfreezeStatus status
;
367 QTAILQ_HEAD(, GuestFsfreezeMount
) mount_list
;
368 } guest_fsfreeze_state
;
371 * Walk the mount table and build a list of local file systems
373 static int guest_fsfreeze_build_mount_list(void)
376 GuestFsfreezeMount
*mount
, *temp
;
377 char const *mtab
= MOUNTED
;
380 QTAILQ_FOREACH_SAFE(mount
, &guest_fsfreeze_state
.mount_list
, next
, temp
) {
381 QTAILQ_REMOVE(&guest_fsfreeze_state
.mount_list
, mount
, next
);
382 g_free(mount
->dirname
);
383 g_free(mount
->devtype
);
387 fp
= setmntent(mtab
, "r");
389 g_warning("fsfreeze: unable to read mtab");
393 while ((ment
= getmntent(fp
))) {
395 * An entry which device name doesn't start with a '/' is
396 * either a dummy file system or a network file system.
397 * Add special handling for smbfs and cifs as is done by
400 if ((ment
->mnt_fsname
[0] != '/') ||
401 (strcmp(ment
->mnt_type
, "smbfs") == 0) ||
402 (strcmp(ment
->mnt_type
, "cifs") == 0)) {
406 mount
= g_malloc0(sizeof(GuestFsfreezeMount
));
407 mount
->dirname
= g_strdup(ment
->mnt_dir
);
408 mount
->devtype
= g_strdup(ment
->mnt_type
);
410 QTAILQ_INSERT_TAIL(&guest_fsfreeze_state
.mount_list
, mount
, next
);
419 * Return status of freeze/thaw
421 GuestFsfreezeStatus
qmp_guest_fsfreeze_status(Error
**err
)
423 return guest_fsfreeze_state
.status
;
427 * Walk list of mounted file systems in the guest, and freeze the ones which
428 * are real local file systems.
430 int64_t qmp_guest_fsfreeze_freeze(Error
**err
)
433 struct GuestFsfreezeMount
*mount
, *temp
;
437 slog("guest-fsfreeze called");
439 if (guest_fsfreeze_state
.status
== GUEST_FSFREEZE_STATUS_FROZEN
) {
443 ret
= guest_fsfreeze_build_mount_list();
448 /* cannot risk guest agent blocking itself on a write in this state */
451 QTAILQ_FOREACH_SAFE(mount
, &guest_fsfreeze_state
.mount_list
, next
, temp
) {
452 fd
= qemu_open(mount
->dirname
, O_RDONLY
);
454 sprintf(err_msg
, "failed to open %s, %s", mount
->dirname
, strerror(errno
));
455 error_set(err
, QERR_QGA_COMMAND_FAILED
, err_msg
);
459 /* we try to cull filesytems we know won't work in advance, but other
460 * filesytems may not implement fsfreeze for less obvious reasons.
461 * these will report EOPNOTSUPP, so we simply ignore them. when
462 * thawing, these filesystems will return an EINVAL instead, due to
463 * not being in a frozen state. Other filesystem-specific
464 * errors may result in EINVAL, however, so the user should check the
465 * number * of filesystems returned here against those returned by the
466 * thaw operation to determine whether everything completed
469 ret
= ioctl(fd
, FIFREEZE
);
470 if (ret
< 0 && errno
!= EOPNOTSUPP
) {
471 sprintf(err_msg
, "failed to freeze %s, %s", mount
->dirname
, strerror(errno
));
472 error_set(err
, QERR_QGA_COMMAND_FAILED
, err_msg
);
481 guest_fsfreeze_state
.status
= GUEST_FSFREEZE_STATUS_FROZEN
;
486 qmp_guest_fsfreeze_thaw(NULL
);
492 * Walk list of frozen file systems in the guest, and thaw them.
494 int64_t qmp_guest_fsfreeze_thaw(Error
**err
)
497 GuestFsfreezeMount
*mount
, *temp
;
499 bool has_error
= false;
501 QTAILQ_FOREACH_SAFE(mount
, &guest_fsfreeze_state
.mount_list
, next
, temp
) {
502 fd
= qemu_open(mount
->dirname
, O_RDONLY
);
507 ret
= ioctl(fd
, FITHAW
);
508 if (ret
< 0 && errno
!= EOPNOTSUPP
&& errno
!= EINVAL
) {
518 guest_fsfreeze_state
.status
= GUEST_FSFREEZE_STATUS_ERROR
;
520 guest_fsfreeze_state
.status
= GUEST_FSFREEZE_STATUS_THAWED
;
526 static void guest_fsfreeze_init(void)
528 guest_fsfreeze_state
.status
= GUEST_FSFREEZE_STATUS_THAWED
;
529 QTAILQ_INIT(&guest_fsfreeze_state
.mount_list
);
532 static void guest_fsfreeze_cleanup(void)
537 if (guest_fsfreeze_state
.status
== GUEST_FSFREEZE_STATUS_FROZEN
) {
538 ret
= qmp_guest_fsfreeze_thaw(&err
);
539 if (ret
< 0 || err
) {
540 slog("failed to clean up frozen filesystems");
546 * Return status of freeze/thaw
548 GuestFsfreezeStatus
qmp_guest_fsfreeze_status(Error
**err
)
550 error_set(err
, QERR_UNSUPPORTED
);
556 * Walk list of mounted file systems in the guest, and freeze the ones which
557 * are real local file systems.
559 int64_t qmp_guest_fsfreeze_freeze(Error
**err
)
561 error_set(err
, QERR_UNSUPPORTED
);
567 * Walk list of frozen file systems in the guest, and thaw them.
569 int64_t qmp_guest_fsfreeze_thaw(Error
**err
)
571 error_set(err
, QERR_UNSUPPORTED
);
577 /* register init/cleanup routines for stateful command groups */
578 void ga_command_state_init(GAState
*s
, GACommandState
*cs
)
581 #if defined(CONFIG_FSFREEZE)
582 ga_command_state_add(cs
, guest_fsfreeze_init
, guest_fsfreeze_cleanup
);
584 ga_command_state_add(cs
, guest_file_init
, NULL
);