]> git.ipfire.org Git - ipfire-2.x.git/blob - src/install+setup/install/main.c
Allow to select the target disk and allow selecting multiple of them.
[ipfire-2.x.git] / src / install+setup / install / main.c
1
2 /* SmoothWall install program.
3 *
4 * This program is distributed under the terms of the GNU General Public
5 * Licence. See the file COPYING for details.
6 *
7 * (c) Lawrence Manning, 2001
8 * Contains main entry point, and misc functions.6
9 *
10 */
11
12 #define _GNU_SOURCE
13
14 #include <assert.h>
15 #include <errno.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <sys/mount.h>
19
20 #include "hw.h"
21 #include "install.h"
22
23 #define INST_FILECOUNT 21000
24 #define UNATTENDED_CONF "/cdrom/boot/unattended.conf"
25 #define LICENSE_FILE "/cdrom/COPYING"
26
27 #define EXT2 0
28 #define EXT3 1
29 #define EXT4 2
30 #define REISERFS 3
31
32 FILE *flog = NULL;
33 char *mylog;
34
35 char **ctr;
36
37 extern char url[STRING_SIZE];
38
39 struct nic nics[20] = { { "" , "" , "" } }; // only defined for compile
40 struct knic knics[20] = { { "" , "" , "" , "" } }; // only defined for compile
41
42 extern char *en_tr[];
43 extern char *es_tr[];
44 extern char *de_tr[];
45 extern char *fr_tr[];
46 extern char *nl_tr[];
47 extern char *pl_tr[];
48 extern char *ru_tr[];
49 extern char *tr_tr[];
50
51 static int newtChecklist(const char* title, const char* message,
52 unsigned int width, unsigned int height, unsigned int num_entries,
53 const char** entries, int* states) {
54 int ret;
55 const int list_height = 4;
56
57 char cbstates[num_entries];
58
59 for (unsigned int i = 0; i < num_entries; i++) {
60 cbstates[i] = states[i] ? '*' : ' ';
61 }
62
63 newtCenteredWindow(width, height, title);
64
65 newtComponent textbox = newtTextbox(1, 1, width - 2, height - 6 - list_height,
66 NEWT_FLAG_WRAP);
67 newtTextboxSetText(textbox, message);
68
69 int top = newtTextboxGetNumLines(textbox) + 2;
70
71 newtComponent form = newtForm(NULL, NULL, 0);
72
73 newtComponent sb = NULL;
74 if (list_height < num_entries) {
75 sb = newtVerticalScrollbar(
76 width - 4, top + 1, list_height,
77 NEWT_COLORSET_CHECKBOX, NEWT_COLORSET_ACTCHECKBOX);
78
79 newtFormAddComponent(form, sb);
80 }
81
82 newtComponent subform = newtForm(sb, NULL, 0);
83 newtFormSetBackground(subform, NEWT_COLORSET_CHECKBOX);
84
85 newtFormSetHeight(subform, list_height);
86 newtFormSetWidth(subform, width - 10);
87
88 for (unsigned int i = 0; i < num_entries; i++) {
89 newtComponent cb = newtCheckbox(4, top + i, entries[i], cbstates[i],
90 NULL, &cbstates[i]);
91
92 newtFormAddComponent(subform, cb);
93 }
94
95 newtFormAddComponents(form, textbox, subform, NULL);
96
97 newtComponent btn_okay = newtButton((width - 18) / 3, height - 4, ctr[TR_OK]);
98 newtComponent btn_cancel = newtButton((width - 18) / 3 * 2 + 9, height - 4, ctr[TR_CANCEL]);
99 newtFormAddComponents(form, btn_okay, btn_cancel, NULL);
100
101 newtComponent answer = newtRunForm(form);
102
103 if ((answer == NULL) || (answer == btn_cancel)) {
104 ret = -1;
105 } else {
106 ret = 0;
107
108 for (unsigned int i = 0; i < num_entries; i++) {
109 states[i] = (cbstates[i] != ' ');
110
111 if (states[i])
112 ret++;
113 }
114 }
115
116 newtFormDestroy(form);
117 newtPopWindow();
118
119 return ret;
120 }
121
122 static int newtWinOkCancel(const char* title, const char* message, int width, int height,
123 const char* btn_txt_ok, const char* btn_txt_cancel) {
124 int ret = 1;
125
126 newtCenteredWindow(width, height, title);
127
128 newtComponent form = newtForm(NULL, NULL, 0);
129
130 newtComponent textbox = newtTextbox(1, 1, width - 2, height - 6, NEWT_FLAG_WRAP);
131 newtTextboxSetText(textbox, message);
132 newtFormAddComponent(form, textbox);
133
134 newtComponent btn_ok = newtButton((width - 16) / 3, height - 4, btn_txt_ok);
135 newtComponent btn_cancel = newtButton((width - 16) / 3 * 2 + 9, height - 4,
136 btn_txt_cancel);
137
138 newtFormAddComponents(form, btn_ok, btn_cancel, NULL);
139
140 newtComponent answer = newtRunForm(form);
141
142 if (answer == btn_ok) {
143 ret = 0;
144 }
145
146 newtFormDestroy(form);
147 newtPopWindow();
148
149 return ret;
150 }
151
152 int main(int argc, char *argv[]) {
153 struct hw* hw = hw_init();
154
155 char discl_msg[40000] = "Disclaimer\n";
156
157 char *langnames[] = { "Deutsch", "English", "Français", "Español", "Nederlands", "Polski", "Русский", "Türkçe", NULL };
158 char *shortlangnames[] = { "de", "en", "fr", "es", "nl", "pl", "ru", "tr", NULL };
159 char **langtrs[] = { de_tr, en_tr, fr_tr, es_tr, nl_tr, pl_tr, ru_tr, tr_tr, NULL };
160 char* sourcedrive = NULL;
161 int rc = 0;
162 char commandstring[STRING_SIZE];
163 char mkfscommand[STRING_SIZE];
164 char *fstypes[] = { "ext2", "ext3", "ext4", "ReiserFS", NULL };
165 int fstype = EXT4;
166 int choice;
167 char shortlangname[10];
168 char message[STRING_SIZE];
169 char title[STRING_SIZE];
170 int allok = 0;
171 int allok_fastexit=0;
172 struct keyvalue *ethernetkv = initkeyvalues();
173 FILE *handle, *cmdfile, *copying;
174 char line[STRING_SIZE];
175 char string[STRING_SIZE];
176 long memory = 0, disk = 0;
177 long system_partition, boot_partition, root_partition, swap_file;
178
179 int unattended = 0;
180 int serialconsole = 0;
181 struct keyvalue *unattendedkv = initkeyvalues();
182 char restore_file[STRING_SIZE] = "";
183
184 setlocale (LC_ALL, "");
185 sethostname( SNAME , 10);
186
187 /* Log file/terminal stuff. */
188 if (argc >= 2)
189 {
190 if (!(flog = fopen(argv[1], "w+")))
191 return 0;
192 }
193 else
194 return 0;
195
196 mylog = argv[1];
197
198 fprintf(flog, "Install program started.\n");
199
200 newtInit();
201 newtCls();
202
203 newtDrawRootText(14, 0, NAME " " VERSION " - " SLOGAN );
204 sprintf (title, "%s %s - %s", NAME, VERSION, SLOGAN);
205
206 if (! (cmdfile = fopen("/proc/cmdline", "r")))
207 {
208 fprintf(flog, "Couldn't open commandline: /proc/cmdline\n");
209 } else {
210 fgets(line, STRING_SIZE, cmdfile);
211
212 // check if we have to make an unattended install
213 if (strstr (line, "unattended") != NULL) {
214 unattended = 1;
215 runcommandwithstatus("/bin/sleep 10", "WARNING: Unattended installation will start in 10 seconds...");
216 }
217 // check if we have to patch for serial console
218 if (strstr (line, "console=ttyS0") != NULL) {
219 serialconsole = 1;
220 }
221 }
222
223 // Load common modules
224 mysystem("/sbin/modprobe vfat"); // USB key
225
226 /* German is the default */
227 for (choice = 0; langnames[choice]; choice++)
228 {
229 if (strcmp(langnames[choice], "English") == 0)
230 break;
231 }
232 if (!langnames[choice])
233 goto EXIT;
234
235 if (!unattended) {
236 rc = newtWinMenu("Language selection", "Select the language you wish to use for the " NAME ".", 50, 5, 5, 8,
237 langnames, &choice, "Ok", NULL);
238 }
239
240 ctr = langtrs[choice];
241 strcpy(shortlangname, shortlangnames[choice]);
242
243 newtPushHelpLine(ctr[TR_HELPLINE]);
244
245 if (!unattended) {
246 sprintf(message, ctr[TR_WELCOME], NAME);
247 newtWinMessage(title, ctr[TR_OK], message);
248 }
249
250 /* Search for a source drive that holds the right
251 * version of the image we are going to install. */
252 sourcedrive = hw_find_source_medium(hw);
253
254 fprintf(flog, "Source drive: %s\n", sourcedrive);
255 if (!sourcedrive) {
256 newtWinMessage(title, ctr[TR_OK], ctr[TR_NO_LOCAL_SOURCE]);
257 runcommandwithstatus("/bin/downloadsource.sh", ctr[TR_DOWNLOADING_ISO]);
258 if ((handle = fopen("/tmp/source_device", "r")) == NULL) {
259 errorbox(ctr[TR_DOWNLOAD_ERROR]);
260 goto EXIT;
261 }
262
263 fgets(sourcedrive, 5, handle);
264 fclose(handle);
265 }
266
267 assert(sourcedrive);
268
269 int r = hw_mount(sourcedrive, SOURCE_MOUNT_PATH, MS_RDONLY);
270 if (r) {
271 fprintf(flog, "Could not mount %s to %s\n", sourcedrive, SOURCE_MOUNT_PATH);
272 fprintf(flog, strerror(errno));
273 exit(1);
274 }
275
276 /* load unattended configuration */
277 if (unattended) {
278 fprintf(flog, "unattended: Reading unattended.conf\n");
279
280 (void) readkeyvalues(unattendedkv, UNATTENDED_CONF);
281 findkey(unattendedkv, "RESTORE_FILE", restore_file);
282 }
283
284 if (!unattended) {
285 // Read the license file.
286 if (!(copying = fopen(LICENSE_FILE, "r"))) {
287 sprintf(discl_msg, "Could not open license file: %s\n", LICENSE_FILE);
288 fprintf(flog, discl_msg);
289 } else {
290 fread(discl_msg, 1, 40000, copying);
291 fclose(copying);
292
293 if (disclaimerbox(discl_msg)==0) {
294 errorbox(ctr[TR_LICENSE_NOT_ACCEPTED]);
295 goto EXIT;
296 }
297 }
298 }
299
300 int part_type = HW_PART_TYPE_NORMAL;
301
302 // Scan for disks to install on.
303 struct hw_disk** disks = hw_find_disks(hw);
304
305 struct hw_disk** selected_disks = NULL;
306 unsigned int num_selected_disks = 0;
307
308 // Check how many disks have been found and what
309 // we can do with them.
310 unsigned int num_disks = hw_count_disks(disks);
311
312 while (1) {
313 // no harddisks found
314 if (num_disks == 0) {
315 errorbox(ctr[TR_NO_HARDDISK]);
316 goto EXIT;
317
318 // exactly one disk has been found
319 } else if (num_disks == 1) {
320 selected_disks = hw_select_disks(disks, NULL);
321
322 // more than one usable disk has been found and
323 // the user needs to choose what to do with them
324 } else {
325 const char* disk_names[num_disks];
326 int disk_selection[num_disks];
327
328 for (unsigned int i = 0; i < num_disks; i++) {
329 disk_names[i] = &disks[i]->description;
330 disk_selection[i] = 0;
331 }
332
333 while (!selected_disks) {
334 rc = newtChecklist(ctr[TR_DISK_SELECTION], ctr[TR_DISK_SELECTION_MSG],
335 50, 20, num_disks, disk_names, disk_selection);
336
337 // Error
338 if (rc < 0) {
339 goto EXIT;
340
341 // Nothing has been selected
342 } else if (rc == 0) {
343 errorbox(ctr[TR_NO_DISK_SELECTED]);
344
345 } else {
346 selected_disks = hw_select_disks(disks, disk_selection);
347 }
348 }
349 }
350
351 num_selected_disks = hw_count_disks(selected_disks);
352
353 if (num_selected_disks == 1) {
354 snprintf(message, sizeof(message), ctr[TR_DISK_SETUP_DESC], (*selected_disks)->description);
355 rc = newtWinOkCancel(ctr[TR_DISK_SETUP], message, 50, 10,
356 ctr[TR_DELETE_ALL_DATA], ctr[TR_CANCEL]);
357
358 if (rc == 0)
359 break;
360
361 } else if (num_selected_disks == 2) {
362 snprintf(message, sizeof(message), ctr[TR_RAID_SETUP_DESC],
363 (*selected_disks)->description, (*selected_disks + 1)->description);
364 rc = newtWinOkCancel(ctr[TR_RAID_SETUP], message, 50, 10,
365 ctr[TR_DELETE_ALL_DATA], ctr[TR_CANCEL]);
366
367 if (rc == 0) {
368 part_type = HW_PART_TYPE_RAID1;
369
370 break;
371 }
372
373 // Currently not supported
374 } else {
375 errorbox(ctr[TR_DISK_CONFIGURATION_NOT_SUPPORTED]);
376 }
377
378 if (selected_disks) {
379 hw_free_disks(selected_disks);
380 selected_disks = NULL;
381 }
382 }
383
384 hw_free_disks(disks);
385
386 struct hw_destination* destination = hw_make_destination(part_type, selected_disks);
387 assert(destination);
388
389 fprintf(flog, "Destination drive: %s\n", destination->path);
390 fprintf(flog, " boot: %s\n", destination->part_boot);
391 fprintf(flog, " swap: %s\n", destination->part_swap);
392 fprintf(flog, " root: %s\n", destination->part_root);
393 fprintf(flog, " data: %s\n", destination->part_data);
394
395 fstypes[0]=ctr[TR_EXT2FS_DESCR];
396 fstypes[1]=ctr[TR_EXT3FS_DESCR];
397 fstypes[2]=ctr[TR_EXT4FS_DESCR];
398 fstypes[3]=ctr[TR_REISERFS_DESCR];
399 fstypes[4]=NULL;
400
401 if (!unattended) {
402 sprintf(message, ctr[TR_CHOOSE_FILESYSTEM]);
403 rc = newtWinMenu( ctr[TR_CHOOSE_FILESYSTEM], message,
404 50, 5, 5, 6, fstypes, &fstype, ctr[TR_OK],
405 ctr[TR_CANCEL], NULL);
406 } else {
407 rc = 1;
408 fstype = EXT4;
409 }
410 if (rc == 2)
411 goto EXIT;
412
413 /* Calculate amount of memory in machine */
414 if ((handle = fopen("/proc/meminfo", "r")))
415 {
416 while (fgets(line, STRING_SIZE-1, handle)) {
417 if (sscanf (line, "MemTotal: %s kB", string)) {
418 memory = atoi(string) / 1024 ;
419 }
420 }
421 fclose(handle);
422 }
423
424 /* Partition, mkswp, mkfs.
425 * before partitioning, first determine the sizes of each
426 * partition. In order to do that we need to know the size of
427 * the disk.
428 */
429 disk = destination->size / 1024 / 1024;
430
431 fprintf(flog, "Disksize = %ld, memory = %ld", disk, memory);
432
433 /* Calculating Swap-Size dependend of Ram Size */
434 if (memory <= 256)
435 swap_file = 128;
436 else if (memory <= 1024 && memory > 256)
437 swap_file = 256;
438 else
439 swap_file = memory / 4;
440
441 /* Calculating Root-Size dependend of Max Disk Space */
442 if ( disk < 2048 )
443 root_partition = 1024;
444 else if ( disk >= 2048 && disk <= 3072 )
445 root_partition = 1536;
446 else
447 root_partition = 2048;
448
449
450 /* Calculating the amount of free space */
451 boot_partition = 64; /* in MB */
452 system_partition = disk - ( root_partition + swap_file + boot_partition );
453
454 fprintf(flog, ", boot = %ld, swap = %ld, mylog = %ld, root = %ld\n",
455 boot_partition, swap_file, system_partition, root_partition);
456 rc = 0;
457
458 if ( (!unattended) && (((disk - (root_partition + swap_file + boot_partition)) < 256 ) && ((disk - (root_partition + boot_partition )) > 256)) ) {
459 rc = newtWinChoice(title, ctr[TR_OK], ctr[TR_CANCEL], ctr[TR_CONTINUE_NO_SWAP]);
460 if (rc == 1){
461 swap_file = 0;
462 system_partition = disk - ( root_partition + swap_file + boot_partition );
463 fprintf(flog, "Changing Swap Size to 0 MB.\n");
464 }
465 else if (rc == 2){
466 fprintf(flog, "Disk is too small.\n");
467 errorbox(ctr[TR_DISK_TOO_SMALL]);goto EXIT;
468 }
469 }
470 else if (disk - (root_partition + swap_file + boot_partition) >= 256) {
471
472 }
473 else {
474 fprintf(flog, "Disk is too small.\n");
475 errorbox(ctr[TR_DISK_TOO_SMALL]);goto EXIT;
476 }
477
478 handle = fopen("/tmp/partitiontable", "w");
479
480 /* Make swapfile */
481 if (swap_file) {
482 fprintf(handle, ",%ld,L,*\n,%ld,S,\n,%ld,L,\n,,L,\n",
483 boot_partition, swap_file, root_partition);
484 } else {
485 fprintf(handle, ",%ld,L,*\n,0,0,\n,%ld,L,\n,,L,\n",
486 boot_partition, root_partition);
487 }
488
489 fclose(handle);
490
491 if (disk < 2097150) {
492 // <2TB use sfdisk and normal mbr
493 snprintf(commandstring, STRING_SIZE, "/sbin/sfdisk -L -uM %s < /tmp/partitiontable", destination->path);
494 } else {
495 // >2TB use parted with gpt
496 snprintf(commandstring, STRING_SIZE, "/usr/sbin/parted -s %s mklabel gpt mkpart boot ext2 1M 64M mkpart swap linux-swap 64M 1000M mkpart root ext4 1000M 5000M mkpart var ext4 5000M 100%% disk_set pmbr_boot on", destination->path);
497 }
498
499 if (runcommandwithstatus(commandstring, ctr[TR_PARTITIONING_DISK]))
500 {
501 errorbox(ctr[TR_UNABLE_TO_PARTITION]);
502 goto EXIT;
503 }
504
505 if (fstype == EXT2) {
506 // mysystem("/sbin/modprobe ext2");
507 sprintf(mkfscommand, "/sbin/mke2fs -T ext2");
508 } else if (fstype == REISERFS) {
509 mysystem("/sbin/modprobe reiserfs");
510 sprintf(mkfscommand, "/sbin/mkreiserfs -f");
511 } else if (fstype == EXT3) {
512 // mysystem("/sbin/modprobe ext3");
513 sprintf(mkfscommand, "/sbin/mke2fs -T ext3");
514 } else if (fstype == EXT4) {
515 // mysystem("/sbin/modprobe ext4");
516 sprintf(mkfscommand, "/sbin/mke2fs -T ext4");
517 }
518
519 snprintf(commandstring, STRING_SIZE, "/sbin/mke2fs -T ext2 -I 128 %s", destination->part_boot);
520 if (runcommandwithstatus(commandstring, ctr[TR_MAKING_BOOT_FILESYSTEM]))
521 {
522 errorbox(ctr[TR_UNABLE_TO_MAKE_BOOT_FILESYSTEM]);
523 goto EXIT;
524 }
525
526 if (swap_file) {
527 snprintf(commandstring, STRING_SIZE, "/sbin/mkswap %s", destination->part_swap);
528 if (runcommandwithstatus(commandstring, ctr[TR_MAKING_SWAPSPACE]))
529 {
530 errorbox(ctr[TR_UNABLE_TO_MAKE_SWAPSPACE]);
531 goto EXIT;
532 }
533 }
534
535 snprintf(commandstring, STRING_SIZE, "%s %s", mkfscommand, destination->part_root);
536 if (runcommandwithstatus(commandstring, ctr[TR_MAKING_ROOT_FILESYSTEM]))
537 {
538 errorbox(ctr[TR_UNABLE_TO_MAKE_ROOT_FILESYSTEM]);
539 goto EXIT;
540 }
541
542 snprintf(commandstring, STRING_SIZE, "%s %s", mkfscommand, destination->part_data);
543 if (runcommandwithstatus(commandstring, ctr[TR_MAKING_LOG_FILESYSTEM]))
544 {
545 errorbox(ctr[TR_UNABLE_TO_MAKE_LOG_FILESYSTEM]);
546 goto EXIT;
547 }
548
549 snprintf(commandstring, STRING_SIZE, "/bin/mount %s /harddisk", destination->part_root);
550 if (runcommandwithstatus(commandstring, ctr[TR_MOUNTING_ROOT_FILESYSTEM]))
551 {
552 errorbox(ctr[TR_UNABLE_TO_MOUNT_ROOT_FILESYSTEM]);
553 goto EXIT;
554 }
555
556 mkdir("/harddisk/boot", S_IRWXU|S_IRWXG|S_IRWXO);
557 mkdir("/harddisk/var", S_IRWXU|S_IRWXG|S_IRWXO);
558 mkdir("/harddisk/var/log", S_IRWXU|S_IRWXG|S_IRWXO);
559
560 snprintf(commandstring, STRING_SIZE, "/bin/mount %s /harddisk/boot", destination->part_boot);
561 if (runcommandwithstatus(commandstring, ctr[TR_MOUNTING_BOOT_FILESYSTEM]))
562 {
563 errorbox(ctr[TR_UNABLE_TO_MOUNT_BOOT_FILESYSTEM]);
564 goto EXIT;
565 }
566 if (swap_file) {
567 snprintf(commandstring, STRING_SIZE, "/sbin/swapon %s", destination->part_swap);
568 if (runcommandwithstatus(commandstring, ctr[TR_MOUNTING_SWAP_PARTITION]))
569 {
570 errorbox(ctr[TR_UNABLE_TO_MOUNT_SWAP_PARTITION]);
571 goto EXIT;
572 }
573 }
574 snprintf(commandstring, STRING_SIZE, "/bin/mount %s /harddisk/var", destination->part_data);
575 if (runcommandwithstatus(commandstring, ctr[TR_MOUNTING_LOG_FILESYSTEM]))
576 {
577 errorbox(ctr[TR_UNABLE_TO_MOUNT_LOG_FILESYSTEM]);
578 goto EXIT;
579 }
580
581 snprintf(commandstring, STRING_SIZE,
582 "/bin/tar -C /harddisk -xvf /cdrom/" SNAME "-" VERSION ".tlz --lzma 2>/dev/null");
583
584 if (runcommandwithprogress(60, 4, title, commandstring, INST_FILECOUNT,
585 ctr[TR_INSTALLING_FILES]))
586 {
587 errorbox(ctr[TR_UNABLE_TO_INSTALL_FILES]);
588 goto EXIT;
589 }
590
591 /* Save language und local settings */
592 write_lang_configs(shortlangname);
593
594 /* mount proc filesystem */
595 mysystem("mkdir /harddisk/proc");
596 mysystem("/bin/mount --bind /proc /harddisk/proc");
597 mysystem("/bin/mount --bind /dev /harddisk/dev");
598 mysystem("/bin/mount --bind /sys /harddisk/sys");
599
600 /* Build cache lang file */
601 snprintf(commandstring, STRING_SIZE, "/usr/sbin/chroot /harddisk /usr/bin/perl -e \"require '" CONFIG_ROOT "/lang.pl'; &Lang::BuildCacheLang\"");
602 if (runcommandwithstatus(commandstring, ctr[TR_INSTALLING_LANG_CACHE]))
603 {
604 errorbox(ctr[TR_UNABLE_TO_INSTALL_LANG_CACHE]);
605 goto EXIT;
606 }
607
608 /* Update /etc/fstab */
609 snprintf(commandstring, STRING_SIZE, "/bin/sed -i -e \"s#DEVICE1#UUID=$(/sbin/blkid %s -sUUID | /usr/bin/cut -d'\"' -f2)#g\" /harddisk/etc/fstab", destination->part_boot);
610 system(commandstring);
611 snprintf(commandstring, STRING_SIZE, "/bin/sed -i -e \"s#DEVICE2#UUID=$(/sbin/blkid %s -sUUID | /usr/bin/cut -d'\"' -f2)#g\" /harddisk/etc/fstab", destination->part_swap);
612 system(commandstring);
613 snprintf(commandstring, STRING_SIZE, "/bin/sed -i -e \"s#DEVICE3#UUID=$(/sbin/blkid %s -sUUID | /usr/bin/cut -d'\"' -f2)#g\" /harddisk/etc/fstab", destination->part_root);
614 system(commandstring);
615 snprintf(commandstring, STRING_SIZE, "/bin/sed -i -e \"s#DEVICE4#UUID=$(/sbin/blkid %s -sUUID | /usr/bin/cut -d'\"' -f2)#g\" /harddisk/etc/fstab", destination->part_data);
616 system(commandstring);
617
618 if (fstype == EXT2) {
619 replace("/harddisk/etc/fstab", "FSTYPE", "ext2");
620 replace("/harddisk/boot/grub/grub.conf", "MOUNT", "ro");
621 } else if (fstype == REISERFS) {
622 replace("/harddisk/etc/fstab", "FSTYPE", "reiserfs");
623 replace("/harddisk/boot/grub/grub.conf", "MOUNT", "ro");
624 } else if (fstype == EXT3) {
625 replace("/harddisk/etc/fstab", "FSTYPE", "ext3");
626 replace("/harddisk/boot/grub/grub.conf", "MOUNT", "ro");
627 } else if (fstype == EXT4) {
628 replace("/harddisk/etc/fstab", "FSTYPE", "ext4");
629 replace("/harddisk/boot/grub/grub.conf", "MOUNT", "ro");
630 }
631
632 replace("/harddisk/boot/grub/grub.conf", "KVER", KERNEL_VERSION);
633
634 snprintf(commandstring, STRING_SIZE, "/bin/sed -i -e \"s#root=ROOT#root=UUID=$(/sbin/blkid %s -sUUID | /usr/bin/cut -d'\"' -f2)#g\" /harddisk/boot/grub/grub.conf", destination->part_root);
635 system(commandstring);
636
637 mysystem("ln -s grub.conf /harddisk/boot/grub/menu.lst");
638
639 system("/bin/sed -e 's#/harddisk#/#g' -e 's#//#/#g' < /proc/mounts > /harddisk/etc/mtab");
640
641 /*
642 * Generate device.map to help grub finding the device to install itself on.
643 */
644 FILE *f = NULL;
645 if (f = fopen("/harddisk/boot/grub/device.map", "w")) {
646 fprintf(f, "(hd0) %s\n", destination->path);
647 fclose(f);
648 }
649
650 snprintf(commandstring, STRING_SIZE,
651 "/usr/sbin/chroot /harddisk /usr/sbin/grub-install --no-floppy %s", destination->path);
652 if (runcommandwithstatus(commandstring, ctr[TR_INSTALLING_GRUB])) {
653 errorbox(ctr[TR_UNABLE_TO_INSTALL_GRUB]);
654 goto EXIT;
655 }
656
657 /* Serial console ? */
658 if (serialconsole) {
659 /* grub */
660 replace("/harddisk/boot/grub/grub.conf", "splashimage", "#splashimage");
661 replace("/harddisk/boot/grub/grub.conf", "#serial", "serial");
662 replace("/harddisk/boot/grub/grub.conf", "#terminal", "terminal");
663 replace("/harddisk/boot/grub/grub.conf", " panic=10 ", " console=ttyS0,115200n8 panic=10 ");
664
665 /*inittab*/
666 replace("/harddisk/etc/inittab", "1:2345:respawn:", "#1:2345:respawn:");
667 replace("/harddisk/etc/inittab", "2:2345:respawn:", "#2:2345:respawn:");
668 replace("/harddisk/etc/inittab", "3:2345:respawn:", "#3:2345:respawn:");
669 replace("/harddisk/etc/inittab", "4:2345:respawn:", "#4:2345:respawn:");
670 replace("/harddisk/etc/inittab", "5:2345:respawn:", "#5:2345:respawn:");
671 replace("/harddisk/etc/inittab", "6:2345:respawn:", "#6:2345:respawn:");
672 replace("/harddisk/etc/inittab", "#7:2345:respawn:", "7:2345:respawn:");
673 }
674
675 /* Set marker that the user has already accepted the gpl */
676 mysystem("/usr/bin/touch /harddisk/var/ipfire/main/gpl_accepted");
677
678 /* Copy restore file from cdrom */
679 if (unattended && (strlen(restore_file) > 0)) {
680 fprintf(flog, "unattended: Copy restore file\n");
681 snprintf(commandstring, STRING_SIZE,
682 "cp /cdrom/%s /harddisk/var/ipfire/backup", restore_file);
683 mysystem(commandstring);
684 }
685
686 mysystem("umount /cdrom");
687 snprintf(commandstring, STRING_SIZE, "/usr/bin/eject /dev/%s", sourcedrive);
688 mysystem(commandstring);
689
690 if (!unattended) {
691 sprintf(message, ctr[TR_CONGRATULATIONS_LONG],
692 NAME, SNAME, NAME);
693 newtWinMessage(ctr[TR_CONGRATULATIONS], ctr[TR_PRESS_OK_TO_REBOOT], message);
694 }
695
696 allok = 1;
697
698 EXIT:
699 fprintf(flog, "Install program ended.\n");
700
701 if (!(allok))
702 newtWinMessage(title, ctr[TR_OK], ctr[TR_PRESS_OK_TO_REBOOT]);
703
704 freekeyvalues(ethernetkv);
705
706 if (allok && !allok_fastexit)
707 {
708 if (unattended) {
709 fprintf(flog, "Entering unattended setup\n");
710 if (unattended_setup(unattendedkv)) {
711 snprintf(commandstring, STRING_SIZE, "/bin/sleep 10");
712 runcommandwithstatus(commandstring, "Unattended installation finished, system will reboot");
713 } else {
714 errorbox("Unattended setup failed.");
715 goto EXIT;
716 }
717 }
718
719 fflush(flog);
720 fclose(flog);
721 newtFinished();
722
723 if (system("/bin/umount /harddisk/proc"))
724 printf("Unable to umount /harddisk/proc.\n");
725 } else {
726 fflush(flog);
727 fclose(flog);
728 newtFinished();
729 }
730
731 fcloseall();
732
733 if (swap_file) {
734 snprintf(commandstring, STRING_SIZE, "/bin/swapoff %s", destination->part_swap);
735 }
736
737 newtFinished();
738
739 system("/bin/umount /harddisk/proc >/dev/null 2>&1");
740 system("/bin/umount /harddisk/dev >/dev/null 2>&1");
741 system("/bin/umount /harddisk/sys >/dev/null 2>&1");
742
743 system("/bin/umount /harddisk/var >/dev/null 2>&1");
744 system("/bin/umount /harddisk/boot >/dev/null 2>&1");
745 system("/bin/umount /harddisk >/dev/null 2>&1");
746
747 if (!(allok))
748 system("/etc/halt");
749
750 // Free resources
751 free(sourcedrive);
752 free(destination);
753
754 if (selected_disks)
755 hw_free_disks(selected_disks);
756
757 hw_free(hw);
758
759 return 0;
760 }