-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontainer.c
More file actions
191 lines (153 loc) · 4.44 KB
/
container.c
File metadata and controls
191 lines (153 loc) · 4.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#define _GNU_SOURCE
#include<sched.h>
#include<stdio.h>
#include<stdlib.h>
#include<sys/wait.h>
#include<unistd.h>
#include<signal.h>
#include<string.h>
#include<sys/utsname.h>
#include<sys/mount.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#define STACK_SIZE 1024*1024
static char child_stack[STACK_SIZE];
void errExit(char *msg){
printf("%s failed\n", msg);
exit(EXIT_FAILURE);
}
static void cgroup_setup(pid_t child_pid){
char str_child_pid[10];
sprintf(str_child_pid, "%d", child_pid);
char str_pid_write[200] = "echo ";
strcat(str_pid_write, str_child_pid);
strcat(str_pid_write," > /sys/fs/cgroup/memory/pa3/tasks");
//system("mkdir /sys/fs/cgroup/memory/pa3");
mkdir("/sys/fs/cgroup/memory/pa3", S_IRWXU);
system("echo \"50000000\" > /sys/fs/cgroup/memory/pa3/memory.limit_in_bytes");
system("echo \"0\" > /sys/fs/cgroup/memory/pa3/memory.swappiness");
system(str_pid_write);
}
static void netns_setup(char x[5], char y[5]){
char cmd[200] = " ";
strcpy(cmd,"ip link add veth");
strcat(cmd,x);
strcat(cmd," type veth peer name veth");
strcat(cmd,y);
system(cmd);
strcpy(cmd,"ip netns add vnet");
strcat(cmd,x);
system(cmd);
strcpy(cmd,"ip link set veth");
strcat(cmd,x);
strcat(cmd," netns vnet");
strcat(cmd,x);
system(cmd);
strcpy(cmd,"ip -n vnet");
strcat(cmd,x);
strcat(cmd," addr add 10.0.");
strcat(cmd,x);
strcat(cmd,".0/24 dev veth");
strcat(cmd,x);
system(cmd);
strcpy(cmd,"ip -n vnet");
strcat(cmd,x);
strcat(cmd," link set veth");
strcat(cmd,x);
strcat(cmd," up");
system(cmd);
strcpy(cmd,"ip -n vnet");
strcat(cmd,x);
strcat(cmd," link set lo up");
system(cmd);
strcpy(cmd,"ip addr add 10.0.");
strcat(cmd,y);
strcat(cmd,".0/24 dev veth");
strcat(cmd,y);
system(cmd);
strcpy(cmd,"ip link set veth");
strcat(cmd,y);
strcat(cmd," up");
system(cmd);
strcpy(cmd,"ip -n vnet");
strcat(cmd,x);
strcat(cmd," route add 10.0.");
strcat(cmd,y);
strcat(cmd,".0/24 dev veth");
strcat(cmd,x);
system(cmd);
strcpy(cmd,"ip route add 10.0.");
strcat(cmd,x);
strcat(cmd,".0/24 dev veth");
strcat(cmd,y);
system(cmd);
}
static int childFunc(void *arg){
sleep(2);
char **args = arg;
struct utsname uts;
printf("PID of child process within container is %ld\n\n",(long)getpid());
if(uname(&uts) == -1)
errExit("uname");
printf("Hostname before changing hostname is %s\n\n", uts.nodename);
if(sethostname(args[2],strlen(args[2])) == -1)
errExit("sethostname");
if(uname(&uts) == -1)
errExit("uname");
printf("Hostname after changing hostname is %s\n\n", uts.nodename);
int n = atoi(args[3]);
int x = 2*n-1, y = 2*n;
char path[50] = "/var/run/netns/vnet";
char x_str[5];
sprintf(x_str, "%d", x);
strcat(path,x_str);
int fd = open(path, O_RDONLY);
if(fd == -1)
errExit("open in child");
if(setns(fd, CLONE_NEWNET) == -1)
errExit("setns");
if(chdir(args[1])==-1)
errExit("chdir");
if(chroot(args[1])==-1)
errExit("chroot");
mount("proc", "/proc", "proc", 0, NULL);
char *args_shell[2];
args_shell[0] = "/bin/bash";
args_shell[1] = NULL;
execve(args_shell[0], args_shell, NULL);
}
int main(int argc, char *argv[]){
int flags = CLONE_NEWNS | CLONE_NEWPID | CLONE_NEWUTS | CLONE_NEWNET | SIGCHLD;
if(argc != 4){
printf("Insufficient number of arguements.\n");
printf("Please pass rootfs, hostname and container_index in that particular order\n");
printf("Exiting...\n");
exit(EXIT_FAILURE);
}
int n = atoi(argv[3]);
int x = 2*n-1, y = 2*n;
char path[50] = "/var/run/netns/vnet";
char x_str[5], y_str[5];
sprintf(x_str, "%d", x);
sprintf(y_str, "%d", y);
strcat(path,x_str);
int fd = open(path, O_RDONLY);
if(fd == -1){
netns_setup(x_str, y_str);
fd = open(path, O_RDONLY);
if(fd == -1)
errExit("open in parent");
else
close(fd);
}
pid_t child_pid = clone(childFunc, child_stack + STACK_SIZE, flags, (void *)argv);
if(child_pid == -1){
errExit("clone");
}
cgroup_setup(child_pid);
if(waitpid(child_pid, NULL, 0) == -1){
errExit("waitpid");
}
exit(EXIT_SUCCESS);
}