1 /* 2 * Copyright (c) 2019 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 int rc; 58 int fd; 59 60 rc = 0; 61 asprintf(&goodbuf, "%s/.template.good", BuildBase); 62 63 /* 64 * Conditionally create the template and discrete copies of certain 65 * directories if we think we are missing things. 66 */ 67 if (force == 0) { 68 asprintf(&buf, "%s/Template", BuildBase); 69 if (stat(buf, &st) < 0) 70 force = 1; 71 free(buf); 72 73 if (stat(goodbuf, &st) < 0) 74 force = 1; 75 } 76 77 dlog(DLOG_ALL, "Check Template: %s\n", 78 (force ? "Must-Create" : "Good")); 79 80 /* 81 * Create the template 82 */ 83 if (force) { 84 remove(goodbuf); /* ignore exit code */ 85 86 rc = 0; 87 asprintf(&buf, "%s/mktemplate %s %s/Template", 88 SCRIPTPATH(SCRIPTDIR), SystemPath, BuildBase); 89 rc = system(buf); 90 if (rc) 91 dfatal("Command failed: %s\n", buf); 92 dlog(DLOG_ALL | DLOG_FILTER, 93 "Template - rc=%d running %s\n", rc, buf); 94 free(buf); 95 96 /* 97 * Make discrete copies of certain extremely heavily used 98 * but small directories. 99 */ 100 if (force) { 101 makeDiscreteCopies("$/bin", "/bin.%03d"); 102 makeDiscreteCopies("$/lib", "/lib.%03d"); 103 makeDiscreteCopies("$/libexec", "/libexec.%03d"); 104 makeDiscreteCopies("$/usr/bin", "/usr.bin.%03d"); 105 } 106 107 /* 108 * Mark the template good... ah, do a sync() to really 109 * be sure that it can't get corrupted. 110 */ 111 sync(); 112 fd = open(goodbuf, O_RDWR|O_CREAT|O_TRUNC, 0644); 113 dassert_errno(fd >= 0, "could not create %s", goodbuf); 114 close(fd); 115 116 dlog(DLOG_ALL | DLOG_FILTER, "Template - done\n"); 117 } 118 free(goodbuf); 119 120 return force; 121 } 122 123 void 124 DoDestroyTemplate(void) 125 { 126 struct stat st; 127 char *path; 128 char *buf; 129 int rc; 130 131 /* 132 * NOTE: rm -rf safety, use a fixed name 'Template' to ensure we 133 * do not accidently blow something up. 134 */ 135 asprintf(&path, "%s/Template", BuildBase); 136 if (stat(path, &st) == 0) { 137 asprintf(&buf, "chflags -R noschg %s; /bin/rm -rf %s", 138 path, path); 139 rc = system(buf); 140 if (rc) 141 dfatal("Command failed: %s (ignored)\n", buf); 142 free(buf); 143 } 144 free(path); 145 } 146 147 /* 148 * Called by the worker support thread to install a new worker 149 * filesystem topology. 150 */ 151 void 152 DoWorkerMounts(worker_t *work) 153 { 154 char *buf; 155 int rc; 156 157 /* 158 * Generate required mounts, domount() will mkdir() the target 159 * directory if necessary and prefix spath with SystemPath if 160 * it starts with $/ 161 */ 162 domount(work, TMPFS_RW, "dummy", "", NULL); 163 asprintf(&buf, "%s/usr", work->basedir); 164 if (mkdir(buf, 0755) != 0) { 165 fprintf(stderr, "Command failed: mkdir %s\n", buf); 166 ++work->mount_error; 167 } 168 domount(work, NULLFS_RO, "$/boot", "/boot", NULL); 169 domount(work, TMPFS_RW, "dummy", "/boot/modules.local", NULL); 170 domount(work, DEVFS_RW, "dummy", "/dev", NULL); 171 domount(work, PROCFS_RO, "dummy", "/proc", NULL); 172 domount(work, NULLFS_RO, "$/bin", "/bin", "/bin.%03d"); 173 domount(work, NULLFS_RO, "$/sbin", "/sbin", NULL); 174 domount(work, NULLFS_RO, "$/lib", "/lib", "/lib.%03d"); 175 domount(work, NULLFS_RO, "$/libexec", "/libexec", "/libexec.%03d"); 176 domount(work, NULLFS_RO, "$/usr/bin", "/usr/bin", "/usr.bin.%03d"); 177 domount(work, NULLFS_RO, "$/usr/include", "/usr/include", NULL); 178 domount(work, NULLFS_RO, "$/usr/lib", "/usr/lib", NULL); 179 domount(work, NULLFS_RO, "$/usr/libdata", "/usr/libdata", NULL); 180 domount(work, NULLFS_RO, "$/usr/libexec", "/usr/libexec", NULL); 181 domount(work, NULLFS_RO, "$/usr/sbin", "/usr/sbin", NULL); 182 domount(work, NULLFS_RO, "$/usr/share", "/usr/share", NULL); 183 domount(work, TMPFS_RW, "dummy", "/usr/local", NULL); 184 domount(work, NULLFS_RO, "$/usr/games", "/usr/games", NULL); 185 /* domount(work, NULLFS_RO, "$/usr/src", "/usr/src", NULL);*/ 186 domount(work, NULLFS_RO, DPortsPath, "/xports", NULL); 187 domount(work, NULLFS_RW, OptionsPath, "/options", NULL); 188 domount(work, NULLFS_RW, PackagesPath, "/packages", NULL); 189 domount(work, NULLFS_RW, DistFilesPath, "/distfiles", NULL); 190 domount(work, TMPFS_RW_BIG, "dummy", "/construction", NULL); 191 if (UseCCache) 192 domount(work, TMPFS_RW_BIG, "dummy", "/ccache", NULL); 193 194 /* 195 * NOTE: Uses blah/. to prevent cp from creating 'Template' under 196 * work->basedir. We want to start with the content. 197 */ 198 asprintf(&buf, "cp -Rp %s/Template/. %s", BuildBase, work->basedir); 199 rc = system(buf); 200 if (rc) { 201 fprintf(stderr, "Command failed: %s\n", buf); 202 ++work->accum_error; 203 snprintf(work->status, sizeof(work->status), 204 "Template copy failed"); 205 } 206 free(buf); 207 } 208 209 /* 210 * Called by the worker support thread to remove a worker 211 * filesystem topology. 212 */ 213 void 214 DoWorkerUnmounts(worker_t *work) 215 { 216 int retries; 217 218 work->mount_error = 0; 219 for (retries = 0; retries < 10; ++retries) { 220 dounmount(work, "/proc"); 221 dounmount(work, "/dev"); 222 /* dounmount(work, "/usr/src");*/ 223 dounmount(work, "/usr/games"); 224 dounmount(work, "/boot/modules.local"); 225 dounmount(work, "/boot"); 226 dounmount(work, "/usr/local"); 227 dounmount(work, "/construction"); 228 dounmount(work, "/ccache"); /* in case of config change */ 229 dounmount(work, "/distfiles"); 230 dounmount(work, "/packages"); 231 dounmount(work, "/options"); 232 dounmount(work, "/xports"); 233 dounmount(work, "/usr/share"); 234 dounmount(work, "/usr/sbin"); 235 dounmount(work, "/usr/libexec"); 236 dounmount(work, "/usr/libdata"); 237 dounmount(work, "/usr/lib"); 238 dounmount(work, "/usr/include"); 239 dounmount(work, "/usr/bin"); 240 dounmount(work, "/libexec"); 241 dounmount(work, "/lib"); 242 dounmount(work, "/sbin"); 243 dounmount(work, "/bin"); 244 dounmount(work, ""); 245 if (work->mount_error == 0) 246 break; 247 sleep(5); 248 work->mount_error = 0; 249 } 250 if (work->mount_error) { 251 ++work->accum_error; 252 snprintf(work->status, sizeof(work->status), 253 "Unable to unmount slot"); 254 } 255 } 256 257 static 258 void 259 domount(worker_t *work, int type, const char *spath, const char *dpath, 260 const char *discretefmt) 261 { 262 const char *prog; 263 const char *sbase; 264 const char *rwstr; 265 const char *optstr; 266 struct stat st; 267 char *buf; 268 char *tmp; 269 int rc; 270 271 /* 272 * Make target directory if necessary. This must occur in-order 273 * since directories may have to be created under prior mounts 274 * in the sequence. 275 */ 276 asprintf(&buf, "%s%s", work->basedir, dpath); 277 if (stat(buf, &st) != 0) { 278 if (mkdir(buf, 0755) != 0) { 279 fprintf(stderr, "Command failed: mkdir %s\n", buf); 280 ++work->mount_error; 281 } 282 } 283 free(buf); 284 285 /* 286 * Setup for mount arguments 287 */ 288 rwstr = (type & MOUNT_TYPE_RW) ? "rw" : "ro"; 289 optstr = ""; 290 291 switch(type & MOUNT_TYPE_MASK) { 292 case MOUNT_TYPE_TMPFS: 293 prog = MOUNT_TMPFS_BINARY; 294 if (type & MOUNT_TYPE_BIG) 295 optstr = " -s 64g"; 296 else 297 optstr = " -s 16g"; 298 break; 299 case MOUNT_TYPE_NULLFS: 300 prog = MOUNT_NULLFS_BINARY; 301 break; 302 case MOUNT_TYPE_DEVFS: 303 prog = MOUNT_DEVFS_BINARY; 304 break; 305 case MOUNT_TYPE_PROCFS: 306 prog = MOUNT_PROCFS_BINARY; 307 break; 308 default: 309 dfatal("Illegal mount type: %08x", type); 310 /* NOT REACHED */ 311 prog = "/bin/hell"; 312 break; 313 } 314 315 /* 316 * Prefix spath 317 */ 318 if (discretefmt) { 319 sbase = BuildBase; 320 asprintf(&tmp, discretefmt, work->index); 321 spath = tmp; 322 } else { 323 if (spath[0] == '$') { 324 ++spath; 325 sbase = SystemPath; 326 if (strcmp(sbase, "/") == 0) 327 ++sbase; 328 } else { 329 sbase = ""; 330 } 331 tmp = NULL; 332 } 333 asprintf(&buf, "%s%s -o %s %s%s %s%s", 334 prog, optstr, rwstr, 335 sbase, spath, work->basedir, dpath); 336 rc = system(buf); 337 if (rc) { 338 fprintf(stderr, "Command failed: %s\n", buf); 339 ++work->mount_error; 340 } 341 free(buf); 342 if (tmp) 343 free(tmp); 344 } 345 346 static 347 void 348 dounmount(worker_t *work, const char *rpath) 349 { 350 char *buf; 351 352 asprintf(&buf, "%s%s", work->basedir, rpath); 353 if (unmount(buf, 0) < 0) { 354 switch(errno) { 355 case ENOENT: 356 case EINVAL: 357 break; 358 default: 359 fprintf(stderr, "Cannot umount %s (%s)\n", 360 buf, strerror(errno)); 361 ++work->mount_error; 362 break; 363 } 364 } 365 free(buf); 366 } 367 368 static 369 void 370 makeDiscreteCopies(const char *spath, const char *discretefmt) 371 { 372 char *src; 373 char *dst; 374 char *buf; 375 struct stat st; 376 int i; 377 int rc; 378 379 for (i = 0; i < MaxWorkers; ++i) { 380 if (spath[0] == '$') { 381 if (strcmp(SystemPath, "/") == 0) 382 asprintf(&src, "%s%s", 383 SystemPath + 1, spath + 1); 384 else 385 asprintf(&src, "%s%s", 386 SystemPath, spath + 1); 387 } else { 388 src = strdup(spath); 389 } 390 asprintf(&buf, discretefmt, i); 391 asprintf(&dst, "%s%s", BuildBase, buf); 392 free(buf); 393 394 if (stat(dst, &st) < 0) { 395 if (mkdir(dst, 0555) < 0) { 396 dlog(DLOG_ALL, "Template - mkdir %s failed\n", 397 dst); 398 dfatal_errno("Cannot mkdir %s:", dst); 399 } 400 } 401 asprintf(&buf, "chflags -R noschg %s; " 402 "rm -rf %s; " 403 "cp -Rp %s/. %s", 404 dst, dst, src, dst); 405 rc = system(buf); 406 dlog(DLOG_ALL | DLOG_FILTER, 407 "Template - rc=%d running %s\n", rc, buf); 408 if (rc) 409 dfatal("Command failed: %s", buf); 410 free(buf); 411 free(src); 412 free(dst); 413 } 414 } 415