2021-10-23 06:03:18 -04:00
|
|
|
|
Allow a QEMU host to set the time and shutdown Guix guests. Styled
|
|
|
|
|
after the patch from the Nix package:
|
|
|
|
|
|
|
|
|
|
https://github.com/NixOS/nixpkgs/blob/master/pkgs/applications/virtualization/qemu/fix-qemu-ga.patch
|
|
|
|
|
|
|
|
|
|
diff --git a/qga/commands-posix.c b/qga/commands-posix.c
|
|
|
|
|
--- a/qga/commands-posix.c
|
|
|
|
|
+++ b/qga/commands-posix.c
|
|
|
|
|
@@ -84,6 +84,7 @@ static void ga_wait_child(pid_t pid, int *status, Error **errp)
|
|
|
|
|
void qmp_guest_shutdown(bool has_mode, const char *mode, Error **errp)
|
|
|
|
|
{
|
|
|
|
|
const char *shutdown_flag;
|
|
|
|
|
+ const char *command;
|
|
|
|
|
Error *local_err = NULL;
|
|
|
|
|
pid_t pid;
|
|
|
|
|
int status;
|
2022-09-09 14:00:05 -04:00
|
|
|
|
@@ -101,10 +102,13 @@ void qmp_guest_shutdown(bool has_mode, const char *mode, Error **errp)
|
2021-10-23 06:03:18 -04:00
|
|
|
|
slog("guest-shutdown called, mode: %s", mode);
|
|
|
|
|
if (!has_mode || strcmp(mode, "powerdown") == 0) {
|
2022-09-09 14:00:05 -04:00
|
|
|
|
shutdown_flag = powerdown_flag;
|
2021-10-23 06:03:18 -04:00
|
|
|
|
+ command = "shutdown";
|
|
|
|
|
} else if (strcmp(mode, "halt") == 0) {
|
2022-09-09 14:00:05 -04:00
|
|
|
|
shutdown_flag = halt_flag;
|
2021-10-23 06:03:18 -04:00
|
|
|
|
+ command = "halt";
|
|
|
|
|
} else if (strcmp(mode, "reboot") == 0) {
|
2022-09-09 14:00:05 -04:00
|
|
|
|
shutdown_flag = reboot_flag;
|
2021-10-23 06:03:18 -04:00
|
|
|
|
+ command = "reboot";
|
|
|
|
|
} else {
|
|
|
|
|
error_setg(errp,
|
|
|
|
|
"mode is invalid (valid values are: halt|powerdown|reboot");
|
2022-09-09 14:00:05 -04:00
|
|
|
|
@@ -123,6 +127,11 @@ void qmp_guest_shutdown(bool has_mode, const char *mode, Error **errp)
|
|
|
|
|
execl("/sbin/shutdown", "shutdown", shutdown_flag, "-g0", "-y",
|
|
|
|
|
"hypervisor initiated shutdown", (char *)NULL);
|
|
|
|
|
#else
|
2021-10-23 06:03:18 -04:00
|
|
|
|
+ /* try Guix’s shutdown/halt/reboot first */
|
|
|
|
|
+ char *path = g_strdup_printf("/run/current-system/profile/sbin/%s", command);
|
2022-09-09 14:00:05 -04:00
|
|
|
|
+ execl(path, command, (char *)NULL);
|
2021-10-23 06:03:18 -04:00
|
|
|
|
+ g_free(path);
|
|
|
|
|
+
|
2022-09-09 14:00:05 -04:00
|
|
|
|
execl("/sbin/shutdown", "shutdown", "-h", shutdown_flag, "+0",
|
|
|
|
|
"hypervisor initiated shutdown", (char *)NULL);
|
|
|
|
|
#endif
|
|
|
|
|
@@ -159,10 +168,12 @@ void qmp_guest_set_time(bool has_time, int64_t time_ns, Error **errp)
|
2021-10-23 06:03:18 -04:00
|
|
|
|
Error *local_err = NULL;
|
|
|
|
|
struct timeval tv;
|
|
|
|
|
static const char hwclock_path[] = "/sbin/hwclock";
|
|
|
|
|
+ static const char hwclock_path_guix[] = "/run/current-system/profile/sbin/hwclock";
|
|
|
|
|
static int hwclock_available = -1;
|
|
|
|
|
|
|
|
|
|
if (hwclock_available < 0) {
|
|
|
|
|
- hwclock_available = (access(hwclock_path, X_OK) == 0);
|
|
|
|
|
+ hwclock_available = (access(hwclock_path_guix, X_OK) == 0) ||
|
|
|
|
|
+ (access(hwclock_path, X_OK) == 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!hwclock_available) {
|
2022-09-09 14:00:05 -04:00
|
|
|
|
@@ -208,6 +219,8 @@ void qmp_guest_set_time(bool has_time, int64_t time_ns, Error **errp)
|
2021-10-23 06:03:18 -04:00
|
|
|
|
|
|
|
|
|
/* Use '/sbin/hwclock -w' to set RTC from the system time,
|
|
|
|
|
* or '/sbin/hwclock -s' to set the system time from RTC. */
|
2022-09-09 14:00:05 -04:00
|
|
|
|
+ execl(hwclock_path_guix, "hwclock", has_time ? "-w" : "-s",
|
|
|
|
|
+ NULL);
|
|
|
|
|
execl(hwclock_path, "hwclock", has_time ? "-w" : "-s", NULL);
|
2021-10-23 06:03:18 -04:00
|
|
|
|
_exit(EXIT_FAILURE);
|
2022-09-09 14:00:05 -04:00
|
|
|
|
} else if (pid < 0) {
|