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