summaryrefslogtreecommitdiff
path: root/libfstools/mount.c
blob: 12c4a31d77c75c27e6cdb6dbde93f30121afd0b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
 * Copyright (C) 2014 John Crispin <blogic@openwrt.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 2.1
 * as published by the Free Software Foundation
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mount.h>

#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <string.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);

/**
 * mount_move - move mounted point to the new location
 *
 * @oldroot: directory that is current location of mount point
 * @newroot: new directory for the mount point
 */
int
mount_move(const char *oldroot, const char *newroot, const char *dir)
{
#ifndef MS_MOVE
#define MS_MOVE	(1 << 13)
#endif
	struct stat s;
	char olddir[64];
	char newdir[64];
	int ret;

	snprintf(olddir, sizeof(olddir), "%s%s", oldroot, dir);
	snprintf(newdir, sizeof(newdir), "%s%s", newroot, dir);

	if (stat(olddir, &s) || !S_ISDIR(s.st_mode))
		return -1;

	if (stat(newdir, &s) || !S_ISDIR(s.st_mode))
		return -1;

	ret = mount(olddir, newdir, NULL, MS_NOATIME | MS_MOVE, NULL);

/*	if (ret)
		ULOG_ERR("failed %s %s: %s\n", olddir, newdir, strerror(errno));*/

	return ret;
}

int
pivot(char *new, char *old)
{
	char pivotdir[64];
	int ret;

	if (mount_move("", new, "/proc"))
		return -1;

	snprintf(pivotdir, sizeof(pivotdir), "%s%s", new, old);

	ret = pivot_root(new, pivotdir);

	if (ret < 0) {
		ULOG_ERR("pivot_root failed %s %s: %s\n", new, pivotdir, strerror(errno));
		return -1;
	}

	mount_move(old, "", "/dev");
	mount_move(old, "", "/tmp");
	mount_move(old, "", "/sys");
	mount_move(old, "", "/overlay");

	return 0;
}

/**
 * fopivot - switch to overlay using passed dir as upper one
 *
 * @rw_root: writable directory that will be used as upper dir
 * @ro_root: directory where old root will be put
 */
int
fopivot(char *rw_root, char *ro_root)
{
	char overlay[64], mount_options[64];

	if (find_filesystem("overlay")) {
		ULOG_ERR("BUG: no suitable fs found\n");
		return -1;
	}

	snprintf(overlay, sizeof(overlay), "overlayfs:%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(mount_options, sizeof(mount_options), "lowerdir=/,upperdir=%s", rw_root);
	if (mount(overlay, "/mnt", "overlayfs", MS_NOATIME, mount_options)) {
		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(mount_options, sizeof(mount_options), "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, mount_options)) {
			if (mount(overlay, "/mnt", "overlayfs", MS_NOATIME, mount_options)) {
				ULOG_ERR("mount failed: %s, options %s\n",
				         strerror(errno), mount_options);
				return -1;
			}
		}
	}

	return pivot("/mnt", ro_root);
}

/**
 * ramoverlay - use RAM to store filesystem changes on top of RO root
 */
int
ramoverlay(void)
{
	mkdir("/tmp/root", 0755);
	mount("tmpfs", "/tmp/root", "tmpfs", MS_NOATIME, "mode=0755");

	return fopivot("/tmp/root", "/rom");
}