xref: /dragonfly/usr.bin/dsynth/mount.c (revision 03517d4e)
1 /*
2  * Copyright (c) 2019-2022 The DragonFly Project.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by Matthew Dillon <dillon@backplane.com>
6  *
7  * This code uses concepts and configuration based on 'synth', by
8  * John R. Marino <draco@marino.st>, which was written in ada.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  *
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in
18  *    the documentation and/or other materials provided with the
19  *    distribution.
20  * 3. Neither the name of The DragonFly Project nor the names of its
21  *    contributors may be used to endorse or promote products derived
22  *    from this software without specific, prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
28  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
30  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
32  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
33  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
34  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  */
37 #include "dsynth.h"
38 
39 static void domount(worker_t *work, int type,
40 			const char *spath, const char *dpath,
41 			const char *discretefmt);
42 static void dounmount(worker_t *work, const char *rpath);
43 static void makeDiscreteCopies(const char *spath, const char *discretefmt);
44 
45 /*
46  * Called by the frontend to create a template which will be cpdup'd
47  * into fresh workers.
48  *
49  * Template must have been previously destroyed.  Errors are fatal
50  */
51 int
52 DoCreateTemplate(int force)
53 {
54 	struct stat st;
55 	char *goodbuf;
56 	char *buf;
57 	const char *reason = "";
58 	int rc;
59 	int fd;
60 	int n;
61 
62 	rc = 0;
63 	asprintf(&goodbuf, "%s/.template.good", BuildBase);
64 
65 	/*
66 	 * Conditionally create the template and discrete copies of certain
67 	 * directories if we think we are missing things.
68 	 */
69 	if (force == 1)
70 		reason = " (Asked to force template creation)";
71 	if (force == 0) {
72 		time_t ls_mtime;
73 
74 		asprintf(&buf, "%s/Template", BuildBase);
75 		if (stat(buf, &st) < 0) {
76 			force = 1;
77 			reason = " (Template file missing)";
78 		}
79 		free(buf);
80 
81 		/*
82 		 * Check to see if the worker count changed and some
83 		 * template dirs are missing or out of date, and also if
84 		 * a new world was installed (via /bin/ls mtime).
85 		 */
86 		asprintf(&buf, "%s/bin/ls", SystemPath);
87 		if (stat(buf, &st) < 0)
88 			dfatal_errno("Unable to locate %s", buf);
89 		free(buf);
90 		ls_mtime = st.st_mtime;
91 
92 		for (n = 0; n < MaxWorkers; ++n) {
93 			asprintf(&buf, "%s/bin.%03d/ls", BuildBase, n);
94 			if (stat(buf, &st) < 0 || st.st_mtime != ls_mtime) {
95 				force = 1;
96 				reason = " (/bin/ls mtime mismatch)";
97 			}
98 			free(buf);
99 		}
100 
101 		if (stat(goodbuf, &st) < 0) {
102 			force = 1;
103 			reason = " (.template.good file missing)";
104 		}
105 	}
106 
107 	dlog(DLOG_ALL, "Check Template: %s%s\n",
108 	     (force ? "Must-Create" : "Good"),
109 	     reason);
110 
111 	/*
112 	 * Create the template
113 	 */
114 	if (force) {
115 		remove(goodbuf);	/* ignore exit code */
116 
117 		rc = 0;
118 		asprintf(&buf, "%s/mktemplate %s %s/Template",
119 			 SCRIPTPATH(SCRIPTDIR), SystemPath, BuildBase);
120 		rc = system(buf);
121 		if (rc)
122 			dfatal("Command failed: %s\n", buf);
123 		dlog(DLOG_ALL | DLOG_FILTER,
124 		     "Template - rc=%d running %s\n", rc, buf);
125 		free(buf);
126 
127 		/*
128 		 * Make discrete copies of certain extremely heavily used
129 		 * but small directories.
130 		 */
131 		makeDiscreteCopies("$/bin", "/bin.%03d");
132 		makeDiscreteCopies("$/lib", "/lib.%03d");
133 		makeDiscreteCopies("$/libexec", "/libexec.%03d");
134 		makeDiscreteCopies("$/usr/bin", "/usr.bin.%03d");
135 
136 		/*
137 		 * Mark the template good... ah, do a sync() to really
138 		 * be sure that it can't get corrupted.
139 		 */
140 		sync();
141 		fd = open(goodbuf, O_RDWR|O_CREAT|O_TRUNC, 0644);
142 		dassert_errno(fd >= 0, "could not create %s", goodbuf);
143 		close(fd);
144 
145 		dlog(DLOG_ALL | DLOG_FILTER, "Template - done\n");
146 	}
147 	free(goodbuf);
148 
149 	return force;
150 }
151 
152 void
153 DoDestroyTemplate(void)
154 {
155 	struct stat st;
156 	char *path;
157 	char *buf;
158 	int rc;
159 
160 	/*
161 	 * NOTE: rm -rf safety, use a fixed name 'Template' to ensure we
162 	 *	 do not accidently blow something up.
163 	 */
164 	asprintf(&path, "%s/Template", BuildBase);
165 	if (stat(path, &st) == 0) {
166 		asprintf(&buf, "chflags -R noschg %s; /bin/rm -rf %s",
167 			 path, path);
168 		rc = system(buf);
169 		if (rc)
170 			dfatal("Command failed: %s (ignored)\n", buf);
171 		free(buf);
172 	}
173 	free(path);
174 }
175 
176 /*
177  * Called by the worker support thread to install a new worker
178  * filesystem topology.
179  */
180 void
181 DoWorkerMounts(worker_t *work)
182 {
183 	char *buf;
184 	int rc;
185 
186 	/*
187 	 * Generate required mounts, domount() will mkdir() the target
188 	 * directory if necessary and prefix spath with SystemPath if
189 	 * it starts with $/
190 	 */
191 	setNumaDomain(work->index);
192 	domount(work, TMPFS_RW, "dummy", "", NULL);
193 	asprintf(&buf, "%s/usr", work->basedir);
194 	if (mkdir(buf, 0755) != 0) {
195 		fprintf(stderr, "Command failed: mkdir %s\n", buf);
196 		++work->mount_error;
197 	}
198 	free(buf);
199 
200 	domount(work, TMPFS_RW,  "dummy", "/boot", NULL);
201 
202 	asprintf(&buf, "%s/boot/modules.local", work->basedir);
203 	if (mkdir(buf, 0755) != 0) {
204 		fprintf(stderr, "Command failed: mkdir %s\n", buf);
205 		++work->mount_error;
206 	}
207 	free(buf);
208 
209 	domount(work, DEVFS_RW,  "dummy", "/dev", NULL);
210 	domount(work, PROCFS_RO, "dummy", "/proc", NULL);
211 	domount(work, NULLFS_RO, "$/bin", "/bin", "/bin.%03d");
212 	domount(work, NULLFS_RO, "$/sbin", "/sbin", NULL);
213 	domount(work, NULLFS_RO, "$/lib", "/lib", "/lib.%03d");
214 	domount(work, NULLFS_RO, "$/libexec", "/libexec", "/libexec.%03d");
215 	domount(work, NULLFS_RO, "$/usr/bin", "/usr/bin", "/usr.bin.%03d");
216 	domount(work, NULLFS_RO, "$/usr/include", "/usr/include", NULL);
217 	domount(work, NULLFS_RO, "$/usr/lib", "/usr/lib", NULL);
218 	domount(work, NULLFS_RO, "$/usr/libdata", "/usr/libdata", NULL);
219 	domount(work, NULLFS_RO, "$/usr/libexec", "/usr/libexec", NULL);
220 	domount(work, NULLFS_RO, "$/usr/sbin", "/usr/sbin", NULL);
221 	domount(work, NULLFS_RO, "$/usr/share", "/usr/share", NULL);
222 	domount(work, TMPFS_RW_MED,  "dummy", "/usr/local", NULL);
223 	domount(work, NULLFS_RO, "$/usr/games", "/usr/games", NULL);
224 	if (UseUsrSrc)
225 		domount(work, NULLFS_RO, "$/usr/src", "/usr/src", NULL);
226 	domount(work, NULLFS_RO, DPortsPath, "/xports", NULL);
227 	domount(work, NULLFS_RW, OptionsPath, "/options", NULL);
228 	domount(work, NULLFS_RW, PackagesPath, "/packages", NULL);
229 	domount(work, NULLFS_RW, DistFilesPath, "/distfiles", NULL);
230 	domount(work, TMPFS_RW_BIG, "dummy", "/construction", NULL);
231 	if (UseCCache)
232 		domount(work, NULLFS_RW, CCachePath, "/ccache", NULL);
233 
234 	/*
235 	 * NOTE: Uses blah/. to prevent cp from creating 'Template' under
236 	 *	 work->basedir.  We want to start with the content.
237 	 */
238 	asprintf(&buf, "cp -Rp %s/Template/. %s", BuildBase, work->basedir);
239 	rc = system(buf);
240 	if (rc) {
241 		fprintf(stderr, "Command failed: %s\n", buf);
242 		++work->accum_error;
243 		snprintf(work->status, sizeof(work->status),
244 			 "Template copy failed");
245 	}
246 	free(buf);
247 	setNumaDomain(-1);
248 }
249 
250 /*
251  * Called by the worker support thread to remove a worker
252  * filesystem topology.
253  *
254  * NOTE: No need to conditionalize UseUsrSrc, it doesn't hurt to
255  *	 issue the umount() if it isn't mounted and it ensures that
256  *	 everything is unmounted properly on cleanup if the state
257  *	 changes.
258  */
259 void
260 DoWorkerUnmounts(worker_t *work)
261 {
262 	int retries;
263 
264 	setNumaDomain(work->index);
265 	work->mount_error = 0;
266 	for (retries = 0; retries < 10; ++retries) {
267 		dounmount(work, "/proc");
268 		dounmount(work, "/dev");
269 		dounmount(work, "/usr/src");
270 		dounmount(work, "/usr/games");
271 		dounmount(work, "/boot");
272 		dounmount(work, "/usr/local");
273 		dounmount(work, "/construction");
274 		dounmount(work, "/ccache");	/* in case of config change */
275 		dounmount(work, "/distfiles");
276 		dounmount(work, "/packages");
277 		dounmount(work, "/options");
278 		dounmount(work, "/xports");
279 		dounmount(work, "/usr/share");
280 		dounmount(work, "/usr/sbin");
281 		dounmount(work, "/usr/libexec");
282 		dounmount(work, "/usr/libdata");
283 		dounmount(work, "/usr/lib");
284 		dounmount(work, "/usr/include");
285 		dounmount(work, "/usr/bin");
286 		dounmount(work, "/libexec");
287 		dounmount(work, "/lib");
288 		dounmount(work, "/sbin");
289 		dounmount(work, "/bin");
290 		dounmount(work, "");
291 		if (work->mount_error == 0)
292 			break;
293 		sleep(5);
294 		work->mount_error = 0;
295 	}
296 	if (work->mount_error) {
297 		++work->accum_error;
298 		snprintf(work->status, sizeof(work->status),
299 			 "Unable to unmount slot");
300 	}
301 	setNumaDomain(-1);
302 }
303 
304 static
305 void
306 domount(worker_t *work, int type, const char *spath, const char *dpath,
307 	const char *discretefmt)
308 {
309 	const char *sbase;
310 	const char *rwstr;
311 	const char *optstr;
312 	const char *typestr;
313 	const char *debug;
314 	struct stat st;
315 	char *buf;
316 	char *tmp;
317 	int rc;
318 
319 	/*
320 	 * Make target directory if necessary.  This must occur in-order
321 	 * since directories may have to be created under prior mounts
322 	 * in the sequence.
323 	 */
324 	asprintf(&buf, "%s%s", work->basedir, dpath);
325 	if (stat(buf, &st) != 0) {
326 		if (mkdir(buf, 0755) != 0) {
327 			fprintf(stderr, "Command failed: mkdir %s\n", buf);
328 			++work->mount_error;
329 		}
330 	}
331 	free(buf);
332 
333 	/*
334 	 * Setup for mount arguments
335 	 */
336 	rwstr = (type & MOUNT_TYPE_RW) ? "rw" : "ro";
337 	optstr = "";
338 	typestr = "";
339 
340 	switch(type & MOUNT_TYPE_MASK) {
341 	case MOUNT_TYPE_TMPFS:
342 		/*
343 		 * When creating a tmpfs filesystem, make sure the big ones
344 		 * requested are big enough for the worst-case dport (which
345 		 * is usually chromium).  If debugging is turned on, its even
346 		 * worse.  You'd better have enough swap!
347 		 */
348 		debug = getbuildenv("WITH_DEBUG");
349 		typestr = "tmpfs";
350 		if (type & MOUNT_TYPE_BIG)
351 			optstr = debug ? " -o size=128g" : " -o size=64g";
352 		else if (type & MOUNT_TYPE_MED)
353 			optstr = debug ? " -o size=32g" : " -o size=16g";
354 		else
355 			optstr = " -o size=16g";
356 		break;
357 	case MOUNT_TYPE_NULLFS:
358 #if defined(__DragonFly__)
359 		typestr = "null";
360 #else
361 		typestr = "nullfs";
362 #endif
363 		break;
364 	case MOUNT_TYPE_DEVFS:
365 		typestr = "devfs";
366 		break;
367 	case MOUNT_TYPE_PROCFS:
368 		typestr = "procfs";
369 		break;
370 	default:
371 		dfatal("Illegal mount type: %08x", type);
372 		/* NOT REACHED */
373 		break;
374 	}
375 
376 	/*
377 	 * Prefix spath
378 	 */
379 	if (discretefmt) {
380 		sbase = BuildBase;
381 		asprintf(&tmp, discretefmt, work->index);
382 		spath = tmp;
383 	} else {
384 		if (spath[0] == '$') {
385 			++spath;
386 			sbase = SystemPath;
387 			if (strcmp(sbase, "/") == 0)
388 				++sbase;
389 		} else {
390 			sbase = "";
391 		}
392 		tmp = NULL;
393 	}
394 	asprintf(&buf, "%s%s -t %s -o %s %s%s %s%s",
395 		MOUNT_BINARY, optstr, typestr, rwstr,
396 		sbase, spath, work->basedir, dpath);
397 	rc = system(buf);
398 	if (rc) {
399 		fprintf(stderr, "Command failed: %s\n", buf);
400 		++work->mount_error;
401 	}
402 	free(buf);
403 	if (tmp)
404 		free(tmp);
405 }
406 
407 static
408 void
409 dounmount(worker_t *work, const char *rpath)
410 {
411 	char *buf;
412 
413 	asprintf(&buf, "%s%s", work->basedir, rpath);
414 	if (unmount(buf, 0) < 0) {
415 		switch(errno) {
416 		case EPERM:	/* This is probably fatal later on in mount */
417 		case ENOENT:	/* Expected if mount already gone */
418 		case EINVAL:	/* Expected if mount already gone (maybe) */
419 			break;
420 		default:
421 			fprintf(stderr, "Cannot umount %s (%s)\n",
422 				buf, strerror(errno));
423 			++work->mount_error;
424 			break;
425 		}
426 	}
427 	free(buf);
428 }
429 
430 static
431 void
432 makeDiscreteCopies(const char *spath, const char *discretefmt)
433 {
434 	char *src;
435 	char *dst;
436 	char *buf;
437 	struct stat st;
438 	int i;
439 	int rc;
440 
441 	for (i = 0; i < MaxWorkers; ++i) {
442 		setNumaDomain(i);
443 		if (spath[0] == '$') {
444 			if (strcmp(SystemPath, "/") == 0)
445 				asprintf(&src, "%s%s",
446 					 SystemPath + 1, spath + 1);
447 			else
448 				asprintf(&src, "%s%s",
449 					 SystemPath, spath + 1);
450 		} else {
451 			src = strdup(spath);
452 		}
453 		asprintf(&buf, discretefmt, i);
454 		asprintf(&dst, "%s%s", BuildBase, buf);
455 		free(buf);
456 
457 		if (stat(dst, &st) < 0) {
458 			if (mkdir(dst, 0555) < 0) {
459 				dlog(DLOG_ALL, "Template - mkdir %s failed\n",
460 				     dst);
461 				dfatal_errno("Cannot mkdir %s:", dst);
462 			}
463 		}
464 		asprintf(&buf, "chflags -R noschg %s; "
465 			       "rm -rf %s; "
466 			       "cp -Rp %s/. %s",
467 			       dst, dst, src, dst);
468 		rc = system(buf);
469 		dlog(DLOG_ALL | DLOG_FILTER,
470 		     "Template - rc=%d running %s\n", rc, buf);
471 		if (rc)
472 			dfatal("Command failed: %s", buf);
473 		free(buf);
474 		free(src);
475 		free(dst);
476 		setNumaDomain(-1);
477 	}
478 }
479