Debian Codegraph @

initrd/qemuinit.c

raw symbols SCIP
1 /**
2 qemuinit; init for quick boot inside qemu.
3 Copyright 2009 Junichi Uekawa
4
5 This init is dedicated for qemubuilder use, with goal of having a
6 very fast boot time by only initializing the essential modules
7 required for qemubuilder operation.
8
9 */
10 #include <stdio.h>
11 #include <unistd.h>
12 #include <stdlib.h>
13 #include <fcntl.h>
14 #include <string.h>
15 #include <sys/types.h>
16 #include <sys/stat.h>
17 #include <sys/wait.h>
18 #include <sys/mount.h>
19 #include <sys/reboot.h>
20 #include <sys/sysmacros.h>
21
22 #include "../parameter.h"
23 #include "../file.h"
24
25 #define PBUILDER_MOUNTPOINT "/var/cache/pbuilder/pbuilder-mnt"
26 #define ROOT_MOUNTPOINT "/root"
27
28 /* global variable to hold /proc/cmdline */
29 char *proc_cmdline = NULL;
30
31 void read_proc_cmdline() {
32 int fd = open("/proc/cmdline", O_RDONLY);
33 char buf[BUFSIZ];
34 int len = read(fd, buf, sizeof(buf) - 1);
35 buf[len] = 0; /* make sure it's NULL-terminated */
36 proc_cmdline = strdup(buf);
37 close(fd);
38 }
39
40 /* parse the command-line option, and return a pointer to
41 space-delimited option string */
42 const char *parse_option(const char *option) {
43 const char *s = proc_cmdline;
44 do {
45 if (!strncmp(option, s, strlen(option)) && s[strlen(option)] == '=') {
46 return s;
47 }
48 } while ((s = strchr(s, ' ')));
49 return NULL;
50 }
51
52 /*
53 Return a strdup string, optionally NULL-terminating if it's
54 space-delimited.
55 */
56 char *strdup_spacedelimit(const char *str) {
57 char *s = strdup(str);
58 char *space = strchr(str, ' ');
59 if (space) {
60 *space = 0;
61 }
62 return s;
63 }
64
65 /*
66 insert module and display error message when insmod returns an
67 error.
68 */
69 int insmod(const char *module) {
70 int ret = forkexeclp("/bin/insmod", "/bin/insmod", module, NULL);
71 if (ret) {
72 fprintf(stderr, "W: insmod of %s returned %i\n", module, ret);
73 }
74 return ret;
75 }
76
77 int main(int argc, char **argv) {
78 printf(" -> qemu-pbuilder first-stage(initrd version)\n");
79
80 umask(S_IWGRP | S_IWOTH);
81
82 chdir("/");
83 setenv("PATH",
84 "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
85 1);
86 setenv("PBUILDER_MOUNTPOINT", PBUILDER_MOUNTPOINT, 1);
87
88 mkdir("/root", 0777); /* mountpoint */
89 mkdir("/proc", 0777);
90 mkdir("/dev", 0777);
91 mkdir("/dev/pts", 0777);
92
93 if (mount("/proc", "/proc", "proc", MS_MGC_VAL, NULL) == -1) {
94 perror("mount proc");
95 }
96
97 if (mount("/dev/pts", "/dev/pts", "devpts", MS_MGC_VAL, NULL) == -1) {
98 perror("mount devpts");
99 }
100
101 read_proc_cmdline();
102
103 /* ext3 driver */
104 insmod("lib/modules/linux-live/kernel/drivers/ide/ide-core.ko");
105 insmod("lib/modules/linux-live/kernel/drivers/ide/ide-disk.ko");
106 insmod("lib/modules/linux-live/kernel/drivers/ide/ide-generic.ko");
107 insmod("lib/modules/linux-live/kernel/fs/mbcache.ko");
108 insmod("lib/modules/linux-live/kernel/fs/jbd/jbd.ko");
109 insmod("lib/modules/linux-live/kernel/fs/ext2/ext2.ko");
110 insmod("lib/modules/linux-live/kernel/fs/ext3/ext3.ko");
111 sleep(1);
112
113 /* ne2k driver */
114 insmod("lib/modules/linux-live/kernel/drivers/net/8390.ko");
115 insmod("lib/modules/linux-live/kernel/drivers/net/ne2k-pci.ko");
116 /* ARM would need different driver... smc */
117
118 if (copy_file("/proc/mounts", "/etc/mtab")) {
119 fprintf(stderr, "Cannot copy file /proc/mounts to /etc/mtab.\n");
120 }
121
122 mknod_inside_chroot("", "dev/sda", S_IFBLK | 0660, makedev(8, 0));
123 mknod_inside_chroot("", "dev/sdb", S_IFBLK | 0660, makedev(8, 16));
124 mknod_inside_chroot("", "dev/hda", S_IFBLK | 0660, makedev(3, 0));
125 mknod_inside_chroot("", "dev/hdb", S_IFBLK | 0660, makedev(3, 64));
126
127 if (mount("/dev/sda", ROOT_MOUNTPOINT, "ext3", MS_MGC_VAL, NULL) == -1) {
128 perror("mount root device try sda");
129 if (mount("/dev/hda", ROOT_MOUNTPOINT, "ext3", MS_MGC_VAL, NULL) ==
130 -1) {
131 perror("mount root device try hda");
132 }
133 }
134
135 if (mount("/dev/sdb",
136 ROOT_MOUNTPOINT PBUILDER_MOUNTPOINT,
137 "ext3",
138 MS_MGC_VAL,
139 NULL) == -1) {
140 perror("mount command device try sdb");
141 if (mount("/dev/hdb",
142 ROOT_MOUNTPOINT PBUILDER_MOUNTPOINT,
143 "ext3",
144 MS_MGC_VAL,
145 NULL) == -1) {
146 perror("mount command device try hdb");
147 }
148 }
149
150 /* debug shell to see what's there. */
151 printf("command-line inside initrd, exit to continue\n");
152 forkexeclp("/bin/sh", "/bin/sh", NULL);
153
154 if (chroot("root/")) {
155 perror("chroot");
156 }
157
158 if (chdir("/")) {
159 perror("chdir");
160 }
161
162 forkexeclp("bin/sh", "bin/sh", PBUILDER_MOUNTPOINT "/pbuilder-run", NULL);
163
164 /* debug shell inside chroot. */
165 printf("command-line inside chroot, exit to continue\n");
166 forkexeclp("bin/sh", "bin/sh", NULL);
167
168 /* turn the machine off */
169 sync();
170 sync();
171 reboot(RB_POWER_OFF);
172 return 0;
173 }