test-btrfs.c revision 10f9c75519671e7c7ab8993b54fe22da7c2d0c38
d7c7c334f56edab8cfc102b657366277a65738cfLennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
d7c7c334f56edab8cfc102b657366277a65738cfLennart Poettering
d7c7c334f56edab8cfc102b657366277a65738cfLennart Poettering/***
d7c7c334f56edab8cfc102b657366277a65738cfLennart Poettering This file is part of systemd.
d7c7c334f56edab8cfc102b657366277a65738cfLennart Poettering
d7c7c334f56edab8cfc102b657366277a65738cfLennart Poettering Copyright 2014 Lennart Poettering
d7c7c334f56edab8cfc102b657366277a65738cfLennart Poettering
d7c7c334f56edab8cfc102b657366277a65738cfLennart Poettering systemd is free software; you can redistribute it and/or modify it
d7c7c334f56edab8cfc102b657366277a65738cfLennart Poettering under the terms of the GNU Lesser General Public License as published by
d7c7c334f56edab8cfc102b657366277a65738cfLennart Poettering the Free Software Foundation; either version 2.1 of the License, or
d7c7c334f56edab8cfc102b657366277a65738cfLennart Poettering (at your option) any later version.
d7c7c334f56edab8cfc102b657366277a65738cfLennart Poettering
d7c7c334f56edab8cfc102b657366277a65738cfLennart Poettering systemd is distributed in the hope that it will be useful, but
d7c7c334f56edab8cfc102b657366277a65738cfLennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
d7c7c334f56edab8cfc102b657366277a65738cfLennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
d7c7c334f56edab8cfc102b657366277a65738cfLennart Poettering Lesser General Public License for more details.
d7c7c334f56edab8cfc102b657366277a65738cfLennart Poettering
d7c7c334f56edab8cfc102b657366277a65738cfLennart Poettering You should have received a copy of the GNU Lesser General Public License
d7c7c334f56edab8cfc102b657366277a65738cfLennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
d7c7c334f56edab8cfc102b657366277a65738cfLennart Poettering***/
d7c7c334f56edab8cfc102b657366277a65738cfLennart Poettering
d7c7c334f56edab8cfc102b657366277a65738cfLennart Poettering#include <stdlib.h>
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering#include <fcntl.h>
d7c7c334f56edab8cfc102b657366277a65738cfLennart Poettering
d7c7c334f56edab8cfc102b657366277a65738cfLennart Poettering#include "log.h"
d7c7c334f56edab8cfc102b657366277a65738cfLennart Poettering#include "fileio.h"
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering#include "util.h"
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering#include "btrfs-util.h"
d7c7c334f56edab8cfc102b657366277a65738cfLennart Poettering
d7c7c334f56edab8cfc102b657366277a65738cfLennart Poetteringint main(int argc, char *argv[]) {
d7c7c334f56edab8cfc102b657366277a65738cfLennart Poettering int r;
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering BtrfsSubvolInfo info;
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering char ts[FORMAT_TIMESTAMP_MAX];
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering int fd;
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering fd = open("/", O_RDONLY|O_CLOEXEC|O_DIRECTORY);
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering if (fd < 0)
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering log_error_errno(errno, "Failed to open root directory: %m");
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering else {
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering r = btrfs_subvol_get_info_fd(fd, &info);
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering if (r < 0)
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering log_error_errno(r, "Failed to get subvolume info: %m");
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering else {
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering log_info("otime: %s", format_timestamp(ts, sizeof(ts), info.otime));
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering log_info("read-only: %s", yes_no(info.read_only));
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering }
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering r = btrfs_subvol_get_read_only_fd(fd);
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering assert_se(r >= 0);
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering log_info("read-only: %s", yes_no(r));
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering }
d7c7c334f56edab8cfc102b657366277a65738cfLennart Poettering
d7c7c334f56edab8cfc102b657366277a65738cfLennart Poettering r = btrfs_subvol_make("/xxxtest");
d7c7c334f56edab8cfc102b657366277a65738cfLennart Poettering if (r < 0)
d7c7c334f56edab8cfc102b657366277a65738cfLennart Poettering log_error_errno(r, "Failed to make subvolume: %m");
d7c7c334f56edab8cfc102b657366277a65738cfLennart Poettering
d7c7c334f56edab8cfc102b657366277a65738cfLennart Poettering r = write_string_file("/xxxtest/afile", "ljsadhfljasdkfhlkjdsfha");
d7c7c334f56edab8cfc102b657366277a65738cfLennart Poettering if (r < 0)
d7c7c334f56edab8cfc102b657366277a65738cfLennart Poettering log_error_errno(r, "Failed to write file: %m");
d7c7c334f56edab8cfc102b657366277a65738cfLennart Poettering
d7c7c334f56edab8cfc102b657366277a65738cfLennart Poettering r = btrfs_subvol_snapshot("/xxxtest", "/xxxtest2", false, false);
d7c7c334f56edab8cfc102b657366277a65738cfLennart Poettering if (r < 0)
d7c7c334f56edab8cfc102b657366277a65738cfLennart Poettering log_error_errno(r, "Failed to make snapshot: %m");
d7c7c334f56edab8cfc102b657366277a65738cfLennart Poettering
d7c7c334f56edab8cfc102b657366277a65738cfLennart Poettering r = btrfs_subvol_snapshot("/xxxtest", "/xxxtest3", true, false);
d7c7c334f56edab8cfc102b657366277a65738cfLennart Poettering if (r < 0)
d7c7c334f56edab8cfc102b657366277a65738cfLennart Poettering log_error_errno(r, "Failed to make snapshot: %m");
d7c7c334f56edab8cfc102b657366277a65738cfLennart Poettering
d7c7c334f56edab8cfc102b657366277a65738cfLennart Poettering r = btrfs_subvol_remove("/xxxtest");
d7c7c334f56edab8cfc102b657366277a65738cfLennart Poettering if (r < 0)
d7c7c334f56edab8cfc102b657366277a65738cfLennart Poettering log_error_errno(r, "Failed to remove subvolume: %m");
d7c7c334f56edab8cfc102b657366277a65738cfLennart Poettering
d7c7c334f56edab8cfc102b657366277a65738cfLennart Poettering r = btrfs_subvol_remove("/xxxtest2");
d7c7c334f56edab8cfc102b657366277a65738cfLennart Poettering if (r < 0)
d7c7c334f56edab8cfc102b657366277a65738cfLennart Poettering log_error_errno(r, "Failed to remove subvolume: %m");
d7c7c334f56edab8cfc102b657366277a65738cfLennart Poettering
d7c7c334f56edab8cfc102b657366277a65738cfLennart Poettering r = btrfs_subvol_remove("/xxxtest3");
d7c7c334f56edab8cfc102b657366277a65738cfLennart Poettering if (r < 0)
d7c7c334f56edab8cfc102b657366277a65738cfLennart Poettering log_error_errno(r, "Failed to remove subvolume: %m");
d7c7c334f56edab8cfc102b657366277a65738cfLennart Poettering
d7c7c334f56edab8cfc102b657366277a65738cfLennart Poettering r = btrfs_subvol_snapshot("/etc", "/etc2", true, true);
d7c7c334f56edab8cfc102b657366277a65738cfLennart Poettering if (r < 0)
d7c7c334f56edab8cfc102b657366277a65738cfLennart Poettering log_error_errno(r, "Failed to make snapshot: %m");
d7c7c334f56edab8cfc102b657366277a65738cfLennart Poettering
d7c7c334f56edab8cfc102b657366277a65738cfLennart Poettering r = btrfs_subvol_remove("/etc2");
d7c7c334f56edab8cfc102b657366277a65738cfLennart Poettering if (r < 0)
d7c7c334f56edab8cfc102b657366277a65738cfLennart Poettering log_error_errno(r, "Failed to remove subvolume: %m");
d7c7c334f56edab8cfc102b657366277a65738cfLennart Poettering
d7c7c334f56edab8cfc102b657366277a65738cfLennart Poettering return 0;
d7c7c334f56edab8cfc102b657366277a65738cfLennart Poettering}