Installer-Pfade gefixxt.
[people/pmueller/ipfire-2.x.git] / src / install+setup / install / main.c
1 /* SmoothWall install program.
2  *
3  * This program is distributed under the terms of the GNU General Public
4  * Licence.  See the file COPYING for details.
5  *
6  * (c) Lawrence Manning, 2001
7  * Contains main entry point, and misc functions.
8  * 
9  */
10
11 #include "install.h"
12 #define _GNU_SOURCE
13  
14 #define CDROM_INSTALL 0
15 #define URL_INSTALL 1
16 #define DISK_INSTALL 2
17 #define INST_FILECOUNT 6600
18 #define UNATTENDED_CONF "/cdrom/boot/unattended.conf"
19
20 int raid_disk = 0;
21 FILE *flog = NULL;
22 char *mylog;
23
24 char **ctr;
25
26 char *pcmcia = NULL;
27 extern char url[STRING_SIZE];
28
29 extern char *en_tr[];
30 extern char *de_tr[];
31
32 int detect_smp() {
33         FILE *fd = NULL;
34         char line[STRING_SIZE];
35         int cpu_count = 0;
36
37         if ((fd = fopen("/proc/cpuinfo", "r")) == NULL) {
38                 return 0;
39         }
40         while (fgets(line, STRING_SIZE, fd) != NULL) {
41                 if (strstr(line, "processor") == line) {
42                         cpu_count++;
43                 }
44         }
45         (void)fclose(fd);
46         return (cpu_count > 1);
47 }
48
49 int generate_packages_list(char *packages, const char *rpmdir, const char *source) {
50
51         FILE *fd=NULL;
52         char buffer[STRING_SIZE];
53         bzero(buffer, sizeof(buffer));
54
55         if ((fd = fopen(source, "r")) == NULL) {
56                 (void) fprintf(flog, "Packages file %s not found\n", source);
57                 return -1;
58         }
59         while (fgets(buffer, sizeof(buffer), fd) != NULL) {
60                 int length = -1;
61                 length = strlen(buffer)-1;
62                 if (length<=0) {
63                         continue;
64                 }
65                 if (buffer[length] == '\n') {
66                         buffer[length]='\0';
67                 }
68                 length = snprintf(packages, STRING_SIZE, "%s %s/%s", strdup(packages), rpmdir, buffer);
69                 if ((length <0) || (length >STRING_SIZE)) {
70                         (void) fprintf(flog, "rpm command line too long: %d\n%s", length, packages);
71                         return -1;
72                 }
73         }
74         if (ferror(fd)) {
75                 (void) fprintf(flog, "Error reading file\n");
76                 (void) fclose(fd);
77                 return -1;
78         }
79         (void) fclose(fd);
80         return 0;
81 }
82
83 long calc_swapsize(long memory, long disk) {
84         if (memory < 128) {
85                 return 256;
86         }
87         if (memory > 1024) {
88                 return 512;
89         }
90
91         return memory*2;
92 }
93
94 int unattended_setup(struct keyvalue *unattendedkv) {
95     struct keyvalue *mainsettings = initkeyvalues();
96     struct keyvalue *ethernetkv = initkeyvalues();
97     FILE *file, *hosts;
98     char commandstring[STRING_SIZE];
99
100     char domainname[STRING_SIZE];
101     char hostname[STRING_SIZE];
102     char keymap[STRING_SIZE];
103     char language[STRING_SIZE];
104     char timezone[STRING_SIZE];
105     char green_address[STRING_SIZE];
106     char green_netmask[STRING_SIZE];
107     char green_netaddress[STRING_SIZE];
108     char green_broadcast[STRING_SIZE];
109     char root_password[STRING_SIZE];
110     char admin_password[STRING_SIZE];
111     char serial_console[STRING_SIZE];
112     char reversesort[STRING_SIZE];
113
114     findkey(unattendedkv, "DOMAINNAME", domainname);
115     findkey(unattendedkv, "HOSTNAME", hostname);
116     findkey(unattendedkv, "KEYMAP", keymap);
117     findkey(unattendedkv, "LANGUAGE", language);
118     findkey(unattendedkv, "TIMEZONE", timezone);
119     findkey(unattendedkv, "GREEN_ADDRESS", green_address);
120     findkey(unattendedkv, "GREEN_NETMASK", green_netmask);
121     findkey(unattendedkv, "GREEN_NETADDRESS", green_netaddress);
122     findkey(unattendedkv, "GREEN_BROADCAST", green_broadcast);
123     findkey(unattendedkv, "ROOT_PASSWORD", root_password);
124     findkey(unattendedkv, "ADMIN_PASSWORD", admin_password);
125     findkey(unattendedkv, "SERIAL_CONSOLE", serial_console);
126     findkey(unattendedkv, "REVERSE_NICS", reversesort);
127
128     /* write main/settings. */
129     replacekeyvalue(mainsettings, "DOMAINNAME", domainname);
130     replacekeyvalue(mainsettings, "HOSTNAME", hostname);
131     replacekeyvalue(mainsettings, "KEYMAP", keymap);
132     replacekeyvalue(mainsettings, "LANGUAGE", language);
133     replacekeyvalue(mainsettings, "TIMEZONE", timezone);
134     writekeyvalues(mainsettings, "/harddisk" CONFIG_ROOT "/main/settings");
135     freekeyvalues(mainsettings);
136
137     /* do setup stuff */
138     fprintf(flog, "unattended: Starting setup\n");
139
140     /* network */
141
142     fprintf(flog, "unattended: setting up network configuration\n");
143
144     (void) readkeyvalues(ethernetkv, "/harddisk" CONFIG_ROOT "/ethernet/settings");
145     replacekeyvalue(ethernetkv, "GREEN_ADDRESS", green_address);
146     replacekeyvalue(ethernetkv, "GREEN_NETMASK", green_netmask);
147     replacekeyvalue(ethernetkv, "GREEN_NETADDRESS", green_netaddress);
148     replacekeyvalue(ethernetkv, "GREEN_BROADCAST", green_broadcast);
149     replacekeyvalue(ethernetkv, "CONFIG_TYPE", "0");
150     replacekeyvalue(ethernetkv, "GREEN_DEV", "eth0");
151     write_ethernet_configs(ethernetkv);
152     freekeyvalues(ethernetkv);
153
154     /* timezone */
155     unlink("/harddisk/etc/localtime");
156     snprintf(commandstring, STRING_SIZE, "/harddisk/%s", timezone);
157     link(commandstring, "/harddisk/etc/localtime");
158
159     /* hostname */
160     fprintf(flog, "unattended: writing hostname.conf\n");
161     if (!(file = fopen("/harddisk" CONFIG_ROOT "/main/hostname.conf", "w")))
162     {
163         errorbox("unattended: ERROR writing hostname.conf");
164         return 0;
165     }
166     fprintf(file, "ServerName %s\n", hostname);
167     fclose(file);                                   
168
169     fprintf(flog, "unattended: writing hosts\n");
170     if (!(hosts = fopen("/harddisk/etc/hosts", "w")))
171     {
172         errorbox("unattended: ERROR writing hosts");
173         return 0;
174     }
175     fprintf(hosts, "127.0.0.1\tlocalhost\n");
176     fprintf(hosts, "%s\t%s.%s\t%s\n", green_address, hostname, domainname, hostname);
177     fclose(hosts);                                                              
178
179     fprintf(flog, "unattended: writing hosts.allow\n");
180     if (!(file = fopen("/harddisk/etc/hosts.allow", "w")))
181     {
182         errorbox("unattended: ERROR writing hosts.allow");
183         return 0;
184     }
185     fprintf(file, "sshd : ALL\n");
186     fprintf(file, "ALL  : localhost\n");
187     fprintf(file, "ALL  : %s/%s\n", green_netaddress, green_netmask);
188     fclose(file);
189
190     fprintf(flog, "unattended: writing hosts.deny\n");
191     if (!(file = fopen("/harddisk/etc/hosts.deny", "w")))
192     {
193         errorbox("unattended: ERROR writing hosts.deny");
194         return 0;
195     }
196     fprintf(file, "ALL : ALL\n");
197     fclose(file);
198
199     if (strcmp(serial_console, "yes") != 0) {
200             snprintf(commandstring, STRING_SIZE,
201                      "/sbin/chroot /harddisk /bin/sed -i -e \"s/^s0/#s0/\" /etc/inittab");
202             if (mysystem(commandstring)) {
203                     errorbox("unattended: ERROR modifying inittab");
204                     return 0;    
205             }
206
207             snprintf(commandstring, STRING_SIZE,
208                      "/sbin/chroot /harddisk /bin/sed -i -e \"s/^serial/#serial/; s/^terminal/#terminal/\" /boot/grub/grub.conf");
209             if (mysystem(commandstring)) {
210                     errorbox("unattended: ERROR modifying inittab");
211                     return 0;
212             }
213     }
214
215     /* set reverse sorting of interfaces */
216     if (strcmp(reversesort, "yes") == 0) {
217             mysystem("/bin/touch /harddisk/var/ipfire/ethernet/reverse_nics");
218     }
219
220     /* set root password */
221     fprintf(flog, "unattended: setting root password\n");
222     
223     snprintf(commandstring, STRING_SIZE,
224             "/sbin/chroot /harddisk /bin/sh -c \"echo 'root:%s' | /usr/sbin/chpasswd\"", root_password);
225     if (mysystem(commandstring)) {
226         errorbox("unattended: ERROR setting root password");
227         return 0;
228     }
229     
230     /* set admin password */
231     fprintf(flog, "unattended: setting admin password\n");
232     snprintf(commandstring, STRING_SIZE,
233             "/sbin/chroot /harddisk /usr/bin/htpasswd -c -m -b " CONFIG_ROOT "/auth/users admin '%s'", admin_password);
234     if (mysystem(commandstring)) {
235         errorbox("unattended: ERROR setting admin password");
236         return 0;    
237     }
238     
239     return 1;                                                           
240 }
241
242 int main(int argc, char *argv[])
243 {
244         char *langnames[] = { "Deutsch", "English", NULL };
245         char *shortlangnames[] = { "de", "en", NULL };
246         char **langtrs[] = { de_tr, en_tr, NULL };
247         char hdletter, cdletter;
248         char harddrive[5], sourcedrive[5];      /* Device holder. */
249         struct devparams hdparams, cdromparams; /* Params for CDROM and HD */
250         int cdmounted = 0; /* Loop flag for inserting a cd. */
251         int rc = 0;
252         char commandstring[STRING_SIZE];
253         char *installtypes[] = { "CDROM", "HTTP/FTP", NULL };
254         int installtype = CDROM_INSTALL;
255         char insertmessage[STRING_SIZE];
256         char insertdevnode[STRING_SIZE];
257         int choice;
258         char shortlangname[10];
259         char message[1000];
260         char title[STRING_SIZE];
261         int allok = 0;
262         int allok_fastexit=0;
263         int unmount_before=0;
264         struct keyvalue *ethernetkv = initkeyvalues();
265         FILE *handle, *cmdfile;
266         char line[STRING_SIZE];
267         char string[STRING_SIZE];
268         long maximum_free = 0, current_free;
269         long memory = 0;
270         long system_partition, boot_partition, root_partition, swap_file;
271         int scsi_disk = 0;
272         int pcmcia_disk = 0;
273         int pcmcia_cdrom = 0;
274         int scsi_cdrom = 0;
275         int ide_cdrom = 0;
276         int fdisk = 0;
277         int hardyn = 0;
278         char *yesnoharddisk[] = { "NO", "YES", NULL };
279         char *yesno[] = { "NO", "YES", NULL };
280         char green[STRING_SIZE];
281         int unattended = 0;
282         struct keyvalue *unattendedkv = initkeyvalues();
283         char packages[STRING_SIZE];
284         int serial_console = 0;
285         char megabridge[STRING_SIZE];
286
287         setlocale (LC_ALL, "");
288         sethostname( SNAME , 10);
289
290         memset(&hdparams, 0, sizeof(struct devparams));
291         memset(&cdromparams, 0, sizeof(struct devparams));
292
293         /* Log file/terminal stuff. */
294         if (argc >= 2)
295         {               
296                 if (!(flog = fopen(argv[1], "w+")))
297                         return 0;
298         }
299         else
300                 return 0;
301         
302         mylog = argv[1];
303         
304         fprintf(flog, "Install program started.\n");
305                 
306         newtInit();
307         newtCls();
308
309         /* Do usb detection first for usb keyboard */
310         if (! (cmdfile = fopen("/proc/cmdline", "r")))
311         {
312                 fprintf(flog, "Couldn't open commandline: /proc/cmdline\n");
313         } else {
314                 fgets(line, STRING_SIZE, cmdfile);
315                 if (strstr (line, "fdisk") != NULL) {
316                         fprintf(flog, "Manual FDISK selected.\n");
317                         fdisk = 1;
318                 }
319                 if (strstr (line, "nopcmcia") == NULL) {
320                         fprintf(flog, "Initializing PCMCIA controllers.\n");
321                         pcmcia = initialize_pcmcia();
322                         if (pcmcia) {
323                                 fprintf (flog, "Detected PCMCIA Controller: %s.\n", pcmcia);
324                                 sprintf(commandstring, "/sbin/modprobe %s", pcmcia);
325                                 mysystem("/sbin/modprobe pcmcia_core");
326                                 mysystem(commandstring);
327                                 mysystem("/sbin/modprobe ds");
328                                 /* pcmcia netcard drivers are not available from Boot floppy,
329                                  * they will be loaded from Drivers floppy later */
330                         } else {
331                                 fprintf (flog, "Detected No PCMCIA Controller.\n");
332                         }
333                 } else {
334                         fprintf(flog, "Skipping PCMCIA detection.\n");
335                 }
336                 if (strstr (line, "nousb") == NULL) {
337                         fprintf(flog, "Initializing USB controllers.\n");
338                         initialize_usb();
339                 } else {
340                         fprintf(flog, "Skipping USB detection.\n");
341                 }
342                 // check if we have to make an unattended install
343                 if (strstr (line, "unattended") != NULL) {              
344                     unattended = 1;
345                 }
346         }
347
348         // make some beeps before wiping the system :)
349         if (unattended) {
350             runcommandwithstatus("/bin/sleep 10", "WARNING: Unattended installation will start in 10 seconds...");
351         }
352         
353         /* German is the default */
354         for (choice = 0; langnames[choice]; choice++)
355         {
356                 if (strcmp(langnames[choice], "Deutsch") == 0)
357                         break;
358         }
359         if (!langnames[choice])
360                 goto EXIT;
361
362         if (!unattended) {
363             rc = newtWinMenu("Language selection",
364                     "Select the language you wish to use for the " NAME ".", 50, 5, 5, 8,
365                     langnames, &choice, "Ok", NULL);
366         }
367
368         ctr = langtrs[choice];
369         strcpy(shortlangname, shortlangnames[choice]);
370
371         mysystem("/bin/setfont lat0-16");
372
373         newtDrawRootText(14, 0, NAME " " VERSION " - " SLOGAN );
374         newtPushHelpLine(ctr[TR_HELPLINE]);
375
376         if (!unattended) {
377                 sprintf(message, ctr[TR_WELCOME], NAME);
378                 sprintf (title, "%s %s - %s", NAME, VERSION, SLOGAN);
379                 newtWinMessage(title, ctr[TR_OK], message);
380
381                 sprintf(message, ctr[TR_SELECT_INSTALLATION_MEDIA_LONG], NAME);
382                 rc = newtWinMenu(ctr[TR_SELECT_INSTALLATION_MEDIA], message,
383                         50, 5, 5, 6, installtypes, &installtype, ctr[TR_OK],
384                         ctr[TR_CANCEL], NULL);
385         }
386         else {
387             rc = 1;
388             installtype = CDROM_INSTALL;
389         }
390
391         if (rc == 2)
392                 goto EXIT;
393
394         // Starting hardware detection
395         runcommandwithstatus("/bin/probehw.sh", ctr[TR_PROBING_HARDWARE]);
396
397         /* CDROM INSTALL */
398         if (installtype == CDROM_INSTALL) {
399
400                 switch (mysystem("/bin/mountsource.sh")) {
401                     case 0:
402                         installtype = CDROM_INSTALL;
403                         cdmounted = 1;
404                         break;
405                     case 1:
406                         installtype = DISK_INSTALL;
407                         break;
408                     case 10:
409                         errorbox(ctr[TR_NO_CDROM]);
410                         goto EXIT;
411                 }
412
413                 /* read source drive letter */
414                 if ((handle = fopen("/source_device", "r")) == NULL) {
415                         errorbox(ctr[TR_ERROR_PROBING_CDROM]);
416                         goto EXIT;
417                 }
418                 fgets(sourcedrive, 5, handle);
419                 fprintf(flog, "Source drive: %s\n", sourcedrive);
420                 fclose(handle);
421
422                 snprintf(cdromparams.devnode, STRING_SIZE, "/dev/%s", sourcedrive);
423                 cdromparams.module = 0;
424                 fprintf(flog, "Source device: %s\n", cdromparams.devnode);
425         }
426
427         /* Configure the network now! */
428         if (installtype == URL_INSTALL) {
429                 /* Network driver and params. */
430                 if (!(networkmenu(ethernetkv))) {
431                         errorbox(ctr[TR_NETWORK_SETUP_FAILED]);
432                         goto EXIT;
433                 }
434
435                 /* Check for ipcop-<VERSION>.tbz2 */
436                 if (checktarball(SNAME "-" VERSION ".tbz2", ctr[TR_ENTER_URL])) {
437                         errorbox(ctr[TR_NO_IPCOP_TARBALL_FOUND]);
438                         goto EXIT;
439                 }
440         }
441
442         /* Get device for the HD.  This has to succeed. */
443         if (!(hdletter = findidetype(IDE_HD)))
444         {
445                 /* Need to clean this up at some point */
446                 if (!try_scsi("sda") || strstr(sourcedrive, "sda") != NULL) {
447                         if (!try_scsi("ida/c0d0")) {
448                                 if (!try_scsi("cciss/c0d0")) {
449                                         if (!try_scsi("rd/c0d0")) {
450                                                 if (!try_scsi("ataraid/d0")) {
451                                                         errorbox(ctr[TR_NO_HARDDISK]);
452                                                         goto EXIT;
453                                                 } else {
454                                                         raid_disk = 1;
455                                                         sprintf(harddrive, "ataraid/d0");
456                                                 }
457                                         } else {
458                                                 raid_disk = 1;
459                                                 sprintf(harddrive, "rd/c0d0");
460                                         }
461                                 } else {
462                                         raid_disk = 1;
463                                         sprintf(harddrive, "cciss/c0d0");
464                                 }
465                         } else {
466                                 raid_disk = 1;
467                                 sprintf(harddrive, "ida/c0d0");
468                         }
469                 } else {
470                     if (strstr(sourcedrive, "sda") != NULL) {
471                         // probably installing from usb stick, try sdb
472                         if (try_scsi("sdb")) {
473                             sprintf(harddrive, "sdb");
474                         }
475                         else {
476                             errorbox(ctr[TR_NO_HARDDISK]);
477                             goto EXIT;
478                         }
479                     }
480                     else {
481                         sprintf(harddrive, "sda");
482                     }
483                 }
484                 scsi_disk = 1;
485         } else
486                 sprintf(harddrive, "hd%c", hdletter);
487
488         fprintf(flog, "Destination drive: %s\n", harddrive);
489
490         /* load unattended configuration */
491         if (unattended) {
492             fprintf(flog, "unattended: Reading unattended.conf\n");
493
494             (void) readkeyvalues(unattendedkv, UNATTENDED_CONF);
495         }
496
497         /* Make the hdparms struct and print the contents. */
498         snprintf(hdparams.devnode, STRING_SIZE, "/dev/%s", harddrive);
499         hdparams.module = 0;
500
501         sprintf(message, ctr[TR_PREPARE_HARDDISK], hdparams.devnode);
502         
503         if (unattended) {
504             hardyn = 1;
505         }
506         
507         while (! hardyn) {
508                 rc = newtWinMenu(title, message,
509                                  50, 5, 5, 6, yesnoharddisk,
510                                  &hardyn, ctr[TR_OK],
511                                  ctr[TR_CANCEL], NULL);
512                 if (rc == 2)
513                         goto EXIT;
514         }
515
516         if (rc == 2)
517                 goto EXIT;
518
519         /* Calculate amount of memory in machine */
520         if ((handle = fopen("/proc/meminfo", "r")))
521         {
522             while (fgets(line, STRING_SIZE-1, handle)) {
523                 if (sscanf (line, "MemTotal: %s kB", string)) {
524                     memory = atoi(string) / 1024 ;
525                 }
526             }
527             fclose(handle);
528         }
529
530         /* Partition, mkswp, mkfs.
531          * before partitioning, first determine the sizes of each
532          * partition.  In order to do that we need to know the size of
533          * the disk. 
534          */
535         /* Don't use mysystem here so we can redirect output */
536         sprintf(commandstring, "/bin/sfdisk -s /dev/%s > /disksize 2> /dev/null", harddrive);
537         system(commandstring);
538
539         /* Calculate amount of disk space */
540         if ((handle = fopen("/disksize", "r")))
541         {
542                 fgets(line, STRING_SIZE-1, handle);
543                 if (sscanf (line, "%s", string)) {
544                         maximum_free = atoi(string) / 1024;
545                 }
546                 fclose(handle);
547         }
548         
549         fprintf(flog, "maximum_free = %ld, memory = %ld", 
550                 maximum_free, memory);
551         
552         swap_file = calc_swapsize(memory, maximum_free);
553
554         if (maximum_free < 512 + swap_file ) {
555                 if (maximum_free < 512) {
556                         errorbox(ctr[TR_DISK_TOO_SMALL]);
557                         goto EXIT;
558                 }
559
560                 if (!unattended) {
561                     rc = newtWinChoice(title, ctr[TR_OK], ctr[TR_CANCEL], ctr[TR_CONTINUE_NO_SWAP]);
562                 }
563                 else {
564                     rc = 1;
565                 }
566                 
567                 if (rc != 1)
568                         goto EXIT;
569                 swap_file = 0;
570         }
571
572         boot_partition = 20; /* in MB */
573         current_free = maximum_free - boot_partition - swap_file;
574
575         root_partition = 2048 ;
576         if (current_free < 512) {
577                 errorbox(ctr[TR_DISK_TOO_SMALL]);
578                 goto EXIT;
579         }
580
581         current_free = current_free - root_partition;
582         if (!swap_file) {
583                 root_partition = root_partition + swap_file;
584         }
585
586         system_partition = current_free;
587
588         fprintf(flog, "boot = %ld, swap = %ld, mylog = %ld, root = %ld\n",
589                 boot_partition, swap_file, system_partition, root_partition);
590
591         handle = fopen("/tmp/partitiontable", "w");
592
593         /* Make swapfile */
594         if (swap_file) {
595                 fprintf(handle, ",%ld,L,*\n,%ld,S,\n,%ld,L,\n,,L,\n",
596                         boot_partition, swap_file, root_partition);
597         } else {
598                 fprintf(handle, ",%ld,L,*\n,0,0,\n,%ld,L,\n,,L,\n",
599                         boot_partition, root_partition);
600         }
601
602         fclose(handle);
603
604         snprintf(commandstring, STRING_SIZE, "/bin/sfdisk -L -uM %s < /tmp/partitiontable", hdparams.devnode);
605         if (runcommandwithstatus(commandstring, ctr[TR_PARTITIONING_DISK]))
606         {
607                 errorbox(ctr[TR_UNABLE_TO_PARTITION]);
608                 goto EXIT;
609         }
610
611         mysystem("/sbin/udevstart");
612
613         if (raid_disk)
614                 snprintf(commandstring, STRING_SIZE, "/bin/mke2fs -T ext2 -c %sp1", hdparams.devnode);
615         else
616                 snprintf(commandstring, STRING_SIZE, "/bin/mke2fs -T ext2 -c %s1", hdparams.devnode);
617         if (runcommandwithstatus(commandstring, ctr[TR_MAKING_BOOT_FILESYSTEM]))
618         {
619                 errorbox(ctr[TR_UNABLE_TO_MAKE_BOOT_FILESYSTEM]);
620                 goto EXIT;
621         }
622
623         if (swap_file) {
624                 if (raid_disk)
625                         snprintf(commandstring, STRING_SIZE, "/sbin/mkswap %sp2", hdparams.devnode);    
626                 else
627                         snprintf(commandstring, STRING_SIZE, "/sbin/mkswap %s2", hdparams.devnode);
628                 if (runcommandwithstatus(commandstring, ctr[TR_MAKING_SWAPSPACE]))
629                 {
630                         errorbox(ctr[TR_UNABLE_TO_MAKE_SWAPSPACE]);
631                         goto EXIT;
632                 }
633         }
634
635         if (raid_disk)
636                 snprintf(commandstring, STRING_SIZE, "/bin/mkreiserfs -f %sp3", hdparams.devnode);      
637         else
638                 snprintf(commandstring, STRING_SIZE, "/bin/mkreiserfs -f %s3", hdparams.devnode);       
639
640         if (runcommandwithstatus(commandstring, ctr[TR_MAKING_ROOT_FILESYSTEM]))
641         {
642                 errorbox(ctr[TR_UNABLE_TO_MAKE_ROOT_FILESYSTEM]);
643                 goto EXIT;
644         }
645
646         if (raid_disk)
647                 snprintf(commandstring, STRING_SIZE, "/bin/mkreiserfs -f %sp4", hdparams.devnode);      
648         else
649                 snprintf(commandstring, STRING_SIZE, "/bin/mkreiserfs -f %s4", hdparams.devnode);       
650
651         if (runcommandwithstatus(commandstring, ctr[TR_MAKING_LOG_FILESYSTEM]))
652         {
653                 errorbox(ctr[TR_UNABLE_TO_MAKE_ROOT_FILESYSTEM]);
654                 goto EXIT;
655         }
656
657         /* Mount harddisk. */
658         if (raid_disk)
659                 snprintf(commandstring, STRING_SIZE, "/bin/mount %sp3 /harddisk", hdparams.devnode);
660         else
661                 snprintf(commandstring, STRING_SIZE, "/bin/mount %s3 /harddisk", hdparams.devnode);
662         if (runcommandwithstatus(commandstring, ctr[TR_MOUNTING_ROOT_FILESYSTEM]))
663         {
664                 errorbox(ctr[TR_UNABLE_TO_MOUNT_ROOT_FILESYSTEM]);
665                 goto EXIT;
666         }
667
668         mkdir("/harddisk/boot", S_IRWXU|S_IRWXG|S_IRWXO);
669         mkdir("/harddisk/var", S_IRWXU|S_IRWXG|S_IRWXO);
670         mkdir("/harddisk/var/log", S_IRWXU|S_IRWXG|S_IRWXO);
671         
672         if (raid_disk)
673                 snprintf(commandstring, STRING_SIZE, "/bin/mount %sp1 /harddisk/boot", hdparams.devnode);
674         else
675                 snprintf(commandstring, STRING_SIZE, "/bin/mount %s1 /harddisk/boot", hdparams.devnode);
676
677         if (runcommandwithstatus(commandstring, ctr[TR_MOUNTING_BOOT_FILESYSTEM]))
678         {
679                 errorbox(ctr[TR_UNABLE_TO_MOUNT_BOOT_FILESYSTEM]);
680                 goto EXIT;
681         }
682         if (swap_file) {
683                 if (raid_disk)
684                         snprintf(commandstring, STRING_SIZE, "/sbin/swapon %sp2", hdparams.devnode);
685                 else
686                         snprintf(commandstring, STRING_SIZE, "/sbin/swapon %s2", hdparams.devnode);
687                 if (runcommandwithstatus(commandstring, ctr[TR_MOUNTING_SWAP_PARTITION]))
688                 {
689                         errorbox(ctr[TR_UNABLE_TO_MOUNT_SWAP_PARTITION]);
690                         goto EXIT;
691                 }
692         }
693         if (raid_disk)
694                 snprintf(commandstring, STRING_SIZE, "/bin/mount %sp4 /harddisk/var", hdparams.devnode);
695         else
696                 snprintf(commandstring, STRING_SIZE, "/bin/mount %s4 /harddisk/var", hdparams.devnode);
697         if (runcommandwithstatus(commandstring, ctr[TR_MOUNTING_LOG_FILESYSTEM]))
698         {
699                 errorbox(ctr[TR_UNABLE_TO_MOUNT_LOG_FILESYSTEM]);
700                 goto EXIT;
701         }
702         
703         snprintf(commandstring, STRING_SIZE, "/bin/tar -C /harddisk -xvjf /cdrom/" SNAME "-" VERSION ".tbz2");
704         
705         if (runcommandwithprogress(60, 4, title, commandstring, INST_FILECOUNT,
706                 ctr[TR_INSTALLING_FILES]))
707         {
708                 errorbox(ctr[TR_UNABLE_TO_INSTALL_FILES]);
709                 goto EXIT;
710         }
711                 
712         /* Save USB controller type to modules.conf */
713         write_usb_modules_conf();
714
715         /* touch the modules.dep files */
716         snprintf(commandstring, STRING_SIZE, 
717                 "/sbin/chroot /harddisk /usr/bin/touch /lib/modules/%s/modules.dep",
718                 KERNEL_VERSION);
719         mysystem(commandstring);
720         snprintf(commandstring, STRING_SIZE, 
721                 "/sbin/chroot /harddisk /usr/bin/touch /lib/modules/%s-smp/modules.dep",
722                 KERNEL_VERSION);
723         mysystem(commandstring);
724
725         /* Rename uname */
726         rename ("/harddisk/bin/uname.bak", "/harddisk/bin/uname");
727
728         /* Write PCMCIA Config */
729         if (pcmcia) {
730                 handle = fopen("/harddisk/etc/modules.conf", "a");
731                 if (handle != NULL) {
732                         fprintf (handle, "# PCMCIA Settings\n");
733                         fprintf (handle, "alias pcmcia-controller %s\n", pcmcia);
734                         fclose(handle);
735                 }
736         }
737
738         handle = fopen("/harddisk/etc/pcmcia.conf", "w");
739         if (handle != NULL) {
740                 if (pcmcia) {
741                         fprintf (handle, "PCMCIA=yes\n");
742                         fprintf (handle, "PCIC=%s\n", pcmcia);
743                 } else {
744                         fprintf (handle, "PCMCIA=no\n");
745                         fprintf (handle, "PCIC=\n");
746                 }
747                 fprintf (handle, "CARDMGR_OPTS=\n");
748                 fprintf (handle, "SCHEME=\n");
749                 fclose(handle);
750         }
751
752         /* *always* write disk configuration */
753         if (!(write_disk_configs(&hdparams))){
754           errorbox(ctr[TR_ERROR_WRITING_CONFIG]);
755           goto EXIT;
756         }
757
758         /* mount proc filesystem */
759         mysystem("mkdir /harddisk/proc");
760         mysystem("/bin/mount -t proc none /harddisk/proc");
761         mysystem("/bin/mount --bind /dev /harddisk/dev");
762
763
764
765         /* if we detected SCSI then fixup */
766         /* doesn't really work cause it sometimes creates a ramdisk on ide systems */
767 /*      mysystem("/bin/probecntrl.sh");
768         if ((handle = fopen("/cntrldriver", "r")))
769         {
770                 char *driver;
771                         fgets(line, STRING_SIZE-1, handle);
772                         fclose(handle);
773                 line[strlen(line) - 1] = 0;
774                 driver = strtok(line, ".");
775                 fprintf(flog, "Detected SCSI driver %s\n",driver);
776                 if (strlen(driver) > 1) {
777                         fprintf(flog, "Fixing up ipfirerd.img\n");
778                         mysystem("/sbin/chroot /harddisk /sbin/modprobe loop");
779                         mkdir("/harddisk/initrd", S_IRWXU|S_IRWXG|S_IRWXO);
780                         snprintf(commandstring, STRING_SIZE, "/sbin/chroot /harddisk /sbin/mkinitrd --with=scsi_mod --with=%s --with=sd_mod --with=sr_mod --with=libata /boot/ipfirerd.img %s", driver, KERNEL_VERSION);
781                         runcommandwithstatus(commandstring, ctr[TR_BUILDING_INITRD]);
782                         snprintf(commandstring, STRING_SIZE, "/sbin/chroot /harddisk /sbin/mkinitrd --with=scsi_mod --with=%s --with=sd_mod --with=sr_mod --with=libata /boot/ipfirerd-smp.img %s-smp", driver, KERNEL_VERSION);
783                         runcommandwithstatus(commandstring, ctr[TR_BUILDING_INITRD]);
784                         mysystem("/sbin/chroot /harddisk /bin/mv /boot/grub/scsigrub.conf /boot/grub/grub.conf");
785                 }
786         } */
787
788         /* Build cache lang file */
789         snprintf(commandstring, STRING_SIZE, "/sbin/chroot /harddisk /usr/bin/perl -e \"require '" CONFIG_ROOT "/lang.pl'; &Lang::BuildCacheLang\"");
790         if (runcommandwithstatus(commandstring, ctr[TR_INSTALLING_LANG_CACHE]))
791         {
792                 errorbox(ctr[TR_UNABLE_TO_INSTALL_LANG_CACHE]);
793                 goto EXIT;
794         }
795
796         if (raid_disk)
797                 sprintf(string, "root=%sp3", hdparams.devnode);
798         else
799                 sprintf(string, "root=%s3", hdparams.devnode);
800         replace( "/harddisk/boot/grub/grub.conf", "root=ROOT", string);
801         mysystem( "sed -i \"s|KVERSION|$(/bin/uname -r)|\" /harddisk/boot/grub/grub.conf" );
802
803         replace( "/harddisk/boot/grub/grubbatch", "DEVICE", hdparams.devnode);
804         /* restore permissions */
805         chmod("/harddisk/boot/grub/grubbatch", S_IXUSR | S_IRUSR | S_IXGRP | S_IRGRP | S_IXOTH | S_IROTH);
806
807         snprintf(commandstring, STRING_SIZE, 
808                  "/sbin/chroot /harddisk /boot/grub/grubbatch");
809         if (runcommandwithstatus(commandstring, ctr[TR_INSTALLING_GRUB])) {
810                 errorbox(ctr[TR_UNABLE_TO_INSTALL_GRUB]);
811                 goto EXIT;
812         }
813
814         /* Update /etc/fstab */
815         replace( "/harddisk/etc/fstab", "DEVICE", hdparams.devnode);
816
817         /* Install bootsplash */
818         mysystem("/bin/installbootsplash.sh");
819
820         mysystem("ln -s grub.conf /harddisk/boot/grub/menu.lst");
821         mysystem("umount /harddisk/proc");
822         mysystem("umount /harddisk/dev");
823
824         if (!unattended) {
825                 sprintf(message, ctr[TR_CONGRATULATIONS_LONG],
826                                 NAME, SNAME, SNAME, NAME, NAME, NAME);
827                 newtWinMessage(ctr[TR_CONGRATULATIONS], ctr[TR_OK], message);
828         }
829                  
830         allok = 1;
831
832 EXIT:
833         fprintf(flog, "Install program ended.\n");      
834         fflush(flog);
835         fclose(flog);
836
837         if (!(allok))
838                 newtWinMessage(title, ctr[TR_OK], ctr[TR_PRESS_OK_TO_REBOOT]);  
839         
840         newtFinished();
841         
842         freekeyvalues(ethernetkv);
843
844         if (allok && !allok_fastexit)
845         {
846                 /* /proc is needed by the module checker.  We have to mount it
847                  * so it can be seen by setup, which is run chrooted. */
848                 if (system("/bin/mount proc -t proc /harddisk/proc"))
849                         printf("Unable to mount proc in /harddisk.");
850                 else
851                 {
852                         if (system("/sbin/chroot /harddisk /usr/local/sbin/setup /dev/tty2 INSTALL"))
853                                 printf("Unable to run setup.\n");
854                         if (system("/bin/umount /harddisk/proc"))
855                                 printf("Unable to umount /harddisk/proc.\n");
856                 }
857         }
858
859         fcloseall();
860
861         system("/sbin/swapoff /harddisk/swapfile");
862         system("/bin/umount /harddisk/var");
863         system("/bin/umount /harddisk/boot");
864         system("/bin/umount /harddisk");
865           
866         system("/etc/halt");
867
868         return 0;
869 }