summaryrefslogtreecommitdiffstats
path: root/src/shared/machine-pool.c
blob: fb0b2f5adcc714c379b6e1c8d34eadfc6045716b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/* SPDX-License-Identifier: LGPL-2.1-or-later */

#include <errno.h>

#include "btrfs-util.h"
#include "label.h"
#include "machine-pool.h"
#include "missing_magic.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;
}