ssh.scm 23 KB

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