-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathOverlay.cpp
241 lines (214 loc) · 9.21 KB
/
Overlay.cpp
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/* SPDX-License-Identifier: LGPL-2.1-or-later */
/* SPDX-FileCopyrightText: 2020 SUSE LLC */
/*
Handling of /etc overlayfs layers
*/
#include "Overlay.hpp"
#include "Configuration.hpp"
#include "Log.hpp"
#include "Mount.hpp"
#include "Snapshot.hpp"
#include "Util.hpp"
#include <cstring>
#include <filesystem>
#include <regex>
#include <selinux/selinux.h>
#include <selinux/context.h>
#include <sstream>
#include <sys/stat.h>
#include <unistd.h>
using std::exception;
using std::string;
using std::stringstream;
using std::unique_ptr;
using std::vector;
namespace fs = std::filesystem;
namespace TransactionalUpdate {
/*
* Create a new overlay instance for the given snapshot number.
* For existing overlays the lowerdirs are read automatically from the given snapshot overlay;
* for this to work the snapshot still has to exist.
* For new overlays `create` has to be called afterwards with a base.
*/
Overlay::Overlay(string snapshot):
workdir(fs::path{config.get("OVERLAY_DIR")} / snapshot / "work-etc")
{
fs::create_directories(workdir);
// Read lowerdirs
// Always use the root file system for reading fstab, as the first snapshot overlay doesn't
// contain an fstab in the /etc overlay yet.
// Note: Due to this for new overlays (i.e. when "create" will be called later) the lowerdirs
// will be initialized with outdated data of the base snapshot - it will be initalized
// correctly during "create".
snapMgr = SnapshotFactory::get();
unique_ptr<Snapshot> snap = snapMgr->open(snapshot);
Mount mntEtc{"/etc"};
mntEtc.setTabSource(snap->getRoot() / "etc" / "fstab");
// Read data from fstab if this is an existing snapshot, just use the defaults otherwise
try {
upperdir = regex_replace(mntEtc.getOption("upperdir"), std::regex("^" + config.get("DRACUT_SYSROOT")), "");
const string fstabLowerdirs = mntEtc.getOption("lowerdir");
string lowerdir;
stringstream ss(fstabLowerdirs);
while (getline(ss, lowerdir, ':')) {
lowerdir = regex_replace(lowerdir, std::regex("^" + config.get("DRACUT_SYSROOT")), "");
lowerdirs.push_back(lowerdir);
}
} catch (exception &e) {}
}
string Overlay::getIdOfOverlayDir(const string dir) {
std::smatch match;
std::regex exp("^(" + config.get("DRACUT_SYSROOT") + ")?" + config.get("OVERLAY_DIR") + "/(.+)/etc$");
if (regex_search(dir.begin(), dir.end(), match, exp)) {
return match[2];
}
return "";
}
string Overlay::getPreviousSnapshotOvlId() {
for (auto it = lowerdirs.begin(); it != lowerdirs.end(); it++) {
string id = getIdOfOverlayDir(*it);
if (! id.empty())
return id;
}
return "";
}
bool Overlay::references(string snapshot) {
for (auto it = lowerdirs.begin(); it != lowerdirs.end(); it++) {
string id = getIdOfOverlayDir(*it);
if (id == snapshot)
return true;
}
return false;
}
bool Overlay::sync(string base, fs::path snapRoot) {
Overlay baseOverlay = Overlay{base};
auto previousSnapId = baseOverlay.getPreviousSnapshotOvlId();
if (previousSnapId.empty()) {
tulog.info("No previous snapshot to sync with - skipping");
return false;
}
unique_ptr<Snapshot> previousSnapshot;
try {
previousSnapshot = snapMgr->open(previousSnapId);
} catch (std::invalid_argument &e) {
tulog.info("Parent snapshot ", previousSnapId, " does not exist any more - skipping rsync");
return false;
}
unique_ptr<Mount> previousEtc{new Mount("/etc", 0, true)};
previousEtc->setTabSource(previousSnapshot->getRoot() / "etc" / "fstab");
// Mount read-only, so mount everything as lowerdir
Overlay previousOvl{previousSnapId};
previousOvl.lowerdirs.insert(previousOvl.lowerdirs.begin(), previousOvl.upperdir);
previousOvl.setMountOptionsForMount(previousEtc);
previousEtc->removeOption("upperdir");
previousEtc->removeOption("workdir");
string syncSource = string(previousOvl.upperdir.parent_path() / "sync" / "etc") + "/";
previousEtc->mount(previousOvl.upperdir.parent_path() / "sync");
tulog.info("Syncing /etc of previous snapshot ", previousSnapId, " as base into new snapshot ", snapRoot);
if (is_selinux_enabled()) {
tulog.info("SELinux is enabled.");
}
try {
Util::exec("rsync --quiet --archive --inplace --xattrs --exclude='/fstab' --acls --delete " + syncSource + " " + string(snapRoot) + "/etc 2>&1");
} catch (exception &e) {
// rsync will fail when synchronizing pre-SELinux snapshots as soon as SELinux enabled,
// so try again without the SELinux xattrs.
tulog.info("Retrying rsync without SELinux xattrs...");
Util::exec("rsync --quiet --archive --inplace --xattrs --filter='-x security.selinux' --exclude='/fstab' --acls --delete " + syncSource + " " + string(snapRoot) + "/etc");
}
return true;
}
void Overlay::setMountOptions(unique_ptr<Mount>& mount) {
string lower;
for (auto lowerdir: lowerdirs) {
if (! lower.empty())
lower.append(":");
lower.append(config.get("DRACUT_SYSROOT") / lowerdir.relative_path());
}
long pagesize = sysconf(_SC_PAGE_SIZE);
if (pagesize > 0 && lower.length() >= reinterpret_cast<unsigned long&>(pagesize)) {
throw std::runtime_error{"Exceeding maximum length of mount options; please boot into the new snapshot before proceeding."};
}
mount->setOption("lowerdir", lower);
mount->setOption("upperdir", config.get("DRACUT_SYSROOT") / upperdir.relative_path());
mount->setOption("workdir", config.get("DRACUT_SYSROOT") / workdir.relative_path());
}
/* Mount all layers mentioned as lowerdirs... */
void Overlay::setMountOptionsForMount(unique_ptr<Mount>& mount) {
string lower;
Mount mntCurrentEtc{"/etc"};
string currentUpper = mntCurrentEtc.getOption("upperdir");
for (auto lowerdir: lowerdirs) {
if (! lower.empty()) {
lower.append(":");
}
// Check whether the current upper directory is part of the snapshot's lower
// directory stack; if so use reuse /etc directly instead, as mounting
// the same upper directory multiple times is not supported by overlayfs
if (getIdOfOverlayDir(lowerdir) == getIdOfOverlayDir(currentUpper)) {
lower.append("/etc");
break;
}
// Replace /etc in lowerdir with /etc of overlay base
if (lowerdir == "/etc") {
std::unique_ptr<Snapshot> snap = snapMgr->open(getIdOfOverlayDir(upperdir));
lower.append(snap->getRoot() / "etc");
} else {
lower.append(lowerdir);
}
}
mount->setOption("lowerdir", lower);
mount->setOption("upperdir", upperdir);
mount->setOption("workdir", workdir);
}
void Overlay::create(string base, string snapshot, fs::path snapRoot) {
upperdir = fs::path{config.get("OVERLAY_DIR")} / snapshot / "etc";
Overlay parent = Overlay{base};
// Remove overlay directory if it already exists (e.g. after the snapshot was deleted)
fs::remove_all(upperdir);
fs::create_directory(upperdir, "/etc");
// Explicitly copy the current /etc permissions, even if the umask was more restrictive
struct stat status;
tulog.info(parent.upperdir.string());
if (stat(parent.upperdir.c_str(), &status) != 0) {
throw std::runtime_error{"could not get permissions of /etc: " + std::string(strerror(errno))};
}
if (chmod(upperdir.c_str(), status.st_mode) != 0) {
throw std::runtime_error{"could not set permissions of " + upperdir.string() + ": " + std::string(strerror(errno))};
}
char* context = NULL;
if (getfilecon("/etc", &context) > 0) {
tulog.debug("selinux context on /etc: " + std::string(context));
if (setfilecon(upperdir.c_str(), context) != 0) {
freecon(context);
throw std::runtime_error{"applying selinux context failed: " + std::string(strerror(errno))};
}
freecon(context);
}
// Assemble the new lowerdirs
lowerdirs.clear();
lowerdirs.push_back(parent.upperdir);
Mount currentEtc{"/etc"};
string currentUpper = currentEtc.getOption("upperdir");
// It is possible that files in /etc will be modified after the creation of the snapshot,
// but before rebooting the system. When using the --continue option and if the snapshot is
// based on the currently running system, then the snapshot stack has to be preserved to
// keep the layer transparency. Otherwise just sync the previous snapshots /etc into the
// new layer as a base.
if (parent.references(getIdOfOverlayDir(currentUpper))) {
for (auto it = parent.lowerdirs.begin(); it != parent.lowerdirs.end(); it++) {
lowerdirs.push_back(*it);
}
} else {
// Syncing copies the result of the parent's lowerdirs into the new snapshot.
// If that succeeded, use the coalesced result instead of all lowerdirs individually.
if (sync(base, snapRoot)) {
lowerdirs.push_back(parent.lowerdirs.back());
} else {
for (auto it = parent.lowerdirs.begin(); it != parent.lowerdirs.end(); it++) {
lowerdirs.push_back(*it);
}
}
}
}
} // namespace TransactionalUpdate