]> git.ipfire.org Git - ipfire-2.x.git/blob - src/installer/main.c
mympd: remove create config start
[ipfire-2.x.git] / src / installer / 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.6
8 *
9 */
10 #define _GNU_SOURCE
11
12 #include <assert.h>
13 #include <errno.h>
14 #include <libsmooth.h>
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <sys/mount.h>
19
20 #include "hw.h"
21
22 // Translation
23 #include <libintl.h>
24 #define _(x) dgettext("installer", x)
25
26 #define INST_FILECOUNT 30000
27 #define LICENSE_FILE "/cdrom/COPYING"
28 #define SOURCE_TEMPFILE "/tmp/downloads/image.iso"
29
30 extern char url[STRING_SIZE];
31
32 FILE* flog = NULL;
33
34 static int newtChecklist(const char* title, const char* message,
35 unsigned int width, unsigned int height, unsigned int num_entries,
36 const char** entries, int* states) {
37 int ret;
38 const int list_height = 4;
39
40 char cbstates[num_entries];
41
42 for (unsigned int i = 0; i < num_entries; i++) {
43 cbstates[i] = states[i] ? '*' : ' ';
44 }
45
46 newtCenteredWindow(width, height, title);
47
48 newtComponent textbox = newtTextbox(1, 1, width - 2, height - 6 - list_height,
49 NEWT_FLAG_WRAP);
50 newtTextboxSetText(textbox, message);
51
52 int top = newtTextboxGetNumLines(textbox) + 2;
53
54 newtComponent form = newtForm(NULL, NULL, 0);
55
56 newtComponent sb = NULL;
57 if (list_height < num_entries) {
58 sb = newtVerticalScrollbar(
59 width - 4, top + 1, list_height,
60 NEWT_COLORSET_CHECKBOX, NEWT_COLORSET_ACTCHECKBOX);
61
62 newtFormAddComponent(form, sb);
63 }
64
65 newtComponent subform = newtForm(sb, NULL, 0);
66 newtFormSetBackground(subform, NEWT_COLORSET_CHECKBOX);
67
68 newtFormSetHeight(subform, list_height);
69 newtFormSetWidth(subform, width - 10);
70
71 for (unsigned int i = 0; i < num_entries; i++) {
72 newtComponent cb = newtCheckbox(4, top + i, entries[i], cbstates[i],
73 NULL, &cbstates[i]);
74
75 newtFormAddComponent(subform, cb);
76 }
77
78 newtFormAddComponents(form, textbox, subform, NULL);
79
80 newtComponent btn_okay = newtButton((width - 18) / 3, height - 4, _("OK"));
81 newtComponent btn_cancel = newtButton((width - 18) / 3 * 2 + 9, height - 4, _("Cancel"));
82 newtFormAddComponents(form, btn_okay, btn_cancel, NULL);
83
84 newtComponent answer = newtRunForm(form);
85
86 if ((answer == NULL) || (answer == btn_cancel)) {
87 ret = -1;
88 } else {
89 ret = 0;
90
91 for (unsigned int i = 0; i < num_entries; i++) {
92 states[i] = (cbstates[i] != ' ');
93
94 if (states[i])
95 ret++;
96 }
97 }
98
99 newtFormDestroy(form);
100 newtPopWindow();
101
102 return ret;
103 }
104
105 static int newtWinOkCancel(const char* title, const char* message, int width, int height,
106 const char* btn_txt_ok, const char* btn_txt_cancel) {
107 int ret = 1;
108
109 unsigned int btn_width_ok = strlen(btn_txt_ok);
110 unsigned int btn_width_cancel = strlen(btn_txt_cancel);
111
112 // Maybe make the box wider to fix both buttons inside
113 unsigned int min_width = btn_width_ok + btn_width_cancel + 5;
114 if (width < min_width)
115 width = min_width;
116
117 unsigned int btn_pos_ok = (width / 3) - (btn_width_ok / 2) - 1;
118 unsigned int btn_pos_cancel = (width * 2 / 3) - (btn_width_cancel / 2) - 1;
119
120 // Move buttons a bit if they overlap
121 while ((btn_pos_ok + btn_width_ok + 5) > btn_pos_cancel) {
122 // Move the cancel button to the right if there is enough space left
123 if ((btn_pos_cancel + btn_width_cancel + 2) < width) {
124 ++btn_pos_cancel;
125 continue;
126 }
127
128 // Move the OK button to the left if possible
129 if (btn_pos_ok > 1) {
130 --btn_pos_ok;
131 continue;
132 }
133
134 // If they still overlap, we cannot fix the situtation
135 // and break. Should actually never get here, because we
136 // adjust the width of the window earlier.
137 break;
138 }
139
140 newtCenteredWindow(width, height, title);
141
142 newtComponent form = newtForm(NULL, NULL, 0);
143
144 newtComponent textbox = newtTextbox(1, 1, width - 2, height - 6, NEWT_FLAG_WRAP);
145 newtTextboxSetText(textbox, message);
146 newtFormAddComponent(form, textbox);
147
148 newtComponent btn_ok = newtButton(btn_pos_ok, height - 4, btn_txt_ok);
149 newtComponent btn_cancel = newtButton(btn_pos_cancel, height - 4, btn_txt_cancel);
150
151 newtFormAddComponents(form, btn_ok, btn_cancel, NULL);
152
153 newtComponent answer = newtRunForm(form);
154
155 if (answer == btn_ok) {
156 ret = 0;
157 }
158
159 newtFormDestroy(form);
160 newtPopWindow();
161
162 return ret;
163 }
164
165 static int newtLicenseBox(const char* title, const char* text, int width, int height) {
166 int ret = 1;
167
168 newtCenteredWindow(width, height, title);
169
170 newtComponent form = newtForm(NULL, NULL, 0);
171
172 newtComponent textbox = newtTextbox(1, 1, width - 2, height - 7,
173 NEWT_FLAG_WRAP|NEWT_FLAG_SCROLL);
174 newtTextboxSetText(textbox, text);
175 newtFormAddComponent(form, textbox);
176
177 char choice;
178 newtComponent checkbox = newtCheckbox(3, height - 3, _("I accept this license"),
179 ' ', " *", &choice);
180
181 newtComponent btn = newtButton(width - 15, height - 4, _("OK"));
182
183 newtFormAddComponents(form, checkbox, btn, NULL);
184
185 newtComponent answer = newtRunForm(form);
186 if (answer == btn && choice == '*')
187 ret = 0;
188
189 newtFormDestroy(form);
190 newtPopWindow();
191
192 return ret;
193 }
194
195 int write_lang_configs(const char* lang) {
196 struct keyvalue *kv = initkeyvalues();
197
198 /* default stuff for main/settings. */
199 replacekeyvalue(kv, "LANGUAGE", (char*)lang);
200 replacekeyvalue(kv, "HOSTNAME", DISTRO_SNAME);
201 replacekeyvalue(kv, "THEME", "ipfire");
202 writekeyvalues(kv, "/harddisk" CONFIG_ROOT "/main/settings");
203 freekeyvalues(kv);
204
205 return 1;
206 }
207
208 static char* get_system_release() {
209 char system_release[STRING_SIZE] = "\0";
210
211 FILE* f = fopen("/etc/system-release", "r");
212 if (f) {
213 fgets(system_release, sizeof(system_release), f);
214 fclose(f);
215 }
216
217 return strdup(system_release);
218 }
219
220 static char* center_string(const char* str, int width) {
221 if (!str)
222 return NULL;
223
224 char* string = NULL;
225 unsigned int str_len = strlen(str);
226
227 if (str_len == width) {
228 string = strdup(str);
229
230 } else if (str_len > width) {
231 string = strdup(str);
232 string[width - 1] = '\0';
233
234 } else {
235 unsigned int indent_length = (width - str_len) / 2;
236 char indent[indent_length + 1];
237
238 for (unsigned int i = 0; i < indent_length; i++) {
239 indent[i] = ' ';
240 }
241 indent[indent_length] = '\0';
242
243 if (asprintf(&string, "%s%s", indent, str) < 0)
244 return NULL;
245 }
246
247 return string;
248 }
249
250 #define DEFAULT_LANG "en.utf8"
251 #define NUM_LANGS 13
252
253 static struct lang {
254 const char* code;
255 char* name;
256 } languages[NUM_LANGS + 1] = {
257 { "fa.utf8", "فارسی (Persian)" },
258 { "da.utf8", "Dansk (Danish)" },
259 { "es.utf8", "Español (Spanish)" },
260 { "en.utf8", "English" },
261 { "fr.utf8", "Français (French)" },
262 { "hr.utf8", "Hrvatski (Croatian)" },
263 { "it.utf8", "Italiano (Italian)" },
264 { "de.utf8", "Deutsch (German)" },
265 { "nl.utf8", "Nederlands (Dutch)" },
266 { "pl.utf8", "Polski (Polish)" },
267 { "pt.utf8", "Portuguese (Brasil)" },
268 { "ru.utf8", "Русский (Russian)" },
269 { "tr.utf8", "Türkçe (Turkish)" },
270 { NULL, NULL },
271 };
272
273 static struct config {
274 int unattended;
275 int serial_console;
276 int novga;
277 int require_networking;
278 int perform_download;
279 int disable_swap;
280 char download_url[STRING_SIZE];
281 char postinstall[STRING_SIZE];
282 const char* language;
283 } config = {
284 .unattended = 0,
285 .serial_console = 0,
286 .novga = 0,
287 .require_networking = 0,
288 .perform_download = 0,
289 .disable_swap = 0,
290 .download_url = DOWNLOAD_URL,
291 .postinstall = "\0",
292 .language = DEFAULT_LANG,
293 };
294
295 static void parse_command_line(FILE* flog, struct config* c) {
296 char buffer[STRING_SIZE];
297 char cmdline[STRING_SIZE];
298
299 FILE* f = fopen("/proc/cmdline", "r");
300 if (!f) {
301 fprintf(flog, "Could not open /proc/cmdline: %m");
302 return;
303 }
304
305 int r = fread(&cmdline, 1, sizeof(cmdline) - 1, f);
306 if (r > 0) {
307 // Remove the trailing newline
308 if (cmdline[r-1] == '\n')
309 cmdline[r-1] = '\0';
310
311 char* token = strtok(cmdline, " ");
312 while (token) {
313 snprintf(buffer, sizeof(buffer), "%s", token);
314
315 char* val = buffer;
316 char* key = strsep(&val, "=");
317
318 // serial console
319 if ((strcmp(key, "console") == 0) && (strncmp(val, "ttyS", 4) == 0))
320 c->serial_console = 1;
321
322 // novga
323 else if (strcmp(key, "novga") == 0)
324 c->novga = 1;
325
326 // enable networking?
327 else if (strcmp(token, "installer.net") == 0)
328 c->require_networking = 1;
329
330 // unattended mode
331 else if (strcmp(token, "installer.unattended") == 0)
332 c->unattended = 1;
333
334 // disable swap
335 else if (strcmp(token, "installer.disable-swap") == 0)
336 c->disable_swap = 1;
337
338 // download url
339 else if (strcmp(key, "installer.download-url") == 0) {
340 snprintf(c->download_url, sizeof(c->download_url), "%s", val);
341 c->perform_download = 1;
342
343 // Require networking for the download
344 c->require_networking = 1;
345
346 // postinstall script
347 } else if (strcmp(key, "installer.postinstall") == 0) {
348 snprintf(c->postinstall, sizeof(c->postinstall), "%s", val);
349
350 // Require networking for the download
351 c->require_networking = 1;
352 }
353
354 token = strtok(NULL, " ");
355 }
356 }
357
358 fclose(f);
359 }
360
361 int main(int argc, char *argv[]) {
362 struct hw* hw = hw_init();
363 const char* logfile = NULL;
364
365 // Read /etc/system-release
366 char* system_release = get_system_release();
367
368 char discl_msg[40000] = "Disclaimer\n";
369
370 char* sourcedrive = NULL;
371 struct hw_destination* destination = NULL;
372 struct hw_disk** selected_disks = NULL;
373 int rc = 0;
374 char commandstring[STRING_SIZE];
375 int choice;
376 char message[STRING_SIZE];
377 char title[STRING_SIZE];
378 int allok = 0;
379 FILE *copying;
380
381 setlocale(LC_ALL, "");
382 sethostname(DISTRO_SNAME, strlen(DISTRO_SNAME));
383
384 /* Log file/terminal stuff. */
385 if (argc >= 2) {
386 logfile = argv[1];
387
388 if (!(flog = fopen(logfile, "w+")))
389 return 0;
390 } else {
391 return 0;
392 }
393
394 fprintf(flog, "Install program started.\n");
395 if (hw->efi)
396 fprintf(flog, "EFI mode enabled\n");
397
398 newtInit();
399 newtCls();
400
401 // Determine the size of the screen
402 int screen_cols = 0;
403 int screen_rows = 0;
404
405 newtGetScreenSize(&screen_cols, &screen_rows);
406
407 // Draw title
408 char* roottext = center_string(system_release, screen_cols);
409 if (roottext)
410 newtDrawRootText(0, 0, roottext);
411
412 snprintf(title, sizeof(title), "%s - %s", DISTRO_NAME, DISTRO_SLOGAN);
413
414 // Parse parameters from the kernel command line
415 parse_command_line(flog, &config);
416
417 if (config.unattended) {
418 splashWindow(title, _("Warning: Unattended installation will start in 10 seconds..."), 10);
419 }
420
421 // Load common modules
422 mysystem(logfile, "/sbin/modprobe vfat"); // USB key
423 mysystem(logfile, "/sbin/modprobe ntfs3"); // USB key
424 hw_stop_all_raid_arrays(logfile);
425
426 if (!config.unattended) {
427 // Language selection
428 char* langnames[NUM_LANGS + 1];
429
430 for (unsigned int i = 0; i < NUM_LANGS; i++) {
431 if (strcmp(languages[i].code, DEFAULT_LANG) == 0)
432 choice = i;
433
434 langnames[i] = languages[i].name;
435 }
436 langnames[NUM_LANGS] = NULL;
437
438 rc = newtWinMenu(_("Language selection"), _("Select the language you wish to use for the installation."),
439 50, 5, 5, 8, langnames, &choice, _("OK"), NULL);
440
441 assert(choice <= NUM_LANGS);
442
443 fprintf(flog, "Selected language: %s (%s)\n", languages[choice].name, languages[choice].code);
444 config.language = languages[choice].code;
445
446 setlocale(LC_ALL, config.language);
447 setenv("LANGUAGE", config.language, 1);
448 }
449
450 // Set helpline
451 char* helpline = NULL;
452 if (config.unattended)
453 helpline = center_string(_("Unattended mode"), screen_cols);
454 else
455 helpline = center_string(_("<Tab>/<Alt-Tab> between elements | <Space> selects | <F12> next screen"), screen_cols);
456
457 if (helpline)
458 newtPushHelpLine(helpline);
459
460 if (!config.unattended) {
461 snprintf(message, sizeof(message),
462 _("Welcome to the %s installation program.\n\n"
463 "Selecting Cancel on any of the following screens will reboot the computer."), DISTRO_NAME);
464 newtWinMessage(title, _("Start installation"), message);
465 }
466
467 /* Search for a source drive that holds the right
468 * version of the image we are going to install. */
469 if (!config.perform_download) {
470 sourcedrive = hw_find_source_medium(hw);
471 fprintf(flog, "Source drive: %s\n", sourcedrive);
472 }
473
474 /* If we could not find a source drive, we will try
475 * downloading the install image */
476 if (!sourcedrive)
477 config.perform_download = 1;
478
479 if (config.perform_download) {
480 if (!config.unattended) {
481 // Show the right message to the user
482 char reason[STRING_SIZE];
483 if (config.perform_download) {
484 snprintf(reason, sizeof(reason),
485 _("The installer will now try downloading the installation image."));
486 } else {
487 snprintf(reason, sizeof(reason),
488 _("No source drive could be found.\n\n"
489 "You can try downloading the required installation image."));
490 }
491 snprintf(message, sizeof(message), "%s %s", reason,
492 _("Please make sure to connect your machine to a network and "
493 "the installer will try connect to acquire an IP address."));
494
495 rc = newtWinOkCancel(title, message, 55, 12,
496 _("Download installation image"), _("Cancel"));
497
498 if (rc != 0)
499 goto EXIT;
500 }
501
502 // Make sure that we enable networking before download
503 config.require_networking = 1;
504 }
505
506 // Try starting the networking if we require it
507 if (config.require_networking) {
508 while (1) {
509 statuswindow(60, 4, title, _("Trying to start networking (DHCP)..."));
510
511 rc = hw_start_networking(logfile);
512 newtPopWindow();
513
514 // Networking was successfully started
515 if (rc == 0) {
516 break;
517
518 // An error happened, ask the user what to do
519 } else {
520 rc = newtWinOkCancel(title, _("Networking could not be started "
521 "but is required to go on with the installation.\n\n"
522 "Please connect your machine to a network with a "
523 "DHCP server and retry."), 50, 10, _("Retry"), _("Cancel"));
524
525 if (rc)
526 goto EXIT;
527 }
528 }
529
530 // Download the image if required
531 if (config.perform_download) {
532 fprintf(flog, "Download URL: %s\n", config.download_url);
533 snprintf(commandstring, sizeof(commandstring), "/usr/bin/downloadsource.sh %s %s",
534 SOURCE_TEMPFILE, config.download_url);
535
536 while (!sourcedrive) {
537 rc = runcommandwithstatus(commandstring, title, _("Downloading installation image..."), logfile);
538
539 FILE* f = fopen(SOURCE_TEMPFILE, "r");
540 if (f) {
541 sourcedrive = strdup(SOURCE_TEMPFILE);
542 fclose(f);
543 } else {
544 char reason[STRING_SIZE] = "-";
545 if (rc == 2)
546 snprintf(reason, sizeof(STRING_SIZE), _("BLAKE2 checksum mismatch"));
547
548 snprintf(message, sizeof(message),
549 _("The installation image could not be downloaded.\n Reason: %s\n\n%s"),
550 reason, config.download_url);
551
552 rc = newtWinOkCancel(title, message, 75, 12, _("Retry"), _("Cancel"));
553 if (rc)
554 goto EXIT;
555 }
556 }
557 }
558 }
559
560 assert(sourcedrive);
561
562 int r = hw_mount(sourcedrive, SOURCE_MOUNT_PATH, "iso9660", MS_RDONLY);
563 if (r) r = hw_mount(sourcedrive, SOURCE_MOUNT_PATH, "ntfs3", MS_RDONLY);
564 if (r) r = hw_mount(sourcedrive, SOURCE_MOUNT_PATH, "vfat", MS_RDONLY);
565 if (r)
566 {
567 snprintf(message, sizeof(message), _("Could not mount %s to %s:\n %s\n"),
568 sourcedrive, SOURCE_MOUNT_PATH, strerror(errno));
569 errorbox(message);
570 goto EXIT;
571 }
572
573 if (!config.unattended) {
574 // Read the license file.
575 if (!(copying = fopen(LICENSE_FILE, "r"))) {
576 sprintf(discl_msg, "Could not open license file: %s\n", LICENSE_FILE);
577 fprintf(flog, "%s", discl_msg);
578 } else {
579 fread(discl_msg, 1, 40000, copying);
580 fclose(copying);
581
582 if (newtLicenseBox(_("License Agreement"), discl_msg, 75, 20)) {
583 errorbox(_("License not accepted!"));
584
585 goto EXIT;
586 }
587 }
588 }
589
590 int part_type = HW_PART_TYPE_NORMAL;
591
592 // Scan for disks to install on.
593 struct hw_disk** disks = hw_find_disks(hw, sourcedrive);
594 unsigned int num_selected_disks = 0;
595
596 // Check how many disks have been found and what
597 // we can do with them.
598 unsigned int num_disks = hw_count_disks(disks);
599
600 while (1) {
601 // no harddisks found
602 if (num_disks == 0) {
603 errorbox(_("No hard disk found."));
604 goto EXIT;
605
606 // exactly one disk has been found
607 // or if we are running in unattended mode, we will select
608 // the first disk and go with that one
609 } else if ((num_disks == 1) || (config.unattended && num_disks >= 1)) {
610 selected_disks = hw_select_first_disk(disks);
611
612 // more than one usable disk has been found and
613 // the user needs to choose what to do with them
614 } else {
615 const char* disk_names[num_disks];
616 int disk_selection[num_disks];
617
618 for (unsigned int i = 0; i < num_disks; i++) {
619 disk_names[i] = disks[i]->description;
620 disk_selection[i] = 0;
621 }
622
623 while (!selected_disks) {
624 rc = newtChecklist(_("Disk Selection"),
625 _("Select the disk(s) you want to install IPFire on. "
626 "First those will be partitioned, and then the partitions will have a filesystem put on them.\n\n"
627 "ALL DATA ON THE DISK WILL BE DESTROYED."),
628 50, 20, num_disks, disk_names, disk_selection);
629
630 // Error
631 if (rc < 0) {
632 goto EXIT;
633
634 // Nothing has been selected
635 } else if (rc == 0) {
636 errorbox(_("No disk has been selected.\n\n"
637 "Please select one or more disks you want to install IPFire on."));
638
639 } else {
640 selected_disks = hw_select_disks(disks, disk_selection);
641 }
642 }
643 }
644
645 // Don't print the auto-selected harddisk setup in
646 // unattended mode.
647 if (config.unattended)
648 break;
649
650 num_selected_disks = hw_count_disks(selected_disks);
651
652 if (num_selected_disks == 1) {
653 snprintf(message, sizeof(message),
654 _("The installation program will now prepare the chosen harddisk:\n\n %s\n\n"
655 "Do you agree to continue?"), (*selected_disks)->description);
656 rc = newtWinOkCancel(_("Disk Setup"), message, 50, 10,
657 _("Delete all data"), _("Cancel"));
658
659 if (rc == 0)
660 break;
661
662 } else if (num_selected_disks == 2) {
663 snprintf(message, sizeof(message),
664 _("The installation program will now set up a RAID configuration on the selected harddisks:\n\n %s\n %s\n\n"
665 "Do you agree to continue?"), selected_disks[0]->description, selected_disks[1]->description);
666 rc = newtWinOkCancel(_("RAID Setup"), message, 50, 14,
667 _("Delete all data"), _("Cancel"));
668
669 if (rc == 0) {
670 part_type = HW_PART_TYPE_RAID1;
671
672 break;
673 }
674
675 // Currently not supported
676 } else {
677 errorbox(_("Your disk configuration is currently not supported."));
678 fprintf(flog, "Num disks selected: %d\n", num_selected_disks);
679 }
680
681 if (selected_disks) {
682 hw_free_disks(selected_disks);
683 selected_disks = NULL;
684 }
685 }
686
687 hw_free_disks(disks);
688
689 // Filesystem selection
690 int filesystem = HW_FS_DEFAULT;
691
692 if (!config.unattended) {
693 struct filesystems {
694 int fstype;
695 char* description;
696 } filesystems[] = {
697 { HW_FS_EXT4, _("ext4 Filesystem") },
698 { HW_FS_EXT4_WO_JOURNAL, _("ext4 Filesystem without journal") },
699 { HW_FS_XFS, _("XFS Filesystem") },
700 { HW_FS_BTRFS, _("BTRFS Filesystem (EXPERIMENTAL)") },
701 { 0, NULL },
702 };
703 unsigned int num_filesystems = sizeof(filesystems) / sizeof(*filesystems);
704
705 char* fs_names[num_filesystems];
706 int fs_choice = 0;
707 for (unsigned int i = 0; i < num_filesystems; i++) {
708 if (HW_FS_DEFAULT == filesystems[i].fstype)
709 fs_choice = i;
710
711 fs_names[i] = filesystems[i].description;
712 }
713
714 rc = newtWinMenu(_("Filesystem Selection"), _("Please choose your filesystem:"),
715 50, 5, 5, 5, fs_names, &fs_choice, _("OK"), _("Cancel"), NULL);
716
717 if (rc == 2)
718 goto EXIT;
719
720 filesystem = filesystems[fs_choice].fstype;
721 }
722
723 destination = hw_make_destination(hw, part_type, selected_disks, config.disable_swap, filesystem);
724
725 if (!destination) {
726 errorbox(_("Your harddisk is too small."));
727 goto EXIT;
728 }
729
730 fprintf(flog, "Destination drive: %s\n", destination->path);
731 fprintf(flog, " bootldr: %s (%lluMB)\n", destination->part_bootldr, BYTES2MB(destination->size_bootldr));
732 fprintf(flog, " boot : %s (%lluMB)\n", destination->part_boot, BYTES2MB(destination->size_boot));
733 fprintf(flog, " ESP : %s (%lluMB)\n", destination->part_boot_efi, BYTES2MB(destination->size_boot_efi));
734 fprintf(flog, " swap : %s (%lluMB)\n", destination->part_swap, BYTES2MB(destination->size_swap));
735 fprintf(flog, " root : %s (%lluMB)\n", destination->part_root, BYTES2MB(destination->size_root));
736 fprintf(flog, "Memory : %lluMB\n", BYTES2MB(hw_memory()));
737
738 // Warn the user if there is not enough space to create a swap partition
739 if (!config.unattended) {
740 if (!config.disable_swap && !*destination->part_swap) {
741 rc = newtWinChoice(title, _("OK"), _("Cancel"),
742 _("Your harddisk is very small, but you can continue without a swap partition."));
743
744 if (rc != 1)
745 goto EXIT;
746 }
747 }
748
749 // Setting up RAID if needed.
750 if (destination->is_raid) {
751 statuswindow(60, 4, title, _("Building RAID..."));
752
753 rc = hw_setup_raid(destination, logfile);
754 if (rc) {
755 errorbox(_("Unable to build the RAID."));
756 goto EXIT;
757 }
758
759 newtPopWindow();
760 } else {
761 // We will have to destroy all RAID setups that may have
762 // been on the devices that we want to use now.
763 hw_destroy_raid_superblocks(destination, logfile);
764 }
765
766 // Execute the partitioning...
767 statuswindow(60, 4, title, _("Partitioning disk..."));
768
769 rc = hw_create_partitions(destination, logfile);
770 if (rc) {
771 errorbox(_("Unable to partition the disk."));
772 goto EXIT;
773 }
774
775 newtPopWindow();
776
777 // Execute the formatting...
778 statuswindow(60, 4, title, _("Creating filesystems..."));
779
780 rc = hw_create_filesystems(destination, logfile);
781 if (rc) {
782 errorbox(_("Unable to create filesystems."));
783 goto EXIT;
784 }
785
786 rc = hw_mount_filesystems(destination, DESTINATION_MOUNT_PATH);
787 if (rc) {
788 errorbox(_("Unable to mount filesystems."));
789 goto EXIT;
790 }
791
792 newtPopWindow();
793
794 // Extract files...
795 snprintf(commandstring, STRING_SIZE,
796 "/bin/tar --acls --xattrs --xattrs-include='*' -C /harddisk -xvf /cdrom/distro.img --zstd 2>/dev/null");
797
798 if (runcommandwithprogress(60, 4, title, commandstring, INST_FILECOUNT,
799 _("Installing the system..."), logfile)) {
800 errorbox(_("Unable to install the system."));
801 goto EXIT;
802 }
803
804 // Write fstab
805 rc = hw_write_fstab(destination);
806 if (rc) {
807 fprintf(flog, "Could not write /etc/fstab\n");
808 goto EXIT;
809 }
810
811 /* Save language und local settings */
812 write_lang_configs(config.language);
813
814 /* Build cache lang file */
815 snprintf(commandstring, STRING_SIZE, "/usr/sbin/chroot /harddisk /usr/bin/perl -e \"require '" CONFIG_ROOT "/lang.pl'; &Lang::BuildCacheLang\"");
816 if (runcommandwithstatus(commandstring, title, _("Installing the language cache..."), logfile)) {
817 errorbox(_("Unable to install the language cache."));
818 goto EXIT;
819 }
820
821 /* trigger udev to add disk-by-uuid entries */
822 snprintf(commandstring, STRING_SIZE, "/usr/sbin/chroot /harddisk /sbin/udevadm trigger");
823 if (runcommandwithstatus(commandstring, title, _("Trigger udev to redetect partitions..."), logfile)) {
824 errorbox(_("Error triggering udev to redetect partitions."));
825 goto EXIT;
826 }
827
828 // Installing bootloader...
829 statuswindow(60, 4, title, _("Installing the bootloader..."));
830
831 /* Serial console ? */
832 if (config.serial_console) {
833 /* grub */
834 FILE* f = fopen(DESTINATION_MOUNT_PATH "/etc/default/grub", "a");
835 if (!f) {
836 errorbox(_("Unable to open /etc/default/grub for writing."));
837 goto EXIT;
838 }
839
840 fprintf(f, "GRUB_TERMINAL=\"serial\"\n");
841 fprintf(f, "GRUB_SERIAL_COMMAND=\"serial --unit=0 --speed=%d\"\n", SERIAL_BAUDRATE);
842 fclose(f);
843
844 replace(DESTINATION_MOUNT_PATH "/etc/default/grub", "panic=10", "panic=10 console=ttyS0,115200n8");
845 }
846
847 /* novga */
848 if (config.novga) {
849 /* grub */
850 FILE* f = fopen(DESTINATION_MOUNT_PATH "/etc/default/grub", "a");
851 if (!f) {
852 errorbox(_("Unable to open /etc/default/grub for writing."));
853 goto EXIT;
854 }
855
856 fprintf(f, "GRUB_GFXMODE=\"none\"\n");
857 fclose(f);
858 }
859
860 rc = hw_install_bootloader(hw, destination, logfile);
861 if (rc) {
862 errorbox(_("Unable to install the bootloader."));
863 goto EXIT;
864 }
865
866 newtPopWindow();
867
868 /* Set marker that the user has already accepted the GPL if the license has been shown
869 * in the installation process. In unatteded mode, the user will be presented the
870 * license when he or she logs on to the web user interface for the first time. */
871 if (!config.unattended)
872 mysystem(logfile, "/usr/bin/touch /harddisk/var/ipfire/main/gpl_accepted");
873
874 /* Copy restore file from cdrom */
875 char* backup_file = hw_find_backup_file(logfile, SOURCE_MOUNT_PATH);
876 if (backup_file) {
877 rc = 0;
878 if (!config.unattended) {
879 rc = newtWinOkCancel(title, _("A backup file has been found on the installation image.\n\n"
880 "Do you want to restore the backup?"), 50, 10, _("Yes"), _("No"));
881 }
882
883 if (rc == 0) {
884 rc = hw_restore_backup(logfile, backup_file, DESTINATION_MOUNT_PATH);
885
886 if (rc) {
887 errorbox(_("An error occured when the backup file was restored."));
888 goto EXIT;
889 }
890 }
891
892 free(backup_file);
893 }
894
895 // Download and execute the postinstall script
896 if (*config.postinstall) {
897 snprintf(commandstring, sizeof(commandstring),
898 "/usr/bin/execute-postinstall.sh %s %s", DESTINATION_MOUNT_PATH, config.postinstall);
899
900 if (runcommandwithstatus(commandstring, title, _("Running post-install script..."), logfile)) {
901 errorbox(_("Post-install script failed."));
902 goto EXIT;
903 }
904 }
905
906 // Umount the destination drive
907 statuswindow(60, 4, title, _("Umounting filesystems..."));
908
909 rc = hw_umount_filesystems(destination, DESTINATION_MOUNT_PATH);
910 if (rc) {
911 // Show an error message if filesystems could not be umounted properly
912 snprintf(message, sizeof(message),
913 _("Could not umount all filesystems successfully:\n\n %s"), strerror(errno));
914 errorbox(message);
915 goto EXIT;
916 }
917
918 // Umount source drive and eject
919 hw_umount(SOURCE_MOUNT_PATH, NULL);
920
921 // Free downloaded ISO image
922 if (strcmp(sourcedrive, SOURCE_TEMPFILE) == 0) {
923 rc = unlink(sourcedrive);
924 if (rc)
925 fprintf(flog, "Could not free downloaded ISO image: %s\n", sourcedrive);
926
927 // or eject real images
928 } else {
929 snprintf(commandstring, STRING_SIZE, "/usr/bin/eject %s", sourcedrive);
930 mysystem(logfile, commandstring);
931 }
932 newtPopWindow();
933
934 // Stop the RAID array if we are using RAID
935 if (destination->is_raid)
936 hw_stop_all_raid_arrays(logfile);
937
938 // Show a short message that the installation went well and
939 // wait a moment so that all disk caches get flushed.
940 if (config.unattended) {
941 splashWindow(title, _("Unattended installation has finished. The system will be shutting down in a moment..."), 5);
942
943 } else {
944 snprintf(message, sizeof(message), _(
945 "%s was successfully installed!\n\n"
946 "Please remove any installation mediums from this system and hit the reboot button. "
947 "Once the system has restarted you will be asked to setup networking and system passwords. "
948 "After that, you should point your web browser at https://%s:444 (or what ever you name "
949 "your %s) for the web configuration console."), DISTRO_NAME, DISTRO_SNAME, DISTRO_NAME);
950 newtWinMessage(_("Congratulations!"), _("Reboot"), message);
951 }
952
953 allok = 1;
954
955 EXIT:
956 fprintf(flog, "Install program ended.\n");
957 fflush(flog);
958 fclose(flog);
959
960 if (!allok)
961 newtWinMessage(title, _("OK"), _("Setup has failed. Press Ok to reboot."));
962
963 newtFinished();
964
965 // Free resources
966 if (system_release)
967 free(system_release);
968
969 if (roottext)
970 free(roottext);
971
972 if (helpline)
973 free(helpline);
974
975 if (sourcedrive)
976 free(sourcedrive);
977
978 if (destination)
979 free(destination);
980
981 hw_stop_all_raid_arrays(logfile);
982
983 if (selected_disks)
984 hw_free_disks(selected_disks);
985
986 if (hw)
987 hw_free(hw);
988
989 fcloseall();
990
991 if (allok == 1)
992 return 0;
993
994 return 1;
995 }