]> git.ipfire.org Git - thirdparty/systemd.git/blame - 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
CommitLineData
d7c7c334
LP
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
10f9c755 22#include <fcntl.h>
d7c7c334 23
10f9c755 24#include "btrfs-util.h"
3ffd4af2
LP
25#include "fd-util.h"
26#include "fileio.h"
27#include "log.h"
6bedfcbb 28#include "parse-util.h"
07630cea 29#include "string-util.h"
3ffd4af2 30#include "util.h"
d7c7c334
LP
31
32int main(int argc, char *argv[]) {
5bcd08db 33 BtrfsQuotaInfo quota;
d9e2daaf 34 int r, fd;
10f9c755
LP
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 {
b6b18498 40 char ts[FORMAT_TIMESTAMP_MAX], bs[FORMAT_BYTES_MAX];
5bcd08db 41 BtrfsSubvolInfo info;
c75f27ea 42
5bcd08db 43 r = btrfs_subvol_get_info_fd(fd, 0, &info);
10f9c755
LP
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));
c75f27ea 48 log_info("read-only (search): %s", yes_no(info.read_only));
10f9c755
LP
49 }
50
5bcd08db 51 r = btrfs_qgroup_get_quota_fd(fd, 0, &quota);
b6b18498
LP
52 if (r < 0)
53 log_error_errno(r, "Failed to get quota info: %m");
54 else {
cb81cd80 55 log_info("referenced: %s", strna(format_bytes(bs, sizeof(bs), quota.referenced)));
b6b18498 56 log_info("exclusive: %s", strna(format_bytes(bs, sizeof(bs), quota.exclusive)));
cb81cd80 57 log_info("referenced_max: %s", strna(format_bytes(bs, sizeof(bs), quota.referenced_max)));
b6b18498
LP
58 log_info("exclusive_max: %s", strna(format_bytes(bs, sizeof(bs), quota.exclusive_max)));
59 }
60
10f9c755 61 r = btrfs_subvol_get_read_only_fd(fd);
c75f27ea
LP
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));
10f9c755 66
c75f27ea 67 safe_close(fd);
10f9c755 68 }
d7c7c334
LP
69
70 r = btrfs_subvol_make("/xxxtest");
71 if (r < 0)
72 log_error_errno(r, "Failed to make subvolume: %m");
73
4c1fc3e4 74 r = write_string_file("/xxxtest/afile", "ljsadhfljasdkfhlkjdsfha", WRITE_STRING_FILE_CREATE);
d7c7c334
LP
75 if (r < 0)
76 log_error_errno(r, "Failed to write file: %m");
77
e9bc1871 78 r = btrfs_subvol_snapshot("/xxxtest", "/xxxtest2", 0);
d7c7c334
LP
79 if (r < 0)
80 log_error_errno(r, "Failed to make snapshot: %m");
81
e9bc1871 82 r = btrfs_subvol_snapshot("/xxxtest", "/xxxtest3", BTRFS_SNAPSHOT_READ_ONLY);
d7c7c334
LP
83 if (r < 0)
84 log_error_errno(r, "Failed to make snapshot: %m");
85
5bcd08db 86 r = btrfs_subvol_remove("/xxxtest", BTRFS_REMOVE_QUOTA);
d7c7c334
LP
87 if (r < 0)
88 log_error_errno(r, "Failed to remove subvolume: %m");
89
5bcd08db 90 r = btrfs_subvol_remove("/xxxtest2", BTRFS_REMOVE_QUOTA);
d7c7c334
LP
91 if (r < 0)
92 log_error_errno(r, "Failed to remove subvolume: %m");
93
5bcd08db 94 r = btrfs_subvol_remove("/xxxtest3", BTRFS_REMOVE_QUOTA);
d7c7c334
LP
95 if (r < 0)
96 log_error_errno(r, "Failed to remove subvolume: %m");
97
e9bc1871 98 r = btrfs_subvol_snapshot("/etc", "/etc2", BTRFS_SNAPSHOT_READ_ONLY|BTRFS_SNAPSHOT_FALLBACK_COPY);
d7c7c334
LP
99 if (r < 0)
100 log_error_errno(r, "Failed to make snapshot: %m");
101
5bcd08db 102 r = btrfs_subvol_remove("/etc2", BTRFS_REMOVE_QUOTA);
d7c7c334
LP
103 if (r < 0)
104 log_error_errno(r, "Failed to remove subvolume: %m");
105
d9e2daaf
LP
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
f70a17f8
LP
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
5bcd08db 143 r = btrfs_subvol_remove("/xxxrectest", BTRFS_REMOVE_QUOTA|BTRFS_REMOVE_RECURSIVE);
d9e2daaf
LP
144 if (r < 0)
145 log_error_errno(r, "Failed to recursively remove subvolume: %m");
146
5bcd08db 147 r = btrfs_subvol_remove("/xxxrectest2", BTRFS_REMOVE_QUOTA|BTRFS_REMOVE_RECURSIVE);
f70a17f8
LP
148 if (r < 0)
149 log_error_errno(r, "Failed to recursively remove subvolume: %m");
150
5bcd08db
LP
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
d7c7c334
LP
199 return 0;
200}