1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2014 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
24 #include "btrfs-util.h"
28 #include "parse-util.h"
29 #include "string-util.h"
32 int main(int argc
, char *argv
[]) {
36 fd
= open("/", O_RDONLY
|O_CLOEXEC
|O_DIRECTORY
);
38 log_error_errno(errno
, "Failed to open root directory: %m");
40 char ts
[FORMAT_TIMESTAMP_MAX
], bs
[FORMAT_BYTES_MAX
];
43 r
= btrfs_subvol_get_info_fd(fd
, 0, &info
);
45 log_error_errno(r
, "Failed to get subvolume info: %m");
47 log_info("otime: %s", format_timestamp(ts
, sizeof(ts
), info
.otime
));
48 log_info("read-only (search): %s", yes_no(info
.read_only
));
51 r
= btrfs_qgroup_get_quota_fd(fd
, 0, "a
);
53 log_error_errno(r
, "Failed to get quota info: %m");
55 log_info("referenced: %s", strna(format_bytes(bs
, sizeof(bs
), quota
.referenced
)));
56 log_info("exclusive: %s", strna(format_bytes(bs
, sizeof(bs
), quota
.exclusive
)));
57 log_info("referenced_max: %s", strna(format_bytes(bs
, sizeof(bs
), quota
.referenced_max
)));
58 log_info("exclusive_max: %s", strna(format_bytes(bs
, sizeof(bs
), quota
.exclusive_max
)));
61 r
= btrfs_subvol_get_read_only_fd(fd
);
63 log_error_errno(r
, "Failed to get read only flag: %m");
65 log_info("read-only (ioctl): %s", yes_no(r
));
70 r
= btrfs_subvol_make("/xxxtest");
72 log_error_errno(r
, "Failed to make subvolume: %m");
74 r
= write_string_file("/xxxtest/afile", "ljsadhfljasdkfhlkjdsfha", WRITE_STRING_FILE_CREATE
);
76 log_error_errno(r
, "Failed to write file: %m");
78 r
= btrfs_subvol_snapshot("/xxxtest", "/xxxtest2", 0);
80 log_error_errno(r
, "Failed to make snapshot: %m");
82 r
= btrfs_subvol_snapshot("/xxxtest", "/xxxtest3", BTRFS_SNAPSHOT_READ_ONLY
);
84 log_error_errno(r
, "Failed to make snapshot: %m");
86 r
= btrfs_subvol_remove("/xxxtest", BTRFS_REMOVE_QUOTA
);
88 log_error_errno(r
, "Failed to remove subvolume: %m");
90 r
= btrfs_subvol_remove("/xxxtest2", BTRFS_REMOVE_QUOTA
);
92 log_error_errno(r
, "Failed to remove subvolume: %m");
94 r
= btrfs_subvol_remove("/xxxtest3", BTRFS_REMOVE_QUOTA
);
96 log_error_errno(r
, "Failed to remove subvolume: %m");
98 r
= btrfs_subvol_snapshot("/etc", "/etc2", BTRFS_SNAPSHOT_READ_ONLY
|BTRFS_SNAPSHOT_FALLBACK_COPY
);
100 log_error_errno(r
, "Failed to make snapshot: %m");
102 r
= btrfs_subvol_remove("/etc2", BTRFS_REMOVE_QUOTA
);
104 log_error_errno(r
, "Failed to remove subvolume: %m");
106 r
= btrfs_subvol_make("/xxxrectest");
108 log_error_errno(r
, "Failed to make subvolume: %m");
110 r
= btrfs_subvol_make("/xxxrectest/xxxrectest2");
112 log_error_errno(r
, "Failed to make subvolume: %m");
114 r
= btrfs_subvol_make("/xxxrectest/xxxrectest3");
116 log_error_errno(r
, "Failed to make subvolume: %m");
118 r
= btrfs_subvol_make("/xxxrectest/xxxrectest3/sub");
120 log_error_errno(r
, "Failed to make subvolume: %m");
122 if (mkdir("/xxxrectest/dir", 0755) < 0)
123 log_error_errno(errno
, "Failed to make directory: %m");
125 r
= btrfs_subvol_make("/xxxrectest/dir/xxxrectest4");
127 log_error_errno(r
, "Failed to make subvolume: %m");
129 if (mkdir("/xxxrectest/dir/xxxrectest4/dir", 0755) < 0)
130 log_error_errno(errno
, "Failed to make directory: %m");
132 r
= btrfs_subvol_make("/xxxrectest/dir/xxxrectest4/dir/xxxrectest5");
134 log_error_errno(r
, "Failed to make subvolume: %m");
136 if (mkdir("/xxxrectest/mnt", 0755) < 0)
137 log_error_errno(errno
, "Failed to make directory: %m");
139 r
= btrfs_subvol_snapshot("/xxxrectest", "/xxxrectest2", BTRFS_SNAPSHOT_RECURSIVE
);
141 log_error_errno(r
, "Failed to snapshot subvolume: %m");
143 r
= btrfs_subvol_remove("/xxxrectest", BTRFS_REMOVE_QUOTA
|BTRFS_REMOVE_RECURSIVE
);
145 log_error_errno(r
, "Failed to recursively remove subvolume: %m");
147 r
= btrfs_subvol_remove("/xxxrectest2", BTRFS_REMOVE_QUOTA
|BTRFS_REMOVE_RECURSIVE
);
149 log_error_errno(r
, "Failed to recursively remove subvolume: %m");
151 r
= btrfs_subvol_make("/xxxquotatest");
153 log_error_errno(r
, "Failed to make subvolume: %m");
155 r
= btrfs_subvol_auto_qgroup("/xxxquotatest", 0, true);
157 log_error_errno(r
, "Failed to set up auto qgroup: %m");
159 r
= btrfs_subvol_make("/xxxquotatest/beneath");
161 log_error_errno(r
, "Failed to make subvolume: %m");
163 r
= btrfs_subvol_auto_qgroup("/xxxquotatest/beneath", 0, false);
165 log_error_errno(r
, "Failed to set up auto qgroup: %m");
167 r
= btrfs_qgroup_set_limit("/xxxquotatest/beneath", 0, 4ULL * 1024 * 1024 * 1024);
169 log_error_errno(r
, "Failed to set up quota limit: %m");
171 r
= btrfs_subvol_set_subtree_quota_limit("/xxxquotatest", 0, 5ULL * 1024 * 1024 * 1024);
173 log_error_errno(r
, "Failed to set up quota limit: %m");
175 r
= btrfs_subvol_snapshot("/xxxquotatest", "/xxxquotatest2", BTRFS_SNAPSHOT_RECURSIVE
|BTRFS_SNAPSHOT_QUOTA
);
177 log_error_errno(r
, "Failed to setup snapshot: %m");
179 r
= btrfs_qgroup_get_quota("/xxxquotatest2/beneath", 0, "a
);
181 log_error_errno(r
, "Failed to query quota: %m");
183 assert_se(quota
.referenced_max
== 4ULL * 1024 * 1024 * 1024);
185 r
= btrfs_subvol_get_subtree_quota("/xxxquotatest2", 0, "a
);
187 log_error_errno(r
, "Failed to query quota: %m");
189 assert_se(quota
.referenced_max
== 5ULL * 1024 * 1024 * 1024);
191 r
= btrfs_subvol_remove("/xxxquotatest", BTRFS_REMOVE_QUOTA
|BTRFS_REMOVE_RECURSIVE
);
193 log_error_errno(r
, "Failed remove subvolume: %m");
195 r
= btrfs_subvol_remove("/xxxquotatest2", BTRFS_REMOVE_QUOTA
|BTRFS_REMOVE_RECURSIVE
);
197 log_error_errno(r
, "Failed remove subvolume: %m");