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