2021-11-26 06:41:56 +03:00
|
|
|
/**
|
|
|
|
* @brief migrate - Relocate root filesystem to tmpfs
|
2018-08-14 11:13:38 +03:00
|
|
|
*
|
|
|
|
* Run as part of system startup to move the ext2 root ramdisk
|
|
|
|
* into a flexible in-memory temporary filesystem, which allows
|
|
|
|
* file creation and editing and is much faster than the using
|
|
|
|
* the ext2 driver against the static in-memory ramdisk.
|
|
|
|
*
|
|
|
|
* Based on the original Python implementation.
|
2021-11-26 06:41:56 +03:00
|
|
|
*
|
|
|
|
* @copyright
|
|
|
|
* This file is part of ToaruOS and is released under the terms
|
|
|
|
* of the NCSA / University of Illinois License - see LICENSE.md
|
|
|
|
* Copyright (C) 2018 K. Lange
|
2018-08-14 11:13:38 +03:00
|
|
|
*/
|
2018-03-15 13:30:04 +03:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
|
2018-03-19 05:38:11 +03:00
|
|
|
#include <toaru/trace.h>
|
|
|
|
#include <toaru/hashmap.h>
|
2018-03-15 13:30:04 +03:00
|
|
|
#define TRACE_APP_NAME "migrate"
|
|
|
|
|
2018-12-21 11:47:19 +03:00
|
|
|
#define TRACE_(...) do { \
|
2018-12-25 18:06:42 +03:00
|
|
|
if (_splash) { char _trace_tmp[512]; sprintf(_trace_tmp, __VA_ARGS__); fprintf(_splash, ":%s", _trace_tmp); fflush(_splash); } \
|
2018-12-21 11:47:19 +03:00
|
|
|
if (_debug) { TRACE(__VA_ARGS__); } \
|
|
|
|
} while (0)
|
2018-07-02 07:21:27 +03:00
|
|
|
|
2018-03-15 13:30:04 +03:00
|
|
|
#define CHUNK_SIZE 4096
|
|
|
|
|
2018-07-02 07:21:27 +03:00
|
|
|
static int _debug = 0;
|
2018-12-21 11:47:19 +03:00
|
|
|
static FILE * _splash = NULL;
|
2018-07-02 07:21:27 +03:00
|
|
|
|
2018-03-15 13:30:04 +03:00
|
|
|
int tokenize(char * str, char * sep, char **buf) {
|
|
|
|
char * pch_i;
|
|
|
|
char * save_i;
|
|
|
|
int argc = 0;
|
|
|
|
pch_i = strtok_r(str,sep,&save_i);
|
|
|
|
if (!pch_i) { return 0; }
|
|
|
|
while (pch_i != NULL) {
|
|
|
|
buf[argc] = (char *)pch_i;
|
|
|
|
++argc;
|
|
|
|
pch_i = strtok_r(NULL,sep,&save_i);
|
|
|
|
}
|
|
|
|
buf[argc] = NULL;
|
|
|
|
return argc;
|
|
|
|
}
|
|
|
|
|
2018-07-18 06:37:33 +03:00
|
|
|
void copy_link(char * source, char * dest, int mode, int uid, int gid) {
|
2018-05-21 15:45:18 +03:00
|
|
|
//fprintf(stderr, "need to copy link %s to %s\n", source, dest);
|
|
|
|
char tmp[1024];
|
|
|
|
readlink(source, tmp, 1024);
|
|
|
|
symlink(tmp, dest);
|
2018-12-05 07:11:06 +03:00
|
|
|
//chmod(dest, mode); /* links don't have modes */
|
2018-07-18 06:37:33 +03:00
|
|
|
chown(dest, uid, gid);
|
2018-03-15 13:30:04 +03:00
|
|
|
}
|
|
|
|
|
2018-07-18 06:37:33 +03:00
|
|
|
void copy_file(char * source, char * dest, int mode,int uid, int gid) {
|
2018-03-15 13:30:04 +03:00
|
|
|
//fprintf(stderr, "need to copy file %s to %s %x\n", source, dest, mode);
|
2021-10-20 12:49:58 +03:00
|
|
|
//TRACE_("Copying %s...", dest);
|
2018-03-15 13:30:04 +03:00
|
|
|
|
|
|
|
int d_fd = open(dest, O_WRONLY | O_CREAT, mode);
|
|
|
|
int s_fd = open(source, O_RDONLY);
|
|
|
|
|
|
|
|
ssize_t length;
|
|
|
|
|
|
|
|
length = lseek(s_fd, 0, SEEK_END);
|
|
|
|
lseek(s_fd, 0, SEEK_SET);
|
|
|
|
|
|
|
|
//fprintf(stderr, "%d bytes to copy\n", length);
|
|
|
|
|
|
|
|
char buf[CHUNK_SIZE];
|
|
|
|
|
|
|
|
while (length > 0) {
|
|
|
|
size_t r = read(s_fd, buf, length < CHUNK_SIZE ? length : CHUNK_SIZE);
|
|
|
|
//fprintf(stderr, "copying %d bytes from %s to %s\n", r, source, dest);
|
|
|
|
write(d_fd, buf, r);
|
|
|
|
length -= r;
|
|
|
|
//fprintf(stderr, "%d bytes remaining\n", length);
|
|
|
|
}
|
|
|
|
|
|
|
|
close(s_fd);
|
|
|
|
close(d_fd);
|
|
|
|
|
2018-07-18 06:37:33 +03:00
|
|
|
chown(dest, uid, gid);
|
2021-09-02 09:10:12 +03:00
|
|
|
chmod(dest, mode);
|
2018-03-15 13:30:04 +03:00
|
|
|
}
|
|
|
|
|
2018-07-18 06:37:33 +03:00
|
|
|
void copy_directory(char * source, char * dest, int mode, int uid, int gid) {
|
2018-03-15 13:30:04 +03:00
|
|
|
DIR * dirp = opendir(source);
|
|
|
|
if (dirp == NULL) {
|
|
|
|
fprintf(stderr, "Failed to copy directory %s\n", source);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-10-20 12:49:58 +03:00
|
|
|
TRACE_("Copying %s/...", dest);
|
2018-03-15 13:30:04 +03:00
|
|
|
//fprintf(stderr, "Creating %s\n", dest);
|
2018-04-25 08:03:29 +03:00
|
|
|
if (!strcmp(dest, "/")) {
|
2018-03-15 13:30:04 +03:00
|
|
|
dest = "";
|
|
|
|
} else {
|
|
|
|
mkdir(dest, mode);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct dirent * ent = readdir(dirp);
|
|
|
|
while (ent != NULL) {
|
|
|
|
if (!strcmp(ent->d_name,".") || !strcmp(ent->d_name,"..")) {
|
|
|
|
//fprintf(stderr, "Skipping %s\n", ent->d_name);
|
|
|
|
ent = readdir(dirp);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
//fprintf(stderr, "not skipping %s/%s → %s/%s\n", source, ent->d_name, dest, ent->d_name);
|
|
|
|
struct stat statbuf;
|
|
|
|
char tmp[strlen(source)+strlen(ent->d_name)+2];
|
|
|
|
sprintf(tmp, "%s/%s", source, ent->d_name);
|
|
|
|
char tmp2[strlen(dest)+strlen(ent->d_name)+2];
|
|
|
|
sprintf(tmp2, "%s/%s", dest, ent->d_name);
|
|
|
|
//fprintf(stderr,"%s → %s\n", tmp, tmp2);
|
2018-04-25 08:03:29 +03:00
|
|
|
lstat(tmp,&statbuf);
|
2018-03-15 13:30:04 +03:00
|
|
|
if (S_ISLNK(statbuf.st_mode)) {
|
2018-07-18 06:37:33 +03:00
|
|
|
copy_link(tmp, tmp2, statbuf.st_mode & 07777, statbuf.st_uid, statbuf.st_gid);
|
2018-03-15 13:30:04 +03:00
|
|
|
} else if (S_ISDIR(statbuf.st_mode)) {
|
2018-07-18 06:37:33 +03:00
|
|
|
copy_directory(tmp, tmp2, statbuf.st_mode & 07777, statbuf.st_uid, statbuf.st_gid);
|
2018-03-15 13:30:04 +03:00
|
|
|
} else if (S_ISREG(statbuf.st_mode)) {
|
2018-07-18 06:37:33 +03:00
|
|
|
copy_file(tmp, tmp2, statbuf.st_mode & 07777, statbuf.st_uid, statbuf.st_gid);
|
2018-03-15 13:30:04 +03:00
|
|
|
} else {
|
|
|
|
fprintf(stderr, " %s is not any of the required file types?\n", tmp);
|
|
|
|
}
|
|
|
|
ent = readdir(dirp);
|
|
|
|
}
|
|
|
|
closedir(dirp);
|
2018-07-18 06:37:33 +03:00
|
|
|
|
|
|
|
chown(dest, uid, gid);
|
2018-03-15 13:30:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void free_ramdisk(char * path) {
|
|
|
|
int fd = open(path, O_RDONLY);
|
|
|
|
ioctl(fd, 0x4001, NULL);
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
hashmap_t * get_cmdline(void) {
|
|
|
|
int fd = open("/proc/cmdline", O_RDONLY);
|
|
|
|
char * out = malloc(1024);
|
|
|
|
size_t r = read(fd, out, 1024);
|
|
|
|
out[r] = '\0';
|
|
|
|
if (out[r-1] == '\n') {
|
|
|
|
out[r-1] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
char * arg = strdup(out);
|
|
|
|
char * argv[1024];
|
|
|
|
int argc = tokenize(arg, " ", argv);
|
|
|
|
|
|
|
|
/* New let's parse the tokens into the arguments list so we can index by key */
|
|
|
|
|
|
|
|
hashmap_t * args = hashmap_create(10);
|
|
|
|
|
|
|
|
for (int i = 0; i < argc; ++i) {
|
|
|
|
char * c = strdup(argv[i]);
|
|
|
|
|
|
|
|
char * name;
|
|
|
|
char * value;
|
|
|
|
|
|
|
|
name = c;
|
|
|
|
value = NULL;
|
|
|
|
/* Find the first = and replace it with a null */
|
|
|
|
char * v = c;
|
|
|
|
while (*v) {
|
|
|
|
if (*v == '=') {
|
|
|
|
*v = '\0';
|
|
|
|
v++;
|
|
|
|
value = v;
|
|
|
|
goto _break;
|
|
|
|
}
|
|
|
|
v++;
|
|
|
|
}
|
|
|
|
|
|
|
|
_break:
|
|
|
|
hashmap_set(args, name, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(arg);
|
|
|
|
free(out);
|
|
|
|
|
|
|
|
return args;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char * argv[]) {
|
|
|
|
|
|
|
|
hashmap_t * cmdline = get_cmdline();
|
|
|
|
|
2018-07-02 07:21:27 +03:00
|
|
|
if (hashmap_has(cmdline, "logtoserial")) {
|
|
|
|
_debug = 1;
|
|
|
|
}
|
|
|
|
|
2018-12-21 11:47:19 +03:00
|
|
|
_splash = fopen("/dev/pex/splash","r+");
|
|
|
|
|
2018-03-15 13:30:04 +03:00
|
|
|
if (hashmap_has(cmdline, "root")) {
|
2018-12-21 11:47:19 +03:00
|
|
|
TRACE_("Original root was %s", (char*)hashmap_get(cmdline, "root"));
|
2018-03-15 13:30:04 +03:00
|
|
|
} else if (hashmap_has(cmdline,"init") && !strcmp(hashmap_get(cmdline,"init"),"/dev/ram0")) {
|
2018-07-02 07:21:27 +03:00
|
|
|
TRACE_("Init is ram0, so this is probably a netboot image, going to assume root is /tmp/netboot.img");
|
2018-03-15 13:30:04 +03:00
|
|
|
hashmap_set(cmdline,"root","/tmp/netboot.img");
|
|
|
|
} else {
|
2018-07-02 07:21:27 +03:00
|
|
|
TRACE_("Fatal: Don't know how to boot this. No root set.\n");
|
2018-03-15 13:30:04 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
char * root = hashmap_get(cmdline,"root");
|
|
|
|
|
|
|
|
char * start = hashmap_get(cmdline,"_start");
|
|
|
|
if (!start) {
|
|
|
|
start = "";
|
|
|
|
}
|
|
|
|
char * root_type = hashmap_get(cmdline,"root_type");
|
|
|
|
if (!root_type) {
|
2021-05-31 04:47:02 +03:00
|
|
|
root_type = "tar";
|
2018-03-15 13:30:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
char tmp[1024];
|
|
|
|
|
2018-07-02 07:21:27 +03:00
|
|
|
TRACE_("Remounting root to /dev/base");
|
2018-03-15 13:30:04 +03:00
|
|
|
sprintf(tmp, "mount %s %s /dev/base", root_type, root);
|
|
|
|
system(tmp);
|
|
|
|
|
2018-07-02 07:21:27 +03:00
|
|
|
TRACE_("Mounting tmpfs to /");
|
2018-12-11 06:41:24 +03:00
|
|
|
system("mount tmpfs x,755 /");
|
2018-03-15 13:30:04 +03:00
|
|
|
|
2018-07-02 07:21:27 +03:00
|
|
|
TRACE_("Migrating root...");
|
2018-07-18 06:37:33 +03:00
|
|
|
copy_directory("/dev/base","/",0660,0,0);
|
2018-12-11 06:41:24 +03:00
|
|
|
system("mount tmpfs x,755 /dev/base");
|
2018-03-15 13:30:04 +03:00
|
|
|
|
|
|
|
if (strstr(root, "/dev/ram") != NULL) {
|
|
|
|
char * tmp = strdup(root);
|
|
|
|
char * c = strchr(tmp, ',');
|
|
|
|
if (c) {
|
|
|
|
*c = '\0';
|
|
|
|
}
|
2018-07-02 07:21:27 +03:00
|
|
|
TRACE_("Freeing ramdisk at %s", tmp);
|
2018-03-15 13:30:04 +03:00
|
|
|
free_ramdisk(tmp);
|
|
|
|
free(tmp);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|