block: print mountpoint if already mounted
[project/fstools.git] / libfstools / mount.c
index e7b57f0..81176ce 100644 (file)
@@ -20,7 +20,7 @@
 #include <unistd.h>
 #include <string.h>
 
-#include "../fs-state.h"
+#include "libfstools.h"
 
 /* this is a raw syscall - man 2 pivot_root */
 extern int pivot_root(const char *new_root, const char *put_old);
@@ -48,7 +48,7 @@ mount_move(char *oldroot, char *newroot, char *dir)
        ret = mount(olddir, newdir, NULL, MS_NOATIME | MS_MOVE, NULL);
 
 /*     if (ret)
-               fprintf(stderr, "failed %s %s: %s\n", olddir, newdir, strerror(errno));*/
+               ULOG_ERR("failed %s %s: %s\n", olddir, newdir, strerror(errno));*/
 
        return ret;
 }
@@ -67,7 +67,7 @@ pivot(char *new, char *old)
        ret = pivot_root(new, pivotdir);
 
        if (ret < 0) {
-               fprintf(stderr, "pivot_root failed %s %s: %s\n", new, pivotdir, strerror(errno));
+               ULOG_ERR("pivot_root failed %s %s: %s\n", new, pivotdir, strerror(errno));
                return -1;
        }
 
@@ -85,16 +85,50 @@ fopivot(char *rw_root, char *ro_root)
        char overlay[64], lowerdir[64];
 
        if (find_filesystem("overlay")) {
-               fprintf(stderr, "BUG: no suitable fs found\n");
+               ULOG_ERR("BUG: no suitable fs found\n");
                return -1;
        }
 
        snprintf(overlay, sizeof(overlay), "overlayfs:%s", rw_root);
-       snprintf(lowerdir, sizeof(lowerdir), "lowerdir=/,upperdir=%s", rw_root);
 
+       /*
+        * First, try to mount without a workdir, for overlayfs v22 and before.
+        * If it fails, it means that we are probably using a v23 and
+        * later versions that require a workdir
+        */
+       snprintf(lowerdir, sizeof(lowerdir), "lowerdir=/,upperdir=%s", rw_root);
        if (mount(overlay, "/mnt", "overlayfs", MS_NOATIME, lowerdir)) {
-               fprintf(stderr, "mount failed: %s\n", strerror(errno));
-               return -1;
+               char upperdir[64], workdir[64], upgrade[64], upgrade_dest[64];
+               struct stat st;
+
+               snprintf(upperdir, sizeof(upperdir), "%s/upper", rw_root);
+               snprintf(workdir, sizeof(workdir), "%s/work", rw_root);
+               snprintf(upgrade, sizeof(upgrade), "%s/sysupgrade.tgz", rw_root);
+               snprintf(upgrade_dest, sizeof(upgrade_dest), "%s/sysupgrade.tgz", upperdir);
+               snprintf(lowerdir, sizeof(lowerdir), "lowerdir=/,upperdir=%s,workdir=%s",
+                        upperdir, workdir);
+
+               /*
+                * Overlay FS v23 and later requires both a upper and
+                * a work directory, both on the same filesystem, but
+                * not part of the same subtree.
+                * We can't really deal with these constraints without
+                * creating two new subdirectories in /overlay.
+                */
+               mkdir(upperdir, 0755);
+               mkdir(workdir, 0755);
+
+               if (stat(upgrade, &st) == 0)
+                   rename(upgrade, upgrade_dest);
+
+               /* Mainlined overlayfs has been renamed to "overlay", try that first */
+               if (mount(overlay, "/mnt", "overlay", MS_NOATIME, lowerdir)) {
+                       if (mount(overlay, "/mnt", "overlayfs", MS_NOATIME, lowerdir)) {
+                               ULOG_ERR("mount failed: %s, options %s\n",
+                                        strerror(errno), lowerdir);
+                               return -1;
+                       }
+               }
        }
 
        return pivot("/mnt", ro_root);