]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-btrfs.c
Merge pull request #7388 from keszybz/doc-tweak
[thirdparty/systemd.git] / src / test / test-btrfs.c
CommitLineData
d7c7c334
LP
1/***
2 This file is part of systemd.
3
4 Copyright 2014 Lennart Poettering
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18***/
19
10f9c755 20#include <fcntl.h>
d7c7c334 21
10f9c755 22#include "btrfs-util.h"
3ffd4af2
LP
23#include "fd-util.h"
24#include "fileio.h"
25#include "log.h"
6bedfcbb 26#include "parse-util.h"
07630cea 27#include "string-util.h"
3ffd4af2 28#include "util.h"
d7c7c334
LP
29
30int main(int argc, char *argv[]) {
5bcd08db 31 BtrfsQuotaInfo quota;
d9e2daaf 32 int r, fd;
10f9c755
LP
33
34 fd = open("/", O_RDONLY|O_CLOEXEC|O_DIRECTORY);
35 if (fd < 0)
36 log_error_errno(errno, "Failed to open root directory: %m");
37 else {
b6b18498 38 char ts[FORMAT_TIMESTAMP_MAX], bs[FORMAT_BYTES_MAX];
5bcd08db 39 BtrfsSubvolInfo info;
c75f27ea 40
5bcd08db 41 r = btrfs_subvol_get_info_fd(fd, 0, &info);
10f9c755
LP
42 if (r < 0)
43 log_error_errno(r, "Failed to get subvolume info: %m");
44 else {
45 log_info("otime: %s", format_timestamp(ts, sizeof(ts), info.otime));
c75f27ea 46 log_info("read-only (search): %s", yes_no(info.read_only));
10f9c755
LP
47 }
48
5bcd08db 49 r = btrfs_qgroup_get_quota_fd(fd, 0, &quota);
b6b18498
LP
50 if (r < 0)
51 log_error_errno(r, "Failed to get quota info: %m");
52 else {
cb81cd80 53 log_info("referenced: %s", strna(format_bytes(bs, sizeof(bs), quota.referenced)));
b6b18498 54 log_info("exclusive: %s", strna(format_bytes(bs, sizeof(bs), quota.exclusive)));
cb81cd80 55 log_info("referenced_max: %s", strna(format_bytes(bs, sizeof(bs), quota.referenced_max)));
b6b18498
LP
56 log_info("exclusive_max: %s", strna(format_bytes(bs, sizeof(bs), quota.exclusive_max)));
57 }
58
10f9c755 59 r = btrfs_subvol_get_read_only_fd(fd);
c75f27ea
LP
60 if (r < 0)
61 log_error_errno(r, "Failed to get read only flag: %m");
62 else
63 log_info("read-only (ioctl): %s", yes_no(r));
10f9c755 64
c75f27ea 65 safe_close(fd);
10f9c755 66 }
d7c7c334
LP
67
68 r = btrfs_subvol_make("/xxxtest");
69 if (r < 0)
70 log_error_errno(r, "Failed to make subvolume: %m");
71
4c1fc3e4 72 r = write_string_file("/xxxtest/afile", "ljsadhfljasdkfhlkjdsfha", WRITE_STRING_FILE_CREATE);
d7c7c334
LP
73 if (r < 0)
74 log_error_errno(r, "Failed to write file: %m");
75
e9bc1871 76 r = btrfs_subvol_snapshot("/xxxtest", "/xxxtest2", 0);
d7c7c334
LP
77 if (r < 0)
78 log_error_errno(r, "Failed to make snapshot: %m");
79
e9bc1871 80 r = btrfs_subvol_snapshot("/xxxtest", "/xxxtest3", BTRFS_SNAPSHOT_READ_ONLY);
d7c7c334
LP
81 if (r < 0)
82 log_error_errno(r, "Failed to make snapshot: %m");
83
5bcd08db 84 r = btrfs_subvol_remove("/xxxtest", BTRFS_REMOVE_QUOTA);
d7c7c334
LP
85 if (r < 0)
86 log_error_errno(r, "Failed to remove subvolume: %m");
87
5bcd08db 88 r = btrfs_subvol_remove("/xxxtest2", BTRFS_REMOVE_QUOTA);
d7c7c334
LP
89 if (r < 0)
90 log_error_errno(r, "Failed to remove subvolume: %m");
91
5bcd08db 92 r = btrfs_subvol_remove("/xxxtest3", BTRFS_REMOVE_QUOTA);
d7c7c334
LP
93 if (r < 0)
94 log_error_errno(r, "Failed to remove subvolume: %m");
95
e9bc1871 96 r = btrfs_subvol_snapshot("/etc", "/etc2", BTRFS_SNAPSHOT_READ_ONLY|BTRFS_SNAPSHOT_FALLBACK_COPY);
d7c7c334
LP
97 if (r < 0)
98 log_error_errno(r, "Failed to make snapshot: %m");
99
5bcd08db 100 r = btrfs_subvol_remove("/etc2", BTRFS_REMOVE_QUOTA);
d7c7c334
LP
101 if (r < 0)
102 log_error_errno(r, "Failed to remove subvolume: %m");
103
d9e2daaf
LP
104 r = btrfs_subvol_make("/xxxrectest");
105 if (r < 0)
106 log_error_errno(r, "Failed to make subvolume: %m");
107
108 r = btrfs_subvol_make("/xxxrectest/xxxrectest2");
109 if (r < 0)
110 log_error_errno(r, "Failed to make subvolume: %m");
111
112 r = btrfs_subvol_make("/xxxrectest/xxxrectest3");
113 if (r < 0)
114 log_error_errno(r, "Failed to make subvolume: %m");
115
116 r = btrfs_subvol_make("/xxxrectest/xxxrectest3/sub");
117 if (r < 0)
118 log_error_errno(r, "Failed to make subvolume: %m");
119
120 if (mkdir("/xxxrectest/dir", 0755) < 0)
121 log_error_errno(errno, "Failed to make directory: %m");
122
123 r = btrfs_subvol_make("/xxxrectest/dir/xxxrectest4");
124 if (r < 0)
125 log_error_errno(r, "Failed to make subvolume: %m");
126
127 if (mkdir("/xxxrectest/dir/xxxrectest4/dir", 0755) < 0)
128 log_error_errno(errno, "Failed to make directory: %m");
129
130 r = btrfs_subvol_make("/xxxrectest/dir/xxxrectest4/dir/xxxrectest5");
131 if (r < 0)
132 log_error_errno(r, "Failed to make subvolume: %m");
133
134 if (mkdir("/xxxrectest/mnt", 0755) < 0)
135 log_error_errno(errno, "Failed to make directory: %m");
136
f70a17f8
LP
137 r = btrfs_subvol_snapshot("/xxxrectest", "/xxxrectest2", BTRFS_SNAPSHOT_RECURSIVE);
138 if (r < 0)
139 log_error_errno(r, "Failed to snapshot subvolume: %m");
140
5bcd08db 141 r = btrfs_subvol_remove("/xxxrectest", BTRFS_REMOVE_QUOTA|BTRFS_REMOVE_RECURSIVE);
d9e2daaf
LP
142 if (r < 0)
143 log_error_errno(r, "Failed to recursively remove subvolume: %m");
144
5bcd08db 145 r = btrfs_subvol_remove("/xxxrectest2", BTRFS_REMOVE_QUOTA|BTRFS_REMOVE_RECURSIVE);
f70a17f8
LP
146 if (r < 0)
147 log_error_errno(r, "Failed to recursively remove subvolume: %m");
148
5bcd08db
LP
149 r = btrfs_subvol_make("/xxxquotatest");
150 if (r < 0)
151 log_error_errno(r, "Failed to make subvolume: %m");
152
153 r = btrfs_subvol_auto_qgroup("/xxxquotatest", 0, true);
154 if (r < 0)
155 log_error_errno(r, "Failed to set up auto qgroup: %m");
156
157 r = btrfs_subvol_make("/xxxquotatest/beneath");
158 if (r < 0)
159 log_error_errno(r, "Failed to make subvolume: %m");
160
161 r = btrfs_subvol_auto_qgroup("/xxxquotatest/beneath", 0, false);
162 if (r < 0)
163 log_error_errno(r, "Failed to set up auto qgroup: %m");
164
165 r = btrfs_qgroup_set_limit("/xxxquotatest/beneath", 0, 4ULL * 1024 * 1024 * 1024);
166 if (r < 0)
167 log_error_errno(r, "Failed to set up quota limit: %m");
168
169 r = btrfs_subvol_set_subtree_quota_limit("/xxxquotatest", 0, 5ULL * 1024 * 1024 * 1024);
170 if (r < 0)
171 log_error_errno(r, "Failed to set up quota limit: %m");
172
173 r = btrfs_subvol_snapshot("/xxxquotatest", "/xxxquotatest2", BTRFS_SNAPSHOT_RECURSIVE|BTRFS_SNAPSHOT_QUOTA);
174 if (r < 0)
175 log_error_errno(r, "Failed to setup snapshot: %m");
176
177 r = btrfs_qgroup_get_quota("/xxxquotatest2/beneath", 0, &quota);
178 if (r < 0)
179 log_error_errno(r, "Failed to query quota: %m");
180
181 assert_se(quota.referenced_max == 4ULL * 1024 * 1024 * 1024);
182
183 r = btrfs_subvol_get_subtree_quota("/xxxquotatest2", 0, &quota);
184 if (r < 0)
185 log_error_errno(r, "Failed to query quota: %m");
186
187 assert_se(quota.referenced_max == 5ULL * 1024 * 1024 * 1024);
188
189 r = btrfs_subvol_remove("/xxxquotatest", BTRFS_REMOVE_QUOTA|BTRFS_REMOVE_RECURSIVE);
190 if (r < 0)
191 log_error_errno(r, "Failed remove subvolume: %m");
192
193 r = btrfs_subvol_remove("/xxxquotatest2", BTRFS_REMOVE_QUOTA|BTRFS_REMOVE_RECURSIVE);
194 if (r < 0)
195 log_error_errno(r, "Failed remove subvolume: %m");
196
d7c7c334
LP
197 return 0;
198}