Skip to content

Commit fce59ef

Browse files
authored
Merge pull request #1327 from kernelkit/container-limits
Container limits
2 parents a42b424 + 3a9d324 commit fce59ef

File tree

13 files changed

+337
-46
lines changed

13 files changed

+337
-46
lines changed

board/common/rootfs/etc/finit.d/available/[email protected]

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@
55
# 'podman load', must not have a timeout.
66
sysv log:prio:local1,tag:%i kill:30 pid:!/run/container:%i.pid \
77
pre:0,/usr/sbin/container cleanup:0,/usr/sbin/container \
8+
cgroup.system,delegate \
89
[2345] <!> :%i container -n %i -- container %i

board/common/rootfs/usr/sbin/container

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -411,10 +411,20 @@ create()
411411
logging="--log-driver syslog"
412412
fi
413413

414+
# Build resource limit arguments
415+
resource=""
416+
if [ -n "$memory" ]; then
417+
resource="$resource --memory=$memory"
418+
fi
419+
if [ -n "$cpu_limit" ]; then
420+
resource="$resource --cpu-quota=$cpu_limit"
421+
fi
422+
414423
# When we get here we've already fetched, or pulled, the image
415-
args="$args --read-only --replace --quiet --cgroup-parent=containers $caps"
424+
args="$args --read-only --replace --quiet $caps"
425+
args="$args --cgroups=enabled --cgroupns=host --cgroup-parent=system/container@$name"
416426
args="$args --restart=$restart --systemd=false --tz=local $privileged"
417-
args="$args $vol $mount $hostname $entrypoint $env $port $logging"
427+
args="$args $vol $mount $hostname $entrypoint $env $port $logging $resource"
418428
pidfile=/run/container:${name}.pid
419429

420430
[ -n "$quiet" ] || log "---------------------------------------"
@@ -716,6 +726,8 @@ options:
716726
--log-path PATH Path for k8s-file log pipe
717727
-m, --mount HOST:DEST Bind mount a read-only file inside a container
718728
--manual Do not start container automatically after creation
729+
--memory BYTES Memory limit in bytes (supports K/M/G suffix)
730+
--cpu-limit LIMIT CPU limit in millicores (1000m = 100% of 1 core)
719731
-n, --name NAME Alternative way of supplying name to start/stop/restart
720732
--privileged Give container extended privileges
721733
-p, --publish PORT Publish ports when creating container
@@ -836,6 +848,14 @@ while [ "$1" != "" ]; do
836848
--manual)
837849
manual=true
838850
;;
851+
--memory)
852+
shift
853+
memory="$1"
854+
;;
855+
--cpu-limit)
856+
shift
857+
cpu_limit="$1"
858+
;;
839859
-n | --name)
840860
shift
841861
name="$1"

package/finit/0001-Increase-MAX_ID_LEN-to-support-longer-service-identi.patch

Lines changed: 0 additions & 35 deletions
This file was deleted.

package/finit/finit.hash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# From https://github.com/troglobit/finit/releases/
2-
sha256 7c128119129324050ff7e5b56d0f33fa152fe254d035c0d0c6f72dc75d6786f3 finit-4.14.tar.gz
2+
sha256 7ccbcead4e3e6734c81a8c5445f4a27738f19a4ab367d702513a201db9b618c7 finit-4.15-rc1.tar.gz
33

44
# Locally calculated
55
sha256 868cb6c5414933a48db11186042cfe65c87480d326734bc6cf0e4b19b4a2e52a LICENSE

package/finit/finit.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#
55
################################################################################
66

7-
FINIT_VERSION = 4.14
7+
FINIT_VERSION = 4.15-rc1
88
FINIT_SITE = https://github.com/troglobit/finit/releases/download/$(FINIT_VERSION)
99
FINIT_LICENSE = MIT
1010
FINIT_LICENSE_FILES = LICENSE

src/confd/src/containers.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,25 @@ static int add(const char *name, struct lyd_node *cif)
294294
fprintf(fp, " --checksum sha512:%s", string);
295295
}
296296

297+
/* Add resource limits for Podman to enforce via cgroups */
298+
node = lydx_get_descendant(lyd_child(cif), "resource-limit", NULL);
299+
if (node) {
300+
struct lyd_node *mem_node, *cpu_node;
301+
302+
/* Memory limit in KiB, Podman accepts with 'k' suffix */
303+
mem_node = lydx_get_descendant(lyd_child(node), "memory", NULL);
304+
if (mem_node)
305+
fprintf(fp, " --memory %sk", lyd_get_value(mem_node));
306+
307+
/* CPU limit in millicores, convert to quota (microseconds per 100ms) */
308+
cpu_node = lydx_get_descendant(lyd_child(node), "cpu", NULL);
309+
if (cpu_node) {
310+
uint32_t millicores = strtoul(lyd_get_value(cpu_node), NULL, 10);
311+
uint32_t quota = millicores * 100; /* 1000m → 100000µs, 2000m → 200000µs */
312+
fprintf(fp, " --cpu-limit %u", quota);
313+
}
314+
}
315+
297316
fprintf(fp, " create %s %s", name, image);
298317

299318
if ((string = lydx_get_cattr(cif, "command")))

src/confd/yang/confd/infix-containers.yang

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ module infix-containers {
2222
prefix infix-sys;
2323
}
2424

25+
revision 2025-12-09 {
26+
description "Add resource management:
27+
- Add resource-limit container with memory and cpu configuration.
28+
- Add resource-usage operational data for live resource usage statistics.";
29+
reference "internal";
30+
}
31+
2532
revision 2025-10-12 {
2633
description "Two major changes:
2734
- Add dedicated 'ident' type for container and volume names.
@@ -341,6 +348,86 @@ module infix-containers {
341348
}
342349
}
343350

351+
container resource-limit {
352+
description "Resource limits for the container.";
353+
354+
leaf memory {
355+
description "Maximum memory limit in kibibytes, default: unlimited.";
356+
type uint64;
357+
units "KiB";
358+
}
359+
360+
leaf cpu {
361+
description "CPU limit in millicores, default: unlimited.
362+
363+
Millicores represent thousandths of a CPU core:
364+
500 = 0.5 cores (50% of one core)
365+
1000 = 1.0 cores (one full core)
366+
2000 = 2.0 cores (two full cores)
367+
3500 = 3.5 cores
368+
369+
This is converted to cgroup cpu.quota internally.";
370+
type uint32;
371+
units "millicores";
372+
}
373+
}
374+
375+
container resource-usage {
376+
description "Runtime container resource usage statistics.";
377+
config false;
378+
379+
leaf memory {
380+
description "Used memory in kibibytes.";
381+
type uint64;
382+
units "KiB";
383+
}
384+
385+
leaf cpu {
386+
description "CPU usage percentage.";
387+
type decimal64 {
388+
fraction-digits 2;
389+
}
390+
units "percent";
391+
}
392+
393+
container block-io {
394+
description "Block I/O statistics";
395+
396+
leaf read {
397+
description "Block I/O read in kibibytes.";
398+
type uint64;
399+
units "KiB";
400+
}
401+
402+
leaf write {
403+
description "Block I/O write in kibibytes.";
404+
type uint64;
405+
units "KiB";
406+
}
407+
}
408+
409+
container net-io {
410+
description "Network I/O statistics";
411+
412+
leaf received {
413+
description "Network I/O received in kibibytes.";
414+
type uint64;
415+
units "KiB";
416+
}
417+
418+
leaf sent {
419+
description "Network I/O sent in kibibytes.";
420+
type uint64;
421+
units "KiB";
422+
}
423+
}
424+
425+
leaf pids {
426+
description "Number of processes/threads.";
427+
type uint32;
428+
}
429+
}
430+
344431
list mount {
345432
description "Files, content, and directories to mount inside container.";
346433
key name;

src/confd/yang/containers.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# -*- sh -*-
22
MODULES=(
33
"infix-interfaces -e containers"
4-
"infix-containers@2025-10-12.yang"
4+
"infix-containers@2025-12-09.yang"
55
)

0 commit comments

Comments
 (0)