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
= qemu_mallocz(sizeof(GuestAgentInfo
));
61 info
->version
= g_strdup(QGA_VERSION
);
66 void qmp_guest_shutdown(bool has_mode
, const char *mode
, Error
**err
)
69 const char *shutdown_flag
;
71 slog("guest-shutdown called, mode: %s", mode
);
72 if (!has_mode
|| strcmp(mode
, "powerdown") == 0) {
74 } else if (strcmp(mode
, "halt") == 0) {
76 } else if (strcmp(mode
, "reboot") == 0) {
79 error_set(err
, QERR_INVALID_PARAMETER_VALUE
, "mode",
80 "halt|powerdown|reboot");
86 /* child, start the shutdown */
92 ret
= execl("/sbin/shutdown", "shutdown", shutdown_flag
, "+0",
93 "hypervisor initiated shutdown", (char*)NULL
);
95 slog("guest-shutdown failed: %s", strerror(errno
));
99 error_set(err
, QERR_UNDEFINED_ERROR
);
103 typedef struct GuestFileHandle
{
106 QTAILQ_ENTRY(GuestFileHandle
) next
;
110 QTAILQ_HEAD(, GuestFileHandle
) filehandles
;
113 static void guest_file_handle_add(FILE *fh
)
115 GuestFileHandle
*gfh
;
117 gfh
= qemu_mallocz(sizeof(GuestFileHandle
));
118 gfh
->id
= fileno(fh
);
120 QTAILQ_INSERT_TAIL(&guest_file_state
.filehandles
, gfh
, next
);
123 static GuestFileHandle
*guest_file_handle_find(int64_t id
)
125 GuestFileHandle
*gfh
;
127 QTAILQ_FOREACH(gfh
, &guest_file_state
.filehandles
, next
)
137 int64_t qmp_guest_file_open(const char *path
, bool has_mode
, const char *mode
, Error
**err
)
146 slog("guest-file-open called, filepath: %s, mode: %s", path
, mode
);
147 fh
= fopen(path
, mode
);
149 error_set(err
, QERR_OPEN_FILE_FAILED
, path
);
153 /* set fd non-blocking to avoid common use cases (like reading from a
154 * named pipe) from hanging the agent
157 ret
= fcntl(fd
, F_GETFL
);
158 ret
= fcntl(fd
, F_SETFL
, ret
| O_NONBLOCK
);
160 error_set(err
, QERR_QGA_COMMAND_FAILED
, "fcntl() failed");
165 guest_file_handle_add(fh
);
166 slog("guest-file-open, handle: %d", fd
);
170 void qmp_guest_file_close(int64_t handle
, Error
**err
)
172 GuestFileHandle
*gfh
= guest_file_handle_find(handle
);
175 slog("guest-file-close called, handle: %ld", handle
);
177 error_set(err
, QERR_FD_NOT_FOUND
, "handle");
181 ret
= fclose(gfh
->fh
);
183 error_set(err
, QERR_QGA_COMMAND_FAILED
, "fclose() failed");
187 QTAILQ_REMOVE(&guest_file_state
.filehandles
, gfh
, next
);
191 struct GuestFileRead
*qmp_guest_file_read(int64_t handle
, bool has_count
,
192 int64_t count
, Error
**err
)
194 GuestFileHandle
*gfh
= guest_file_handle_find(handle
);
195 GuestFileRead
*read_data
= NULL
;
201 error_set(err
, QERR_FD_NOT_FOUND
, "handle");
206 count
= QGA_READ_COUNT_DEFAULT
;
207 } else if (count
< 0) {
208 error_set(err
, QERR_INVALID_PARAMETER
, "count");
213 buf
= qemu_mallocz(count
+1);
214 read_count
= fread(buf
, 1, count
, fh
);
216 slog("guest-file-read failed, handle: %ld", handle
);
217 error_set(err
, QERR_QGA_COMMAND_FAILED
, "fread() failed");
220 read_data
= qemu_mallocz(sizeof(GuestFileRead
));
221 read_data
->count
= read_count
;
222 read_data
->eof
= feof(fh
);
224 read_data
->buf_b64
= g_base64_encode(buf
, read_count
);
233 GuestFileWrite
*qmp_guest_file_write(int64_t handle
, const char *buf_b64
,
234 bool has_count
, int64_t count
, Error
**err
)
236 GuestFileWrite
*write_data
= NULL
;
240 GuestFileHandle
*gfh
= guest_file_handle_find(handle
);
244 error_set(err
, QERR_FD_NOT_FOUND
, "handle");
249 buf
= g_base64_decode(buf_b64
, &buf_len
);
253 } else if (count
< 0 || count
> buf_len
) {
255 error_set(err
, QERR_INVALID_PARAMETER
, "count");
259 write_count
= fwrite(buf
, 1, count
, fh
);
261 slog("guest-file-write failed, handle: %ld", handle
);
262 error_set(err
, QERR_QGA_COMMAND_FAILED
, "fwrite() error");
264 write_data
= qemu_mallocz(sizeof(GuestFileWrite
));
265 write_data
->count
= write_count
;
266 write_data
->eof
= feof(fh
);
274 struct GuestFileSeek
*qmp_guest_file_seek(int64_t handle
, int64_t offset
,
275 int64_t whence
, Error
**err
)
277 GuestFileHandle
*gfh
= guest_file_handle_find(handle
);
278 GuestFileSeek
*seek_data
= NULL
;
283 error_set(err
, QERR_FD_NOT_FOUND
, "handle");
288 ret
= fseek(fh
, offset
, whence
);
290 error_set(err
, QERR_QGA_COMMAND_FAILED
, strerror(errno
));
292 seek_data
= qemu_mallocz(sizeof(GuestFileRead
));
293 seek_data
->position
= ftell(fh
);
294 seek_data
->eof
= feof(fh
);
301 void qmp_guest_file_flush(int64_t handle
, Error
**err
)
303 GuestFileHandle
*gfh
= guest_file_handle_find(handle
);
308 error_set(err
, QERR_FD_NOT_FOUND
, "handle");
315 error_set(err
, QERR_QGA_COMMAND_FAILED
, strerror(errno
));
319 static void guest_file_init(void)
321 QTAILQ_INIT(&guest_file_state
.filehandles
);
324 #if defined(CONFIG_FSFREEZE)
325 static void disable_logging(void)
327 ga_disable_logging(ga_state
);
330 static void enable_logging(void)
332 ga_enable_logging(ga_state
);
335 typedef struct GuestFsfreezeMount
{
338 QTAILQ_ENTRY(GuestFsfreezeMount
) next
;
339 } GuestFsfreezeMount
;
342 GuestFsfreezeStatus status
;
343 QTAILQ_HEAD(, GuestFsfreezeMount
) mount_list
;
344 } guest_fsfreeze_state
;
347 * Walk the mount table and build a list of local file systems
349 static int guest_fsfreeze_build_mount_list(void)
352 GuestFsfreezeMount
*mount
, *temp
;
353 char const *mtab
= MOUNTED
;
356 QTAILQ_FOREACH_SAFE(mount
, &guest_fsfreeze_state
.mount_list
, next
, temp
) {
357 QTAILQ_REMOVE(&guest_fsfreeze_state
.mount_list
, mount
, next
);
358 qemu_free(mount
->dirname
);
359 qemu_free(mount
->devtype
);
363 fp
= setmntent(mtab
, "r");
365 g_warning("fsfreeze: unable to read mtab");
369 while ((ment
= getmntent(fp
))) {
371 * An entry which device name doesn't start with a '/' is
372 * either a dummy file system or a network file system.
373 * Add special handling for smbfs and cifs as is done by
376 if ((ment
->mnt_fsname
[0] != '/') ||
377 (strcmp(ment
->mnt_type
, "smbfs") == 0) ||
378 (strcmp(ment
->mnt_type
, "cifs") == 0)) {
382 mount
= qemu_mallocz(sizeof(GuestFsfreezeMount
));
383 mount
->dirname
= qemu_strdup(ment
->mnt_dir
);
384 mount
->devtype
= qemu_strdup(ment
->mnt_type
);
386 QTAILQ_INSERT_TAIL(&guest_fsfreeze_state
.mount_list
, mount
, next
);
395 * Return status of freeze/thaw
397 GuestFsfreezeStatus
qmp_guest_fsfreeze_status(Error
**err
)
399 return guest_fsfreeze_state
.status
;
403 * Walk list of mounted file systems in the guest, and freeze the ones which
404 * are real local file systems.
406 int64_t qmp_guest_fsfreeze_freeze(Error
**err
)
409 struct GuestFsfreezeMount
*mount
, *temp
;
413 slog("guest-fsfreeze called");
415 if (guest_fsfreeze_state
.status
== GUEST_FSFREEZE_STATUS_FROZEN
) {
419 ret
= guest_fsfreeze_build_mount_list();
424 /* cannot risk guest agent blocking itself on a write in this state */
427 QTAILQ_FOREACH_SAFE(mount
, &guest_fsfreeze_state
.mount_list
, next
, temp
) {
428 fd
= qemu_open(mount
->dirname
, O_RDONLY
);
430 sprintf(err_msg
, "failed to open %s, %s", mount
->dirname
, strerror(errno
));
431 error_set(err
, QERR_QGA_COMMAND_FAILED
, err_msg
);
435 /* we try to cull filesytems we know won't work in advance, but other
436 * filesytems may not implement fsfreeze for less obvious reasons.
437 * these will report EOPNOTSUPP, so we simply ignore them. when
438 * thawing, these filesystems will return an EINVAL instead, due to
439 * not being in a frozen state. Other filesystem-specific
440 * errors may result in EINVAL, however, so the user should check the
441 * number * of filesystems returned here against those returned by the
442 * thaw operation to determine whether everything completed
445 ret
= ioctl(fd
, FIFREEZE
);
446 if (ret
< 0 && errno
!= EOPNOTSUPP
) {
447 sprintf(err_msg
, "failed to freeze %s, %s", mount
->dirname
, strerror(errno
));
448 error_set(err
, QERR_QGA_COMMAND_FAILED
, err_msg
);
457 guest_fsfreeze_state
.status
= GUEST_FSFREEZE_STATUS_FROZEN
;
462 qmp_guest_fsfreeze_thaw(NULL
);
468 * Walk list of frozen file systems in the guest, and thaw them.
470 int64_t qmp_guest_fsfreeze_thaw(Error
**err
)
473 GuestFsfreezeMount
*mount
, *temp
;
475 bool has_error
= false;
477 QTAILQ_FOREACH_SAFE(mount
, &guest_fsfreeze_state
.mount_list
, next
, temp
) {
478 fd
= qemu_open(mount
->dirname
, O_RDONLY
);
483 ret
= ioctl(fd
, FITHAW
);
484 if (ret
< 0 && errno
!= EOPNOTSUPP
&& errno
!= EINVAL
) {
494 guest_fsfreeze_state
.status
= GUEST_FSFREEZE_STATUS_ERROR
;
496 guest_fsfreeze_state
.status
= GUEST_FSFREEZE_STATUS_THAWED
;
502 static void guest_fsfreeze_init(void)
504 guest_fsfreeze_state
.status
= GUEST_FSFREEZE_STATUS_THAWED
;
505 QTAILQ_INIT(&guest_fsfreeze_state
.mount_list
);
508 static void guest_fsfreeze_cleanup(void)
513 if (guest_fsfreeze_state
.status
== GUEST_FSFREEZE_STATUS_FROZEN
) {
514 ret
= qmp_guest_fsfreeze_thaw(&err
);
515 if (ret
< 0 || err
) {
516 slog("failed to clean up frozen filesystems");
522 * Return status of freeze/thaw
524 GuestFsfreezeStatus
qmp_guest_fsfreeze_status(Error
**err
)
526 error_set(err
, QERR_UNSUPPORTED
);
532 * Walk list of mounted file systems in the guest, and freeze the ones which
533 * are real local file systems.
535 int64_t qmp_guest_fsfreeze_freeze(Error
**err
)
537 error_set(err
, QERR_UNSUPPORTED
);
543 * Walk list of frozen file systems in the guest, and thaw them.
545 int64_t qmp_guest_fsfreeze_thaw(Error
**err
)
547 error_set(err
, QERR_UNSUPPORTED
);
553 /* register init/cleanup routines for stateful command groups */
554 void ga_command_state_init(GAState
*s
, GACommandState
*cs
)
557 #if defined(CONFIG_FSFREEZE)
558 ga_command_state_add(cs
, guest_fsfreeze_init
, guest_fsfreeze_cleanup
);
560 ga_command_state_add(cs
, guest_file_init
, NULL
);