]>
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
);
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.
70 bool attach_block_device(struct lxc_conf
*conf
)
74 if (!conf
->rootfs
.path
)
77 path
= conf
->rootfs
.path
;
78 if (!requires_nbd(path
))
81 path
= strchr(path
, ':');
86 if (!attach_nbd(path
, conf
))
93 * return block size of dev->src in units of bytes
95 int blk_getsize(struct lxc_storage
*bdev
, uint64_t *size
)
100 src
= lxc_storage_get_path(bdev
->src
, bdev
->type
);
102 fd
= open(src
, O_RDONLY
| O_CLOEXEC
);
104 SYSERROR("Failed to open \"%s\"", src
);
108 /* size of device in bytes */
109 ret
= ioctl(fd
, BLKGETSIZE64
, size
);
111 SYSERROR("Failed to get block size of dev-src");
117 void detach_block_device(struct lxc_conf
*conf
)
119 if (conf
->nbd_idx
!= -1)
120 detach_nbd_idx(conf
->nbd_idx
);
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
131 int detect_fs(struct lxc_storage
*bdev
, char *type
, int len
)
138 char *sp1
, *sp2
, *sp3
;
139 const char *l
, *srcdev
;
140 char devpath
[PATH_MAX
];
143 if (!bdev
|| !bdev
->src
|| !bdev
->dest
)
146 srcdev
= lxc_storage_get_path(bdev
->src
, bdev
->type
);
150 SYSERROR("Failed to create pipe");
156 SYSERROR("Failed to fork process");
164 memset(type
, 0, len
);
166 ret
= read(p
[0], type
, len
- 1);
168 SYSERROR("Failed to read FSType from pipe");
169 } else if (ret
== 0) {
170 ERROR("FSType not found - child exited early");
180 type
[len
- 1] = '\0';
181 INFO("Detected FSType \"%s\" for \"%s\"", type
, srcdev
);
186 if (unshare(CLONE_NEWNS
) < 0)
189 if (detect_shared_rootfs())
190 if (mount(NULL
, "/", NULL
, MS_SLAVE
| MS_REC
, NULL
)) {
191 SYSERROR("Failed to make / rslave");
192 ERROR("Continuing...");
195 ret
= mount_unknown_fs(srcdev
, bdev
->dest
, bdev
->mntopts
);
197 ERROR("Failed to mount \"%s\" onto \"%s\" to detect FSType", srcdev
,
202 l
= linkderef(srcdev
, devpath
);
206 f
= fopen("/proc/self/mounts", "r");
210 while (getline(&line
, &linelen
, f
) != -1) {
211 sp1
= strchr(line
, ' ');
219 sp2
= strchr(sp1
+ 1, ' ');
224 sp3
= strchr(sp2
+ 1, ' ');
230 if (write(p
[1], sp2
, strlen(sp2
)) != strlen(sp2
))
239 int do_mkfs_exec_wrapper(void *args
)
250 size_t len
= 5 + strlen(data
[0]) + 1;
256 ret
= snprintf(mkfs
, len
, "mkfs.%s", data
[0]);
257 if (ret
< 0 || (size_t)ret
>= len
) {
262 TRACE("Executing \"%s %s\"", mkfs
, data
[1]);
263 execlp(mkfs
, mkfs
, data
[1], (char *)NULL
);
265 SYSERROR("Failed to run \"%s %s\"", mkfs
, data
[1]);
272 * This will return 1 for physical disks, qemu-nbd, loop, etc right now only lvm
275 int is_blktype(struct lxc_storage
*b
)
277 if (strcmp(b
->type
, "lvm") == 0)
283 int mount_unknown_fs(const char *rootfs
, const char *target
,
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
308 for (i
= 0; i
< sizeof(fsfile
) / sizeof(fsfile
[0]); i
++) {
309 if (access(fsfile
[i
], F_OK
))
312 ret
= lxc_file_for_each_line(fsfile
[i
], find_fstype_cb
, &cbarg
);
314 ERROR("Failed to parse \"%s\"", fsfile
[i
]);
322 ERROR("Failed to determine FSType for \"%s\"", rootfs
);
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
332 int find_fstype_cb(char *buffer
, void *data
)
340 unsigned long mntflags
;
344 /* we don't try 'nodev' entries */
345 if (strstr(buffer
, "nodev"))
349 fstype
+= lxc_char_left_gc(fstype
, strlen(fstype
));
350 fstype
[lxc_char_right_gc(fstype
, strlen(fstype
))] = '\0';
352 DEBUG("Trying to mount \"%s\"->\"%s\" with FSType \"%s\"", cbarg
->rootfs
,
353 cbarg
->target
, fstype
);
355 if (parse_mntopts(cbarg
->options
, &mntflags
, &mntdata
) < 0) {
360 if (mount(cbarg
->rootfs
, cbarg
->target
, fstype
, mntflags
, mntdata
)) {
361 SYSDEBUG("Failed to mount");
368 INFO("Mounted \"%s\" on \"%s\", with FSType \"%s\"", cbarg
->rootfs
,
369 cbarg
->target
, fstype
);
374 const char *linkderef(const char *path
, char *dest
)
379 ret
= stat(path
, &sbuf
);
381 SYSERROR("Failed to get status of file - \"%s\"", path
);
385 if (!S_ISLNK(sbuf
.st_mode
))
388 ret
= readlink(path
, dest
, PATH_MAX
);
390 SYSERROR("Failed to read link of \"%s\"", path
);
392 } else if (ret
>= PATH_MAX
) {
393 ERROR("The name of link of \"%s\" is too long", path
);
402 * is an unprivileged user allowed to make this kind of snapshot
404 bool unpriv_snap_allowed(struct lxc_storage
*b
, const char *t
, bool snap
,
408 /* New type will be same as original (unless snap && b->type ==
409 * dir, in which case it will be overlayfs -- which is also
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)
422 /* Unprivileged users can copy and snapshot dir, overlayfs, and loop.
423 * In particular, not zfs, btrfs, or lvm.
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)
435 uint64_t get_fssize(char *s
)
440 ret
= strtoull(s
, &end
, 0);
442 ERROR("Invalid blockdev size '%s', using default size", s
);
446 while (isblank(*end
))
450 ret
*= 1024ULL * 1024ULL; /* MB by default */
451 } else if (*end
== 'b' || *end
== 'B') {
453 } else if (*end
== 'k' || *end
== 'K') {
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;
462 ERROR("Invalid blockdev unit size '%c' in '%s', using default size", *end
, s
);
469 bool is_valid_storage_type(const char *type
)
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)
485 int storage_destroy_wrapper(void *data
)
487 struct lxc_conf
*conf
= data
;
490 SYSERROR("Failed to setgid to 0");
494 if (setgroups(0, NULL
) < 0)
495 SYSWARN("Failed to clear groups");
498 SYSERROR("Failed to setuid to 0");
502 if (!storage_destroy(conf
)) {
503 ERROR("Failed to destroy storage");