Artifact [e11582ede8]

Artifact e11582ede8c4822e27592ae8365cbc05a2864274:


#include <sys/types.h>
#include <sys/mount.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdarg.h>
#include <stdio.h>

int run(const char *path, ...) {
	va_list ap;
	char **argv;
	int argvMax, argvIdx;
	pid_t pid;
	int pidstat;

	pid = fork();
	if (pid == ((pid_t) -1)) {
		return(-1);
	}
	
	if (pid != 0) {
		waitpid(pid, &pidstat, 0);

		return(pidstat);
	}

	argvMax = 32;
	argv = malloc(sizeof(*argv) * argvMax);

	va_start(ap, path);

	for (argvIdx = 0; argvIdx < argvMax; argvIdx++) {
		argv[argvIdx] = va_arg(ap, char *);
		if (argv[argvIdx] == NULL) {
			break;
		}
	}
	
	va_end(ap);

	execv(path, argv);

	exit(EXIT_FAILURE);
}

int main(int argc, char **argv) {
	/*
	 * Remount root filesystem read-write
	 */
	mount("/dev/vda2", "/", "ext4", MS_REMOUNT, NULL);

	/*
	 * Mount needed filesystems
	 */
	mkdir("/dev", 0755);
	mount("devtmpfs", "/dev", "devtmpfs", 0, NULL);

	mkdir("/tmp", 0755);
	mount("tmpfs", "/tmp", "tmpfs", 0, NULL);

	mkdir("/proc", 0755);
	mount("proc", "/proc", "proc", 0, NULL);

	mkdir("/sys", 0755);
	mount("sysfs", "/sys", "sysfs", 0, NULL);

	/*
	 * Setup networking
	 */
	run("/bin/init-networking", "init-networking", NULL);
	run("/bin/init-fuse", "init-fuse", NULL);

	mkdir("/etc", 0755);
	mkdir("/bin", 0755);
	mkdir("/lib", 0755);
	mkdir("/opt", 0755);
	mkdir("/opt/appfs", 0755);

	mkdir("/var", 0755);
	mkdir("/var/cache", 0755);
	mkdir("/var/cache/appfs", 0755);
	mount("/dev/vda3", "/var/cache/appfs", "ext4", 0, NULL);

	run("/bin/appfsd", "appfsd", "/var/cache/appfs", "/opt/appfs", NULL);

	symlink(".", "/usr");
	symlink("lib", "/lib64");

	symlink("/proc/self/mounts", "/etc/mtab");

	symlink("/opt/appfs/core.appfs.rkeene.org/bash/platform/latest/bin/bash", "/bin/bash");
	symlink("/opt/appfs/core.appfs.rkeene.org/coreutils/platform/latest/bin/env", "/bin/env");

	symlink("/bin/bash", "/bin/sh");

	setenv("PATH", "/bin:/opt/appfs/core.appfs.rkeene.org/coreutils/platform/latest/bin", 1);
	run("/bin/appfs-cache", "appfs-cache", "install", "-lib", "core.appfs.rkeene.org", "glibc", NULL);
	run("/bin/appfs-cache", "appfs-cache", "install", "core.appfs.rkeene.org", "coreutils", NULL);
	run("/bin/appfs-cache", "appfs-cache", "install", "core.appfs.rkeene.org", "procps-ng", NULL);
	setenv("PATH", "/bin", 1);

	run("/bin/sh", "sh", NULL);

	/*
	 * Remount root filesystem read-only
	 */
	umount("/dev");
	umount("/tmp");
	umount("/proc");
	umount("/sys");
	umount("/opt/appfs");
	umount("/var/cache/appfs");
	mount("/dev/vda2", "/", "ext4", MS_REMOUNT | MS_RDONLY, NULL);
	sync();

	return(0);
}