]> xenbits.xensource.com Git - libvirt.git/commitdiff
qemu: Move channelTargetDir into stateDir
authorMichal Privoznik <mprivozn@redhat.com>
Thu, 20 Apr 2023 08:16:43 +0000 (10:16 +0200)
committerMichal Privoznik <mprivozn@redhat.com>
Thu, 17 Aug 2023 15:22:09 +0000 (17:22 +0200)
For historical reasons (i.e. unknown reason) we put channel
sockets into a path derived from cfg->libDir which is a path that
survives host reboots (e.g. /var/lib/libvirt/...). This is not
necessary and in fact for session daemon creates a longer prefix:

  XDG_CONFIG_HOME -> /home/user/.config
  XDG_RUNTIME_DIR -> /run/user/1000

Worse, if host is rebooted suddenly (e.g. due to power loss) then
we leave files behind and nobody will ever remove them.

Therefore, place the channel target dir into state dir.

Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=2173980
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Pavel Hrdina <phrdina@redhat.com>
libvirt.spec.in
src/qemu/qemu_conf.c
src/qemu/qemu_domain.c
tests/qemuhotplugtestdevices/qemuhotplug-qemu-agent-detach.xml
tests/qemuhotplugtestdomains/qemuhotplug-base-live+qemu-agent-detach.xml
tests/qemuhotplugtestdomains/qemuhotplug-base-live+qemu-agent.xml
tests/qemuxml2argvdata/channel-unix-source-path.xml
tests/qemuxml2xmloutdata/channel-unix-source-path-active.xml
tests/qemuxml2xmloutdata/channel-unix-source-path-inactive.xml
tests/testutilsqemu.c

index 3b179d1fd989b977f37ccb5450c43273213c141a..7157cfe3b4fcf795ba5a7326ccf5f8f56785cc48 100644 (file)
@@ -2135,7 +2135,6 @@ exit 0
 %ghost %dir %{_rundir}/libvirt/qemu/slirp/
 %ghost %dir %{_rundir}/libvirt/qemu/swtpm/
 %dir %attr(0751, %{qemu_user}, %{qemu_group}) %{_localstatedir}/lib/libvirt/qemu/
-%dir %attr(0751, %{qemu_user}, %{qemu_group}) %{_localstatedir}/lib/libvirt/qemu/channel/
 %dir %attr(0751, %{qemu_user}, %{qemu_group}) %{_localstatedir}/lib/libvirt/qemu/checkpoint/
 %dir %attr(0751, %{qemu_user}, %{qemu_group}) %{_localstatedir}/lib/libvirt/qemu/dump/
 %dir %attr(0751, %{qemu_user}, %{qemu_group}) %{_localstatedir}/lib/libvirt/qemu/nvram/
index 532fe36be36ebb0d1674a714681ee44b7df2f717..3f811d064fea76130fdf12bdcd57409f807fb3d3 100644 (file)
@@ -143,6 +143,7 @@ virQEMUDriverConfig *virQEMUDriverConfigNew(bool privileged,
         cfg->configBaseDir = g_strdup_printf("%s/etc", root);
         cfg->stateDir = g_strdup_printf("%s/run/qemu", root);
         cfg->swtpmStateDir = g_strdup_printf("%s/run/swtpm", root);
+        cfg->channelTargetDir = g_strdup_printf("%s/channel", cfg->stateDir);
         cfg->cacheDir = g_strdup_printf("%s/cache/qemu", root);
         cfg->libDir = g_strdup_printf("%s/lib/qemu", root);
         cfg->swtpmStorageDir = g_strdup_printf("%s/lib/swtpm", root);
@@ -151,7 +152,6 @@ virQEMUDriverConfig *virQEMUDriverConfigNew(bool privileged,
         cfg->snapshotDir = g_strdup_printf("%s/snapshot", cfg->libDir);
         cfg->checkpointDir = g_strdup_printf("%s/checkpoint", cfg->libDir);
         cfg->autoDumpPath = g_strdup_printf("%s/dump", cfg->libDir);
-        cfg->channelTargetDir = g_strdup_printf("%s/channel", cfg->libDir);
         cfg->nvramDir = g_strdup_printf("%s/nvram", cfg->libDir);
         cfg->memoryBackingDir = g_strdup_printf("%s/ram", cfg->libDir);
     } else if (privileged) {
@@ -163,8 +163,8 @@ virQEMUDriverConfig *virQEMUDriverConfigNew(bool privileged,
         cfg->configBaseDir = g_strdup(SYSCONFDIR "/libvirt");
 
         cfg->stateDir = g_strdup_printf("%s/libvirt/qemu", RUNSTATEDIR);
-
         cfg->swtpmStateDir = g_strdup_printf("%s/libvirt/qemu/swtpm", RUNSTATEDIR);
+        cfg->channelTargetDir = g_strdup_printf("%s/channel", cfg->stateDir);
 
         cfg->cacheDir = g_strdup_printf("%s/cache/libvirt/qemu", LOCALSTATEDIR);
 
@@ -173,7 +173,6 @@ virQEMUDriverConfig *virQEMUDriverConfigNew(bool privileged,
         cfg->snapshotDir = g_strdup_printf("%s/snapshot", cfg->libDir);
         cfg->checkpointDir = g_strdup_printf("%s/checkpoint", cfg->libDir);
         cfg->autoDumpPath = g_strdup_printf("%s/dump", cfg->libDir);
-        cfg->channelTargetDir = g_strdup_printf("%s/channel", cfg->libDir);
         cfg->nvramDir = g_strdup_printf("%s/nvram", cfg->libDir);
         cfg->memoryBackingDir = g_strdup_printf("%s/ram", cfg->libDir);
         cfg->swtpmStorageDir = g_strdup_printf("%s/lib/libvirt/swtpm",
@@ -190,8 +189,8 @@ virQEMUDriverConfig *virQEMUDriverConfigNew(bool privileged,
 
         rundir = virGetUserRuntimeDirectory();
         cfg->stateDir = g_strdup_printf("%s/qemu/run", rundir);
-
         cfg->swtpmStateDir = g_strdup_printf("%s/swtpm", cfg->stateDir);
+        cfg->channelTargetDir = g_strdup_printf("%s/channel", cfg->stateDir);
 
         cfg->configBaseDir = virGetUserConfigDirectory();
 
@@ -201,8 +200,6 @@ virQEMUDriverConfig *virQEMUDriverConfigNew(bool privileged,
         cfg->checkpointDir = g_strdup_printf("%s/qemu/checkpoint",
                                              cfg->configBaseDir);
         cfg->autoDumpPath = g_strdup_printf("%s/qemu/dump", cfg->configBaseDir);
-        cfg->channelTargetDir = g_strdup_printf("%s/qemu/channel",
-                                                cfg->configBaseDir);
         cfg->nvramDir = g_strdup_printf("%s/qemu/nvram", cfg->configBaseDir);
         cfg->memoryBackingDir = g_strdup_printf("%s/qemu/ram", cfg->configBaseDir);
         cfg->swtpmStorageDir = g_strdup_printf("%s/qemu/swtpm",
index 224350dabc32c602ed8e9a9b19d9ef558f140da1..d46516856ebc8290c13e7f5cc9e9bf3a42844483 100644 (file)
@@ -5401,6 +5401,28 @@ qemuDomainDefaultNetModel(const virDomainDef *def,
 }
 
 
+
+static bool
+qemuDomainChrMatchDefaultPath(const char *prefix,
+                              const char *infix,
+                              const char *target,
+                              const char *path)
+{
+    g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER;
+    g_autofree char *regexp = NULL;
+
+    virBufferEscapeRegex(&buf, "^%s", prefix);
+    if (infix)
+        virBufferEscapeRegex(&buf, "%s", infix);
+    virBufferAddLit(&buf, "/(target/)?([^/]+\\.)|(domain-[^/]+/)|([0-9]+-[^/]+/)");
+    virBufferEscapeRegex(&buf, "%s$", target);
+
+    regexp = virBufferContentAndReset(&buf);
+
+    return virStringMatch(path, regexp);
+}
+
+
 /*
  * Clear auto generated unix socket paths:
  *
@@ -5421,6 +5443,9 @@ qemuDomainDefaultNetModel(const virDomainDef *def,
  *
  * This function clears the path for migration as well, so we need to clear
  * the path even if we are not storing it in the XML.
+ *
+ * Please note, as of libvirt 9.7.0 the channelTargetDir is no longer derived
+ * from cfg->libDir but rather cfg->stateDir.
  */
 static void
 qemuDomainChrDefDropDefaultPath(virDomainChrDef *chr,
@@ -5439,14 +5464,31 @@ qemuDomainChrDefDropDefaultPath(virDomainChrDef *chr,
 
     cfg = virQEMUDriverGetConfig(driver);
 
-    virBufferEscapeRegex(&buf, "^%s", cfg->channelTargetDir);
-    virBufferAddLit(&buf, "/(target/)?([^/]+\\.)|(domain-[^/]+/)|([0-9]+-[^/]+/)");
-    virBufferEscapeRegex(&buf, "%s$", chr->target.name);
+    if (qemuDomainChrMatchDefaultPath(cfg->channelTargetDir,
+                                      NULL,
+                                      chr->target.name,
+                                      chr->source->data.nix.path)) {
+        VIR_FREE(chr->source->data.nix.path);
+        return;
+    }
 
-    regexp = virBufferContentAndReset(&buf);
+    /* Previously, channelTargetDir was derived from cfg->libdir, or
+     * cfg->configBaseDir even. Try them too. */
+    if (qemuDomainChrMatchDefaultPath(cfg->libDir,
+                                      "/channel",
+                                      chr->target.name,
+                                      chr->source->data.nix.path)) {
+        VIR_FREE(chr->source->data.nix.path);
+        return;
+    }
 
-    if (virStringMatch(chr->source->data.nix.path, regexp))
+    if (qemuDomainChrMatchDefaultPath(cfg->configBaseDir,
+                                      "/qemu/channel",
+                                      chr->target.name,
+                                      chr->source->data.nix.path)) {
         VIR_FREE(chr->source->data.nix.path);
+        return;
+    }
 }
 
 
index 7871de59c4044eb84b5011b31f01f97affebbd9e..dd4feae855fe464130ecf60340a9a121542c7878 100644 (file)
@@ -1,5 +1,5 @@
     <channel type='unix'>
-      <source mode='bind' path='/var/lib/libvirt/qemu/channel/7-hotplug/org.qemu.guest_agent.0'/>
+      <source mode='bind' path='/var/run/libvirt/qemu/channel/7-hotplug/org.qemu.guest_agent.0'/>
       <target type='virtio' name='org.qemu.guest_agent.0'/>
       <address type='virtio-serial' controller='0' bus='0' port='1'/>
     </channel>
index bf2afb67d9c8238063e26780bf27f5711296d57a..cfa75094852a1e42e23049f3e18d16ffa3b1943f 100644 (file)
@@ -39,7 +39,7 @@
       <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>
     </controller>
     <channel type='unix'>
-      <source mode='bind' path='/var/lib/libvirt/qemu/channel/7-hotplug/org.qemu.guest_agent.0'/>
+      <source mode='bind' path='/var/run/libvirt/qemu/channel/7-hotplug/org.qemu.guest_agent.0'/>
       <target type='virtio' name='org.qemu.guest_agent.0'/>
       <alias name='channel0'/>
       <address type='virtio-serial' controller='0' bus='0' port='1'/>
index 00191a9cb8ba4e862454dbfba5d831333271339b..eb543dbaf40707eeb06b89421c2c7c5eee543a2e 100644 (file)
@@ -42,7 +42,7 @@
       <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>
     </controller>
     <channel type='unix'>
-      <source mode='bind' path='/var/lib/libvirt/qemu/channel/7-hotplug/org.qemu.guest_agent.0'/>
+      <source mode='bind' path='/var/run/libvirt/qemu/channel/7-hotplug/org.qemu.guest_agent.0'/>
       <target type='virtio' name='org.qemu.guest_agent.0'/>
       <alias name='channel0'/>
       <address type='virtio-serial' controller='0' bus='0' port='1'/>
index db0e99c3d12f5d3d738c03afca8051166bee1051..df548d88e72778cc471a8dc6925abbb37ed6547b 100644 (file)
       <source mode='bind' path='/var/lib/libvirt/qemu/channel/1-QEMUGuest1/org.qemu.guest_agent.4'/>
       <target type='virtio' name='org.qemu.guest_agent.4'/>
     </channel>
+    <channel type='unix'>
+      <source mode='bind' path='/var/run/libvirt/qemu/channel/1-QEMUGuest1/org.qemu.guest_agent.5'/>
+      <target type='virtio' name='org.qemu.guest_agent.5'/>
+    </channel>
     <memballoon model='none'/>
   </devices>
 </domain>
index 68835ceb151c4f9c11c781727e6d02534bd6ddd5..ec927d37c68d9eaf3adb4814f4f6ad0bfa7fc918 100644 (file)
       <target type='virtio' name='org.qemu.guest_agent.4'/>
       <address type='virtio-serial' controller='0' bus='0' port='5'/>
     </channel>
+    <channel type='unix'>
+      <source mode='bind' path='/var/run/libvirt/qemu/channel/1-QEMUGuest1/org.qemu.guest_agent.5'/>
+      <target type='virtio' name='org.qemu.guest_agent.5'/>
+      <address type='virtio-serial' controller='0' bus='0' port='6'/>
+    </channel>
     <input type='mouse' bus='ps2'/>
     <input type='keyboard' bus='ps2'/>
     <audio id='1' type='none'/>
index 738c1184c0ed511df51ea92d95b4e4a7a0612819..c979bedb3929ee7e287c8eaf13a4e5bb10727562 100644 (file)
       <target type='virtio' name='org.qemu.guest_agent.4'/>
       <address type='virtio-serial' controller='0' bus='0' port='5'/>
     </channel>
+    <channel type='unix'>
+      <target type='virtio' name='org.qemu.guest_agent.5'/>
+      <address type='virtio-serial' controller='0' bus='0' port='6'/>
+    </channel>
     <input type='mouse' bus='ps2'/>
     <input type='keyboard' bus='ps2'/>
     <audio id='1' type='none'/>
index 2d3e04de73840c52e01eeb1f79d724764e1e8291..db911d7c90fd98b67a2508b07ff4e9caaa0add6e 100644 (file)
@@ -460,7 +460,7 @@ int qemuTestDriverInit(virQEMUDriver *driver)
     VIR_FREE(cfg->libDir);
     cfg->libDir = g_strdup("/var/lib/libvirt/qemu");
     VIR_FREE(cfg->channelTargetDir);
-    cfg->channelTargetDir = g_strdup("/var/lib/libvirt/qemu/channel");
+    cfg->channelTargetDir = g_strdup("/var/run/libvirt/qemu/channel");
     VIR_FREE(cfg->memoryBackingDir);
     cfg->memoryBackingDir = g_strdup("/var/lib/libvirt/qemu/ram");
     VIR_FREE(cfg->nvramDir);