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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
|
;; DEBUG variant of hello-container.scm: same OS, but with the
;; container-fingerprint activation and the DBG2 monitor loop that
;; dumps process/service/syslog state into the container log.
;;
;; Minimal Guix system container with the 'hello' package and an SSH
;; server, so it can be inspected over ssh (nsenter is not always an
;; option). Build with --network so sshd is reachable on the host:
;;
;; guix system container --network debug-container.scm
;;
;; then start it via the run-guix-system-container daemon and connect:
;;
;; ssh -p 2222 -i hello-container-key root@127.0.0.1
(use-modules (gnu)
(gnu packages bash)
(gnu services base)
(gnu services shepherd)
(gnu services sysctl)
(srfi srfi-1))
(use-package-modules base)
(use-service-modules ssh)
;; Pre-generated SSH host key (etc/ in this repository),
;; installed at activation and pinned via (generate-host-keys? #f), so
;; every container build presents the SAME host key and known_hosts
;; does not complain about a changed identification.
(define ssh-host-key (local-file "../etc/ssh-container-key"
"ssh_host_ecdsa_key"))
(define ssh-host-key.pub (local-file "../etc/ssh-container-key.pub"
"ssh_host_ecdsa_key.pub"))
(operating-system
(host-name "debug-container")
(timezone "Europe/Amsterdam")
(locale "en_US.utf8")
(bootloader (bootloader-configuration
(bootloader grub-bootloader)
(targets '("/dev/sdX"))))
(file-systems (cons (file-system
(device (file-system-label "does-not-matter"))
(mount-point "/")
(type "ext4"))
%base-file-systems))
;; Users are mandatory in an OS declaration. Give root a password so
;; we could also log in with a password ("hello").
(users (cons* (user-account
(name "root")
(uid 0)
(group "root")
(password (crypt "hello" "$6$abc$0123456789abcdef$")))
(user-account
(name "hello")
(group "users"))
%base-user-accounts))
;; Globally installed packages; 'hello' and 'bash' end up in
;; /run/current-system/profile/bin
(packages (cons* bash hello %base-packages))
(services
(append
(list
;; Plain openssh as in the Guix manual example; host keys are
;; pinned (see ssh-host-key above), not generated at boot.
;; Password-less root login with etc/hello-container-key.
(service openssh-service-type
(openssh-configuration
(port-number 2222)
(permit-root-login #t)
(password-authentication? #f)
(generate-host-keys? #f)
(authorized-keys
`(("root" ,(local-file "../etc/hello-container-key.pub"
"authorized_keys"))))))
;; Install the pinned ECDSA host key into /etc/ssh where sshd
;; looks for it by default.
(simple-service 'container-ssh-host-key
activation-service-type
(with-imported-modules '((guix build utils))
#~(begin
(use-modules (guix build utils))
(mkdir-p "/etc/ssh")
(copy-file #$ssh-host-key
"/etc/ssh/ssh_host_ecdsa_key")
(chmod "/etc/ssh/ssh_host_ecdsa_key" #o600)
(copy-file #$ssh-host-key.pub
"/etc/ssh/ssh_host_ecdsa_key.pub")
(chmod "/etc/ssh/ssh_host_ecdsa_key.pub" #o644))))
;; Show the daemon fingerprint (passed in the
;; GUIX_CONTAINER_FINGERPRINT environment variable by
;; run-guix-system-container) in /etc/issue and
;; /etc/container-fingerprint, so a login immediately reveals
;; which daemon generation started this container.
(simple-service 'container-fingerprint
activation-service-type
#~(begin
(display "ACT: fingerprint begin\n")
(force-output)
(let ((fp (getenv "GUIX_CONTAINER_FINGERPRINT")))
(when fp
(call-with-output-file "/etc/container-fingerprint"
(lambda (port) (display fp port)))
(false-if-exception (delete-file "/etc/issue"))
(call-with-output-file "/etc/issue"
(lambda (port)
(display
(string-append "\nhello-container ["
fp
"]\nThis is a Guix system container.\n\n")
port)))))
;; DEBUG: detached monitor that survives into
;; the shepherd phase, reporting process,
;; service and syslog state to the container
;; log (sshd errors end up in syslog).
(system
"/run/current-system/profile/bin/bash -c 'while true; do \
echo ===DBG2===; \
/run/current-system/profile/bin/ps -ef; \
/run/current-system/profile/bin/herd status 2>&1; \
/run/current-system/profile/bin/tail -40 /var/log/messages 2>&1; echo; \
/run/current-system/profile/bin/sleep 20; done' &")
(display "ACT: fingerprint done\n")
(force-output)))
;; sshd needs the privilege separation directory /var/empty; on
;; a full system it is created as the home directory of some
;; system account, but a minimal container has none.
(simple-service 'container-var-empty
activation-service-type
(with-imported-modules '((guix build utils))
#~(begin
(use-modules (guix build utils))
(mkdir-p "/var/empty"))))
;; In a container sysctl (writes /proc/sys) and udev (coldplug on
;; the static /dev the container runtime sets up) are useless and
;; their start-up hangs the whole boot: urandom-seed requires
;; udev, user-processes requires urandom-seed, and ssh-daemon
;; requires user-processes. Provide no-ops instead.
(simple-service 'container-sysctl
shepherd-root-service-type
(list (shepherd-service
(provision '(sysctl))
(one-shot? #t)
(start #~(const #t)))))
(simple-service 'container-udev
shepherd-root-service-type
(list (shepherd-service
(provision '(udev))
(one-shot? #t)
(start #~(const #t))))))
(modify-services %base-services
;; Drop the guix service: its activation runs 'guix archive
;; --generate-key' at every boot, which stalled the container
;; boot; a minimal container does not need guix-daemon anyway.
(delete guix-service-type)
(delete sysctl-service-type)
(delete udev-service-type)))))
|