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