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