]> git.ipfire.org Git - ipfire-2.x.git/blob - src/installer/main.c
installer+grub: Fix serial console support with GRUB2.
[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
11 #include <assert.h>
12 #include <errno.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <sys/mount.h>
17
18 #include "hw.h"
19 #include "install.h"
20
21 // Translation
22 #include <libintl.h>
23 #define _(x) dgettext("installer", x)
24
25 #define INST_FILECOUNT 21000
26 #define UNATTENDED_CONF "/cdrom/boot/unattended.conf"
27 #define LICENSE_FILE "/cdrom/COPYING"
28
29 extern char url[STRING_SIZE];
30
31 static int newtChecklist(const char* title, const char* message,
32 unsigned int width, unsigned int height, unsigned int num_entries,
33 const char** entries, int* states) {
34 int ret;
35 const int list_height = 4;
36
37 char cbstates[num_entries];
38
39 for (unsigned int i = 0; i < num_entries; i++) {
40 cbstates[i] = states[i] ? '*' : ' ';
41 }
42
43 newtCenteredWindow(width, height, title);
44
45 newtComponent textbox = newtTextbox(1, 1, width - 2, height - 6 - list_height,
46 NEWT_FLAG_WRAP);
47 newtTextboxSetText(textbox, message);
48
49 int top = newtTextboxGetNumLines(textbox) + 2;
50
51 newtComponent form = newtForm(NULL, NULL, 0);
52
53 newtComponent sb = NULL;
54 if (list_height < num_entries) {
55 sb = newtVerticalScrollbar(
56 width - 4, top + 1, list_height,
57 NEWT_COLORSET_CHECKBOX, NEWT_COLORSET_ACTCHECKBOX);
58
59 newtFormAddComponent(form, sb);
60 }
61
62 newtComponent subform = newtForm(sb, NULL, 0);
63 newtFormSetBackground(subform, NEWT_COLORSET_CHECKBOX);
64
65 newtFormSetHeight(subform, list_height);
66 newtFormSetWidth(subform, width - 10);
67
68 for (unsigned int i = 0; i < num_entries; i++) {
69 newtComponent cb = newtCheckbox(4, top + i, entries[i], cbstates[i],
70 NULL, &cbstates[i]);
71
72 newtFormAddComponent(subform, cb);
73 }
74
75 newtFormAddComponents(form, textbox, subform, NULL);
76
77 newtComponent btn_okay = newtButton((width - 18) / 3, height - 4, _("OK"));
78 newtComponent btn_cancel = newtButton((width - 18) / 3 * 2 + 9, height - 4, _("Cancel"));
79 newtFormAddComponents(form, btn_okay, btn_cancel, NULL);
80
81 newtComponent answer = newtRunForm(form);
82
83 if ((answer == NULL) || (answer == btn_cancel)) {
84 ret = -1;
85 } else {
86 ret = 0;
87
88 for (unsigned int i = 0; i < num_entries; i++) {
89 states[i] = (cbstates[i] != ' ');
90
91 if (states[i])
92 ret++;
93 }
94 }
95
96 newtFormDestroy(form);
97 newtPopWindow();
98
99 return ret;
100 }
101
102 static int newtWinOkCancel(const char* title, const char* message, int width, int height,
103 const char* btn_txt_ok, const char* btn_txt_cancel) {
104 int ret = 1;
105
106 newtCenteredWindow(width, height, title);
107
108 newtComponent form = newtForm(NULL, NULL, 0);
109
110 newtComponent textbox = newtTextbox(1, 1, width - 2, height - 6, NEWT_FLAG_WRAP);
111 newtTextboxSetText(textbox, message);
112 newtFormAddComponent(form, textbox);
113
114 newtComponent btn_ok = newtButton((width - 16) / 3, height - 4, btn_txt_ok);
115 newtComponent btn_cancel = newtButton((width - 16) / 3 * 2 + 9, height - 4,
116 btn_txt_cancel);
117
118 newtFormAddComponents(form, btn_ok, btn_cancel, NULL);
119
120 newtComponent answer = newtRunForm(form);
121
122 if (answer == btn_ok) {
123 ret = 0;
124 }
125
126 newtFormDestroy(form);
127 newtPopWindow();
128
129 return ret;
130 }
131
132 static int newtLicenseBox(const char* title, const char* text, int width, int height) {
133 int ret = 1;
134
135 newtCenteredWindow(width, height, title);
136
137 newtComponent form = newtForm(NULL, NULL, 0);
138
139 newtComponent textbox = newtTextbox(1, 1, width - 2, height - 7,
140 NEWT_FLAG_WRAP|NEWT_FLAG_SCROLL);
141 newtTextboxSetText(textbox, text);
142 newtFormAddComponent(form, textbox);
143
144 char choice;
145 newtComponent checkbox = newtCheckbox(3, height - 3, _("I accept this license"),
146 ' ', " *", &choice);
147
148 newtComponent btn = newtButton(width - 15, height - 4, _("OK"));
149
150 newtFormAddComponents(form, checkbox, btn, NULL);
151
152 newtComponent answer = newtRunForm(form);
153 if (answer == btn && choice == '*')
154 ret = 0;
155
156 newtFormDestroy(form);
157 newtPopWindow();
158
159 return ret;
160 }
161
162 int write_lang_configs(const char *lang) {
163 struct keyvalue *kv = initkeyvalues();
164
165 /* default stuff for main/settings. */
166 replacekeyvalue(kv, "LANGUAGE", lang);
167 replacekeyvalue(kv, "HOSTNAME", SNAME);
168 replacekeyvalue(kv, "THEME", "ipfire");
169 writekeyvalues(kv, "/harddisk" CONFIG_ROOT "/main/settings");
170 freekeyvalues(kv);
171
172 return 1;
173 }
174
175 static char* get_system_release() {
176 char system_release[STRING_SIZE] = "\0";
177
178 FILE* f = fopen("/etc/system-release", "r");
179 if (f) {
180 fgets(system_release, sizeof(system_release), f);
181 fclose(f);
182 }
183
184 return strdup(system_release);
185 }
186
187 static char* center_string(const char* str, int width) {
188 unsigned int str_len = strlen(str);
189
190 unsigned int indent_length = (width - str_len) / 2;
191 char indent[indent_length + 1];
192
193 for (unsigned int i = 0; i < indent_length; i++) {
194 indent[i] = ' ';
195 }
196 indent[indent_length] = '\0';
197
198 char* string = NULL;
199 if (asprintf(&string, "%s%s", indent, str) < 0)
200 return NULL;
201
202 return string;
203 }
204
205 #define DEFAULT_LANG "English"
206 #define NUM_LANGS 8
207
208 static struct lang {
209 const char* code;
210 char* name;
211 } languages[NUM_LANGS + 1] = {
212 { "nl_NL.utf8", "Dutch (Nederlands)" },
213 { "en_US.utf8", "English" },
214 { "fr_FR.utf8", "French (Français)" },
215 { "de_DE.utf8", "German (Deutsch)" },
216 { "pl_PL.utf8", "Polish (Polski)" },
217 { "ru_RU.utf8", "Russian (Русский)" },
218 { "es_ES.utf8", "Spanish (Español)" },
219 { "tr_TR.utf8", "Turkish (Türkçe)" },
220 { NULL, NULL },
221 };
222
223 int main(int argc, char *argv[]) {
224 struct hw* hw = hw_init();
225 const char* logfile = NULL;
226
227 // Read /etc/system-release
228 char* system_release = get_system_release();
229
230 char discl_msg[40000] = "Disclaimer\n";
231
232 char* sourcedrive = NULL;
233 int rc = 0;
234 char commandstring[STRING_SIZE];
235 int choice;
236 char shortlangname[10];
237 char message[STRING_SIZE];
238 char title[STRING_SIZE];
239 int allok = 0;
240 FILE *handle, *cmdfile, *copying;
241 char line[STRING_SIZE];
242
243 int unattended = 0;
244 int serialconsole = 0;
245 struct keyvalue *unattendedkv = initkeyvalues();
246 char restore_file[STRING_SIZE] = "";
247
248 setlocale (LC_ALL, "");
249 sethostname( SNAME , 10);
250
251 /* Log file/terminal stuff. */
252 FILE* flog = NULL;
253 if (argc >= 2) {
254 logfile = argv[1];
255
256 if (!(flog = fopen(logfile, "w+")))
257 return 0;
258 } else {
259 return 0;
260 }
261
262 fprintf(flog, "Install program started.\n");
263
264 newtInit();
265 newtCls();
266
267 // Determine the size of the screen
268 int screen_cols = 0;
269 int screen_rows = 0;
270
271 newtGetScreenSize(&screen_cols, &screen_rows);
272
273 // Draw title
274 char* roottext = center_string(system_release, screen_cols);
275 newtDrawRootText(0, 0, roottext);
276
277 snprintf(title, sizeof(title), "%s - %s", NAME, SLOGAN);
278
279 if (! (cmdfile = fopen("/proc/cmdline", "r")))
280 {
281 fprintf(flog, "Couldn't open commandline: /proc/cmdline\n");
282 } else {
283 fgets(line, STRING_SIZE, cmdfile);
284
285 // check if we have to make an unattended install
286 if (strstr (line, "unattended") != NULL) {
287 unattended = 1;
288 runcommandwithstatus("/bin/sleep 10", title, "WARNING: Unattended installation will start in 10 seconds...", NULL);
289 }
290 // check if we have to patch for serial console
291 if (strstr (line, "console=ttyS0") != NULL) {
292 serialconsole = 1;
293 }
294 }
295
296 // Load common modules
297 mysystem(logfile, "/sbin/modprobe vfat"); // USB key
298 hw_stop_all_raid_arrays(logfile);
299
300 if (!unattended) {
301 // Language selection
302 char* langnames[NUM_LANGS + 1];
303
304 for (unsigned int i = 0; i < NUM_LANGS; i++) {
305 if (strcmp(languages[i].name, DEFAULT_LANG) == 0)
306 choice = i;
307
308 langnames[i] = languages[i].name;
309 }
310 langnames[NUM_LANGS] = NULL;
311
312 rc = newtWinMenu(_("Language selection"), _("Select the language you wish to use for the installation."),
313 50, 5, 5, 8, langnames, &choice, _("OK"), NULL);
314
315 assert(choice <= NUM_LANGS);
316
317 fprintf(flog, "Selected language: %s (%s)\n", languages[choice].name, languages[choice].code);
318
319 setenv("LANGUAGE", languages[choice].code, 1);
320 setlocale(LC_ALL, languages[choice].code);
321 }
322
323 char* helpline = center_string(_("<Tab>/<Alt-Tab> between elements | <Space> selects | <F12> next screen"), screen_cols);
324 newtPushHelpLine(helpline);
325
326 if (!unattended) {
327 snprintf(message, sizeof(message),
328 _("Welcome to the %s installation program.\n\n"
329 "Selecting Cancel on any of the following screens will reboot the computer."), NAME);
330 newtWinMessage(title, _("Start installation"), message);
331 }
332
333 /* Search for a source drive that holds the right
334 * version of the image we are going to install. */
335 sourcedrive = hw_find_source_medium(hw);
336
337 fprintf(flog, "Source drive: %s\n", sourcedrive);
338 if (!sourcedrive) {
339 newtWinMessage(title, _("OK"), _("No local source media found. Starting download."));
340 runcommandwithstatus("/bin/downloadsource.sh", title, _("Downloading installation image ..."), logfile);
341 if ((handle = fopen("/tmp/source_device", "r")) == NULL) {
342 errorbox(_("Download error"));
343 goto EXIT;
344 }
345
346 fgets(sourcedrive, 5, handle);
347 fclose(handle);
348 }
349
350 assert(sourcedrive);
351
352 int r = hw_mount(sourcedrive, SOURCE_MOUNT_PATH, "iso9660", MS_RDONLY);
353 if (r) {
354 fprintf(flog, "Could not mount %s to %s\n", sourcedrive, SOURCE_MOUNT_PATH);
355 fprintf(flog, strerror(errno));
356 exit(1);
357 }
358
359 /* load unattended configuration */
360 if (unattended) {
361 fprintf(flog, "unattended: Reading unattended.conf\n");
362
363 (void) readkeyvalues(unattendedkv, UNATTENDED_CONF);
364 findkey(unattendedkv, "RESTORE_FILE", restore_file);
365 }
366
367 if (!unattended) {
368 // Read the license file.
369 if (!(copying = fopen(LICENSE_FILE, "r"))) {
370 sprintf(discl_msg, "Could not open license file: %s\n", LICENSE_FILE);
371 fprintf(flog, discl_msg);
372 } else {
373 fread(discl_msg, 1, 40000, copying);
374 fclose(copying);
375
376 if (newtLicenseBox(_("License Agreement"), discl_msg, 75, 20)) {
377 errorbox(_("License not accepted!"));
378
379 goto EXIT;
380 }
381 }
382 }
383
384 int part_type = HW_PART_TYPE_NORMAL;
385
386 // Scan for disks to install on.
387 struct hw_disk** disks = hw_find_disks(hw);
388
389 struct hw_disk** selected_disks = NULL;
390 unsigned int num_selected_disks = 0;
391
392 // Check how many disks have been found and what
393 // we can do with them.
394 unsigned int num_disks = hw_count_disks(disks);
395
396 while (1) {
397 // no harddisks found
398 if (num_disks == 0) {
399 errorbox(_("No hard disk found."));
400 goto EXIT;
401
402 // exactly one disk has been found
403 } else if (num_disks == 1) {
404 selected_disks = hw_select_disks(disks, NULL);
405
406 // more than one usable disk has been found and
407 // the user needs to choose what to do with them
408 } else {
409 const char* disk_names[num_disks];
410 int disk_selection[num_disks];
411
412 for (unsigned int i = 0; i < num_disks; i++) {
413 disk_names[i] = &disks[i]->description;
414 disk_selection[i] = 0;
415 }
416
417 while (!selected_disks) {
418 rc = newtChecklist(_("Disk Selection"),
419 _("Select the disk(s) you want to install IPFire on. "
420 "First those will be partitioned, and then the partitions will have a filesystem put on them.\n\n"
421 "ALL DATA ON THE DISK WILL BE DESTROYED."),
422 50, 20, num_disks, disk_names, disk_selection);
423
424 // Error
425 if (rc < 0) {
426 goto EXIT;
427
428 // Nothing has been selected
429 } else if (rc == 0) {
430 errorbox(_("No disk has been selected.\n\n"
431 "Please select one or more disks you want to install IPFire on."));
432
433 } else {
434 selected_disks = hw_select_disks(disks, disk_selection);
435 }
436 }
437 }
438
439 num_selected_disks = hw_count_disks(selected_disks);
440
441 if (num_selected_disks == 1) {
442 snprintf(message, sizeof(message),
443 _("The installation program will now prepare the chosen harddisk:\n\n %s\n\n"
444 "Do you agree to continue?"), (*selected_disks)->description);
445 rc = newtWinOkCancel(_("Disk Setup"), message, 50, 10,
446 _("Delete all data"), _("Cancel"));
447
448 if (rc == 0)
449 break;
450
451 } else if (num_selected_disks == 2) {
452 snprintf(message, sizeof(message),
453 _("The installation program will now set up a RAID configuration on the selected harddisks:\n\n %s\n %s\n\n"
454 "Do you agree to continue?"), selected_disks[0]->description, selected_disks[1]->description);
455 rc = newtWinOkCancel(_("RAID Setup"), message, 50, 14,
456 _("Delete all data"), _("Cancel"));
457
458 if (rc == 0) {
459 part_type = HW_PART_TYPE_RAID1;
460
461 break;
462 }
463
464 // Currently not supported
465 } else {
466 errorbox(_("You disk configuration is currently not supported."));
467 }
468
469 if (selected_disks) {
470 hw_free_disks(selected_disks);
471 selected_disks = NULL;
472 }
473 }
474
475 hw_free_disks(disks);
476
477 struct hw_destination* destination = hw_make_destination(part_type, selected_disks);
478
479 if (!destination) {
480 errorbox(_("Your harddisk is too small."));
481 goto EXIT;
482 }
483
484 fprintf(flog, "Destination drive: %s\n", destination->path);
485 fprintf(flog, " bootldr: %s (%lluMB)\n", destination->part_bootldr, BYTES2MB(destination->size_bootldr));
486 fprintf(flog, " boot : %s (%lluMB)\n", destination->part_boot, BYTES2MB(destination->size_boot));
487 fprintf(flog, " swap : %s (%lluMB)\n", destination->part_swap, BYTES2MB(destination->size_swap));
488 fprintf(flog, " root : %s (%lluMB)\n", destination->part_root, BYTES2MB(destination->size_root));
489 fprintf(flog, " data : %s (%lluMB)\n", destination->part_data, BYTES2MB(destination->size_data));
490
491 // Warn the user if there is not enough space to create a swap partition
492 if (!unattended && !*destination->part_swap) {
493 rc = newtWinChoice(title, _("OK"), _("Cancel"),
494 _("Your harddisk is very small, but you can continue without a swap partition."));
495
496 if (rc != 1)
497 goto EXIT;
498 }
499
500 // Filesystem selection
501 if (!unattended) {
502 struct filesystems {
503 int fstype;
504 const char* description;
505 } filesystems[] = {
506 { HW_FS_EXT4, _("ext4 Filesystem") },
507 { HW_FS_EXT4_WO_JOURNAL, _("ext4 Filesystem without journal") },
508 { HW_FS_XFS, _("XFS Filesystem") },
509 { HW_FS_REISERFS, _("ReiserFS Filesystem") },
510 { 0, NULL },
511 };
512 unsigned int num_filesystems = sizeof(filesystems) / sizeof(*filesystems);
513
514 char* fs_names[num_filesystems];
515 int fs_choice = 0;
516 for (unsigned int i = 0; i < num_filesystems; i++) {
517 if (HW_FS_DEFAULT == filesystems[i].fstype)
518 fs_choice = i;
519
520 fs_names[i] = filesystems[i].description;
521 }
522
523 rc = newtWinMenu(_("Filesystem Selection"), _("Please choose your filesystem:"),
524 50, 5, 5, 6, fs_names, &fs_choice, _("OK"), _("Cancel"), NULL);
525
526 if (rc == 2)
527 goto EXIT;
528
529 destination->filesystem = filesystems[fs_choice].fstype;
530 }
531
532 // Setting up RAID if needed.
533 if (destination->is_raid) {
534 statuswindow(60, 4, title, _("Building RAID..."));
535
536 rc = hw_setup_raid(destination, logfile);
537 if (rc) {
538 errorbox(_("Unable to build the RAID."));
539 goto EXIT;
540 }
541
542 newtPopWindow();
543 }
544
545 // Execute the partitioning...
546 statuswindow(60, 4, title, _("Partitioning disk..."));
547
548 rc = hw_create_partitions(destination, logfile);
549 if (rc) {
550 errorbox(_("Unable to partition the disk."));
551 goto EXIT;
552 }
553
554 newtPopWindow();
555
556 // Execute the formatting...
557 statuswindow(60, 4, title, _("Creating filesystems..."));
558
559 rc = hw_create_filesystems(destination, logfile);
560 if (rc) {
561 errorbox(_("Unable to create filesystems."));
562 goto EXIT;
563 }
564
565 rc = hw_mount_filesystems(destination, DESTINATION_MOUNT_PATH);
566 if (rc) {
567 errorbox(_("Unable to mount filesystems."));
568 goto EXIT;
569 }
570
571 newtPopWindow();
572
573 // Extract files...
574 snprintf(commandstring, STRING_SIZE,
575 "/bin/tar -C /harddisk -xvf /cdrom/distro.img --lzma 2>/dev/null");
576
577 if (runcommandwithprogress(60, 4, title, commandstring, INST_FILECOUNT,
578 _("Installing the system..."), logfile)) {
579 errorbox(_("Unable to install the system."));
580 goto EXIT;
581 }
582
583 // Write fstab
584 rc = hw_write_fstab(destination);
585 if (rc) {
586 fprintf(flog, "Could not write /etc/fstab\n");
587 goto EXIT;
588 }
589
590 /* Save language und local settings */
591 write_lang_configs(shortlangname);
592
593 /* Build cache lang file */
594 snprintf(commandstring, STRING_SIZE, "/usr/sbin/chroot /harddisk /usr/bin/perl -e \"require '" CONFIG_ROOT "/lang.pl'; &Lang::BuildCacheLang\"");
595 if (runcommandwithstatus(commandstring, title, _("Installing the language cache..."), logfile)) {
596 errorbox(_("Unable to install the language cache."));
597 goto EXIT;
598 }
599
600 // Installing bootloader...
601 statuswindow(60, 4, title, _("Installing the bootloader..."));
602
603 /* Serial console ? */
604 if (serialconsole) {
605 /* grub */
606 FILE* f = fopen(DESTINATION_MOUNT_PATH "/etc/default/grub", "a");
607 if (!f) {
608 errorbox(_("Unable to open /etc/default/grub for writing."));
609 goto EXIT;
610 }
611
612 fprintf(f, "GRUB_TERMINAL=\"serial console\"\n");
613 fprintf(f, "GRUB_SERIAL_COMMAND=\"serial --unit=0 --speed=%d\"\n", SERIAL_BAUDRATE);
614 fclose(f);
615
616 replace(DESTINATION_MOUNT_PATH "/etc/default/grub", "panic=10", "panic=10 console=ttyS0,115200n8");
617
618 /* inittab */
619 replace("/harddisk/etc/inittab", "1:2345:respawn:", "#1:2345:respawn:");
620 replace("/harddisk/etc/inittab", "2:2345:respawn:", "#2:2345:respawn:");
621 replace("/harddisk/etc/inittab", "3:2345:respawn:", "#3:2345:respawn:");
622 replace("/harddisk/etc/inittab", "4:2345:respawn:", "#4:2345:respawn:");
623 replace("/harddisk/etc/inittab", "5:2345:respawn:", "#5:2345:respawn:");
624 replace("/harddisk/etc/inittab", "6:2345:respawn:", "#6:2345:respawn:");
625 replace("/harddisk/etc/inittab", "#7:2345:respawn:", "7:2345:respawn:");
626 }
627
628 rc = hw_install_bootloader(destination, logfile);
629 if (rc) {
630 errorbox(_("Unable to install the bootloader."));
631 goto EXIT;
632 }
633
634 newtPopWindow();
635
636 /* Set marker that the user has already accepted the gpl */
637 mysystem(logfile, "/usr/bin/touch /harddisk/var/ipfire/main/gpl_accepted");
638
639 /* Copy restore file from cdrom */
640 if (unattended && (strlen(restore_file) > 0)) {
641 fprintf(flog, "unattended: Copy restore file\n");
642 snprintf(commandstring, STRING_SIZE,
643 "cp /cdrom/%s /harddisk/var/ipfire/backup", restore_file);
644 mysystem(logfile, commandstring);
645 }
646
647 // Umount the destination drive
648 hw_umount_filesystems(destination, DESTINATION_MOUNT_PATH);
649
650 // Stop the RAID array if we are using RAID
651 if (destination->is_raid)
652 hw_stop_all_raid_arrays(logfile);
653
654 // Umount source drive and eject
655 hw_umount(SOURCE_MOUNT_PATH);
656
657 snprintf(commandstring, STRING_SIZE, "/usr/bin/eject %s", sourcedrive);
658 mysystem(logfile, commandstring);
659
660 if (!unattended) {
661 snprintf(message, sizeof(message), _(
662 "%s was successfully installed!\n\n"
663 "Please remove any installation mediums from this system and hit the reboot button. "
664 "Once the system has restarted you will be asked to setup networking and system passwords. "
665 "After that, you should point your web browser at https://%s:444 (or what ever you name "
666 "your %s) for the web configuration console."), NAME, SNAME, NAME);
667 newtWinMessage(_("Congratulations!"), _("Reboot"), message);
668 }
669
670 allok = 1;
671
672 EXIT:
673 fprintf(flog, "Install program ended.\n");
674 fflush(flog);
675 fclose(flog);
676
677 if (!allok)
678 newtWinMessage(title, _("OK"), _("Setup has failed. Press Ok to reboot."));
679
680 newtFinished();
681
682 // Free resources
683 free(system_release);
684 free(roottext);
685 free(helpline);
686
687 free(sourcedrive);
688 free(destination);
689
690 hw_stop_all_raid_arrays(logfile);
691
692 if (selected_disks)
693 hw_free_disks(selected_disks);
694
695 hw_free(hw);
696
697 fcloseall();
698
699 if (allok == 1)
700 return 0;
701
702 return 1;
703 }