machine: Fix hardcoded /var/lib/machines paths

This commit is contained in:
Daan De Meyer
2025-12-08 14:56:39 +01:00
parent 154c642e7b
commit 1075316a4b
8 changed files with 158 additions and 91 deletions
+114
View 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;
+4
View 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);
-53
View File
@@ -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;
}
-6
View File
@@ -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);
-1
View 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',