• source navigation  • diff markup  • identifier search  • freetext search  • 

Sources/fstools/libfstools/extroot.c

  1 /*
  2  * Copyright (C) 2014 John Crispin <blogic@openwrt.org>
  3  *
  4  * This program is free software; you can redistribute it and/or modify
  5  * it under the terms of the GNU Lesser General Public License version 2.1
  6  * as published by the Free Software Foundation
  7  *
  8  * This program is distributed in the hope that it will be useful,
  9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 11  * GNU General Public License for more details.
 12  */
 13 
 14 #include <unistd.h>
 15 #include <stdlib.h>
 16 #include <stdio.h>
 17 #include <string.h>
 18 #include <sys/mount.h>
 19 #include <sys/types.h>
 20 #include <sys/stat.h>
 21 #include <sys/wait.h>
 22 #include <libgen.h>
 23 
 24 #include "libfstools.h"
 25 
 26 /*
 27  * This will execute "block extroot" and make use of mounted extroot or return
 28  * an error.
 29  */
 30 int mount_extroot(char const *extroot_prefix)
 31 {
 32         char ldlib_path[32];
 33         char block_path[32];
 34         char kmod_loader[64];
 35         struct stat s;
 36         pid_t pid;
 37 
 38         /* try finding the library directory */
 39         snprintf(ldlib_path, sizeof(ldlib_path), "%s/upper/lib", extroot_prefix);
 40 
 41         if (stat(ldlib_path, &s) || !S_ISDIR(s.st_mode))
 42                 snprintf(ldlib_path, sizeof(ldlib_path), "%s/lib", extroot_prefix);
 43 
 44         /* try finding the block executable */
 45         snprintf(block_path, sizeof(block_path), "%s/upper/sbin/block", extroot_prefix);
 46 
 47         if (stat(block_path, &s) || !S_ISREG(s.st_mode))
 48                 snprintf(block_path, sizeof(block_path), "%s/sbin/block", extroot_prefix);
 49 
 50         if (stat(block_path, &s) || !S_ISREG(s.st_mode))
 51                 snprintf(block_path, sizeof(block_path), "/sbin/block");
 52 
 53         if (stat(block_path, &s) || !S_ISREG(s.st_mode))
 54                 return -1;
 55 
 56         /* set LD_LIBRARY_PATH env var and load kmods from overlay if we found a lib directory there */
 57         if (!stat(ldlib_path, &s) && S_ISDIR(s.st_mode)) {
 58                 ULOG_INFO("loading kmods from internal overlay\n");
 59                 setenv("LD_LIBRARY_PATH", ldlib_path, 1);
 60                 snprintf(kmod_loader, sizeof(kmod_loader),
 61                          "/sbin/kmodloader %s/etc/modules-boot.d/", dirname(ldlib_path));
 62                 if (system(kmod_loader))
 63                         ULOG_ERR("failed to launch kmodloader from internal overlay\n");
 64         }
 65 
 66         pid = fork();
 67         if (!pid) {
 68                 mkdir("/tmp/extroot", 0755);
 69                 execl(block_path, block_path, "extroot", NULL);
 70                 exit(-1);
 71         } else if (pid > 0) {
 72                 int status;
 73 
 74                 waitpid(pid, &status, 0);
 75                 if (!WEXITSTATUS(status)) {
 76                         if (find_mount("/tmp/extroot/mnt")) {
 77                                 mount("/dev/root", "/", NULL, MS_NOATIME | MS_REMOUNT | MS_RDONLY, 0);
 78 
 79                                 mkdir("/tmp/extroot/mnt/proc", 0755);
 80                                 mkdir("/tmp/extroot/mnt/dev", 0755);
 81                                 mkdir("/tmp/extroot/mnt/sys", 0755);
 82                                 mkdir("/tmp/extroot/mnt/tmp", 0755);
 83                                 mkdir("/tmp/extroot/mnt/rom", 0755);
 84 
 85                                 if (mount_move("/tmp/extroot", "", "/mnt")) {
 86                                         ULOG_ERR("moving pivotroot failed - continue normal boot\n");
 87                                         umount("/tmp/extroot/mnt");
 88                                 } else if (pivot("/mnt", "/rom")) {
 89                                         ULOG_ERR("switching to pivotroot failed - continue normal boot\n");
 90                                         umount("/mnt");
 91                                 } else {
 92                                         umount("/tmp/overlay");
 93                                         rmdir("/tmp/overlay");
 94                                         rmdir("/tmp/extroot/mnt");
 95                                         rmdir("/tmp/extroot");
 96                                         return 0;
 97                                 }
 98                         } else if (find_mount("/tmp/extroot/overlay")) {
 99                                 if (mount_move("/tmp/extroot", "", "/overlay")) {
100                                         ULOG_ERR("moving extroot failed - continue normal boot\n");
101                                         umount("/tmp/extroot/overlay");
102                                 } else if (fopivot("/overlay", "/rom")) {
103                                         ULOG_ERR("switching to extroot failed - continue normal boot\n");
104                                         umount("/overlay");
105                                 } else {
106                                         umount("/tmp/overlay");
107                                         rmdir("/tmp/overlay");
108                                         rmdir("/tmp/extroot/overlay");
109                                         rmdir("/tmp/extroot");
110                                         return 0;
111                                 }
112                         }
113                 }
114         }
115         return -1;
116 }
117 

This page was automatically generated by LXR 0.3.1.  •  OpenWrt