1 /*
2  * Copyright (c)2004,2015 The DragonFly Project.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  *   Redistributions of source code must retain the above copyright
9  *   notice, this list of conditions and the following disclaimer.
10  *
11  *   Redistributions in binary form must reproduce the above copyright
12  *   notice, this list of conditions and the following disclaimer in
13  *   the documentation and/or other materials provided with the
14  *   distribution.
15  *
16  *   Neither the name of the DragonFly Project nor the names of its
17  *   contributors may be used to endorse or promote products derived
18  *   from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
31  * OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 /*
35  * fn_install.c
36  * Installer Function : Install OS Files.
37  * $Id: fn_install.c,v 1.74 2006/04/18 19:43:48 joerg Exp $
38  */
39 
40 #include <libgen.h>
41 #include <string.h>
42 
43 #define SOURCES_CONF_FILE "usr/share/installer/sources.conf"
44 
45 #ifdef ENABLE_NLS
46 #include <libintl.h>
47 #define _(String) gettext (String)
48 #else
49 #define _(String) (String)
50 #endif
51 
52 #include "libaura/mem.h"
53 #include "libaura/buffer.h"
54 #include "libaura/fspred.h"
55 
56 #include "libdfui/dfui.h"
57 #include "libdfui/system.h"
58 
59 #include "libinstaller/commands.h"
60 #include "libinstaller/confed.h"
61 #include "libinstaller/diskutil.h"
62 #include "libinstaller/functions.h"
63 #include "libinstaller/uiutil.h"
64 
65 #include "flow.h"
66 #include "pathnames.h"
67 #include "fn.h"
68 
69 /*
70  * NOTE: Even though /var/run doesn't need to be backedup, nearly all
71  *	 services depend on it so it is best to leave it on the root.
72  */
73 static const char *nullfs_mountpt[] = {
74 	"/usr/obj", "/var/crash", "/var/cache",
75 	"/var/spool", "/var/log", "/var/tmp",
76 	NULL };
77 static const char *nullfs_mountname[] = {
78 	"/build/usr.obj", "/build/var.crash", "/build/var.cache",
79 	"/build/var.spool", "/build/var.log", "/build/var.tmp",
80 	NULL };
81 
82 static void
83 handle_altfs(struct i_fn_args *a, struct commands *cmds)
84 {
85 	int i;
86 
87 	/*
88 	 * Create directories for null mounts if not specified as a partition.
89 	 * (null mounts are from /build)
90 	 */
91 	for (i = 0; nullfs_mountpt[i]; ++i) {
92 		if (subpartition_find(storage_get_selected_slice(a->s), "%s", nullfs_mountpt[i]) != NULL)
93 			continue;
94 
95 		/*
96 		 * Create directory infrastructure for null-mount if
97 		 * necessary, then issue the null mount(s).
98 		 */
99 		command_add(cmds, "%s%s -p %smnt%s",
100 			    a->os_root, cmd_name(a, "MKDIR"),
101 			    a->os_root, nullfs_mountname[i]);
102 		command_add(cmds, "%s%s -p %smnt%s",
103 			    a->os_root, cmd_name(a, "MKDIR"),
104 			    a->os_root, nullfs_mountpt[i]);
105 		command_add(cmds, "%s%s %smnt%s %smnt%s",
106 		    a->os_root, cmd_name(a, "MOUNT_NULL"),
107 		    a->os_root, nullfs_mountname[i],
108 		    a->os_root, nullfs_mountpt[i]);
109 	}
110 
111 	/*
112 	 * Create directory for /tmp and tmpfs mount if not specified as
113 	 * a partition.
114 	 */
115 	if (subpartition_find(storage_get_selected_slice(a->s), "%s", "/tmp") == NULL) {
116 		command_add(cmds, "%s%s -p %smnt/tmp",
117 			    a->os_root, cmd_name(a, "MKDIR"), a->os_root);
118 		command_add(cmds, "%s%s 1777 %smnt/tmp",
119 			    a->os_root, cmd_name(a, "CHMOD"), a->os_root);
120 		command_add(cmds, "%s%s dummy %smnt/tmp",
121 			    a->os_root, cmd_name(a, "MOUNT_TMPFS"), a->os_root);
122 	}
123 }
124 
125 static void
126 unmount_altfs(struct i_fn_args *a __unused, struct commands *cmds __unused)
127 {
128 	return;
129 #if 0
130 	int i;
131 
132 	/*
133 	 * Unmount null mounts
134 	 */
135 	i = sizeof(nullfs_mountpt) / sizeof(nullfs_mountpt[0]) - 1;
136 	while (i >= 0) {
137 		if (subpartition_find(storage_get_selected_slice(a->s), "%s", nullfs_mountpt[i]) != NULL)
138 			continue;
139 
140 		/*
141 		 * Create directory infrastructure for null-mount if
142 		 * necessary, then issue the null mount(s).
143 		 */
144 		command_add(cmds, "%s%s %smnt%s",
145 			    a->os_root, cmd_name(a, "UMOUNT"),
146 			    a->os_root, nullfs_mountpt[i]);
147 		--i;
148 	}
149 
150 	/*
151 	 * Unmount tmpfs mounts
152 	 */
153 	if (subpartition_find(storage_get_selected_slice(a->s), "%s", "/tmp") == NULL) {
154 		command_add(cmds, "%s%s -p %smnt%s",
155 			    a->os_root, cmd_name(a, "UMOUNT"),
156 			    a->os_root, "/tmp");
157 	}
158 #endif
159 }
160 
161 /*
162  * fn_install_os: actually put DragonFly on a disk.
163  */
164 void
165 fn_install_os(struct i_fn_args *a)
166 {
167 	struct subpartition *sp, *spnext;
168 	struct commands *cmds;
169 	struct command *cmd;
170 	int i, seen_it, prefix, j, needcrypt;
171 	FILE *sources_conf;
172 	char line[256];
173 	char cp_src[64][256];
174 	char file_path[256];
175 	char *string;
176 	int lines = 0;
177 	int nfsidx;
178 
179 	/*
180 	 * Read SOURCES_CONF_FILE and populate our copy sources.
181 	 */
182 	snprintf(file_path, 256, "%s%s", a->os_root, SOURCES_CONF_FILE);
183 	sources_conf = fopen(file_path, "r");
184 	i_log(a, "Reading %s", file_path);
185 	while(fgets(line, 256, sources_conf) != NULL && lines < 63) {
186 		if(strlen(line)>0)
187 			line[strlen(line)-1] = '\0';
188 		strlcpy(cp_src[lines], line, 256);
189 		i_log(a,"Adding %s to copy source table.", cp_src[lines]);
190 		lines++;
191 	}
192 	i_log(a,"Added %i total items to copy source table.", lines);
193 	strcpy(cp_src[lines], "");
194 	fclose(sources_conf);
195 
196 	cmds = commands_new();
197 
198 	/*
199 	 * If swap isn't mounted yet, mount it.
200 	 */
201 	if (measure_activated_swap(a) == 0) {
202 		for (sp = slice_subpartition_first(storage_get_selected_slice(a->s));
203 		    sp != NULL; sp = subpartition_next(sp)) {
204 			if (!subpartition_is_swap(sp))
205 				continue;
206 			command_add(cmds, "%s%s /dev/%s",
207 			    a->os_root,
208 			    cmd_name(a, "SWAPON"),
209 			    subpartition_is_encrypted(sp) ?
210 			    "mapper/swap" : subpartition_get_device_name(sp));
211 		}
212 	}
213 
214 	/*
215 	 * Unmount anything already mounted on /mnt.
216 	 */
217 	unmount_altfs(a, cmds);
218 	unmount_all_under(a, cmds, "%smnt", a->os_root);
219 
220 	/* Check if crypto support is needed */
221 	needcrypt = 0;
222 	for (sp = slice_subpartition_first(storage_get_selected_slice(a->s));
223 	     sp != NULL; sp = subpartition_next(sp)) {
224 		if (subpartition_is_encrypted(sp)) {
225 			needcrypt = 1;
226 			break;
227 		}
228 	}
229 
230 	for (sp = slice_subpartition_first(storage_get_selected_slice(a->s));
231 	     sp != NULL; sp = subpartition_next(sp)) {
232 		if (strcmp(subpartition_get_mountpoint(sp), "/") == 0 ||
233 		    strcmp(subpartition_get_mountpoint(sp), "/build") == 0) {
234 			/* make sure mountpoint directory exists */
235 			command_add(cmds, "%s%s -p %smnt%s",
236 				    a->os_root, cmd_name(a, "MKDIR"),
237 				    a->os_root,
238 				    subpartition_get_mountpoint(sp));
239 			if (use_hammer == 1) {
240 				command_add(cmds, "%s%s /dev/%s %smnt%s",
241 				    a->os_root, cmd_name(a, "MOUNT_HAMMER"),
242 				    (subpartition_is_encrypted(sp) ?
243 				     fn_mapper_name(subpartition_get_device_name(sp), 0) : subpartition_get_device_name(sp)),
244 				    a->os_root,
245 				    subpartition_get_mountpoint(sp));
246 			} else {
247 				command_add(cmds, "%s%s /dev/%s %smnt%s",
248 				    a->os_root, cmd_name(a, "MOUNT"),
249 				    subpartition_is_encrypted(sp) ?
250 				     fn_mapper_name(subpartition_get_device_name(sp), 0) : subpartition_get_device_name(sp),
251 				    a->os_root,
252 				    subpartition_get_mountpoint(sp));
253 			}
254 		}
255 	}
256 
257 	/*
258 	 * Create mount points and mount subpartitions on them.
259 	 */
260 	for (sp = slice_subpartition_first(storage_get_selected_slice(a->s));
261 	     sp != NULL; sp = subpartition_next(sp)) {
262 		if (subpartition_is_swap(sp)) {
263 			/*
264 			 * Set this subpartition as the dump device.
265 			 */
266 			command_add(cmds, "%s%s -v /dev/%s",
267 			    a->os_root, cmd_name(a, "DUMPON"),
268 			    subpartition_is_encrypted(sp) ?
269 			    "mapper/swap" : subpartition_get_device_name(sp));
270 
271 			asprintf(&string, "/dev/%s",
272 			    subpartition_is_encrypted(sp) ?
273 			    "mapper/swap" : subpartition_get_device_name(sp));
274 			config_var_set(rc_conf, "dumpdev", string);
275 			free(string);
276 			continue;
277 		}
278 
279 		/*
280 		 * mount everything except / and /build (which have already
281 		 * been mounted).  This should also get /boot.
282 		 */
283 		if (strcmp(subpartition_get_mountpoint(sp), "/") != 0 &&
284 		    strcmp(subpartition_get_mountpoint(sp), "/build") != 0) {
285 			/* make sure mountpoint directory exists */
286 			command_add(cmds, "%s%s -p %smnt%s",
287 			    a->os_root, cmd_name(a, "MKDIR"),
288 			    a->os_root,
289 			    subpartition_get_mountpoint(sp));
290 			/* Don't mount it if it's TMPFS-backed. */
291 			if (subpartition_is_tmpfsbacked(sp))
292 				continue;
293 			if (subpartition_is_encrypted(sp)) {
294 				command_add(cmds, "%s%s /dev/%s %smnt%s",
295 				    a->os_root, cmd_name(a, "MOUNT"),
296 				    fn_mapper_name(subpartition_get_device_name(sp), 0),
297 				    a->os_root,
298 				    subpartition_get_mountpoint(sp));
299 			} else {
300 				command_add(cmds, "%s%s /dev/%s %smnt%s",
301 				    a->os_root, cmd_name(a, "MOUNT"),
302 				    subpartition_get_device_name(sp),
303 				    a->os_root,
304 				    subpartition_get_mountpoint(sp));
305 			}
306 		}
307 	}
308 
309 	/*
310 	 * Take care of tmpfs and null-mounts from /build
311 	 */
312 	handle_altfs(a, cmds);
313 
314 	/*
315 	 * Actually copy files now.
316 	 */
317 	for (i = 0; cp_src[i] != NULL && cp_src[i][0] != '\0'; i++) {
318 		char *src, *dest, *dn, *tmp_dest;
319 
320 		dest = cp_src[i];
321 
322 		/*
323 		 * If dest would be on an TMPFS-backed
324 		 * mountpoint, don't bother copying it.
325 		 */
326 		sp = subpartition_of(storage_get_selected_slice(a->s),
327 				     "%s%s", a->os_root, &dest[1]);
328 		if (sp != NULL && subpartition_is_tmpfsbacked(sp)) {
329 			continue;
330 		}
331 
332 		/*
333 		 * Create intermediate directories, if needed.
334 		 */
335 		tmp_dest = aura_strdup(dest);
336 		dn = dirname(tmp_dest);
337 		if (is_dir("%s%s", a->os_root, &dn[1]) &&
338 		    !is_dir("%smnt%s", a->os_root, dn)) {
339 			command_add(cmds, "%s%s -p %smnt%s",
340 			    a->os_root, cmd_name(a, "MKDIR"),
341 			    a->os_root, dn);
342 		}
343 		aura_free(tmp_dest, "directory name");
344 
345 		/*
346 		 * If a directory by the same name but with the suffix
347 		 * ".hdd" exists on the installation media, cpdup that
348 		 * instead.  This is particularly useful with /etc, which
349 		 * may have significantly different behaviour on the
350 		 * live CD compared to a standard HDD boot.
351 		 */
352 		if (is_dir("%s%s.hdd", a->os_root, &dest[1]))
353 			asprintf(&src, "%s.hdd", &dest[1]);
354 		else
355 			asprintf(&src, "%s", &dest[1]);
356 
357 		/*
358 		 * Cpdup the chosen file or directory onto the HDD.
359 		 * if it exists on the source.
360 		 */
361 		if (is_dir("%s%s", a->os_root, src) ||
362 		    is_file("%s%s", a->os_root, src)) {
363 			cmd = command_add(cmds, "%s%s %s%s %smnt%s",
364 			    a->os_root, cmd_name(a, "CPDUP"),
365 			    a->os_root, src,
366 			    a->os_root, dest);
367 			command_set_log_mode(cmd, COMMAND_LOG_QUIET);
368 		}
369 	}
370 
371 	/*
372 	 * Now, because cpdup does not cross mount points,
373 	 * we must copy anything that the user might've made a
374 	 * seperate mount point for (e.g. /usr/libdata/lint.)
375 	 */
376 	nfsidx = 0;
377 	sp = NULL;
378 	spnext = slice_subpartition_first(storage_get_selected_slice(a->s));
379 
380 	for (;;) {
381 		const char *mountpt;
382 
383 		/*
384 		 * Iterate nullfs mounts and then partitions
385 		 */
386 		if (nullfs_mountpt[nfsidx]) {
387 			mountpt = nullfs_mountpt[nfsidx];
388 			++nfsidx;
389 		} else {
390 			sp = spnext;
391 			if (sp == NULL)
392 				break;
393 			spnext = subpartition_next(sp);
394 			mountpt = subpartition_get_mountpoint(sp);
395 		}
396 
397 		/*
398 		 * If the subpartition is a swap subpartition or an
399 		 * TMPFS-backed mountpoint, don't try to copy anything
400 		 * into it.
401 		 */
402 		if (sp) {
403 			if (subpartition_is_swap(sp) ||
404 			    subpartition_is_tmpfsbacked(sp)) {
405 				continue;
406 			}
407 		}
408 
409 		/*
410 		 * If the mountpoint doesn't even exist on the installation
411 		 * medium, don't try to copy anything from it!  We assume
412 		 * it's an empty subpartition for the user's needs.
413 		 */
414 		if (!is_dir("%s%s", a->os_root, mountpt + 1))
415 			continue;
416 
417 		/*
418 		 * Don't bother copying the mountpoint IF:
419 		 * - we've already said to copy it, or something besides it
420 		 *   (it's a prefix of something in cp_src); or
421 		 * - we haven't said to copy it
422 		 *   (nothing in cp_src is a prefix of it.)
423 		 */
424 		seen_it = 0;
425 		prefix = 0;
426 		for (i = 0; cp_src[i] != NULL && cp_src[i][0] != '\0'; i++) {
427 			if (strncmp(mountpt, cp_src[i],
428 			    strlen(mountpt)) == 0) {
429 				seen_it = 1;
430 				break;
431 			}
432 			if (strncmp(cp_src[i], mountpt, strlen(cp_src[i])) == 0) {
433 				prefix = 1;
434 			}
435 		}
436 		if (seen_it || !prefix)
437 			continue;
438 
439 		/*
440 		 * Otherwise, cpdup the subpartition.
441 		 *
442 		 * XXX check for .hdd-extended source dirs here, too,
443 		 * eventually - but for now, /etc.hdd will never be
444 		 * the kind of tricky sub-mount-within-a-mount-point
445 		 * that this part of the code is meant to handle.
446 		 */
447 		cmd = command_add(cmds, "%s%s %s%s %smnt%s",
448 			a->os_root, cmd_name(a, "CPDUP"),
449 			a->os_root, mountpt + 1,
450 			a->os_root, mountpt);
451 		command_set_log_mode(cmd, COMMAND_LOG_QUIET);
452 	}
453 
454 	/*
455 	 * Create symlinks.
456 	 */
457 
458 	/* Take care of /sys. */
459 	command_add(cmds, "%s%s -s usr/src/sys %smnt/sys",
460 	    a->os_root, cmd_name(a, "LN"), a->os_root);
461 
462 	/*
463 	 * Make sure /home exists (goes on root mount otherwise).
464 	 */
465 	command_add(cmds, "%s%s -p %smnt/home",
466 		    a->os_root, cmd_name(a, "MKDIR"), a->os_root);
467 
468 	/*
469 	 * XXX check for other possible combinations too?
470 	 */
471 
472 	/*
473 	 * Clean up.  In case some file didn't make it, use rm -f
474 	 */
475 	command_add(cmds, "%s%s -f %smnt/boot/loader.conf",
476 	    a->os_root, cmd_name(a, "RM"), a->os_root);
477 	command_add(cmds, "%s%s -f %smnt/tmp/install.log",
478 	    a->os_root, cmd_name(a, "RM"), a->os_root);
479 	command_add(cmds, "%s%s -f %smnt/tmp/t[12]",
480 	    a->os_root, cmd_name(a, "RM"), a->os_root);
481 	command_add(cmds, "%s%s -f %smnt/tmp/test_in",
482 	    a->os_root, cmd_name(a, "RM"), a->os_root);
483 	command_add(cmds, "%s%s -f %smnt/tmp/test_out",
484 	    a->os_root, cmd_name(a, "RM"), a->os_root);
485 
486 	/*
487 	 * Copy pristine versions over any files we might have installed.
488 	 * This allows the resulting file tree to be customized.
489 	 */
490 	for (i = 0; cp_src[i] != NULL && cp_src[i][0] != '\0'; i++) {
491 		char *src, *dest, *dn, *tmp_dest;
492 
493 		src = cp_src[i];
494 		dest = cp_src[i];
495 
496 		/*
497 		 * Get the directory that the desired thing to
498 		 * copy resides in.
499 		 */
500 		tmp_dest = aura_strdup(dest);
501 		dn = dirname(tmp_dest);
502 
503 		/*
504 		 * If this dir doesn't exist in PRISTINE_DIR
505 		 * on the install media, just skip it.
506 		 */
507 		if (!is_dir("%s%s%s", a->os_root, PRISTINE_DIR, dn)) {
508 			aura_free(tmp_dest, _("directory name"));
509 			continue;
510 		}
511 
512 		/*
513 		 * Create intermediate directories, if needed.
514 		 */
515 		if (!is_dir("%smnt%s", a->os_root, dn)) {
516 			command_add(cmds, "%s%s -p %smnt%s",
517 			    a->os_root, cmd_name(a, "MKDIR"),
518 			    a->os_root, dn);
519 		}
520 		aura_free(tmp_dest, "directory name");
521 
522 		/*
523 		 * Cpdup the chosen file or directory onto the HDD.
524 		 */
525 		cmd = command_add(cmds, "%s%s %s%s %smnt%s",
526 		    a->os_root, cmd_name(a, "CPDUP"),
527 		    a->os_root, src,
528 		    a->os_root, dest);
529 
530 		cmd = command_add(cmds,
531 		    "%s%s %s%s%s %smnt%s",
532 		    a->os_root, cmd_name(a, "CPDUP"),
533 		    a->os_root, PRISTINE_DIR, src,
534 		    a->os_root, dest);
535 		command_set_log_mode(cmd, COMMAND_LOG_QUIET);
536 	}
537 
538 	/*
539 	 * Rebuild the user database, to get rid of any extra users
540 	 * from the LiveCD that aren't supposed to be installed
541 	 * (copying a pristine master.passwd isn't enough.)
542 	 */
543 	command_add(cmds, "%s%s -p -d %smnt/etc %smnt/etc/master.passwd",
544 	    a->os_root, cmd_name(a, "PWD_MKDB"), a->os_root, a->os_root);
545 
546 	/*
547 	 * Create missing directories for special mounts.
548 	 */
549 	command_add(cmds, "%s%s %smnt/proc",
550 	    a->os_root, cmd_name(a, "MKDIR"), a->os_root);
551 	command_add(cmds, "%s%s %smnt/dev",
552 	    a->os_root, cmd_name(a, "MKDIR"), a->os_root);
553 	command_add(cmds, "%s%s %smnt/mnt",
554 	    a->os_root, cmd_name(a, "MKDIR"), a->os_root);
555 
556 	/* Write new fstab. */
557 	command_add(cmds, "%s%s '%s' >%smnt/etc/fstab",
558 	    a->os_root, cmd_name(a, "ECHO"),
559 	    "# Device\t\tMountpoint\tFStype\tOptions\t\tDump\tPass#",
560 	    a->os_root);
561 
562 	for (sp = slice_subpartition_first(storage_get_selected_slice(a->s));
563 	     sp != NULL; sp = subpartition_next(sp)) {
564 		if (strcmp(subpartition_get_mountpoint(sp), "swap") == 0) {
565 			command_add(cmds, "%s%s '/dev/%s\t\tnone\t\tswap\tsw\t\t0\t0' >>%smnt/etc/fstab",
566 			    a->os_root, cmd_name(a, "ECHO"),
567 			    subpartition_is_encrypted(sp) ?
568 			    "mapper/swap" : subpartition_get_device_name(sp),
569 			    a->os_root);
570 			if (subpartition_is_encrypted(sp)) {
571 				command_add(cmds,
572 				    "%s%s 'swap\t/dev/%s\tnone\tnone' >>%smnt/etc/crypttab",
573 				    a->os_root, cmd_name(a, "ECHO"),
574 				    subpartition_get_device_name(sp),
575 				    a->os_root);
576 			}
577 		} else {
578 			const char *fsname;
579 			int order;
580 
581 			/*
582 			 * fs type (/boot is always ufs)
583 			 */
584 			if (strcmp(subpartition_get_mountpoint(sp), "/boot") == 0)
585 				fsname = "ufs";
586 			else if (use_hammer)
587 				fsname = "hammer";
588 			else
589 				fsname = "ufs";
590 
591 			if (strcmp(subpartition_get_mountpoint(sp), "/") == 0)
592 				order = 1;
593 			else
594 				order = 2;
595 
596 			/*
597 			 * Adjust loader.conf for root partition
598 			 */
599 			if (strcmp(subpartition_get_mountpoint(sp), "/") == 0) {
600 				if (subpartition_is_encrypted(sp)) {
601 					command_add(cmds,
602 					    "%s%s 'vfs.root.mountfrom=\"ufs:md0s0\"' >>%smnt/boot/loader.conf",
603 					    a->os_root, cmd_name(a, "ECHO"),
604 					    a->os_root);
605 					command_add(cmds,
606 					    "%s%s 'vfs.root.realroot=\"crypt:%s:%s:%s\"' >>%smnt/boot/loader.conf",
607 					    a->os_root, cmd_name(a, "ECHO"),
608 					    fsname,
609 					    subpartition_get_device_name(sp),
610 					    fn_mapper_name(subpartition_get_device_name(sp), -1),
611 					    a->os_root);
612 				} else {
613 					command_add(cmds,
614 					    "%s%s 'vfs.root.mountfrom=\"%s:%s\"' >>%smnt/boot/loader.conf",
615 					    a->os_root, cmd_name(a, "ECHO"),
616 					    fsname,
617 					    subpartition_get_device_name(sp),
618 					    a->os_root);
619 				}
620 			}
621 			if (subpartition_is_tmpfsbacked(sp)) {
622 				command_add(cmds, "%s%s 'tmpfs\t\t\t%s\t\ttmpfs\trw,-s%luM\t1\t1' >>%smnt/etc/fstab",
623 				    a->os_root, cmd_name(a, "ECHO"),
624 				    subpartition_get_mountpoint(sp),
625 				    subpartition_get_capacity(sp),
626 				    a->os_root);
627 			} else if (subpartition_is_encrypted(sp)) {
628 				command_add(cmds, "%s%s '%s\t/dev/%s\tnone\tnone' >>%smnt/etc/crypttab",
629 				    a->os_root, cmd_name(a, "ECHO"),
630 				    fn_mapper_name(subpartition_get_device_name(sp), -1),
631 				    subpartition_get_device_name(sp),
632 				    a->os_root);
633 				command_add(cmds, "%s%s '/dev/%s\t\t%s\t\t%s\trw\t\t2\t2' >>%smnt/etc/fstab",
634 				    a->os_root, cmd_name(a, "ECHO"),
635 				    fn_mapper_name(subpartition_get_device_name(sp), 0),
636 				    subpartition_get_mountpoint(sp),
637 				    fsname,
638 				    a->os_root);
639 			} else {
640 				command_add(cmds, "%s%s '/dev/%s\t\t%s\t\t%s\trw\t\t%d\t%d' >>%smnt/etc/fstab",
641 				    a->os_root, cmd_name(a, "ECHO"),
642 				    subpartition_get_device_name(sp),
643 				    subpartition_get_mountpoint(sp),
644 				    fsname,
645 				    order, order,
646 				    a->os_root);
647 			}
648 		}
649 	}
650 
651 	/*
652 	 * Take care of NULL mounts from /build for things like /var/crash
653 	 * and /usr/obj if not specified as a discrete partition.
654 	 */
655 	for (j = 0; nullfs_mountpt[j] != NULL; j++) {
656 		if (subpartition_find(storage_get_selected_slice(a->s),
657 				      "%s", nullfs_mountpt[i]) != NULL) {
658 			continue;
659 		}
660 		command_add(cmds,
661 		    "%s%s '%s\t%s\t\tnull\trw\t\t0\t0' >>%smnt/etc/fstab",
662 		    a->os_root, cmd_name(a, "ECHO"),
663 		    nullfs_mountname[j],
664 		    nullfs_mountpt[j],
665 		    a->os_root);
666 	}
667 
668 	/*
669 	 * Take care of /tmp as a tmpfs filesystem
670 	 */
671 	if (subpartition_find(storage_get_selected_slice(a->s), "/tmp") == NULL) {
672 		command_add(cmds,
673 		    "%s%s 'tmpfs\t/tmp\t\ttmpfs\trw\t\t0\t0' >>%smnt/etc/fstab",
674 		    a->os_root, cmd_name(a, "ECHO"), a->os_root);
675 	}
676 
677 	/*
678 	 * Take care of /proc
679 	 */
680 	command_add(cmds, "%s%s '%s' >>%smnt/etc/fstab",
681 	    a->os_root, cmd_name(a, "ECHO"),
682 	    "proc\t\t\t/proc\t\tprocfs\trw\t\t0\t0",
683 	    a->os_root);
684 
685 	/* Backup the disklabel and the log. */
686 	command_add(cmds, "%s%s %s > %smnt/etc/disklabel.%s",
687 	    a->os_root, cmd_name(a, "DISKLABEL64"),
688 	    slice_get_device_name(storage_get_selected_slice(a->s)),
689 	    a->os_root,
690 	    slice_get_device_name(storage_get_selected_slice(a->s)));
691 
692 #if 0
693 	/* 'chflags nohistory' as needed */
694 	for (j = 0; pfs_mountpt[j] != NULL; j++)
695 		if (pfs_nohistory[j] == 1)
696 			command_add(cmds, "%s%s -R nohistory %smnt%s",
697 			    a->os_root, cmd_name(a, "CHFLAGS"),
698 			    a->os_root, pfs_mountpt[j]);
699 #endif
700 
701 	/* Do some preparation if encrypted partitions were configured */
702 	if (needcrypt) {
703 		command_add(cmds,
704 		    "%s%s 'dm_load=\"yes\"' >>%smnt/boot/loader.conf",
705 		    a->os_root, cmd_name(a, "ECHO"),
706 		    a->os_root);
707 		command_add(cmds,
708 		    "%s%s 'dm_target_crypt_load=\"yes\"' >>%smnt/boot/loader.conf",
709 		    a->os_root, cmd_name(a, "ECHO"),
710 		    a->os_root);
711 		command_add(cmds,
712 		    "%s%s 'initrd.img_load=\"YES\"' >>%smnt/boot/loader.conf",
713 		    a->os_root, cmd_name(a, "ECHO"),
714 		    a->os_root);
715 		command_add(cmds,
716 		    "%s%s 'initrd.img_type=\"md_image\"' >>%smnt/boot/loader.conf",
717 		    a->os_root, cmd_name(a, "ECHO"),
718 		    a->os_root);
719 	}
720 
721 	/* Customize stuff here */
722 	if(is_file("%susr/local/bin/after_installation_routines.sh", a->os_root)) {
723 		command_add(cmds, "%susr/local/bin/after_installation_routines.sh",
724 		    a->os_root);
725 	}
726 
727 	/* Save the installation log. */
728 	command_add(cmds, "%s%s %sinstall.log %smnt/var/log/install.log",
729 	    a->os_root, cmd_name(a, "CP"),
730 	    a->tmp, a->os_root);
731 	command_add(cmds, "%s%s 600 %smnt/var/log/install.log",
732 	    a->os_root, cmd_name(a, "CHMOD"), a->os_root);
733 
734 	/*
735 	 * Do it!
736 	 */
737 	/* commands_preview(a->c, cmds); */
738 	if (!commands_execute(a, cmds)) {
739 		inform(a->c, _("%s was not fully installed."), OPERATING_SYSTEM_NAME);
740 		a->result = 0;
741 	} else {
742 		a->result = 1;
743 	}
744 	commands_free(cmds);
745 	cmds = commands_new();
746 
747 	if (a->result) {
748 		config_vars_write(rc_conf, CONFIG_TYPE_SH, "%smnt/etc/rc.conf",
749 		    a->os_root);
750 		config_vars_free(rc_conf);
751 		rc_conf = config_vars_new();
752 	}
753 
754 	/*
755 	 * Unmount everything we mounted on /mnt.  This is done in a seperate
756 	 * command chain, so that partitions are unmounted, even if an error
757 	 * occurs in one of the preceding commands, or it is cancelled.
758 	 */
759 	unmount_altfs(a, cmds);
760 	unmount_all_under(a, cmds, "%smnt", a->os_root);
761 
762 	/*
763 	 * Once everything is unmounted, if the install went successfully,
764 	 * make sure once and for all that the disklabel is bootable.
765 	 */
766 	if (a->result)
767 		command_add(cmds, "%s%s -B %s",
768 		    a->os_root, cmd_name(a, "DISKLABEL64"),
769 		    slice_get_device_name(storage_get_selected_slice(a->s)));
770 
771 	if (!commands_execute(a, cmds))
772 		inform(a->c, _("Warning: subpartitions were not correctly unmounted."));
773 
774 	commands_free(cmds);
775 
776 	/*
777 	 * Finally, remove all swap and any mappings.
778 	 */
779 	if (swapoff_all(a) == NULL)
780 		inform(a->c, _("Warning: swap could not be turned off."));
781 	if (remove_all_mappings(a) == NULL)
782 		inform(a->c, _("Warning: mappings could not be removed."));
783 }
784 
785 /*
786  * /dev/mapper/
787  *
788  * (result is persistant until next call)
789  */
790 const char *
791 fn_mapper_name(const char *mountpt, int withdev)
792 {
793 	const char *src;
794 	static char *save;
795 
796 	src = strrchr(mountpt, '/');
797 	if (src == NULL || src[1] == 0)
798 		src = "root";
799 	else
800 		++src;
801 
802 	if (save)
803 		free(save);
804 	switch(withdev) {
805 	case -1:
806 		asprintf(&save, "%s", src);
807 		break;
808 	case 0:
809 		asprintf(&save, "mapper/%s", src);
810 		break;
811 	case 1:
812 	default:
813 		asprintf(&save, "/dev/mapper/%s", src);
814 		break;
815 	}
816 	return save;
817 }
818