1 /*
2  * Copyright (c)2004 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 static const char *pfs_mountpt[8] = {"/var", "/tmp", "/usr", "/home",
70 	"/usr/obj", "/var/crash", "/var/tmp", NULL};
71 
72 static const int pfs_nohistory[8] = {0, 1, 0, 0, 1, 1, 1};
73 
74 static void
75 handle_pfs(struct i_fn_args *a, struct commands *cmds)
76 {
77 	int j;
78 
79 	/*
80 	 * Create PFS root directory.
81 	 */
82 	command_add(cmds, "%s%s -p %smnt/pfs",
83 	    a->os_root, cmd_name(a, "MKDIR"),
84 	    a->os_root);
85 
86 	for (j = 0; pfs_mountpt[j] != NULL; j++) {
87 		/*
88 		 * We have a PFS for a subdirectory, e.g. /var/crash, so we
89 		 * need to create /pfs/var.crash
90 		 */
91 		if (rindex(pfs_mountpt[j]+1, '/') != NULL) {
92 			command_add(cmds, "%s%s pfs-master %smnt/pfs%s.%s",
93 			    a->os_root, cmd_name(a, "HAMMER"),
94 			    a->os_root, dirname(pfs_mountpt[j]),
95 			    basename(pfs_mountpt[j]));
96 			command_add(cmds, "%s%s -p %smnt%s",
97 			    a->os_root, cmd_name(a, "MKDIR"),
98 			    a->os_root, pfs_mountpt[j]);
99 			command_add(cmds, "%s%s %smnt/pfs%s.%s %smnt%s",
100 			    a->os_root, cmd_name(a, "MOUNT_NULL"),
101 			    a->os_root, dirname(pfs_mountpt[j]),
102 			    basename(pfs_mountpt[j]),
103 			    a->os_root, pfs_mountpt[j]);
104 		} else {
105 			command_add(cmds, "%s%s pfs-master %smnt/pfs%s",
106 			    a->os_root, cmd_name(a, "HAMMER"),
107 			    a->os_root, pfs_mountpt[j]);
108 			command_add(cmds, "%s%s -p %smnt%s",
109 			    a->os_root, cmd_name(a, "MKDIR"),
110 			    a->os_root, pfs_mountpt[j]);
111 			command_add(cmds, "%s%s %smnt/pfs%s %smnt%s",
112 			    a->os_root, cmd_name(a, "MOUNT_NULL"),
113 			    a->os_root, pfs_mountpt[j],
114 			    a->os_root, pfs_mountpt[j]);
115 		}
116 	}
117 }
118 
119 /*
120  * fn_install_os: actually put DragonFly on a disk.
121  */
122 void
123 fn_install_os(struct i_fn_args *a)
124 {
125 	struct subpartition *sp;
126 	struct commands *cmds;
127 	struct command *cmd;
128 	int i, seen_it, prefix, j, needcrypt;
129 	FILE *sources_conf;
130 	char line[256];
131 	char cp_src[64][256];
132 	char file_path[256];
133 	char *string;
134 	int lines = 0;
135 
136 	/*
137 	 * Read SOURCES_CONF_FILE and populate our copy sources.
138 	 */
139 	snprintf(file_path, 256, "%s%s", a->os_root, SOURCES_CONF_FILE);
140 	sources_conf = fopen(file_path, "r");
141 	i_log(a, "Reading %s", file_path);
142 	while(fgets(line, 256, sources_conf) != NULL && lines < 63) {
143 		if(strlen(line)>0)
144 			line[strlen(line)-1] = '\0';
145 		strlcpy(cp_src[lines], line, 256);
146 		i_log(a,"Adding %s to copy source table.", cp_src[lines]);
147 		lines++;
148 	}
149 	i_log(a,"Added %i total items to copy source table.", lines);
150 	strcpy(cp_src[lines], "");
151 	fclose(sources_conf);
152 
153 	cmds = commands_new();
154 
155 	/*
156 	 * If swap isn't mounted yet, mount it.
157 	 */
158 	if (measure_activated_swap(a) == 0) {
159 		for (sp = slice_subpartition_first(storage_get_selected_slice(a->s));
160 		    sp != NULL; sp = subpartition_next(sp)) {
161 			if (!subpartition_is_swap(sp))
162 				continue;
163 			command_add(cmds, "%s%s /dev/%s",
164 			    a->os_root,
165 			    cmd_name(a, "SWAPON"),
166 			    subpartition_is_encrypted(sp) ?
167 			    "mapper/swap" : subpartition_get_device_name(sp));
168 		}
169 	}
170 
171 	/*
172 	 * Unmount anything already mounted on /mnt.
173 	 */
174 	unmount_all_under(a, cmds, "%smnt", a->os_root);
175 
176 	/* Check if crypto support is needed */
177 	needcrypt = 0;
178 	for (sp = slice_subpartition_first(storage_get_selected_slice(a->s));
179 	     sp != NULL; sp = subpartition_next(sp)) {
180 		if (subpartition_is_encrypted(sp)) {
181 			needcrypt = 1;
182 			break;
183 		}
184 	}
185 
186 	for (sp = slice_subpartition_first(storage_get_selected_slice(a->s));
187 	     sp != NULL; sp = subpartition_next(sp)) {
188 		if (strcmp(subpartition_get_mountpoint(sp), "/") == 0) {
189 			if (use_hammer == 1) {
190 				command_add(cmds, "%s%s /dev/%s %smnt%s",
191 				    a->os_root, cmd_name(a, "MOUNT_HAMMER"),
192 				    subpartition_is_encrypted(sp) ?
193 				    "mapper/root" : subpartition_get_device_name(sp),
194 				    a->os_root,
195 				    subpartition_get_mountpoint(sp));
196 			} else {
197 				command_add(cmds, "%s%s /dev/%s %smnt%s",
198 				    a->os_root, cmd_name(a, "MOUNT"),
199 				    subpartition_get_device_name(sp),
200 				    a->os_root,
201 				    subpartition_get_mountpoint(sp));
202 			}
203 		}
204 	}
205 
206 	/*
207 	 * Create mount points and mount subpartitions on them.
208 	 */
209 	for (sp = slice_subpartition_first(storage_get_selected_slice(a->s));
210 	     sp != NULL; sp = subpartition_next(sp)) {
211 		if (subpartition_is_swap(sp)) {
212 			/*
213 			 * Set this subpartition as the dump device.
214 			 */
215 			if (subpartition_get_capacity(sp) < storage_get_memsize(a->s))
216 				continue;
217 
218 			command_add(cmds, "%s%s -v /dev/%s",
219 			    a->os_root, cmd_name(a, "DUMPON"),
220 			    subpartition_is_encrypted(sp) ?
221 			    "mapper/swap" : subpartition_get_device_name(sp));
222 
223 			asprintf(&string, "/dev/%s",
224 			    subpartition_is_encrypted(sp) ?
225 			    "mapper/swap" : subpartition_get_device_name(sp));
226 			config_var_set(rc_conf, "dumpdev", string);
227 			free(string);
228 			continue;
229 		}
230 
231 		if (use_hammer == 0) {
232 			/* / is already mounted */
233 			if (strcmp(subpartition_get_mountpoint(sp), "/") != 0) {
234 				command_add(cmds, "%s%s -p %smnt%s",
235 				    a->os_root, cmd_name(a, "MKDIR"),
236 				    a->os_root,
237 				    subpartition_get_mountpoint(sp));
238 				/* Don't mount it if it's TMPFS-backed. */
239 				if (subpartition_is_tmpfsbacked(sp))
240 					continue;
241 				if (subpartition_is_encrypted(sp)) {
242 					command_add(cmds, "%s%s /dev/mapper/%s %smnt%s",
243 					    a->os_root, cmd_name(a, "MOUNT"),
244 					    subpartition_get_mountpoint(sp) + 1,
245 					    a->os_root,
246 					    subpartition_get_mountpoint(sp));
247 				} else {
248 					command_add(cmds, "%s%s /dev/%s %smnt%s",
249 					    a->os_root, cmd_name(a, "MOUNT"),
250 					    subpartition_get_device_name(sp),
251 					    a->os_root,
252 					    subpartition_get_mountpoint(sp));
253 				}
254 			}
255 		} else if (strcmp(subpartition_get_mountpoint(sp), "/boot") == 0) {
256 			command_add(cmds, "%s%s -p %smnt%s",
257 			    a->os_root, cmd_name(a, "MKDIR"),
258 			    a->os_root,
259 			    subpartition_get_mountpoint(sp));
260 			command_add(cmds, "%s%s /dev/%s %smnt%s",
261 			    a->os_root, cmd_name(a, "MOUNT"),
262 			    subpartition_get_device_name(sp),
263 			    a->os_root,
264 			    subpartition_get_mountpoint(sp));
265 		}
266 	}
267 
268 	/*
269 	 * Take care of HAMMER PFS.
270 	 */
271 	if (use_hammer == 1)
272 		handle_pfs(a, cmds);
273 
274 	/*
275 	 * Actually copy files now.
276 	 */
277 
278 	for (i = 0; cp_src[i] != NULL && cp_src[i][0] != '\0'; i++) {
279 		char *src, *dest, *dn, *tmp_dest;
280 
281 		dest = cp_src[i];
282 
283 		/*
284 		 * If dest would be on an TMPFS-backed
285 		 * mountpoint, don't bother copying it.
286 		 */
287 		sp = subpartition_of(storage_get_selected_slice(a->s),
288 				     "%s%s", a->os_root, &dest[1]);
289 		if (sp != NULL && subpartition_is_tmpfsbacked(sp)) {
290 			continue;
291 		}
292 
293 		/*
294 		 * Create intermediate directories, if needed.
295 		 */
296 		tmp_dest = aura_strdup(dest);
297 		dn = dirname(tmp_dest);
298 		if (is_dir("%s%s", a->os_root, &dn[1]) &&
299 		    !is_dir("%smnt%s", a->os_root, dn)) {
300 			command_add(cmds, "%s%s -p %smnt%s",
301 			    a->os_root, cmd_name(a, "MKDIR"),
302 			    a->os_root, dn);
303 		}
304 		aura_free(tmp_dest, "directory name");
305 
306 		/*
307 		 * If a directory by the same name but with the suffix
308 		 * ".hdd" exists on the installation media, cpdup that
309 		 * instead.  This is particularly useful with /etc, which
310 		 * may have significantly different behaviour on the
311 		 * live CD compared to a standard HDD boot.
312 		 */
313 		if (is_dir("%s%s.hdd", a->os_root, &dest[1]))
314 			asprintf(&src, "%s.hdd", &dest[1]);
315 		else
316 			asprintf(&src, "%s", &dest[1]);
317 
318 		if (is_dir("%s%s", a->os_root, src) || is_file("%s%s", a->os_root, src)) {
319 			/*
320 			 * Cpdup the chosen file or directory onto the HDD.
321 			 * if it exists on the source.
322 			 */
323 			cmd = command_add(cmds, "%s%s %s%s %smnt%s",
324 			    a->os_root, cmd_name(a, "CPDUP"),
325 			    a->os_root, src,
326 			    a->os_root, dest);
327 			command_set_log_mode(cmd, COMMAND_LOG_QUIET);
328 		}
329 	}
330 
331 	/*
332 	 * Now, because cpdup does not cross mount points,
333 	 * we must copy anything that the user might've made a
334 	 * seperate mount point for (e.g. /usr/libdata/lint.)
335 	 */
336 	for (sp = slice_subpartition_first(storage_get_selected_slice(a->s));
337 	     sp != NULL; sp = subpartition_next(sp)) {
338 		/*
339 		 * If the subpartition is a swap subpartition or an
340 		 * TMPFS-backed mountpoint, don't try to copy anything
341 		 * into it.
342 		 */
343 		if (subpartition_is_swap(sp) || subpartition_is_tmpfsbacked(sp))
344 			continue;
345 
346 		/*
347 		 * If the mountpoint doesn't even exist on the installation
348 		 * medium, don't try to copy anything from it!  We assume
349 		 * it's an empty subpartition for the user's needs.
350 		 */
351 		if (!is_dir("%s%s", a->os_root, &subpartition_get_mountpoint(sp)[1]))
352 			continue;
353 
354 		/*
355 		 * Don't bother copying the mountpoint IF:
356 		 * - we've already said to copy it, or something besides it
357 		 *   (it's a prefix of something in cp_src); or
358 		 * - we haven't said to copy it
359 		 *   (nothing in cp_src is a prefix of it.)
360 		 */
361 		seen_it = 0;
362 		prefix = 0;
363 		for (i = 0; cp_src[i] != NULL && cp_src[i][0] != '\0'; i++) {
364 			if (strncmp(subpartition_get_mountpoint(sp), cp_src[i],
365 			    strlen(subpartition_get_mountpoint(sp))) == 0) {
366 				seen_it = 1;
367 				break;
368 			}
369 			if (strncmp(cp_src[i], subpartition_get_mountpoint(sp),
370 			    strlen(cp_src[i])) == 0) {
371 				prefix = 1;
372 			}
373 		}
374 		if (seen_it || !prefix)
375 			continue;
376 
377 		/*
378 		 * Otherwise, cpdup the subpartition.
379 		 *
380 		 * XXX check for .hdd-extended source dirs here, too,
381 		 * eventually - but for now, /etc.hdd will never be
382 		 * the kind of tricky sub-mount-within-a-mount-point
383 		 * that this part of the code is meant to handle.
384 		 */
385 		cmd = command_add(cmds, "%s%s %s%s %smnt%s",
386 		    a->os_root, cmd_name(a, "CPDUP"),
387 		    a->os_root, &subpartition_get_mountpoint(sp)[1],
388 		    a->os_root, subpartition_get_mountpoint(sp));
389 		command_set_log_mode(cmd, COMMAND_LOG_QUIET);
390 	}
391 
392 	/*
393 	 * Create symlinks.
394 	 */
395 
396 	/* Take care of /sys. */
397 	command_add(cmds, "%s%s -s usr/src/sys %smnt/sys",
398 	    a->os_root, cmd_name(a, "LN"), a->os_root);
399 
400 	/*
401 	 * If the user has both /var and /tmp subpartitions,
402 	 * symlink /var/tmp to /tmp.
403 	 */
404 	if (subpartition_find(storage_get_selected_slice(a->s), "/tmp") != NULL &&
405 	    subpartition_find(storage_get_selected_slice(a->s), "/var") != NULL) {
406 		command_add(cmds, "%s%s 1777 %smnt/tmp",
407 		    a->os_root, cmd_name(a, "CHMOD"), a->os_root);
408 		command_add(cmds, "%s%s -rf %smnt/var/tmp",
409 		    a->os_root, cmd_name(a, "RM"), a->os_root);
410 		command_add(cmds, "%s%s -s /tmp %smnt/var/tmp",
411 		    a->os_root, cmd_name(a, "LN"), a->os_root);
412 	}
413 
414 	/*
415 	 * If the user has /var, but no /tmp,
416 	 * symlink /tmp to /var/tmp.
417 	 */
418 	if (subpartition_find(storage_get_selected_slice(a->s), "/tmp") == NULL &&
419 	    subpartition_find(storage_get_selected_slice(a->s), "/var") != NULL) {
420 		command_add(cmds, "%s%s -rf %smnt/tmp",
421 		    a->os_root, cmd_name(a, "RM"), a->os_root);
422 		command_add(cmds, "%s%s -s /var/tmp %smnt/tmp",
423 		    a->os_root, cmd_name(a, "LN"), a->os_root);
424 	}
425 
426 	/*
427 	 * If the user has /usr, but no /home,
428 	 * symlink /home to /usr/home.
429 	 */
430 	if (subpartition_find(storage_get_selected_slice(a->s), "/home") == NULL &&
431 	    subpartition_find(storage_get_selected_slice(a->s), "/usr") != NULL) {
432 		command_add(cmds, "%s%s -rf %smnt/home",
433 		    a->os_root, cmd_name(a, "RM"), a->os_root);
434 		command_add(cmds, "%s%s %smnt/usr/home",
435 		    a->os_root, cmd_name(a, "MKDIR"), a->os_root);
436 		command_add(cmds, "%s%s -s /usr/home %smnt/home",
437 		    a->os_root, cmd_name(a, "LN"), a->os_root);
438 	}
439 
440 	/*
441 	 * XXX check for other possible combinations too?
442 	 */
443 
444 	/*
445 	 * Clean up.  In case some file didn't make it, use rm -f
446 	 */
447 	command_add(cmds, "%s%s -f %smnt/boot/loader.conf",
448 	    a->os_root, cmd_name(a, "RM"), a->os_root);
449 	command_add(cmds, "%s%s -f %smnt/tmp/install.log",
450 	    a->os_root, cmd_name(a, "RM"), a->os_root);
451 	command_add(cmds, "%s%s -f %smnt/tmp/t[12]",
452 	    a->os_root, cmd_name(a, "RM"), a->os_root);
453 	command_add(cmds, "%s%s -f %smnt/tmp/test_in",
454 	    a->os_root, cmd_name(a, "RM"), a->os_root);
455 	command_add(cmds, "%s%s -f %smnt/tmp/test_out",
456 	    a->os_root, cmd_name(a, "RM"), a->os_root);
457 
458 	/*
459 	 * Copy pristine versions over any files we might have installed.
460 	 * This allows the resulting file tree to be customized.
461 	 */
462 	for (i = 0; cp_src[i] != NULL && cp_src[i][0] != '\0'; i++) {
463 		char *src, *dest, *dn, *tmp_dest;
464 
465 		src = cp_src[i];
466 		dest = cp_src[i];
467 
468 		/*
469 		 * Get the directory that the desired thing to
470 		 * copy resides in.
471 		 */
472 		tmp_dest = aura_strdup(dest);
473 		dn = dirname(tmp_dest);
474 
475 		/*
476 		 * If this dir doesn't exist in PRISTINE_DIR
477 		 * on the install media, just skip it.
478 		 */
479 		if (!is_dir("%s%s%s", a->os_root, PRISTINE_DIR, dn)) {
480 			aura_free(tmp_dest, _("directory name"));
481 			continue;
482 		}
483 
484 		/*
485 		 * Create intermediate directories, if needed.
486 		 */
487 		if (!is_dir("%smnt%s", a->os_root, dn)) {
488 			command_add(cmds, "%s%s -p %smnt%s",
489 			    a->os_root, cmd_name(a, "MKDIR"),
490 			    a->os_root, dn);
491 		}
492 		aura_free(tmp_dest, "directory name");
493 
494 		/*
495 		 * Cpdup the chosen file or directory onto the HDD.
496 		 */
497 		cmd = command_add(cmds, "%s%s %s%s %smnt%s",
498 		    a->os_root, cmd_name(a, "CPDUP"),
499 		    a->os_root, src,
500 		    a->os_root, dest);
501 
502 		cmd = command_add(cmds,
503 		    "%s%s %s%s%s %smnt%s",
504 		    a->os_root, cmd_name(a, "CPDUP"),
505 		    a->os_root, PRISTINE_DIR, src,
506 		    a->os_root, dest);
507 		command_set_log_mode(cmd, COMMAND_LOG_QUIET);
508 	}
509 
510 	/*
511 	 * Rebuild the user database, to get rid of any extra users
512 	 * from the LiveCD that aren't supposed to be installed
513 	 * (copying a pristine master.passwd isn't enough.)
514 	 */
515 	command_add(cmds, "%s%s -p -d %smnt/etc %smnt/etc/master.passwd",
516 	    a->os_root, cmd_name(a, "PWD_MKDB"), a->os_root, a->os_root);
517 
518 	/* Create missing directories. */
519 	command_add(cmds, "%s%s %smnt/proc",
520 	    a->os_root, cmd_name(a, "MKDIR"), a->os_root);
521 	command_add(cmds, "%s%s %smnt/mnt",
522 	    a->os_root, cmd_name(a, "MKDIR"), a->os_root);
523 
524 	/* Write new fstab. */
525 	command_add(cmds, "%s%s '%s' >%smnt/etc/fstab",
526 	    a->os_root, cmd_name(a, "ECHO"),
527 	    "# Device\t\tMountpoint\tFStype\tOptions\t\tDump\tPass#",
528 	    a->os_root);
529 
530 	for (sp = slice_subpartition_first(storage_get_selected_slice(a->s));
531 	     sp != NULL; sp = subpartition_next(sp)) {
532 		if (strcmp(subpartition_get_mountpoint(sp), "swap") == 0) {
533 			command_add(cmds, "%s%s '/dev/%s\t\tnone\t\tswap\tsw\t\t0\t0' >>%smnt/etc/fstab",
534 			    a->os_root, cmd_name(a, "ECHO"),
535 			    subpartition_is_encrypted(sp) ?
536 			    "mapper/swap" : subpartition_get_device_name(sp),
537 			    a->os_root);
538 			if (subpartition_is_encrypted(sp)) {
539 				command_add(cmds,
540 				    "%s%s 'swap\t/dev/%s\tnone\tnone' >>%smnt/etc/crypttab",
541 				    a->os_root, cmd_name(a, "ECHO"),
542 				    subpartition_get_device_name(sp),
543 				    a->os_root);
544 			}
545 		} else if (use_hammer == 0) {
546 			if (strcmp(subpartition_get_mountpoint(sp), "/") == 0) {
547 				command_add(cmds, "%s%s '/dev/%s\t\t%s\t\tufs\trw\t\t1\t1' >>%smnt/etc/fstab",
548 				    a->os_root, cmd_name(a, "ECHO"),
549 				    subpartition_get_device_name(sp),
550 				    subpartition_get_mountpoint(sp),
551 				    a->os_root);
552 			} else if (subpartition_is_tmpfsbacked(sp)) {
553 				command_add(cmds, "%s%s 'tmpfs\t\t\t%s\t\ttmpfs\trw,-s%luM\t1\t1' >>%smnt/etc/fstab",
554 				    a->os_root, cmd_name(a, "ECHO"),
555 				    subpartition_get_mountpoint(sp),
556 				    subpartition_get_capacity(sp),
557 				    a->os_root);
558 			} else if (subpartition_is_encrypted(sp)) {
559 				command_add(cmds, "%s%s '%s\t/dev/%s\tnone\tnone' >>%smnt/etc/crypttab",
560 				    a->os_root, cmd_name(a, "ECHO"),
561 				    subpartition_get_mountpoint(sp) + 1,
562 				    subpartition_get_device_name(sp),
563 				    a->os_root);
564 				command_add(cmds, "%s%s '/dev/mapper/%s\t\t%s\t\tufs\trw\t\t2\t2' >>%smnt/etc/fstab",
565 				    a->os_root, cmd_name(a, "ECHO"),
566 				    subpartition_get_mountpoint(sp) + 1,
567 				    subpartition_get_mountpoint(sp),
568 				    a->os_root);
569 			} else {
570 				command_add(cmds, "%s%s '/dev/%s\t\t%s\t\tufs\trw\t\t2\t2' >>%smnt/etc/fstab",
571 				    a->os_root, cmd_name(a, "ECHO"),
572 				    subpartition_get_device_name(sp),
573 				    subpartition_get_mountpoint(sp),
574 				    a->os_root);
575 			}
576 		} else {
577 			if (strcmp(subpartition_get_mountpoint(sp), "/") == 0) {
578 				command_add(cmds, "%s%s '/dev/%s\t\t%s\t\thammer\trw\t\t1\t1' >>%smnt/etc/fstab",
579 				    a->os_root, cmd_name(a, "ECHO"),
580 				    subpartition_get_device_name(sp),
581 				    subpartition_get_mountpoint(sp),
582 				    a->os_root);
583 				if (subpartition_is_encrypted(sp)) {
584 					command_add(cmds,
585 					    "%s%s 'vfs.root.mountfrom=\"ufs:md0s0\"' >>%smnt/boot/loader.conf",
586 					    a->os_root, cmd_name(a, "ECHO"),
587 					    a->os_root);
588 					command_add(cmds,
589 					    "%s%s 'vfs.root.realroot=\"crypt:hammer:%s:root\"' >>%smnt/boot/loader.conf",
590 					    a->os_root, cmd_name(a, "ECHO"),
591 					    subpartition_get_device_name(sp),
592 					    a->os_root);
593 				} else {
594 					command_add(cmds,
595 					    "%s%s 'vfs.root.mountfrom=\"hammer:%s\"' >>%smnt/boot/loader.conf",
596 					    a->os_root, cmd_name(a, "ECHO"),
597 					    subpartition_get_device_name(sp),
598 					    a->os_root);
599 				}
600 			} else if (strcmp(subpartition_get_mountpoint(sp), "/boot") == 0) {
601 				command_add(cmds, "%s%s '/dev/%s\t\t%s\t\tufs\trw\t\t1\t1' >>%smnt/etc/fstab",
602 				    a->os_root, cmd_name(a, "ECHO"),
603 				    subpartition_get_device_name(sp),
604 				    subpartition_get_mountpoint(sp),
605 				    a->os_root);
606 			}
607 		}
608 	}
609 
610 	/*
611 	 * Take care of HAMMER PFS null mounts.
612 	 */
613 	if (use_hammer == 1) {
614 		for (j = 0; pfs_mountpt[j] != NULL; j++) {
615 			if (rindex(pfs_mountpt[j]+1, '/') != NULL)
616 				command_add(cmds, "%s%s '/pfs%s.%s\t%s\t\tnull\trw\t\t0\t0' >>%smnt/etc/fstab",
617 				    a->os_root, cmd_name(a, "ECHO"),
618 				    dirname(pfs_mountpt[j]),
619 				    basename(pfs_mountpt[j]),
620 				    pfs_mountpt[j],
621 				    a->os_root);
622 			else
623 				command_add(cmds, "%s%s '/pfs%s\t\t%s\t\tnull\trw\t\t0\t0' >>%smnt/etc/fstab",
624 				    a->os_root, cmd_name(a, "ECHO"),
625 				    pfs_mountpt[j],
626 				    pfs_mountpt[j],
627 				    a->os_root);
628 		}
629 	}
630 
631 	command_add(cmds, "%s%s '%s' >>%smnt/etc/fstab",
632 	    a->os_root, cmd_name(a, "ECHO"),
633 	    "proc\t\t\t/proc\t\tprocfs\trw\t\t0\t0",
634 	    a->os_root);
635 
636 	/* Backup the disklabel and the log. */
637 	command_add(cmds, "%s%s %s > %smnt/etc/disklabel.%s",
638 	    a->os_root, cmd_name(a, "DISKLABEL64"),
639 	    slice_get_device_name(storage_get_selected_slice(a->s)),
640 	    a->os_root,
641 	    slice_get_device_name(storage_get_selected_slice(a->s)));
642 
643 	/* 'chflags nohistory' as needed */
644 	for (j = 0; pfs_mountpt[j] != NULL; j++)
645 		if (pfs_nohistory[j] == 1)
646 			command_add(cmds, "%s%s -R nohistory %smnt%s",
647 			    a->os_root, cmd_name(a, "CHFLAGS"),
648 			    a->os_root, pfs_mountpt[j]);
649 
650 	command_add(cmds, "%s%s %sinstall.log %smnt/var/log/install.log",
651 	    a->os_root, cmd_name(a, "CP"),
652 	    a->tmp, a->os_root);
653 	command_add(cmds, "%s%s 600 %smnt/var/log/install.log",
654 	    a->os_root, cmd_name(a, "CHMOD"), a->os_root);
655 
656 	/* Do some preparation if encrypted partitions were configured */
657 	if (needcrypt) {
658 		command_add(cmds,
659 		    "%s%s 'dm_load=\"yes\"' >>%smnt/boot/loader.conf",
660 		    a->os_root, cmd_name(a, "ECHO"),
661 		    a->os_root);
662 		command_add(cmds,
663 		    "%s%s 'dm_target_crypt_load=\"yes\"' >>%smnt/boot/loader.conf",
664 		    a->os_root, cmd_name(a, "ECHO"),
665 		    a->os_root);
666 		if (use_hammer) {
667 			command_add(cmds, "%s%s -b %smnt/boot -t %smnt/tmp",
668 			    a->os_root, cmd_name(a, "MKINITRD"),
669 			    a->os_root, a->os_root);
670 			command_add(cmds, "%s%s -rf %smnt/tmp/initrd",
671 			    a->os_root, cmd_name(a, "RM"), a->os_root);
672 			command_add(cmds,
673 			    "%s%s 'initrd.img_load=\"YES\"' >>%smnt/boot/loader.conf",
674 			    a->os_root, cmd_name(a, "ECHO"),
675 			    a->os_root);
676 			command_add(cmds,
677 			    "%s%s 'initrd.img_type=\"md_image\"' >>%smnt/boot/loader.conf",
678 			    a->os_root, cmd_name(a, "ECHO"),
679 			    a->os_root);
680 		}
681 	}
682 
683 	/* Customize stuff here */
684 	if(is_file("%susr/local/bin/after_installation_routines.sh", a->os_root)) {
685 		command_add(cmds, "%susr/local/bin/after_installation_routines.sh",
686 		    a->os_root);
687 	}
688 
689 	/*
690 	 * Do it!
691 	 */
692 	/* commands_preview(a->c, cmds); */
693 	if (!commands_execute(a, cmds)) {
694 		inform(a->c, _("%s was not fully installed."), OPERATING_SYSTEM_NAME);
695 		a->result = 0;
696 	} else {
697 		a->result = 1;
698 	}
699 	commands_free(cmds);
700 	cmds = commands_new();
701 
702 	if (a->result) {
703 		config_vars_write(rc_conf, CONFIG_TYPE_SH, "%smnt/etc/rc.conf",
704 		    a->os_root);
705 		config_vars_free(rc_conf);
706 		rc_conf = config_vars_new();
707 	}
708 
709 	/*
710 	 * Unmount everything we mounted on /mnt.  This is done in a seperate
711 	 * command chain, so that partitions are unmounted, even if an error
712 	 * occurs in one of the preceding commands, or it is cancelled.
713 	 */
714 	unmount_all_under(a, cmds, "%smnt", a->os_root);
715 
716 	/*
717 	 * Once everything is unmounted, if the install went successfully,
718 	 * make sure once and for all that the disklabel is bootable.
719 	 */
720 	if (a->result)
721 		command_add(cmds, "%s%s -B %s",
722 		    a->os_root, cmd_name(a, "DISKLABEL64"),
723 		    slice_get_device_name(storage_get_selected_slice(a->s)));
724 
725 	if (!commands_execute(a, cmds))
726 		inform(a->c, _("Warning: subpartitions were not correctly unmounted."));
727 
728 	commands_free(cmds);
729 
730 	/*
731 	 * Finally, remove all swap and any mappings.
732 	 */
733 	if (swapoff_all(a) == NULL)
734 		inform(a->c, _("Warning: swap could not be turned off."));
735 	if (remove_all_mappings(a) == NULL)
736 		inform(a->c, _("Warning: mappings could not be removed."));
737 }
738