]> git.ipfire.org Git - ipfire-2.x.git/blobdiff - src/installer/main.c
installer: Enable new partitioning code to be run in unattended mode
[ipfire-2.x.git] / src / installer / main.c
index e32718b924813285a588b59b7aba9e5b2cd22600..f197dc351c39a054ba1099717856769d9af4d6cc 100644 (file)
@@ -10,6 +10,7 @@
 
 #include <assert.h>
 #include <errno.h>
+#include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <sys/mount.h>
 #define UNATTENDED_CONF "/cdrom/boot/unattended.conf"
 #define LICENSE_FILE   "/cdrom/COPYING"
 
-FILE *flog = NULL;
-char *mylog;
-
-char **ctr;
-
 extern char url[STRING_SIZE];
 
 static int newtChecklist(const char* title, const char* message,
@@ -115,8 +111,11 @@ static int newtWinOkCancel(const char* title, const char* message, int width, in
        newtTextboxSetText(textbox, message);
        newtFormAddComponent(form, textbox);
 
-       newtComponent btn_ok = newtButton((width - 16) / 3, height - 4, btn_txt_ok);
-       newtComponent btn_cancel = newtButton((width - 16) / 3 * 2 + 9, height - 4,
+       unsigned int btn_width_ok = strlen(btn_txt_ok);
+       unsigned int btn_width_cancel = strlen(btn_txt_cancel);
+
+       newtComponent btn_ok = newtButton((width / 3) - (btn_width_ok / 2) - 2, height - 4, btn_txt_ok);
+       newtComponent btn_cancel = newtButton((width * 2 / 3) - (btn_width_cancel / 2) - 2, height - 4,
                btn_txt_cancel);
 
        newtFormAddComponents(form, btn_ok, btn_cancel, NULL);
@@ -176,18 +175,70 @@ int write_lang_configs(const char *lang) {
        return 1;
 }
 
+static char* get_system_release() {
+       char system_release[STRING_SIZE] = "\0";
+
+       FILE* f = fopen("/etc/system-release", "r");
+       if (f) {
+               fgets(system_release, sizeof(system_release), f);
+               fclose(f);
+       }
+
+       return strdup(system_release);
+}
+
+static char* center_string(const char* str, int width) {
+       unsigned int str_len = strlen(str);
+
+       unsigned int indent_length = (width - str_len) / 2;
+       char indent[indent_length + 1];
+
+       for (unsigned int i = 0; i < indent_length; i++) {
+               indent[i] = ' ';
+       }
+       indent[indent_length] = '\0';
+
+       char* string = NULL;
+       if (asprintf(&string, "%s%s", indent, str) < 0)
+               return NULL;
+
+       return string;
+}
+
+#define DEFAULT_LANG "English"
+#define NUM_LANGS 8
+
+static struct lang {
+       const char* code;
+       char* name;
+} languages[NUM_LANGS + 1] = {
+       { "da.utf8",    "Danish (Dansk)" },
+       { "nl_NL.utf8", "Dutch (Nederlands)" },
+       { "en_US.utf8", "English" },
+       { "fr_FR.utf8", "French (Français)" },
+       { "de_DE.utf8", "German (Deutsch)" },
+       { "pl_PL.utf8", "Polish (Polski)" },
+       { "pt_BR.utf8", "Portuguese (Brasil)" },
+       { "ru_RU.utf8", "Russian (Русский)" },
+       { "es_ES.utf8", "Spanish (Español)" },
+       { "tr_TR.utf8", "Turkish (Türkçe)" },
+       { NULL, NULL },
+};
+
 int main(int argc, char *argv[]) {
        struct hw* hw = hw_init();
+       const char* logfile = NULL;
+
+       // Read /etc/system-release
+       char* system_release = get_system_release();
 
        char discl_msg[40000] = "Disclaimer\n";
 
-       char *langnames[] = { "Deutsch", "English", "Français", "Español", "Nederlands", "Polski", "Русский", "Türkçe", NULL };
-       char *shortlangnames[] = { "de", "en", "fr", "es", "nl", "pl", "ru", "tr", NULL };
        char* sourcedrive = NULL;
        int rc = 0;
        char commandstring[STRING_SIZE];
        int choice;
-       char shortlangname[10];
+       char language[STRING_SIZE];
        char message[STRING_SIZE];
        char title[STRING_SIZE];
        int allok = 0;
@@ -203,35 +254,43 @@ int main(int argc, char *argv[]) {
        sethostname( SNAME , 10);
 
        /* Log file/terminal stuff. */
-       if (argc >= 2)
-       {               
-               if (!(flog = fopen(argv[1], "w+")))
+       FILE* flog = NULL;
+       if (argc >= 2) {
+               logfile = argv[1];
+
+               if (!(flog = fopen(logfile, "w+")))
                        return 0;
-       }
-       else
+       } else {
                return 0;
-       
-       mylog = argv[1];
-       
+       }
+
        fprintf(flog, "Install program started.\n");
                
        newtInit();
        newtCls();
 
-       newtDrawRootText(14, 0, NAME " " VERSION " - " SLOGAN );
-       sprintf (title, "%s %s - %s", NAME, VERSION, SLOGAN);
+       // Determine the size of the screen
+       int screen_cols = 0;
+       int screen_rows = 0;
+
+       newtGetScreenSize(&screen_cols, &screen_rows);
 
-       if (! (cmdfile = fopen("/proc/cmdline", "r")))
-       {
+       // Draw title
+       char* roottext = center_string(system_release, screen_cols);
+       newtDrawRootText(0, 0, roottext);
+
+       snprintf(title, sizeof(title), "%s - %s", NAME, SLOGAN);
+
+       if (! (cmdfile = fopen("/proc/cmdline", "r"))) {
                fprintf(flog, "Couldn't open commandline: /proc/cmdline\n");
        } else {
                fgets(line, STRING_SIZE, cmdfile);
-               
+
                // check if we have to make an unattended install
-               if (strstr (line, "unattended") != NULL) {
+               if (strstr(line, "installer.unattended") != NULL) {
+                   splashWindow(title, _("Warning: Unattended installation will start in 10 seconds..."), 10);
                    unattended = 1;
-                   runcommandwithstatus("/bin/sleep 10", title, "WARNING: Unattended installation will start in 10 seconds...");
-               }               
+               }
                // check if we have to patch for serial console
                if (strstr (line, "console=ttyS0") != NULL) {
                    serialconsole = 1;
@@ -239,30 +298,39 @@ int main(int argc, char *argv[]) {
        }
 
        // Load common modules
-       mysystem("/sbin/modprobe vfat"); // USB key
-       hw_stop_all_raid_arrays();
-
-       /* German is the default */
-       for (choice = 0; langnames[choice]; choice++)
-       {
-               if (strcmp(langnames[choice], "English") == 0)
-                       break;
-       }
-       if (!langnames[choice])
-               goto EXIT;
+       mysystem(logfile, "/sbin/modprobe vfat"); // USB key
+       hw_stop_all_raid_arrays(logfile);
 
        if (!unattended) {
-           rc = newtWinMenu("Language selection", "Select the language you wish to use for the " NAME ".", 50, 5, 5, 8,
-                   langnames, &choice, "Ok", NULL);
-       }
+               // Language selection
+               char* langnames[NUM_LANGS + 1];
+
+               for (unsigned int i = 0; i < NUM_LANGS; i++) {
+                       if (strcmp(languages[i].name, DEFAULT_LANG) == 0)
+                               choice = i;
+
+                       langnames[i] = languages[i].name;
+               }
+               langnames[NUM_LANGS] = NULL;
+
+               rc = newtWinMenu(_("Language selection"), _("Select the language you wish to use for the installation."),
+                       50, 5, 5, 8, langnames, &choice, _("OK"), NULL);
+
+               assert(choice <= NUM_LANGS);
 
-       setlocale(LC_ALL, shortlangnames[choice]);
+               fprintf(flog, "Selected language: %s (%s)\n", languages[choice].name, languages[choice].code);
+               snprintf(language, sizeof(language), languages[choice].code);
+
+               setenv("LANGUAGE", language, 1);
+               setlocale(LC_ALL, language);
+       }
 
-       newtPushHelpLine(_("<Tab>/<Alt-Tab> between elements | <Space> selects | <F12> next screen"));
+       char* helpline = center_string(_("<Tab>/<Alt-Tab> between elements | <Space> selects | <F12> next screen"), screen_cols);
+       newtPushHelpLine(helpline);
 
        if (!unattended) {
                snprintf(message, sizeof(message),
-                       _("Welcome to the %s installation program. "
+                       _("Welcome to the %s installation program.\n\n"
                        "Selecting Cancel on any of the following screens will reboot the computer."), NAME);
                newtWinMessage(title, _("Start installation"), message);
        }
@@ -274,7 +342,7 @@ int main(int argc, char *argv[]) {
        fprintf(flog, "Source drive: %s\n", sourcedrive);
        if (!sourcedrive) {
                newtWinMessage(title, _("OK"), _("No local source media found. Starting download."));
-               runcommandwithstatus("/bin/downloadsource.sh", title, _("Downloading installation image ..."));
+               runcommandwithstatus("/bin/downloadsource.sh", title, _("Downloading installation image ..."), logfile);
                if ((handle = fopen("/tmp/source_device", "r")) == NULL) {
                        errorbox(_("Download error"));
                        goto EXIT;
@@ -310,7 +378,7 @@ int main(int argc, char *argv[]) {
                        fread(discl_msg, 1, 40000, copying);
                        fclose(copying);
 
-                       if (newtLicenseBox(title, discl_msg, 75, 20)) {
+                       if (newtLicenseBox(_("License Agreement"), discl_msg, 75, 20)) {
                                errorbox(_("License not accepted!"));
 
                                goto EXIT;
@@ -321,7 +389,7 @@ int main(int argc, char *argv[]) {
        int part_type = HW_PART_TYPE_NORMAL;
 
        // Scan for disks to install on.
-       struct hw_disk** disks = hw_find_disks(hw);
+       struct hw_disk** disks = hw_find_disks(hw, sourcedrive);
 
        struct hw_disk** selected_disks = NULL;
        unsigned int num_selected_disks = 0;
@@ -337,8 +405,10 @@ int main(int argc, char *argv[]) {
                        goto EXIT;
 
                // exactly one disk has been found
-               } else if (num_disks == 1) {
-                       selected_disks = hw_select_disks(disks, NULL);
+               // or if we are running in unattended mode, we will select
+               // the first disk and go with that one
+               } else if ((num_disks == 1) || (unattended && num_disks >= 1)) {
+                       selected_disks = hw_select_first_disk(disks);
 
                // more than one usable disk has been found and
                // the user needs to choose what to do with them
@@ -373,6 +443,11 @@ int main(int argc, char *argv[]) {
                        }
                }
 
+               // Don't print the auto-selected harddisk setup in
+               // unattended mode.
+               if (unattended)
+                       break;
+
                num_selected_disks = hw_count_disks(selected_disks);
 
                if (num_selected_disks == 1) {
@@ -388,7 +463,7 @@ int main(int argc, char *argv[]) {
                } else if (num_selected_disks == 2) {
                        snprintf(message, sizeof(message),
                                _("The installation program will now set up a RAID configuration on the selected harddisks:\n\n  %s\n  %s\n\n"
-                               "Do you agree to continue?"), (*selected_disks)->description, (*selected_disks + 1)->description);
+                               "Do you agree to continue?"), selected_disks[0]->description, selected_disks[1]->description);
                        rc = newtWinOkCancel(_("RAID Setup"), message, 50, 14,
                                _("Delete all data"), _("Cancel"));
 
@@ -400,7 +475,8 @@ int main(int argc, char *argv[]) {
 
                // Currently not supported
                } else {
-                       errorbox(_("You disk configuration is currently not supported."));
+                       errorbox(_("Your disk configuration is currently not supported."));
+                       fprintf(flog, "Num disks selected: %d\n", num_selected_disks);
                }
 
                if (selected_disks) {
@@ -424,11 +500,12 @@ int main(int argc, char *argv[]) {
        fprintf(flog, "  swap   : %s (%lluMB)\n", destination->part_swap, BYTES2MB(destination->size_swap));
        fprintf(flog, "  root   : %s (%lluMB)\n", destination->part_root, BYTES2MB(destination->size_root));
        fprintf(flog, "  data   : %s (%lluMB)\n", destination->part_data, BYTES2MB(destination->size_data));
+       fprintf(flog, "Memory   : %lluMB\n", BYTES2MB(hw_memory()));
 
        // Warn the user if there is not enough space to create a swap partition
        if (!unattended && !*destination->part_swap) {
                rc = newtWinChoice(title, _("OK"), _("Cancel"),
-                       _("Your harddisk is very small, but you can continue with an very small swap. (Use with caution)."));
+                       _("Your harddisk is very small, but you can continue without a swap partition."));
 
                if (rc != 1)
                        goto EXIT;
@@ -460,30 +537,33 @@ int main(int argc, char *argv[]) {
                rc = newtWinMenu(_("Filesystem Selection"), _("Please choose your filesystem:"),
                        50, 5, 5, 6, fs_names, &fs_choice, _("OK"), _("Cancel"), NULL);
 
-               if (rc == 0)
-                       destination->filesystem = filesystems[fs_choice].fstype;
-
-               else
+               if (rc == 2)
                        goto EXIT;
+
+               destination->filesystem = filesystems[fs_choice].fstype;
        }
 
        // Setting up RAID if needed.
        if (destination->is_raid) {
                statuswindow(60, 4, title, _("Building RAID..."));
 
-               rc = hw_setup_raid(destination);
+               rc = hw_setup_raid(destination, logfile);
                if (rc) {
                        errorbox(_("Unable to build the RAID."));
                        goto EXIT;
                }
 
                newtPopWindow();
+       } else {
+               // We will have to destroy all RAID setups that may have
+               // been on the devices that we want to use now.
+               hw_destroy_raid_superblocks(destination, logfile);
        }
 
        // Execute the partitioning...
        statuswindow(60, 4, title, _("Partitioning disk..."));
 
-       rc = hw_create_partitions(destination);
+       rc = hw_create_partitions(destination, logfile);
        if (rc) {
                errorbox(_("Unable to partition the disk."));
                goto EXIT;
@@ -494,7 +574,7 @@ int main(int argc, char *argv[]) {
        // Execute the formatting...
        statuswindow(60, 4, title, _("Creating filesystems..."));
 
-       rc = hw_create_filesystems(destination);
+       rc = hw_create_filesystems(destination, logfile);
        if (rc) {
                errorbox(_("Unable to create filesystems."));
                goto EXIT;
@@ -513,7 +593,7 @@ int main(int argc, char *argv[]) {
                "/bin/tar -C /harddisk  -xvf /cdrom/distro.img --lzma 2>/dev/null");
 
        if (runcommandwithprogress(60, 4, title, commandstring, INST_FILECOUNT,
-                       _("Installing the system..."))) {
+                       _("Installing the system..."), logfile)) {
                errorbox(_("Unable to install the system."));
                goto EXIT;
        }
@@ -526,11 +606,11 @@ int main(int argc, char *argv[]) {
        }
 
        /* Save language und local settings */
-       write_lang_configs(shortlangname);
+       write_lang_configs(language);
 
        /* Build cache lang file */
        snprintf(commandstring, STRING_SIZE, "/usr/sbin/chroot /harddisk /usr/bin/perl -e \"require '" CONFIG_ROOT "/lang.pl'; &Lang::BuildCacheLang\"");
-       if (runcommandwithstatus(commandstring, title, _("Installing the language cache..."))) {
+       if (runcommandwithstatus(commandstring, title, _("Installing the language cache..."), logfile)) {
                errorbox(_("Unable to install the language cache."));
                goto EXIT;
        }
@@ -538,23 +618,22 @@ int main(int argc, char *argv[]) {
        // Installing bootloader...
        statuswindow(60, 4, title, _("Installing the bootloader..."));
 
-       rc = hw_install_bootloader(destination);
-       if (rc) {
-               errorbox(_("Unable to install the bootloader."));
-               goto EXIT;
-       }
-
-       newtPopWindow();
-
        /* Serial console ? */
        if (serialconsole) {
                /* grub */
-               replace("/harddisk/boot/grub/grub.conf", "splashimage", "#splashimage");
-               replace("/harddisk/boot/grub/grub.conf", "#serial", "serial");
-               replace("/harddisk/boot/grub/grub.conf", "#terminal", "terminal");
-               replace("/harddisk/boot/grub/grub.conf", " panic=10 ", " console=ttyS0,115200n8 panic=10 ");
+               FILE* f = fopen(DESTINATION_MOUNT_PATH "/etc/default/grub", "a");
+               if (!f) {
+                       errorbox(_("Unable to open /etc/default/grub for writing."));
+                       goto EXIT;
+               }
+
+               fprintf(f, "GRUB_TERMINAL=\"serial\"\n");
+               fprintf(f, "GRUB_SERIAL_COMMAND=\"serial --unit=0 --speed=%d\"\n", SERIAL_BAUDRATE);
+               fclose(f);
+
+               replace(DESTINATION_MOUNT_PATH "/etc/default/grub", "panic=10", "panic=10 console=ttyS0,115200n8");
 
-               /*inittab*/
+               /* inittab */
                replace("/harddisk/etc/inittab", "1:2345:respawn:", "#1:2345:respawn:");
                replace("/harddisk/etc/inittab", "2:2345:respawn:", "#2:2345:respawn:");
                replace("/harddisk/etc/inittab", "3:2345:respawn:", "#3:2345:respawn:");
@@ -564,57 +643,69 @@ int main(int argc, char *argv[]) {
                replace("/harddisk/etc/inittab", "#7:2345:respawn:", "7:2345:respawn:");
        }
 
+       rc = hw_install_bootloader(destination, logfile);
+       if (rc) {
+               errorbox(_("Unable to install the bootloader."));
+               goto EXIT;
+       }
+
+       newtPopWindow();
+
        /* Set marker that the user has already accepted the gpl */
-       mysystem("/usr/bin/touch /harddisk/var/ipfire/main/gpl_accepted");
+       mysystem(logfile, "/usr/bin/touch /harddisk/var/ipfire/main/gpl_accepted");
 
        /* Copy restore file from cdrom */
        if (unattended && (strlen(restore_file) > 0)) {
                fprintf(flog, "unattended: Copy restore file\n");
                snprintf(commandstring, STRING_SIZE, 
                        "cp /cdrom/%s /harddisk/var/ipfire/backup", restore_file);
-               mysystem(commandstring);
+               mysystem(logfile, commandstring);
        }
 
+       // Umount the destination drive
+       hw_umount_filesystems(destination, DESTINATION_MOUNT_PATH);
+
+       // Stop the RAID array if we are using RAID
+       if (destination->is_raid)
+               hw_stop_all_raid_arrays(logfile);
+
        // Umount source drive and eject
        hw_umount(SOURCE_MOUNT_PATH);
 
        snprintf(commandstring, STRING_SIZE, "/usr/bin/eject %s", sourcedrive);
-       mysystem(commandstring);
+       mysystem(logfile, commandstring);
 
        if (!unattended) {
-               snprintf(message, sizeof(message), _("%s was successfully installed. "
-                       "Please remove any installation mediums from this system. "
-                       "Setup will now run where you may configure networking and the system passwords. "
-                       "After Setup has been completed, you should point your web browser at https://%s:444 "
-                       "(or whatever you name your %s), and configure dialup networking (if required) and "
-                       "remote access."), NAME, SNAME, NAME);
+               snprintf(message, sizeof(message), _(
+                       "%s was successfully installed!\n\n"
+                       "Please remove any installation mediums from this system and hit the reboot button. "
+                       "Once the system has restarted you will be asked to setup networking and system passwords. "
+                       "After that, you should point your web browser at https://%s:444 (or what ever you name "
+                       "your %s) for the web configuration console."), NAME, SNAME, NAME);
                newtWinMessage(_("Congratulations!"), _("Reboot"), message);
        }
 
        allok = 1;
 
 EXIT:
-       fprintf(flog, "Install program ended.\n");      
-
-       if (!(allok))
-               newtWinMessage(title, _("OK"), _("Press Ok to reboot."));
+       fprintf(flog, "Install program ended.\n");
+       fflush(flog);
+       fclose(flog);
 
-       if (allok) {
-               fflush(flog);
-               fclose(flog);
-       }
+       if (!allok)
+               newtWinMessage(title, _("OK"), _("Setup has failed. Press Ok to reboot."));
 
        newtFinished();
 
        // Free resources
-       free(sourcedrive);
+       free(system_release);
+       free(roottext);
+       free(helpline);
 
-       if (destination) {
-               hw_umount_filesystems(destination, DESTINATION_MOUNT_PATH);
-               free(destination);
-       }
+       free(sourcedrive);
+       free(destination);
 
-       hw_stop_all_raid_arrays();
+       hw_stop_all_raid_arrays(logfile);
 
        if (selected_disks)
                hw_free_disks(selected_disks);