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