chiark / gitweb /
machined: add support for reporting image size via btrfs quota
[elogind.git] / src / test / test-btrfs.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2014 Lennart Poettering
7
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.
12
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.
17
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/>.
20 ***/
21
22 #include <stdlib.h>
23 #include <fcntl.h>
24
25 #include "log.h"
26 #include "fileio.h"
27 #include "util.h"
28 #include "btrfs-util.h"
29
30 int main(int argc, char *argv[]) {
31         int r;
32         int fd;
33
34         fd = open("/", O_RDONLY|O_CLOEXEC|O_DIRECTORY);
35         if (fd < 0)
36                 log_error_errno(errno, "Failed to open root directory: %m");
37         else {
38                 BtrfsSubvolInfo info;
39                 BtrfsQuotaInfo quota;
40                 char ts[FORMAT_TIMESTAMP_MAX], bs[FORMAT_BYTES_MAX];
41
42                 r = btrfs_subvol_get_info_fd(fd, &info);
43                 if (r < 0)
44                         log_error_errno(r, "Failed to get subvolume info: %m");
45                 else {
46                         log_info("otime: %s", format_timestamp(ts, sizeof(ts), info.otime));
47                         log_info("read-only (search): %s", yes_no(info.read_only));
48                 }
49
50                 r = btrfs_subvol_get_quota_fd(fd, &quota);
51                 if (r < 0)
52                         log_error_errno(r, "Failed to get quota info: %m");
53                 else {
54                         log_info("referred: %s", strna(format_bytes(bs, sizeof(bs), quota.referred)));
55                         log_info("exclusive: %s", strna(format_bytes(bs, sizeof(bs), quota.exclusive)));
56                         log_info("referred_max: %s", strna(format_bytes(bs, sizeof(bs), quota.referred_max)));
57                         log_info("exclusive_max: %s", strna(format_bytes(bs, sizeof(bs), quota.exclusive_max)));
58                 }
59
60                 r = btrfs_subvol_get_read_only_fd(fd);
61                 if (r < 0)
62                         log_error_errno(r, "Failed to get read only flag: %m");
63                 else
64                         log_info("read-only (ioctl): %s", yes_no(r));
65
66                 safe_close(fd);
67         }
68
69         r = btrfs_subvol_make("/xxxtest");
70         if (r < 0)
71                 log_error_errno(r, "Failed to make subvolume: %m");
72
73         r = write_string_file("/xxxtest/afile", "ljsadhfljasdkfhlkjdsfha");
74         if (r < 0)
75                 log_error_errno(r, "Failed to write file: %m");
76
77         r = btrfs_subvol_snapshot("/xxxtest", "/xxxtest2", false, false);
78         if (r < 0)
79                 log_error_errno(r, "Failed to make snapshot: %m");
80
81         r = btrfs_subvol_snapshot("/xxxtest", "/xxxtest3", true, false);
82         if (r < 0)
83                 log_error_errno(r, "Failed to make snapshot: %m");
84
85         r = btrfs_subvol_remove("/xxxtest");
86         if (r < 0)
87                 log_error_errno(r, "Failed to remove subvolume: %m");
88
89         r = btrfs_subvol_remove("/xxxtest2");
90         if (r < 0)
91                 log_error_errno(r, "Failed to remove subvolume: %m");
92
93         r = btrfs_subvol_remove("/xxxtest3");
94         if (r < 0)
95                 log_error_errno(r, "Failed to remove subvolume: %m");
96
97         r = btrfs_subvol_snapshot("/etc", "/etc2", true, true);
98         if (r < 0)
99                 log_error_errno(r, "Failed to make snapshot: %m");
100
101         r = btrfs_subvol_remove("/etc2");
102         if (r < 0)
103                 log_error_errno(r, "Failed to remove subvolume: %m");
104
105         return 0;
106 }