1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright 2014 Lennart Poettering
8 #include "btrfs-util.h"
12 #include "parse-util.h"
13 #include "string-util.h"
16 int main(int argc
, char *argv
[]) {
20 fd
= open("/", O_RDONLY
|O_CLOEXEC
|O_DIRECTORY
);
22 log_error_errno(errno
, "Failed to open root directory: %m");
24 char ts
[FORMAT_TIMESTAMP_MAX
], bs
[FORMAT_BYTES_MAX
];
27 r
= btrfs_subvol_get_info_fd(fd
, 0, &info
);
29 log_error_errno(r
, "Failed to get subvolume info: %m");
31 log_info("otime: %s", format_timestamp(ts
, sizeof(ts
), info
.otime
));
32 log_info("read-only (search): %s", yes_no(info
.read_only
));
35 r
= btrfs_qgroup_get_quota_fd(fd
, 0, "a
);
37 log_error_errno(r
, "Failed to get quota info: %m");
39 log_info("referenced: %s", strna(format_bytes(bs
, sizeof(bs
), quota
.referenced
)));
40 log_info("exclusive: %s", strna(format_bytes(bs
, sizeof(bs
), quota
.exclusive
)));
41 log_info("referenced_max: %s", strna(format_bytes(bs
, sizeof(bs
), quota
.referenced_max
)));
42 log_info("exclusive_max: %s", strna(format_bytes(bs
, sizeof(bs
), quota
.exclusive_max
)));
45 r
= btrfs_subvol_get_read_only_fd(fd
);
47 log_error_errno(r
, "Failed to get read only flag: %m");
49 log_info("read-only (ioctl): %s", yes_no(r
));
54 r
= btrfs_subvol_make("/xxxtest");
56 log_error_errno(r
, "Failed to make subvolume: %m");
58 r
= write_string_file("/xxxtest/afile", "ljsadhfljasdkfhlkjdsfha", WRITE_STRING_FILE_CREATE
);
60 log_error_errno(r
, "Failed to write file: %m");
62 r
= btrfs_subvol_snapshot("/xxxtest", "/xxxtest2", 0);
64 log_error_errno(r
, "Failed to make snapshot: %m");
66 r
= btrfs_subvol_snapshot("/xxxtest", "/xxxtest3", BTRFS_SNAPSHOT_READ_ONLY
);
68 log_error_errno(r
, "Failed to make snapshot: %m");
70 r
= btrfs_subvol_remove("/xxxtest", BTRFS_REMOVE_QUOTA
);
72 log_error_errno(r
, "Failed to remove subvolume: %m");
74 r
= btrfs_subvol_remove("/xxxtest2", BTRFS_REMOVE_QUOTA
);
76 log_error_errno(r
, "Failed to remove subvolume: %m");
78 r
= btrfs_subvol_remove("/xxxtest3", BTRFS_REMOVE_QUOTA
);
80 log_error_errno(r
, "Failed to remove subvolume: %m");
82 r
= btrfs_subvol_snapshot("/etc", "/etc2", BTRFS_SNAPSHOT_READ_ONLY
|BTRFS_SNAPSHOT_FALLBACK_COPY
);
84 log_error_errno(r
, "Failed to make snapshot: %m");
86 r
= btrfs_subvol_remove("/etc2", BTRFS_REMOVE_QUOTA
);
88 log_error_errno(r
, "Failed to remove subvolume: %m");
90 r
= btrfs_subvol_make("/xxxrectest");
92 log_error_errno(r
, "Failed to make subvolume: %m");
94 r
= btrfs_subvol_make("/xxxrectest/xxxrectest2");
96 log_error_errno(r
, "Failed to make subvolume: %m");
98 r
= btrfs_subvol_make("/xxxrectest/xxxrectest3");
100 log_error_errno(r
, "Failed to make subvolume: %m");
102 r
= btrfs_subvol_make("/xxxrectest/xxxrectest3/sub");
104 log_error_errno(r
, "Failed to make subvolume: %m");
106 if (mkdir("/xxxrectest/dir", 0755) < 0)
107 log_error_errno(errno
, "Failed to make directory: %m");
109 r
= btrfs_subvol_make("/xxxrectest/dir/xxxrectest4");
111 log_error_errno(r
, "Failed to make subvolume: %m");
113 if (mkdir("/xxxrectest/dir/xxxrectest4/dir", 0755) < 0)
114 log_error_errno(errno
, "Failed to make directory: %m");
116 r
= btrfs_subvol_make("/xxxrectest/dir/xxxrectest4/dir/xxxrectest5");
118 log_error_errno(r
, "Failed to make subvolume: %m");
120 if (mkdir("/xxxrectest/mnt", 0755) < 0)
121 log_error_errno(errno
, "Failed to make directory: %m");
123 r
= btrfs_subvol_snapshot("/xxxrectest", "/xxxrectest2", BTRFS_SNAPSHOT_RECURSIVE
);
125 log_error_errno(r
, "Failed to snapshot subvolume: %m");
127 r
= btrfs_subvol_remove("/xxxrectest", BTRFS_REMOVE_QUOTA
|BTRFS_REMOVE_RECURSIVE
);
129 log_error_errno(r
, "Failed to recursively remove subvolume: %m");
131 r
= btrfs_subvol_remove("/xxxrectest2", BTRFS_REMOVE_QUOTA
|BTRFS_REMOVE_RECURSIVE
);
133 log_error_errno(r
, "Failed to recursively remove subvolume: %m");
135 r
= btrfs_subvol_make("/xxxquotatest");
137 log_error_errno(r
, "Failed to make subvolume: %m");
139 r
= btrfs_subvol_auto_qgroup("/xxxquotatest", 0, true);
141 log_error_errno(r
, "Failed to set up auto qgroup: %m");
143 r
= btrfs_subvol_make("/xxxquotatest/beneath");
145 log_error_errno(r
, "Failed to make subvolume: %m");
147 r
= btrfs_subvol_auto_qgroup("/xxxquotatest/beneath", 0, false);
149 log_error_errno(r
, "Failed to set up auto qgroup: %m");
151 r
= btrfs_qgroup_set_limit("/xxxquotatest/beneath", 0, 4ULL * 1024 * 1024 * 1024);
153 log_error_errno(r
, "Failed to set up quota limit: %m");
155 r
= btrfs_subvol_set_subtree_quota_limit("/xxxquotatest", 0, 5ULL * 1024 * 1024 * 1024);
157 log_error_errno(r
, "Failed to set up quota limit: %m");
159 r
= btrfs_subvol_snapshot("/xxxquotatest", "/xxxquotatest2", BTRFS_SNAPSHOT_RECURSIVE
|BTRFS_SNAPSHOT_QUOTA
);
161 log_error_errno(r
, "Failed to setup snapshot: %m");
163 r
= btrfs_qgroup_get_quota("/xxxquotatest2/beneath", 0, "a
);
165 log_error_errno(r
, "Failed to query quota: %m");
167 assert_se(quota
.referenced_max
== 4ULL * 1024 * 1024 * 1024);
169 r
= btrfs_subvol_get_subtree_quota("/xxxquotatest2", 0, "a
);
171 log_error_errno(r
, "Failed to query quota: %m");
173 assert_se(quota
.referenced_max
== 5ULL * 1024 * 1024 * 1024);
175 r
= btrfs_subvol_remove("/xxxquotatest", BTRFS_REMOVE_QUOTA
|BTRFS_REMOVE_RECURSIVE
);
177 log_error_errno(r
, "Failed remove subvolume: %m");
179 r
= btrfs_subvol_remove("/xxxquotatest2", BTRFS_REMOVE_QUOTA
|BTRFS_REMOVE_RECURSIVE
);
181 log_error_errno(r
, "Failed remove subvolume: %m");