migrate.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /* vim: tabstop=4 shiftwidth=4 noexpandtab
  2. * This file is part of ToaruOS and is released under the terms
  3. * of the NCSA / University of Illinois License - see LICENSE.md
  4. * Copyright (C) 2018 K. Lange
  5. *
  6. * migrate - Relocate root filesystem to tmpfs
  7. *
  8. * Run as part of system startup to move the ext2 root ramdisk
  9. * into a flexible in-memory temporary filesystem, which allows
  10. * file creation and editing and is much faster than the using
  11. * the ext2 driver against the static in-memory ramdisk.
  12. *
  13. * Based on the original Python implementation.
  14. */
  15. #include <string.h>
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <dirent.h>
  19. #include <unistd.h>
  20. #include <fcntl.h>
  21. #include <sys/stat.h>
  22. #include <sys/ioctl.h>
  23. #include <toaru/trace.h>
  24. #include <toaru/hashmap.h>
  25. #define TRACE_APP_NAME "migrate"
  26. #define TRACE_(...) do { if (_debug) { TRACE(__VA_ARGS__); } } while (0)
  27. #define CHUNK_SIZE 4096
  28. static int _debug = 0;
  29. int tokenize(char * str, char * sep, char **buf) {
  30. char * pch_i;
  31. char * save_i;
  32. int argc = 0;
  33. pch_i = strtok_r(str,sep,&save_i);
  34. if (!pch_i) { return 0; }
  35. while (pch_i != NULL) {
  36. buf[argc] = (char *)pch_i;
  37. ++argc;
  38. pch_i = strtok_r(NULL,sep,&save_i);
  39. }
  40. buf[argc] = NULL;
  41. return argc;
  42. }
  43. void copy_link(char * source, char * dest, int mode, int uid, int gid) {
  44. //fprintf(stderr, "need to copy link %s to %s\n", source, dest);
  45. char tmp[1024];
  46. readlink(source, tmp, 1024);
  47. symlink(tmp, dest);
  48. chmod(dest, mode);
  49. chown(dest, uid, gid);
  50. }
  51. void copy_file(char * source, char * dest, int mode,int uid, int gid) {
  52. //fprintf(stderr, "need to copy file %s to %s %x\n", source, dest, mode);
  53. int d_fd = open(dest, O_WRONLY | O_CREAT, mode);
  54. int s_fd = open(source, O_RDONLY);
  55. ssize_t length;
  56. length = lseek(s_fd, 0, SEEK_END);
  57. lseek(s_fd, 0, SEEK_SET);
  58. //fprintf(stderr, "%d bytes to copy\n", length);
  59. char buf[CHUNK_SIZE];
  60. while (length > 0) {
  61. size_t r = read(s_fd, buf, length < CHUNK_SIZE ? length : CHUNK_SIZE);
  62. //fprintf(stderr, "copying %d bytes from %s to %s\n", r, source, dest);
  63. write(d_fd, buf, r);
  64. length -= r;
  65. //fprintf(stderr, "%d bytes remaining\n", length);
  66. }
  67. close(s_fd);
  68. close(d_fd);
  69. chown(dest, uid, gid);
  70. }
  71. void copy_directory(char * source, char * dest, int mode, int uid, int gid) {
  72. DIR * dirp = opendir(source);
  73. if (dirp == NULL) {
  74. fprintf(stderr, "Failed to copy directory %s\n", source);
  75. return;
  76. }
  77. //fprintf(stderr, "Creating %s\n", dest);
  78. if (!strcmp(dest, "/")) {
  79. dest = "";
  80. } else {
  81. mkdir(dest, mode);
  82. }
  83. struct dirent * ent = readdir(dirp);
  84. while (ent != NULL) {
  85. if (!strcmp(ent->d_name,".") || !strcmp(ent->d_name,"..")) {
  86. //fprintf(stderr, "Skipping %s\n", ent->d_name);
  87. ent = readdir(dirp);
  88. continue;
  89. }
  90. //fprintf(stderr, "not skipping %s/%s → %s/%s\n", source, ent->d_name, dest, ent->d_name);
  91. struct stat statbuf;
  92. char tmp[strlen(source)+strlen(ent->d_name)+2];
  93. sprintf(tmp, "%s/%s", source, ent->d_name);
  94. char tmp2[strlen(dest)+strlen(ent->d_name)+2];
  95. sprintf(tmp2, "%s/%s", dest, ent->d_name);
  96. //fprintf(stderr,"%s → %s\n", tmp, tmp2);
  97. lstat(tmp,&statbuf);
  98. if (S_ISLNK(statbuf.st_mode)) {
  99. copy_link(tmp, tmp2, statbuf.st_mode & 07777, statbuf.st_uid, statbuf.st_gid);
  100. } else if (S_ISDIR(statbuf.st_mode)) {
  101. copy_directory(tmp, tmp2, statbuf.st_mode & 07777, statbuf.st_uid, statbuf.st_gid);
  102. } else if (S_ISREG(statbuf.st_mode)) {
  103. copy_file(tmp, tmp2, statbuf.st_mode & 07777, statbuf.st_uid, statbuf.st_gid);
  104. } else {
  105. fprintf(stderr, " %s is not any of the required file types?\n", tmp);
  106. }
  107. ent = readdir(dirp);
  108. }
  109. closedir(dirp);
  110. chown(dest, uid, gid);
  111. }
  112. void free_ramdisk(char * path) {
  113. int fd = open(path, O_RDONLY);
  114. ioctl(fd, 0x4001, NULL);
  115. close(fd);
  116. }
  117. hashmap_t * get_cmdline(void) {
  118. int fd = open("/proc/cmdline", O_RDONLY);
  119. char * out = malloc(1024);
  120. size_t r = read(fd, out, 1024);
  121. out[r] = '\0';
  122. if (out[r-1] == '\n') {
  123. out[r-1] = '\0';
  124. }
  125. char * arg = strdup(out);
  126. char * argv[1024];
  127. int argc = tokenize(arg, " ", argv);
  128. /* New let's parse the tokens into the arguments list so we can index by key */
  129. hashmap_t * args = hashmap_create(10);
  130. for (int i = 0; i < argc; ++i) {
  131. char * c = strdup(argv[i]);
  132. char * name;
  133. char * value;
  134. name = c;
  135. value = NULL;
  136. /* Find the first = and replace it with a null */
  137. char * v = c;
  138. while (*v) {
  139. if (*v == '=') {
  140. *v = '\0';
  141. v++;
  142. value = v;
  143. goto _break;
  144. }
  145. v++;
  146. }
  147. _break:
  148. hashmap_set(args, name, value);
  149. }
  150. free(arg);
  151. free(out);
  152. return args;
  153. }
  154. int main(int argc, char * argv[]) {
  155. hashmap_t * cmdline = get_cmdline();
  156. if (hashmap_has(cmdline, "logtoserial")) {
  157. _debug = 1;
  158. }
  159. if (hashmap_has(cmdline, "root")) {
  160. TRACE_("Original root was %s", hashmap_get(cmdline, "root"));
  161. } else if (hashmap_has(cmdline,"init") && !strcmp(hashmap_get(cmdline,"init"),"/dev/ram0")) {
  162. TRACE_("Init is ram0, so this is probably a netboot image, going to assume root is /tmp/netboot.img");
  163. hashmap_set(cmdline,"root","/tmp/netboot.img");
  164. } else {
  165. TRACE_("Fatal: Don't know how to boot this. No root set.\n");
  166. return 1;
  167. }
  168. char * root = hashmap_get(cmdline,"root");
  169. char * start = hashmap_get(cmdline,"_start");
  170. if (!start) {
  171. start = "";
  172. }
  173. char * root_type = hashmap_get(cmdline,"root_type");
  174. if (!root_type) {
  175. root_type = "ext2";
  176. }
  177. char tmp[1024];
  178. TRACE_("Remounting root to /dev/base");
  179. sprintf(tmp, "mount %s %s /dev/base", root_type, root);
  180. system(tmp);
  181. TRACE_("Mounting tmpfs to /");
  182. system("mount tmpfs x /");
  183. TRACE_("Migrating root...");
  184. copy_directory("/dev/base","/",0660,0,0);
  185. system("mount tmpfs x /dev/base");
  186. if (strstr(root, "/dev/ram") != NULL) {
  187. char * tmp = strdup(root);
  188. char * c = strchr(tmp, ',');
  189. if (c) {
  190. *c = '\0';
  191. }
  192. TRACE_("Freeing ramdisk at %s", tmp);
  193. free_ramdisk(tmp);
  194. free(tmp);
  195. }
  196. return 0;
  197. }