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