]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/shared/machine-pool.c
tree-wide: drop redundant _cleanup_ macros (#8810)
[thirdparty/systemd.git] / src / shared / machine-pool.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
432cea00
LP
2/***
3 This file is part of systemd.
4
5 Copyright 2015 Lennart Poettering
432cea00
LP
6***/
7
a8fbdf54
TA
8#include <errno.h>
9#include <fcntl.h>
10#include <linux/loop.h>
11#include <signal.h>
12#include <stdbool.h>
13#include <stdio.h>
14#include <stdlib.h>
e306723e 15#include <sys/file.h>
a8fbdf54 16#include <sys/ioctl.h>
07630cea 17#include <sys/mount.h>
432cea00 18#include <sys/prctl.h>
a8fbdf54
TA
19#include <sys/stat.h>
20#include <sys/statfs.h>
432cea00 21#include <sys/statvfs.h>
a8fbdf54
TA
22#include <unistd.h>
23
24#include "sd-bus-protocol.h"
25#include "sd-bus.h"
432cea00 26
b5efdb8a 27#include "alloc-util.h"
07630cea 28#include "btrfs-util.h"
3ffd4af2 29#include "fd-util.h"
0d39fa9c 30#include "fileio.h"
f4f15635 31#include "fs-util.h"
de2e28d8 32#include "label.h"
cd2eb9e9 33#include "lockfile-util.h"
a8fbdf54 34#include "log.h"
3ffd4af2 35#include "machine-pool.h"
a8fbdf54
TA
36#include "macro.h"
37#include "missing.h"
432cea00 38#include "mkdir.h"
f4f15635 39#include "mount-util.h"
6bedfcbb 40#include "parse-util.h"
432cea00 41#include "path-util.h"
07630cea 42#include "process-util.h"
24882e06 43#include "signal-util.h"
8fcde012 44#include "stat-util.h"
07630cea 45#include "string-util.h"
432cea00
LP
46
47#define VAR_LIB_MACHINES_SIZE_START (1024UL*1024UL*500UL)
48#define VAR_LIB_MACHINES_FREE_MIN (1024UL*1024UL*750UL)
49
50static int check_btrfs(void) {
51 struct statfs sfs;
52
53 if (statfs("/var/lib/machines", &sfs) < 0) {
54 if (errno != ENOENT)
55 return -errno;
56
57 if (statfs("/var/lib", &sfs) < 0)
58 return -errno;
59 }
60
61 return F_TYPE_EQUAL(sfs.f_type, BTRFS_SUPER_MAGIC);
62}
63
4cee5eed 64static int setup_machine_raw(uint64_t size, sd_bus_error *error) {
432cea00
LP
65 _cleanup_free_ char *tmp = NULL;
66 _cleanup_close_ int fd = -1;
67 struct statvfs ss;
68 pid_t pid = 0;
432cea00
LP
69 int r;
70
71 /* We want to be able to make use of btrfs-specific file
72 * system features, in particular subvolumes, reflinks and
73 * quota. Hence, if we detect that /var/lib/machines.raw is
74 * not located on btrfs, let's create a loopback file, place a
75 * btrfs file system into it, and mount it to
76 * /var/lib/machines. */
77
78 fd = open("/var/lib/machines.raw", O_RDWR|O_CLOEXEC|O_NONBLOCK|O_NOCTTY);
c10d6bdb
LP
79 if (fd >= 0)
80 return TAKE_FD(fd);
432cea00
LP
81
82 if (errno != ENOENT)
83 return sd_bus_error_set_errnof(error, errno, "Failed to open /var/lib/machines.raw: %m");
84
14bcf25c 85 r = tempfn_xxxxxx("/var/lib/machines.raw", NULL, &tmp);
432cea00
LP
86 if (r < 0)
87 return r;
88
89 (void) mkdir_p_label("/var/lib", 0755);
90 fd = open(tmp, O_RDWR|O_CREAT|O_EXCL|O_NOCTTY|O_CLOEXEC, 0600);
91 if (fd < 0)
92 return sd_bus_error_set_errnof(error, errno, "Failed to create /var/lib/machines.raw: %m");
93
94 if (fstatvfs(fd, &ss) < 0) {
95 r = sd_bus_error_set_errnof(error, errno, "Failed to determine free space on /var/lib/machines.raw: %m");
96 goto fail;
97 }
98
99 if (ss.f_bsize * ss.f_bavail < VAR_LIB_MACHINES_FREE_MIN) {
100 r = sd_bus_error_setf(error, SD_BUS_ERROR_FAILED, "Not enough free disk space to set up /var/lib/machines.");
101 goto fail;
102 }
103
4cee5eed 104 if (ftruncate(fd, size) < 0) {
432cea00
LP
105 r = sd_bus_error_set_errnof(error, errno, "Failed to enlarge /var/lib/machines.raw: %m");
106 goto fail;
107 }
108
4c253ed1
LP
109 r = safe_fork("(mkfs)", FORK_RESET_SIGNALS|FORK_DEATHSIG, &pid);
110 if (r < 0) {
111 sd_bus_error_set_errnof(error, r, "Failed to fork mkfs.btrfs: %m");
432cea00
LP
112 goto fail;
113 }
4c253ed1 114 if (r == 0) {
432cea00
LP
115
116 /* Child */
117
432cea00
LP
118 fd = safe_close(fd);
119
120 execlp("mkfs.btrfs", "-Lvar-lib-machines", tmp, NULL);
121 if (errno == ENOENT)
579afbea 122 _exit(99);
432cea00
LP
123
124 _exit(EXIT_FAILURE);
125 }
126
2e87a1fd 127 r = wait_for_terminate_and_check("mkfs", pid, 0);
432cea00
LP
128 pid = 0;
129
2e87a1fd
LP
130 if (r < 0) {
131 sd_bus_error_set_errnof(error, r, "Failed to wait for mkfs.btrfs: %m");
432cea00
LP
132 goto fail;
133 }
2e87a1fd 134 if (r == 99) {
432cea00
LP
135 r = sd_bus_error_set_errnof(error, ENOENT, "Cannot set up /var/lib/machines, mkfs.btrfs is missing");
136 goto fail;
137 }
2e87a1fd
LP
138 if (r != EXIT_SUCCESS) {
139 r = sd_bus_error_setf(error, SD_BUS_ERROR_FAILED, "mkfs.btrfs failed with error code %i", r);
432cea00
LP
140 goto fail;
141 }
142
f85ef957
AC
143 r = rename_noreplace(AT_FDCWD, tmp, AT_FDCWD, "/var/lib/machines.raw");
144 if (r < 0) {
145 sd_bus_error_set_errnof(error, r, "Failed to move /var/lib/machines.raw into place: %m");
432cea00
LP
146 goto fail;
147 }
148
c10d6bdb 149 return TAKE_FD(fd);
432cea00
LP
150
151fail:
132764a2 152 unlink_noerrno(tmp);
432cea00
LP
153
154 if (pid > 1)
155 kill_and_sigcont(pid, SIGKILL);
156
157 return r;
158}
159
4cee5eed 160int setup_machine_directory(uint64_t size, sd_bus_error *error) {
8e766630 161 _cleanup_(release_lock_file) LockFile lock_file = LOCK_FILE_INIT;
432cea00
LP
162 struct loop_info64 info = {
163 .lo_flags = LO_FLAGS_AUTOCLEAR,
164 };
165 _cleanup_close_ int fd = -1, control = -1, loop = -1;
166 _cleanup_free_ char* loopdev = NULL;
5bcd08db 167 char tmpdir[] = "/tmp/machine-pool.XXXXXX", *mntdir = NULL;
432cea00 168 bool tmpdir_made = false, mntdir_made = false, mntdir_mounted = false;
26166c88 169 char buf[FORMAT_BYTES_MAX];
432cea00
LP
170 int r, nr = -1;
171
4cee5eed
LP
172 /* btrfs cannot handle file systems < 16M, hence use this as minimum */
173 if (size == (uint64_t) -1)
174 size = VAR_LIB_MACHINES_SIZE_START;
175 else if (size < 16*1024*1024)
176 size = 16*1024*1024;
177
403e5b32
LP
178 /* Make sure we only set the directory up once at a time */
179 r = make_lock_file("/run/systemd/machines.lock", LOCK_EX, &lock_file);
180 if (r < 0)
181 return r;
182
432cea00
LP
183 r = check_btrfs();
184 if (r < 0)
185 return sd_bus_error_set_errnof(error, r, "Failed to determine whether /var/lib/machines is located on btrfs: %m");
186 if (r > 0) {
187 (void) btrfs_subvol_make_label("/var/lib/machines");
188
189 r = btrfs_quota_enable("/var/lib/machines", true);
190 if (r < 0)
5bcd08db 191 log_warning_errno(r, "Failed to enable quota for /var/lib/machines, ignoring: %m");
432cea00 192
5bcd08db
LP
193 r = btrfs_subvol_auto_qgroup("/var/lib/machines", 0, true);
194 if (r < 0)
195 log_warning_errno(r, "Failed to set up default quota hierarchy for /var/lib/machines, ignoring: %m");
196
197 return 1;
198 }
199
e1873695 200 if (path_is_mount_point("/var/lib/machines", NULL, AT_SYMLINK_FOLLOW) > 0) {
5bcd08db 201 log_debug("/var/lib/machines is already a mount point, not creating loopback file for it.");
432cea00
LP
202 return 0;
203 }
204
5bcd08db
LP
205 r = dir_is_populated("/var/lib/machines");
206 if (r < 0 && r != -ENOENT)
207 return r;
208 if (r > 0) {
209 log_debug("/var/log/machines is already populated, not creating loopback file for it.");
210 return 0;
211 }
212
213 r = mkfs_exists("btrfs");
c3b0e5ac
EV
214 if (r == 0)
215 return sd_bus_error_set_errnof(error, ENOENT, "Cannot set up /var/lib/machines, mkfs.btrfs is missing");
5bcd08db
LP
216 if (r < 0)
217 return r;
432cea00 218
4cee5eed 219 fd = setup_machine_raw(size, error);
432cea00
LP
220 if (fd < 0)
221 return fd;
222
223 control = open("/dev/loop-control", O_RDWR|O_CLOEXEC|O_NOCTTY|O_NONBLOCK);
224 if (control < 0)
225 return sd_bus_error_set_errnof(error, errno, "Failed to open /dev/loop-control: %m");
226
227 nr = ioctl(control, LOOP_CTL_GET_FREE);
228 if (nr < 0)
229 return sd_bus_error_set_errnof(error, errno, "Failed to allocate loop device: %m");
230
231 if (asprintf(&loopdev, "/dev/loop%i", nr) < 0) {
232 r = -ENOMEM;
233 goto fail;
234 }
235
236 loop = open(loopdev, O_CLOEXEC|O_RDWR|O_NOCTTY|O_NONBLOCK);
237 if (loop < 0) {
238 r = sd_bus_error_set_errnof(error, errno, "Failed to open loopback device: %m");
239 goto fail;
240 }
241
242 if (ioctl(loop, LOOP_SET_FD, fd) < 0) {
243 r = sd_bus_error_set_errnof(error, errno, "Failed to bind loopback device: %m");
244 goto fail;
245 }
246
247 if (ioctl(loop, LOOP_SET_STATUS64, &info) < 0) {
248 r = sd_bus_error_set_errnof(error, errno, "Failed to enable auto-clear for loopback device: %m");
249 goto fail;
250 }
251
252 /* We need to make sure the new /var/lib/machines directory
253 * has an access mode of 0700 at the time it is first made
254 * available. mkfs will create it with 0755 however. Hence,
255 * let's mount the directory into an inaccessible directory
256 * below /tmp first, fix the access mode, and move it to the
257 * public place then. */
258
259 if (!mkdtemp(tmpdir)) {
260 r = sd_bus_error_set_errnof(error, errno, "Failed to create temporary mount parent directory: %m");
261 goto fail;
262 }
263 tmpdir_made = true;
264
265 mntdir = strjoina(tmpdir, "/mnt");
266 if (mkdir(mntdir, 0700) < 0) {
267 r = sd_bus_error_set_errnof(error, errno, "Failed to create temporary mount directory: %m");
268 goto fail;
269 }
270 mntdir_made = true;
271
272 if (mount(loopdev, mntdir, "btrfs", 0, NULL) < 0) {
273 r = sd_bus_error_set_errnof(error, errno, "Failed to mount loopback device: %m");
274 goto fail;
275 }
276 mntdir_mounted = true;
277
278 r = btrfs_quota_enable(mntdir, true);
279 if (r < 0)
280 log_warning_errno(r, "Failed to enable quota, ignoring: %m");
281
5bcd08db
LP
282 r = btrfs_subvol_auto_qgroup(mntdir, 0, true);
283 if (r < 0)
284 log_warning_errno(r, "Failed to set up default quota hierarchy, ignoring: %m");
285
432cea00
LP
286 if (chmod(mntdir, 0700) < 0) {
287 r = sd_bus_error_set_errnof(error, errno, "Failed to fix owner: %m");
288 goto fail;
289 }
290
291 (void) mkdir_p_label("/var/lib/machines", 0700);
292
293 if (mount(mntdir, "/var/lib/machines", NULL, MS_BIND, NULL) < 0) {
294 r = sd_bus_error_set_errnof(error, errno, "Failed to mount directory into right place: %m");
295 goto fail;
296 }
297
26166c88
LP
298 (void) syncfs(fd);
299
300 log_info("Set up /var/lib/machines as btrfs loopback file system of size %s mounted on /var/lib/machines.raw.", format_bytes(buf, sizeof(buf), size));
301
432cea00
LP
302 (void) umount2(mntdir, MNT_DETACH);
303 (void) rmdir(mntdir);
304 (void) rmdir(tmpdir);
305
5bcd08db 306 return 1;
432cea00
LP
307
308fail:
309 if (mntdir_mounted)
310 (void) umount2(mntdir, MNT_DETACH);
311
312 if (mntdir_made)
313 (void) rmdir(mntdir);
314 if (tmpdir_made)
315 (void) rmdir(tmpdir);
316
317 if (loop >= 0) {
318 (void) ioctl(loop, LOOP_CLR_FD);
319 loop = safe_close(loop);
320 }
321
322 if (control >= 0 && nr >= 0)
323 (void) ioctl(control, LOOP_CTL_REMOVE, nr);
324
325 return r;
326}
26166c88
LP
327
328static int sync_path(const char *p) {
329 _cleanup_close_ int fd = -1;
330
331 fd = open(p, O_RDONLY|O_CLOEXEC|O_NOCTTY);
332 if (fd < 0)
333 return -errno;
334
335 if (syncfs(fd) < 0)
336 return -errno;
337
338 return 0;
339}
340
341int grow_machine_directory(void) {
342 char buf[FORMAT_BYTES_MAX];
343 struct statvfs a, b;
344 uint64_t old_size, new_size, max_add;
345 int r;
346
347 /* Ensure the disk space data is accurate */
348 sync_path("/var/lib/machines");
349 sync_path("/var/lib/machines.raw");
350
351 if (statvfs("/var/lib/machines.raw", &a) < 0)
352 return -errno;
353
354 if (statvfs("/var/lib/machines", &b) < 0)
355 return -errno;
356
357 /* Don't grow if not enough disk space is available on the host */
358 if (((uint64_t) a.f_bavail * (uint64_t) a.f_bsize) <= VAR_LIB_MACHINES_FREE_MIN)
359 return 0;
360
361 /* Don't grow if at least 1/3th of the fs is still free */
362 if (b.f_bavail > b.f_blocks / 3)
363 return 0;
364
a8eaaee7 365 /* Calculate how much we are willing to add at most */
26166c88
LP
366 max_add = ((uint64_t) a.f_bavail * (uint64_t) a.f_bsize) - VAR_LIB_MACHINES_FREE_MIN;
367
368 /* Calculate the old size */
369 old_size = (uint64_t) b.f_blocks * (uint64_t) b.f_bsize;
370
371 /* Calculate the new size as three times the size of what is used right now */
372 new_size = ((uint64_t) b.f_blocks - (uint64_t) b.f_bavail) * (uint64_t) b.f_bsize * 3;
373
374 /* Always, grow at least to the start size */
375 if (new_size < VAR_LIB_MACHINES_SIZE_START)
376 new_size = VAR_LIB_MACHINES_SIZE_START;
377
378 /* If the new size is smaller than the old size, don't grow */
379 if (new_size < old_size)
380 return 0;
381
382 /* Ensure we never add more than the maximum */
383 if (new_size > old_size + max_add)
384 new_size = old_size + max_add;
385
386 r = btrfs_resize_loopback("/var/lib/machines", new_size, true);
387 if (r <= 0)
388 return r;
389
5bcd08db
LP
390 /* Also bump the quota, of both the subvolume leaf qgroup, as
391 * well as of any subtree quota group by the same id but a
392 * higher level, if it exists. */
393 (void) btrfs_qgroup_set_limit("/var/lib/machines", 0, new_size);
394 (void) btrfs_subvol_set_subtree_quota_limit("/var/lib/machines", 0, new_size);
26166c88
LP
395
396 log_info("Grew /var/lib/machines btrfs loopback file system to %s.", format_bytes(buf, sizeof(buf), new_size));
397 return 1;
398}