2 * Copyright (C) 2014 John Crispin <blogic@openwrt.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License version 2.1
6 * as published by the Free Software Foundation
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
14 #include <sys/types.h>
16 #include <sys/mount.h>
23 #include "libfstools.h"
25 /* this is a raw syscall - man 2 pivot_root */
26 extern int pivot_root(const char *new_root, const char *put_old);
29 * mount_move - move mounted point to the new location
31 * @oldroot: directory that is current location of mount point
32 * @newroot: new directory for the mount point
35 mount_move(char *oldroot, char *newroot, char *dir)
38 #define MS_MOVE (1 << 13)
45 snprintf(olddir, sizeof(olddir), "%s%s", oldroot, dir);
46 snprintf(newdir, sizeof(newdir), "%s%s", newroot, dir);
48 if (stat(olddir, &s) || !S_ISDIR(s.st_mode))
51 if (stat(newdir, &s) || !S_ISDIR(s.st_mode))
54 ret = mount(olddir, newdir, NULL, MS_NOATIME | MS_MOVE, NULL);
57 ULOG_ERR("failed %s %s: %m\n", olddir, newdir);*/
63 pivot(char *new, char *old)
68 if (mount_move("", new, "/proc"))
71 snprintf(pivotdir, sizeof(pivotdir), "%s%s", new, old);
73 ret = pivot_root(new, pivotdir);
76 ULOG_ERR("pivot_root failed %s %s: %m\n", new, pivotdir);
80 mount_move(old, "", "/dev");
81 mount_move(old, "", "/tmp");
82 mount_move(old, "", "/sys");
83 mount_move(old, "", "/overlay");
89 * fopivot - switch to overlay using passed dir as upper one
91 * @rw_root: writable directory that will be used as upper dir
92 * @ro_root: directory where old root will be put
95 fopivot(char *rw_root, char *ro_root)
97 char overlay[64], mount_options[64];
99 if (find_filesystem("overlay")) {
100 ULOG_ERR("BUG: no suitable fs found\n");
104 snprintf(overlay, sizeof(overlay), "overlayfs:%s", rw_root);
107 * First, try to mount without a workdir, for overlayfs v22 and before.
108 * If it fails, it means that we are probably using a v23 and
109 * later versions that require a workdir
111 snprintf(mount_options, sizeof(mount_options), "lowerdir=/,upperdir=%s", rw_root);
112 if (mount(overlay, "/mnt", "overlayfs", MS_NOATIME, mount_options)) {
113 char upperdir[64], workdir[64], upgrade[64], upgrade_dest[64];
116 snprintf(upperdir, sizeof(upperdir), "%s/upper", rw_root);
117 snprintf(workdir, sizeof(workdir), "%s/work", rw_root);
118 snprintf(upgrade, sizeof(upgrade), "%s/sysupgrade.tgz", rw_root);
119 snprintf(upgrade_dest, sizeof(upgrade_dest), "%s/sysupgrade.tgz", upperdir);
120 snprintf(mount_options, sizeof(mount_options), "lowerdir=/,upperdir=%s,workdir=%s",
124 * Overlay FS v23 and later requires both a upper and
125 * a work directory, both on the same filesystem, but
126 * not part of the same subtree.
127 * We can't really deal with these constraints without
128 * creating two new subdirectories in /overlay.
130 mkdir(upperdir, 0755);
131 mkdir(workdir, 0755);
133 if (stat(upgrade, &st) == 0)
134 rename(upgrade, upgrade_dest);
136 /* Mainlined overlayfs has been renamed to "overlay", try that first */
137 if (mount(overlay, "/mnt", "overlay", MS_NOATIME, mount_options)) {
138 if (mount(overlay, "/mnt", "overlayfs", MS_NOATIME, mount_options)) {
139 ULOG_ERR("mount failed: %s, options %m\n", mount_options);
145 return pivot("/mnt", ro_root);
149 * ramoverlay - use RAM to store filesystem changes on top of RO root
154 mkdir("/tmp/root", 0755);
155 mount("tmpfs", "/tmp/root", "tmpfs", MS_NOATIME, "mode=0755");
157 return fopivot("/tmp/root", "/rom");