system: Add store-directory-prefix to boot-parameters.
[jackhill/guix/guix.git] / gnu / machine / ssh.scm
CommitLineData
fa9edf09 1;;; GNU Guix --- Functional package management for GNU
e8134442 2;;; Copyright © 2019 Jakob L. Kreuze <zerodaysfordays@sdf.org>
ea6e2299 3;;; Copyright © 2020 Ludovic Courtès <ludo@gnu.org>
fa9edf09
JK
4;;;
5;;; This file is part of GNU Guix.
6;;;
7;;; GNU Guix is free software; you can redistribute it and/or modify it
8;;; under the terms of the GNU General Public License as published by
9;;; the Free Software Foundation; either version 3 of the License, or (at
10;;; your option) any later version.
11;;;
12;;; GNU Guix is distributed in the hope that it will be useful, but
13;;; WITHOUT ANY WARRANTY; without even the implied warranty of
14;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15;;; GNU General Public License for more details.
16;;;
17;;; You should have received a copy of the GNU General Public License
18;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
19
20(define-module (gnu machine ssh)
9c70c460 21 #:use-module (gnu bootloader)
fa9edf09
JK
22 #:use-module (gnu machine)
23 #:autoload (gnu packages gnupg) (guile-gcrypt)
fa9edf09 24 #:use-module (gnu system)
fd3119db
JK
25 #:use-module (gnu system file-systems)
26 #:use-module (gnu system uuid)
a396dd01 27 #:use-module ((gnu services) #:select (sexp->system-provenance))
fd3119db 28 #:use-module (guix diagnostics)
fa9edf09
JK
29 #:use-module (guix gexp)
30 #:use-module (guix i18n)
31 #:use-module (guix modules)
32 #:use-module (guix monads)
3033d59a 33 #:use-module (guix pki)
fa9edf09
JK
34 #:use-module (guix records)
35 #:use-module (guix remote)
5c793753 36 #:use-module (guix scripts system reconfigure)
fa9edf09
JK
37 #:use-module (guix ssh)
38 #:use-module (guix store)
fd3119db 39 #:use-module (guix utils)
3033d59a 40 #:use-module (gcrypt pk-crypto)
fdbba544 41 #:use-module (ice-9 format)
fa9edf09 42 #:use-module (ice-9 match)
3033d59a 43 #:use-module (ice-9 textual-ports)
9c70c460 44 #:use-module (srfi srfi-1)
ea6e2299 45 #:use-module (srfi srfi-9)
fa9edf09 46 #:use-module (srfi srfi-19)
5c793753 47 #:use-module (srfi srfi-26)
2c8e04f1 48 #:use-module (srfi srfi-34)
fa9edf09
JK
49 #:use-module (srfi srfi-35)
50 #:export (managed-host-environment-type
51
52 machine-ssh-configuration
53 machine-ssh-configuration?
54 machine-ssh-configuration
55
56 machine-ssh-configuration-host-name
d84e9b75 57 machine-ssh-configuration-build-locally?
3033d59a 58 machine-ssh-configuration-authorize?
a396dd01 59 machine-ssh-configuration-allow-downgrades?
fa9edf09
JK
60 machine-ssh-configuration-port
61 machine-ssh-configuration-user
ed15dfcf 62 machine-ssh-configuration-host-key
fa9edf09
JK
63 machine-ssh-configuration-session))
64
65;;; Commentary:
66;;;
67;;; This module implements remote evaluation and system deployment for
53f21b3f 68;;; machines that are accessible over SSH and have a known host-name. In the
fa9edf09
JK
69;;; sense of the broader "machine" interface, we describe the environment for
70;;; such machines as 'managed-host.
71;;;
72;;; Code:
73
74\f
75;;;
76;;; Parameters for the SSH client.
77;;;
78
79(define-record-type* <machine-ssh-configuration> machine-ssh-configuration
80 make-machine-ssh-configuration
81 machine-ssh-configuration?
3033d59a
JK
82 (host-name machine-ssh-configuration-host-name) ; string
83 (system machine-ssh-configuration-system) ; string
84 (build-locally? machine-ssh-configuration-build-locally? ; boolean
d84e9b75 85 (default #t))
3033d59a
JK
86 (authorize? machine-ssh-configuration-authorize? ; boolean
87 (default #t))
a396dd01
LC
88 (allow-downgrades? machine-ssh-configuration-allow-downgrades? ; boolean
89 (default #f))
3033d59a 90 (port machine-ssh-configuration-port ; integer
d84e9b75 91 (default 22))
3033d59a 92 (user machine-ssh-configuration-user ; string
d84e9b75 93 (default "root"))
3033d59a 94 (identity machine-ssh-configuration-identity ; path to a private key
d84e9b75 95 (default #f))
3033d59a 96 (session machine-ssh-configuration-session ; session
ed15dfcf
LC
97 (default #f))
98 (host-key machine-ssh-configuration-host-key ; #f | string
d84e9b75 99 (default #f)))
fa9edf09
JK
100
101(define (machine-ssh-session machine)
102 "Return the SSH session that was given in MACHINE's configuration, or create
103one from the configuration's parameters if one was not provided."
104 (maybe-raise-unsupported-configuration-error machine)
105 (let ((config (machine-configuration machine)))
106 (or (machine-ssh-configuration-session config)
107 (let ((host-name (machine-ssh-configuration-host-name config))
108 (user (machine-ssh-configuration-user config))
109 (port (machine-ssh-configuration-port config))
ed15dfcf
LC
110 (identity (machine-ssh-configuration-identity config))
111 (host-key (machine-ssh-configuration-host-key config)))
2617d956
LC
112 (unless host-key
113 (warning (G_ "<machine-ssh-configuration> without a 'host-key' \
114is deprecated~%")))
fa9edf09
JK
115 (open-ssh-session host-name
116 #:user user
117 #:port port
ed15dfcf
LC
118 #:identity identity
119 #:host-key host-key)))))
fa9edf09
JK
120
121\f
122;;;
123;;; Remote evaluation.
124;;;
125
5ea7537b
JK
126(define (machine-become-command machine)
127 "Return as a list of strings the program and arguments necessary to run a
128shell command with escalated privileges for MACHINE's configuration."
129 (if (string= "root" (machine-ssh-configuration-user
130 (machine-configuration machine)))
131 '()
132 '("/run/setuid-programs/sudo" "-n" "--")))
133
fa9edf09
JK
134(define (managed-host-remote-eval machine exp)
135 "Internal implementation of 'machine-remote-eval' for MACHINE instances with
136an environment type of 'managed-host."
137 (maybe-raise-unsupported-configuration-error machine)
2c8e04f1
JK
138 (let ((config (machine-configuration machine)))
139 (remote-eval exp (machine-ssh-session machine)
140 #:build-locally?
141 (machine-ssh-configuration-build-locally? config)
142 #:system
4cc5e520
JK
143 (machine-ssh-configuration-system config)
144 #:become-command
145 (machine-become-command machine))))
fa9edf09
JK
146
147\f
fd3119db
JK
148;;;
149;;; Safety checks.
150;;;
151
ea6e2299
LC
152;; Assertion to be executed remotely. This abstraction exists to allow us to
153;; gather a list of expressions to be evaluated and eventually evaluate them
154;; all at once instead of one by one. (This is pretty much a monad.)
155(define-record-type <remote-assertion>
156 (remote-assertion exp proc)
157 remote-assertion?
158 (exp remote-assertion-expression)
159 (proc remote-assertion-procedure))
160
161(define-syntax-rule (remote-let ((var exp)) body ...)
162 "Return a <remote-assertion> that binds VAR to the result of evaluating EXP,
163a gexp, remotely, and evaluate BODY in that context."
164 (remote-assertion exp (lambda (var) body ...)))
165
fd3119db 166(define (machine-check-file-system-availability machine)
ea6e2299
LC
167 "Return a list of <remote-assertion> that raise a '&message' error condition
168if any of the file-systems specified in MACHINE's 'system' declaration do not
169exist on the machine."
fd3119db
JK
170 (define file-systems
171 (filter (lambda (fs)
172 (and (file-system-mount? fs)
173 (not (member (file-system-type fs)
174 %pseudo-file-system-types))
1c3b709e
S
175 ;; Don't try to validate network file systems.
176 (not (string-prefix? "nfs" (file-system-type fs)))
fd3119db
JK
177 (not (memq 'bind-mount (file-system-flags fs)))))
178 (operating-system-file-systems (machine-operating-system machine))))
179
180 (define (check-literal-file-system fs)
ea6e2299
LC
181 (remote-let ((errno #~(catch 'system-error
182 (lambda ()
183 (stat #$(file-system-device fs))
184 #t)
185 (lambda args
186 (system-error-errno args)))))
fd3119db 187 (when (number? errno)
d51bfe24 188 (raise (formatted-message (G_ "device '~a' not found: ~a")
fd3119db 189 (file-system-device fs)
d51bfe24 190 (strerror errno))))))
fd3119db
JK
191
192 (define (check-labeled-file-system fs)
193 (define remote-exp
02460db0
SB
194 (with-imported-modules (source-module-closure
195 '((gnu build file-systems)))
fd3119db
JK
196 #~(begin
197 (use-modules (gnu build file-systems))
198 (find-partition-by-label #$(file-system-label->string
199 (file-system-device fs))))))
200
ea6e2299 201 (remote-let ((result remote-exp))
fd3119db 202 (unless result
d51bfe24 203 (raise (formatted-message (G_ "no file system with label '~a'")
fd3119db 204 (file-system-label->string
d51bfe24 205 (file-system-device fs)))))))
fd3119db
JK
206
207 (define (check-uuid-file-system fs)
208 (define remote-exp
209 (with-imported-modules (source-module-closure
210 '((gnu build file-systems)
211 (gnu system uuid)))
212 #~(begin
213 (use-modules (gnu build file-systems)
214 (gnu system uuid))
215
0dd04b99
MC
216 (let ((uuid (uuid #$(uuid->string (file-system-device fs))
217 '#$(uuid-type (file-system-device fs)))))
218 (find-partition-by-uuid uuid)))))
fd3119db 219
ea6e2299 220 (remote-let ((result remote-exp))
fd3119db 221 (unless result
d51bfe24
LC
222 (raise (formatted-message (G_ "no file system with UUID '~a'")
223 (uuid->string (file-system-device fs)))))))
ea6e2299
LC
224
225 (append (map check-literal-file-system
226 (filter (lambda (fs)
227 (string? (file-system-device fs)))
228 file-systems))
229 (map check-labeled-file-system
230 (filter (lambda (fs)
231 (file-system-label? (file-system-device fs)))
232 file-systems))
233 (map check-uuid-file-system
234 (filter (lambda (fs)
235 (uuid? (file-system-device fs)))
236 file-systems))))
fd3119db
JK
237
238(define (machine-check-initrd-modules machine)
ea6e2299
LC
239 "Return a list of <remote-assertion> that raise a '&message' error condition
240if any of the modules needed by 'needed-for-boot' file systems in MACHINE are
241not available in the initrd."
fd3119db
JK
242 (define file-systems
243 (filter file-system-needed-for-boot?
244 (operating-system-file-systems (machine-operating-system machine))))
245
246 (define (missing-modules fs)
247 (define remote-exp
248 (let ((device (file-system-device fs)))
249 (with-imported-modules (source-module-closure
250 '((gnu build file-systems)
251 (gnu build linux-modules)
252 (gnu system uuid)))
dac7dd1b
MO
253 #~(begin
254 (use-modules (gnu build file-systems)
255 (gnu build linux-modules)
256 (gnu system uuid))
fd3119db 257
dac7dd1b
MO
258 (define dev
259 #$(cond ((string? device) device)
260 ((uuid? device) #~(find-partition-by-uuid
261 (string->uuid
262 #$(uuid->string device))))
263 ((file-system-label? device)
264 #~(find-partition-by-label
265 #$(file-system-label->string device)))))
fd3119db 266
dac7dd1b
MO
267 (missing-modules dev '#$(operating-system-initrd-modules
268 (machine-operating-system machine)))))))
ea6e2299
LC
269
270 (remote-let ((missing remote-exp))
271 (unless (null? missing)
272 (raise (condition
273 (&message
8bc74505 274 (message (format #f (G_ "missing modules for ~a:~{ ~a~}~%")
ea6e2299
LC
275 (file-system-device fs)
276 missing))))))))
277
278 (map missing-modules file-systems))
fd3119db 279
a396dd01
LC
280(define* (machine-check-forward-update machine)
281 "Check whether we are making a forward update for MACHINE. Depending on its
282'allow-upgrades?' field, raise an error or display a warning if we are
283potentially downgrading it."
284 (define config
285 (machine-configuration machine))
286
287 (define validate-reconfigure
288 (if (machine-ssh-configuration-allow-downgrades? config)
289 warn-about-backward-reconfigure
290 ensure-forward-reconfigure))
291
292 (remote-let ((provenance #~(call-with-input-file
293 "/run/current-system/provenance"
294 read)))
295 (define channels
296 (sexp->system-provenance provenance))
297
298 (check-forward-update validate-reconfigure
299 #:current-channels channels)))
300
2c8e04f1
JK
301(define (machine-check-building-for-appropriate-system machine)
302 "Raise a '&message' error condition if MACHINE is configured to be built
303locally and the 'system' field does not match the '%current-system' reported
304by MACHINE."
305 (let ((config (machine-configuration machine))
306 (system (remote-system (machine-ssh-session machine))))
307 (when (and (machine-ssh-configuration-build-locally? config)
308 (not (string= system (machine-ssh-configuration-system config))))
d51bfe24 309 (raise (formatted-message (G_ "incorrect target system\
ea6e2299 310 ('~a' was given, while the system reports that it is '~a')~%")
2c8e04f1 311 (machine-ssh-configuration-system config)
d51bfe24 312 system)))))
2c8e04f1 313
fd3119db
JK
314(define (check-deployment-sanity machine)
315 "Raise a '&message' error condition if it is clear that deploying MACHINE's
316'system' declaration would fail."
ea6e2299
LC
317 (define assertions
318 (append (machine-check-file-system-availability machine)
a396dd01
LC
319 (machine-check-initrd-modules machine)
320 (list (machine-check-forward-update machine))))
ea6e2299
LC
321
322 (define aggregate-exp
323 ;; Gather all the expressions so that a single round-trip is enough to
324 ;; evaluate all the ASSERTIONS remotely.
325 #~(map (lambda (file)
326 (false-if-exception (primitive-load file)))
327 '#$(map (lambda (assertion)
328 (scheme-file "remote-assertion.scm"
329 (remote-assertion-expression assertion)))
330 assertions)))
331
332 ;; First check MACHINE's system type--an incorrect value for 'system' would
333 ;; cause subsequent invocations of 'remote-eval' to fail.
334 (machine-check-building-for-appropriate-system machine)
335
336 (mlet %store-monad ((values (machine-remote-eval machine aggregate-exp)))
337 (for-each (lambda (proc value)
338 (proc value))
339 (map remote-assertion-procedure assertions)
340 values)
341 (return #t)))
fd3119db
JK
342
343\f
fa9edf09
JK
344;;;
345;;; System deployment.
346;;;
347
fa9edf09
JK
348(define (machine-boot-parameters machine)
349 "Monadic procedure returning a list of 'boot-parameters' for the generations
350of MACHINE's system profile, ordered from most recent to oldest."
351 (define bootable-kernel-arguments
352 (@@ (gnu system) bootable-kernel-arguments))
353
354 (define remote-exp
355 (with-extensions (list guile-gcrypt)
356 (with-imported-modules (source-module-closure '((guix config)
357 (guix profiles)))
358 #~(begin
359 (use-modules (guix config)
360 (guix profiles)
361 (ice-9 textual-ports))
362
363 (define %system-profile
364 (string-append %state-directory "/profiles/system"))
365
366 (define (read-file path)
367 (call-with-input-file path
368 (lambda (port)
369 (get-string-all port))))
370
371 (map (lambda (generation)
372 (let* ((system-path (generation-file-name %system-profile
373 generation))
374 (boot-parameters-path (string-append system-path
375 "/parameters"))
376 (time (stat:mtime (lstat system-path))))
377 (list generation
378 system-path
379 time
380 (read-file boot-parameters-path))))
381 (reverse (generation-numbers %system-profile)))))))
382
383 (mlet* %store-monad ((generations (machine-remote-eval machine remote-exp)))
384 (return
385 (map (lambda (generation)
386 (match generation
387 ((generation system-path time serialized-params)
388 (let* ((params (call-with-input-string serialized-params
389 read-boot-parameters))
390 (root (boot-parameters-root-device params))
391 (label (boot-parameters-label params)))
392 (boot-parameters
393 (inherit params)
394 (label
395 (string-append label " (#"
396 (number->string generation) ", "
397 (let ((time (make-time time-utc 0 time)))
398 (date->string (time-utc->date time)
399 "~Y-~m-~d ~H:~M"))
400 ")"))
401 (kernel-arguments
402 (append (bootable-kernel-arguments system-path root)
403 (boot-parameters-kernel-arguments params))))))))
404 generations))))
405
9c70c460
JK
406(define-syntax-rule (with-roll-back should-roll-back? mbody ...)
407 "Catch exceptions that arise when binding MBODY, a monadic expression in
408%STORE-MONAD, and collect their arguments in a &deploy-error condition, with
409the 'should-roll-back' field set to SHOULD-ROLL-BACK?"
410 (catch #t
411 (lambda ()
412 mbody ...)
413 (lambda args
414 (raise (condition (&deploy-error
415 (should-roll-back should-roll-back?)
416 (captured-args args)))))))
417
fa9edf09
JK
418(define (deploy-managed-host machine)
419 "Internal implementation of 'deploy-machine' for MACHINE instances with an
420environment type of 'managed-host."
421 (maybe-raise-unsupported-configuration-error machine)
3033d59a
JK
422 (when (machine-ssh-configuration-authorize?
423 (machine-configuration machine))
424 (unless (file-exists? %public-key-file)
d51bfe24 425 (raise (formatted-message (G_ "no signing key '~a'. \
3033d59a 426have you run 'guix archive --generate-key?'")
d51bfe24 427 %public-key-file)))
3033d59a
JK
428 (remote-authorize-signing-key (call-with-input-file %public-key-file
429 (lambda (port)
430 (string->canonical-sexp
431 (get-string-all port))))
4cc5e520
JK
432 (machine-ssh-session machine)
433 (machine-become-command machine)))
fd3119db
JK
434 (mlet %store-monad ((_ (check-deployment-sanity machine))
435 (boot-parameters (machine-boot-parameters machine)))
d97ce204 436 (let* ((os (machine-operating-system machine))
5c793753
JK
437 (eval (cut machine-remote-eval machine <>))
438 (menu-entries (map boot-parameters->menu-entry boot-parameters))
439 (bootloader-configuration (operating-system-bootloader os))
440 (bootcfg (operating-system-bootcfg os menu-entries)))
441 (mbegin %store-monad
9c70c460
JK
442 (with-roll-back #f
443 (switch-to-system eval os))
444 (with-roll-back #t
445 (mbegin %store-monad
446 (upgrade-shepherd-services eval os)
447 (install-bootloader eval bootloader-configuration bootcfg)))))))
448
449\f
450;;;
451;;; Roll-back.
452;;;
453
454(define (roll-back-managed-host machine)
455 "Internal implementation of 'roll-back-machine' for MACHINE instances with
456an environment type of 'managed-host."
457 (define remote-exp
458 (with-extensions (list guile-gcrypt)
459 (with-imported-modules (source-module-closure '((guix config)
460 (guix profiles)))
461 #~(begin
462 (use-modules (guix config)
463 (guix profiles))
464
465 (define %system-profile
466 (string-append %state-directory "/profiles/system"))
467
468 (define target-generation
469 (relative-generation %system-profile -1))
470
471 (if target-generation
472 (switch-to-generation %system-profile target-generation)
473 'error)))))
474
475 (define roll-back-failure
476 (condition (&message (message (G_ "could not roll-back machine")))))
477
478 (mlet* %store-monad ((boot-parameters (machine-boot-parameters machine))
479 (_ -> (if (< (length boot-parameters) 2)
480 (raise roll-back-failure)))
481 (entries -> (map boot-parameters->menu-entry
482 (list (second boot-parameters))))
eaf09639
MÁAV
483 (locale -> (boot-parameters-locale
484 (second boot-parameters)))
582cf925
MÁAV
485 (store-dir -> (boot-parameters-store-directory-prefix
486 (second boot-parameters)))
9c70c460
JK
487 (old-entries -> (map boot-parameters->menu-entry
488 (drop boot-parameters 2)))
489 (bootloader -> (operating-system-bootloader
490 (machine-operating-system machine)))
491 (bootcfg (lower-object
492 ((bootloader-configuration-file-generator
493 (bootloader-configuration-bootloader
494 bootloader))
495 bootloader entries
eaf09639 496 #:locale locale
582cf925 497 #:store-directory-prefix store-dir
9c70c460
JK
498 #:old-entries old-entries)))
499 (remote-result (machine-remote-eval machine remote-exp)))
500 (when (eqv? 'error remote-result)
501 (raise roll-back-failure))))
fa9edf09
JK
502
503\f
504;;;
505;;; Environment type.
506;;;
507
508(define managed-host-environment-type
509 (environment-type
510 (machine-remote-eval managed-host-remote-eval)
511 (deploy-machine deploy-managed-host)
9c70c460 512 (roll-back-machine roll-back-managed-host)
fa9edf09 513 (name 'managed-host-environment-type)
53f21b3f 514 (description "Provisioning for machines that are accessible over SSH
fa9edf09
JK
515and have a known host-name. This entails little more than maintaining an SSH
516connection to the host.")))
517
518(define (maybe-raise-unsupported-configuration-error machine)
519 "Raise an error if MACHINE's configuration is not an instance of
520<machine-ssh-configuration>."
521 (let ((config (machine-configuration machine))
522 (environment (environment-type-name (machine-environment machine))))
523 (unless (and config (machine-ssh-configuration? config))
d51bfe24 524 (raise (formatted-message (G_ "unsupported machine configuration '~a'
fa9edf09
JK
525for environment of type '~a'")
526 config
d51bfe24 527 environment)))))
a396dd01
LC
528
529;; Local Variables:
530;; eval: (put 'remote-let 'scheme-indent-function 1)
531;; End: