Articles

Limit what a jail can use

Memory, processes, CPU, disk, firewall states and network bandwidth limits for a jail with jailops config set. What each limit does when it is reached, and how to check that it applies.

Why a ceiling

One busy jail should not take the host with it.

A jail that leaks memory, forks without end, fills the disk or opens thousands of connections is a problem for every other jail on the host. A limit makes it its own problem: the jail slows down or fails an allocation, and everything else carries on.

Limits are keys of the jail. They are enforced by the kernel and the filesystem, not by a program that watches.

Setting a limit

One key, one value.

on the host
$ jailops config set web limits.memory 2G --project shop
$ jailops config set web limits.cpu 2 --project shop
$ jailops config set web limits.pids 500 --project shop
$ jailops config set web limits.disk 20GiB --project shop

The same keys work in a profile and in a file, so a limit can be part of what you declare for a jail.

What each one does

Nothing is killed.

The limits of a jail and what happens at each
KeyAt the limit
limits.memoryAn allocation beyond it fails. The programs that asked get the error; nothing is killed.
limits.pidsfork fails while the jail has that many processes.
limits.cpuThe jail is slowed down to that many cores.
limits.diskWrites fail. The limit covers the root, the data, the volumes and the snapshots of the jail.
limits.statesThe jail's firewall rules create no new connections beyond the number. One jail cannot fill the host's table.

A jail that sets no limits.states gets a default of 10000 per rule, which a busy web or DNS server does not reach. Memory counts what the programs reserve, so a busy database needs more than it uses when idle: start generously and lower it.

Bandwidth

In each direction.

The network device of a jail can be limited for what it sends and for what it receives, in bits per second. The limit applies to everything the jail sends or receives, whoever started the connection, and it survives a restart.

20 Mbit/s out, 50 Mbit/s in
$ jailops config set web nic.eth0.egress 20M --project shop
$ jailops config set web nic.eth0.ingress 50M --project shop

Checking it

The doctor says when a limit does not apply.

The memory, process and CPU limits need the kernel's resource accounting, which is a boot setting: kern.racct.enable=1 in /boot/loader.conf. Without it a jail with such a limit still starts, but the limit is not enforced. jailops doctor fails the check sysctl.kern.racct.enable and the jail shows the condition Limits as NotEnforced, so you see it before it matters.

The memory, process and CPU limits take effect at the next start of the jail. The disk limit applies at once.

When a jail reaches a limit, the host publishes an event you can watch: What the host tells you by itself.

More articles

Try it on one server.

Try the console from the screenshots, install on a clean FreeBSD 15.1 machine, or add JailOps to a host you already run.