From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:470:142:3::10]:40865) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hsWRo-0004CB-5K for guix-patches@gnu.org; Tue, 30 Jul 2019 14:02:05 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hsWRm-00078n-PD for guix-patches@gnu.org; Tue, 30 Jul 2019 14:02:04 -0400 Received: from debbugs.gnu.org ([209.51.188.43]:42321) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1hsWRm-00078d-LC for guix-patches@gnu.org; Tue, 30 Jul 2019 14:02:02 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1hsWRm-0006d6-G3 for guix-patches@gnu.org; Tue, 30 Jul 2019 14:02:02 -0400 Subject: [bug#36846] [PATCH v3] machine: Implement safety checks. Resent-Message-ID: From: zerodaysfordays@sdf.lonestar.org (Jakob L. Kreuze) References: <87lfwgii14.fsf@sdf.lonestar.org> <87mugvv2ef.fsf@sdf.lonestar.org> Date: Tue, 30 Jul 2019 13:58:59 -0400 In-Reply-To: <87mugvv2ef.fsf@sdf.lonestar.org> (Jakob L. Kreuze's message of "Tue, 30 Jul 2019 13:49:12 -0400") Message-ID: <87imrjv1y4.fsf_-_@sdf.lonestar.org> MIME-Version: 1.0 Content-Type: multipart/signed; boundary="=-=-="; micalg=pgp-sha256; protocol="application/pgp-signature" List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guix-patches-bounces+kyle=kyleam.com@gnu.org Sender: "Guix-patches" To: 36846@debbugs.gnu.org --=-=-= Content-Type: text/plain Content-Transfer-Encoding: quoted-printable * gnu/machine/ssh.scm (machine-check-file-system-availability) (machine-check-initrd-modules, check-deployment-sanity): New variable. (deploy-managed-host): Perform safety checks before deploying. =2D-- gnu/machine/ssh.scm | 127 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 126 insertions(+), 1 deletion(-) diff --git a/gnu/machine/ssh.scm b/gnu/machine/ssh.scm index 552eafa9de..d60adccf67 100644 =2D-- a/gnu/machine/ssh.scm +++ b/gnu/machine/ssh.scm @@ -20,6 +20,9 @@ #:use-module (gnu machine) #:autoload (gnu packages gnupg) (guile-gcrypt) #:use-module (gnu system) + #:use-module (gnu system file-systems) + #:use-module (gnu system uuid) + #:use-module (guix diagnostics) #:use-module (guix gexp) #:use-module (guix i18n) #:use-module (guix modules) @@ -29,6 +32,7 @@ #:use-module (guix scripts system reconfigure) #:use-module (guix ssh) #:use-module (guix store) + #:use-module (guix utils) #:use-module (ice-9 match) #:use-module (srfi srfi-19) #:use-module (srfi srfi-26) @@ -98,6 +102,127 @@ an environment type of 'managed-host." (maybe-raise-unsupported-configuration-error machine) (remote-eval exp (machine-ssh-session machine))) =20 + +;;; +;;; Safety checks. +;;; + +(define (machine-check-file-system-availability machine) + "Raise a '&message' error condition if any of the file-systems specified= in +MACHINE's 'system' declaration do not exist on the machine." + (define file-systems + (filter (lambda (fs) + (and (file-system-mount? fs) + (not (member (file-system-type fs) + %pseudo-file-system-types)) + (not (memq 'bind-mount (file-system-flags fs))))) + (operating-system-file-systems (machine-operating-system machi= ne)))) + + (define (check-literal-file-system fs) + (define remote-exp + #~(catch 'system-error + (lambda () + (stat #$(file-system-device fs)) + #t) + (lambda args + (system-error-errno args)))) + + (mlet %store-monad ((errno (machine-remote-eval machine remote-exp))) + (when (number? errno) + (raise (condition + (&message (message (format #f (G_ "device '~a' not found: = ~a") + (file-system-device fs) + (strerror errno))))))) + (return #t))) + + (define (check-labeled-file-system fs) + (define remote-exp + (with-imported-modules '((gnu build file-systems)) + #~(begin + (use-modules (gnu build file-systems)) + (find-partition-by-label #$(file-system-label->string + (file-system-device fs)))))) + + (mlet %store-monad ((result (machine-remote-eval machine remote-exp))) + (unless result + (raise (condition (&message + (message (format #f (G_ "no file system with la= bel '~a'") + (file-system-label->string + (file-system-device fs)))))))) + (return #t))) + + (define (check-uuid-file-system fs) + (define remote-exp + (with-imported-modules '((gnu build file-systems)) + #~(begin + (use-modules (gnu build file-systems)) + (find-partition-by-uuid #$(file-system-device fs))))) + + (mlet %store-monad ((result (machine-remote-eval machine remote-exp))) + (unless result + (raise (condition (&message + (message (format #f (G_ "no file system with UU= ID '~a'") + (uuid->string (file-system-dev= ice fs)))))))) + (return #t))) + + (mbegin %store-monad + (mapm %store-monad check-literal-file-system + (filter (lambda (fs) + (string? (file-system-device fs))) + file-systems)) + (mapm %store-monad check-labeled-file-system + (filter (lambda (fs) + (file-system-label? (file-system-device fs))) + file-systems)) + (mapm %store-monad check-uuid-file-system + (filter (lambda (fs) + (uuid? (file-system-device fs))) + file-systems)))) + +(define (machine-check-initrd-modules machine) + "Raise a '&message' error condition if any of the modules needed by +'needed-for-boot' file systems in MACHINE are not available in the initrd." + (define file-systems + (filter file-system-needed-for-boot? + (operating-system-file-systems (machine-operating-system machi= ne)))) + + (define (missing-modules fs) + (define remote-exp + (let ((device (file-system-device fs))) + (with-imported-modules (source-module-closure + '((gnu build linux-modules))) + #~(begin + (use-modules (gnu build linux-modules)) + + (define dev + #$(cond ((string? device) device) + ((uuid? device) #~(find-partition-by-uuid #$device= )) + ((file-system-label? device) + #~(find-partition-by-label + (file-system-label->string #$device))))) + + (missing-modules dev '#$(operating-system-initrd-modules + (machine-operating-system machine))= ))))) + (mlet %store-monad ((missing (machine-remote-eval machine remote-exp))) + (return (list fs missing)))) + + (mlet %store-monad ((missing (mapm %store-monad missing-modules file-sys= tems))) + (for-each (match-lambda + ((fs missing) + (unless (null? missing) + (raise (condition (&message + (message (format #f (G_ "~a missing = modules ~{ ~a~}~%") + (file-system-device= fs) missing)))))))) + missing) + (return #t))) + +(define (check-deployment-sanity machine) + "Raise a '&message' error condition if it is clear that deploying MACHIN= E's +'system' declaration would fail." + (mbegin %store-monad + (machine-check-file-system-availability machine) + (machine-check-initrd-modules machine))) + ;;; ;;; System deployment. @@ -166,7 +291,7 @@ of MACHINE's system profile, ordered from most recent t= o oldest." environment type of 'managed-host." (maybe-raise-unsupported-configuration-error machine) (mlet %store-monad ((boot-parameters (machine-boot-parameters machine))) =2D (let* ((os (machine-system machine)) + (let* ((os (machine-operating-system machine)) (eval (cut machine-remote-eval machine <>)) (menu-entries (map boot-parameters->menu-entry boot-parameters)) (bootloader-configuration (operating-system-bootloader os)) =2D-=20 2.22.0 --=-=-= Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAEBCAAdFiEEa1VJLOiXAjQ2BGSm9Qb9Fp2P2VoFAl1AhWMACgkQ9Qb9Fp2P 2Vpuww/+IY3YfjoecPDX1TAk3/JSQDRKX3JdOq0RWTwoBz/uAQZ9wgw+sJrU6G0y DYnqw5EqI76KCGRzDMojyhDEoB87ufYA7B67NB3yLdMq7GVnJQVk0Urw6LpW08ow wG/Bw06CMWiUqcL0AG6CL0hbKny4+AOqajyX2b40RHEJ/oNVFI/MiXqVe0zkOz2W qHvnG8ObzDOoC0TXstTy+jJnyNgxlxkrcX0i8d5pw9MJV8vAu5f/xZABziPdAt9l OtPOYzBlnvY8SYTdhVl4pCgGD2NUlTDJ2SEjjlnYVugRHB/kEdIzicfTdpd0bCZc n6Tz0zxoGjbOLPaCVuTQS1gNHFcms1XJeZ1dyjCRHoslaFIgKGsCqPnbgxagLgyr x+Y4aBDNhXxGrUtiNvWy7qKp2z229z3HePY4VQMTSo0vZB6bMHR2AlEpjTAuM8Fy GsvbrZuLARDt26hxahIba8mmM4S/KLrnPEtGRdJqBbuHHxeRKGBYQANNQBNa5VxU 9/WHJh+kktTYivYVShdAHevW8g/FLCZcsTeYpx/5NGAW1D7w3D00mcxApBFT2LDS cadN7dI1kb1jq4VXAys1ubNMQ6hGLsXtMuj6QiJyL9/8MxQORc+8zyalf4zZc2Wa vzlPK8zN8hjax+u4Ps+emcjOAOhlpZU52UDFGYta4PjPyiWi8ig= =/dfV -----END PGP SIGNATURE----- --=-=-=--