]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blobdiff - src/installer/main.c
installer: Simplify kernel command line parsing
[people/pmueller/ipfire-2.x.git] / src / installer / main.c
index 513ebf41a2cce2fdc9f2fd1c9e362c9261ef0fe6..c1a61b3457f5a7290b5260673f73354f00f90776 100644 (file)
 
 #include <assert.h>
 #include <errno.h>
+#include <libsmooth.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <sys/mount.h>
 
 #include "hw.h"
-#include "install.h"
 
 // Translation
 #include <libintl.h>
 #define _(x) dgettext("installer", x)
 
 #define INST_FILECOUNT 21000
-#define UNATTENDED_CONF "/cdrom/boot/unattended.conf"
 #define LICENSE_FILE   "/cdrom/COPYING"
+#define SOURCE_TEMPFILE "/tmp/downloaded-image.iso"
 
 extern char url[STRING_SIZE];
 
@@ -225,6 +225,47 @@ static struct lang {
        { NULL, NULL },
 };
 
+static struct config {
+       int unattended;
+       int serial_console;
+       int require_networking;
+} config = {
+       .unattended = 0,
+       .serial_console = 0,
+       .require_networking = 0,
+};
+
+static void parse_command_line(struct config* c) {
+       char cmdline[STRING_SIZE];
+
+       FILE* f = fopen("/proc/cmdline", "r");
+       if (!f)
+               return;
+
+       int r = fread(&cmdline, 1, sizeof(cmdline) - 1, f);
+       if (r > 0) {
+               char* token = strtok(&cmdline, " ");
+
+               while (token) {
+                       // serial console
+                       if (strcmp(token, "console=ttyS0") == 0)
+                               c->serial_console = 1;
+
+                       // enable networking?
+                       else if (strcmp(token, "installer.net") == 0)
+                               c->require_networking = 1;
+
+                       // unattended mode
+                       else if (strcmp(token, "installer.unattended") == 0)
+                               c->unattended = 1;
+
+                       token = strtok(NULL, " ");
+               }
+       }
+
+       fclose(f);
+}
+
 int main(int argc, char *argv[]) {
        struct hw* hw = hw_init();
        const char* logfile = NULL;
@@ -242,14 +283,9 @@ int main(int argc, char *argv[]) {
        char message[STRING_SIZE];
        char title[STRING_SIZE];
        int allok = 0;
-       FILE *handle, *cmdfile, *copying;
+       FILE *handle, *copying;
        char line[STRING_SIZE];
                
-       int unattended = 0;
-       int serialconsole = 0;
-       struct keyvalue *unattendedkv = initkeyvalues();
-       char restore_file[STRING_SIZE] = "";
-
        setlocale (LC_ALL, "");
        sethostname( SNAME , 10);
 
@@ -281,28 +317,18 @@ int main(int argc, char *argv[]) {
 
        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) {
-                   unattended = 1;
-                   runcommandwithstatus("/bin/sleep 10", title, "WARNING: Unattended installation will start in 10 seconds...", NULL);
-               }               
-               // check if we have to patch for serial console
-               if (strstr (line, "console=ttyS0") != NULL) {
-                   serialconsole = 1;
-               }
+       // Parse parameters from the kernel command line
+       parse_command_line(&config);
+
+       if (config.unattended) {
+               splashWindow(title, _("Warning: Unattended installation will start in 10 seconds..."), 10);
        }
 
        // Load common modules
        mysystem(logfile, "/sbin/modprobe vfat"); // USB key
        hw_stop_all_raid_arrays(logfile);
 
-       if (!unattended) {
+       if (!config.unattended) {
                // Language selection
                char* langnames[NUM_LANGS + 1];
 
@@ -314,7 +340,7 @@ int main(int argc, char *argv[]) {
                }
                langnames[NUM_LANGS] = NULL;
 
-           rc = newtWinMenu(_("Language selection"), _("Select the language you wish to use for the installation."),
+               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);
@@ -329,7 +355,7 @@ int main(int argc, char *argv[]) {
        char* helpline = center_string(_("<Tab>/<Alt-Tab> between elements | <Space> selects | <F12> next screen"), screen_cols);
        newtPushHelpLine(helpline);
 
-       if (!unattended) {
+       if (!config.unattended) {
                snprintf(message, sizeof(message),
                        _("Welcome to the %s installation program.\n\n"
                        "Selecting Cancel on any of the following screens will reboot the computer."), NAME);
@@ -339,18 +365,77 @@ int main(int argc, char *argv[]) {
        /* Search for a source drive that holds the right
         * version of the image we are going to install. */
        sourcedrive = hw_find_source_medium(hw);
-
        fprintf(flog, "Source drive: %s\n", sourcedrive);
+
+       /* If we could not find a source drive, we will try
+        * downloading the install image */
        if (!sourcedrive) {
-               newtWinMessage(title, _("OK"), _("No local source media found. Starting download."));
-               runcommandwithstatus("/bin/downloadsource.sh", title, _("Downloading installation image ..."), logfile);
-               if ((handle = fopen("/tmp/source_device", "r")) == NULL) {
-                       errorbox(_("Download error"));
-                       goto EXIT;
+               if (!config.unattended) {
+                       // Show the right message to the user
+                       char reason[STRING_SIZE];
+                       if (config.require_networking) {
+                               snprintf(reason, sizeof(reason),
+                                       _("The installer will now try downloading the installation image."));
+                       } else {
+                               snprintf(reason, sizeof(reason),
+                                       _("No source drive could be found.\n\n"
+                                       "You can try downloading the required installation image."));
+                       }
+                       snprintf(message, sizeof(message), "%s %s", reason,
+                               _("Please make sure to connect your machine to a network and "
+                                "the installer will try connect to acquire an IP address."));
+
+                       rc = newtWinOkCancel(title, message, 55, 12,
+                               _("Download installation image"), _("Cancel"));
+
+                       if (rc != 0)
+                               goto EXIT;
                }
 
-               fgets(sourcedrive, 5, handle);
-               fclose(handle);
+               config.require_networking = 1;
+       }
+
+       // Try starting the networking if we require it
+       if (config.require_networking) {
+               while (1) {
+                       statuswindow(60, 4, title, _("Trying to start networking (DHCP)..."));
+
+                       rc = hw_start_networking(logfile);
+                       newtPopWindow();
+
+                       // Networking was successfully started
+                       if (rc == 0) {
+                               break;
+
+                       // An error happened, ask the user what to do
+                       } else {
+                               rc = newtWinOkCancel(title, _("Networking could not be started "
+                                       "but is required to go on with the installation.\n\n"
+                                       "Please connect your machine to a network with a "
+                                       "DHCP server and retry."), 50, 10, _("Retry"), _("Cancel"));
+
+                               if (rc)
+                                       goto EXIT;
+                       }
+               }
+
+               // Download the image if required
+               while (!sourcedrive) {
+                       snprintf(commandstring, sizeof(commandstring), "/usr/bin/downloadsource.sh %s", SOURCE_TEMPFILE);
+                       runcommandwithstatus(commandstring, title, _("Downloading installation image..."), logfile);
+
+                       FILE* f = fopen(SOURCE_TEMPFILE, "r");
+                       if (f) {
+                               sourcedrive = SOURCE_TEMPFILE;
+                               fclose(f);
+                       } else {
+                               rc = newtWinOkCancel(title, _("The installation image could not be downloaded."),
+                                       60, 8, _("Retry"), _("Cancel"));
+
+                               if (rc)
+                                       goto EXIT;
+                       }
+               }
        }
 
        assert(sourcedrive);
@@ -362,15 +447,7 @@ int main(int argc, char *argv[]) {
                exit(1);
        }
 
-       /* load unattended configuration */
-       if (unattended) {
-           fprintf(flog, "unattended: Reading unattended.conf\n");
-
-           (void) readkeyvalues(unattendedkv, UNATTENDED_CONF);
-           findkey(unattendedkv, "RESTORE_FILE", restore_file);
-       }
-
-       if (!unattended) {
+       if (!config.unattended) {
                // Read the license file.
                if (!(copying = fopen(LICENSE_FILE, "r"))) {
                        sprintf(discl_msg, "Could not open license file: %s\n", LICENSE_FILE);
@@ -406,8 +483,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) || (config.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
@@ -442,6 +521,11 @@ int main(int argc, char *argv[]) {
                        }
                }
 
+               // Don't print the auto-selected harddisk setup in
+               // unattended mode.
+               if (config.unattended)
+                       break;
+
                num_selected_disks = hw_count_disks(selected_disks);
 
                if (num_selected_disks == 1) {
@@ -497,7 +581,7 @@ int main(int argc, char *argv[]) {
        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) {
+       if (!config.unattended && !*destination->part_swap) {
                rc = newtWinChoice(title, _("OK"), _("Cancel"),
                        _("Your harddisk is very small, but you can continue without a swap partition."));
 
@@ -506,7 +590,7 @@ int main(int argc, char *argv[]) {
        }
 
        // Filesystem selection
-       if (!unattended) {
+       if (!config.unattended) {
                struct filesystems {
                        int fstype;
                        const char* description;
@@ -613,7 +697,7 @@ int main(int argc, char *argv[]) {
        statuswindow(60, 4, title, _("Installing the bootloader..."));
 
        /* Serial console ? */
-       if (serialconsole) {
+       if (config.serial_console) {
                /* grub */
                FILE* f = fopen(DESTINATION_MOUNT_PATH "/etc/default/grub", "a");
                if (!f) {
@@ -649,11 +733,24 @@ int main(int argc, char *argv[]) {
        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(logfile, commandstring);
+       char* backup_file = hw_find_backup_file(logfile, SOURCE_MOUNT_PATH);
+       if (backup_file) {
+               rc = 0;
+               if (!config.unattended) {
+                       rc = newtWinOkCancel(title, _("A backup file has been found on the installation image.\n\n"
+                               "Do you want to restore the backup?"), 50, 10, _("Yes"), _("No"));
+               }
+
+               if (rc == 0) {
+                       rc = hw_restore_backup(logfile, backup_file, DESTINATION_MOUNT_PATH);
+
+                       if (rc) {
+                               errorbox(_("An error occured when the backup file was restored."));
+                               goto EXIT;
+                       }
+               }
+
+               free(backup_file);
        }
 
        // Umount the destination drive
@@ -669,7 +766,7 @@ int main(int argc, char *argv[]) {
        snprintf(commandstring, STRING_SIZE, "/usr/bin/eject %s", sourcedrive);
        mysystem(logfile, commandstring);
 
-       if (!unattended) {
+       if (!config.unattended) {
                snprintf(message, sizeof(message), _(
                        "%s was successfully installed!\n\n"
                        "Please remove any installation mediums from this system and hit the reboot button. "