Advertisement
Carsten_Milkau

init.c

Jun 20th, 2016
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.25 KB | None | 0 0
  1. #include <errno.h>
  2. #include <error.h>
  3. #include <limits.h>
  4. #include <stdlib.h>
  5. #include <unistd.h>
  6. #include <sys/mount.h>
  7. #include <sys/types.h>
  8. #include <sys/wait.h>
  9.  
  10. extern char     **environ;
  11.  
  12. #define _error error
  13.  
  14. int     main (int argc, const char * const argv[])
  15. {
  16.         static const char * const       root    = "/dev/md/Volume0_0p3";
  17.         static const char * const       rootfs  = "xfs";
  18.         char                    real_root[PATH_MAX];
  19.         int                     retval  = EXIT_SUCCESS;
  20.         pid_t                   child;
  21.  
  22.         // mount /sys and /proc
  23.         if (mount("proc", "/proc", "proc", MS_MGC_VAL, NULL) == -1)
  24.         { _error(0, errno, "mount -t proc proc /proc failed with error %d", errno); return errno; }
  25.         if (mount("sysfs", "/sys", "sysfs", MS_MGC_VAL, NULL) == -1)
  26.         { _error(0, errno, "mount -t sysfs sysfs /sys failed with error %d", errno); return errno; }
  27.  
  28.         _error(0, 0, "Success mounting /proc and /sys\n");
  29.  
  30.         // run mdadm
  31.         child = vfork();
  32.         if (child == 0) { execl("/sbin/mdadm", "/sbin/mdadm", "--assemble", "--scan", "--auto=mdp", NULL); }
  33.         else
  34.         {
  35.                 siginfo_t       info;
  36.  
  37.                 waitid(P_PID, child, &info, WEXITED);
  38.                 retval = info.si_status;
  39.         }
  40.         if (retval != EXIT_SUCCESS) { _error(0, 0, "mdadm --assemble --scan --auto=mdp exited with status %d, aborting.\n", retval); return retval; }
  41.  
  42.         _error(0, 0, "Finished assembling RAID arrays\n");
  43.         sync();
  44.  
  45.         // mount /
  46.         if (realpath(root, real_root) == NULL)
  47.         { _error(0, errno, "realpath %s failed with error %d", root, errno); return errno; }
  48.         if (mount(real_root, "/mnt", rootfs, MS_MGC_VAL, NULL) == -1)
  49.  
  50.         _error(0, 0, "Success mounting /\n");
  51.         sync();
  52.  
  53.         // run init
  54.         retval = execl("/sbin/switch_root", "/sbin/switch_root", "/mnt", "sbin/init", NULL);
  55.         if (retval == -1) { _error(0, errno, "/sbin/switch_root /mnt /sbin/init failed with error %d", errno); return errno; }
  56.         else if (retval != EXIT_SUCCESS) { _error(0, 0, "/sbin/switch_root /mnt /sbin/init exited with status %d, aborting.\n", retval); return retval; }
  57.  
  58.         return EXIT_SUCCESS;
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement