1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2014 Lennart Poettering
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
23 #include "btrfs-util.h"
27 #include "parse-util.h"
28 #include "string-util.h"
31 int main(int argc
, char *argv
[]) {
35 fd
= open("/", O_RDONLY
|O_CLOEXEC
|O_DIRECTORY
);
37 log_error_errno(errno
, "Failed to open root directory: %m");
39 char ts
[FORMAT_TIMESTAMP_MAX
], bs
[FORMAT_BYTES_MAX
];
42 r
= btrfs_subvol_get_info_fd(fd
, 0, &info
);
44 log_error_errno(r
, "Failed to get subvolume info: %m");
46 log_info("otime: %s", format_timestamp(ts
, sizeof(ts
), info
.otime
));
47 log_info("read-only (search): %s", yes_no(info
.read_only
));
50 r
= btrfs_qgroup_get_quota_fd(fd
, 0, "a
);
52 log_error_errno(r
, "Failed to get quota info: %m");
54 log_info("referenced: %s", strna(format_bytes(bs
, sizeof(bs
), quota
.referenced
)));
55 log_info("exclusive: %s", strna(format_bytes(bs
, sizeof(bs
), quota
.exclusive
)));
56 log_info("referenced_max: %s", strna(format_bytes(bs
, sizeof(bs
), quota
.referenced_max
)));
57 log_info("exclusive_max: %s", strna(format_bytes(bs
, sizeof(bs
), quota
.exclusive_max
)));
60 r
= btrfs_subvol_get_read_only_fd(fd
);
62 log_error_errno(r
, "Failed to get read only flag: %m");
64 log_info("read-only (ioctl): %s", yes_no(r
));
69 r
= btrfs_subvol_make("/xxxtest");
71 log_error_errno(r
, "Failed to make subvolume: %m");
73 r
= write_string_file("/xxxtest/afile", "ljsadhfljasdkfhlkjdsfha", WRITE_STRING_FILE_CREATE
);
75 log_error_errno(r
, "Failed to write file: %m");
77 r
= btrfs_subvol_snapshot("/xxxtest", "/xxxtest2", 0);
79 log_error_errno(r
, "Failed to make snapshot: %m");
81 r
= btrfs_subvol_snapshot("/xxxtest", "/xxxtest3", BTRFS_SNAPSHOT_READ_ONLY
);
83 log_error_errno(r
, "Failed to make snapshot: %m");
85 r
= btrfs_subvol_remove("/xxxtest", BTRFS_REMOVE_QUOTA
);
87 log_error_errno(r
, "Failed to remove subvolume: %m");
89 r
= btrfs_subvol_remove("/xxxtest2", BTRFS_REMOVE_QUOTA
);
91 log_error_errno(r
, "Failed to remove subvolume: %m");
93 r
= btrfs_subvol_remove("/xxxtest3", BTRFS_REMOVE_QUOTA
);
95 log_error_errno(r
, "Failed to remove subvolume: %m");
97 r
= btrfs_subvol_snapshot("/etc", "/etc2", BTRFS_SNAPSHOT_READ_ONLY
|BTRFS_SNAPSHOT_FALLBACK_COPY
);
99 log_error_errno(r
, "Failed to make snapshot: %m");
101 r
= btrfs_subvol_remove("/etc2", BTRFS_REMOVE_QUOTA
);
103 log_error_errno(r
, "Failed to remove subvolume: %m");
105 r
= btrfs_subvol_make("/xxxrectest");
107 log_error_errno(r
, "Failed to make subvolume: %m");
109 r
= btrfs_subvol_make("/xxxrectest/xxxrectest2");
111 log_error_errno(r
, "Failed to make subvolume: %m");
113 r
= btrfs_subvol_make("/xxxrectest/xxxrectest3");
115 log_error_errno(r
, "Failed to make subvolume: %m");
117 r
= btrfs_subvol_make("/xxxrectest/xxxrectest3/sub");
119 log_error_errno(r
, "Failed to make subvolume: %m");
121 if (mkdir("/xxxrectest/dir", 0755) < 0)
122 log_error_errno(errno
, "Failed to make directory: %m");
124 r
= btrfs_subvol_make("/xxxrectest/dir/xxxrectest4");
126 log_error_errno(r
, "Failed to make subvolume: %m");
128 if (mkdir("/xxxrectest/dir/xxxrectest4/dir", 0755) < 0)
129 log_error_errno(errno
, "Failed to make directory: %m");
131 r
= btrfs_subvol_make("/xxxrectest/dir/xxxrectest4/dir/xxxrectest5");
133 log_error_errno(r
, "Failed to make subvolume: %m");
135 if (mkdir("/xxxrectest/mnt", 0755) < 0)
136 log_error_errno(errno
, "Failed to make directory: %m");
138 r
= btrfs_subvol_snapshot("/xxxrectest", "/xxxrectest2", BTRFS_SNAPSHOT_RECURSIVE
);
140 log_error_errno(r
, "Failed to snapshot subvolume: %m");
142 r
= btrfs_subvol_remove("/xxxrectest", BTRFS_REMOVE_QUOTA
|BTRFS_REMOVE_RECURSIVE
);
144 log_error_errno(r
, "Failed to recursively remove subvolume: %m");
146 r
= btrfs_subvol_remove("/xxxrectest2", BTRFS_REMOVE_QUOTA
|BTRFS_REMOVE_RECURSIVE
);
148 log_error_errno(r
, "Failed to recursively remove subvolume: %m");
150 r
= btrfs_subvol_make("/xxxquotatest");
152 log_error_errno(r
, "Failed to make subvolume: %m");
154 r
= btrfs_subvol_auto_qgroup("/xxxquotatest", 0, true);
156 log_error_errno(r
, "Failed to set up auto qgroup: %m");
158 r
= btrfs_subvol_make("/xxxquotatest/beneath");
160 log_error_errno(r
, "Failed to make subvolume: %m");
162 r
= btrfs_subvol_auto_qgroup("/xxxquotatest/beneath", 0, false);
164 log_error_errno(r
, "Failed to set up auto qgroup: %m");
166 r
= btrfs_qgroup_set_limit("/xxxquotatest/beneath", 0, 4ULL * 1024 * 1024 * 1024);
168 log_error_errno(r
, "Failed to set up quota limit: %m");
170 r
= btrfs_subvol_set_subtree_quota_limit("/xxxquotatest", 0, 5ULL * 1024 * 1024 * 1024);
172 log_error_errno(r
, "Failed to set up quota limit: %m");
174 r
= btrfs_subvol_snapshot("/xxxquotatest", "/xxxquotatest2", BTRFS_SNAPSHOT_RECURSIVE
|BTRFS_SNAPSHOT_QUOTA
);
176 log_error_errno(r
, "Failed to setup snapshot: %m");
178 r
= btrfs_qgroup_get_quota("/xxxquotatest2/beneath", 0, "a
);
180 log_error_errno(r
, "Failed to query quota: %m");
182 assert_se(quota
.referenced_max
== 4ULL * 1024 * 1024 * 1024);
184 r
= btrfs_subvol_get_subtree_quota("/xxxquotatest2", 0, "a
);
186 log_error_errno(r
, "Failed to query quota: %m");
188 assert_se(quota
.referenced_max
== 5ULL * 1024 * 1024 * 1024);
190 r
= btrfs_subvol_remove("/xxxquotatest", BTRFS_REMOVE_QUOTA
|BTRFS_REMOVE_RECURSIVE
);
192 log_error_errno(r
, "Failed remove subvolume: %m");
194 r
= btrfs_subvol_remove("/xxxquotatest2", BTRFS_REMOVE_QUOTA
|BTRFS_REMOVE_RECURSIVE
);
196 log_error_errno(r
, "Failed remove subvolume: %m");