1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
5 #include "btrfs-util.h"
8 #include "format-util.h"
10 #include "string-util.h"
12 int main(int argc
, char *argv
[]) {
16 fd
= open("/", O_RDONLY
|O_CLOEXEC
|O_DIRECTORY
);
18 log_error_errno(errno
, "Failed to open root directory: %m");
22 r
= btrfs_subvol_get_info_fd(fd
, 0, &info
);
24 log_error_errno(r
, "Failed to get subvolume info: %m");
26 log_info("otime: %s", FORMAT_TIMESTAMP(info
.otime
));
27 log_info("read-only (search): %s", yes_no(info
.read_only
));
30 r
= btrfs_qgroup_get_quota_fd(fd
, 0, "a
);
32 log_error_errno(r
, "Failed to get quota info: %m");
34 log_info("referenced: %s", strna(FORMAT_BYTES(quota
.referenced
)));
35 log_info("exclusive: %s", strna(FORMAT_BYTES(quota
.exclusive
)));
36 log_info("referenced_max: %s", strna(FORMAT_BYTES(quota
.referenced_max
)));
37 log_info("exclusive_max: %s", strna(FORMAT_BYTES(quota
.exclusive_max
)));
40 r
= btrfs_subvol_get_read_only_fd(fd
);
42 log_error_errno(r
, "Failed to get read only flag: %m");
44 log_info("read-only (ioctl): %s", yes_no(r
));
49 r
= btrfs_subvol_make("/xxxtest");
51 log_error_errno(r
, "Failed to make subvolume: %m");
53 r
= write_string_file("/xxxtest/afile", "ljsadhfljasdkfhlkjdsfha", WRITE_STRING_FILE_CREATE
);
55 log_error_errno(r
, "Failed to write file: %m");
57 r
= btrfs_subvol_snapshot_at(AT_FDCWD
, "/xxxtest", AT_FDCWD
, "/xxxtest2", 0);
59 log_error_errno(r
, "Failed to make snapshot: %m");
61 r
= btrfs_subvol_snapshot_at(AT_FDCWD
, "/xxxtest", AT_FDCWD
, "/xxxtest3", BTRFS_SNAPSHOT_READ_ONLY
);
63 log_error_errno(r
, "Failed to make snapshot: %m");
65 r
= btrfs_subvol_remove("/xxxtest", BTRFS_REMOVE_QUOTA
);
67 log_error_errno(r
, "Failed to remove subvolume: %m");
69 r
= btrfs_subvol_remove("/xxxtest2", BTRFS_REMOVE_QUOTA
);
71 log_error_errno(r
, "Failed to remove subvolume: %m");
73 r
= btrfs_subvol_remove("/xxxtest3", BTRFS_REMOVE_QUOTA
);
75 log_error_errno(r
, "Failed to remove subvolume: %m");
77 r
= btrfs_subvol_snapshot_at(AT_FDCWD
, "/etc", AT_FDCWD
, "/etc2",
78 BTRFS_SNAPSHOT_READ_ONLY
|BTRFS_SNAPSHOT_FALLBACK_COPY
);
80 log_error_errno(r
, "Failed to make snapshot: %m");
82 r
= btrfs_subvol_remove("/etc2", BTRFS_REMOVE_QUOTA
);
84 log_error_errno(r
, "Failed to remove subvolume: %m");
86 r
= btrfs_subvol_make("/xxxrectest");
88 log_error_errno(r
, "Failed to make subvolume: %m");
90 r
= btrfs_subvol_make("/xxxrectest/xxxrectest2");
92 log_error_errno(r
, "Failed to make subvolume: %m");
94 r
= btrfs_subvol_make("/xxxrectest/xxxrectest3");
96 log_error_errno(r
, "Failed to make subvolume: %m");
98 r
= btrfs_subvol_make("/xxxrectest/xxxrectest3/sub");
100 log_error_errno(r
, "Failed to make subvolume: %m");
102 if (mkdir("/xxxrectest/dir", 0755) < 0)
103 log_error_errno(errno
, "Failed to make directory: %m");
105 r
= btrfs_subvol_make("/xxxrectest/dir/xxxrectest4");
107 log_error_errno(r
, "Failed to make subvolume: %m");
109 if (mkdir("/xxxrectest/dir/xxxrectest4/dir", 0755) < 0)
110 log_error_errno(errno
, "Failed to make directory: %m");
112 r
= btrfs_subvol_make("/xxxrectest/dir/xxxrectest4/dir/xxxrectest5");
114 log_error_errno(r
, "Failed to make subvolume: %m");
116 if (mkdir("/xxxrectest/mnt", 0755) < 0)
117 log_error_errno(errno
, "Failed to make directory: %m");
119 r
= btrfs_subvol_snapshot_at(AT_FDCWD
, "/xxxrectest", AT_FDCWD
, "/xxxrectest2", BTRFS_SNAPSHOT_RECURSIVE
);
121 log_error_errno(r
, "Failed to snapshot subvolume: %m");
123 r
= btrfs_subvol_remove("/xxxrectest", BTRFS_REMOVE_QUOTA
|BTRFS_REMOVE_RECURSIVE
);
125 log_error_errno(r
, "Failed to recursively remove subvolume: %m");
127 r
= btrfs_subvol_remove("/xxxrectest2", BTRFS_REMOVE_QUOTA
|BTRFS_REMOVE_RECURSIVE
);
129 log_error_errno(r
, "Failed to recursively remove subvolume: %m");
131 r
= btrfs_subvol_make("/xxxquotatest");
133 log_error_errno(r
, "Failed to make subvolume: %m");
135 r
= btrfs_subvol_auto_qgroup("/xxxquotatest", 0, true);
137 log_error_errno(r
, "Failed to set up auto qgroup: %m");
139 r
= btrfs_subvol_make("/xxxquotatest/beneath");
141 log_error_errno(r
, "Failed to make subvolume: %m");
143 r
= btrfs_subvol_auto_qgroup("/xxxquotatest/beneath", 0, false);
145 log_error_errno(r
, "Failed to set up auto qgroup: %m");
147 r
= btrfs_qgroup_set_limit("/xxxquotatest/beneath", 0, 4ULL * 1024 * 1024 * 1024);
149 log_error_errno(r
, "Failed to set up quota limit: %m");
151 r
= btrfs_subvol_set_subtree_quota_limit("/xxxquotatest", 0, 5ULL * 1024 * 1024 * 1024);
153 log_error_errno(r
, "Failed to set up quota limit: %m");
155 r
= btrfs_subvol_snapshot_at(AT_FDCWD
, "/xxxquotatest", AT_FDCWD
, "/xxxquotatest2",
156 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");