Subject: [bug#73494] [PATCH v3 2/3] services: cleanup: Make /var/run a symlink of /run.

From: Hilton Chain <[email-masked]>
Date: Thu, 6 Mar 2025 14:22:43 +0800

* gnu/services.scm (cleanup-gexp): Make /var/run a symlink of /run.
* doc/guix.texi (File Systems): Document it.
* gnu/system/file-systems.scm (file-system-mapping->bind-mount): Replace
/var/run in source path with /run.
* gnu/services/dbus.scm (dbus-activation): Adjust accordingly.

Change-Id: I7b94d3e2fe1bef66f435e84bc77f32311dddd0ce
---
 doc/guix.texi               |  3 +++
 gnu/services.scm            |  5 +++--
 gnu/services/dbus.scm       | 31 -------------------------------
 gnu/system/file-systems.scm |  7 ++++++-
 4 files changed, 12 insertions(+), 34 deletions(-)

diff --git a/doc/guix.texi b/doc/guix.texi
index 70e5db4156..3cec887591 100644
--- a/doc/guix.texi
+++ b/doc/guix.texi
@@ -18231,6 +18231,9 @@ File Systems
 @defvar %runtime-variable-data
 This file system is mounted as @file{/run} and contains system
 information data describing the system since it was booted.
+
+Additionally, another directory with same role, @file{/var/run}, is
+implemented as a symlink to @file{/run}.
 @end defvar
 
 @defvar %binary-format-file-system
diff --git a/gnu/services.scm b/gnu/services.scm
index ea855ad193..7805cae971 100644
--- a/gnu/services.scm
+++ b/gnu/services.scm
@@ -668,8 +668,9 @@ (define (cleanup-gexp _)
            ;; hence the 'chmod' calls.
            (mkdir "/tmp" #o1777)
            (chmod "/tmp" #o1777)
-           (mkdir "/var/run" #o755)
-           (chmod "/var/run" #o755))))))
+           ;; /run is implemented as tmpfs mounted at early boot.
+           (mkdir-p "/var")
+           (symlink "/run" "/var/run"))))))
 
 (define cleanup-service-type
   ;; Service that cleans things up in /tmp and similar.
diff --git a/gnu/services/dbus.scm b/gnu/services/dbus.scm
index 76e04bf221..9292172e01 100644
--- a/gnu/services/dbus.scm
+++ b/gnu/services/dbus.scm
@@ -190,37 +190,6 @@ (define (dbus-activation config)
           ;; world-readable.
           (mkdir-p/perms "/run/dbus" user #o755))
 
-        (catch 'system-error
-          (lambda ()
-            (symlink "/run/dbus" "/var/run/dbus"))
-          (lambda args
-            (let ((errno (system-error-errno args)))
-              (cond
-               ((= errno EEXIST)
-                (let ((existing-name
-                       (false-if-exception
-                        (readlink "/var/run/dbus"))))
-                  (unless (equal? existing-name "/run/dbus")
-                    ;; Move the content of /var/run/dbus to /run/dbus, and
-                    ;; retry.
-                    (let ((dir (opendir "/var/run/dbus")))
-                      (let loop ((next (readdir dir)))
-                        (cond
-                         ((eof-object? next) (closedir dir))
-                         ((member next '("." "..")) (loop (readdir dir)))
-                         (else
-                          (begin
-                            (rename-file (string-append "/var/run/dbus/" next)
-                                         (string-append "/run/dbus/" next))
-                            (loop (readdir dir)))))))
-                    (rmdir "/var/run/dbus")
-                    (symlink "/run/dbus" "/var/run/dbus"))))
-               (else
-                (format (current-error-port)
-                        "Failed to symlink /run/dbus to /var/run/dbus: ~s~%"
-                        (strerror errno))
-                (error "cannot create /var/run/dbus"))))))
-
         (unless (file-exists? "/etc/machine-id")
           (format #t "creating /etc/machine-id...~%")
           (invoke (string-append #$(dbus-configuration-dbus config)
diff --git a/gnu/system/file-systems.scm b/gnu/system/file-systems.scm
index b2766707a3..697fcf4f82 100644
--- a/gnu/system/file-systems.scm
+++ b/gnu/system/file-systems.scm
@@ -594,7 +594,12 @@ (define (file-system-mapping->bind-mount mapping)
     (($ <file-system-mapping> source target writable?)
      (file-system
        (mount-point target)
-       (device source)
+       (device
+        ;; /var/run is implemented as a symlink to /run.
+        (if (and (string? source)
+                 (string-prefix? "/var/run" source))
+            (string-drop source 4)
+            source))
        (type "none")
        (flags (if writable?
                   '(bind-mount)
-- 
2.48.1