]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/storage/storage_utils.c
67da0f9beddca9e48f43edc459aad0d6424a383d
[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 /* the bulk of this needs to become a common helper */
65 char *dir_new_path(char *src, const char *oldname, const char *name,
66 const char *oldpath, const char *lxcpath)
67 {
68 char *ret, *p, *p2;
69 int l1, l2, nlen;
70
71 nlen = strlen(src) + 1;
72 l1 = strlen(oldpath);
73 p = src;
74 /* if src starts with oldpath, look for oldname only after
75 * that path */
76 if (strncmp(src, oldpath, l1) == 0) {
77 p += l1;
78 nlen += (strlen(lxcpath) - l1);
79 }
80 l2 = strlen(oldname);
81 while ((p = strstr(p, oldname)) != NULL) {
82 p += l2;
83 nlen += strlen(name) - l2;
84 }
85
86 ret = malloc(nlen);
87 if (!ret)
88 return NULL;
89
90 p = ret;
91 if (strncmp(src, oldpath, l1) == 0) {
92 p += sprintf(p, "%s", lxcpath);
93 src += l1;
94 }
95
96 while ((p2 = strstr(src, oldname)) != NULL) {
97 size_t retlen;
98
99 /* copy text up to oldname */
100 retlen = strlcpy(p, src, p2 - src);
101 if (retlen >= p2 - src) {
102 free(ret);
103 return NULL;
104 }
105
106 /* move target pointer (p) */
107 p += p2 - src;
108 /* print new name in place of oldname */
109 p += sprintf(p, "%s", name);
110 /* move src to end of oldname */
111 src = p2 + l2;
112 }
113
114 /* copy the rest of src */
115 sprintf(p, "%s", src);
116 return ret;
117 }
118
119 /*
120 * attach_block_device returns true if all went well,
121 * meaning either a block device was attached or was not
122 * needed. It returns false if something went wrong and
123 * container startup should be stopped.
124 */
125 bool attach_block_device(struct lxc_conf *conf)
126 {
127 char *path;
128
129 if (!conf->rootfs.path)
130 return true;
131
132 path = conf->rootfs.path;
133 if (!requires_nbd(path))
134 return true;
135
136 path = strchr(path, ':');
137 if (!path)
138 return false;
139
140 path++;
141 if (!attach_nbd(path, conf))
142 return false;
143
144 return true;
145 }
146
147 /*
148 * return block size of dev->src in units of bytes
149 */
150 int blk_getsize(struct lxc_storage *bdev, uint64_t *size)
151 {
152 int fd, ret;
153 const char *src;
154
155 src = lxc_storage_get_path(bdev->src, bdev->type);
156 fd = open(src, O_RDONLY);
157 if (fd < 0)
158 return -1;
159
160 /* size of device in bytes */
161 ret = ioctl(fd, BLKGETSIZE64, size);
162 close(fd);
163 return ret;
164 }
165
166 void detach_block_device(struct lxc_conf *conf)
167 {
168 if (conf->nbd_idx != -1)
169 detach_nbd_idx(conf->nbd_idx);
170 }
171
172 /*
173 * Given a lxc_storage (presumably blockdev-based), detect the fstype
174 * by trying mounting (in a private mntns) it.
175 * @lxc_storage: bdev to investigate
176 * @type: preallocated char* in which to write the fstype
177 * @len: length of passed in char*
178 * Returns length of fstype, of -1 on error
179 */
180 int detect_fs(struct lxc_storage *bdev, char *type, int len)
181 {
182 int ret;
183 int p[2];
184 size_t linelen;
185 pid_t pid;
186 FILE *f;
187 char *sp1, *sp2, *sp3;
188 const char *l, *srcdev;
189 char devpath[PATH_MAX];
190 char *line = NULL;
191
192 if (!bdev || !bdev->src || !bdev->dest)
193 return -1;
194
195 srcdev = lxc_storage_get_path(bdev->src, bdev->type);
196
197 ret = pipe(p);
198 if (ret < 0)
199 return -1;
200
201 if ((pid = fork()) < 0)
202 return -1;
203
204 if (pid > 0) {
205 int status;
206 close(p[1]);
207 memset(type, 0, len);
208 ret = read(p[0], type, len - 1);
209 close(p[0]);
210 if (ret < 0) {
211 SYSERROR("error reading from pipe");
212 wait(&status);
213 return -1;
214 } else if (ret == 0) {
215 ERROR("child exited early - fstype not found");
216 wait(&status);
217 return -1;
218 }
219 wait(&status);
220 type[len - 1] = '\0';
221 INFO("detected fstype %s for %s", type, srcdev);
222 return ret;
223 }
224
225 if (unshare(CLONE_NEWNS) < 0)
226 exit(1);
227
228 if (detect_shared_rootfs()) {
229 if (mount(NULL, "/", NULL, MS_SLAVE | MS_REC, NULL)) {
230 SYSERROR("Failed to make / rslave");
231 ERROR("Continuing...");
232 }
233 }
234
235 ret = mount_unknown_fs(srcdev, bdev->dest, bdev->mntopts);
236 if (ret < 0) {
237 ERROR("failed mounting %s onto %s to detect fstype", srcdev,
238 bdev->dest);
239 exit(1);
240 }
241
242 l = linkderef(srcdev, devpath);
243 if (!l)
244 exit(1);
245 f = fopen("/proc/self/mounts", "r");
246 if (!f)
247 exit(1);
248
249 while (getline(&line, &linelen, f) != -1) {
250 sp1 = strchr(line, ' ');
251 if (!sp1)
252 exit(1);
253 *sp1 = '\0';
254 if (strcmp(line, l))
255 continue;
256 sp2 = strchr(sp1 + 1, ' ');
257 if (!sp2)
258 exit(1);
259 *sp2 = '\0';
260 sp3 = strchr(sp2 + 1, ' ');
261 if (!sp3)
262 exit(1);
263 *sp3 = '\0';
264 sp2++;
265 if (write(p[1], sp2, strlen(sp2)) != strlen(sp2))
266 exit(1);
267
268 exit(0);
269 }
270
271 exit(1);
272 }
273
274 int do_mkfs_exec_wrapper(void *args)
275 {
276 int ret;
277 char *mkfs;
278 char **data = args;
279 /* strlen("mkfs.")
280 * +
281 * strlen(data[0])
282 * +
283 * \0
284 */
285 size_t len = 5 + strlen(data[0]) + 1;
286
287 mkfs = malloc(len);
288 if (!mkfs)
289 return -1;
290
291 ret = snprintf(mkfs, len, "mkfs.%s", data[0]);
292 if (ret < 0 || (size_t)ret >= len) {
293 free(mkfs);
294 return -1;
295 }
296
297 TRACE("executing \"%s %s\"", mkfs, data[1]);
298 execlp(mkfs, mkfs, data[1], (char *)NULL);
299 SYSERROR("failed to run \"%s %s \"", mkfs, data[1]);
300 free(mkfs);
301 return -1;
302 }
303
304 /*
305 * This will return 1 for physical disks, qemu-nbd, loop, etc right now only lvm
306 * is a block device.
307 */
308 int is_blktype(struct lxc_storage *b)
309 {
310 if (strcmp(b->type, "lvm") == 0)
311 return 1;
312
313 return 0;
314 }
315
316 int mount_unknown_fs(const char *rootfs, const char *target,
317 const char *options)
318 {
319 size_t i;
320 int ret;
321 struct cbarg {
322 const char *rootfs;
323 const char *target;
324 const char *options;
325 } cbarg = {
326 .rootfs = rootfs,
327 .target = target,
328 .options = options,
329 };
330
331 /*
332 * find the filesystem type with brute force:
333 * first we check with /etc/filesystems, in case the modules
334 * are auto-loaded and fall back to the supported kernel fs
335 */
336 char *fsfile[] = {
337 "/etc/filesystems",
338 "/proc/filesystems",
339 };
340
341 for (i = 0; i < sizeof(fsfile) / sizeof(fsfile[0]); i++) {
342 if (access(fsfile[i], F_OK))
343 continue;
344
345 ret = lxc_file_for_each_line(fsfile[i], find_fstype_cb, &cbarg);
346 if (ret < 0) {
347 ERROR("failed to parse '%s'", fsfile[i]);
348 return -1;
349 }
350
351 if (ret)
352 return 0;
353 }
354
355 ERROR("failed to determine fs type for '%s'", rootfs);
356 return -1;
357 }
358
359 /*
360 * These are copied from conf.c. However as conf.c will be moved to using
361 * the callback system, they can be pulled from there eventually, so we
362 * don't need to pollute utils.c with these low level functions
363 */
364 int find_fstype_cb(char *buffer, void *data)
365 {
366 struct cbarg {
367 const char *rootfs;
368 const char *target;
369 const char *options;
370 } *cbarg = data;
371
372 unsigned long mntflags;
373 char *mntdata;
374 char *fstype;
375
376 /* we don't try 'nodev' entries */
377 if (strstr(buffer, "nodev"))
378 return 0;
379
380 fstype = buffer;
381 fstype += lxc_char_left_gc(fstype, strlen(fstype));
382 fstype[lxc_char_right_gc(fstype, strlen(fstype))] = '\0';
383
384 DEBUG("trying to mount '%s'->'%s' with fstype '%s'", cbarg->rootfs,
385 cbarg->target, fstype);
386
387 if (parse_mntopts(cbarg->options, &mntflags, &mntdata) < 0) {
388 free(mntdata);
389 return 0;
390 }
391
392 if (mount(cbarg->rootfs, cbarg->target, fstype, mntflags, mntdata)) {
393 SYSDEBUG("mount failed with error");
394 free(mntdata);
395 return 0;
396 }
397
398 free(mntdata);
399
400 INFO("mounted '%s' on '%s', with fstype '%s'", cbarg->rootfs,
401 cbarg->target, fstype);
402
403 return 1;
404 }
405
406 const char *linkderef(const char *path, char *dest)
407 {
408 struct stat sbuf;
409 ssize_t ret;
410
411 ret = stat(path, &sbuf);
412 if (ret < 0)
413 return NULL;
414
415 if (!S_ISLNK(sbuf.st_mode))
416 return path;
417
418 ret = readlink(path, dest, PATH_MAX);
419 if (ret < 0) {
420 SYSERROR("error reading link %s", path);
421 return NULL;
422 } else if (ret >= PATH_MAX) {
423 ERROR("link in %s too long", path);
424 return NULL;
425 }
426 dest[ret] = '\0';
427
428 return dest;
429 }
430
431 /*
432 * is an unprivileged user allowed to make this kind of snapshot
433 */
434 bool unpriv_snap_allowed(struct lxc_storage *b, const char *t, bool snap,
435 bool maybesnap)
436 {
437 if (!t) {
438 /* New type will be same as original (unless snap && b->type ==
439 * dir, in which case it will be overlayfs -- which is also
440 * allowed).
441 */
442 if (strcmp(b->type, "dir") == 0 ||
443 strcmp(b->type, "overlay") == 0 ||
444 strcmp(b->type, "overlayfs") == 0 ||
445 strcmp(b->type, "btrfs") == 0 ||
446 strcmp(b->type, "loop") == 0)
447 return true;
448
449 return false;
450 }
451
452 /* Unprivileged users can copy and snapshot dir, overlayfs, and loop.
453 * In particular, not zfs, btrfs, or lvm.
454 */
455 if (strcmp(t, "dir") == 0 ||
456 strcmp(t, "overlay") == 0 ||
457 strcmp(t, "overlayfs") == 0 ||
458 strcmp(t, "btrfs") == 0 ||
459 strcmp(t, "loop") == 0)
460 return true;
461
462 return false;
463 }
464
465 uint64_t get_fssize(char *s)
466 {
467 uint64_t ret;
468 char *end;
469
470 ret = strtoull(s, &end, 0);
471 if (end == s) {
472 ERROR("Invalid blockdev size '%s', using default size", s);
473 return 0;
474 }
475
476 while (isblank(*end))
477 end++;
478
479 if (*end == '\0') {
480 ret *= 1024ULL * 1024ULL; /* MB by default */
481 } else if (*end == 'b' || *end == 'B') {
482 ret *= 1ULL;
483 } else if (*end == 'k' || *end == 'K') {
484 ret *= 1024ULL;
485 } else if (*end == 'm' || *end == 'M') {
486 ret *= 1024ULL * 1024ULL;
487 } else if (*end == 'g' || *end == 'G') {
488 ret *= 1024ULL * 1024ULL * 1024ULL;
489 } else if (*end == 't' || *end == 'T') {
490 ret *= 1024ULL * 1024ULL * 1024ULL * 1024ULL;
491 } else {
492 ERROR("Invalid blockdev unit size '%c' in '%s', using default size", *end, s);
493 return 0;
494 }
495
496 return ret;
497 }
498
499 bool is_valid_storage_type(const char *type)
500 {
501 if (strcmp(type, "dir") == 0 ||
502 strcmp(type, "btrfs") == 0 ||
503 strcmp(type, "loop") == 0 ||
504 strcmp(type, "lvm") == 0 ||
505 strcmp(type, "nbd") == 0 ||
506 strcmp(type, "overlay") == 0 ||
507 strcmp(type, "overlayfs") == 0 ||
508 strcmp(type, "rbd") == 0 ||
509 strcmp(type, "zfs") == 0)
510 return true;
511
512 return false;
513 }
514
515 int storage_destroy_wrapper(void *data)
516 {
517 struct lxc_conf *conf = data;
518
519 if (setgid(0) < 0) {
520 ERROR("Failed to setgid to 0");
521 return -1;
522 }
523
524 if (setgroups(0, NULL) < 0)
525 WARN("Failed to clear groups");
526
527 if (setuid(0) < 0) {
528 ERROR("Failed to setuid to 0");
529 return -1;
530 }
531
532 if (!storage_destroy(conf))
533 return -1;
534
535 return 0;
536 }