[pve-devel] [PATCH v2 kvm] add -wait-for-cgroup option to deal with systemd-run race

Wolfgang Bumiller w.bumiller at proxmox.com
Wed Jun 1 12:19:42 CEST 2016


---
Changes since v2:
  removed leftovers from old patch
  fixed the option documentation (also a leftover from an patch)

 ...r-cgroup-option-to-deal-with-systemd-run-.patch | 145 +++++++++++++++++++++
 debian/patches/series                              |   1 +
 2 files changed, 146 insertions(+)
 create mode 100644 debian/patches/pve/0046-add-wait-for-cgroup-option-to-deal-with-systemd-run-.patch

diff --git a/debian/patches/pve/0046-add-wait-for-cgroup-option-to-deal-with-systemd-run-.patch b/debian/patches/pve/0046-add-wait-for-cgroup-option-to-deal-with-systemd-run-.patch
new file mode 100644
index 0000000..03a2f1c
--- /dev/null
+++ b/debian/patches/pve/0046-add-wait-for-cgroup-option-to-deal-with-systemd-run-.patch
@@ -0,0 +1,145 @@
+From b5eceb21c4f82187a4e2cb1fb5146b5b6ecdb66a Mon Sep 17 00:00:00 2001
+From: Wolfgang Bumiller <w.bumiller at proxmox.com>
+Date: Tue, 31 May 2016 15:58:39 +0200
+Subject: [PATCH] add -wait-for-cgroup option to deal with systemd-run race
+
+---
+ os-posix.c      | 92 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+ qemu-options.hx |  2 ++
+ 2 files changed, 94 insertions(+)
+
+diff --git a/os-posix.c b/os-posix.c
+index e4da406..5bc1cbc 100644
+--- a/os-posix.c
++++ b/os-posix.c
+@@ -49,6 +49,7 @@ static struct passwd *user_pwd;
+ static const char *chroot_dir;
+ static int daemonize;
+ static int daemon_pipe;
++static GPtrArray *wait_cgroups;
+ 
+ void os_setup_early_signal_handling(void)
+ {
+@@ -156,6 +157,11 @@ void os_parse_cmd_args(int index, const char *optarg)
+     case QEMU_OPTION_daemonize:
+         daemonize = 1;
+         break;
++    case QEMU_OPTION_wait_for_cgroup:
++        if (!wait_cgroups)
++            wait_cgroups = g_ptr_array_new();
++        g_ptr_array_add(wait_cgroups, g_strdup(optarg));
++        break;
+ #if defined(CONFIG_LINUX)
+     case QEMU_OPTION_enablefips:
+         fips_set_state(true);
+@@ -202,8 +208,94 @@ static void change_root(void)
+ 
+ }
+ 
++static bool wait_for_cgroup(char *cgroup)
++{
++    static bool unified = false;
++
++    bool found = false;
++    char *colon;
++    const char *type;
++    size_t type_len;
++    FILE *fh;
++    char *line = NULL;
++    size_t n = 0;
++    ssize_t linelen;
++
++    if (unified) {
++        fprintf(stderr, "Mixed cgroup v1 and v2 parameters\n");
++        exit(1);
++    }
++
++    colon = strchr(cgroup, ':');
++    if (!colon) {
++        unified = true;
++        type = "";
++    } else {
++        type = cgroup;
++        *colon = 0;
++        cgroup = colon+1;
++    }
++    type_len = strlen(type);
++
++    while (!found) {
++        fh = fopen("/proc/self/cgroup", "r");
++        if (!fh) {
++            perror("open(/proc/self/cgroup)");
++            exit(1);
++        }
++
++        while ((linelen = getline(&line, &n, fh)) != -1) {
++            line[linelen-1] = 0; /* cut newline */
++            if (strncmp(line, "0::", 3) == 0) {
++                /* This is a cgroup v2 file */
++                if (strcmp(line+3, cgroup) == 0) {
++                    found = true;
++                }
++                /* cgroup v2 only has one line */
++                break;
++            } else {
++                colon = strchr(line, ':');
++                if (!colon) {
++                    /* bad cgroup line */
++                    continue;
++                }
++                colon += 1;
++                if (strncmp(colon, type, type_len) != 0 || colon[type_len] != ':') {
++                    /* not the cgroup we're interested in */
++                    continue;
++                }
++                if (strcmp(colon + type_len + 1, cgroup) == 0) {
++                    found = true;
++                    break;
++                }
++            }
++        }
++
++        fclose(fh);
++        free(line);
++        line = NULL;
++        if (!found) {
++            usleep(50000);
++        }
++    }
++
++    return found;
++}
++
+ void os_daemonize(void)
+ {
++    if (wait_cgroups) {
++        char **cgroups, **i;
++
++        g_ptr_array_add(wait_cgroups, NULL);
++        cgroups = (char**)g_ptr_array_free(wait_cgroups, FALSE);
++
++        for (i = cgroups; *i; ++i) {
++            wait_for_cgroup(*i);
++        }
++
++        g_strfreev(cgroups);
++    }
+     if (daemonize) {
+         pid_t pid;
+         int fds[2];
+diff --git a/qemu-options.hx b/qemu-options.hx
+index a9ecd76..fc71ee3 100644
+--- a/qemu-options.hx
++++ b/qemu-options.hx
+@@ -3119,6 +3119,8 @@ ETEXI
+ #ifndef _WIN32
+ DEF("daemonize", 0, QEMU_OPTION_daemonize, \
+     "-daemonize      daemonize QEMU after initializing\n", QEMU_ARCH_ALL)
++DEF("wait-for-cgroup", HAS_ARG, QEMU_OPTION_wait_for_cgroup, \
++    "-wait-for-cgroup  wait for a specific cgroup setting before daemonizing\n", QEMU_ARCH_ALL)
+ #endif
+ STEXI
+ @item -daemonize
+-- 
+2.1.4
+
diff --git a/debian/patches/series b/debian/patches/series
index 10e5d46..5184986 100644
--- a/debian/patches/series
+++ b/debian/patches/series
@@ -43,6 +43,7 @@ pve/0042-vma-writer-don-t-bail-out-on-zero-length-files.patch
 pve/0043-vma-better-driver-guessing-for-bdrv_open.patch
 pve/0044-block-add-zeroinit.patch
 pve/0045-vma-add-format-option-to-device-mapping.patch
+pve/0046-add-wait-for-cgroup-option-to-deal-with-systemd-run-.patch
 extra/0001-vnc-clear-vs-tlscreds-after-unparenting-it.patch
 extra/CVE-2016-2198-ehci-null-pointer.patch
 extra/CVE-2016-2391-usb-ohci-avoid-multiple-eof-timers.patch
-- 
2.1.4




More information about the pve-devel mailing list