You are not logged in.

#1 2013-08-05 08:55:21

EvanPurkhiser
Member
From: San Francisco
Registered: 2010-08-17
Posts: 225
Website

[Solved] Set linux virtual console colors early at boot

Hey guys,

I recently discovered that you can change the colors for your framebuffer linux terminal by simply writing some control characters with hex color values to the tty.

Something like this:

if [ "$TERM" = "linux" ]; then
    echo -en "\e]P0222222" #black    -> this is the background color as well. 
    echo -en "\e]P8222222" #darkgray
    echo -en "\e]P1803232" #darkred
    echo -en "\e]P9982b2b" #red
    echo -en "\e]P25b762f" #darkgreen
    echo -en "\e]PA89b83f" #green
    echo -en "\e]P3aa9943" #brown
    echo -en "\e]PBefef60" #yellow
    echo -en "\e]P4324c80" #darkblue
    echo -en "\e]PC2b4f98" #blue
    echo -en "\e]P5706c9a" #darkmagenta
    echo -en "\e]PD826ab1" #magenta
    echo -en "\e]P692b19e" #darkcyan
    echo -en "\e]PEa1cdcd" #cyan
    echo -en "\e]P7ffffff" #lightgray
    echo -en "\e]PFdedede" #white   -> this is the foreground color as well.
    clear #for background artifacting
fi

Is there a way to have a script like this be executed very early on in the boot process? Perhapse during the initramfs stage? I suspect I might need to setup a hook for mkinitcpio?

Or is there another way to customize the console colors?

Thanks!

Last edited by EvanPurkhiser (2013-08-17 12:25:17)

Offline

#2 2013-08-15 01:31:57

EvanPurkhiser
Member
From: San Francisco
Registered: 2010-08-17
Posts: 225
Website

Re: [Solved] Set linux virtual console colors early at boot

As I suspected this can be accomplished by adding a hook to my mkinitcpio configuration.

Here's what I have right now, however it still needs some work so that all the ttys are affected, perhaps there's still even a better way?

/usr/lib/initcpio/hooks/colors

#!/usr/bin/ash

run_hook() {
    if [ "$TERM" = "linux" ]; then
        echo -en "\e]P0222222" #black    -> this is the background color as well. 
        echo -en "\e]P8222222" #darkgray
        echo -en "\e]P1803232" #darkred
        echo -en "\e]P9982b2b" #red
        echo -en "\e]P25b762f" #darkgreen
        echo -en "\e]PA89b83f" #green
        echo -en "\e]P3aa9943" #brown
        echo -en "\e]PBefef60" #yellow
        echo -en "\e]P4324c80" #darkblue
        echo -en "\e]PC2b4f98" #blue
        echo -en "\e]P5706c9a" #darkmagenta
        echo -en "\e]PD826ab1" #magenta
        echo -en "\e]P692b19e" #darkcyan
        echo -en "\e]PEa1cdcd" #cyan
        echo -en "\e]P7ffffff" #lightgray
        echo -en "\e]PFdedede" #white   -> this is the foreground color as well.
        clear #for background artifacting
    fi
}

# vim: set ft=sh ts=4 sw=4 et:

/usr/lib/initcpio/install/colors

#!/bin/bash

build() {
    add_runscript
}

help() {
    cat <<HELPEOF
This hook will setup the console colors in early user space
HELPEOF
}

# vim: set ft=sh ts=4 sw=4 et:

I'll make this into a AUR package once I'm a little more confident in it.

Offline

#3 2013-08-15 06:12:06

EvanPurkhiser
Member
From: San Francisco
Registered: 2010-08-17
Posts: 225
Website

Re: [Solved] Set linux virtual console colors early at boot

I actually don't think I like this solution (echoing control sequences). While this does appear to work, I think it's a bit hackish. It also has some problems, namely:

  • If you issue 'reset' in your terminal the colors will be reset to the defaults.

  • The colors are only set on tty0. I've tried also echoing the control sequence for changing the colors to the other ttys at boot time, however, I don't think a VT has been initalized on them yet so it causes some strange corruption.

I took a closer look at the linux virtual terminal source code, and noticed that it does appear to be possible to properly configure the default color palette: See the con_set_cmap() function.

Digging into the VT source some more I can see that this method is only called in one place: In theg vt_ioctl function. Now, vt_ioctl sounds pretty interesting and sounds like exactly what I'm looking for. Since what I'm trying to do is similar to setting the console font, I thought maybe there could be some hints in the setfont source. Lo and behold the setfont utility interfaces with ioctl. (This was wrong, setfont doesn't use vt_ioctl..)

Where I am now: I feel like I'm really close to figuring this out. At this point I would like to see if there is a utility already written that will set the color palette using a vt_ioctl call. And if not, I'll start fiddling writing my own.

Last edited by EvanPurkhiser (2013-08-15 07:17:52)

Offline

#4 2013-08-15 06:53:26

EvanPurkhiser
Member
From: San Francisco
Registered: 2010-08-17
Posts: 225
Website

Re: [Solved] Set linux virtual console colors early at boot

Kernel parameters!!

I just realized that the default colors are actually exposed as kernel parameters. If you look just below the definition of the default colors you can see this.

For anyone wanting to give it a whirl here are the colors for Solarized Dark:

vt.default_red=0x07,0xdc,0x85,0xb5,0x26,0xd3,0x2a,0xee,0x00,0xcb,0x58,0x65,0x83,0x6c,0x93,0xfd vt.default_grn=0x36,0x32,0x99,0x89,0x8b,0x36,0xa1,0xe8,0x2b,0x4b,0x6e,0x7b,0x94,0x71,0xa1,0xf6 vt.default_blu=0x42,0x2f,0x00,0x00,0xd2,0x82,0x98,0xd5,0x36,0x16,0x75,0x83,0x96,0xc4,0xa1,0xe3

I'm still not completely happy with this solution since now I have to stick this huge string at the end of my kernel line, but this is certainly the best solution yet as it fixes the two issues mentioned above!

I will still investigate the vt_ioctl, but will now mark this thread as [Solution Available]

Offline

#5 2013-08-15 07:15:42

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,424
Website

Re: [Solved] Set linux virtual console colors early at boot

I've been following along; nice work.

Much as I like the idea, there is no way I am appending that to me kernel line, though... tongue


Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#6 2013-08-15 09:41:38

EvanPurkhiser
Member
From: San Francisco
Registered: 2010-08-17
Posts: 225
Website

Re: [Solved] Set linux virtual console colors early at boot

Definitely agreed!

Here's some proof of concept C code that will set the console colors to black and white.

It requires root to be executed, so please read through the code if you would like to try it! (If you change the file from /dev/console to /proc/self/fd/0 you can run it as your user from a tty)

The C is pretty terrible I know, I'd like to make the utility be able to read some kind of configuration file containing the colors, or maybe even just pass it a list of hexadecimal codes.

The goal here will be to do something very similar to how the consolefont mkinitcpio hook works.

Last edited by EvanPurkhiser (2013-08-15 09:49:06)

Offline

#7 2013-08-17 12:25:05

EvanPurkhiser
Member
From: San Francisco
Registered: 2010-08-17
Posts: 225
Website

Re: [Solved] Set linux virtual console colors early at boot

I think I'm finally ready to mark this topic with a big old [Solved]!

I've created two packages to handle this. One is a utility that sets the consoles color palette, and the other is a mkinitcpio hook that runs the utility during early userspace.

Offline

Board footer

Powered by FluxBB