]> git.ipfire.org Git - ipfire-2.x.git/blobdiff - src/installer/main.c
installer: Implement option to run a postinstall script in the installer
[ipfire-2.x.git] / src / installer / main.c
index 5a2e0c40670dad1f083cdffccad92214e525c5e2..ad388e6a910cf512d891d5dd1610dc2c3bb17ec0 100644 (file)
@@ -7,6 +7,7 @@
  * Contains main entry point, and misc functions.6
  * 
  */
+#define _GNU_SOURCE
 
 #include <assert.h>
 #include <errno.h>
@@ -162,7 +163,7 @@ static int newtLicenseBox(const char* title, const char* text, int width, int he
        return ret;
 }
 
-int write_lang_configs(const char *lang) {
+int write_lang_configs(char* lang) {
        struct keyvalue *kv = initkeyvalues();
 
        /* default stuff for main/settings. */
@@ -206,7 +207,7 @@ static char* center_string(const char* str, int width) {
 }
 
 #define DEFAULT_LANG "English"
-#define NUM_LANGS 8
+#define NUM_LANGS 10
 
 static struct lang {
        const char* code;
@@ -230,13 +231,17 @@ static struct config {
        int serial_console;
        int require_networking;
        int perform_download;
+       int disable_swap;
        char download_url[STRING_SIZE];
+       char postinstall[STRING_SIZE];
 } config = {
        .unattended = 0,
        .serial_console = 0,
        .require_networking = 0,
        .perform_download = 0,
+       .disable_swap = 0,
        .download_url = DOWNLOAD_URL,
+       .postinstall = "\0",
 };
 
 static void parse_command_line(struct config* c) {
@@ -253,7 +258,7 @@ static void parse_command_line(struct config* c) {
 
                while (token) {
                        strncpy(buffer, token, sizeof(buffer));
-                       char* val = &buffer;
+                       char* val = buffer;
                        char* key = strsep(&val, "=");
 
                        // serial console
@@ -268,11 +273,22 @@ static void parse_command_line(struct config* c) {
                        else if (strcmp(token, "installer.unattended") == 0)
                                c->unattended = 1;
 
+                       // disable swap
+                       else if (strcmp(token, "installer.disable-swap") == 0)
+                               c->disable_swap = 1;
+
                        // download url
                        else if (strcmp(key, "installer.download-url") == 0) {
-                               strncpy(&c->download_url, val, sizeof(c->download_url));
+                               strncpy(c->download_url, val, sizeof(c->download_url));
                                c->perform_download = 1;
 
+                               // Require networking for the download
+                               c->require_networking = 1;
+
+                       // postinstall script
+                       } else if (strcmp(key, "installer.postinstall") == 0) {
+                               strncpy(c->postinstall, val, sizeof(c->postinstall));
+
                                // Require networking for the download
                                c->require_networking = 1;
                        }
@@ -301,11 +317,10 @@ int main(int argc, char *argv[]) {
        char message[STRING_SIZE];
        char title[STRING_SIZE];
        int allok = 0;
-       FILE *handle, *copying;
-       char line[STRING_SIZE];
-               
-       setlocale (LC_ALL, "");
-       sethostname( SNAME , 10);
+       FILE *copying;
+
+       setlocale(LC_ALL, "");
+       sethostname(SNAME, 10);
 
        /* Log file/terminal stuff. */
        FILE* flog = NULL;
@@ -364,7 +379,7 @@ int main(int argc, char *argv[]) {
                assert(choice <= NUM_LANGS);
 
                fprintf(flog, "Selected language: %s (%s)\n", languages[choice].name, languages[choice].code);
-               snprintf(language, sizeof(language), languages[choice].code);
+               snprintf(language, sizeof(language), "%s", languages[choice].code);
 
                setenv("LANGUAGE", language, 1);
                setlocale(LC_ALL, language);
@@ -487,7 +502,7 @@ int main(int argc, char *argv[]) {
                // Read the license file.
                if (!(copying = fopen(LICENSE_FILE, "r"))) {
                        sprintf(discl_msg, "Could not open license file: %s\n", LICENSE_FILE);
-                       fprintf(flog, discl_msg);
+                       fprintf(flog, "%s", discl_msg);
                } else {
                        fread(discl_msg, 1, 40000, copying);
                        fclose(copying);
@@ -510,7 +525,7 @@ int main(int argc, char *argv[]) {
 
        // Check how many disks have been found and what
        // we can do with them.
-       unsigned int num_disks = hw_count_disks(disks);
+       unsigned int num_disks = hw_count_disks((const struct hw_disk**)disks);
 
        while (1) {
                // no harddisks found
@@ -522,7 +537,7 @@ int main(int argc, char *argv[]) {
                // 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);
+                       selected_disks = hw_select_first_disk((const struct hw_disk**)disks);
 
                // more than one usable disk has been found and
                // the user needs to choose what to do with them
@@ -531,7 +546,7 @@ int main(int argc, char *argv[]) {
                        int disk_selection[num_disks];
 
                        for (unsigned int i = 0; i < num_disks; i++) {
-                               disk_names[i] = &disks[i]->description;
+                               disk_names[i] = disks[i]->description;
                                disk_selection[i] = 0;
                        }
 
@@ -562,7 +577,7 @@ int main(int argc, char *argv[]) {
                if (config.unattended)
                        break;
 
-               num_selected_disks = hw_count_disks(selected_disks);
+               num_selected_disks = hw_count_disks((const struct hw_disk**)selected_disks);
 
                if (num_selected_disks == 1) {
                        snprintf(message, sizeof(message),
@@ -601,7 +616,7 @@ int main(int argc, char *argv[]) {
 
        hw_free_disks(disks);
 
-       struct hw_destination* destination = hw_make_destination(part_type, selected_disks);
+       struct hw_destination* destination = hw_make_destination(part_type, selected_disks, config.disable_swap);
 
        if (!destination) {
                errorbox(_("Your harddisk is too small."));
@@ -617,19 +632,21 @@ 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 (!config.unattended && !*destination->part_swap) {
-               rc = newtWinChoice(title, _("OK"), _("Cancel"),
-                       _("Your harddisk is very small, but you can continue without a swap partition."));
+       if (!config.unattended) {
+               if (!config.disable_swap && !*destination->part_swap) {
+                       rc = newtWinChoice(title, _("OK"), _("Cancel"),
+                               _("Your harddisk is very small, but you can continue without a swap partition."));
 
-               if (rc != 1)
-                       goto EXIT;
+                       if (rc != 1)
+                               goto EXIT;
+               }
        }
 
        // Filesystem selection
        if (!config.unattended) {
                struct filesystems {
                        int fstype;
-                       const char* description;
+                       char* description;
                } filesystems[] = {
                        { HW_FS_EXT4,            _("ext4 Filesystem") },
                        { HW_FS_EXT4_WO_JOURNAL, _("ext4 Filesystem without journal") },
@@ -799,6 +816,17 @@ int main(int argc, char *argv[]) {
        // Umount source drive and eject
        hw_umount(SOURCE_MOUNT_PATH);
 
+       // Download and execute the postinstall script
+       if (*config.postinstall) {
+               snprintf(commandstring, sizeof(commandstring),
+                       "/usr/bin/execute-postinstall.sh %s %s", DESTINATION_MOUNT_PATH, config.postinstall);
+
+               if (runcommandwithstatus(commandstring, title, _("Running post-install script..."), logfile)) {
+                       errorbox(_("Post-install script failed."));
+                       goto EXIT;
+               }
+       }
+
        snprintf(commandstring, STRING_SIZE, "/usr/bin/eject %s", sourcedrive);
        mysystem(logfile, commandstring);