1 /* SmoothWall install program.
3 * This program is distributed under the terms of the GNU General Public
4 * Licence. See the file COPYING for details.
6 * (c) Lawrence Manning, 2001
7 * Contains main entry point, and misc functions.6
15 #include <sys/mount.h>
22 #define _(x) dgettext("installer", x)
24 #define INST_FILECOUNT 21000
25 #define UNATTENDED_CONF "/cdrom/boot/unattended.conf"
26 #define LICENSE_FILE "/cdrom/COPYING"
33 extern char url
[STRING_SIZE
];
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
) {
39 const int list_height
= 4;
41 char cbstates
[num_entries
];
43 for (unsigned int i
= 0; i
< num_entries
; i
++) {
44 cbstates
[i
] = states
[i
] ? '*' : ' ';
47 newtCenteredWindow(width
, height
, title
);
49 newtComponent textbox
= newtTextbox(1, 1, width
- 2, height
- 6 - list_height
,
51 newtTextboxSetText(textbox
, message
);
53 int top
= newtTextboxGetNumLines(textbox
) + 2;
55 newtComponent form
= newtForm(NULL
, NULL
, 0);
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
);
63 newtFormAddComponent(form
, sb
);
66 newtComponent subform
= newtForm(sb
, NULL
, 0);
67 newtFormSetBackground(subform
, NEWT_COLORSET_CHECKBOX
);
69 newtFormSetHeight(subform
, list_height
);
70 newtFormSetWidth(subform
, width
- 10);
72 for (unsigned int i
= 0; i
< num_entries
; i
++) {
73 newtComponent cb
= newtCheckbox(4, top
+ i
, entries
[i
], cbstates
[i
],
76 newtFormAddComponent(subform
, cb
);
79 newtFormAddComponents(form
, textbox
, subform
, NULL
);
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
);
85 newtComponent answer
= newtRunForm(form
);
87 if ((answer
== NULL
) || (answer
== btn_cancel
)) {
92 for (unsigned int i
= 0; i
< num_entries
; i
++) {
93 states
[i
] = (cbstates
[i
] != ' ');
100 newtFormDestroy(form
);
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
) {
110 newtCenteredWindow(width
, height
, title
);
112 newtComponent form
= newtForm(NULL
, NULL
, 0);
114 newtComponent textbox
= newtTextbox(1, 1, width
- 2, height
- 6, NEWT_FLAG_WRAP
);
115 newtTextboxSetText(textbox
, message
);
116 newtFormAddComponent(form
, textbox
);
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,
122 newtFormAddComponents(form
, btn_ok
, btn_cancel
, NULL
);
124 newtComponent answer
= newtRunForm(form
);
126 if (answer
== btn_ok
) {
130 newtFormDestroy(form
);
136 static int newtLicenseBox(const char* title
, const char* text
, int width
, int height
) {
139 newtCenteredWindow(width
, height
, title
);
141 newtComponent form
= newtForm(NULL
, NULL
, 0);
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
);
149 newtComponent checkbox
= newtCheckbox(3, height
- 3, _("I accept this license"),
152 newtComponent btn
= newtButton(width
- 15, height
- 4, _("OK"));
154 newtFormAddComponents(form
, checkbox
, btn
, NULL
);
156 newtComponent answer
= newtRunForm(form
);
157 if (answer
== btn
&& choice
== '*')
160 newtFormDestroy(form
);
166 int write_lang_configs(const char *lang
) {
167 struct keyvalue
*kv
= initkeyvalues();
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");
179 int main(int argc
, char *argv
[]) {
180 struct hw
* hw
= hw_init();
182 char discl_msg
[40000] = "Disclaimer\n";
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
;
188 char commandstring
[STRING_SIZE
];
190 char shortlangname
[10];
191 char message
[STRING_SIZE
];
192 char title
[STRING_SIZE
];
194 FILE *handle
, *cmdfile
, *copying
;
195 char line
[STRING_SIZE
];
198 int serialconsole
= 0;
199 struct keyvalue
*unattendedkv
= initkeyvalues();
200 char restore_file
[STRING_SIZE
] = "";
202 setlocale (LC_ALL
, "");
203 sethostname( SNAME
, 10);
205 /* Log file/terminal stuff. */
208 if (!(flog
= fopen(argv
[1], "w+")))
216 fprintf(flog
, "Install program started.\n");
221 newtDrawRootText(14, 0, NAME
" " VERSION
" - " SLOGAN
);
222 sprintf (title
, "%s %s - %s", NAME
, VERSION
, SLOGAN
);
224 if (! (cmdfile
= fopen("/proc/cmdline", "r")))
226 fprintf(flog
, "Couldn't open commandline: /proc/cmdline\n");
228 fgets(line
, STRING_SIZE
, cmdfile
);
230 // check if we have to make an unattended install
231 if (strstr (line
, "unattended") != NULL
) {
233 runcommandwithstatus("/bin/sleep 10", title
, "WARNING: Unattended installation will start in 10 seconds...");
235 // check if we have to patch for serial console
236 if (strstr (line
, "console=ttyS0") != NULL
) {
241 // Load common modules
242 mysystem("/sbin/modprobe vfat"); // USB key
243 hw_stop_all_raid_arrays();
245 /* German is the default */
246 for (choice
= 0; langnames
[choice
]; choice
++)
248 if (strcmp(langnames
[choice
], "English") == 0)
251 if (!langnames
[choice
])
255 rc
= newtWinMenu("Language selection", "Select the language you wish to use for the " NAME
".", 50, 5, 5, 8,
256 langnames
, &choice
, "Ok", NULL
);
259 setlocale(LC_ALL
, shortlangnames
[choice
]);
261 newtPushHelpLine(_("<Tab>/<Alt-Tab> between elements | <Space> selects | <F12> next screen"));
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
);
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
);
274 fprintf(flog
, "Source drive: %s\n", 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"));
283 fgets(sourcedrive
, 5, handle
);
289 int r
= hw_mount(sourcedrive
, SOURCE_MOUNT_PATH
, "iso9660", MS_RDONLY
);
291 fprintf(flog
, "Could not mount %s to %s\n", sourcedrive
, SOURCE_MOUNT_PATH
);
292 fprintf(flog
, strerror(errno
));
296 /* load unattended configuration */
298 fprintf(flog
, "unattended: Reading unattended.conf\n");
300 (void) readkeyvalues(unattendedkv
, UNATTENDED_CONF
);
301 findkey(unattendedkv
, "RESTORE_FILE", restore_file
);
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
);
310 fread(discl_msg
, 1, 40000, copying
);
313 if (newtLicenseBox(title
, discl_msg
, 75, 20)) {
314 errorbox(_("License not accepted!"));
321 int part_type
= HW_PART_TYPE_NORMAL
;
323 // Scan for disks to install on.
324 struct hw_disk
** disks
= hw_find_disks(hw
);
326 struct hw_disk
** selected_disks
= NULL
;
327 unsigned int num_selected_disks
= 0;
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
);
334 // no harddisks found
335 if (num_disks
== 0) {
336 errorbox(_("No hard disk found."));
339 // exactly one disk has been found
340 } else if (num_disks
== 1) {
341 selected_disks
= hw_select_disks(disks
, NULL
);
343 // more than one usable disk has been found and
344 // the user needs to choose what to do with them
346 const char* disk_names
[num_disks
];
347 int disk_selection
[num_disks
];
349 for (unsigned int i
= 0; i
< num_disks
; i
++) {
350 disk_names
[i
] = &disks
[i
]->description
;
351 disk_selection
[i
] = 0;
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
);
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."));
371 selected_disks
= hw_select_disks(disks
, disk_selection
);
376 num_selected_disks
= hw_count_disks(selected_disks
);
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"));
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"));
396 part_type
= HW_PART_TYPE_RAID1
;
401 // Currently not supported
403 errorbox(_("You disk configuration is currently not supported."));
406 if (selected_disks
) {
407 hw_free_disks(selected_disks
);
408 selected_disks
= NULL
;
412 hw_free_disks(disks
);
414 struct hw_destination
* destination
= hw_make_destination(part_type
, selected_disks
);
417 errorbox(_("Your harddisk is too small."));
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
));
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)."));
437 // Filesystem selection
441 const char* description
;
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") },
449 unsigned int num_filesystems
= sizeof(filesystems
) / sizeof(*filesystems
);
451 char* fs_names
[num_filesystems
];
453 for (unsigned int i
= 0; i
< num_filesystems
; i
++) {
454 if (HW_FS_DEFAULT
== filesystems
[i
].fstype
)
457 fs_names
[i
] = filesystems
[i
].description
;
460 rc
= newtWinMenu(_("Filesystem Selection"), _("Please choose your filesystem:"),
461 50, 5, 5, 6, fs_names
, &fs_choice
, _("OK"), _("Cancel"), NULL
);
464 destination
->filesystem
= filesystems
[fs_choice
].fstype
;
470 // Setting up RAID if needed.
471 if (destination
->is_raid
) {
472 statuswindow(60, 4, title
, _("Building RAID..."));
474 rc
= hw_setup_raid(destination
);
476 errorbox(_("Unable to build the RAID."));
483 // Execute the partitioning...
484 statuswindow(60, 4, title
, _("Partitioning disk..."));
486 rc
= hw_create_partitions(destination
);
488 errorbox(_("Unable to partition the disk."));
494 // Execute the formatting...
495 statuswindow(60, 4, title
, _("Creating filesystems..."));
497 rc
= hw_create_filesystems(destination
);
499 errorbox(_("Unable to create filesystems."));
503 rc
= hw_mount_filesystems(destination
, DESTINATION_MOUNT_PATH
);
505 errorbox(_("Unable to mount filesystems."));
512 snprintf(commandstring
, STRING_SIZE
,
513 "/bin/tar -C /harddisk -xvf /cdrom/distro.img --lzma 2>/dev/null");
515 if (runcommandwithprogress(60, 4, title
, commandstring
, INST_FILECOUNT
,
516 _("Installing the system..."))) {
517 errorbox(_("Unable to install the system."));
522 rc
= hw_write_fstab(destination
);
524 fprintf(flog
, "Could not write /etc/fstab\n");
528 /* Save language und local settings */
529 write_lang_configs(shortlangname
);
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."));
538 // Installing bootloader...
539 statuswindow(60, 4, title
, _("Installing the bootloader..."));
541 rc
= hw_install_bootloader(destination
);
543 errorbox(_("Unable to install the bootloader."));
549 /* Serial console ? */
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 ");
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:");
567 /* Set marker that the user has already accepted the gpl */
568 mysystem("/usr/bin/touch /harddisk/var/ipfire/main/gpl_accepted");
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
);
578 // Umount source drive and eject
579 hw_umount(SOURCE_MOUNT_PATH
);
581 snprintf(commandstring
, STRING_SIZE
, "/usr/bin/eject %s", sourcedrive
);
582 mysystem(commandstring
);
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
);
597 fprintf(flog
, "Install program ended.\n");
600 newtWinMessage(title
, _("OK"), _("Press Ok to reboot."));
613 hw_umount_filesystems(destination
, DESTINATION_MOUNT_PATH
);
617 hw_stop_all_raid_arrays();
620 hw_free_disks(selected_disks
);