]>
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
36 #include <sys/mount.h>
37 #include <sys/prctl.h>
38 #include <sys/types.h>
46 #include "storage_utils.h"
50 #define BLKGETSIZE64 _IOR(0x12, 114, size_t)
53 lxc_log_define(storage_utils
, lxc
);
55 /* the bulk of this needs to become a common helper */
56 char *dir_new_path(char *src
, const char *oldname
, const char *name
,
57 const char *oldpath
, const char *lxcpath
)
62 nlen
= strlen(src
) + 1;
65 /* if src starts with oldpath, look for oldname only after
67 if (strncmp(src
, oldpath
, l1
) == 0) {
69 nlen
+= (strlen(lxcpath
) - l1
);
72 while ((p
= strstr(p
, oldname
)) != NULL
) {
74 nlen
+= strlen(name
) - l2
;
82 if (strncmp(src
, oldpath
, l1
) == 0) {
83 p
+= sprintf(p
, "%s", lxcpath
);
87 while ((p2
= strstr(src
, oldname
)) != NULL
) {
88 /* copy text up to oldname */
89 strncpy(p
, src
, p2
- src
);
90 /* move target pointer (p) */
92 /* print new name in place of oldname */
93 p
+= sprintf(p
, "%s", name
);
94 /* move src to end of oldname */
97 /* copy the rest of src */
98 sprintf(p
, "%s", src
);
103 * attach_block_device returns true if all went well,
104 * meaning either a block device was attached or was not
105 * needed. It returns false if something went wrong and
106 * container startup should be stopped.
108 bool attach_block_device(struct lxc_conf
*conf
)
112 if (!conf
->rootfs
.path
)
115 path
= conf
->rootfs
.path
;
116 if (!requires_nbd(path
))
119 path
= strchr(path
, ':');
124 if (!attach_nbd(path
, conf
))
131 * return block size of dev->src in units of bytes
133 int blk_getsize(struct lxc_storage
*bdev
, uint64_t *size
)
138 src
= lxc_storage_get_path(bdev
->src
, bdev
->type
);
139 fd
= open(src
, O_RDONLY
);
143 /* size of device in bytes */
144 ret
= ioctl(fd
, BLKGETSIZE64
, size
);
149 void detach_block_device(struct lxc_conf
*conf
)
151 if (conf
->nbd_idx
!= -1)
152 detach_nbd_idx(conf
->nbd_idx
);
156 * Given a lxc_storage (presumably blockdev-based), detect the fstype
157 * by trying mounting (in a private mntns) it.
158 * @lxc_storage: bdev to investigate
159 * @type: preallocated char* in which to write the fstype
160 * @len: length of passed in char*
161 * Returns length of fstype, of -1 on error
163 int detect_fs(struct lxc_storage
*bdev
, char *type
, int len
)
170 char *sp1
, *sp2
, *sp3
;
171 const char *l
, *srcdev
;
172 char devpath
[MAXPATHLEN
];
175 if (!bdev
|| !bdev
->src
|| !bdev
->dest
)
178 srcdev
= lxc_storage_get_path(bdev
->src
, bdev
->type
);
184 if ((pid
= fork()) < 0)
190 memset(type
, 0, len
);
191 ret
= read(p
[0], type
, len
- 1);
194 SYSERROR("error reading from pipe");
197 } else if (ret
== 0) {
198 ERROR("child exited early - fstype not found");
203 type
[len
- 1] = '\0';
204 INFO("detected fstype %s for %s", type
, srcdev
);
208 if (unshare(CLONE_NEWNS
) < 0)
211 if (detect_shared_rootfs()) {
212 if (mount(NULL
, "/", NULL
, MS_SLAVE
| MS_REC
, NULL
)) {
213 SYSERROR("Failed to make / rslave");
214 ERROR("Continuing...");
218 ret
= mount_unknown_fs(srcdev
, bdev
->dest
, bdev
->mntopts
);
220 ERROR("failed mounting %s onto %s to detect fstype", srcdev
,
225 l
= linkderef(srcdev
, devpath
);
228 f
= fopen("/proc/self/mounts", "r");
232 while (getline(&line
, &linelen
, f
) != -1) {
233 sp1
= strchr(line
, ' ');
239 sp2
= strchr(sp1
+ 1, ' ');
243 sp3
= strchr(sp2
+ 1, ' ');
248 if (write(p
[1], sp2
, strlen(sp2
)) != strlen(sp2
))
257 int do_mkfs_exec_wrapper(void *args
)
268 size_t len
= 5 + strlen(data
[0]) + 1;
274 ret
= snprintf(mkfs
, len
, "mkfs.%s", data
[0]);
275 if (ret
< 0 || (size_t)ret
>= len
) {
280 TRACE("executing \"%s %s\"", mkfs
, data
[1]);
281 execlp(mkfs
, mkfs
, data
[1], (char *)NULL
);
282 SYSERROR("failed to run \"%s %s \"", mkfs
, data
[1]);
288 * This will return 1 for physical disks, qemu-nbd, loop, etc right now only lvm
291 int is_blktype(struct lxc_storage
*b
)
293 if (strcmp(b
->type
, "lvm") == 0)
299 int mount_unknown_fs(const char *rootfs
, const char *target
,
315 * find the filesystem type with brute force:
316 * first we check with /etc/filesystems, in case the modules
317 * are auto-loaded and fall back to the supported kernel fs
324 for (i
= 0; i
< sizeof(fsfile
) / sizeof(fsfile
[0]); i
++) {
325 if (access(fsfile
[i
], F_OK
))
328 ret
= lxc_file_for_each_line(fsfile
[i
], find_fstype_cb
, &cbarg
);
330 ERROR("failed to parse '%s'", fsfile
[i
]);
338 ERROR("failed to determine fs type for '%s'", rootfs
);
343 * These are copied from conf.c. However as conf.c will be moved to using
344 * the callback system, they can be pulled from there eventually, so we
345 * don't need to pollute utils.c with these low level functions
347 int find_fstype_cb(char *buffer
, void *data
)
355 unsigned long mntflags
;
359 /* we don't try 'nodev' entries */
360 if (strstr(buffer
, "nodev"))
364 fstype
+= lxc_char_left_gc(fstype
, strlen(fstype
));
365 fstype
[lxc_char_right_gc(fstype
, strlen(fstype
))] = '\0';
367 DEBUG("trying to mount '%s'->'%s' with fstype '%s'", cbarg
->rootfs
,
368 cbarg
->target
, fstype
);
370 if (parse_mntopts(cbarg
->options
, &mntflags
, &mntdata
) < 0) {
375 if (mount(cbarg
->rootfs
, cbarg
->target
, fstype
, mntflags
, mntdata
)) {
376 DEBUG("mount failed with error: %s", strerror(errno
));
383 INFO("mounted '%s' on '%s', with fstype '%s'", cbarg
->rootfs
,
384 cbarg
->target
, fstype
);
389 const char *linkderef(const char *path
, char *dest
)
394 ret
= stat(path
, &sbuf
);
398 if (!S_ISLNK(sbuf
.st_mode
))
401 ret
= readlink(path
, dest
, MAXPATHLEN
);
403 SYSERROR("error reading link %s", path
);
405 } else if (ret
>= MAXPATHLEN
) {
406 ERROR("link in %s too long", path
);
415 * is an unprivileged user allowed to make this kind of snapshot
417 bool unpriv_snap_allowed(struct lxc_storage
*b
, const char *t
, bool snap
,
421 /* New type will be same as original (unless snap && b->type ==
422 * dir, in which case it will be overlayfs -- which is also
425 if (strcmp(b
->type
, "dir") == 0 ||
426 strcmp(b
->type
, "aufs") == 0 ||
427 strcmp(b
->type
, "overlay") == 0 ||
428 strcmp(b
->type
, "overlayfs") == 0 ||
429 strcmp(b
->type
, "btrfs") == 0 ||
430 strcmp(b
->type
, "loop") == 0)
436 /* Unprivileged users can copy and snapshot dir, overlayfs, and loop.
437 * In particular, not zfs, btrfs, or lvm.
439 if (strcmp(t
, "dir") == 0 ||
440 strcmp(t
, "aufs") == 0 ||
441 strcmp(t
, "overlay") == 0 ||
442 strcmp(t
, "overlayfs") == 0 ||
443 strcmp(t
, "btrfs") == 0 ||
444 strcmp(t
, "loop") == 0)
450 bool is_valid_storage_type(const char *type
)
452 if (strcmp(type
, "dir") == 0 ||
453 strcmp(type
, "btrfs") == 0 ||
454 strcmp(type
, "aufs") == 0 ||
455 strcmp(type
, "loop") == 0 ||
456 strcmp(type
, "lvm") == 0 ||
457 strcmp(type
, "nbd") == 0 ||
458 strcmp(type
, "overlay") == 0 ||
459 strcmp(type
, "overlayfs") == 0 ||
460 strcmp(type
, "rbd") == 0 ||
461 strcmp(type
, "zfs") == 0)
467 int storage_destroy_wrapper(void *data
)
469 struct lxc_conf
*conf
= data
;
472 ERROR("Failed to setgid to 0");
476 if (setgroups(0, NULL
) < 0)
477 WARN("Failed to clear groups");
480 ERROR("Failed to setuid to 0");
484 if (!storage_destroy(conf
))