machine-pool.c revision 4cee5eede280b7fd48c18a1942616c4ac896a554
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2015 Lennart Poettering
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include "util.h"
#include "mkdir.h"
#include "btrfs-util.h"
#include "path-util.h"
#include "machine-pool.h"
static int check_btrfs(void) {
return -errno;
return -errno;
}
}
int r;
/* We want to be able to make use of btrfs-specific file
* system features, in particular subvolumes, reflinks and
* quota. Hence, if we detect that /var/lib/machines.raw is
* not located on btrfs, let's create a loopback file, place a
* btrfs file system into it, and mount it to
if (fd >= 0) {
r = fd;
fd = -1;
return r;
}
if (r < 0)
return r;
if (fd < 0)
r = sd_bus_error_set_errnof(error, errno, "Failed to determine free space on /var/lib/machines.raw: %m");
goto fail;
}
r = sd_bus_error_setf(error, SD_BUS_ERROR_FAILED, "Not enough free disk space to set up /var/lib/machines.");
goto fail;
}
goto fail;
}
if (pid < 0) {
goto fail;
}
if (pid == 0) {
/* Child */
return 99;
}
if (r < 0) {
goto fail;
}
pid = 0;
goto fail;
}
r = sd_bus_error_set_errnof(error, ENOENT, "Cannot set up /var/lib/machines, mkfs.btrfs is missing");
goto fail;
}
r = sd_bus_error_setf(error, SD_BUS_ERROR_FAILED, "mkfs.btrfs failed with error code %i", si.si_status);
goto fail;
}
goto fail;
}
r = fd;
fd = -1;
return r;
fail:
if (tmp)
if (pid > 1)
return r;
}
struct loop_info64 info = {
};
int r, nr = -1;
/* btrfs cannot handle file systems < 16M, hence use this as minimum */
/* Make sure we only set the directory up once at a time */
if (r < 0)
return r;
r = check_btrfs();
if (r < 0)
return sd_bus_error_set_errnof(error, r, "Failed to determine whether /var/lib/machines is located on btrfs: %m");
if (r > 0) {
(void) btrfs_subvol_make_label("/var/lib/machines");
r = btrfs_quota_enable("/var/lib/machines", true);
if (r < 0)
log_warning_errno(r, "Failed to enable quota, ignoring: %m");
return 0;
}
if (path_is_mount_point("/var/lib/machines", true) > 0 ||
dir_is_empty("/var/lib/machines") == 0)
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "/var/lib/machines is not a btrfs file system. Operation is not supported on legacy file systems.");
if (fd < 0)
return fd;
if (control < 0)
if (nr < 0)
r = -ENOMEM;
goto fail;
}
if (loop < 0) {
goto fail;
}
goto fail;
}
goto fail;
}
* has an access mode of 0700 at the time it is first made
* available. mkfs will create it with 0755 however. Hence,
* let's mount the directory into an inaccessible directory
* below /tmp first, fix the access mode, and move it to the
* public place then. */
goto fail;
}
tmpdir_made = true;
goto fail;
}
mntdir_made = true;
goto fail;
}
mntdir_mounted = true;
r = btrfs_quota_enable(mntdir, true);
if (r < 0)
log_warning_errno(r, "Failed to enable quota, ignoring: %m");
goto fail;
}
goto fail;
}
return 0;
fail:
if (mntdir_mounted)
if (mntdir_made)
if (tmpdir_made)
if (loop >= 0) {
}
return r;
}