forked from containers/crun
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate.c
183 lines (152 loc) · 5.99 KB
/
update.c
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
/*
* crun - OCI runtime written in C
*
* Copyright (C) 2017, 2018, 2019 Giuseppe Scrivano <[email protected]>
* crun is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* crun is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with crun. If not, see <http://www.gnu.org/licenses/>.
*/
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <argp.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include "crun.h"
#include "libcrun/container.h"
#include "libcrun/utils.h"
static char doc[] = "OCI runtime";
static char *resources = NULL;
static libcrun_context_t crun_context;
enum
{
FIRST_VALUE = 1000,
BLKIO_WEIGHT = FIRST_VALUE,
CPU_PERIOD,
CPU_QUOTA,
CPU_SHARE,
CPU_RT_PERIOD,
CPU_RT_RUNTIME,
CPUSET_CPUS,
CPUSET_MEMS,
KERNEL_MEMORY,
KERNEL_MEMORY_TCP,
MEMORY,
MEMORY_RESERVATION,
MEMORY_SWAP,
PIDS_LIMIT,
LAST_VALUE,
};
struct description_s
{
int id;
const char *section;
const char *name;
int numeric;
};
static struct description_s descriptors[] = { { BLKIO_WEIGHT, "blockIO", "weight", 1 },
{ CPU_PERIOD, "cpu", "period", 1 },
{ CPU_QUOTA, "cpu", "quota", 1 },
{ CPU_SHARE, "cpu", "shares", 1 },
{ CPU_RT_PERIOD, "cpu", "realtimePeriod", 1 },
{ CPU_RT_RUNTIME, "cpu", "realtimeRuntime", 1 },
{ CPUSET_CPUS, "cpu", "cpus", 0 },
{ CPUSET_MEMS, "cpu", "mems", 0 },
{ KERNEL_MEMORY, "memory", "kernel", 1 },
{ KERNEL_MEMORY_TCP, "memory", "kernelTCP", 1 },
{ MEMORY, "memory", "limit", 1 },
{ MEMORY_RESERVATION, "memory", "reservation", 1 },
{ MEMORY_SWAP, "memory", "swap", 1 },
{ PIDS_LIMIT, "pids", "limit", 1 },
{ 0 } };
static struct libcrun_update_value_s *values;
size_t values_len = 0;
static void
set_value (int id, const char *value)
{
values = xrealloc (values, (values_len + 1) * sizeof (struct libcrun_update_value_s));
values[values_len].section = descriptors[id - FIRST_VALUE].section;
values[values_len].name = descriptors[id - FIRST_VALUE].name;
values[values_len].numeric = descriptors[id - FIRST_VALUE].numeric;
values[values_len].value = value;
values_len++;
}
static struct argp_option options[]
= { { "resources", 'r', "FILE", 0, "path to the file containing the resources to update", 0 },
{ "blkio-weight", BLKIO_WEIGHT, "VALUE", 0, "Specifies per cgroup weight", 0 },
{ "cpu-period", CPU_PERIOD, "VALUE", 0, "CPU CFS period to be used for hardcapping", 0 },
{ "cpu-quota", CPU_QUOTA, "VALUE", 0, "CPU CFS hardcap limit", 0 },
{ "cpu-share", CPU_SHARE, "VALUE", 0, "CPU shares", 0 },
{ "cpu-rt-period", CPU_RT_PERIOD, "VALUE", 0, "CPU realtime period to be used for hardcapping", 0 },
{ "cpu-rt-runtime", CPU_RT_RUNTIME, "VALUE", 0, "CPU realtime hardcap limit", 0 },
{ "cpuset-cpus", CPUSET_CPUS, "VALUE", 0, "CPU(s) to use", 0 },
{ "cpuset-mems", CPUSET_MEMS, "VALUE", 0, "Memory node(s) to use", 0 },
{ "kernel-memory", KERNEL_MEMORY, "VALUE", 0, "Kernel memory limit", 0 },
{ "kernel-memory-tcp", KERNEL_MEMORY_TCP, "VALUE", 0, "Kernel memory limit for tcp buffer", 0 },
{ "memory", MEMORY, "VALUE", 0, "Memory limit", 0 },
{ "memory-reservation", MEMORY_RESERVATION, "VALUE", 0, "Memory reservation or soft_limit", 0 },
{ "memory-swap", MEMORY_SWAP, "VALUE", 0, "Total memory usage", 0 },
{ "pids-limit", PIDS_LIMIT, "VALUE", 0, "Maximum number of pids allowed in the container", 0 },
{
0,
} };
static char args_doc[] = "update [OPTION]... CONTAINER";
static error_t
parse_opt (int key, char *arg, struct argp_state *state)
{
switch (key)
{
case 'r':
resources = argp_mandatory_argument (arg, state);
break;
case ARGP_KEY_NO_ARGS:
libcrun_fail_with_error (0, "please specify a ID for the container");
case BLKIO_WEIGHT:
case CPU_PERIOD:
case CPU_QUOTA:
case CPU_SHARE:
case CPU_RT_PERIOD:
case CPU_RT_RUNTIME:
case CPUSET_CPUS:
case CPUSET_MEMS:
case KERNEL_MEMORY:
case KERNEL_MEMORY_TCP:
case MEMORY:
case MEMORY_RESERVATION:
case MEMORY_SWAP:
case PIDS_LIMIT:
set_value (key, argp_mandatory_argument (arg, state));
break;
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}
static struct argp run_argp = { options, parse_opt, args_doc, doc, NULL, NULL, NULL };
int
crun_command_update (struct crun_global_arguments *global_args, int argc, char **argv, libcrun_error_t *err)
{
int first_arg = 0, ret;
argp_parse (&run_argp, argc, argv, ARGP_IN_ORDER, &first_arg, &crun_context);
crun_assert_n_args (argc - first_arg, 1, 1);
ret = init_libcrun_context (&crun_context, argv[first_arg], global_args, err);
if (UNLIKELY (ret < 0))
return ret;
if (resources == NULL)
{
ret = libcrun_container_update_from_values (&crun_context, argv[first_arg], values, values_len, err);
free (values);
return ret;
}
return libcrun_container_update_from_file (&crun_context, argv[first_arg], resources, err);
}