]>
git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/storage/storage_utils.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
17 #include <sys/mount.h>
18 #include <sys/prctl.h>
20 #include <sys/types.h>
29 #include "storage_utils.h"
30 #include "syscall_wrappers.h"
34 #include "include/strlcpy.h"
38 #define BLKGETSIZE64 _IOR(0x12, 114, size_t)
41 lxc_log_define(storage_utils
, lxc
);
44 * attach_block_device returns true if all went well,
45 * meaning either a block device was attached or was not
46 * needed. It returns false if something went wrong and
47 * container startup should be stopped.
49 bool attach_block_device(struct lxc_conf
*conf
)
53 if (!conf
->rootfs
.path
)
56 path
= conf
->rootfs
.path
;
57 if (!requires_nbd(path
))
60 path
= strchr(path
, ':');
65 if (!attach_nbd(path
, conf
))
72 * return block size of dev->src in units of bytes
74 int blk_getsize(struct lxc_storage
*bdev
, uint64_t *size
)
79 src
= lxc_storage_get_path(bdev
->src
, bdev
->type
);
81 fd
= open(src
, O_RDONLY
| O_CLOEXEC
);
83 SYSERROR("Failed to open \"%s\"", src
);
87 /* size of device in bytes */
88 ret
= ioctl(fd
, BLKGETSIZE64
, size
);
90 SYSERROR("Failed to get block size of dev-src");
96 void detach_block_device(struct lxc_conf
*conf
)
98 if (conf
->nbd_idx
!= -1)
99 detach_nbd_idx(conf
->nbd_idx
);
103 * Given a lxc_storage (presumably blockdev-based), detect the fstype
104 * by trying mounting (in a private mntns) it.
105 * @lxc_storage: bdev to investigate
106 * @type: preallocated char* in which to write the fstype
107 * @len: length of passed in char*
108 * Returns length of fstype, of -1 on error
110 int detect_fs(struct lxc_storage
*bdev
, char *type
, int len
)
117 char *sp1
, *sp2
, *sp3
;
118 const char *l
, *srcdev
;
119 char devpath
[PATH_MAX
];
122 if (!bdev
|| !bdev
->src
|| !bdev
->dest
)
125 srcdev
= lxc_storage_get_path(bdev
->src
, bdev
->type
);
129 SYSERROR("Failed to create pipe");
135 SYSERROR("Failed to fork process");
143 memset(type
, 0, len
);
145 ret
= read(p
[0], type
, len
- 1);
147 SYSERROR("Failed to read FSType from pipe");
148 } else if (ret
== 0) {
149 ERROR("FSType not found - child exited early");
159 type
[len
- 1] = '\0';
160 INFO("Detected FSType \"%s\" for \"%s\"", type
, srcdev
);
165 if (unshare(CLONE_NEWNS
) < 0)
168 if (detect_shared_rootfs())
169 if (mount(NULL
, "/", NULL
, MS_SLAVE
| MS_REC
, NULL
)) {
170 SYSERROR("Failed to make / rslave");
171 ERROR("Continuing...");
174 ret
= mount_unknown_fs(srcdev
, bdev
->dest
, bdev
->mntopts
);
176 ERROR("Failed to mount \"%s\" onto \"%s\" to detect FSType", srcdev
,
181 l
= linkderef(srcdev
, devpath
);
185 f
= fopen("/proc/self/mounts", "r");
189 while (getline(&line
, &linelen
, f
) != -1) {
190 sp1
= strchr(line
, ' ');
198 sp2
= strchr(sp1
+ 1, ' ');
203 sp3
= strchr(sp2
+ 1, ' ');
209 if (write(p
[1], sp2
, strlen(sp2
)) != strlen(sp2
))
218 int do_mkfs_exec_wrapper(void *args
)
229 size_t len
= 5 + strlen(data
[0]) + 1;
235 ret
= snprintf(mkfs
, len
, "mkfs.%s", data
[0]);
236 if (ret
< 0 || (size_t)ret
>= len
) {
241 TRACE("Executing \"%s %s\"", mkfs
, data
[1]);
242 execlp(mkfs
, mkfs
, data
[1], (char *)NULL
);
244 SYSERROR("Failed to run \"%s %s\"", mkfs
, data
[1]);
251 * This will return 1 for physical disks, qemu-nbd, loop, etc right now only lvm
254 int is_blktype(struct lxc_storage
*b
)
256 if (strcmp(b
->type
, "lvm") == 0)
262 int mount_unknown_fs(const char *rootfs
, const char *target
,
278 * find the filesystem type with brute force:
279 * first we check with /etc/filesystems, in case the modules
280 * are auto-loaded and fall back to the supported kernel fs
287 for (i
= 0; i
< sizeof(fsfile
) / sizeof(fsfile
[0]); i
++) {
288 if (access(fsfile
[i
], F_OK
))
291 ret
= lxc_file_for_each_line(fsfile
[i
], find_fstype_cb
, &cbarg
);
293 ERROR("Failed to parse \"%s\"", fsfile
[i
]);
301 ERROR("Failed to determine FSType for \"%s\"", rootfs
);
307 * These are copied from conf.c. However as conf.c will be moved to using
308 * the callback system, they can be pulled from there eventually, so we
309 * don't need to pollute utils.c with these low level functions
311 int find_fstype_cb(char *buffer
, void *data
)
318 unsigned long mntflags
= 0;
319 char *mntdata
= NULL
;
322 /* we don't try 'nodev' entries */
323 if (strstr(buffer
, "nodev"))
327 fstype
+= lxc_char_left_gc(fstype
, strlen(fstype
));
328 fstype
[lxc_char_right_gc(fstype
, strlen(fstype
))] = '\0';
330 DEBUG("Trying to mount \"%s\"->\"%s\" with FSType \"%s\"", cbarg
->rootfs
,
331 cbarg
->target
, fstype
);
333 if (parse_mntopts(cbarg
->options
, &mntflags
, &mntdata
) < 0) {
338 if (mount(cbarg
->rootfs
, cbarg
->target
, fstype
, mntflags
, mntdata
)) {
339 SYSDEBUG("Failed to mount");
346 INFO("Mounted \"%s\" on \"%s\", with FSType \"%s\"", cbarg
->rootfs
,
347 cbarg
->target
, fstype
);
352 const char *linkderef(const char *path
, char *dest
)
357 ret
= stat(path
, &sbuf
);
359 SYSERROR("Failed to get status of file - \"%s\"", path
);
363 if (!S_ISLNK(sbuf
.st_mode
))
366 ret
= readlink(path
, dest
, PATH_MAX
);
368 SYSERROR("Failed to read link of \"%s\"", path
);
370 } else if (ret
>= PATH_MAX
) {
371 ERROR("The name of link of \"%s\" is too long", path
);
380 * is an unprivileged user allowed to make this kind of snapshot
382 bool unpriv_snap_allowed(struct lxc_storage
*b
, const char *t
, bool snap
,
386 /* New type will be same as original (unless snap && b->type ==
387 * dir, in which case it will be overlayfs -- which is also
390 if (strcmp(b
->type
, "dir") == 0 ||
391 strcmp(b
->type
, "overlay") == 0 ||
392 strcmp(b
->type
, "overlayfs") == 0 ||
393 strcmp(b
->type
, "btrfs") == 0 ||
394 strcmp(b
->type
, "loop") == 0)
400 /* Unprivileged users can copy and snapshot dir, overlayfs, and loop.
401 * In particular, not zfs, btrfs, or lvm.
403 if (strcmp(t
, "dir") == 0 ||
404 strcmp(t
, "overlay") == 0 ||
405 strcmp(t
, "overlayfs") == 0 ||
406 strcmp(t
, "btrfs") == 0 ||
407 strcmp(t
, "loop") == 0)
413 uint64_t get_fssize(char *s
)
418 ret
= strtoull(s
, &end
, 0);
420 ERROR("Invalid blockdev size '%s', using default size", s
);
424 while (isblank(*end
))
428 ret
*= 1024ULL * 1024ULL; /* MB by default */
429 } else if (*end
== 'b' || *end
== 'B') {
431 } else if (*end
== 'k' || *end
== 'K') {
433 } else if (*end
== 'm' || *end
== 'M') {
434 ret
*= 1024ULL * 1024ULL;
435 } else if (*end
== 'g' || *end
== 'G') {
436 ret
*= 1024ULL * 1024ULL * 1024ULL;
437 } else if (*end
== 't' || *end
== 'T') {
438 ret
*= 1024ULL * 1024ULL * 1024ULL * 1024ULL;
440 ERROR("Invalid blockdev unit size '%c' in '%s', using default size", *end
, s
);
447 bool is_valid_storage_type(const char *type
)
449 if (strcmp(type
, "dir") == 0 ||
450 strcmp(type
, "btrfs") == 0 ||
451 strcmp(type
, "loop") == 0 ||
452 strcmp(type
, "lvm") == 0 ||
453 strcmp(type
, "nbd") == 0 ||
454 strcmp(type
, "overlay") == 0 ||
455 strcmp(type
, "overlayfs") == 0 ||
456 strcmp(type
, "rbd") == 0 ||
457 strcmp(type
, "zfs") == 0)
463 int storage_destroy_wrapper(void *data
)
465 struct lxc_conf
*conf
= data
;
467 (void)lxc_setgroups(0, NULL
);
470 SYSERROR("Failed to setgid to 0");
475 SYSERROR("Failed to setuid to 0");
479 if (!storage_destroy(conf
)) {
480 ERROR("Failed to destroy storage");