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
16 #include <sys/mount.h>
23 #define _(x) dgettext("installer", x)
25 #define INST_FILECOUNT 21000
26 #define UNATTENDED_CONF "/cdrom/boot/unattended.conf"
27 #define LICENSE_FILE "/cdrom/COPYING"
34 extern char url
[STRING_SIZE
];
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
) {
40 const int list_height
= 4;
42 char cbstates
[num_entries
];
44 for (unsigned int i
= 0; i
< num_entries
; i
++) {
45 cbstates
[i
] = states
[i
] ? '*' : ' ';
48 newtCenteredWindow(width
, height
, title
);
50 newtComponent textbox
= newtTextbox(1, 1, width
- 2, height
- 6 - list_height
,
52 newtTextboxSetText(textbox
, message
);
54 int top
= newtTextboxGetNumLines(textbox
) + 2;
56 newtComponent form
= newtForm(NULL
, NULL
, 0);
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
);
64 newtFormAddComponent(form
, sb
);
67 newtComponent subform
= newtForm(sb
, NULL
, 0);
68 newtFormSetBackground(subform
, NEWT_COLORSET_CHECKBOX
);
70 newtFormSetHeight(subform
, list_height
);
71 newtFormSetWidth(subform
, width
- 10);
73 for (unsigned int i
= 0; i
< num_entries
; i
++) {
74 newtComponent cb
= newtCheckbox(4, top
+ i
, entries
[i
], cbstates
[i
],
77 newtFormAddComponent(subform
, cb
);
80 newtFormAddComponents(form
, textbox
, subform
, NULL
);
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
);
86 newtComponent answer
= newtRunForm(form
);
88 if ((answer
== NULL
) || (answer
== btn_cancel
)) {
93 for (unsigned int i
= 0; i
< num_entries
; i
++) {
94 states
[i
] = (cbstates
[i
] != ' ');
101 newtFormDestroy(form
);
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
) {
111 newtCenteredWindow(width
, height
, title
);
113 newtComponent form
= newtForm(NULL
, NULL
, 0);
115 newtComponent textbox
= newtTextbox(1, 1, width
- 2, height
- 6, NEWT_FLAG_WRAP
);
116 newtTextboxSetText(textbox
, message
);
117 newtFormAddComponent(form
, textbox
);
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,
123 newtFormAddComponents(form
, btn_ok
, btn_cancel
, NULL
);
125 newtComponent answer
= newtRunForm(form
);
127 if (answer
== btn_ok
) {
131 newtFormDestroy(form
);
137 static int newtLicenseBox(const char* title
, const char* text
, int width
, int height
) {
140 newtCenteredWindow(width
, height
, title
);
142 newtComponent form
= newtForm(NULL
, NULL
, 0);
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
);
150 newtComponent checkbox
= newtCheckbox(3, height
- 3, _("I accept this license"),
153 newtComponent btn
= newtButton(width
- 15, height
- 4, _("OK"));
155 newtFormAddComponents(form
, checkbox
, btn
, NULL
);
157 newtComponent answer
= newtRunForm(form
);
158 if (answer
== btn
&& choice
== '*')
161 newtFormDestroy(form
);
167 int write_lang_configs(const char *lang
) {
168 struct keyvalue
*kv
= initkeyvalues();
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");
180 static char* get_system_release() {
181 char system_release
[STRING_SIZE
] = "\0";
183 FILE* f
= fopen("/etc/system-release", "r");
185 fgets(system_release
, sizeof(system_release
), f
);
189 return strdup(system_release
);
192 static char* center_string(const char* str
, int width
) {
193 unsigned int str_len
= strlen(str
);
195 unsigned int indent_length
= (width
- str_len
) / 2;
196 char indent
[indent_length
+ 1];
198 for (unsigned int i
= 0; i
< indent_length
; i
++) {
201 indent
[indent_length
] = '\0';
204 if (asprintf(&string
, "%s%s", indent
, str
) < 0)
210 int main(int argc
, char *argv
[]) {
211 struct hw
* hw
= hw_init();
213 // Read /etc/system-release
214 char* system_release
= get_system_release();
216 char discl_msg
[40000] = "Disclaimer\n";
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
;
222 char commandstring
[STRING_SIZE
];
224 char shortlangname
[10];
225 char message
[STRING_SIZE
];
226 char title
[STRING_SIZE
];
228 FILE *handle
, *cmdfile
, *copying
;
229 char line
[STRING_SIZE
];
232 int serialconsole
= 0;
233 struct keyvalue
*unattendedkv
= initkeyvalues();
234 char restore_file
[STRING_SIZE
] = "";
236 setlocale (LC_ALL
, "");
237 sethostname( SNAME
, 10);
239 /* Log file/terminal stuff. */
242 if (!(flog
= fopen(argv
[1], "w+")))
250 fprintf(flog
, "Install program started.\n");
255 // Determine the size of the screen
259 newtGetScreenSize(&screen_cols
, &screen_rows
);
262 char* roottext
= center_string(system_release
, screen_cols
);
263 newtDrawRootText(0, 0, roottext
);
265 sprintf (title
, "%s %s - %s", NAME
, VERSION
, SLOGAN
);
267 if (! (cmdfile
= fopen("/proc/cmdline", "r")))
269 fprintf(flog
, "Couldn't open commandline: /proc/cmdline\n");
271 fgets(line
, STRING_SIZE
, cmdfile
);
273 // check if we have to make an unattended install
274 if (strstr (line
, "unattended") != NULL
) {
276 runcommandwithstatus("/bin/sleep 10", title
, "WARNING: Unattended installation will start in 10 seconds...");
278 // check if we have to patch for serial console
279 if (strstr (line
, "console=ttyS0") != NULL
) {
284 // Load common modules
285 mysystem("/sbin/modprobe vfat"); // USB key
286 hw_stop_all_raid_arrays();
288 /* German is the default */
289 for (choice
= 0; langnames
[choice
]; choice
++)
291 if (strcmp(langnames
[choice
], "English") == 0)
294 if (!langnames
[choice
])
298 rc
= newtWinMenu("Language selection", "Select the language you wish to use for the " NAME
".", 50, 5, 5, 8,
299 langnames
, &choice
, "Ok", NULL
);
302 setlocale(LC_ALL
, shortlangnames
[choice
]);
304 char* helpline
= center_string(_("<Tab>/<Alt-Tab> between elements | <Space> selects | <F12> next screen"), screen_cols
);
305 newtPushHelpLine(helpline
);
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
);
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
);
318 fprintf(flog
, "Source drive: %s\n", 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"));
327 fgets(sourcedrive
, 5, handle
);
333 int r
= hw_mount(sourcedrive
, SOURCE_MOUNT_PATH
, "iso9660", MS_RDONLY
);
335 fprintf(flog
, "Could not mount %s to %s\n", sourcedrive
, SOURCE_MOUNT_PATH
);
336 fprintf(flog
, strerror(errno
));
340 /* load unattended configuration */
342 fprintf(flog
, "unattended: Reading unattended.conf\n");
344 (void) readkeyvalues(unattendedkv
, UNATTENDED_CONF
);
345 findkey(unattendedkv
, "RESTORE_FILE", restore_file
);
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
);
354 fread(discl_msg
, 1, 40000, copying
);
357 if (newtLicenseBox(title
, discl_msg
, 75, 20)) {
358 errorbox(_("License not accepted!"));
365 int part_type
= HW_PART_TYPE_NORMAL
;
367 // Scan for disks to install on.
368 struct hw_disk
** disks
= hw_find_disks(hw
);
370 struct hw_disk
** selected_disks
= NULL
;
371 unsigned int num_selected_disks
= 0;
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
);
378 // no harddisks found
379 if (num_disks
== 0) {
380 errorbox(_("No hard disk found."));
383 // exactly one disk has been found
384 } else if (num_disks
== 1) {
385 selected_disks
= hw_select_disks(disks
, NULL
);
387 // more than one usable disk has been found and
388 // the user needs to choose what to do with them
390 const char* disk_names
[num_disks
];
391 int disk_selection
[num_disks
];
393 for (unsigned int i
= 0; i
< num_disks
; i
++) {
394 disk_names
[i
] = &disks
[i
]->description
;
395 disk_selection
[i
] = 0;
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
);
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."));
415 selected_disks
= hw_select_disks(disks
, disk_selection
);
420 num_selected_disks
= hw_count_disks(selected_disks
);
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"));
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"));
440 part_type
= HW_PART_TYPE_RAID1
;
445 // Currently not supported
447 errorbox(_("You disk configuration is currently not supported."));
450 if (selected_disks
) {
451 hw_free_disks(selected_disks
);
452 selected_disks
= NULL
;
456 hw_free_disks(disks
);
458 struct hw_destination
* destination
= hw_make_destination(part_type
, selected_disks
);
461 errorbox(_("Your harddisk is too small."));
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
));
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)."));
481 // Filesystem selection
485 const char* description
;
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") },
493 unsigned int num_filesystems
= sizeof(filesystems
) / sizeof(*filesystems
);
495 char* fs_names
[num_filesystems
];
497 for (unsigned int i
= 0; i
< num_filesystems
; i
++) {
498 if (HW_FS_DEFAULT
== filesystems
[i
].fstype
)
501 fs_names
[i
] = filesystems
[i
].description
;
504 rc
= newtWinMenu(_("Filesystem Selection"), _("Please choose your filesystem:"),
505 50, 5, 5, 6, fs_names
, &fs_choice
, _("OK"), _("Cancel"), NULL
);
510 destination
->filesystem
= filesystems
[fs_choice
].fstype
;
513 // Setting up RAID if needed.
514 if (destination
->is_raid
) {
515 statuswindow(60, 4, title
, _("Building RAID..."));
517 rc
= hw_setup_raid(destination
);
519 errorbox(_("Unable to build the RAID."));
526 // Execute the partitioning...
527 statuswindow(60, 4, title
, _("Partitioning disk..."));
529 rc
= hw_create_partitions(destination
);
531 errorbox(_("Unable to partition the disk."));
537 // Execute the formatting...
538 statuswindow(60, 4, title
, _("Creating filesystems..."));
540 rc
= hw_create_filesystems(destination
);
542 errorbox(_("Unable to create filesystems."));
546 rc
= hw_mount_filesystems(destination
, DESTINATION_MOUNT_PATH
);
548 errorbox(_("Unable to mount filesystems."));
555 snprintf(commandstring
, STRING_SIZE
,
556 "/bin/tar -C /harddisk -xvf /cdrom/distro.img --lzma 2>/dev/null");
558 if (runcommandwithprogress(60, 4, title
, commandstring
, INST_FILECOUNT
,
559 _("Installing the system..."))) {
560 errorbox(_("Unable to install the system."));
565 rc
= hw_write_fstab(destination
);
567 fprintf(flog
, "Could not write /etc/fstab\n");
571 /* Save language und local settings */
572 write_lang_configs(shortlangname
);
574 /* Build cache lang file */
575 snprintf(commandstring
, STRING_SIZE
, "/usr/sbin/chroot /harddisk /usr/bin/perl -e \"require '" CONFIG_ROOT
"/lang.pl'; &Lang::BuildCacheLang\"");
576 if (runcommandwithstatus(commandstring
, title
, _("Installing the language cache..."))) {
577 errorbox(_("Unable to install the language cache."));
581 // Installing bootloader...
582 statuswindow(60, 4, title
, _("Installing the bootloader..."));
584 rc
= hw_install_bootloader(destination
);
586 errorbox(_("Unable to install the bootloader."));
592 /* Serial console ? */
595 replace("/harddisk/boot/grub/grub.conf", "splashimage", "#splashimage");
596 replace("/harddisk/boot/grub/grub.conf", "#serial", "serial");
597 replace("/harddisk/boot/grub/grub.conf", "#terminal", "terminal");
598 replace("/harddisk/boot/grub/grub.conf", " panic=10 ", " console=ttyS0,115200n8 panic=10 ");
601 replace("/harddisk/etc/inittab", "1:2345:respawn:", "#1:2345:respawn:");
602 replace("/harddisk/etc/inittab", "2:2345:respawn:", "#2:2345:respawn:");
603 replace("/harddisk/etc/inittab", "3:2345:respawn:", "#3:2345:respawn:");
604 replace("/harddisk/etc/inittab", "4:2345:respawn:", "#4:2345:respawn:");
605 replace("/harddisk/etc/inittab", "5:2345:respawn:", "#5:2345:respawn:");
606 replace("/harddisk/etc/inittab", "6:2345:respawn:", "#6:2345:respawn:");
607 replace("/harddisk/etc/inittab", "#7:2345:respawn:", "7:2345:respawn:");
610 /* Set marker that the user has already accepted the gpl */
611 mysystem("/usr/bin/touch /harddisk/var/ipfire/main/gpl_accepted");
613 /* Copy restore file from cdrom */
614 if (unattended
&& (strlen(restore_file
) > 0)) {
615 fprintf(flog
, "unattended: Copy restore file\n");
616 snprintf(commandstring
, STRING_SIZE
,
617 "cp /cdrom/%s /harddisk/var/ipfire/backup", restore_file
);
618 mysystem(commandstring
);
621 // Umount source drive and eject
622 hw_umount(SOURCE_MOUNT_PATH
);
624 snprintf(commandstring
, STRING_SIZE
, "/usr/bin/eject %s", sourcedrive
);
625 mysystem(commandstring
);
628 snprintf(message
, sizeof(message
), _("%s was successfully installed. "
629 "Please remove any installation mediums from this system. "
630 "Setup will now run where you may configure networking and the system passwords. "
631 "After Setup has been completed, you should point your web browser at https://%s:444 "
632 "(or whatever you name your %s), and configure dialup networking (if required) and "
633 "remote access."), NAME
, SNAME
, NAME
);
634 newtWinMessage(_("Congratulations!"), _("Reboot"), message
);
640 fprintf(flog
, "Install program ended.\n");
643 newtWinMessage(title
, _("OK"), _("Press Ok to reboot."));
653 free(system_release
);
660 hw_umount_filesystems(destination
, DESTINATION_MOUNT_PATH
);
664 hw_stop_all_raid_arrays();
667 hw_free_disks(selected_disks
);