Skip to content

Commit

Permalink
nspawn: optionally tint the background color of a container
Browse files Browse the repository at this point in the history
  • Loading branch information
poettering committed Jan 23, 2024
1 parent 9ebd115 commit 3d8ba7b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
15 changes: 15 additions & 0 deletions man/systemd-nspawn.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1711,6 +1711,21 @@ After=sys-subsystem-net-devices-ens1.device</programlisting>

<xi:include href="version-info.xml" xpointer="v242"/></listitem>
</varlistentry>

<varlistentry>
<term><option>--background=<replaceable>COLOR</replaceable></option></term>

<listitem><para>Change the terminal background color to the specified ANSI color as long as the
container runs. The color specified should be an ANSI X3.64 SGR background color, i.e. strings such
as <literal>40</literal>, <literal>41</literal>, …, <literal>47</literal>, <literal>48;2;…</literal>,
<literal>48;5;…</literal>. See <ulink
url="https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_(Select_Graphic_Rendition)_parameters">ANSI
Escape Code (Wikipedia)</ulink> for details. Assign an empty string to disable any coloring.</para>

<xi:include href="version-info.xml" xpointer="v256"/>
</listitem>
</varlistentry>

</variablelist>

</refsect2>
Expand Down
2 changes: 1 addition & 1 deletion shell-completion/bash/systemd-nspawn
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ _systemd_nspawn() {
--pivot-root --property --private-users --private-users-ownership --network-namespace-path
--network-ipvlan --network-veth-extra --network-zone -p --port --system-call-filter --overlay
--overlay-ro --settings --rlimit --hostname --no-new-privileges --oom-score-adjust --cpu-affinity
--resolv-conf --timezone --root-hash-sig'
--resolv-conf --timezone --root-hash-sig --background'
)

_init_completion || return
Expand Down
22 changes: 19 additions & 3 deletions src/nspawn/nspawn.c
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ static bool arg_suppress_sync = false;
static char *arg_settings_filename = NULL;
static Architecture arg_architecture = _ARCHITECTURE_INVALID;
static ImagePolicy *arg_image_policy = NULL;
static char *arg_background = NULL;

STATIC_DESTRUCTOR_REGISTER(arg_directory, freep);
STATIC_DESTRUCTOR_REGISTER(arg_template, freep);
Expand Down Expand Up @@ -272,6 +273,7 @@ STATIC_DESTRUCTOR_REGISTER(arg_sysctl, strv_freep);
STATIC_DESTRUCTOR_REGISTER(arg_bind_user, strv_freep);
STATIC_DESTRUCTOR_REGISTER(arg_settings_filename, freep);
STATIC_DESTRUCTOR_REGISTER(arg_image_policy, image_policy_freep);
STATIC_DESTRUCTOR_REGISTER(arg_background, freep);

static int handle_arg_console(const char *arg) {
if (streq(arg, "help")) {
Expand Down Expand Up @@ -450,6 +452,7 @@ static int help(void) {
" --console=MODE Select how stdin/stdout/stderr and /dev/console are\n"
" set up for the container.\n"
" -P --pipe Equivalent to --console=pipe\n\n"
" --background=COLOR Set ANSI color for background\n"
"%3$sCredentials:%4$s\n"
" --set-credential=ID:VALUE\n"
" Pass a credential with literal value to container.\n"
Expand Down Expand Up @@ -745,6 +748,7 @@ static int parse_argv(int argc, char *argv[]) {
ARG_BIND_USER,
ARG_SUPPRESS_SYNC,
ARG_IMAGE_POLICY,
ARG_BACKGROUND,
};

static const struct option options[] = {
Expand Down Expand Up @@ -819,6 +823,7 @@ static int parse_argv(int argc, char *argv[]) {
{ "bind-user", required_argument, NULL, ARG_BIND_USER },
{ "suppress-sync", required_argument, NULL, ARG_SUPPRESS_SYNC },
{ "image-policy", required_argument, NULL, ARG_IMAGE_POLICY },
{ "background", required_argument, NULL, ARG_BACKGROUND },
{}
};

Expand Down Expand Up @@ -1608,6 +1613,12 @@ static int parse_argv(int argc, char *argv[]) {
return r;
break;

case ARG_BACKGROUND:
r = free_and_strdup_warn(&arg_background, optarg);
if (r < 0)
return r;
break;

case '?':
return -EINVAL;

Expand Down Expand Up @@ -5339,9 +5350,14 @@ static int run_container(
return log_error_errno(r, "Failed to create PTY forwarder: %m");

if (arg_console_width != UINT_MAX || arg_console_height != UINT_MAX)
(void) pty_forward_set_width_height(forward,
arg_console_width,
arg_console_height);
(void) pty_forward_set_width_height(
forward,
arg_console_width,
arg_console_height);

if (!isempty(arg_background))
(void) pty_forward_set_background_color(forward, arg_background);

break;

default:
Expand Down

0 comments on commit 3d8ba7b

Please sign in to comment.