1 /* 2 * This is a simple init for shared rootfs guests. This part should be limited 3 * to doing mounts and running stage 2 of the init process. 4 */ 5 #include <sys/mount.h> 6 #include <string.h> 7 #include <unistd.h> 8 #include <stdio.h> 9 #include <errno.h> 10 #include <linux/reboot.h> 11 12 static int run_process(char *filename) 13 { 14 char *new_argv[] = { filename, NULL }; 15 char *new_env[] = { "TERM=linux", "DISPLAY=192.168.33.1:0", NULL }; 16 17 return execve(filename, new_argv, new_env); 18 } 19 20 static int run_process_sandbox(char *filename) 21 { 22 char *new_argv[] = { filename, "/virt/sandbox.sh", NULL }; 23 char *new_env[] = { "TERM=linux", NULL }; 24 25 return execve(filename, new_argv, new_env); 26 } 27 28 static void do_mounts(void) 29 { 30 mount("hostfs", "/host", "9p", MS_RDONLY, "trans=virtio,version=9p2000.L"); 31 mount("", "/sys", "sysfs", 0, NULL); 32 mount("proc", "/proc", "proc", 0, NULL); 33 mount("devtmpfs", "/dev", "devtmpfs", 0, NULL); 34 mkdir("/dev/pts", 0755); 35 mount("devpts", "/dev/pts", "devpts", 0, NULL); 36 } 37 38 int main(int argc, char *argv[]) 39 { 40 pid_t child; 41 int status; 42 43 puts("Mounting..."); 44 45 do_mounts(); 46 47 /* get session leader */ 48 setsid(); 49 50 /* set controlling terminal */ 51 ioctl(0, TIOCSCTTY, 1); 52 53 child = fork(); 54 if (child < 0) { 55 printf("Fatal: fork() failed with %d\n", child); 56 return 0; 57 } else if (child == 0) { 58 if (access("/virt/sandbox.sh", R_OK) == 0) 59 run_process_sandbox("/bin/sh"); 60 else 61 run_process("/bin/sh"); 62 } else { 63 waitpid(child, &status, 0); 64 } 65 66 reboot(LINUX_REBOOT_CMD_RESTART); 67 68 printf("Init failed: %s\n", strerror(errno)); 69 70 return 0; 71 } 72