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