From 66005c453d5b4f229e469b8a6a92e95f813989d7 Mon Sep 17 00:00:00 2001 From: Ryan Moeller Date: Thu, 14 May 2020 23:38:11 +0000 Subject: [PATCH] jail: Add exec.prepare and exec.release command hooks This change introduces new jail command hooks that run before and after any other actions. The exec.prepare hook can be used for example to invoke a script that checks if the jail's root exists, creating it if it does not. Since arbitrary variables in jail.conf can be passed to the command, it can be pretty useful for templating jails. An example use case for exec.release would be to remove the filesystem of an ephemeral jail. The names "prepare" and "release" are borrowed from the names of similar hooks in libvirt. Reviewed by: jamie, manpages, mmacy Approved by: mmacy (mentor) MFC after: 1 week Differential Revision: https://reviews.freebsd.org/D24829 --- usr.sbin/jail/command.c | 3 ++- usr.sbin/jail/config.c | 2 ++ usr.sbin/jail/jail.8 | 12 +++++++++++- usr.sbin/jail/jail.c | 2 ++ usr.sbin/jail/jailp.h | 2 ++ 5 files changed, 19 insertions(+), 2 deletions(-) diff --git a/usr.sbin/jail/command.c b/usr.sbin/jail/command.c index e2e2984aabf3..a5c1839849fd 100644 --- a/usr.sbin/jail/command.c +++ b/usr.sbin/jail/command.c @@ -148,7 +148,8 @@ next_command(struct cfjail *j) if (j->comstring == NULL || j->comstring->len == 0 || (create_failed && (comparam == IP_EXEC_PRESTART || comparam == IP_EXEC_CREATED || comparam == IP_EXEC_START || - comparam == IP_COMMAND || comparam == IP_EXEC_POSTSTART))) + comparam == IP_COMMAND || comparam == IP_EXEC_POSTSTART || + comparam == IP_EXEC_PREPARE))) continue; switch (run_command(j)) { case -1: diff --git a/usr.sbin/jail/config.c b/usr.sbin/jail/config.c index e66d955ca218..234c4e257976 100644 --- a/usr.sbin/jail/config.c +++ b/usr.sbin/jail/config.c @@ -71,8 +71,10 @@ static const struct ipspec intparams[] = { [IP_EXEC_JAIL_USER] = {"exec.jail_user", PF_INTERNAL}, [IP_EXEC_POSTSTART] = {"exec.poststart", PF_INTERNAL}, [IP_EXEC_POSTSTOP] = {"exec.poststop", PF_INTERNAL}, + [IP_EXEC_PREPARE] = {"exec.prepare", PF_INTERNAL}, [IP_EXEC_PRESTART] = {"exec.prestart", PF_INTERNAL}, [IP_EXEC_PRESTOP] = {"exec.prestop", PF_INTERNAL}, + [IP_EXEC_RELEASE] = {"exec.release", PF_INTERNAL}, [IP_EXEC_CREATED] = {"exec.created", PF_INTERNAL}, [IP_EXEC_START] = {"exec.start", PF_INTERNAL}, [IP_EXEC_STOP] = {"exec.stop", PF_INTERNAL}, diff --git a/usr.sbin/jail/jail.8 b/usr.sbin/jail/jail.8 index b813daff3b53..b43eb765292b 100644 --- a/usr.sbin/jail/jail.8 +++ b/usr.sbin/jail/jail.8 @@ -25,7 +25,7 @@ .\" .\" $FreeBSD$ .\" -.Dd April 17, 2020 +.Dd May 14, 2020 .Dt JAIL 8 .Os .Sh NAME @@ -726,6 +726,11 @@ not be created or removed, as appropriate. .Pp The pseudo-parameters are: .Bl -tag -width indent +.It Va exec.prepare +Command(s) to run in the system environment to prepare a jail for creation. +These commands are executed before assigning IP addresses and mounting +filesystems, so they may be used to create a new jail filesystem if it does +not already exist. .It Va exec.prestart Command(s) to run in the system environment before a jail is created. .It Va exec.created @@ -760,6 +765,11 @@ A typical command to run is .Dq sh /etc/rc.shutdown jail . .It Va exec.poststop Command(s) to run in the system environment after a jail is removed. +.It Va exec.release +Command(s) to run in the system environment after all other actions are done. +These commands are executed after unmounting filesystems and removing IP +addresses, so they may be used to remove a jail filesystem if it is no longer +needed. .It Va exec.clean Run commands in a clean environment. The environment is discarded except for diff --git a/usr.sbin/jail/jail.c b/usr.sbin/jail/jail.c index bedeb7f5efc5..920e573b9149 100644 --- a/usr.sbin/jail/jail.c +++ b/usr.sbin/jail/jail.c @@ -87,6 +87,7 @@ static struct permspec perm_sysctl[] = { static const enum intparam startcommands[] = { IP__NULL, + IP_EXEC_PREPARE, #ifdef INET IP__IP4_IFADDR, #endif @@ -126,6 +127,7 @@ static const enum intparam stopcommands[] = { #ifdef INET IP__IP4_IFADDR, #endif + IP_EXEC_RELEASE, IP__NULL }; diff --git a/usr.sbin/jail/jailp.h b/usr.sbin/jail/jailp.h index ee71c0e8001b..0ed0755f74b6 100644 --- a/usr.sbin/jail/jailp.h +++ b/usr.sbin/jail/jailp.h @@ -87,8 +87,10 @@ enum intparam { IP_EXEC_JAIL_USER, /* Run jailed commands as this user */ IP_EXEC_POSTSTART, /* Commands run outside jail after creating */ IP_EXEC_POSTSTOP, /* Commands run outside jail after removing */ + IP_EXEC_PREPARE, /* Commands run outside jail before addrs and mounting */ IP_EXEC_PRESTART, /* Commands run outside jail before creating */ IP_EXEC_PRESTOP, /* Commands run outside jail before removing */ + IP_EXEC_RELEASE, /* Commands run outside jail after addrs and unmounted */ IP_EXEC_CREATED, /* Commands run outside jail right after it was started */ IP_EXEC_START, /* Commands run inside jail on creation */ IP_EXEC_STOP, /* Commands run inside jail on removal */