block: add support for logging to dmesg
[project/fstools.git] / block.c
diff --git a/block.c b/block.c
index a1609b4..6eb1933 100644 (file)
--- a/block.c
+++ b/block.c
@@ -19,6 +19,8 @@
 #include <syslog.h>
 #include <libgen.h>
 #include <glob.h>
+#include <dirent.h>
+#include <stdarg.h>
 
 #include <sys/stat.h>
 #include <sys/types.h>
 
 #include "libblkid-tiny/libblkid-tiny.h"
 
+#ifdef UBIFS_EXTROOT
+#include "libubi/libubi.h"
+#endif
+
+static int kmsg = 0;
+
+static void klog(int prio, const char *fmt, ...)
+{
+       va_list ap;
+       FILE *f = fopen("/dev/kmsg", "w");
+
+       if (f) {
+               fprintf(f, "<%d>", prio);
+
+               va_start(ap, fmt);
+               vfprintf(f, fmt, ap);
+               va_end(ap);
+
+               fclose(f);
+       }
+}
+
+#define KINFO(fmt, ...) do { \
+               if (kmsg) klog(LOG_INFO, "block: " fmt, ## __VA_ARGS__); \
+       } while (0)
+
 #define ERROR(fmt, ...) do { \
-               syslog(LOG_ERR, fmt, ## __VA_ARGS__); \
-               fprintf(stderr, "block: "fmt, ## __VA_ARGS__); \
+               if (kmsg) \
+                       klog(LOG_ERR, "block: " fmt, ## __VA_ARGS__); \
+               else { \
+                       syslog(LOG_ERR, fmt, ## __VA_ARGS__); \
+                       fprintf(stderr, "block: "fmt, ## __VA_ARGS__); \
+               } \
        } while (0)
 
 enum {
@@ -354,7 +386,7 @@ static struct mount* find_swap(const char *uuid, const char *label, const char *
        vlist_for_each_element(&mounts, m, node) {
                if (m->type != TYPE_SWAP)
                        continue;
-               if (uuid && m->uuid && !strcmp(m->uuid, uuid))
+               if (uuid && m->uuid && !strcasecmp(m->uuid, uuid))
                        return m;
                if (label && m->label && !strcmp(m->label, label))
                        return m;
@@ -373,7 +405,7 @@ static struct mount* find_block(const char *uuid, const char *label, const char
        vlist_for_each_element(&mounts, m, node) {
                if (m->type != TYPE_MOUNT)
                        continue;
-               if (m->uuid && uuid && !strcmp(m->uuid, uuid))
+               if (m->uuid && uuid && !strcasecmp(m->uuid, uuid))
                        return m;
                if (m->label && label && !strcmp(m->label, label))
                        return m;
@@ -406,14 +438,16 @@ static int config_load(char *cfg)
                uci_set_confdir(ctx, path);
        }
 
-       if (uci_load(ctx, "fstab", &pkg))
-       {
-               char *err;
-               uci_get_errorstr(ctx, &err, "fstab");
-               ERROR("extroot: failed to load %s/etc/config/%s\n",
-                     cfg ? cfg : "", err);
-               free(err);
-               return -1;
+       if (uci_load(ctx, "fstab", &pkg)) {
+               uci_set_confdir(ctx, "/etc/config");
+               if (uci_load(ctx, "fstab", &pkg)) {
+                       char *err;
+                       uci_get_errorstr(ctx, &err, "fstab");
+                       ERROR("extroot: failed to load %s/etc/config/%s\n",
+                             cfg ? cfg : "", err);
+                       free(err);
+                       return -1;
+               }
        }
 
        vlist_update(&mounts);
@@ -477,11 +511,13 @@ static void cache_load(int mtd)
        if (mtd) {
                _cache_load("/dev/mtdblock*");
                _cache_load("/dev/ubiblock*");
+               _cache_load("/dev/ubi?*_?*");
        }
        _cache_load("/dev/mmcblk*");
        _cache_load("/dev/sd*");
        _cache_load("/dev/hd*");
        _cache_load("/dev/md*");
+       _cache_load("/dev/vd*");
        _cache_load("/dev/mapper/*");
 }
 
@@ -528,7 +564,7 @@ static struct blkid_struct_probe* find_block_info(char *uuid, char *label, char
 
        if (uuid)
                list_for_each_entry(pr, &devices, list)
-                       if (!strcmp(pr->uuid, uuid))
+                       if (!strcasecmp(pr->uuid, uuid))
                                return pr;
 
        if (label)
@@ -592,6 +628,10 @@ static void check_filesystem(struct blkid_struct_probe *pr)
        struct stat statbuf;
        char *e2fsck = "/usr/sbin/e2fsck";
 
+       /* UBIFS does not need stuff like fsck */
+       if (!strncmp(pr->id->name, "ubifs", 5))
+               return;
+
        if (strncmp(pr->id->name, "ext", 3)) {
                ERROR("check_filesystem: %s is not supported\n", pr->id->name);
                return;
@@ -822,13 +862,108 @@ static int find_block_mtd(char *name, char *part, int plen)
        return 0;
 }
 
+#ifdef UBIFS_EXTROOT
+static int find_ubi_vol(libubi_t libubi, char *name, int *dev_num, int *vol_id)
+{
+       int dev = 0;
+
+       while (ubi_dev_present(libubi, dev))
+       {
+               struct ubi_dev_info dev_info;
+               struct ubi_vol_info vol_info;
+
+               if (ubi_get_dev_info1(libubi, dev++, &dev_info))
+                       continue;
+               if (ubi_get_vol_info1_nm(libubi, dev_info.dev_num, name, &vol_info))
+                       continue;
+
+               *dev_num = dev_info.dev_num;
+               *vol_id = vol_info.vol_id;
+
+               return 0;
+       }
+
+       return -1;
+}
+
+static int find_block_ubi(libubi_t libubi, char *name, char *part, int plen)
+{
+       int dev_num;
+       int vol_id;
+       int err = -1;
+
+       err = find_ubi_vol(libubi, name, &dev_num, &vol_id);
+       if (!err)
+               snprintf(part, plen, "/dev/ubi%d_%d", dev_num, vol_id);
+
+       return err;
+}
+
+static int find_block_ubi_RO(libubi_t libubi, char *name, char *part, int plen)
+{
+       int dev_num;
+       int vol_id;
+       int err = -1;
+
+       err = find_ubi_vol(libubi, name, &dev_num, &vol_id);
+       if (!err)
+               snprintf(part, plen, "/dev/ubiblock%d_%d", dev_num, vol_id);
+
+       return err;
+}
+#endif
+
+static int find_root_dev(char *buf, int len)
+{
+       DIR *d;
+       dev_t root;
+       struct stat s;
+       struct dirent *e;
+
+       if (stat("/", &s))
+               return -1;
+
+       if (!(d = opendir("/dev")))
+               return -1;
+
+       root = s.st_dev;
+
+       while ((e = readdir(d)) != NULL) {
+               snprintf(buf, len, "/dev/%s", e->d_name);
+
+               if (stat(buf, &s) || s.st_rdev != root)
+                       continue;
+
+               closedir(d);
+               return 0;
+       }
+
+       closedir(d);
+       return -1;
+}
+
 static int check_extroot(char *path)
 {
        struct blkid_struct_probe *pr = NULL;
        char fs[32];
 
-       if (find_block_mtd("rootfs", fs, sizeof(fs)))
-               return -1;
+#ifdef UBIFS_EXTROOT
+       if (find_block_mtd("rootfs", fs, sizeof(fs))) {
+               int err = -1;
+               libubi_t libubi;
+
+               libubi = libubi_open();
+               err = find_block_ubi_RO(libubi, "rootfs", fs, sizeof(fs));
+               libubi_close(libubi);
+               if (err)
+                       return -1;
+       }
+#else
+       if (find_block_mtd("rootfs", fs, sizeof(fs))) {
+               if (find_root_dev(fs, sizeof(fs)))
+                       return -1;
+       }
+#endif
 
        list_for_each_entry(pr, &devices, list) {
                if (!strcmp(pr->dev, fs)) {
@@ -837,6 +972,10 @@ static int check_extroot(char *path)
                        char tag[64];
                        char uuid[64] = { 0 };
 
+                       snprintf(tag, sizeof(tag), "%s/etc", path);
+                       if (stat(tag, &s))
+                               mkdir_p(tag);
+
                        snprintf(tag, sizeof(tag), "%s/etc/.extroot-uuid", path);
                        if (stat(tag, &s)) {
                                fp = fopen(tag, "w+");
@@ -858,7 +997,7 @@ static int check_extroot(char *path)
 
                        fgets(uuid, sizeof(uuid), fp);
                        fclose(fp);
-                       if (!strcmp(uuid, pr->uuid))
+                       if (!strcasecmp(uuid, pr->uuid))
                                return 0;
                        ERROR("extroot: uuid tag does not match rom uuid\n");
                }
@@ -866,6 +1005,9 @@ static int check_extroot(char *path)
        return -1;
 }
 
+/*
+ * Read info about extroot from UCI (using prefix) and mount it.
+ */
 static int mount_extroot(char *cfg)
 {
        char overlay[] = "/tmp/extroot/overlay";
@@ -875,9 +1017,11 @@ static int mount_extroot(char *cfg)
        struct mount *m;
        int err = -1;
 
+       /* Load @cfg/etc/config/fstab */
        if (config_load(cfg))
                return -2;
 
+       /* See if there is extroot-specific mount config */
        m = find_block(NULL, NULL, NULL, "/");
        if (!m)
                m = find_block(NULL, NULL, NULL, "/overlay");
@@ -888,6 +1032,7 @@ static int mount_extroot(char *cfg)
                return -1;
        }
 
+       /* Find block device pointed by the mount config */
        pr = find_block_info(m->uuid, m->label, m->device);
 
        if (!pr && delay_root){
@@ -898,7 +1043,8 @@ static int mount_extroot(char *cfg)
                pr = find_block_info(m->uuid, m->label, m->device);
        }
        if (pr) {
-               if (strncmp(pr->id->name, "ext", 3)) {
+               if (strncmp(pr->id->name, "ext", 3) &&
+                   strncmp(pr->id->name, "ubifs", 5)) {
                        ERROR("extroot: %s is not supported, try ext4\n", pr->id->name);
                        return -1;
                }
@@ -929,9 +1075,11 @@ static int mount_extroot(char *cfg)
 static int main_extroot(int argc, char **argv)
 {
        struct blkid_struct_probe *pr;
-       char fs[32] = { 0 };
-       char fs_data[32] = { 0 };
+       char blkdev_path[32] = { 0 };
        int err = -1;
+#ifdef UBIFS_EXTROOT
+       libubi_t libubi;
+#endif
 
        if (!getenv("PREINIT"))
                return -1;
@@ -941,29 +1089,29 @@ static int main_extroot(int argc, char **argv)
                return -1;
        }
 
+       kmsg = 1;
+
        mkblkdev();
        cache_load(1);
 
-       find_block_mtd("rootfs", fs, sizeof(fs));
-       if (!fs[0]) {
-               ERROR("extroot: unable to locate rootfs mtdblock\n");
-               return -2;
-       }
-
-       pr = find_block_info(NULL, NULL, fs);
-       if (!pr) {
-               ERROR("extroot: unable to retrieve rootfs information\n");
-               return -3;
-       }
+       /*
+        * Look for "rootfs_data". We will want to mount it and check for
+        * extroot configuration.
+        */
 
-       find_block_mtd("rootfs_data", fs_data, sizeof(fs_data));
-       if (fs_data[0]) {
-               pr = find_block_info(NULL, NULL, fs_data);
+       /* Start with looking for MTD partition */
+       find_block_mtd("rootfs_data", blkdev_path, sizeof(blkdev_path));
+       if (blkdev_path[0]) {
+               pr = find_block_info(NULL, NULL, blkdev_path);
                if (pr && !strcmp(pr->id->name, "jffs2")) {
                        char cfg[] = "/tmp/jffs_cfg";
 
+                       /*
+                        * Mount MTD part and try extroot (using
+                        * /etc/config/fstab from that partition)
+                        */
                        mkdir_p(cfg);
-                       if (!mount(fs_data, cfg, "jffs2", MS_NOATIME, NULL)) {
+                       if (!mount(blkdev_path, cfg, "jffs2", MS_NOATIME, NULL)) {
                                err = mount_extroot(cfg);
                                umount2(cfg, MNT_DETACH);
                        }
@@ -974,6 +1122,28 @@ static int main_extroot(int argc, char **argv)
                }
        }
 
+#ifdef UBIFS_EXTROOT
+       /* ... but it also could be an UBI volume */
+       memset(blkdev_path, 0, sizeof(blkdev_path));
+       libubi = libubi_open();
+       find_block_ubi(libubi, "rootfs_data", blkdev_path, sizeof(blkdev_path));
+       libubi_close(libubi);
+       if (blkdev_path[0]) {
+               char cfg[] = "/tmp/ubifs_cfg";
+
+               /* Mount volume and try extroot (using fstab from that vol) */
+               mkdir_p(cfg);
+               if (!mount(blkdev_path, cfg, "ubifs", MS_NOATIME, NULL)) {
+                       err = mount_extroot(cfg);
+                       umount2(cfg, MNT_DETACH);
+               }
+               if (err < 0)
+                       rmdir("/tmp/overlay");
+               rmdir(cfg);
+               return err;
+       }
+#endif
+
        return mount_extroot(NULL);
 }