Skip to content
This repository was archived by the owner on Jan 6, 2023. It is now read-only.

Commit 6dc013f

Browse files
Gao fengGao feng
authored andcommitted
hyperStart source code
The files under src directory is the codes of hyperStart, use autogen.sh && configure && make to generate hyper-initrd.img, this image will be used to start hyper instance. Signed-off-by: Gao feng <[email protected]>
1 parent 4782098 commit 6dc013f

28 files changed

+6841
-0
lines changed

.gitignore

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
*#*#
2+
*.#*#
3+
*.a
4+
*.o
5+
*.orig
6+
*.rej
7+
.#*
8+
.git
9+
.deps
10+
init
11+
build/hyper_daemon
12+
build/hyper-initrd.img
13+
build/root/*
14+
build/daemon
15+
build/.*
16+
src/.*
17+
Makefile
18+
Makefile.in
19+
cscope.files
20+
cscope.in.out
21+
cscope.out
22+
cscope.po.out
23+
stamp-h
24+
stamp-h.in
25+
stamp-h1
26+
/config.h
27+
/config.h.in
28+
/config.log
29+
/config.status
30+
/configure
31+
/aclocal.m4
32+
/autoscan.log
33+
/autom4te.cache
34+
/compile
35+
/depcomp
36+
/install-sh
37+
/missing

Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
SUBDIRS=src build

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
The init task for hyper.
2+
exec ./autogen.sh to compile hyperinit

autogen.sh

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#!/bin/sh
2+
3+
srcdir=`dirname $0`
4+
test -z "$srcdir" && srcidr=.
5+
6+
cd $srcdir
7+
DIE=0
8+
9+
test -f src/init.c || {
10+
echo
11+
echo "You must run this script in the top-level hyperint drectory."
12+
echo
13+
DIE=1
14+
}
15+
16+
(autoconf --version) < /dev/null > /dev/null 2>&1 || {
17+
echo
18+
echo "You must have autoconf installed to generate the hyperinit."
19+
echo
20+
DIE=1
21+
}
22+
23+
(autoheader --version) < /dev/null > /dev/null 2>&1 || {
24+
echo
25+
echo "You must have autoheader installed to generate the hypernit."
26+
echo
27+
DIE=1
28+
}
29+
30+
(automake --version) < /dev/null > /dev/null 2>&1 || {
31+
echo
32+
echo "You must have automake installed to generate the hypernit."
33+
echo
34+
DIE=1
35+
}
36+
(autoreconf --version) < /dev/null > /dev/null 2>&1 || {
37+
echo
38+
echo "You must have autoreconf installed to generate the hypernit."
39+
echo
40+
DIE=1
41+
}
42+
43+
if test "$DIE" -eq 1; then
44+
exit 1
45+
fi
46+
47+
echo
48+
echo "Generating build-system with:"
49+
echo " aclocal: $(aclocal --version | head -1)"
50+
echo " autoconf: $(autoconf --version | head -1)"
51+
echo " autoheader: $(autoheader --version | head -1)"
52+
echo " automake: $(automake --version | head -1)"
53+
echo
54+
55+
rm -rf autom4te.cache
56+
57+
aclocal
58+
autoconf
59+
autoheader
60+
automake --add-missing
61+
62+
echo
63+
echo "type '$srcdir/configure' and 'make' to compile hyperinit."
64+
echo

build/Makefile.am

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
bin_PROGRAMS=hyper_daemon
2+
hyper_daemon_SOURCES=hyper_daemon.c ../src/net.c
3+
all-local:
4+
bash ./make-initrd.sh

build/hyper_daemon.c

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
#define _GNU_SOURCE
2+
#include <stdio.h>
3+
#include <sys/socket.h>
4+
#include <linux/un.h>
5+
#include <sys/types.h>
6+
#include <sys/stat.h>
7+
#include <fcntl.h>
8+
#include <unistd.h>
9+
#include <string.h>
10+
11+
#include "../src/hyper.h"
12+
#include "../src/net.h"
13+
14+
int test_sendmsg(int fd, unsigned int type, unsigned int len, char *message)
15+
{
16+
uint8_t buf[4096];
17+
18+
19+
/* send hyper info to guest */
20+
hyper_set_be32(buf, type);
21+
22+
len += 8;
23+
24+
fprintf(stdout, "type is %u, len is %u\n", type, len);
25+
hyper_set_be32(buf + 4, len);
26+
27+
if (message)
28+
memcpy(buf + 8, message, len - 8);
29+
30+
if (write(fd, buf, len) != len) {
31+
fprintf(stderr, "send SETDVM MESSAGE failed\n");
32+
return -1;
33+
}
34+
35+
fprintf(stdout, "finish sending\n");
36+
return 0;
37+
}
38+
39+
int test_sendmsg_from_file(int fd, unsigned int type, char *file)
40+
{
41+
int file_fd = open(file, O_RDONLY);
42+
uint8_t buf[4096];
43+
unsigned int len = 0;
44+
45+
if (file_fd < 0) {
46+
perror("fail to open file");
47+
return -1;
48+
}
49+
50+
while (len < 4096) {
51+
int size = read(file_fd, buf + len, sizeof(buf) - len);
52+
53+
fprintf(stdout, "read %d data\n", size);
54+
if (size < 0) {
55+
perror("fail to read data");
56+
return -1;
57+
} else if (size == 0) {
58+
/* buf[len] = '\0';*/
59+
fprintf(stdout, "get buf %s, call sendmsg\n", buf);
60+
test_sendmsg(fd, type, len, (char *)buf);
61+
break;
62+
}
63+
64+
len += size;
65+
}
66+
67+
close(file_fd);
68+
69+
if (read(fd, buf, 8) != 8) {
70+
fprintf(stderr, "read response failed\n");
71+
return -1;
72+
}
73+
74+
type = hyper_get_be32(buf);
75+
if (type != ACK) {
76+
fprintf(stderr, "incorrect type %d\n", type);
77+
return -1;
78+
}
79+
80+
return 0;
81+
}
82+
83+
int main(int argc, char *argv[])
84+
{
85+
int sock, fd = -1;
86+
struct sockaddr_un addr;
87+
uint8_t buf[8];
88+
unsigned int type;
89+
90+
unlink("/tmp/hyper.sock");
91+
92+
sock = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0);
93+
if (sock == -1) {
94+
perror("create unix socket failed");
95+
return -1;
96+
}
97+
98+
addr.sun_family = AF_UNIX;
99+
strncpy(addr.sun_path, "/tmp/hyper.sock", UNIX_PATH_MAX);
100+
addr.sun_path[UNIX_PATH_MAX - 1] = '\0';
101+
102+
if (bind(sock, ((struct sockaddr *) &addr), sizeof(addr)) == -1) {
103+
perror("bind failed");
104+
goto out;
105+
}
106+
107+
if (listen(sock, 1) == -1) {
108+
perror("bind failed");
109+
goto out;
110+
}
111+
112+
while (fd == -1)
113+
fd = accept4(sock, NULL, NULL, SOCK_CLOEXEC);
114+
115+
fprintf(stdout, "connected\n");
116+
if (read(fd, buf, 8) != 8) {
117+
fprintf(stderr, "read failed, buf %s\n", buf);
118+
goto out1;
119+
}
120+
121+
type = hyper_get_be32(buf);
122+
fprintf(stdout, "get type %d\n", type);
123+
124+
if (type != READY) {
125+
fprintf(stderr, "incorrect type %d\n", type);
126+
goto out1;
127+
}
128+
129+
fprintf(stdout, "get length %d\n", hyper_get_be32(buf + 4));
130+
131+
/* test_sendmsg_from_file(fd, SETDVM, "sethyper.json"); */
132+
if (test_sendmsg_from_file(fd, STARTPOD, "startpod.json") < 0) {
133+
fprintf(stderr, "send startpod message failed\n");
134+
goto out1;
135+
}
136+
137+
if (test_sendmsg_from_file(fd, EXECCMD, "execcmd.json") < 0) {
138+
fprintf(stderr, "send execcmd message failed\n");
139+
goto out1;
140+
}
141+
142+
if (test_sendmsg(fd, STOPPOD, 0, NULL) < 0) {
143+
fprintf(stderr, "send stoppod message failed\n");
144+
goto out1;
145+
}
146+
147+
if (read(fd, buf, 8) != 8) {
148+
fprintf(stderr, "read response failed\n");
149+
return -1;
150+
}
151+
152+
type = hyper_get_be32(buf);
153+
if (type != ACK) {
154+
fprintf(stderr, "incorrect type %d\n", type);
155+
return -1;
156+
}
157+
158+
if (read(fd, buf, 8) != 8) {
159+
fprintf(stderr, "read response failed\n");
160+
return -1;
161+
}
162+
out1:
163+
close(fd);
164+
out:
165+
close(sock);
166+
167+
return 0;
168+
}

build/kernel

2.59 MB
Binary file not shown.

0 commit comments

Comments
 (0)