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