]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-btrfs.c
Merge pull request #25132 from yuwata/core-device-inactivate-removed-device-on-switch...
[thirdparty/systemd.git] / src / test / test-btrfs.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <fcntl.h>
4
5 #include "btrfs-util.h"
6 #include "fd-util.h"
7 #include "fileio.h"
8 #include "format-util.h"
9 #include "log.h"
10 #include "string-util.h"
11
12 int main(int argc, char *argv[]) {
13 BtrfsQuotaInfo quota;
14 int r, fd;
15
16 fd = open("/", O_RDONLY|O_CLOEXEC|O_DIRECTORY);
17 if (fd < 0)
18 log_error_errno(errno, "Failed to open root directory: %m");
19 else {
20 BtrfsSubvolInfo info;
21
22 r = btrfs_subvol_get_info_fd(fd, 0, &info);
23 if (r < 0)
24 log_error_errno(r, "Failed to get subvolume info: %m");
25 else {
26 log_info("otime: %s", FORMAT_TIMESTAMP(info.otime));
27 log_info("read-only (search): %s", yes_no(info.read_only));
28 }
29
30 r = btrfs_qgroup_get_quota_fd(fd, 0, &quota);
31 if (r < 0)
32 log_error_errno(r, "Failed to get quota info: %m");
33 else {
34 log_info("referenced: %s", strna(FORMAT_BYTES(quota.referenced)));
35 log_info("exclusive: %s", strna(FORMAT_BYTES(quota.exclusive)));
36 log_info("referenced_max: %s", strna(FORMAT_BYTES(quota.referenced_max)));
37 log_info("exclusive_max: %s", strna(FORMAT_BYTES(quota.exclusive_max)));
38 }
39
40 r = btrfs_subvol_get_read_only_fd(fd);
41 if (r < 0)
42 log_error_errno(r, "Failed to get read only flag: %m");
43 else
44 log_info("read-only (ioctl): %s", yes_no(r));
45
46 safe_close(fd);
47 }
48
49 r = btrfs_subvol_make("/xxxtest");
50 if (r < 0)
51 log_error_errno(r, "Failed to make subvolume: %m");
52
53 r = write_string_file("/xxxtest/afile", "ljsadhfljasdkfhlkjdsfha", WRITE_STRING_FILE_CREATE);
54 if (r < 0)
55 log_error_errno(r, "Failed to write file: %m");
56
57 r = btrfs_subvol_snapshot("/xxxtest", "/xxxtest2", 0);
58 if (r < 0)
59 log_error_errno(r, "Failed to make snapshot: %m");
60
61 r = btrfs_subvol_snapshot("/xxxtest", "/xxxtest3", BTRFS_SNAPSHOT_READ_ONLY);
62 if (r < 0)
63 log_error_errno(r, "Failed to make snapshot: %m");
64
65 r = btrfs_subvol_remove("/xxxtest", BTRFS_REMOVE_QUOTA);
66 if (r < 0)
67 log_error_errno(r, "Failed to remove subvolume: %m");
68
69 r = btrfs_subvol_remove("/xxxtest2", BTRFS_REMOVE_QUOTA);
70 if (r < 0)
71 log_error_errno(r, "Failed to remove subvolume: %m");
72
73 r = btrfs_subvol_remove("/xxxtest3", BTRFS_REMOVE_QUOTA);
74 if (r < 0)
75 log_error_errno(r, "Failed to remove subvolume: %m");
76
77 r = btrfs_subvol_snapshot("/etc", "/etc2", BTRFS_SNAPSHOT_READ_ONLY|BTRFS_SNAPSHOT_FALLBACK_COPY);
78 if (r < 0)
79 log_error_errno(r, "Failed to make snapshot: %m");
80
81 r = btrfs_subvol_remove("/etc2", BTRFS_REMOVE_QUOTA);
82 if (r < 0)
83 log_error_errno(r, "Failed to remove subvolume: %m");
84
85 r = btrfs_subvol_make("/xxxrectest");
86 if (r < 0)
87 log_error_errno(r, "Failed to make subvolume: %m");
88
89 r = btrfs_subvol_make("/xxxrectest/xxxrectest2");
90 if (r < 0)
91 log_error_errno(r, "Failed to make subvolume: %m");
92
93 r = btrfs_subvol_make("/xxxrectest/xxxrectest3");
94 if (r < 0)
95 log_error_errno(r, "Failed to make subvolume: %m");
96
97 r = btrfs_subvol_make("/xxxrectest/xxxrectest3/sub");
98 if (r < 0)
99 log_error_errno(r, "Failed to make subvolume: %m");
100
101 if (mkdir("/xxxrectest/dir", 0755) < 0)
102 log_error_errno(errno, "Failed to make directory: %m");
103
104 r = btrfs_subvol_make("/xxxrectest/dir/xxxrectest4");
105 if (r < 0)
106 log_error_errno(r, "Failed to make subvolume: %m");
107
108 if (mkdir("/xxxrectest/dir/xxxrectest4/dir", 0755) < 0)
109 log_error_errno(errno, "Failed to make directory: %m");
110
111 r = btrfs_subvol_make("/xxxrectest/dir/xxxrectest4/dir/xxxrectest5");
112 if (r < 0)
113 log_error_errno(r, "Failed to make subvolume: %m");
114
115 if (mkdir("/xxxrectest/mnt", 0755) < 0)
116 log_error_errno(errno, "Failed to make directory: %m");
117
118 r = btrfs_subvol_snapshot("/xxxrectest", "/xxxrectest2", BTRFS_SNAPSHOT_RECURSIVE);
119 if (r < 0)
120 log_error_errno(r, "Failed to snapshot subvolume: %m");
121
122 r = btrfs_subvol_remove("/xxxrectest", BTRFS_REMOVE_QUOTA|BTRFS_REMOVE_RECURSIVE);
123 if (r < 0)
124 log_error_errno(r, "Failed to recursively remove subvolume: %m");
125
126 r = btrfs_subvol_remove("/xxxrectest2", BTRFS_REMOVE_QUOTA|BTRFS_REMOVE_RECURSIVE);
127 if (r < 0)
128 log_error_errno(r, "Failed to recursively remove subvolume: %m");
129
130 r = btrfs_subvol_make("/xxxquotatest");
131 if (r < 0)
132 log_error_errno(r, "Failed to make subvolume: %m");
133
134 r = btrfs_subvol_auto_qgroup("/xxxquotatest", 0, true);
135 if (r < 0)
136 log_error_errno(r, "Failed to set up auto qgroup: %m");
137
138 r = btrfs_subvol_make("/xxxquotatest/beneath");
139 if (r < 0)
140 log_error_errno(r, "Failed to make subvolume: %m");
141
142 r = btrfs_subvol_auto_qgroup("/xxxquotatest/beneath", 0, false);
143 if (r < 0)
144 log_error_errno(r, "Failed to set up auto qgroup: %m");
145
146 r = btrfs_qgroup_set_limit("/xxxquotatest/beneath", 0, 4ULL * 1024 * 1024 * 1024);
147 if (r < 0)
148 log_error_errno(r, "Failed to set up quota limit: %m");
149
150 r = btrfs_subvol_set_subtree_quota_limit("/xxxquotatest", 0, 5ULL * 1024 * 1024 * 1024);
151 if (r < 0)
152 log_error_errno(r, "Failed to set up quota limit: %m");
153
154 r = btrfs_subvol_snapshot("/xxxquotatest", "/xxxquotatest2", BTRFS_SNAPSHOT_RECURSIVE|BTRFS_SNAPSHOT_QUOTA);
155 if (r < 0)
156 log_error_errno(r, "Failed to set up snapshot: %m");
157
158 r = btrfs_qgroup_get_quota("/xxxquotatest2/beneath", 0, &quota);
159 if (r < 0)
160 log_error_errno(r, "Failed to query quota: %m");
161
162 assert_se(quota.referenced_max == 4ULL * 1024 * 1024 * 1024);
163
164 r = btrfs_subvol_get_subtree_quota("/xxxquotatest2", 0, &quota);
165 if (r < 0)
166 log_error_errno(r, "Failed to query quota: %m");
167
168 assert_se(quota.referenced_max == 5ULL * 1024 * 1024 * 1024);
169
170 r = btrfs_subvol_remove("/xxxquotatest", BTRFS_REMOVE_QUOTA|BTRFS_REMOVE_RECURSIVE);
171 if (r < 0)
172 log_error_errno(r, "Failed remove subvolume: %m");
173
174 r = btrfs_subvol_remove("/xxxquotatest2", BTRFS_REMOVE_QUOTA|BTRFS_REMOVE_RECURSIVE);
175 if (r < 0)
176 log_error_errno(r, "Failed remove subvolume: %m");
177
178 return 0;
179 }