1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
5 #include "btrfs-util.h"
8 #include "format-util.h"
10 #include "string-util.h"
13 int main(int argc
, char *argv
[]) {
17 fd
= open("/", O_RDONLY
|O_CLOEXEC
|O_DIRECTORY
);
19 log_error_errno(errno
, "Failed to open root directory: %m");
21 char ts
[FORMAT_TIMESTAMP_MAX
], bs
[FORMAT_BYTES_MAX
];
24 r
= btrfs_subvol_get_info_fd(fd
, 0, &info
);
26 log_error_errno(r
, "Failed to get subvolume info: %m");
28 log_info("otime: %s", format_timestamp(ts
, sizeof(ts
), info
.otime
));
29 log_info("read-only (search): %s", yes_no(info
.read_only
));
32 r
= btrfs_qgroup_get_quota_fd(fd
, 0, "a
);
34 log_error_errno(r
, "Failed to get quota info: %m");
36 log_info("referenced: %s", strna(format_bytes(bs
, sizeof(bs
), quota
.referenced
)));
37 log_info("exclusive: %s", strna(format_bytes(bs
, sizeof(bs
), quota
.exclusive
)));
38 log_info("referenced_max: %s", strna(format_bytes(bs
, sizeof(bs
), quota
.referenced_max
)));
39 log_info("exclusive_max: %s", strna(format_bytes(bs
, sizeof(bs
), quota
.exclusive_max
)));
42 r
= btrfs_subvol_get_read_only_fd(fd
);
44 log_error_errno(r
, "Failed to get read only flag: %m");
46 log_info("read-only (ioctl): %s", yes_no(r
));
51 r
= btrfs_subvol_make("/xxxtest");
53 log_error_errno(r
, "Failed to make subvolume: %m");
55 r
= write_string_file("/xxxtest/afile", "ljsadhfljasdkfhlkjdsfha", WRITE_STRING_FILE_CREATE
);
57 log_error_errno(r
, "Failed to write file: %m");
59 r
= btrfs_subvol_snapshot("/xxxtest", "/xxxtest2", 0);
61 log_error_errno(r
, "Failed to make snapshot: %m");
63 r
= btrfs_subvol_snapshot("/xxxtest", "/xxxtest3", BTRFS_SNAPSHOT_READ_ONLY
);
65 log_error_errno(r
, "Failed to make snapshot: %m");
67 r
= btrfs_subvol_remove("/xxxtest", BTRFS_REMOVE_QUOTA
);
69 log_error_errno(r
, "Failed to remove subvolume: %m");
71 r
= btrfs_subvol_remove("/xxxtest2", BTRFS_REMOVE_QUOTA
);
73 log_error_errno(r
, "Failed to remove subvolume: %m");
75 r
= btrfs_subvol_remove("/xxxtest3", BTRFS_REMOVE_QUOTA
);
77 log_error_errno(r
, "Failed to remove subvolume: %m");
79 r
= btrfs_subvol_snapshot("/etc", "/etc2", BTRFS_SNAPSHOT_READ_ONLY
|BTRFS_SNAPSHOT_FALLBACK_COPY
);
81 log_error_errno(r
, "Failed to make snapshot: %m");
83 r
= btrfs_subvol_remove("/etc2", BTRFS_REMOVE_QUOTA
);
85 log_error_errno(r
, "Failed to remove subvolume: %m");
87 r
= btrfs_subvol_make("/xxxrectest");
89 log_error_errno(r
, "Failed to make subvolume: %m");
91 r
= btrfs_subvol_make("/xxxrectest/xxxrectest2");
93 log_error_errno(r
, "Failed to make subvolume: %m");
95 r
= btrfs_subvol_make("/xxxrectest/xxxrectest3");
97 log_error_errno(r
, "Failed to make subvolume: %m");
99 r
= btrfs_subvol_make("/xxxrectest/xxxrectest3/sub");
101 log_error_errno(r
, "Failed to make subvolume: %m");
103 if (mkdir("/xxxrectest/dir", 0755) < 0)
104 log_error_errno(errno
, "Failed to make directory: %m");
106 r
= btrfs_subvol_make("/xxxrectest/dir/xxxrectest4");
108 log_error_errno(r
, "Failed to make subvolume: %m");
110 if (mkdir("/xxxrectest/dir/xxxrectest4/dir", 0755) < 0)
111 log_error_errno(errno
, "Failed to make directory: %m");
113 r
= btrfs_subvol_make("/xxxrectest/dir/xxxrectest4/dir/xxxrectest5");
115 log_error_errno(r
, "Failed to make subvolume: %m");
117 if (mkdir("/xxxrectest/mnt", 0755) < 0)
118 log_error_errno(errno
, "Failed to make directory: %m");
120 r
= btrfs_subvol_snapshot("/xxxrectest", "/xxxrectest2", BTRFS_SNAPSHOT_RECURSIVE
);
122 log_error_errno(r
, "Failed to snapshot subvolume: %m");
124 r
= btrfs_subvol_remove("/xxxrectest", BTRFS_REMOVE_QUOTA
|BTRFS_REMOVE_RECURSIVE
);
126 log_error_errno(r
, "Failed to recursively remove subvolume: %m");
128 r
= btrfs_subvol_remove("/xxxrectest2", BTRFS_REMOVE_QUOTA
|BTRFS_REMOVE_RECURSIVE
);
130 log_error_errno(r
, "Failed to recursively remove subvolume: %m");
132 r
= btrfs_subvol_make("/xxxquotatest");
134 log_error_errno(r
, "Failed to make subvolume: %m");
136 r
= btrfs_subvol_auto_qgroup("/xxxquotatest", 0, true);
138 log_error_errno(r
, "Failed to set up auto qgroup: %m");
140 r
= btrfs_subvol_make("/xxxquotatest/beneath");
142 log_error_errno(r
, "Failed to make subvolume: %m");
144 r
= btrfs_subvol_auto_qgroup("/xxxquotatest/beneath", 0, false);
146 log_error_errno(r
, "Failed to set up auto qgroup: %m");
148 r
= btrfs_qgroup_set_limit("/xxxquotatest/beneath", 0, 4ULL * 1024 * 1024 * 1024);
150 log_error_errno(r
, "Failed to set up quota limit: %m");
152 r
= btrfs_subvol_set_subtree_quota_limit("/xxxquotatest", 0, 5ULL * 1024 * 1024 * 1024);
154 log_error_errno(r
, "Failed to set up quota limit: %m");
156 r
= btrfs_subvol_snapshot("/xxxquotatest", "/xxxquotatest2", BTRFS_SNAPSHOT_RECURSIVE
|BTRFS_SNAPSHOT_QUOTA
);
158 log_error_errno(r
, "Failed to set up snapshot: %m");
160 r
= btrfs_qgroup_get_quota("/xxxquotatest2/beneath", 0, "a
);
162 log_error_errno(r
, "Failed to query quota: %m");
164 assert_se(quota
.referenced_max
== 4ULL * 1024 * 1024 * 1024);
166 r
= btrfs_subvol_get_subtree_quota("/xxxquotatest2", 0, "a
);
168 log_error_errno(r
, "Failed to query quota: %m");
170 assert_se(quota
.referenced_max
== 5ULL * 1024 * 1024 * 1024);
172 r
= btrfs_subvol_remove("/xxxquotatest", BTRFS_REMOVE_QUOTA
|BTRFS_REMOVE_RECURSIVE
);
174 log_error_errno(r
, "Failed remove subvolume: %m");
176 r
= btrfs_subvol_remove("/xxxquotatest2", BTRFS_REMOVE_QUOTA
|BTRFS_REMOVE_RECURSIVE
);
178 log_error_errno(r
, "Failed remove subvolume: %m");