1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 
3 #include <errno.h>
4 
5 #include "btrfs-util.h"
6 #include "label.h"
7 #include "machine-pool.h"
8 #include "missing_magic.h"
9 #include "stat-util.h"
10 
check_btrfs(void)11 static int check_btrfs(void) {
12         struct statfs sfs;
13 
14         if (statfs("/var/lib/machines", &sfs) < 0) {
15                 if (errno != ENOENT)
16                         return -errno;
17 
18                 if (statfs("/var/lib", &sfs) < 0)
19                         return -errno;
20         }
21 
22         return F_TYPE_EQUAL(sfs.f_type, BTRFS_SUPER_MAGIC);
23 }
24 
setup_machine_directory(sd_bus_error * error)25 int setup_machine_directory(sd_bus_error *error) {
26         int r;
27 
28         r = check_btrfs();
29         if (r < 0)
30                 return sd_bus_error_set_errnof(error, r, "Failed to determine whether /var/lib/machines is located on btrfs: %m");
31         if (r == 0)
32                 return 0;
33 
34         (void) btrfs_subvol_make_label("/var/lib/machines");
35 
36         r = btrfs_quota_enable("/var/lib/machines", true);
37         if (r < 0)
38                 log_warning_errno(r, "Failed to enable quota for /var/lib/machines, ignoring: %m");
39 
40         r = btrfs_subvol_auto_qgroup("/var/lib/machines", 0, true);
41         if (r < 0)
42                 log_warning_errno(r, "Failed to set up default quota hierarchy for /var/lib/machines, ignoring: %m");
43 
44         return 1;
45 }
46