Skip to content

Commit

Permalink
bdev: "detect" loop file
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
  • Loading branch information
Christian Brauner authored and stgraber committed Jul 16, 2017
1 parent 24e1591 commit 5e924aa
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
11 changes: 9 additions & 2 deletions src/lxc/bdev/bdev.c
Expand Up @@ -854,12 +854,16 @@ static const struct bdev_type *get_bdev_by_name(const char *name)
return NULL;
}

static const struct bdev_type *bdev_query(struct lxc_conf *conf, const char *src)
static const struct bdev_type *bdev_query(struct lxc_conf *conf,
const char *src)
{
size_t i;

if (conf->rootfs.bdev_type)
if (conf->rootfs.bdev_type) {
DEBUG("config file specified rootfs type \"%s\"",
conf->rootfs.bdev_type);
return get_bdev_by_name(conf->rootfs.bdev_type);
}

for (i = 0; i < numbdevs; i++) {
int r;
Expand All @@ -870,6 +874,9 @@ static const struct bdev_type *bdev_query(struct lxc_conf *conf, const char *src

if (i == numbdevs)
return NULL;

DEBUG("detected rootfs type \"%s\"", bdevs[i].name);

return &bdevs[i];
}

Expand Down
20 changes: 19 additions & 1 deletion src/lxc/bdev/lxcloop.c
Expand Up @@ -28,6 +28,7 @@
#include <string.h>
#include <unistd.h>
#include <linux/loop.h>
#include <sys/stat.h>
#include <sys/types.h>

#include "bdev.h"
Expand Down Expand Up @@ -157,22 +158,39 @@ int loop_destroy(struct bdev *orig)

int loop_detect(const char *path)
{
int ret;
struct stat s;

if (strncmp(path, "loop:", 5) == 0)
return 1;

ret = stat(path, &s);
if (ret < 0)
return 0;

if (__S_ISTYPE(s.st_mode, S_IFREG))
return 1;

return 0;
}

int loop_mount(struct bdev *bdev)
{
int ret, loopfd;
char loname[MAXPATHLEN];
char *src = bdev->src;

if (strcmp(bdev->type, "loop"))
return -22;

if (!bdev->src || !bdev->dest)
return -22;

loopfd = lxc_prepare_loop_dev(bdev->src + 5, loname, LO_FLAGS_AUTOCLEAR);
/* skip prefix */
if (!strncmp(bdev->src, "loop:", 5))
src += 5;

loopfd = lxc_prepare_loop_dev(src, loname, LO_FLAGS_AUTOCLEAR);
if (loopfd < 0)
return -1;
DEBUG("prepared loop device \"%s\"", loname);
Expand Down

0 comments on commit 5e924aa

Please sign in to comment.