installer: Install the kernel used to install the system to /boot/kernel.
[dragonfly.git] / usr.sbin / installer / dfuibe_installer / fn_install.c
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;
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 %sdev/%s",
164                             a->os_root,
165                             cmd_name(a, "SWAPON"),
166                             a->os_root,
167                             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         for (sp = slice_subpartition_first(storage_get_selected_slice(a->s));
177              sp != NULL; sp = subpartition_next(sp)) {
178                 if (strcmp(subpartition_get_mountpoint(sp), "/") == 0) {
179                         if (use_hammer == 1) {
180                                 command_add(cmds, "%s%s %sdev/%s %smnt%s",
181                                     a->os_root, cmd_name(a, "MOUNT_HAMMER"),
182                                     a->os_root,
183                                     subpartition_get_device_name(sp),
184                                     a->os_root,
185                                     subpartition_get_mountpoint(sp));
186                         } else {
187                                 command_add(cmds, "%s%s %sdev/%s %smnt%s",
188                                     a->os_root, cmd_name(a, "MOUNT"),
189                                     a->os_root,
190                                     subpartition_get_device_name(sp),
191                                     a->os_root,
192                                     subpartition_get_mountpoint(sp));
193                         }
194                 }
195         }
196
197         /*
198          * Create mount points and mount subpartitions on them.
199          */
200         for (sp = slice_subpartition_first(storage_get_selected_slice(a->s));
201              sp != NULL; sp = subpartition_next(sp)) {
202                 if (subpartition_is_swap(sp)) {
203                         /*
204                          * Set this subpartition as the dump device.
205                          */
206                         if (subpartition_get_capacity(sp) < storage_get_memsize(a->s))
207                                 continue;
208
209                         command_add(cmds, "%s%s -v %sdev/%s",
210                             a->os_root, cmd_name(a, "DUMPON"),
211                             a->os_root,
212                             subpartition_get_device_name(sp));
213
214                         asprintf(&string, "/dev/%s",
215                             subpartition_get_device_name(sp));
216                         config_var_set(rc_conf, "dumpdev", string);
217                         free(string);
218                         continue;
219                 }
220
221                 if (use_hammer == 0) {
222                         /* / is already mounted */
223                         if (strcmp(subpartition_get_mountpoint(sp), "/") != 0) {
224                                 command_add(cmds, "%s%s -p %smnt%s",
225                                     a->os_root, cmd_name(a, "MKDIR"),
226                                     a->os_root,
227                                     subpartition_get_mountpoint(sp));
228                                 /* Don't mount it if it's TMPFS-backed. */
229                                 if (subpartition_is_tmpfsbacked(sp))
230                                         continue;
231                                 command_add(cmds, "%s%s %sdev/%s %smnt%s",
232                                     a->os_root, cmd_name(a, "MOUNT"),
233                                     a->os_root,
234                                     subpartition_get_device_name(sp),
235                                     a->os_root,
236                                     subpartition_get_mountpoint(sp));
237                         }
238                 } else if (strcmp(subpartition_get_mountpoint(sp), "/boot") == 0) {
239                         command_add(cmds, "%s%s -p %smnt%s",
240                             a->os_root, cmd_name(a, "MKDIR"),
241                             a->os_root,
242                             subpartition_get_mountpoint(sp));
243                         command_add(cmds, "%s%s %sdev/%s %smnt%s",
244                             a->os_root, cmd_name(a, "MOUNT"),
245                             a->os_root,
246                             subpartition_get_device_name(sp),
247                             a->os_root,
248                             subpartition_get_mountpoint(sp));
249                 }
250         }
251
252         /*
253          * Take care of HAMMER PFS.
254          */
255         if (use_hammer == 1)
256                 handle_pfs(a, cmds);
257
258         /*
259          * Actually copy files now.
260          */
261
262         for (i = 0; cp_src[i] != NULL && cp_src[i][0] != '\0'; i++) {
263                 char *src, *dest, *dn, *tmp_dest;
264
265                 dest = cp_src[i];
266
267                 /*
268                  * If dest would be on an TMPFS-backed
269                  * mountpoint, don't bother copying it.
270                  */
271                 sp = subpartition_of(storage_get_selected_slice(a->s),
272                                      "%s%s", a->os_root, &dest[1]);
273                 if (sp != NULL && subpartition_is_tmpfsbacked(sp)) {
274                         continue;
275                 }
276
277                 /*
278                  * Create intermediate directories, if needed.
279                  */
280                 tmp_dest = aura_strdup(dest);
281                 dn = dirname(tmp_dest);
282                 if (is_dir("%s%s", a->os_root, &dn[1]) &&
283                     !is_dir("%smnt%s", a->os_root, dn)) {
284                         command_add(cmds, "%s%s -p %smnt%s",
285                             a->os_root, cmd_name(a, "MKDIR"),
286                             a->os_root, dn);
287                 }
288                 aura_free(tmp_dest, "directory name");
289
290                 /*
291                  * If a directory by the same name but with the suffix
292                  * ".hdd" exists on the installation media, cpdup that
293                  * instead.  This is particularly useful with /etc, which
294                  * may have significantly different behaviour on the
295                  * live CD compared to a standard HDD boot.
296                  */
297                 if (is_dir("%s%s.hdd", a->os_root, &dest[1]))
298                         asprintf(&src, "%s.hdd", &dest[1]);
299                 else
300                         asprintf(&src, "%s", &dest[1]);
301
302                 if (is_dir("%s%s", a->os_root, src) || is_file("%s%s", a->os_root, src)) {
303                         /*
304                          * Cpdup the chosen file or directory onto the HDD.
305                          * if it exists on the source.
306                          */
307                         cmd = command_add(cmds, "%s%s %s%s %smnt%s",
308                             a->os_root, cmd_name(a, "CPDUP"),
309                             a->os_root, src,
310                             a->os_root, dest);
311                         command_set_log_mode(cmd, COMMAND_LOG_QUIET);
312                 }
313         }
314
315         /*
316          * Now, because cpdup does not cross mount points,
317          * we must copy anything that the user might've made a
318          * seperate mount point for (e.g. /usr/libdata/lint.)
319          */
320         for (sp = slice_subpartition_first(storage_get_selected_slice(a->s));
321              sp != NULL; sp = subpartition_next(sp)) {
322                 /*
323                  * If the subpartition is a swap subpartition or an
324                  * TMPFS-backed mountpoint, don't try to copy anything
325                  * into it.
326                  */
327                 if (subpartition_is_swap(sp) || subpartition_is_tmpfsbacked(sp))
328                         continue;
329
330                 /*
331                  * If the mountpoint doesn't even exist on the installation
332                  * medium, don't try to copy anything from it!  We assume
333                  * it's an empty subpartition for the user's needs.
334                  */
335                 if (!is_dir("%s%s", a->os_root, &subpartition_get_mountpoint(sp)[1]))
336                         continue;
337
338                 /*
339                  * Don't bother copying the mountpoint IF:
340                  * - we've already said to copy it, or something besides it
341                  *   (it's a prefix of something in cp_src); or
342                  * - we haven't said to copy it
343                  *   (nothing in cp_src is a prefix of it.)
344                  */
345                 seen_it = 0;
346                 prefix = 0;
347                 for (i = 0; cp_src[i] != NULL && cp_src[i][0] != '\0'; i++) {
348                         if (strncmp(subpartition_get_mountpoint(sp), cp_src[i],
349                             strlen(subpartition_get_mountpoint(sp))) == 0) {
350                                 seen_it = 1;
351                                 break;
352                         }
353                         if (strncmp(cp_src[i], subpartition_get_mountpoint(sp),
354                             strlen(cp_src[i])) == 0) {
355                                 prefix = 1;
356                         }
357                 }
358                 if (seen_it || !prefix)
359                         continue;
360
361                 /*
362                  * Otherwise, cpdup the subpartition.
363                  *
364                  * XXX check for .hdd-extended source dirs here, too,
365                  * eventually - but for now, /etc.hdd will never be
366                  * the kind of tricky sub-mount-within-a-mount-point
367                  * that this part of the code is meant to handle.
368                  */
369                 cmd = command_add(cmds, "%s%s %s%s %smnt%s",
370                     a->os_root, cmd_name(a, "CPDUP"),
371                     a->os_root, &subpartition_get_mountpoint(sp)[1],
372                     a->os_root, subpartition_get_mountpoint(sp));
373                 command_set_log_mode(cmd, COMMAND_LOG_QUIET);
374         }
375
376         /*
377          * Create symlinks.
378          */
379
380         /* Take care of /sys. */
381         command_add(cmds, "%s%s -s usr/src/sys %smnt/sys",
382             a->os_root, cmd_name(a, "LN"), a->os_root);
383
384         /*
385          * If the user has both /var and /tmp subpartitions,
386          * symlink /var/tmp to /tmp.
387          */
388         if (subpartition_find(storage_get_selected_slice(a->s), "/tmp") != NULL &&
389             subpartition_find(storage_get_selected_slice(a->s), "/var") != NULL) {
390                 command_add(cmds, "%s%s 1777 %smnt/tmp",
391                     a->os_root, cmd_name(a, "CHMOD"), a->os_root);
392                 command_add(cmds, "%s%s -rf %smnt/var/tmp",
393                     a->os_root, cmd_name(a, "RM"), a->os_root);
394                 command_add(cmds, "%s%s -s /tmp %smnt/var/tmp",
395                     a->os_root, cmd_name(a, "LN"), a->os_root);
396         }
397
398         /*
399          * If the user has /var, but no /tmp,
400          * symlink /tmp to /var/tmp.
401          */
402         if (subpartition_find(storage_get_selected_slice(a->s), "/tmp") == NULL &&
403             subpartition_find(storage_get_selected_slice(a->s), "/var") != NULL) {
404                 command_add(cmds, "%s%s -rf %smnt/tmp",
405                     a->os_root, cmd_name(a, "RM"), a->os_root);
406                 command_add(cmds, "%s%s -s /var/tmp %smnt/tmp",
407                     a->os_root, cmd_name(a, "LN"), a->os_root);
408         }
409
410         /*
411          * If the user has /usr, but no /home,
412          * symlink /home to /usr/home.
413          */
414         if (subpartition_find(storage_get_selected_slice(a->s), "/home") == NULL &&
415             subpartition_find(storage_get_selected_slice(a->s), "/usr") != NULL) {
416                 command_add(cmds, "%s%s -rf %smnt/home",
417                     a->os_root, cmd_name(a, "RM"), a->os_root);
418                 command_add(cmds, "%s%s %smnt/usr/home",
419                     a->os_root, cmd_name(a, "MKDIR"), a->os_root);
420                 command_add(cmds, "%s%s -s /usr/home %smnt/home",
421                     a->os_root, cmd_name(a, "LN"), a->os_root);
422         }
423
424         /*
425          * XXX check for other possible combinations too?
426          */
427
428         /*
429          * Clean up.  In case some file didn't make it, use rm -f
430          */
431         command_add(cmds, "%s%s -f %smnt/boot/loader.conf",
432             a->os_root, cmd_name(a, "RM"), a->os_root);
433         command_add(cmds, "%s%s -f %smnt/tmp/install.log",
434             a->os_root, cmd_name(a, "RM"), a->os_root);
435
436         /*
437          * Copy pristine versions over any files we might have installed.
438          * This allows the resulting file tree to be customized.
439          */
440         for (i = 0; cp_src[i] != NULL && cp_src[i][0] != '\0'; i++) {
441                 char *src, *dest, *dn, *tmp_dest;
442
443                 src = cp_src[i];
444                 dest = cp_src[i];
445
446                 /*
447                  * Get the directory that the desired thing to
448                  * copy resides in.
449                  */
450                 tmp_dest = aura_strdup(dest);
451                 dn = dirname(tmp_dest);
452
453                 /*
454                  * If this dir doesn't exist in PRISTINE_DIR
455                  * on the install media, just skip it.
456                  */
457                 if (!is_dir("%s%s%s", a->os_root, PRISTINE_DIR, dn)) {
458                         aura_free(tmp_dest, _("directory name"));
459                         continue;
460                 }
461
462                 /*
463                  * Create intermediate directories, if needed.
464                  */
465                 if (!is_dir("%smnt%s", a->os_root, dn)) {
466                         command_add(cmds, "%s%s -p %smnt%s",
467                             a->os_root, cmd_name(a, "MKDIR"),
468                             a->os_root, dn);
469                 }
470                 aura_free(tmp_dest, "directory name");
471
472                 /*
473                  * Cpdup the chosen file or directory onto the HDD.
474                  */
475                 cmd = command_add(cmds, "%s%s %s%s %smnt%s",
476                     a->os_root, cmd_name(a, "CPDUP"),
477                     a->os_root, src,
478                     a->os_root, dest);
479
480                 cmd = command_add(cmds,
481                     "%s%s %s%s%s %smnt%s",
482                     a->os_root, cmd_name(a, "CPDUP"),
483                     a->os_root, PRISTINE_DIR, src,
484                     a->os_root, dest);
485                 command_set_log_mode(cmd, COMMAND_LOG_QUIET);
486         }
487
488         /*
489          * Rebuild the user database, to get rid of any extra users
490          * from the LiveCD that aren't supposed to be installed
491          * (copying a pristine master.passwd isn't enough.)
492          */
493         command_add(cmds, "%s%s -p -d %smnt/etc %smnt/etc/master.passwd",
494             a->os_root, cmd_name(a, "PWD_MKDB"), a->os_root, a->os_root);
495
496         /* Create missing directories. */
497         command_add(cmds, "%s%s %smnt/proc",
498             a->os_root, cmd_name(a, "MKDIR"), a->os_root);
499         command_add(cmds, "%s%s %smnt/mnt",
500             a->os_root, cmd_name(a, "MKDIR"), a->os_root);
501
502         /* Write new fstab. */
503         command_add(cmds, "%s%s '%s' >%smnt/etc/fstab",
504             a->os_root, cmd_name(a, "ECHO"),
505             "# Device\t\tMountpoint\tFStype\tOptions\t\tDump\tPass#",
506             a->os_root);
507
508         for (sp = slice_subpartition_first(storage_get_selected_slice(a->s));
509              sp != NULL; sp = subpartition_next(sp)) {
510                 if (strcmp(subpartition_get_mountpoint(sp), "swap") == 0) {
511                         command_add(cmds, "%s%s '/dev/%s\t\tnone\t\tswap\tsw\t\t0\t0' >>%smnt/etc/fstab",
512                             a->os_root, cmd_name(a, "ECHO"),
513                             subpartition_get_device_name(sp),
514                             a->os_root);
515                 } else if (use_hammer == 0) {
516                         if (strcmp(subpartition_get_mountpoint(sp), "/") == 0) {
517                                 command_add(cmds, "%s%s '/dev/%s\t\t%s\t\tufs\trw\t\t1\t1' >>%smnt/etc/fstab",
518                                     a->os_root, cmd_name(a, "ECHO"),
519                                     subpartition_get_device_name(sp),
520                                     subpartition_get_mountpoint(sp),
521                                     a->os_root);
522                                 command_add(cmds, "%s%s 'kern.emergency_intr_enable=1' >>%smnt/boot/loader.conf",
523                                     a->os_root, cmd_name(a, "ECHO"), a->os_root);
524                         } else if (subpartition_is_tmpfsbacked(sp)) {
525                                 command_add(cmds, "%s%s 'tmpfs\t\t\t%s\t\ttmpfs\trw,-s%luM\t1\t1' >>%smnt/etc/fstab",
526                                         a->os_root, cmd_name(a, "ECHO"),
527                                         subpartition_get_mountpoint(sp),
528                                         subpartition_get_capacity(sp),
529                                         a->os_root);
530                         } else {
531                                 command_add(cmds, "%s%s '/dev/%s\t\t%s\t\tufs\trw\t\t2\t2' >>%smnt/etc/fstab",
532                                     a->os_root, cmd_name(a, "ECHO"),
533                                     subpartition_get_device_name(sp),
534                                     subpartition_get_mountpoint(sp),
535                                     a->os_root);
536                         }
537                 } else {
538                         if (strcmp(subpartition_get_mountpoint(sp), "/") == 0) {
539                                 command_add(cmds, "%s%s '/dev/%s\t\t%s\t\thammer\trw\t\t1\t1' >>%smnt/etc/fstab",
540                                     a->os_root, cmd_name(a, "ECHO"),
541                                     subpartition_get_device_name(sp),
542                                     subpartition_get_mountpoint(sp),
543                                     a->os_root);
544                                 command_add(cmds, "%s%s 'vfs.root.mountfrom=\"hammer:%s\"' >>%smnt/boot/loader.conf",
545                                     a->os_root, cmd_name(a, "ECHO"),
546                                     subpartition_get_device_name(sp),
547                                     a->os_root);
548                                 command_add(cmds, "%s%s 'kern.emergency_intr_enable=1' >>%smnt/boot/loader.conf",
549                                     a->os_root, cmd_name(a, "ECHO"), a->os_root);
550                         } else if (strcmp(subpartition_get_mountpoint(sp), "/boot") == 0) {
551                                 command_add(cmds, "%s%s '/dev/%s\t\t%s\t\tufs\trw\t\t1\t1' >>%smnt/etc/fstab",
552                                     a->os_root, cmd_name(a, "ECHO"),
553                                     subpartition_get_device_name(sp),
554                                     subpartition_get_mountpoint(sp),
555                                     a->os_root);
556                         }
557                 }
558         }
559
560         /*
561          * Take care of HAMMER PFS null mounts.
562          */
563         if (use_hammer == 1) {
564                 for (j = 0; pfs_mountpt[j] != NULL; j++) {
565                         if (rindex(pfs_mountpt[j]+1, '/') != NULL)
566                                 command_add(cmds, "%s%s '/pfs%s.%s\t%s\t\tnull\trw\t\t0\t0' >>%smnt/etc/fstab",
567                                     a->os_root, cmd_name(a, "ECHO"),
568                                     dirname(pfs_mountpt[j]),
569                                     basename(pfs_mountpt[j]),
570                                     pfs_mountpt[j],
571                                     a->os_root);
572                         else
573                                 command_add(cmds, "%s%s '/pfs%s\t\t%s\t\tnull\trw\t\t0\t0' >>%smnt/etc/fstab",
574                                     a->os_root, cmd_name(a, "ECHO"),
575                                     pfs_mountpt[j],
576                                     pfs_mountpt[j],
577                                     a->os_root);
578                 }
579         }
580
581         command_add(cmds, "%s%s '%s' >>%smnt/etc/fstab",
582             a->os_root, cmd_name(a, "ECHO"),
583             "proc\t\t\t/proc\t\tprocfs\trw\t\t0\t0",
584             a->os_root);
585
586         /* Backup the disklabel and the log. */
587         command_add(cmds, "%s%s %s > %smnt/etc/disklabel.%s",
588             a->os_root, cmd_name(a, "DISKLABEL64"),
589             slice_get_device_name(storage_get_selected_slice(a->s)),
590             a->os_root,
591             slice_get_device_name(storage_get_selected_slice(a->s)));
592
593         /* 'chflags nohistory' as needed */
594         for (j = 0; pfs_mountpt[j] != NULL; j++)
595                 if (pfs_nohistory[j] == 1)
596                         command_add(cmds, "%s%s -R nohistory %smnt%s",
597                             a->os_root, cmd_name(a, "CHFLAGS"),
598                             a->os_root, pfs_mountpt[j]);
599
600         command_add(cmds, "%s%s %sinstall.log %smnt/var/log/install.log",
601             a->os_root, cmd_name(a, "CP"),
602             a->tmp, a->os_root);
603         command_add(cmds, "%s%s 600 %smnt/var/log/install.log",
604             a->os_root, cmd_name(a, "CHMOD"), a->os_root);
605
606         /*
607          * XXX
608          * Move the kernel used for booting (UP or SMP) to /boot/kernel
609          * and remove the other kernel
610          */
611         command_add(cmds,
612             "%s%s %smnt`%s%s $(%s%s -n kern.bootfile)` %smnt/boot/kernel",
613             a->os_root, cmd_name(a, "MV"),
614             a->os_root,
615             a->os_root, cmd_name(a, "DIRNAME"),
616             a->os_root, cmd_name(a, "SYSCTL"),
617             a->os_root);
618         command_add(cmds,
619             "%s%s -R noschg %smnt/boot/[A-Z][A-Z]*; %s%s -rf %smnt/boot/[A-Z][A-Z]*",
620             a->os_root, cmd_name(a, "CHFLAGS"),
621             a->os_root,
622             a->os_root, cmd_name(a, "RM"),
623             a->os_root);
624
625         /* Customize stuff here */
626         if(is_file("%susr/local/bin/after_installation_routines.sh", a->os_root)) {
627                 command_add(cmds, "%susr/local/bin/after_installation_routines.sh",
628                     a->os_root);
629         }
630
631         /*
632          * Do it!
633          */
634         /* commands_preview(a->c, cmds); */
635         if (!commands_execute(a, cmds)) {
636                 inform(a->c, _("%s was not fully installed."), OPERATING_SYSTEM_NAME);
637                 a->result = 0;
638         } else {
639                 a->result = 1;
640         }
641         commands_free(cmds);
642         cmds = commands_new();
643
644         if (a->result) {
645                 config_vars_write(rc_conf, CONFIG_TYPE_SH, "%smnt/etc/rc.conf",
646                     a->os_root);
647                 config_vars_free(rc_conf);
648                 rc_conf = config_vars_new();
649         }
650
651         /*
652          * Unmount everything we mounted on /mnt.  This is done in a seperate
653          * command chain, so that partitions are unmounted, even if an error
654          * occurs in one of the preceding commands, or it is cancelled.
655          */
656         unmount_all_under(a, cmds, "%smnt", a->os_root);
657
658         /*
659          * Once everything is unmounted, if the install went successfully,
660          * make sure once and for all that the disklabel is bootable.
661          */
662         if (a->result)
663                 command_add(cmds, "%s%s -B %s",
664                     a->os_root, cmd_name(a, "DISKLABEL64"),
665                     slice_get_device_name(storage_get_selected_slice(a->s)));
666
667         if (!commands_execute(a, cmds))
668                 inform(a->c, _("Warning: subpartitions were not correctly unmounted."));
669
670         commands_free(cmds);
671
672         /*
673          * Finally, remove all swap.
674          */
675         if (swapoff_all(a) == NULL)
676                 inform(a->c, _("Warning: swap could not be turned off."));
677 }