]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/storage/storage_utils.c
storage: remove unused function
[mirror_lxc.git] / src / lxc / storage / storage_utils.c
1 /*
2 * lxc: linux Container library
3 *
4 * Copyright © 2017 Canonical Ltd.
5 *
6 * Authors:
7 * Christian Brauner <christian.brauner@ubuntu.com>
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24 #ifndef _GNU_SOURCE
25 #define _GNU_SOURCE 1
26 #endif
27 #include <ctype.h>
28 #include <dirent.h>
29 #include <errno.h>
30 #include <fcntl.h>
31 #include <grp.h>
32 #include <inttypes.h>
33 #include <libgen.h>
34 #include <sched.h>
35 #include <stdint.h>
36 #include <stdio.h>
37 #include <string.h>
38 #include <sys/mount.h>
39 #include <sys/prctl.h>
40 #include <sys/stat.h>
41 #include <sys/types.h>
42 #include <sys/wait.h>
43 #include <unistd.h>
44
45 #include "config.h"
46 #include "log.h"
47 #include "nbd.h"
48 #include "parse.h"
49 #include "storage.h"
50 #include "storage_utils.h"
51 #include "syscall_wrappers.h"
52 #include "utils.h"
53
54 #ifndef HAVE_STRLCPY
55 #include "include/strlcpy.h"
56 #endif
57
58 #ifndef BLKGETSIZE64
59 #define BLKGETSIZE64 _IOR(0x12, 114, size_t)
60 #endif
61
62 lxc_log_define(storage_utils, lxc);
63
64 /*
65 * attach_block_device returns true if all went well,
66 * meaning either a block device was attached or was not
67 * needed. It returns false if something went wrong and
68 * container startup should be stopped.
69 */
70 bool attach_block_device(struct lxc_conf *conf)
71 {
72 char *path;
73
74 if (!conf->rootfs.path)
75 return true;
76
77 path = conf->rootfs.path;
78 if (!requires_nbd(path))
79 return true;
80
81 path = strchr(path, ':');
82 if (!path)
83 return false;
84
85 path++;
86 if (!attach_nbd(path, conf))
87 return false;
88
89 return true;
90 }
91
92 /*
93 * return block size of dev->src in units of bytes
94 */
95 int blk_getsize(struct lxc_storage *bdev, uint64_t *size)
96 {
97 int fd, ret;
98 const char *src;
99
100 src = lxc_storage_get_path(bdev->src, bdev->type);
101
102 fd = open(src, O_RDONLY | O_CLOEXEC);
103 if (fd < 0) {
104 SYSERROR("Failed to open \"%s\"", src);
105 return -1;
106 }
107
108 /* size of device in bytes */
109 ret = ioctl(fd, BLKGETSIZE64, size);
110 if (ret < 0)
111 SYSERROR("Failed to get block size of dev-src");
112
113 close(fd);
114 return ret;
115 }
116
117 void detach_block_device(struct lxc_conf *conf)
118 {
119 if (conf->nbd_idx != -1)
120 detach_nbd_idx(conf->nbd_idx);
121 }
122
123 /*
124 * Given a lxc_storage (presumably blockdev-based), detect the fstype
125 * by trying mounting (in a private mntns) it.
126 * @lxc_storage: bdev to investigate
127 * @type: preallocated char* in which to write the fstype
128 * @len: length of passed in char*
129 * Returns length of fstype, of -1 on error
130 */
131 int detect_fs(struct lxc_storage *bdev, char *type, int len)
132 {
133 int ret;
134 int p[2];
135 size_t linelen;
136 pid_t pid;
137 FILE *f;
138 char *sp1, *sp2, *sp3;
139 const char *l, *srcdev;
140 char devpath[PATH_MAX];
141 char *line = NULL;
142
143 if (!bdev || !bdev->src || !bdev->dest)
144 return -1;
145
146 srcdev = lxc_storage_get_path(bdev->src, bdev->type);
147
148 ret = pipe(p);
149 if (ret < 0) {
150 SYSERROR("Failed to create pipe");
151 return -1;
152 }
153
154 pid = fork();
155 if (pid < 0) {
156 SYSERROR("Failed to fork process");
157 return -1;
158 }
159
160 if (pid > 0) {
161 int status;
162
163 close(p[1]);
164 memset(type, 0, len);
165
166 ret = read(p[0], type, len - 1);
167 if (ret < 0) {
168 SYSERROR("Failed to read FSType from pipe");
169 } else if (ret == 0) {
170 ERROR("FSType not found - child exited early");
171 ret = -1;
172 }
173
174 close(p[0]);
175 wait(&status);
176
177 if (ret < 0)
178 return ret;
179
180 type[len - 1] = '\0';
181 INFO("Detected FSType \"%s\" for \"%s\"", type, srcdev);
182
183 return ret;
184 }
185
186 if (unshare(CLONE_NEWNS) < 0)
187 _exit(EXIT_FAILURE);
188
189 if (detect_shared_rootfs())
190 if (mount(NULL, "/", NULL, MS_SLAVE | MS_REC, NULL)) {
191 SYSERROR("Failed to make / rslave");
192 ERROR("Continuing...");
193 }
194
195 ret = mount_unknown_fs(srcdev, bdev->dest, bdev->mntopts);
196 if (ret < 0) {
197 ERROR("Failed to mount \"%s\" onto \"%s\" to detect FSType", srcdev,
198 bdev->dest);
199 _exit(EXIT_FAILURE);
200 }
201
202 l = linkderef(srcdev, devpath);
203 if (!l)
204 _exit(EXIT_FAILURE);
205
206 f = fopen("/proc/self/mounts", "r");
207 if (!f)
208 _exit(EXIT_FAILURE);
209
210 while (getline(&line, &linelen, f) != -1) {
211 sp1 = strchr(line, ' ');
212 if (!sp1)
213 _exit(EXIT_FAILURE);
214
215 *sp1 = '\0';
216 if (strcmp(line, l))
217 continue;
218
219 sp2 = strchr(sp1 + 1, ' ');
220 if (!sp2)
221 _exit(EXIT_FAILURE);
222 *sp2 = '\0';
223
224 sp3 = strchr(sp2 + 1, ' ');
225 if (!sp3)
226 _exit(EXIT_FAILURE);
227 *sp3 = '\0';
228
229 sp2++;
230 if (write(p[1], sp2, strlen(sp2)) != strlen(sp2))
231 _exit(EXIT_FAILURE);
232
233 _exit(EXIT_SUCCESS);
234 }
235
236 _exit(EXIT_FAILURE);
237 }
238
239 int do_mkfs_exec_wrapper(void *args)
240 {
241 int ret;
242 char *mkfs;
243 char **data = args;
244 /* strlen("mkfs.")
245 * +
246 * strlen(data[0])
247 * +
248 * \0
249 */
250 size_t len = 5 + strlen(data[0]) + 1;
251
252 mkfs = malloc(len);
253 if (!mkfs)
254 return -1;
255
256 ret = snprintf(mkfs, len, "mkfs.%s", data[0]);
257 if (ret < 0 || (size_t)ret >= len) {
258 free(mkfs);
259 return -1;
260 }
261
262 TRACE("Executing \"%s %s\"", mkfs, data[1]);
263 execlp(mkfs, mkfs, data[1], (char *)NULL);
264
265 SYSERROR("Failed to run \"%s %s\"", mkfs, data[1]);
266 free(mkfs);
267
268 return -1;
269 }
270
271 /*
272 * This will return 1 for physical disks, qemu-nbd, loop, etc right now only lvm
273 * is a block device.
274 */
275 int is_blktype(struct lxc_storage *b)
276 {
277 if (strcmp(b->type, "lvm") == 0)
278 return 1;
279
280 return 0;
281 }
282
283 int mount_unknown_fs(const char *rootfs, const char *target,
284 const char *options)
285 {
286 size_t i;
287 int ret;
288 struct cbarg {
289 const char *rootfs;
290 const char *target;
291 const char *options;
292 } cbarg = {
293 .rootfs = rootfs,
294 .target = target,
295 .options = options,
296 };
297
298 /*
299 * find the filesystem type with brute force:
300 * first we check with /etc/filesystems, in case the modules
301 * are auto-loaded and fall back to the supported kernel fs
302 */
303 char *fsfile[] = {
304 "/etc/filesystems",
305 "/proc/filesystems",
306 };
307
308 for (i = 0; i < sizeof(fsfile) / sizeof(fsfile[0]); i++) {
309 if (access(fsfile[i], F_OK))
310 continue;
311
312 ret = lxc_file_for_each_line(fsfile[i], find_fstype_cb, &cbarg);
313 if (ret < 0) {
314 ERROR("Failed to parse \"%s\"", fsfile[i]);
315 return -1;
316 }
317
318 if (ret)
319 return 0;
320 }
321
322 ERROR("Failed to determine FSType for \"%s\"", rootfs);
323
324 return -1;
325 }
326
327 /*
328 * These are copied from conf.c. However as conf.c will be moved to using
329 * the callback system, they can be pulled from there eventually, so we
330 * don't need to pollute utils.c with these low level functions
331 */
332 int find_fstype_cb(char *buffer, void *data)
333 {
334 struct cbarg {
335 const char *rootfs;
336 const char *target;
337 const char *options;
338 } *cbarg = data;
339
340 unsigned long mntflags;
341 char *mntdata;
342 char *fstype;
343
344 /* we don't try 'nodev' entries */
345 if (strstr(buffer, "nodev"))
346 return 0;
347
348 fstype = buffer;
349 fstype += lxc_char_left_gc(fstype, strlen(fstype));
350 fstype[lxc_char_right_gc(fstype, strlen(fstype))] = '\0';
351
352 DEBUG("Trying to mount \"%s\"->\"%s\" with FSType \"%s\"", cbarg->rootfs,
353 cbarg->target, fstype);
354
355 if (parse_mntopts(cbarg->options, &mntflags, &mntdata) < 0) {
356 free(mntdata);
357 return 0;
358 }
359
360 if (mount(cbarg->rootfs, cbarg->target, fstype, mntflags, mntdata)) {
361 SYSDEBUG("Failed to mount");
362 free(mntdata);
363 return 0;
364 }
365
366 free(mntdata);
367
368 INFO("Mounted \"%s\" on \"%s\", with FSType \"%s\"", cbarg->rootfs,
369 cbarg->target, fstype);
370
371 return 1;
372 }
373
374 const char *linkderef(const char *path, char *dest)
375 {
376 struct stat sbuf;
377 ssize_t ret;
378
379 ret = stat(path, &sbuf);
380 if (ret < 0) {
381 SYSERROR("Failed to get status of file - \"%s\"", path);
382 return NULL;
383 }
384
385 if (!S_ISLNK(sbuf.st_mode))
386 return path;
387
388 ret = readlink(path, dest, PATH_MAX);
389 if (ret < 0) {
390 SYSERROR("Failed to read link of \"%s\"", path);
391 return NULL;
392 } else if (ret >= PATH_MAX) {
393 ERROR("The name of link of \"%s\" is too long", path);
394 return NULL;
395 }
396 dest[ret] = '\0';
397
398 return dest;
399 }
400
401 /*
402 * is an unprivileged user allowed to make this kind of snapshot
403 */
404 bool unpriv_snap_allowed(struct lxc_storage *b, const char *t, bool snap,
405 bool maybesnap)
406 {
407 if (!t) {
408 /* New type will be same as original (unless snap && b->type ==
409 * dir, in which case it will be overlayfs -- which is also
410 * allowed).
411 */
412 if (strcmp(b->type, "dir") == 0 ||
413 strcmp(b->type, "overlay") == 0 ||
414 strcmp(b->type, "overlayfs") == 0 ||
415 strcmp(b->type, "btrfs") == 0 ||
416 strcmp(b->type, "loop") == 0)
417 return true;
418
419 return false;
420 }
421
422 /* Unprivileged users can copy and snapshot dir, overlayfs, and loop.
423 * In particular, not zfs, btrfs, or lvm.
424 */
425 if (strcmp(t, "dir") == 0 ||
426 strcmp(t, "overlay") == 0 ||
427 strcmp(t, "overlayfs") == 0 ||
428 strcmp(t, "btrfs") == 0 ||
429 strcmp(t, "loop") == 0)
430 return true;
431
432 return false;
433 }
434
435 uint64_t get_fssize(char *s)
436 {
437 uint64_t ret;
438 char *end;
439
440 ret = strtoull(s, &end, 0);
441 if (end == s) {
442 ERROR("Invalid blockdev size '%s', using default size", s);
443 return 0;
444 }
445
446 while (isblank(*end))
447 end++;
448
449 if (*end == '\0') {
450 ret *= 1024ULL * 1024ULL; /* MB by default */
451 } else if (*end == 'b' || *end == 'B') {
452 ret *= 1ULL;
453 } else if (*end == 'k' || *end == 'K') {
454 ret *= 1024ULL;
455 } else if (*end == 'm' || *end == 'M') {
456 ret *= 1024ULL * 1024ULL;
457 } else if (*end == 'g' || *end == 'G') {
458 ret *= 1024ULL * 1024ULL * 1024ULL;
459 } else if (*end == 't' || *end == 'T') {
460 ret *= 1024ULL * 1024ULL * 1024ULL * 1024ULL;
461 } else {
462 ERROR("Invalid blockdev unit size '%c' in '%s', using default size", *end, s);
463 return 0;
464 }
465
466 return ret;
467 }
468
469 bool is_valid_storage_type(const char *type)
470 {
471 if (strcmp(type, "dir") == 0 ||
472 strcmp(type, "btrfs") == 0 ||
473 strcmp(type, "loop") == 0 ||
474 strcmp(type, "lvm") == 0 ||
475 strcmp(type, "nbd") == 0 ||
476 strcmp(type, "overlay") == 0 ||
477 strcmp(type, "overlayfs") == 0 ||
478 strcmp(type, "rbd") == 0 ||
479 strcmp(type, "zfs") == 0)
480 return true;
481
482 return false;
483 }
484
485 int storage_destroy_wrapper(void *data)
486 {
487 struct lxc_conf *conf = data;
488
489 if (setgid(0) < 0) {
490 SYSERROR("Failed to setgid to 0");
491 return -1;
492 }
493
494 if (setgroups(0, NULL) < 0)
495 SYSWARN("Failed to clear groups");
496
497 if (setuid(0) < 0) {
498 SYSERROR("Failed to setuid to 0");
499 return -1;
500 }
501
502 if (!storage_destroy(conf)) {
503 ERROR("Failed to destroy storage");
504 return -1;
505 }
506
507 return 0;
508 }