block: also handle /dev/mdX (softraid) and /dev/mapper/* (LVM2) devices
[project/ubox.git] / block.c
diff --git a/block.c b/block.c
index d0fce7e..edfa627 100644 (file)
--- a/block.c
+++ b/block.c
@@ -137,6 +137,14 @@ static char *blobmsg_get_strdup(struct blob_attr *attr)
        return strdup(blobmsg_get_string(attr));
 }
 
+static char *blobmsg_get_basename(struct blob_attr *attr)
+{
+       if (!attr)
+               return NULL;
+
+       return strdup(basename(blobmsg_get_string(attr)));
+}
+
 static int mount_add(struct uci_section *s)
 {
        struct blob_attr *tb[__MOUNT_MAX] = { 0 };
@@ -158,7 +166,7 @@ static int mount_add(struct uci_section *s)
        m->label = blobmsg_get_strdup(tb[MOUNT_LABEL]);
        m->target = blobmsg_get_strdup(tb[MOUNT_TARGET]);
        m->options = blobmsg_get_strdup(tb[MOUNT_OPTIONS]);
-       m->device = blobmsg_get_strdup(tb[MOUNT_DEVICE]);
+       m->device = blobmsg_get_basename(tb[MOUNT_DEVICE]);
        m->overlay = m->extroot = 0;
        if (m->target && !strcmp(m->target, "/"))
                m->extroot = 1;
@@ -191,7 +199,7 @@ static int swap_add(struct uci_section *s)
        memset(m, 0, sizeof(struct mount));
        m->type = TYPE_SWAP;
        m->uuid = blobmsg_get_strdup(tb[SWAP_UUID]);
-       m->device = blobmsg_get_strdup(tb[SWAP_DEVICE]);
+       m->device = blobmsg_get_basename(tb[SWAP_DEVICE]);
        if (tb[SWAP_PRIO])
                m->prio = blobmsg_get_u32(tb[SWAP_PRIO]);
        if (m->prio)
@@ -319,7 +327,7 @@ static int _cache_load(const char *path)
                struct blkid_struct_probe *pr = malloc(sizeof(struct blkid_struct_probe));
                memset(pr, 0, sizeof(struct blkid_struct_probe));
                probe_block(gl.gl_pathv[j], pr);
-               if (pr->err)
+               if (pr->err || !pr->id)
                        free(pr);
                else
                        list_add_tail(&pr->list, &devices);
@@ -336,6 +344,10 @@ static void cache_load(int mtd)
                _cache_load("/dev/mtdblock*");
        _cache_load("/dev/mmcblk*");
        _cache_load("/dev/sd*");
+       _cache_load("/dev/sdc*");
+       _cache_load("/dev/hd*");
+       _cache_load("/dev/md*");
+       _cache_load("/dev/mapper/*");
 }
 
 static int print_block_info(struct blkid_struct_probe *pr)
@@ -369,7 +381,7 @@ static int print_block_uci(struct blkid_struct_probe *pr)
        if (pr->uuid[0])
                printf("\toption\tuuid\t'%s'\n", pr->uuid);
        else
-               printf("\toption\tdevice\t'%s'\n", basename(pr->dev));
+               printf("\toption\tdevice\t'%s'\n", pr->dev);
        printf("\toption\tenabled\t'0'\n\n");
 
        return 0;
@@ -448,11 +460,10 @@ static void check_filesystem(struct blkid_struct_probe *pr)
                return;
        }
 
-       if (stat(e2fsck, &statbuf) < 0)
-               return;
-
-       if (!(statbuf.st_mode & S_IXUSR))
+       if (stat(e2fsck, &statbuf) < 0) {
+               fprintf(stderr, "check_filesystem: %s not found\n", e2fsck);
                return;
+       }
 
        pid = fork();
        if (!pid) {
@@ -460,7 +471,10 @@ static void check_filesystem(struct blkid_struct_probe *pr)
                exit(-1);
        } else if (pid > 0) {
                int status;
+
                waitpid(pid, &status, 0);
+               if (WEXITSTATUS(status))
+                       fprintf(stderr, "check_filesystem: %s returned %d\n", e2fsck, WEXITSTATUS(status));
        }
 }
 
@@ -702,6 +716,8 @@ static int mount_extroot(char *cfg)
        if (!pr && delay_root){
                fprintf(stderr, "extroot: is not ready yet, retrying in %ui seconds\n", delay_root);
                sleep(delay_root);
+               mkblkdev();
+               cache_load(0);
                pr = find_block_info(m->uuid, m->label, NULL);
        }
        if (pr) {