]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
machine: Fix hardcoded /var/lib/machines paths
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Tue, 2 Dec 2025 10:11:34 +0000 (11:11 +0100)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Mon, 8 Dec 2025 13:56:39 +0000 (14:56 +0100)
src/import/importd.c
src/machine/image-varlink.c
src/machine/machined-dbus.c
src/shared/discover-image.c
src/shared/discover-image.h
src/shared/machine-pool.c [deleted file]
src/shared/machine-pool.h [deleted file]
src/shared/meson.build

index 2ffdc3a4aa685fe42154bb8842b8f9f46651b6b4..39d761683c3dc024d6aa2f77c7728ff691fcaf11 100644 (file)
@@ -27,7 +27,6 @@
 #include "import-common.h"
 #include "import-util.h"
 #include "json-util.h"
-#include "machine-pool.h"
 #include "main-func.h"
 #include "notify-recv.h"
 #include "os-util.h"
@@ -820,9 +819,9 @@ static int method_import_tar_or_raw(sd_bus_message *msg, void *userdata, sd_bus_
                                          "Local image name %s is invalid", local);
 
         if (class == IMAGE_MACHINE) {
-                r = setup_machine_directory(error, m->use_btrfs_subvol, m->use_btrfs_quota);
+                r = image_setup_pool(m->runtime_scope, class, m->use_btrfs_subvol, m->use_btrfs_quota);
                 if (r < 0)
-                        return r;
+                        return sd_bus_error_set_errnof(error, r, "Failed to set up machine pool: %m");
         }
 
         type = startswith(sd_bus_message_get_member(msg), "ImportTar") ?
@@ -921,9 +920,9 @@ static int method_import_fs(sd_bus_message *msg, void *userdata, sd_bus_error *e
                                          "Local image name %s is invalid", local);
 
         if (class == IMAGE_MACHINE) {
-                r = setup_machine_directory(error, m->use_btrfs_subvol, m->use_btrfs_quota);
+                r = image_setup_pool(m->runtime_scope, class, m->use_btrfs_subvol, m->use_btrfs_quota);
                 if (r < 0)
-                        return r;
+                        return sd_bus_error_set_errnof(error, r, "Failed to set up machine pool: %m");
         }
 
         r = transfer_new(m, &t);
@@ -1126,9 +1125,9 @@ static int method_pull_tar_or_raw(sd_bus_message *msg, void *userdata, sd_bus_er
                                          "Unknown verification mode %s", verify);
 
         if (class == IMAGE_MACHINE) {
-                r = setup_machine_directory(error, m->use_btrfs_subvol, m->use_btrfs_quota);
+                r = image_setup_pool(m->runtime_scope, class, m->use_btrfs_subvol, m->use_btrfs_quota);
                 if (r < 0)
-                        return r;
+                        return sd_bus_error_set_errnof(error, r, "Failed to set up machine pool: %m");
         }
 
         type = startswith(sd_bus_message_get_member(msg), "PullTar") ?
index b2a3bd4c93193f3262e892381489decbb07148b4..141261eaaed31b3698e5c4168449867b1c48fe8a 100644 (file)
@@ -14,7 +14,6 @@
 #include "image-varlink.h"
 #include "io-util.h"
 #include "json-util.h"
-#include "machine-pool.h"
 #include "machined.h"
 #include "operation.h"
 #include "process-util.h"
@@ -280,7 +279,11 @@ int vl_method_set_pool_limit(sd_varlink *link, sd_json_variant *parameters, sd_v
         }
 
         /* Set up the machine directory if necessary */
-        r = setup_machine_directory(/* error = */ NULL, /* use_btrfs_subvol= */ true, /* use_btrfs_quota= */ true);
+        r = image_setup_pool(
+                        manager->runtime_scope,
+                        IMAGE_MACHINE,
+                        /* use_btrfs_subvol= */ true,
+                        /* use_btrfs_quota= */ true);
         if (r < 0)
                 return r;
 
index 25f72ae3cdb41d0ce1bfc6953e15e129eabaca44..398cd0e19153bc600867d773bce368565b2305d8 100644 (file)
@@ -6,9 +6,7 @@
 #include "sd-id128.h"
 
 #include "alloc-util.h"
-#include "btrfs-util.h"
 #include "bus-common-errors.h"
-#include "bus-get-properties.h"
 #include "bus-locator.h"
 #include "bus-message-util.h"
 #include "bus-object.h"
@@ -27,7 +25,6 @@
 #include "io-util.h"
 #include "machine.h"
 #include "machine-dbus.h"
-#include "machine-pool.h"
 #include "machined.h"
 #include "namespace-util.h"
 #include "operation.h"
 #include "unit-def.h"
 #include "user-util.h"
 
-static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_pool_path, "s", "/var/lib/machines");
+static int property_get_pool_path(
+                sd_bus *bus,
+                const char *path,
+                const char *interface,
+                const char *property,
+                sd_bus_message *reply,
+                void *userdata,
+                sd_bus_error *error) {
+
+        _cleanup_free_ char *poolpath = NULL;
+        Manager *m = ASSERT_PTR(userdata);
+
+        assert(bus);
+        assert(reply);
+
+        (void) image_get_pool_path(m->runtime_scope, IMAGE_MACHINE, &poolpath);
+
+        return sd_bus_message_append(reply, "s", strempty(poolpath));
+}
 
 static int property_get_pool_usage(
                 sd_bus *bus,
@@ -50,19 +65,13 @@ static int property_get_pool_usage(
                 void *userdata,
                 sd_bus_error *error) {
 
-        _cleanup_close_ int fd = -EBADF;
+        Manager *m = ASSERT_PTR(userdata);
         uint64_t usage = UINT64_MAX;
 
         assert(bus);
         assert(reply);
 
-        fd = open("/var/lib/machines", O_RDONLY|O_CLOEXEC|O_DIRECTORY);
-        if (fd >= 0) {
-                BtrfsQuotaInfo q;
-
-                if (btrfs_subvol_get_subtree_quota_fd(fd, 0, &q) >= 0)
-                        usage = q.referenced;
-        }
+        (void) image_get_pool_usage(m->runtime_scope, IMAGE_MACHINE, &usage);
 
         return sd_bus_message_append(reply, "t", usage);
 }
@@ -76,19 +85,13 @@ static int property_get_pool_limit(
                 void *userdata,
                 sd_bus_error *error) {
 
-        _cleanup_close_ int fd = -EBADF;
+        Manager *m = ASSERT_PTR(userdata);
         uint64_t size = UINT64_MAX;
 
         assert(bus);
         assert(reply);
 
-        fd = open("/var/lib/machines", O_RDONLY|O_CLOEXEC|O_DIRECTORY);
-        if (fd >= 0) {
-                BtrfsQuotaInfo q;
-
-                if (btrfs_subvol_get_subtree_quota_fd(fd, 0, &q) >= 0)
-                        size = q.referenced_max;
-        }
+        (void) image_get_pool_limit(m->runtime_scope, IMAGE_MACHINE, &size);
 
         return sd_bus_message_append(reply, "t", size);
 }
@@ -1077,9 +1080,13 @@ static int method_set_pool_limit(sd_bus_message *message, void *userdata, sd_bus
         }
 
         /* Set up the machine directory if necessary */
-        r = setup_machine_directory(error, /* use_btrfs_subvol= */ true, /* use_btrfs_quota= */ true);
+        r = image_setup_pool(
+                        m->runtime_scope,
+                        IMAGE_MACHINE,
+                        /* use_btrfs_subvol= */ true,
+                        /* use_btrfs_quota= */ true);
         if (r < 0)
-                return r;
+                return sd_bus_error_set_errnof(error, r, "Failed to set up machine pool: %m");
 
         r = image_set_pool_limit(m->runtime_scope, IMAGE_MACHINE, limit);
         if (ERRNO_IS_NEG_NOT_SUPPORTED(r))
index 3c952ba369624b8075dc183013b2a8fe469fba01..8c46251ebf0e290d28c337bb44b363c78ec6c131 100644 (file)
@@ -31,6 +31,7 @@
 #include "hashmap.h"
 #include "hostname-setup.h"
 #include "id128-util.h"
+#include "label-util.h"
 #include "lock-util.h"
 #include "log.h"
 #include "loop-util.h"
@@ -1919,6 +1920,119 @@ int image_set_pool_limit(RuntimeScope scope, ImageClass class, uint64_t referenc
         return 0;
 }
 
+int image_get_pool_path(RuntimeScope scope, ImageClass class, char **ret) {
+        assert(scope >= 0 && scope < _RUNTIME_SCOPE_MAX);
+        assert(class >= 0 && class < _IMAGE_CLASS_MAX);
+        assert(ret);
+
+        return get_pool_directory(scope, class, /* fname= */ NULL, /* suffix= */ NULL, ret);
+}
+
+int image_get_pool_usage(RuntimeScope scope, ImageClass class, uint64_t *ret) {
+        int r;
+
+        assert(scope >= 0 && scope < _RUNTIME_SCOPE_MAX);
+        assert(class >= 0 && class < _IMAGE_CLASS_MAX);
+        assert(ret);
+
+        _cleanup_free_ char *pool = NULL;
+        r = get_pool_directory(scope, class, /* fname= */ NULL, /* suffix= */ NULL, &pool);
+        if (r < 0)
+                return r;
+
+        _cleanup_close_ int fd = open(pool, O_RDONLY|O_CLOEXEC|O_DIRECTORY);
+        if (fd < 0)
+                return -errno;
+
+        BtrfsQuotaInfo q;
+        r = btrfs_subvol_get_subtree_quota_fd(fd, /* subvol_id= */ 0, &q);
+        if (r < 0)
+                return r;
+
+        *ret = q.referenced;
+        return 0;
+}
+
+int image_get_pool_limit(RuntimeScope scope, ImageClass class, uint64_t *ret) {
+        int r;
+
+        assert(scope >= 0 && scope < _RUNTIME_SCOPE_MAX);
+        assert(class >= 0 && class < _IMAGE_CLASS_MAX);
+        assert(ret);
+
+        _cleanup_free_ char *pool = NULL;
+        r = get_pool_directory(scope, class, /* fname= */ NULL, /* suffix= */ NULL, &pool);
+        if (r < 0)
+                return r;
+
+        _cleanup_close_ int fd = open(pool, O_RDONLY|O_CLOEXEC|O_DIRECTORY);
+        if (fd < 0)
+                return -errno;
+
+        BtrfsQuotaInfo q;
+        r = btrfs_subvol_get_subtree_quota_fd(fd, /* subvol_id= */ 0, &q);
+        if (r < 0)
+                return r;
+
+        *ret = q.referenced_max;
+        return 0;
+}
+
+static int check_btrfs(const char *path) {
+        struct statfs sfs;
+        int r;
+
+        if (statfs(path, &sfs) < 0) {
+                if (errno != ENOENT)
+                        return -errno;
+
+                _cleanup_free_ char *parent = NULL;
+                r = path_extract_directory(path, &parent);
+                if (r < 0)
+                        return r;
+
+                if (statfs(parent, &sfs) < 0)
+                        return -errno;
+        }
+
+        return F_TYPE_EQUAL(sfs.f_type, BTRFS_SUPER_MAGIC);
+}
+
+int image_setup_pool(RuntimeScope scope, ImageClass class, bool use_btrfs_subvol, bool use_btrfs_quota) {
+        int r;
+
+        assert(class >= 0 && class < _IMAGE_CLASS_MAX);
+
+        _cleanup_free_ char *pool = NULL;
+        r = image_get_pool_path(scope, class, &pool);
+        if (r < 0)
+                return r;
+
+        r = check_btrfs(pool);
+        if (r < 0)
+                return r;
+        if (r == 0)
+                return 0;
+
+        if (!use_btrfs_subvol)
+                return 0;
+
+        (void) btrfs_subvol_make_label(pool);
+
+        if (!use_btrfs_quota)
+                return 0;
+
+        r = btrfs_quota_enable(pool, /* b= */ true);
+        if (r < 0)
+                log_warning_errno(r, "Failed to enable quota for %s, ignoring: %m", pool);
+
+        r = btrfs_subvol_auto_qgroup(pool, /* subvol_id= */ 0, /* create_intermediary_qgroup= */ true);
+        if (r < 0)
+                log_warning_errno(r, "Failed to set up default quota hierarchy for %s, ignoring: %m", pool);
+
+        return 0;
+}
+
 int image_read_metadata(Image *i, const ImagePolicy *image_policy, RuntimeScope scope) {
         _cleanup_(release_lock_file) LockFile global_lock = LOCK_FILE_INIT, local_lock = LOCK_FILE_INIT;
         int r;
index 3d7291a8a0dc19abc3cb8dc2081d4e855de2afb8..881a59fdc2304e2b3c5cb2e1f49f6e9aae50cc1f 100644 (file)
@@ -69,6 +69,10 @@ int image_name_lock(RuntimeScope scope, const char *name, int operation, LockFil
 
 int image_set_limit(Image *i, uint64_t referenced_max);
 int image_set_pool_limit(RuntimeScope scope, ImageClass class, uint64_t referenced_max);
+int image_get_pool_path(RuntimeScope scope, ImageClass class, char **ret);
+int image_get_pool_usage(RuntimeScope scope, ImageClass class, uint64_t *ret);
+int image_get_pool_limit(RuntimeScope scope, ImageClass class, uint64_t *ret);
+int image_setup_pool(RuntimeScope scope, ImageClass class, bool use_btrfs_subvol, bool use_btrfs_quota);
 
 int image_read_metadata(Image *i, const ImagePolicy *image_policy, RuntimeScope scope);
 
diff --git a/src/shared/machine-pool.c b/src/shared/machine-pool.c
deleted file mode 100644 (file)
index 2fcb480..0000000
+++ /dev/null
@@ -1,53 +0,0 @@
-/* SPDX-License-Identifier: LGPL-2.1-or-later */
-
-#include <linux/magic.h>
-
-#include "sd-bus.h"
-
-#include "btrfs-util.h"
-#include "label-util.h"
-#include "log.h"
-#include "machine-pool.h"
-#include "stat-util.h"
-
-static int check_btrfs(void) {
-        struct statfs sfs;
-
-        if (statfs("/var/lib/machines", &sfs) < 0) {
-                if (errno != ENOENT)
-                        return -errno;
-
-                if (statfs("/var/lib", &sfs) < 0)
-                        return -errno;
-        }
-
-        return F_TYPE_EQUAL(sfs.f_type, BTRFS_SUPER_MAGIC);
-}
-
-int setup_machine_directory(sd_bus_error *error, bool use_btrfs_subvol, bool use_btrfs_quota) {
-        int r;
-
-        r = check_btrfs();
-        if (r < 0)
-                return sd_bus_error_set_errnof(error, r, "Failed to determine whether /var/lib/machines is located on btrfs: %m");
-        if (r == 0)
-                return 0;
-
-        if (!use_btrfs_subvol)
-                return 0;
-
-        (void) btrfs_subvol_make_label("/var/lib/machines");
-
-        if (!use_btrfs_quota)
-                return 0;
-
-        r = btrfs_quota_enable("/var/lib/machines", true);
-        if (r < 0)
-                log_warning_errno(r, "Failed to enable quota for /var/lib/machines, ignoring: %m");
-
-        r = btrfs_subvol_auto_qgroup("/var/lib/machines", 0, true);
-        if (r < 0)
-                log_warning_errno(r, "Failed to set up default quota hierarchy for /var/lib/machines, ignoring: %m");
-
-        return 0;
-}
diff --git a/src/shared/machine-pool.h b/src/shared/machine-pool.h
deleted file mode 100644 (file)
index feb3663..0000000
+++ /dev/null
@@ -1,6 +0,0 @@
-/* SPDX-License-Identifier: LGPL-2.1-or-later */
-#pragma once
-
-#include "shared-forward.h"
-
-int setup_machine_directory(sd_bus_error *error, bool use_btrfs_subvol, bool use_btrfs_quota);
index 671bcb2f4f7783ea7c7efba831aafe5b08a27ee9..e0b86e0b81ce1713b8131d22799afc96c3225192 100644 (file)
@@ -125,7 +125,6 @@ shared_sources = files(
         'machine-bind-user.c',
         'machine-credential.c',
         'machine-id-setup.c',
-        'machine-pool.c',
         'macvlan-util.c',
         'main-func.c',
         'mkdir-label.c',