aboutsummaryrefslogtreecommitdiffstats
path: root/layers/console.nix
blob: 417b761c48b1ed745f548248540cd3f2e1693293 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# A shell on the serial console, i.e. in the run script's terminal: root,
# logged in, unless services.getty.autologinUser says otherwise (another
# user, or null for a login prompt, which needs a password).  Small, so
# better imported by another layer than a layer, with an image, of its
# own:
#   imports = [ /path/to/firecracker/layers/console.nix ];
# though that works, too:
#   --arg layers '[ layers/console.nix ./my-layer.nix ]'
# Without it, the guest has no console (no getty, no kbd), and the way in
# is ssh (see users.users.<name>.openssh.authorizedKeys in a layer).
{ lib, pkgs, ... }:
let
  # A serial line, unlike a pty, doesn't get told when the terminal's size
  # changes: the guest only knows the size the run script saw at boot
  # (systemd.tty.rows/columns.ttyS0).  So ask the terminal (ESC[6n, the
  # cursor's position, after moving it as far as it goes, then back), and
  # set what it answers.
  resize = pkgs.writeShellScriptBin "resize" ''
    # usage: resize  — sets the terminal's size (stty rows and cols) to
    # what the terminal says it is; for a serial console, whose size
    # doesn't follow the terminal's.
    set -eu
    export PATH=${lib.makeBinPath [ pkgs.coreutils ]}
    tty=$(tty) || { echo "resize: not a terminal" >&2; exit 1; }
    old=$(stty -g)
    trap 'stty "$old"' EXIT
    stty raw -echo
    printf '\0337\033[999;999H\033[6n\0338' > "$tty"
    if ! IFS='[;' read -r -s -t 1 -d R _ rows cols ||
        ! test "''${rows:-x}" -gt 0 2>/dev/null || ! test "''${cols:-x}" -gt 0 2>/dev/null; then
      echo "resize: no answer from the terminal" >&2
      exit 1
    fi
    stty rows "$rows" cols "$cols"
  '';
in
{
  # NixOS's gettys come with the console (services/ttys/getty.nix), and
  # with them kbd and systemd-vconsole-setup, which has nothing to do
  # here, but would still get started at boot.
  console.enable = true;
  systemd.units."systemd-vconsole-setup.service".enable = lib.mkForce false;

  # Only on ttyS0 (console= on the kernel command line, which systemd's
  # getty generator goes by), not on tty1.
  services.getty.autologinUser = lib.mkDefault "root";
  systemd.targets.getty.wants = lib.mkForce [ ];

  # On the serial console, the size follows the terminal's before each
  # prompt (so a tmux attached there gets the right one), until the
  # terminal doesn't answer.  A tmux attached there doesn't learn of later
  # changes: detach (C-b d) and attach again, or run resize.
  environment.systemPackages = [ resize ];
  programs.bash.interactiveShellInit = ''
    case $(tty 2>/dev/null) in
      /dev/ttyS*)
        __fc_resize() {
          test -n "''${__fc_noresize-}" || resize 2>/dev/null || __fc_noresize=1
        }
        PROMPT_COMMAND=(__fc_resize "''${PROMPT_COMMAND[@]}")
    esac
  '';
}