shutdown: Add Xen kexec support

In the Xen case, it's the hypervisor which manages kexec. We thus
have to ask it whether a kernel is loaded, instead of relying on
/sys/kernel/kexec_loaded.
This commit is contained in:
Samuel Thibault
2022-11-02 20:47:41 +01:00
committed by Luca Boccassi
parent 23f3a6f5ff
commit ede5a78f50
15 changed files with 115 additions and 12 deletions
-9
View File
@@ -23,15 +23,6 @@ int saved_argc = 0;
char **saved_argv = NULL;
static int saved_in_initrd = -1;
bool kexec_loaded(void) {
_cleanup_free_ char *s = NULL;
if (read_one_line_file("/sys/kernel/kexec_loaded", &s) < 0)
return false;
return s[0] == '1';
}
int prot_from_flags(int flags) {
switch (flags & O_ACCMODE) {
-2
View File
@@ -19,8 +19,6 @@ static inline void save_argc_argv(int argc, char **argv) {
saved_argv = argv;
}
bool kexec_loaded(void);
int prot_from_flags(int flags) _const_;
bool in_initrd(void);
+1
View File
@@ -24,6 +24,7 @@
#include "main-func.h"
#include "memory-util.h"
#include "process-util.h"
#include "reboot-util.h"
#include "special.h"
#define SERVER_FD_MAX 16
+2 -1
View File
@@ -465,7 +465,8 @@ libshared_deps = [threads,
libseccomp,
libselinux,
libzstd,
libxz]
libxz,
libxenctrl]
libshared_sym_path = '@0@/libshared.sym'.format(meson.current_source_dir())
+88
View File
@@ -1,11 +1,28 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include <errno.h>
#include <error.h>
#include <fcntl.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <unistd.h>
#if HAVE_XENCTRL
#define __XEN_INTERFACE_VERSION__ 0x00040900
#include <xen/xen.h>
#include <xen/kexec.h>
#include <xen/sys/privcmd.h>
#endif
#include "alloc-util.h"
#include "errno-util.h"
#include "fd-util.h"
#include "fileio.h"
#include "log.h"
#include "memory-util.h"
#include "proc-cmdline.h"
#include "raw-reboot.h"
#include "reboot-util.h"
@@ -107,3 +124,74 @@ int shall_restore_state(void) {
return r > 0 ? ret : true;
}
static int xen_kexec_loaded(void) {
#if HAVE_XENCTRL
size_t size;
_cleanup_close_ int privcmd_fd = -1, buf_fd = -1;
void *buffer;
int r;
if (access("/proc/xen", F_OK) < 0) {
if (errno == ENOENT)
return -EOPNOTSUPP;
return log_debug_errno(errno, "Unable to test whether /proc/xen exists: %m");
}
size = page_size();
if (sizeof(xen_kexec_status_t) > size)
return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "page_size is too small for hypercall");
privcmd_fd = open("/dev/xen/privcmd", O_RDWR|O_CLOEXEC);
if (privcmd_fd < 0)
return log_debug_errno(errno, "Cannot access /dev/xen/privcmd: %m");
buf_fd = open("/dev/xen/hypercall", O_RDWR|O_CLOEXEC);
if (buf_fd < 0)
return log_debug_errno(errno, "Cannot access /dev/xen/hypercall: %m");
buffer = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED, buf_fd, 0);
if (buffer == MAP_FAILED)
return log_debug_errno(errno, "Cannot allocate buffer for hypercall: %m");
*(xen_kexec_status_t*) buffer = (xen_kexec_status_t) {
.type = KEXEC_TYPE_DEFAULT,
};
privcmd_hypercall_t call = {
.op = __HYPERVISOR_kexec_op,
.arg = {
KEXEC_CMD_kexec_status,
PTR_TO_UINT64(buffer),
},
};
r = RET_NERRNO(ioctl(privcmd_fd, IOCTL_PRIVCMD_HYPERCALL, &call));
if (r < 0)
log_debug_errno(r, "kexec_status failed: %m");
munmap(buffer, size);
return r;
#else
return -EOPNOTSUPP;
#endif
}
bool kexec_loaded(void) {
_cleanup_free_ char *s = NULL;
int r;
r = xen_kexec_loaded();
if (r >= 0)
return r;
r = read_one_line_file("/sys/kernel/kexec_loaded", &s);
if (r < 0) {
if (r != -ENOENT)
log_debug_errno(r, "Unable to read /sys/kernel/kexec_loaded, ignoring: %m");
return false;
}
return s[0] == '1';
}
+2
View File
@@ -13,3 +13,5 @@ int read_reboot_parameter(char **parameter);
int reboot_with_parameter(RebootFlags flags);
int shall_restore_state(void);
bool kexec_loaded(void);
@@ -4,6 +4,7 @@
#include "alloc-util.h"
#include "pretty-print.h"
#include "reboot-util.h"
#include "systemctl-compat-shutdown.h"
#include "systemctl-sysv-compat.h"
#include "systemctl.h"
+1
View File
@@ -17,6 +17,7 @@
#include "path-util.h"
#include "pretty-print.h"
#include "process-util.h"
#include "reboot-util.h"
#include "rlimit-util.h"
#include "sigbus.h"
#include "signal-util.h"