d6949eae3b35f3c529e386b1dbd947aa0dd3ece8
[dragonfly.git] / contrib / bsdinstaller-1.1.6 / src / backend / 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/local/share/dfuibe_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  * fn_install_os: actually put DragonFly on a disk.
71  */
72 void
73 fn_install_os(struct i_fn_args *a)
74 {
75         struct subpartition *sp;
76         struct commands *cmds;
77         struct command *cmd;
78         int i, seen_it, prefix;
79         FILE *sources_conf;
80         char line[256];
81         char cp_src[64][256];
82         char file_path[256];
83         int lines = 0;
84         long mfsbacked_size;
85
86         /* 
87          * If SOURCES_CONF_FILE exists, lets read in and 
88          * populate our copy sources.   If it does not exist
89          * simply set the list by hand.
90          */
91         if (!is_file("%s%s", a->os_root, SOURCES_CONF_FILE)) {
92                 i_log(a, "Using internal copy sources table.");
93                 strcpy(cp_src[0],"/COPYRIGHT");
94                 strcpy(cp_src[1],"/var");
95                 strcpy(cp_src[2],"/bin");
96                 strcpy(cp_src[3],"/boot");
97                 strcpy(cp_src[4],"/cdrom");
98                 strcpy(cp_src[5],"/dev");
99                 strcpy(cp_src[6],"/etc");
100                 strcpy(cp_src[7],"/libexec");
101                 strcpy(cp_src[8],"/lib");
102                 strcpy(cp_src[9],"/kernel");
103                 strcpy(cp_src[10],"/modules");
104                 strcpy(cp_src[11],"/root");
105                 strcpy(cp_src[12],"/sbin");
106                 strcpy(cp_src[13],"/sys");
107                 strcpy(cp_src[14],"/tmp");
108                 strcpy(cp_src[15],"/usr/bin");
109                 strcpy(cp_src[16],"/usr/games");
110                 strcpy(cp_src[17],"/usr/include");
111                 strcpy(cp_src[18],"/usr/lib");
112                 strcpy(cp_src[19],"/usr/local");
113                 strcpy(cp_src[20],"/usr/local/OpenOffice.org1.1.3");
114                 strcpy(cp_src[21],"/usr/X11R6");
115                 strcpy(cp_src[22],"/usr/libdata");
116                 strcpy(cp_src[23],"/usr/libexec");
117                 strcpy(cp_src[24],"/usr/obj");
118                 strcpy(cp_src[25],"/usr/sbin");
119                 strcpy(cp_src[26],"/usr/share");
120                 strcpy(cp_src[27],"/usr/src");
121                 strcpy(cp_src[28],"");
122         } else {
123                 snprintf(file_path, 256, "%s%s", a->os_root, SOURCES_CONF_FILE);
124                 sources_conf = fopen(file_path, "r");
125                 i_log(a, "Reading %s%s", a->os_root, SOURCES_CONF_FILE);
126                 while(fgets(line, 256, sources_conf) != NULL && lines < 63) {
127                         if(strlen(line)>0)
128                                 line[strlen(line)-1] = '\0';
129                         strlcpy(cp_src[lines], line, 256);
130                         i_log(a,"Adding %s to copy source table.", cp_src[lines]);
131                         lines++;
132                 }
133                 i_log(a,"Added %i total items to copy source table.", lines);
134                 strcpy(cp_src[lines], "");
135                 fclose(sources_conf);
136         }
137
138         cmds = commands_new();
139
140         /*
141          * If swap isn't mounted yet, mount it.
142          */
143         if (measure_activated_swap(a) == 0) {
144                 for (sp = slice_subpartition_first(storage_get_selected_slice(a->s));
145                     sp != NULL; sp = subpartition_next(sp)) {
146                         if (!subpartition_is_swap(sp))
147                                 continue;
148                         command_add(cmds, "%s%s %sdev/%s",
149                             a->os_root,
150                             cmd_name(a, "SWAPON"),
151                             a->os_root,
152                             subpartition_get_device_name(sp));
153                 }
154         }
155
156         /*
157          * Unmount anything already mounted on /mnt.
158          */
159         unmount_all_under(a, cmds, "%smnt", a->os_root);
160
161         /*
162          * Create mount points and mount subpartitions on them.
163          */
164         for (sp = slice_subpartition_first(storage_get_selected_slice(a->s));
165              sp != NULL; sp = subpartition_next(sp)) {
166                 if (subpartition_is_swap(sp)) {
167                         /*
168                          * Set this subpartition as the dump device.
169                          */
170 #ifdef AUTOMATICALLY_ENABLE_CRASH_DUMPS
171                         if (subpartition_get_capacity(sp) < storage_get_memsize(a->s))
172                                 continue;
173
174                         command_add(cmds, "%s%s -v %sdev/%s",
175                             a->os_root, cmd_name(a, "DUMPON"),
176                             a->os_root,
177                             subpartition_get_device_name(sp));
178
179                         asprintf(&string, "/dev/%s",
180                             subpartition_get_device_name(sp));
181                         config_var_set(rc_conf, "dumpdev", string);
182                         free(string);
183                         config_var_set(rc_conf, "dumpdir", "/var/crash");
184 #endif
185                         continue;
186                 }
187         
188                 if (strcmp(subpartition_get_mountpoint(sp), "/") != 0) {
189                         command_add(cmds, "%s%s -p %smnt%s",
190                             a->os_root, cmd_name(a, "MKDIR"),
191                             a->os_root, subpartition_get_mountpoint(sp));
192                 }
193
194                 /*
195                  * Don't mount it if it's MFS-backed.
196                  */
197
198                 if (subpartition_is_mfsbacked(sp)) {
199                         continue;
200                 }
201
202                 command_add(cmds, "%s%s %sdev/%s %smnt%s",
203                     a->os_root, cmd_name(a, "MOUNT"),
204                     a->os_root,
205                     subpartition_get_device_name(sp),
206                     a->os_root,
207                     subpartition_get_mountpoint(sp));
208         }
209
210         /*
211          * Actually copy files now.
212          */
213
214         for (i = 0; cp_src[i] != NULL && cp_src[i][0] != '\0'; i++) {
215                 char *src, *dest, *dn, *tmp_dest;
216
217                 dest = cp_src[i];
218
219                 /*
220                  * If dest would be on an MFS-backed
221                  * mountpoint, don't bother copying it.
222                  */
223                 sp = subpartition_of(storage_get_selected_slice(a->s),
224                                      "%s%s", a->os_root, &dest[1]);
225                 if (sp != NULL && subpartition_is_mfsbacked(sp)) {
226                         continue;
227                 }
228
229                 /*
230                  * Create intermediate directories, if needed.
231                  */
232                 tmp_dest = aura_strdup(dest);
233                 dn = dirname(tmp_dest);
234                 if (is_dir("%s%s", a->os_root, &dn[1]) &&
235                     !is_dir("%smnt%s", a->os_root, dn)) {
236                         command_add(cmds, "%s%s -p %smnt%s",
237                             a->os_root, cmd_name(a, "MKDIR"),
238                             a->os_root, dn);
239                 }
240                 aura_free(tmp_dest, "directory name");
241
242                 /*
243                  * If a directory by the same name but with the suffix
244                  * ".hdd" exists on the installation media, cpdup that
245                  * instead.  This is particularly useful with /etc, which
246                  * may have significantly different behaviour on the
247                  * live CD compared to a standard HDD boot.
248                  */
249                 if (is_dir("%s%s.hdd", a->os_root, &dest[1]))
250                         asprintf(&src, "%s.hdd", &dest[1]);
251                 else
252                         asprintf(&src, "%s", &dest[1]);
253
254                 if (is_dir("%s%s", a->os_root, src) || is_file("%s%s", a->os_root, src)) {
255                         /*
256                          * Cpdup the chosen file or directory onto the HDD.
257                          * if it exists on the source.
258                          */
259                         cmd = command_add(cmds, "%s%s %s%s %smnt%s",
260                             a->os_root, cmd_name(a, "CPDUP"),
261                             a->os_root, src,
262                             a->os_root, dest);
263                         command_set_log_mode(cmd, COMMAND_LOG_QUIET);
264                 }
265         }
266
267         /*
268          * Now, because cpdup does not cross mount points,
269          * we must copy anything that the user might've made a
270          * seperate mount point for (e.g. /usr/libdata/lint.)
271          */
272         for (sp = slice_subpartition_first(storage_get_selected_slice(a->s));
273              sp != NULL; sp = subpartition_next(sp)) {
274                 /*
275                  * If the subpartition is a swap subpartition or an
276                  * MFS-backed mountpoint, don't try to copy anything
277                  * into it.
278                  */
279                 if (subpartition_is_swap(sp) || subpartition_is_mfsbacked(sp))
280                         continue;
281
282                 /*
283                  * If the mountpoint doesn't even exist on the installation
284                  * medium, don't try to copy anything from it!  We assume
285                  * it's an empty subpartition for the user's needs.
286                  */
287                 if (!is_dir("%s%s", a->os_root, &subpartition_get_mountpoint(sp)[1]))
288                         continue;
289
290                 /*
291                  * Don't bother copying the mountpoint IF:
292                  * - we've already said to copy it, or something besides it
293                  *   (it's a prefix of something in cp_src); or
294                  * - we haven't said to copy it
295                  *   (nothing in cp_src is a prefix of it.)
296                  */
297                 seen_it = 0;
298                 prefix = 0;
299                 for (i = 0; cp_src[i] != NULL && cp_src[i][0] != '\0'; i++) {
300                         if (strncmp(subpartition_get_mountpoint(sp), cp_src[i],
301                             strlen(subpartition_get_mountpoint(sp))) == 0) {
302                                 seen_it = 1;
303                                 break;
304                         }
305                         if (strncmp(cp_src[i], subpartition_get_mountpoint(sp),
306                             strlen(cp_src[i])) == 0) {
307                                 prefix = 1;
308                         }
309                 }
310                 if (seen_it || !prefix)
311                         continue;
312
313                 /*
314                  * Otherwise, cpdup the subpartition.
315                  *
316                  * XXX check for .hdd-extended source dirs here, too,
317                  * eventually - but for now, /etc.hdd will never be
318                  * the kind of tricky sub-mount-within-a-mount-point
319                  * that this part of the code is meant to handle.
320                  */
321                 cmd = command_add(cmds, "%s%s %s%s %smnt%s",
322                     a->os_root, cmd_name(a, "CPDUP"),
323                     a->os_root, &subpartition_get_mountpoint(sp)[1],
324                     a->os_root, subpartition_get_mountpoint(sp));
325                 command_set_log_mode(cmd, COMMAND_LOG_QUIET);
326         }
327         
328         /*
329          * Create symlinks.
330          */
331
332         /*
333          * If the user has both /var and /tmp subparitions,
334          * symlink /var/tmp to /tmp.
335          */
336         if (subpartition_find(storage_get_selected_slice(a->s), "/tmp") != NULL &&
337             subpartition_find(storage_get_selected_slice(a->s), "/var") != NULL) {
338                 command_add(cmds, "%s%s 1777 %smnt/tmp",
339                     a->os_root, cmd_name(a, "CHMOD"), a->os_root);
340                 command_add(cmds, "%s%s -rf %smnt/var/tmp",
341                     a->os_root, cmd_name(a, "RM"), a->os_root);
342                 command_add(cmds, "%s%s -s /tmp %smnt/var/tmp",
343                     a->os_root, cmd_name(a, "LN"), a->os_root);
344         }
345
346         /*
347          * If the user has /var, but no /tmp,
348          * symlink /tmp to /var/tmp.
349          */
350         if (subpartition_find(storage_get_selected_slice(a->s), "/tmp") == NULL &&
351             subpartition_find(storage_get_selected_slice(a->s), "/var") != NULL) {
352                 command_add(cmds, "%s%s -rf %smnt/tmp",
353                     a->os_root, cmd_name(a, "RM"), a->os_root);
354                 command_add(cmds, "%s%s -s /var/tmp %smnt/tmp",
355                     a->os_root, cmd_name(a, "LN"), a->os_root);
356         }
357
358         /*
359          * If the user has /usr, but no /home,
360          * symlink /home to /usr/home.
361          */
362         if (subpartition_find(storage_get_selected_slice(a->s), "/home") == NULL &&
363             subpartition_find(storage_get_selected_slice(a->s), "/usr") != NULL) {
364                 command_add(cmds, "%s%s -rf %smnt/home",
365                     a->os_root, cmd_name(a, "RM"), a->os_root);
366                 command_add(cmds, "%s%s %smnt/usr/home",
367                     a->os_root, cmd_name(a, "MKDIR"), a->os_root);
368                 command_add(cmds, "%s%s -s /usr/home %smnt/home",
369                     a->os_root, cmd_name(a, "LN"), a->os_root);
370         }
371
372         /*
373          * XXX check for other possible combinations too?
374          */
375
376         /*
377          * Clean up.  In case some file didn't make it, use rm -f
378          */
379 #ifdef __DragonFly__
380         command_add(cmds, "%s%s -f %smnt/boot/loader.conf",
381             a->os_root, cmd_name(a, "RM"), a->os_root);
382 #endif
383         command_add(cmds, "%s%s -f %smnt/tmp/install.log",
384             a->os_root, cmd_name(a, "RM"), a->os_root);
385
386         /*
387          * Copy pristine versions over any files we might have installed.
388          * This allows the resulting file tree to be customized.
389          */
390         for (i = 0; cp_src[i] != NULL && cp_src[i][0] != '\0'; i++) {
391                 char *src, *dest, *dn, *tmp_dest;
392
393                 src = cp_src[i];
394                 dest = cp_src[i];
395
396                 /*
397                  * Get the directory that the desired thing to
398                  * copy resides in.
399                  */
400                 tmp_dest = aura_strdup(dest);
401                 dn = dirname(tmp_dest);
402
403                 /*
404                  * If this dir doesn't exist in PRISTINE_DIR
405                  * on the install media, just skip it.
406                  */
407                 if (!is_dir("%s%s%s", a->os_root, PRISTINE_DIR, dn)) {
408                         aura_free(tmp_dest, _("directory name"));
409                         continue;
410                 }
411
412                 /*
413                  * Create intermediate directories, if needed.
414                  */
415                 if (!is_dir("%smnt%s", a->os_root, dn)) {
416                         command_add(cmds, "%s%s -p %smnt%s",
417                             a->os_root, cmd_name(a, "MKDIR"),
418                             a->os_root, dn);
419                 }
420                 aura_free(tmp_dest, "directory name");
421
422                 /*
423                  * Cpdup the chosen file or directory onto the HDD.
424                  */
425                 cmd = command_add(cmds, "%s%s %s%s %smnt%s",
426                     a->os_root, cmd_name(a, "CPDUP"),
427                     a->os_root, src,
428                     a->os_root, dest);
429
430                 cmd = command_add(cmds,
431                     "%s%s %s%s%s %smnt%s",
432                     a->os_root, cmd_name(a, "CPDUP"),
433                     a->os_root, PRISTINE_DIR, src,
434                     a->os_root, dest);
435                 command_set_log_mode(cmd, COMMAND_LOG_QUIET);
436         }
437         
438         /*
439          * Rebuild the user database, to get rid of any extra users
440          * from the LiveCD that aren't supposed to be installed
441          * (copying a pristine master.passwd isn't enough.)
442          */
443         command_add(cmds, "%s%s -p -d %smnt/etc %smnt/etc/master.passwd",
444             a->os_root, cmd_name(a, "PWD_MKDB"), a->os_root, a->os_root);
445
446         /* Create missing directories. */
447         command_add(cmds, "%s%s %smnt/proc",
448             a->os_root, cmd_name(a, "MKDIR"), a->os_root);
449         command_add(cmds, "%s%s %smnt/mnt",
450             a->os_root, cmd_name(a, "MKDIR"), a->os_root);
451
452         /* Write new fstab. */
453
454         command_add(cmds, "%s%s '%s' >%smnt/etc/fstab",
455             a->os_root, cmd_name(a, "ECHO"),
456             "# Device\t\tMountpoint\tFStype\tOptions\t\tDump\tPass#",
457             a->os_root);
458
459         for (sp = slice_subpartition_first(storage_get_selected_slice(a->s));
460              sp != NULL; sp = subpartition_next(sp)) {
461                 if (strcmp(subpartition_get_mountpoint(sp), "/") == 0) {
462                         command_add(cmds, "%s%s '/dev/%s\t\t%s\t\tufs\trw\t\t1\t1' >>%smnt/etc/fstab",
463                             a->os_root, cmd_name(a, "ECHO"),
464                             subpartition_get_device_name(sp),
465                             subpartition_get_mountpoint(sp),
466                             a->os_root);
467                 } else if (strcmp(subpartition_get_mountpoint(sp), "swap") == 0) {
468                         command_add(cmds, "%s%s '/dev/%s\t\tnone\t\tswap\tsw\t\t0\t0' >>%smnt/etc/fstab",
469                             a->os_root, cmd_name(a, "ECHO"),
470                             subpartition_get_device_name(sp),
471                             a->os_root);
472                 } else if (subpartition_is_mfsbacked(sp)) {
473                         mfsbacked_size = slice_get_capacity(storage_get_selected_slice(a->s)) * 2048;
474                         command_add(cmds, "%s%s 'swap\t\t%s\t\t\tmfs\trw,-s%ld\t\t1\t1' >>%smnt/etc/fstab",
475                                 a->os_root, cmd_name(a, "ECHO"),
476                                 subpartition_get_mountpoint(sp),
477                                 mfsbacked_size,
478                                 a->os_root);
479                 } else {
480                         command_add(cmds, "%s%s '/dev/%s\t\t%s\t\tufs\trw\t\t2\t2' >>%smnt/etc/fstab",
481                             a->os_root, cmd_name(a, "ECHO"),
482                             subpartition_get_device_name(sp),
483                             subpartition_get_mountpoint(sp),
484                             a->os_root);
485                 }
486         }
487
488         command_add(cmds, "%s%s '%s' >>%smnt/etc/fstab",
489             a->os_root, cmd_name(a, "ECHO"),
490             "proc\t\t\t/proc\t\tprocfs\trw\t\t0\t0",
491             a->os_root);
492
493         /* Backup the disklabel and the log. */
494
495         command_add(cmds, "%s%s %s > %smnt/etc/disklabel.%s",
496             a->os_root, cmd_name(a, "DISKLABEL"),
497             slice_get_device_name(storage_get_selected_slice(a->s)),
498             a->os_root,
499             slice_get_device_name(storage_get_selected_slice(a->s)));
500         command_add(cmds, "%s%s %sinstall.log %smnt/var/log/install.log",
501             a->os_root, cmd_name(a, "CP"),
502             a->tmp, a->os_root);
503         command_add(cmds, "%s%s 600 %smnt/var/log/install.log",
504             a->os_root, cmd_name(a, "CHMOD"), a->os_root);
505
506         /* Customize stuff here */
507         if(is_file("%susr/local/bin/after_installation_routines.sh")) {
508                 command_add(cmds, "%susr/local/bin/after_installation_routines.sh",
509                 a->os_root, _("Running after installation custom routines..."));
510         }
511
512         /*
513          * Do it!
514          */
515         /* commands_preview(cmds); */
516         if (!commands_execute(a, cmds)) {
517                 inform(a->c, _("%s was not fully installed."), OPERATING_SYSTEM_NAME);
518                 a->result = 0;
519         } else {
520                 a->result = 1;
521         }
522         commands_free(cmds);
523
524         /*
525          * Unmount everything we mounted on /mnt.  This is done in a seperate
526          * command chain, so that partitions are unmounted, even if an error
527          * occurs in one of the preceding commands, or it is cancelled.
528          */
529         cmds = commands_new();
530         unmount_all_under(a, cmds, "%smnt", a->os_root);
531
532         /*
533          * Once everything is unmounted, if the install went successfully,
534          * make sure once and for all that the disklabel is bootable.
535          */
536         if (a->result) {
537                 command_add(cmds, "%s%s -B %s",
538                     a->os_root, cmd_name(a, "DISKLABEL"),
539                     slice_get_device_name(storage_get_selected_slice(a->s)));
540         }
541
542         if (!commands_execute(a, cmds))
543                 inform(a->c, _("Warning: subpartitions were not correctly unmounted."));
544
545         commands_free(cmds);
546 }
547