From 126777bc019a54afb4ec51299f2cf9d2841698aa Mon Sep 17 00:00:00 2001 From: Pascal Rigaux Date: Wed, 25 Apr 2007 12:26:16 +0000 Subject: re-sync after the big svn loss --- mdk-stage1/stage1.c | 234 +++++----------------------------------------------- 1 file changed, 20 insertions(+), 214 deletions(-) (limited to 'mdk-stage1/stage1.c') diff --git a/mdk-stage1/stage1.c b/mdk-stage1/stage1.c index 6024f20ca..41456cc5c 100644 --- a/mdk-stage1/stage1.c +++ b/mdk-stage1/stage1.c @@ -34,7 +34,6 @@ #include #include #include -_syscall2(int,pivot_root,const char *,new_root,const char *,put_old) #include "stage1.h" @@ -45,11 +44,10 @@ _syscall2(int,pivot_root,const char *,new_root,const char *,put_old) #include "tools.h" #include "automatic.h" #include "mount.h" -#include "insmod.h" #include "thirdparty.h" #ifdef ENABLE_PCMCIA -#include "pcmcia_/pcmcia.h" +#include "pcmcia/pcmcia.h" #endif #ifndef DISABLE_CDROM @@ -121,7 +119,7 @@ static pid_t shell_pid = 0; static void spawn_shell(void) { int fd; - char * shell_name[] = { "/tmp/sh", NULL }; + char * shell_name[] = { "/sbin/sh", NULL }; log_message("spawning a shell"); @@ -236,12 +234,10 @@ static void handle_pcmcia(void) my_insmod("ds", ANY_DRIVER_TYPE, NULL, 0); my_insmod("pcmcia", ANY_DRIVER_TYPE, NULL, 0); - /* call to cardmgr takes time, let's use the wait message */ - wait_message("Enabling PCMCIA extension cards..."); - log_message("cardmgr rc: %d", cardmgr_call()); - remove_wait_message(); + /* setup a dynamic resource database for non statically mapped PCMCIA sockets */ + pcmcia_socket_startup(-1); - add_to_env("PCMCIA", pcmcia_adapter); + add_to_env("PCMCIA", pcmcia_adapter); } #endif @@ -283,17 +279,14 @@ static void method_select_and_prepare(void) #endif #ifndef DISABLE_CDROM means[i] = cdrom_install; means_auto[i++] = cdrom_install_auto; - allow_additional_modules_floppy = 0; #endif #ifndef DISABLE_DISK means[i] = disk_install; means_auto[i++] = disk_install_auto; - allow_additional_modules_floppy = 0; #endif means[i] = thirdparty_install; means_auto[i++] = thirdparty_install_auto; means[i] = NULL; unlink(IMAGE_LOCATION); - rmdir(IMAGE_LOCATION); /* useful if we change the method, eg: we have automatic:cdrom but go back to nfs */ results = ask_from_list_auto("Please choose the installation method.", means, &choice, "method", means_auto); @@ -314,7 +307,6 @@ static void method_select_and_prepare(void) if (!strcmp(choice, network_nfs_install)) results = nfs_prepare(); -#ifndef MANDRAKE_MOVE if (!strcmp(choice, network_ftp_install)) results = ftp_prepare(); @@ -325,7 +317,6 @@ static void method_select_and_prepare(void) if (!strcmp(choice, network_ka_install)) results = ka_prepare(); #endif -#endif #endif if (!strcmp(choice, thirdparty_install)) { @@ -348,203 +339,30 @@ static enum return_type create_initial_fs_symlinks(char* symlinks) if (scall(!(f = fopen(symlinks, "rb")), "fopen")) return RETURN_ERROR; while (fgets(buf, sizeof(buf), f)) { - char oldpath[500], newpath[500], newpathfinal[500]; + char oldpath[500], newpath[500]; buf[strlen(buf)-1] = '\0'; // trim \n if (sscanf(buf, "%s %s", oldpath, newpath) != 2) { - sprintf(oldpath, "%s%s", STAGE2_LOCATION_ROOTED, buf); - sprintf(newpathfinal, "%s%s", SLASH_LOCATION, buf); - } else { - sprintf(newpathfinal, "%s%s", SLASH_LOCATION, newpath); + sprintf(oldpath, "%s%s", STAGE2_LOCATION, buf); + sprintf(newpath, "%s", buf); } - log_message("creating symlink %s -> %s", oldpath, newpathfinal); - if (scall(symlink(oldpath, newpathfinal), "symlink")) - return RETURN_ERROR; - } - fclose(f); - return RETURN_OK; -} - -static enum return_type create_initial_fs_devices(char* devices) -{ - FILE *f; - char buf[5000]; - - // need to create the few devices needed to start up stage2 in a decent manner, we can't symlink or they will keep CD busy - if (scall(mkdir(SLASH_LOCATION "/dev", 0755), "mkdir")) - return RETURN_ERROR; - if (scall(!(f = fopen(devices, "rb")), "fopen")) - return RETURN_ERROR; - while (fgets(buf, sizeof(buf), f)) { - char name[500], path[500], type; - int major, minor; - sscanf(buf, "%s %c %d %d", name, &type, &major, &minor); - sprintf(path, "%s%s", SLASH_LOCATION, name); - log_message("creating device %s %c %d %d", path, type, major, minor); - if (scall(mknod(path, (type == 'c' ? S_IFCHR : S_IFBLK) | 0600, makedev(major, minor)), "mknod")) + recursiveRemove_if_it_exists(newpath); + log_message("creating symlink %s -> %s", oldpath, newpath); + if (scall(symlink(oldpath, newpath), "symlink")) return RETURN_ERROR; } fclose(f); return RETURN_OK; } -#ifdef MANDRAKE_MOVE -static enum return_type handle_move_clp(char* clp_name, char* live, char* location_live, char* location_mount, int* is_symlink, int preload) -{ - if (mount_clp_may_preload(clp_name, location_mount, preload) == RETURN_OK) { - return RETURN_OK; - } else { - char *full_live = asprintf_("%s%s", location_live, live); - log_message("no %s found (or disabled), trying to fallback on plain tree", clp_name); - if (!access(full_live, R_OK)) { - if (scall(symlink(location_live, location_mount), "symlink")) - return RETURN_ERROR; - *is_symlink = 1; - return RETURN_OK; - } else { - log_message("move: can't find %s nor %s, proceeding hoping files will be there", clp_name, full_live); - return RETURN_OK; - } - } -} - -int mandrake_move_post(void) -{ - int boot__real_is_symlink_to_raw = 0; - int always__real_is_symlink_to_raw = 0; - int totem__real_is_symlink_to_raw = 0; - int main__real_is_symlink_to_raw = 0; - - if (handle_move_clp("live_tree_boot.clp", "/usr/bin/runstage2.pl", - IMAGE_LOCATION "/live_tree_boot", BOOT_LOCATION, - &boot__real_is_symlink_to_raw, 1) != RETURN_OK) - return RETURN_ERROR; - - if (handle_move_clp("live_tree_always.clp", "/bin/bash", - IMAGE_LOCATION "/live_tree_always", ALWAYS_LOCATION, - &always__real_is_symlink_to_raw, 1) != RETURN_OK) - return RETURN_ERROR; - - if (handle_move_clp("live_tree_totem.clp", "/usr/bin/totem", - IMAGE_LOCATION "/live_tree_totem", TOTEM_LOCATION, - &totem__real_is_symlink_to_raw, 1) != RETURN_OK) - return RETURN_ERROR; - - if (handle_move_clp("live_tree.clp", "/etc/fstab", - IMAGE_LOCATION "/live_tree", STAGE2_LOCATION, - &main__real_is_symlink_to_raw, 0) != RETURN_OK) - return RETURN_ERROR; - - // in case we didn't mount any clp, because gzloop.o is not available later in /lib/modules - my_insmod("gzloop", ANY_DRIVER_TYPE, NULL, 0); - - // hardcoded :( - if (!access(TOTEM_LOCATION, R_OK)) { - if (scall(symlink("/image_totem/usr", SLASH_LOCATION "/usr"), "symlink")) - return RETURN_ERROR; - } else - // need a fallback in case we don't use image_totem.clp nor live_tree_totem, but we're in -u mode - if (scall(symlink(STAGE2_LOCATION_ROOTED "/usr", SLASH_LOCATION "/usr"), "symlink")) - return RETURN_ERROR; - - if (create_initial_fs_symlinks(STAGE2_LOCATION "/move/symlinks") != RETURN_OK || - create_initial_fs_devices(STAGE2_LOCATION "/move/devices") != RETURN_OK) - return RETURN_ERROR; - - if (boot__real_is_symlink_to_raw) { - if (scall(unlink(BOOT_LOCATION), "unlink")) - return RETURN_ERROR; - if (scall(symlink(IMAGE_LOCATION_REL "/live_tree_boot", BOOT_LOCATION), "symlink")) - return RETURN_ERROR; - } - - if (always__real_is_symlink_to_raw) { - if (scall(unlink(ALWAYS_LOCATION), "unlink")) - return RETURN_ERROR; - if (scall(symlink(IMAGE_LOCATION_REL "/live_tree_always", ALWAYS_LOCATION), "symlink")) - return RETURN_ERROR; - } - - if (totem__real_is_symlink_to_raw) { - if (scall(unlink(TOTEM_LOCATION), "unlink")) - return RETURN_ERROR; - if (scall(symlink(IMAGE_LOCATION_REL "/live_tree_totem", TOTEM_LOCATION), "symlink")) - return RETURN_ERROR; - } - - if (main__real_is_symlink_to_raw) { - if (scall(unlink(STAGE2_LOCATION), "unlink")) - return RETURN_ERROR; - if (scall(symlink(IMAGE_LOCATION_REL "/live_tree", STAGE2_LOCATION), "symlink")) - return RETURN_ERROR; - } - return RETURN_OK; -} -#endif - -int do_pivot_root(void) -{ - int fd; - char rootdev[] = "0x0100"; - - if (IS_DEBUGSTAGE1) - while (1); - - log_message("pivot_rooting"); - // trick so that kernel won't try to mount the root device when initrd exits - if (scall((fd = open("/proc/sys/kernel/real-root-dev", O_WRONLY)) < 0, "open")) - return RETURN_ERROR; - if (scall(write(fd, rootdev, strlen(rootdev)) != (signed)strlen(rootdev), "write")) { - close(fd); - return RETURN_ERROR; - } - close(fd); - - if (scall(mkdir(SLASH_LOCATION "/stage1", 0755), "mkdir")) - return RETURN_ERROR; - - if (scall(pivot_root(SLASH_LOCATION, SLASH_LOCATION "/stage1"), "pivot_root")) - return RETURN_ERROR; - - return RETURN_OK; -} - void finish_preparing(void) { -#ifdef MANDRAKE_MOVE - if (mandrake_move_post() != RETURN_OK) - stg1_fatal_message("Fatal error when launching MandrakeMove."); -#else - mkdir(SLASH_LOCATION "/etc", 0755); - mkdir(SLASH_LOCATION "/var", 0755); - if (IS_RESCUE) { - if (create_initial_fs_symlinks(STAGE2_LOCATION "/usr/share/symlinks") != RETURN_OK) - stg1_fatal_message("Fatal error finishing initialization."); - - } else { - if (create_initial_fs_symlinks(STAGE2_LOCATION "/usr/share/symlinks") != RETURN_OK || - create_initial_fs_devices(STAGE2_LOCATION "/usr/share/devices") != RETURN_OK) - stg1_fatal_message("Fatal error finishing initialization."); - } -#endif + recursiveRemove("/init"); + + if (create_initial_fs_symlinks(STAGE2_LOCATION "/usr/share/symlinks") != RETURN_OK) + stg1_fatal_message("Fatal error finishing initialization."); /* /tmp/syslog is used by the second init, so it must be copied now, not in stage2 */ /* we remove it to ensure the old one is not copied over it in stage2 */ - copy_file("/tmp/syslog", SLASH_LOCATION "/tmp/syslog", NULL); - unlink("/tmp/syslog"); - copy_file("/etc/resolv.conf", SLASH_LOCATION "/etc/resolv.conf", NULL); - mkdir(SLASH_LOCATION "/modules", 0755); - copy_file("/modules/modules.dep", SLASH_LOCATION "/modules/modules.dep", NULL); - - if (!IS_RESCUE) { - copy_file(STAGE2_LOCATION "/etc/init", SLASH_LOCATION "/etc/init", NULL); - chmod(SLASH_LOCATION "/etc/init", 0755); - } - - umount("/tmp/tmpfs"); - do_pivot_root(); - - if (file_size(IS_RESCUE ? "/sbin/init" : "/etc/init") == -1) - stg1_fatal_message("Fatal error giving hand to second stage."); #ifdef SPAWN_SHELL if (shell_pid != 0) { @@ -576,13 +394,6 @@ int main(int argc __attribute__ ((unused)), char **argv __attribute__ ((unused)) set_param(MODE_TESTING); } - if (!IS_TESTING) { - mkdir(SLASH_LOCATION, 0755); - if (scall(mount("none", SLASH_LOCATION, "tmpfs", MS_MGC_VAL, NULL), "mount tmpfs")) - fatal_error("Fatal error initializing."); - mkdir(SLASH_LOCATION "/tmp", 0755); - } - #ifdef SPAWN_INTERACTIVE spawn_interactive(); #endif @@ -618,18 +429,13 @@ int main(int argc __attribute__ ((unused)), char **argv __attribute__ ((unused)) "your own risk. Alternatively, you may reboot your system now."); } -#ifdef MANDRAKE_MOVE - if (total_memory() < MEM_LIMIT_MOVE) - stg1_info_message(DISTRIB_NAME " typically needs more than %d Mbytes of memory (detected %d Mbytes). You may proceed, but the machine may crash or lock up for no apparent reason. Continue at your own risk. Alternatively, you may reboot your system now.", - MEM_LIMIT_MOVE, total_memory()); -#endif method_select_and_prepare(); -#ifndef MANDRAKE_MOVE + thirdparty_destroy(); + if (access(STAGE2_LOCATION, R_OK) != 0) if (symlink(IMAGE_LOCATION_REL "/" LIVE_LOCATION_REL, STAGE2_LOCATION) != 0) log_perror("symlink from " IMAGE_LOCATION_REL "/" LIVE_LOCATION_REL " to " STAGE2_LOCATION " failed"); -#endif #ifdef SPAWN_INTERACTIVE if (interactive_pid != 0) @@ -641,9 +447,9 @@ int main(int argc __attribute__ ((unused)), char **argv __attribute__ ((unused)) finish_frontend(); close_log(); - if (IS_TESTING) - return 0; + if (IS_RESCUE) + return 66; /* ask init to exec new init */ else - return 66; + return 0x35; /* ask init to run stage2 binary */ #endif } -- cgit v1.2.1