]>
git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/storage/storage_utils.c
2 * lxc: linux Container library
4 * Copyright © 2017 Canonical Ltd.
7 * Christian Brauner <christian.brauner@ubuntu.com>
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.
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.
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
38 #include <sys/mount.h>
39 #include <sys/prctl.h>
41 #include <sys/types.h>
50 #include "storage_utils.h"
51 #include "syscall_wrappers.h"
55 #include "include/strlcpy.h"
59 #define BLKGETSIZE64 _IOR(0x12, 114, size_t)
62 lxc_log_define(storage_utils
, lxc
);
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
)
71 nlen
= strlen(src
) + 1;
74 /* if src starts with oldpath, look for oldname only after
76 if (strncmp(src
, oldpath
, l1
) == 0) {
78 nlen
+= (strlen(lxcpath
) - l1
);
81 while ((p
= strstr(p
, oldname
)) != NULL
) {
83 nlen
+= strlen(name
) - l2
;
91 if (strncmp(src
, oldpath
, l1
) == 0) {
92 p
+= sprintf(p
, "%s", lxcpath
);
96 while ((p2
= strstr(src
, oldname
)) != NULL
) {
99 /* copy text up to oldname */
100 retlen
= strlcpy(p
, src
, p2
- src
);
101 if (retlen
>= p2
- src
) {
106 /* move target pointer (p) */
108 /* print new name in place of oldname */
109 p
+= sprintf(p
, "%s", name
);
110 /* move src to end of oldname */
114 /* copy the rest of src */
115 sprintf(p
, "%s", src
);
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.
125 bool attach_block_device(struct lxc_conf
*conf
)
129 if (!conf
->rootfs
.path
)
132 path
= conf
->rootfs
.path
;
133 if (!requires_nbd(path
))
136 path
= strchr(path
, ':');
141 if (!attach_nbd(path
, conf
))
148 * return block size of dev->src in units of bytes
150 int blk_getsize(struct lxc_storage
*bdev
, uint64_t *size
)
155 src
= lxc_storage_get_path(bdev
->src
, bdev
->type
);
157 fd
= open(src
, O_RDONLY
| O_CLOEXEC
);
159 SYSERROR("Failed to open \"%s\"", src
);
163 /* size of device in bytes */
164 ret
= ioctl(fd
, BLKGETSIZE64
, size
);
166 SYSERROR("Failed to get block size of dev-src");
172 void detach_block_device(struct lxc_conf
*conf
)
174 if (conf
->nbd_idx
!= -1)
175 detach_nbd_idx(conf
->nbd_idx
);
179 * Given a lxc_storage (presumably blockdev-based), detect the fstype
180 * by trying mounting (in a private mntns) it.
181 * @lxc_storage: bdev to investigate
182 * @type: preallocated char* in which to write the fstype
183 * @len: length of passed in char*
184 * Returns length of fstype, of -1 on error
186 int detect_fs(struct lxc_storage
*bdev
, char *type
, int len
)
193 char *sp1
, *sp2
, *sp3
;
194 const char *l
, *srcdev
;
195 char devpath
[PATH_MAX
];
198 if (!bdev
|| !bdev
->src
|| !bdev
->dest
)
201 srcdev
= lxc_storage_get_path(bdev
->src
, bdev
->type
);
205 SYSERROR("Failed to create pipe");
211 SYSERROR("Failed to fork process");
218 memset(type
, 0, len
);
219 ret
= read(p
[0], type
, len
- 1);
222 SYSERROR("error reading from pipe");
225 } else if (ret
== 0) {
226 ERROR("child exited early - fstype not found");
231 type
[len
- 1] = '\0';
232 INFO("detected fstype %s for %s", type
, srcdev
);
236 if (unshare(CLONE_NEWNS
) < 0)
239 if (detect_shared_rootfs()) {
240 if (mount(NULL
, "/", NULL
, MS_SLAVE
| MS_REC
, NULL
)) {
241 SYSERROR("Failed to make / rslave");
242 ERROR("Continuing...");
246 ret
= mount_unknown_fs(srcdev
, bdev
->dest
, bdev
->mntopts
);
248 ERROR("failed mounting %s onto %s to detect fstype", srcdev
,
253 l
= linkderef(srcdev
, devpath
);
256 f
= fopen("/proc/self/mounts", "r");
260 while (getline(&line
, &linelen
, f
) != -1) {
261 sp1
= strchr(line
, ' ');
267 sp2
= strchr(sp1
+ 1, ' ');
271 sp3
= strchr(sp2
+ 1, ' ');
276 if (write(p
[1], sp2
, strlen(sp2
)) != strlen(sp2
))
285 int do_mkfs_exec_wrapper(void *args
)
296 size_t len
= 5 + strlen(data
[0]) + 1;
302 ret
= snprintf(mkfs
, len
, "mkfs.%s", data
[0]);
303 if (ret
< 0 || (size_t)ret
>= len
) {
308 TRACE("executing \"%s %s\"", mkfs
, data
[1]);
309 execlp(mkfs
, mkfs
, data
[1], (char *)NULL
);
310 SYSERROR("failed to run \"%s %s \"", mkfs
, data
[1]);
316 * This will return 1 for physical disks, qemu-nbd, loop, etc right now only lvm
319 int is_blktype(struct lxc_storage
*b
)
321 if (strcmp(b
->type
, "lvm") == 0)
327 int mount_unknown_fs(const char *rootfs
, const char *target
,
343 * find the filesystem type with brute force:
344 * first we check with /etc/filesystems, in case the modules
345 * are auto-loaded and fall back to the supported kernel fs
352 for (i
= 0; i
< sizeof(fsfile
) / sizeof(fsfile
[0]); i
++) {
353 if (access(fsfile
[i
], F_OK
))
356 ret
= lxc_file_for_each_line(fsfile
[i
], find_fstype_cb
, &cbarg
);
358 ERROR("failed to parse '%s'", fsfile
[i
]);
366 ERROR("failed to determine fs type for '%s'", rootfs
);
371 * These are copied from conf.c. However as conf.c will be moved to using
372 * the callback system, they can be pulled from there eventually, so we
373 * don't need to pollute utils.c with these low level functions
375 int find_fstype_cb(char *buffer
, void *data
)
383 unsigned long mntflags
;
387 /* we don't try 'nodev' entries */
388 if (strstr(buffer
, "nodev"))
392 fstype
+= lxc_char_left_gc(fstype
, strlen(fstype
));
393 fstype
[lxc_char_right_gc(fstype
, strlen(fstype
))] = '\0';
395 DEBUG("trying to mount '%s'->'%s' with fstype '%s'", cbarg
->rootfs
,
396 cbarg
->target
, fstype
);
398 if (parse_mntopts(cbarg
->options
, &mntflags
, &mntdata
) < 0) {
403 if (mount(cbarg
->rootfs
, cbarg
->target
, fstype
, mntflags
, mntdata
)) {
404 SYSDEBUG("mount failed with error");
411 INFO("mounted '%s' on '%s', with fstype '%s'", cbarg
->rootfs
,
412 cbarg
->target
, fstype
);
417 const char *linkderef(const char *path
, char *dest
)
422 ret
= stat(path
, &sbuf
);
424 SYSERROR("Failed to get status of file - \"%s\"", path
);
428 if (!S_ISLNK(sbuf
.st_mode
))
431 ret
= readlink(path
, dest
, PATH_MAX
);
433 SYSERROR("error reading link %s", path
);
435 } else if (ret
>= PATH_MAX
) {
436 ERROR("link in %s too long", path
);
445 * is an unprivileged user allowed to make this kind of snapshot
447 bool unpriv_snap_allowed(struct lxc_storage
*b
, const char *t
, bool snap
,
451 /* New type will be same as original (unless snap && b->type ==
452 * dir, in which case it will be overlayfs -- which is also
455 if (strcmp(b
->type
, "dir") == 0 ||
456 strcmp(b
->type
, "overlay") == 0 ||
457 strcmp(b
->type
, "overlayfs") == 0 ||
458 strcmp(b
->type
, "btrfs") == 0 ||
459 strcmp(b
->type
, "loop") == 0)
465 /* Unprivileged users can copy and snapshot dir, overlayfs, and loop.
466 * In particular, not zfs, btrfs, or lvm.
468 if (strcmp(t
, "dir") == 0 ||
469 strcmp(t
, "overlay") == 0 ||
470 strcmp(t
, "overlayfs") == 0 ||
471 strcmp(t
, "btrfs") == 0 ||
472 strcmp(t
, "loop") == 0)
478 uint64_t get_fssize(char *s
)
483 ret
= strtoull(s
, &end
, 0);
485 ERROR("Invalid blockdev size '%s', using default size", s
);
489 while (isblank(*end
))
493 ret
*= 1024ULL * 1024ULL; /* MB by default */
494 } else if (*end
== 'b' || *end
== 'B') {
496 } else if (*end
== 'k' || *end
== 'K') {
498 } else if (*end
== 'm' || *end
== 'M') {
499 ret
*= 1024ULL * 1024ULL;
500 } else if (*end
== 'g' || *end
== 'G') {
501 ret
*= 1024ULL * 1024ULL * 1024ULL;
502 } else if (*end
== 't' || *end
== 'T') {
503 ret
*= 1024ULL * 1024ULL * 1024ULL * 1024ULL;
505 ERROR("Invalid blockdev unit size '%c' in '%s', using default size", *end
, s
);
512 bool is_valid_storage_type(const char *type
)
514 if (strcmp(type
, "dir") == 0 ||
515 strcmp(type
, "btrfs") == 0 ||
516 strcmp(type
, "loop") == 0 ||
517 strcmp(type
, "lvm") == 0 ||
518 strcmp(type
, "nbd") == 0 ||
519 strcmp(type
, "overlay") == 0 ||
520 strcmp(type
, "overlayfs") == 0 ||
521 strcmp(type
, "rbd") == 0 ||
522 strcmp(type
, "zfs") == 0)
528 int storage_destroy_wrapper(void *data
)
530 struct lxc_conf
*conf
= data
;
533 SYSERROR("Failed to setgid to 0");
537 if (setgroups(0, NULL
) < 0)
538 SYSWARN("Failed to clear groups");
541 SYSERROR("Failed to setuid to 0");
545 if (!storage_destroy(conf
)) {
546 ERROR("Failed to destroy storage");