]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-btrfs.c
Merge pull request #27846 from keszybz/link-mode-generation
[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_at(AT_FDCWD, "/xxxtest", AT_FDCWD, "/xxxtest2", 0);
58 if (r < 0)
59 log_error_errno(r, "Failed to make snapshot: %m");
60
61 r = btrfs_subvol_snapshot_at(AT_FDCWD, "/xxxtest", AT_FDCWD, "/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_at(AT_FDCWD, "/etc", AT_FDCWD, "/etc2",
78 BTRFS_SNAPSHOT_READ_ONLY|BTRFS_SNAPSHOT_FALLBACK_COPY);
79 if (r < 0)
80 log_error_errno(r, "Failed to make snapshot: %m");
81
82 r = btrfs_subvol_remove("/etc2", BTRFS_REMOVE_QUOTA);
83 if (r < 0)
84 log_error_errno(r, "Failed to remove subvolume: %m");
85
86 r = btrfs_subvol_make("/xxxrectest");
87 if (r < 0)
88 log_error_errno(r, "Failed to make subvolume: %m");
89
90 r = btrfs_subvol_make("/xxxrectest/xxxrectest2");
91 if (r < 0)
92 log_error_errno(r, "Failed to make subvolume: %m");
93
94 r = btrfs_subvol_make("/xxxrectest/xxxrectest3");
95 if (r < 0)
96 log_error_errno(r, "Failed to make subvolume: %m");
97
98 r = btrfs_subvol_make("/xxxrectest/xxxrectest3/sub");
99 if (r < 0)
100 log_error_errno(r, "Failed to make subvolume: %m");
101
102 if (mkdir("/xxxrectest/dir", 0755) < 0)
103 log_error_errno(errno, "Failed to make directory: %m");
104
105 r = btrfs_subvol_make("/xxxrectest/dir/xxxrectest4");
106 if (r < 0)
107 log_error_errno(r, "Failed to make subvolume: %m");
108
109 if (mkdir("/xxxrectest/dir/xxxrectest4/dir", 0755) < 0)
110 log_error_errno(errno, "Failed to make directory: %m");
111
112 r = btrfs_subvol_make("/xxxrectest/dir/xxxrectest4/dir/xxxrectest5");
113 if (r < 0)
114 log_error_errno(r, "Failed to make subvolume: %m");
115
116 if (mkdir("/xxxrectest/mnt", 0755) < 0)
117 log_error_errno(errno, "Failed to make directory: %m");
118
119 r = btrfs_subvol_snapshot_at(AT_FDCWD, "/xxxrectest", AT_FDCWD, "/xxxrectest2", BTRFS_SNAPSHOT_RECURSIVE);
120 if (r < 0)
121 log_error_errno(r, "Failed to snapshot subvolume: %m");
122
123 r = btrfs_subvol_remove("/xxxrectest", BTRFS_REMOVE_QUOTA|BTRFS_REMOVE_RECURSIVE);
124 if (r < 0)
125 log_error_errno(r, "Failed to recursively remove subvolume: %m");
126
127 r = btrfs_subvol_remove("/xxxrectest2", BTRFS_REMOVE_QUOTA|BTRFS_REMOVE_RECURSIVE);
128 if (r < 0)
129 log_error_errno(r, "Failed to recursively remove subvolume: %m");
130
131 r = btrfs_subvol_make("/xxxquotatest");
132 if (r < 0)
133 log_error_errno(r, "Failed to make subvolume: %m");
134
135 r = btrfs_subvol_auto_qgroup("/xxxquotatest", 0, true);
136 if (r < 0)
137 log_error_errno(r, "Failed to set up auto qgroup: %m");
138
139 r = btrfs_subvol_make("/xxxquotatest/beneath");
140 if (r < 0)
141 log_error_errno(r, "Failed to make subvolume: %m");
142
143 r = btrfs_subvol_auto_qgroup("/xxxquotatest/beneath", 0, false);
144 if (r < 0)
145 log_error_errno(r, "Failed to set up auto qgroup: %m");
146
147 r = btrfs_qgroup_set_limit("/xxxquotatest/beneath", 0, 4ULL * 1024 * 1024 * 1024);
148 if (r < 0)
149 log_error_errno(r, "Failed to set up quota limit: %m");
150
151 r = btrfs_subvol_set_subtree_quota_limit("/xxxquotatest", 0, 5ULL * 1024 * 1024 * 1024);
152 if (r < 0)
153 log_error_errno(r, "Failed to set up quota limit: %m");
154
155 r = btrfs_subvol_snapshot_at(AT_FDCWD, "/xxxquotatest", AT_FDCWD, "/xxxquotatest2",
156 BTRFS_SNAPSHOT_RECURSIVE|BTRFS_SNAPSHOT_QUOTA);
157 if (r < 0)
158 log_error_errno(r, "Failed to set up 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 }