[systemd-devel] nsapwned container terminal background color

Zbigniew Jędrzejewski-Szmek zbyszek at in.waw.pl
Mon Mar 20 04:11:34 UTC 2017


On Sun, Mar 19, 2017 at 09:30:05PM +0000, Zbigniew Jędrzejewski-Szmek wrote:
> On Sun, Mar 19, 2017 at 04:37:53PM +0000, arnaud gaboury wrote:
> > I am trying with no success to have one background terminal color for the
> > host and another one for the container.
> > 
> > Some settings:
> > - on host:
> > window manager : i3
> > terminal : urxvt-256
> > 
> > -on container:
> > no X, it is a server.
> > 
> > Until now, I have tried to write a wrapper script to urxvt on host. This
> > script is to test value of $HOST or $TERM, then fire urxvt with background
> > color as option. This does not work.
> > 
> > I have to figure out  that when i log my container, I am first on an urxvt
> > terminal which become, once I am logged as user on container, a xterm.
> > I am totally lost where to write instruction as the terminal color
> > background: on host or container? and what app shall be tested to set the
> > environment and decide the bg color (agetty?) ?
> 
> Are you starting the container manually using systemd-nspawn in a terminal
> emulator window? If yes, then the following works (tested with gnome-terminal,
> but I think urxvt supports the same sequences):
> 
> [Unit]
> DefaultDependencies=no
> Conflicts=shutdown.target
> 
> [Service]
> StandardOutput=tty
> ExecStart=/usr/bin/printf '\033]11;darkgreen\007'
> ExecStop=/usr/bin/printf '\033]11;black\007'
> Type=oneshot
> RemainAfterExit=true
> 
> [Install]
> WantedBy=basic.target
> 
> (It would be nicer to undo the setting and return to whatever the old
> background was, but I don't know how to do this.)
I figured it out, although it's a bit ugly ;)

==> /etc/systemd/system/bgcolor.service <==
[Unit]
DefaultDependencies=no
Conflicts=shutdown.target
Description=Set terminal background

[Service]
StandardInput=tty
StandardOutput=tty
ExecStart=/usr/local/bin/bgcolor.sh start /run/bgcolor.txt teal
ExecStop=/usr/local/bin/bgcolor.sh stop /run/bgcolor.txt
Type=oneshot
RemainAfterExit=true

[Install]
WantedBy=sysinit.target

==> /usr/local/bin/bgcolor.sh <==
#!/bin/sh -e

file="$2"
field=11      # background

if [ "$1" == "start" ]; then
    color="$3"

    oldstty=$(stty -g)
    stty raw -echo min 0
    #          OSC   Ps  ;Pt ST
    printf "\033]${field};?\033\\"
    IFS=';' read -t1 -r -d '\' answer
    echo "$answer" | sed 's/^.*\;//;s/[^rgb:0-9a-f/]//g' >"$file"
    stty "$oldstty"

    printf "\033]${field};${color}\007"
elif [ "$1" == "stop" ]; then
    color=$(cat "$file")
    printf "\033]${field};${color}\007"
fi

This will set the background to teal during early boot, storing the
original color in the specified file, and reset it back during
shutdown. It seems to interfere with the systemd console output
(newlines get messed up), and sometimes the restoration does not
work. I suspect that both issues are caused by interference of
systemd printing to the console concurrently with the script. Most
likely putting a verify&retry loop around the query for original
color would fix the second issue.

Zbyszek




More information about the systemd-devel mailing list