From b1e48f222b4805012b4fd2ef5b4aa46884ee0a8d Mon Sep 17 00:00:00 2001 From: Ludovic Courtès Date: Fri, 29 Aug 2014 14:37:58 +0200 Subject: offload: Ignore unreachable machines. Fixes . Reported by Andreas Enge . * guix/scripts/offload.scm (remote-pipe): Augment docstring. (machine-load): Return +inf.0 instead of 1 if MACHINE does not respond or responds badly. --- guix/scripts/offload.scm | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'guix') diff --git a/guix/scripts/offload.scm b/guix/scripts/offload.scm index 18af511ed8..c17de34acc 100644 --- a/guix/scripts/offload.scm +++ b/guix/scripts/offload.scm @@ -181,7 +181,8 @@ (define* (remote-pipe machine mode command #:key (error-port (current-error-port)) (quote? #t)) "Run COMMAND (a string list) on MACHINE, assuming an lsh gateway has been set up. When QUOTE? is true, perform shell-quotation of all the elements of -COMMAND." +COMMAND. Return either a pipe opened with MODE, or #f if the lsh client could +not be started." (define (shell-quote str) ;; Sort-of shell-quote STR so it can be passed as an argument to the ;; shell. @@ -535,7 +536,7 @@ (define (machine-load machine) (line (read-line pipe))) (close-pipe pipe) (if (eof-object? line) - 1. + +inf.0 ;MACHINE does not respond, so assume it is infinitely loaded (match (string-tokenize line) ((one five fifteen . _) (let* ((raw (string->number five)) @@ -546,7 +547,7 @@ (define (machine-load machine) (build-machine-name machine) raw normalized) normalized)) (_ - 1.))))) + +inf.0))))) ;something's fishy about MACHINE, so avoid it (define (machine-less-loaded? m1 m2) "Return #t if the load on M1 is lower than that on M2." -- cgit v1.2.3 From b9a31d90e907db0a593ec80aacc35a0523a009f6 Mon Sep 17 00:00:00 2001 From: Ludovic Courtès Date: Fri, 29 Aug 2014 14:53:15 +0200 Subject: offload: Ignore EEXIST when registering a .drv as a GC root. Fixes . Reported by Mark H Weaver . * guix/scripts/offload.scm (register-gc-root)[script]: Wrap 'symlink' call in "catch 'system-error", and ignore EEXIST errors. --- guix/scripts/offload.scm | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) (limited to 'guix') diff --git a/guix/scripts/offload.scm b/guix/scripts/offload.scm index c17de34acc..b3b502425c 100644 --- a/guix/scripts/offload.scm +++ b/guix/scripts/offload.scm @@ -316,8 +316,17 @@ (define script (let ((root-directory (string-append %state-directory "/gcroots/tmp"))) (false-if-exception (mkdir root-directory)) - (symlink ,file - (string-append root-directory "/" ,%gc-root-file))))) + (catch 'system-error + (lambda () + (symlink ,file + (string-append root-directory "/" ,%gc-root-file))) + (lambda args + ;; If FILE already exists, we can assume that either it's a stale + ;; reference (which is fine), or another process is already + ;; building the derivation represented by FILE (which is fine + ;; too.) Thus, do nothing in that case. + (unless (= EEXIST (system-error-errno args)) + (apply throw args))))))) (let ((pipe (remote-pipe machine OPEN_READ `("guile" "-c" ,(object->string script))))) -- cgit v1.2.3 From 79601521fceb6b2f76d87cf3df45a76e43b1ffcf Mon Sep 17 00:00:00 2001 From: Ludovic Courtès Date: Sat, 30 Aug 2014 21:52:32 +0200 Subject: profiles: Compute transaction effects in a functional way. * guix/profiles.scm (manifest-transaction-effects): New procedure. (manifest-show-transaction): Use it instead of locally computing it. * tests/profiles.scm (glibc): New variable. ("manifest-transaction-effects"): New test. --- guix/profiles.scm | 49 +++++++++++++++++++++++++++++++++---------------- tests/profiles.scm | 19 +++++++++++++++++++ 2 files changed, 52 insertions(+), 16 deletions(-) (limited to 'guix') diff --git a/guix/profiles.scm b/guix/profiles.scm index 55c3b6e768..843040156c 100644 --- a/guix/profiles.scm +++ b/guix/profiles.scm @@ -32,6 +32,7 @@ (define-module (guix profiles) #:use-module (ice-9 format) #:use-module (srfi srfi-1) #:use-module (srfi srfi-9) + #:use-module (srfi srfi-11) #:use-module (srfi srfi-19) #:use-module (srfi srfi-26) #:export (manifest make-manifest @@ -60,6 +61,7 @@ (define-module (guix profiles) manifest-transaction-install manifest-transaction-remove manifest-perform-transaction + manifest-transaction-effects manifest-show-transaction profile-manifest @@ -266,6 +268,35 @@ (define-record-type* manifest-transaction (remove manifest-transaction-remove ; list of (default '()))) +(define (manifest-transaction-effects manifest transaction) + "Compute the effect of applying TRANSACTION to MANIFEST. Return 3 values: +the list of packages that would be removed, installed, or upgraded when +applying TRANSACTION to MANIFEST." + (define (manifest-entry->pattern entry) + (manifest-pattern + (name (manifest-entry-name entry)) + (output (manifest-entry-output entry)))) + + (let loop ((input (manifest-transaction-install transaction)) + (install '()) + (upgrade '())) + (match input + (() + (let ((remove (manifest-transaction-remove transaction))) + (values (manifest-matching-entries manifest remove) + (reverse install) (reverse upgrade)))) + ((entry rest ...) + ;; Check whether installing ENTRY corresponds to the installation of a + ;; new package or to an upgrade. + + ;; XXX: When the exact same output directory is installed, we're not + ;; really upgrading anything. Add a check for that case. + (let* ((pattern (manifest-entry->pattern entry)) + (upgrade? (manifest-installed? manifest pattern))) + (loop rest + (if upgrade? install (cons entry install)) + (if upgrade? (cons entry upgrade) upgrade))))))) + (define (manifest-perform-transaction manifest transaction) "Perform TRANSACTION on MANIFEST and return new manifest." (let ((install (manifest-transaction-install transaction)) @@ -284,22 +315,8 @@ (define (package-strings name version output item) item))) name version output item)) - (let* ((remove (manifest-matching-entries - manifest (manifest-transaction-remove transaction))) - (install/upgrade (manifest-transaction-install transaction)) - (install '()) - (upgrade (append-map - (lambda (entry) - (let ((matching - (manifest-matching-entries - manifest - (list (manifest-pattern - (name (manifest-entry-name entry)) - (output (manifest-entry-output entry))))))) - (when (null? matching) - (set! install (cons entry install))) - matching)) - install/upgrade))) + (let-values (((remove install upgrade) + (manifest-transaction-effects manifest transaction))) (match remove ((($ name version output item _) ..1) (let ((len (length name)) diff --git a/tests/profiles.scm b/tests/profiles.scm index 047c5ba49b..d88def32fd 100644 --- a/tests/profiles.scm +++ b/tests/profiles.scm @@ -26,6 +26,7 @@ (define-module (test-profiles) #:use-module (guix derivations) #:use-module (gnu packages bootstrap) #:use-module (ice-9 match) + #:use-module (srfi srfi-11) #:use-module (srfi srfi-64)) ;; Test the (guix profiles) module. @@ -53,6 +54,13 @@ (define guile-2.0.9:debug (manifest-entry (inherit guile-2.0.9) (output "debug"))) +(define glibc + (manifest-entry + (name "glibc") + (version "2.19") + (item "/gnu/store/...") + (output "out"))) + (test-begin "profiles") @@ -136,6 +144,17 @@ (define guile-2.0.9:debug (equal? m1 m2) (null? (manifest-entries m3))))) +(test-assert "manifest-transaction-effects" + (let* ((m0 (manifest (list guile-1.8.8))) + (t (manifest-transaction + (install (list guile-2.0.9 glibc)) + (remove (list (manifest-pattern (name "coreutils"))))))) + (let-values (((remove install upgrade) + (manifest-transaction-effects m0 t))) + (and (null? remove) + (equal? (list glibc) install) + (equal? (list guile-2.0.9) upgrade))))) + (test-assert "profile-derivation" (run-with-store %store (mlet* %store-monad -- cgit v1.2.3 From 4720f524fcab388a5a8bd62a0adf5587e5026b96 Mon Sep 17 00:00:00 2001 From: Ludovic Courtès Date: Sun, 31 Aug 2014 22:17:56 +0200 Subject: guix package: Fix search path lookup when an obsolete version is installed. Before that, 'guix package --search-paths' would not work if, say, 'foo-0.2' is installed but the distro provides 'foo-0.3'. * guix/scripts/package.scm (search-path-environment-variables)[manifest-entry->package]: Handle the case where 'find-best-packages-by-name' returns '(). --- guix/scripts/package.scm | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'guix') diff --git a/guix/scripts/package.scm b/guix/scripts/package.scm index 6ecf37e1a6..95c0130c95 100644 --- a/guix/scripts/package.scm +++ b/guix/scripts/package.scm @@ -305,10 +305,12 @@ (define manifest-entry->package ;; Use 'find-best-packages-by-name' and not 'find-packages-by-name'; ;; the former traverses the module tree only once and then allows for ;; efficient access via a vhash. - (match (or (find-best-packages-by-name name version) - (find-best-packages-by-name name #f)) + (match (find-best-packages-by-name name version) ((p _ ...) p) - (_ #f))))) + (_ + (match (find-best-packages-by-name name #f) + ((p _ ...) p) + (_ #f))))))) (define search-path-definition (match-lambda -- cgit v1.2.3 From ef8993e2dc90fd5d63d016fc45912ad451bf787c Mon Sep 17 00:00:00 2001 From: Ludovic Courtès Date: Tue, 2 Sep 2014 21:12:59 +0200 Subject: profiles: Report the old and new version number in upgrades. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * guix/profiles.scm (manifest-lookup): New procedure. (manifest-installed?): Use it. (manifest-transaction-effects): Return a pair of entries for upgrades. (right-arrow): New procedure. (manifest-show-transaction)[upgrade-string, →]: New variables. Report upgrades using 'upgrade-string'. * tests/profiles.scm ("manifest-show-transaction"): New test. ("manifest-transaction-effects"): Match UPGRADE against a pair. --- guix/profiles.scm | 53 +++++++++++++++++++++++++++++++++++++++++++++-------- tests/profiles.scm | 20 +++++++++++++++++++- 2 files changed, 64 insertions(+), 9 deletions(-) (limited to 'guix') diff --git a/guix/profiles.scm b/guix/profiles.scm index 843040156c..52bd5bc332 100644 --- a/guix/profiles.scm +++ b/guix/profiles.scm @@ -53,6 +53,7 @@ (define-module (guix profiles) manifest-remove manifest-add + manifest-lookup manifest-installed? manifest-matching-entries @@ -237,11 +238,16 @@ (define (same-entry? entry name output) (manifest-entries manifest) entries)))) +(define (manifest-lookup manifest pattern) + "Return the first item of MANIFEST that matches PATTERN, or #f if there is +no match.." + (find (entry-predicate pattern) + (manifest-entries manifest))) + (define (manifest-installed? manifest pattern) "Return #t if MANIFEST has an entry matching PATTERN (a manifest-pattern), #f otherwise." - (->bool (find (entry-predicate pattern) - (manifest-entries manifest)))) + (->bool (manifest-lookup manifest pattern))) (define (manifest-matching-entries manifest patterns) "Return all the entries of MANIFEST that match one of the PATTERNS." @@ -271,7 +277,9 @@ (define-record-type* manifest-transaction (define (manifest-transaction-effects manifest transaction) "Compute the effect of applying TRANSACTION to MANIFEST. Return 3 values: the list of packages that would be removed, installed, or upgraded when -applying TRANSACTION to MANIFEST." +applying TRANSACTION to MANIFEST. Upgrades are represented as pairs where the +head is the entry being upgraded and the tail is the entry that will replace +it." (define (manifest-entry->pattern entry) (manifest-pattern (name (manifest-entry-name entry)) @@ -292,10 +300,12 @@ (define (manifest-entry->pattern entry) ;; XXX: When the exact same output directory is installed, we're not ;; really upgrading anything. Add a check for that case. (let* ((pattern (manifest-entry->pattern entry)) - (upgrade? (manifest-installed? manifest pattern))) + (previous (manifest-lookup manifest pattern))) (loop rest - (if upgrade? install (cons entry install)) - (if upgrade? (cons entry upgrade) upgrade))))))) + (if previous install (cons entry install)) + (if previous + (alist-cons previous entry upgrade) + upgrade))))))) (define (manifest-perform-transaction manifest transaction) "Perform TRANSACTION on MANIFEST and return new manifest." @@ -304,6 +314,20 @@ (define (manifest-perform-transaction manifest transaction) (manifest-add (manifest-remove manifest remove) install))) +(define (right-arrow port) + "Return either a string containing the 'RIGHT ARROW' character, or an ASCII +replacement if PORT is not Unicode-capable." + (with-fluids ((%default-port-encoding (port-encoding port))) + (let ((arrow "→")) + (catch 'encoding-error + (lambda () + (with-fluids ((%default-port-conversion-strategy 'error)) + (with-output-to-string + (lambda () + (display arrow))))) + (lambda (key . args) + ">"))))) + (define* (manifest-show-transaction store manifest transaction #:key dry-run?) "Display what will/would be installed/removed from MANIFEST by TRANSACTION." @@ -315,6 +339,17 @@ (define (package-strings name version output item) item))) name version output item)) + (define → ;an arrow that can be represented on stderr + (right-arrow (current-error-port))) + + (define (upgrade-string name old-version new-version output item) + (format #f " ~a\t~a ~a ~a\t~a\t~a" name + old-version → new-version + output + (if (package? item) + (package-output store item output) + item))) + (let-values (((remove install upgrade) (manifest-transaction-effects manifest transaction))) (match remove @@ -334,9 +369,11 @@ (define (package-strings name version output item) remove)))) (_ #f)) (match upgrade - ((($ name version output item _) ..1) + (((($ name old-version) + . ($ _ new-version output item)) ..1) (let ((len (length name)) - (upgrade (package-strings name version output item))) + (upgrade (map upgrade-string + name old-version new-version output item))) (if dry-run? (format (current-error-port) (N_ "The following package would be upgraded:~%~{~a~%~}~%" diff --git a/tests/profiles.scm b/tests/profiles.scm index d88def32fd..879f71073f 100644 --- a/tests/profiles.scm +++ b/tests/profiles.scm @@ -26,6 +26,7 @@ (define-module (test-profiles) #:use-module (guix derivations) #:use-module (gnu packages bootstrap) #:use-module (ice-9 match) + #:use-module (ice-9 regex) #:use-module (srfi srfi-11) #:use-module (srfi srfi-64)) @@ -153,7 +154,24 @@ (define glibc (manifest-transaction-effects m0 t))) (and (null? remove) (equal? (list glibc) install) - (equal? (list guile-2.0.9) upgrade))))) + (equal? (list (cons guile-1.8.8 guile-2.0.9)) upgrade))))) + +(test-assert "manifest-show-transaction" + (let* ((m (manifest (list guile-1.8.8))) + (t (manifest-transaction (install (list guile-2.0.9))))) + (let-values (((remove install upgrade) + (manifest-transaction-effects m t))) + (with-store store + (and (string-match "guile\t1.8.8 → 2.0.9" + (with-fluids ((%default-port-encoding "UTF-8")) + (with-error-to-string + (lambda () + (manifest-show-transaction store m t))))) + (string-match "guile\t1.8.8 > 2.0.9" + (with-fluids ((%default-port-encoding "ISO-8859-1")) + (with-error-to-string + (lambda () + (manifest-show-transaction store m t)))))))))) (test-assert "profile-derivation" (run-with-store %store -- cgit v1.2.3 From 9a914764313657b82225cc36feb05ac50fe26635 Mon Sep 17 00:00:00 2001 From: Ludovic Courtès Date: Tue, 2 Sep 2014 21:25:57 +0200 Subject: profiles: Report version numbers in a separate column. * guix/profiles.scm (manifest-show-transaction)[package-strings, upgrade-strings]: Show version number in separate column. Show OUTPUT in first column, and only when it's different from "out". --- guix/profiles.scm | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'guix') diff --git a/guix/profiles.scm b/guix/profiles.scm index 52bd5bc332..919f27d250 100644 --- a/guix/profiles.scm +++ b/guix/profiles.scm @@ -333,7 +333,9 @@ (define* (manifest-show-transaction store manifest transaction "Display what will/would be installed/removed from MANIFEST by TRANSACTION." (define (package-strings name version output item) (map (lambda (name version output item) - (format #f " ~a-~a\t~a\t~a" name version output + (format #f " ~a~:[:~a~;~*~]\t~a\t~a" + name + (equal? output "out") output version (if (package? item) (package-output store item output) item))) @@ -343,9 +345,9 @@ (define → ;an arrow that can be represented on stderr (right-arrow (current-error-port))) (define (upgrade-string name old-version new-version output item) - (format #f " ~a\t~a ~a ~a\t~a\t~a" name + (format #f " ~a~:[:~a~;~*~]\t~a ~a ~a\t~a" + name (equal? output "out") output old-version → new-version - output (if (package? item) (package-output store item output) item))) @@ -353,7 +355,7 @@ (define (upgrade-string name old-version new-version output item) (let-values (((remove install upgrade) (manifest-transaction-effects manifest transaction))) (match remove - ((($ name version output item _) ..1) + ((($ name version output item) ..1) (let ((len (length name)) (remove (package-strings name version output item))) (if dry-run? -- cgit v1.2.3 From 5e3b388b51780373e68c19a46a279a809e82d461 Mon Sep 17 00:00:00 2001 From: Cyril Roelandt Date: Mon, 1 Sep 2014 01:45:09 +0200 Subject: Move specification->package to gnu/packages.scm. * guix/scripts/build.scm (specification->package): Move from here... * gnu/packages.scm: ... to here. --- gnu/packages.scm | 26 +++++++++++++++++++++++++- guix/scripts/build.scm | 23 +---------------------- 2 files changed, 26 insertions(+), 23 deletions(-) (limited to 'guix') diff --git a/gnu/packages.scm b/gnu/packages.scm index 14ad75561c..26d87c6b16 100644 --- a/gnu/packages.scm +++ b/gnu/packages.scm @@ -28,6 +28,7 @@ (define-module (gnu packages) #:use-module (ice-9 vlist) #:use-module (ice-9 match) #:use-module (srfi srfi-1) + #:use-module (srfi srfi-11) #:use-module (srfi srfi-26) #:use-module (srfi srfi-39) #:export (search-patch @@ -45,7 +46,9 @@ (define-module (gnu packages) package-transitive-dependents package-covering-dependents - check-package-freshness)) + check-package-freshness + + specification->package)) ;;; Commentary: ;;; @@ -326,3 +329,24 @@ (define (check-package-freshness package) (case key ((getaddrinfo-error ftp-error) #f) (else (apply throw key args)))))) + +(define (specification->package spec) + "Return a package matching SPEC. SPEC may be a package name, or a package +name followed by a hyphen and a version number. If the version number is not +present, return the preferred newest version." + (let-values (((name version) + (package-name->name+version spec))) + (match (find-best-packages-by-name name version) + ((p) ; one match + p) + ((p x ...) ; several matches + (warning (_ "ambiguous package specification `~a'~%") spec) + (warning (_ "choosing ~a from ~a~%") + (package-full-name p) + (location->string (package-location p))) + p) + (_ ; no matches + (if version + (leave (_ "~A: package not found for version ~a~%") + name version) + (leave (_ "~A: unknown package~%") name)))))) diff --git a/guix/scripts/build.scm b/guix/scripts/build.scm index 5e4647de79..09401e923c 100644 --- a/guix/scripts/build.scm +++ b/guix/scripts/build.scm @@ -33,7 +33,7 @@ (define-module (guix scripts build) #:use-module (srfi srfi-26) #:use-module (srfi srfi-34) #:use-module (srfi srfi-37) - #:autoload (gnu packages) (find-best-packages-by-name) + #:autoload (gnu packages) (specification->package) #:autoload (guix download) (download-to-store) #:export (%standard-build-options set-build-options-from-command-line @@ -41,27 +41,6 @@ (define-module (guix scripts build) guix-build)) -(define (specification->package spec) - "Return a package matching SPEC. SPEC may be a package name, or a package -name followed by a hyphen and a version number. If the version number is not -present, return the preferred newest version." - (let-values (((name version) - (package-name->name+version spec))) - (match (find-best-packages-by-name name version) - ((p) ; one match - p) - ((p x ...) ; several matches - (warning (_ "ambiguous package specification `~a'~%") spec) - (warning (_ "choosing ~a from ~a~%") - (package-full-name p) - (location->string (package-location p))) - p) - (_ ; no matches - (if version - (leave (_ "~A: package not found for version ~a~%") - name version) - (leave (_ "~A: unknown package~%") name)))))) - (define (register-root store paths root) "Register ROOT as an indirect GC root for all of PATHS." (let* ((root (string-append (canonicalize-path (dirname root)) -- cgit v1.2.3 From b4f5e0e87c112bd4b8425be0c17524ce9c2a85ca Mon Sep 17 00:00:00 2001 From: Cyril Roelandt Date: Mon, 1 Sep 2014 02:13:21 +0200 Subject: scripts: add guix lint * guix/scripts/lint.scm: New file. Defines a 'lint' tool for Guix packages. * tests/lint.scm: New file. * Makefile.am (MODULES, SCM_TESTS): Add them. * po/guix/Makevars: Update appropriately. * po/guix/POTFILES.in: Update appropriately. * doc/guix.texi: Document "guix lint". --- Makefile.am | 4 +- doc/guix.texi | 29 ++++++- guix/scripts/lint.scm | 213 ++++++++++++++++++++++++++++++++++++++++++++++++++ po/guix/Makevars | 3 +- po/guix/POTFILES.in | 1 + tests/lint.scm | 110 ++++++++++++++++++++++++++ 6 files changed, 357 insertions(+), 3 deletions(-) create mode 100644 guix/scripts/lint.scm create mode 100644 tests/lint.scm (limited to 'guix') diff --git a/Makefile.am b/Makefile.am index fff5958355..371b85c235 100644 --- a/Makefile.am +++ b/Makefile.am @@ -89,6 +89,7 @@ MODULES = \ guix/scripts/authenticate.scm \ guix/scripts/refresh.scm \ guix/scripts/system.scm \ + guix/scripts/lint.scm \ guix.scm \ $(GNU_SYSTEM_MODULES) @@ -159,7 +160,8 @@ SCM_TESTS = \ tests/nar.scm \ tests/union.scm \ tests/profiles.scm \ - tests/syscalls.scm + tests/syscalls.scm \ + tests/lint.scm SH_TESTS = \ tests/guix-build.sh \ diff --git a/doc/guix.texi b/doc/guix.texi index 46f2c70b85..384e2a9ced 100644 --- a/doc/guix.texi +++ b/doc/guix.texi @@ -1459,7 +1459,10 @@ definitions like the one above may be automatically converted from the Nixpkgs distribution using the @command{guix import} command.}, the package may actually be built using the @code{guix build} command-line tool (@pxref{Invoking guix build}). @xref{Packaging Guidelines}, for -more information on how to test package definitions. +more information on how to test package definitions, and +@ref{Invoking guix lint}, for information on how to check a definition +for style conformance. + Eventually, updating the package definition to a new upstream version can be partly automated by the @command{guix refresh} command @@ -2328,6 +2331,7 @@ programming interface of Guix in a convenient way. * Invoking guix download:: Downloading a file and printing its hash. * Invoking guix hash:: Computing the cryptographic hash of a file. * Invoking guix refresh:: Updating package definitions. +* Invoking guix lint:: Finding errors in package definitions. @end menu @node Invoking guix build @@ -2705,6 +2709,29 @@ for in @code{$PATH}. @end table +@node Invoking guix lint +@section Invoking @command{guix lint} +The @command{guix lint} is meant to help package developers avoid common +errors and use a consistent style. It runs a few checks on a given set of +packages in order to find common mistakes in their definitions. + +The general syntax is: + +@example +guix lint @var{options} @var{package}@dots{} +@end example + +If no package is given on the command line, then all packages are checked. +The @var{options} may be zero or more of the following: + +@table @code + +@item --list-checkers +@itemx -l +List and describe all the available checkers that will be run on packages +and exit. + +@end table @c ********************************************************************* @node GNU Distribution diff --git a/guix/scripts/lint.scm b/guix/scripts/lint.scm new file mode 100644 index 0000000000..e3b06977ee --- /dev/null +++ b/guix/scripts/lint.scm @@ -0,0 +1,213 @@ +;;; GNU Guix --- Functional package management for GNU +;;; Copyright © 2014 Cyril Roelandt +;;; +;;; This file is part of GNU Guix. +;;; +;;; GNU Guix is free software; you can redistribute it and/or modify it +;;; under the terms of the GNU General Public License as published by +;;; the Free Software Foundation; either version 3 of the License, or (at +;;; your option) any later version. +;;; +;;; GNU Guix is distributed in the hope that it will be useful, but +;;; WITHOUT ANY WARRANTY; without even the implied warranty of +;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;;; GNU General Public License for more details. +;;; +;;; You should have received a copy of the GNU General Public License +;;; along with GNU Guix. If not, see . + +(define-module (guix scripts lint) + #:use-module (guix base32) + #:use-module (guix packages) + #:use-module (guix records) + #:use-module (guix ui) + #:use-module (guix utils) + #:use-module (gnu packages) + #:use-module (ice-9 match) + #:use-module (srfi srfi-1) + #:use-module (srfi srfi-9) + #:use-module (srfi srfi-11) + #:use-module (srfi srfi-37) + #:export (guix-lint + check-inputs-should-be-native + check-patches + check-synopsis-style)) + + +;;; +;;; Command-line options. +;;; + +(define %default-options + ;; Alist of default option values. + '()) + +(define (show-help) + (display (_ "Usage: guix lint [OPTION]... [PACKAGE]... +Run a set of checkers on the specified package; if none is specified, run the checkers on all packages.\n")) + (display (_ " + -h, --help display this help and exit")) + (display (_ " + -l, --list-checkers display the list of available lint checkers")) + (display (_ " + -V, --version display version information and exit")) + (newline) + (show-bug-report-information)) + +(define %options + ;; Specification of the command-line options. + ;; TODO: add some options: + ;; * --checkers=checker1,checker2...: only run the specified checkers + ;; * --certainty=[low,medium,high]: only run checkers that have at least this + ;; 'certainty'. + (list (option '(#\h "help") #f #f + (lambda args + (show-help) + (exit 0))) + (option '(#\l "list-checkers") #f #f + (lambda args + (list-checkers-and-exit))) + (option '(#\V "version") #f #f + (lambda args + (show-version-and-exit "guix lint"))))) + + +;;; +;;; Helpers +;;; +(define* (emit-warning package message #:optional field) + ;; Emit a warning about PACKAGE, printing the location of FIELD if it is + ;; given, the location of PACKAGE otherwise, the full name of PACKAGE and the + ;; provided MESSAGE. + (let ((loc (or (package-field-location package field) + (package-location package)))) + (warning (_ "~a: ~a: ~a~%") + (location->string loc) + (package-full-name package) + message))) + + +;;; +;;; Checkers +;;; +(define-record-type* + lint-checker make-lint-checker + lint-checker? + ;; TODO: add a 'certainty' field that shows how confident we are in the + ;; checker. Then allow users to only run checkers that have a certain + ;; 'certainty' level. + (name lint-checker-name) + (description lint-checker-description) + (check lint-checker-check)) + +(define (list-checkers-and-exit) + ;; Print information about all available checkers and exit. + (format #t (_ "Available checkers:~%")) + (for-each (lambda (checker) + (format #t "- ~a: ~a~%" + (lint-checker-name checker) + (lint-checker-description checker))) + %checkers) + (exit 0)) + +(define (check-inputs-should-be-native package) + ;; Emit a warning if some inputs of PACKAGE are likely to belong to its + ;; native inputs. + (let ((inputs (package-inputs package))) + (match inputs + (((labels packages . _) ...) + (when (member "pkg-config" + (map package-name (filter package? packages))) + (emit-warning package + "pkg-config should probably be a native input" + 'inputs)))))) + + +(define (check-synopsis-style package) + ;; Emit a warning if stylistic issues are found in the synopsis of PACKAGE. + (define (check-final-period synopsis) + ;; Synopsis should not end with a period, except for some special cases. + (if (and (string=? (string-take-right synopsis 1) ".") + (not (string=? (string-take-right synopsis 4) "etc."))) + (emit-warning package + "no period allowed at the end of the synopsis" + 'synopsis))) + + (define (check-start-article synopsis) + (if (or (string=? (string-take synopsis 2) "A ") + (string=? (string-take synopsis 3) "An ")) + (emit-warning package + "no article allowed at the beginning of the synopsis" + 'synopsis))) + + (let ((synopsis (package-synopsis package))) + (if (string? synopsis) + (begin + (check-final-period synopsis) + (check-start-article synopsis))))) + +(define (check-patches package) + ;; Emit a warning if the patches requires by PACKAGE are badly named. + (let ((patches (and=> (package-source package) origin-patches)) + (name (package-name package)) + (full-name (package-full-name package))) + (if (and patches + (any (lambda (patch) + (let ((filename (basename patch))) + (not (or (eq? (string-contains filename name) 0) + (eq? (string-contains filename full-name) 0))))) + patches)) + (emit-warning package + "file names of patches should start with the package name" + 'patches)))) + +(define %checkers + (list + (lint-checker + (name "inputs-should-be-native") + (description "Identify inputs that should be native inputs") + (check check-inputs-should-be-native)) + (lint-checker + (name "patch-filenames") + (description "Validate filenames of patches") + (check check-patches)) + (lint-checker + (name "synopsis") + (description "Validate package synopsis") + (check check-synopsis-style)))) + +(define (run-checkers package) + ;; Run all the checkers on PACKAGE. + (for-each (lambda (checker) + ((lint-checker-check checker) package)) + %checkers)) + + +;;; +;;; Entry Point +;;; + +(define (guix-lint . args) + (define (parse-options) + ;; Return the alist of option values. + (args-fold* args %options + (lambda (opt name arg result) + (leave (_ "~A: unrecognized option~%") name)) + (lambda (arg result) + (alist-cons 'argument arg result)) + %default-options)) + + (let* ((opts (parse-options)) + (args (filter-map (match-lambda + (('argument . value) + value) + (_ #f)) + (reverse opts)))) + + + (if (null? args) + (fold-packages (lambda (p r) (run-checkers p)) '()) + (for-each + (lambda (spec) + (run-checkers spec)) + (map specification->package args))))) diff --git a/po/guix/Makevars b/po/guix/Makevars index 87bb438418..f5b498caa9 100644 --- a/po/guix/Makevars +++ b/po/guix/Makevars @@ -10,7 +10,8 @@ top_builddir = ../.. XGETTEXT_OPTIONS = \ --language=Scheme --from-code=UTF-8 \ --keyword=_ --keyword=N_ \ - --keyword=message + --keyword=message \ + --keyword=description COPYRIGHT_HOLDER = Ludovic Courtès diff --git a/po/guix/POTFILES.in b/po/guix/POTFILES.in index bf2d31306a..5cc68ff404 100644 --- a/po/guix/POTFILES.in +++ b/po/guix/POTFILES.in @@ -10,6 +10,7 @@ guix/scripts/pull.scm guix/scripts/substitute-binary.scm guix/scripts/authenticate.scm guix/scripts/system.scm +guix/scripts/lint.scm guix/gnu-maintenance.scm guix/ui.scm guix/http-client.scm diff --git a/tests/lint.scm b/tests/lint.scm new file mode 100644 index 0000000000..f6dae47ca6 --- /dev/null +++ b/tests/lint.scm @@ -0,0 +1,110 @@ +;;; GNU Guix --- Functional package management for GNU +;;; Copyright © 2012, 2013 Cyril Roelandt +;;; +;;; This file is part of GNU Guix. +;;; +;;; GNU Guix is free software; you can redistribute it and/or modify it +;;; under the terms of the GNU General Public License as published by +;;; the Free Software Foundation; either version 3 of the License, or (at +;;; your option) any later version. +;;; +;;; GNU Guix is distributed in the hope that it will be useful, but +;;; WITHOUT ANY WARRANTY; without even the implied warranty of +;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;;; GNU General Public License for more details. +;;; +;;; You should have received a copy of the GNU General Public License +;;; along with GNU Guix. If not, see . + + +(define-module (test-packages) + #:use-module (guix build download) + #:use-module (guix build-system gnu) + #:use-module (guix packages) + #:use-module (guix scripts lint) + #:use-module (guix ui) + #:use-module (gnu packages) + #:use-module (gnu packages pkg-config) + #:use-module (srfi srfi-64)) + +;; Test the linter. + + +(test-begin "lint") + +(define-syntax-rule (dummy-package name* extra-fields ...) + (package extra-fields ... (name name*) (version "0") (source #f) + (build-system gnu-build-system) + (synopsis #f) (description #f) + (home-page #f) (license #f) )) + +(define (call-with-warnings thunk) + (let ((port (open-output-string))) + (parameterize ((guix-warning-port port)) + (thunk)) + (get-output-string port))) + +(test-assert "synopsis: ends with a period" + (->bool + (string-contains (call-with-warnings + (lambda () + (let ((pkg (dummy-package "x" + (synopsis "Bad synopsis.")))) + (check-synopsis-style pkg)))) + "no period allowed at the end of the synopsis"))) + +(test-assert "synopsis: ends with 'etc.'" + (->bool + (string-null? (call-with-warnings + (lambda () + (let ((pkg (dummy-package "x" + (synopsis "Foo, bar, etc.")))) + (check-synopsis-style pkg))))))) + +(test-assert "synopsis: starts with 'A'" + (->bool + (string-contains (call-with-warnings + (lambda () + (let ((pkg (dummy-package "x" + (synopsis "A bad synopŝis")))) + (check-synopsis-style pkg)))) + "no article allowed at the beginning of the synopsis"))) + +(test-assert "synopsis: starts with 'An'" + (->bool + (string-contains (call-with-warnings + (lambda () + (let ((pkg (dummy-package "x" + (synopsis "An awful synopsis")))) + (check-synopsis-style pkg)))) + "no article allowed at the beginning of the synopsis"))) + +(test-assert "inputs: pkg-config is probably a native input" + (->bool + (string-contains + (call-with-warnings + (lambda () + (let ((pkg (dummy-package "x" + (inputs `(("pkg-config" ,pkg-config)))))) + (check-inputs-should-be-native pkg)))) + "pkg-config should probably be a native input"))) + +(test-assert "patches: file names" + (->bool + (string-contains + (call-with-warnings + (lambda () + (let ((pkg (dummy-package "x" + (source + (origin + (method url-fetch) + (uri "someurl") + (sha256 "somesha") + (patches (list "/path/to/y.patch"))))))) + (check-patches pkg)))) + "file names of patches should start with the package name"))) + +(test-end "lint") + + +(exit (= (test-runner-fail-count (test-runner-current)) 0)) -- cgit v1.2.3 From b002e9d08eb909e9fa1a84b0feed1662dce0bd3f Mon Sep 17 00:00:00 2001 From: Ludovic Courtès Date: Wed, 3 Sep 2014 09:01:28 +0200 Subject: guix lint: Remove "guix lint: " prefix from warnings. This allows editors to parse warnings correctly. * guix/scripts/lint.scm (emit-warning): Use 'format' instead of 'warning', to avoid the "guix lint: " prefix in messages. * tests/lint.scm (call-with-warnings): Indent. --- guix/scripts/lint.scm | 8 ++++---- tests/lint.scm | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) (limited to 'guix') diff --git a/guix/scripts/lint.scm b/guix/scripts/lint.scm index e3b06977ee..83dde9a1a1 100644 --- a/guix/scripts/lint.scm +++ b/guix/scripts/lint.scm @@ -81,10 +81,10 @@ (define* (emit-warning package message #:optional field) ;; provided MESSAGE. (let ((loc (or (package-field-location package field) (package-location package)))) - (warning (_ "~a: ~a: ~a~%") - (location->string loc) - (package-full-name package) - message))) + (format (guix-warning-port) (_ "~a: ~a: ~a~%") + (location->string loc) + (package-full-name package) + message))) ;;; diff --git a/tests/lint.scm b/tests/lint.scm index f6dae47ca6..56558c904f 100644 --- a/tests/lint.scm +++ b/tests/lint.scm @@ -39,10 +39,10 @@ (define-syntax-rule (dummy-package name* extra-fields ...) (home-page #f) (license #f) )) (define (call-with-warnings thunk) - (let ((port (open-output-string))) - (parameterize ((guix-warning-port port)) - (thunk)) - (get-output-string port))) + (let ((port (open-output-string))) + (parameterize ((guix-warning-port port)) + (thunk)) + (get-output-string port))) (test-assert "synopsis: ends with a period" (->bool -- cgit v1.2.3 From 548f7a8fa2a4745d0e35e175e201513857440991 Mon Sep 17 00:00:00 2001 From: Ludovic Courtès Date: Wed, 3 Sep 2014 10:47:05 +0200 Subject: Move operating system helpers from (guix build …) to (gnu build …). MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * guix/build/activation.scm, guix/build/install.scm, guix/build/linux-initrd.scm, guix/build/vm.scm: Move to... * gnu/build: ... here. * Makefile.am (MODULES): Remove the above guix/build/ files. * gnu-system.am (GNU_SYSTEM_MODULES): Add the above gnu/build/ files here. * gnu/services/base.scm, gnu/services/dmd.scm, gnu/system.scm, gnu/system/linux-initrd.scm, gnu/system/vm.scm, guix/scripts/system.scm: Adjust to the new module names. --- Makefile.am | 4 - gnu-system.am | 16 +- gnu/build/activation.scm | 226 ++++++++++++++ gnu/build/install.scm | 135 +++++++++ gnu/build/linux-initrd.scm | 702 ++++++++++++++++++++++++++++++++++++++++++++ gnu/build/vm.scm | 259 ++++++++++++++++ gnu/services/base.scm | 2 +- gnu/services/dmd.scm | 4 +- gnu/system.scm | 8 +- gnu/system/linux-initrd.scm | 6 +- gnu/system/vm.scm | 12 +- guix/build/activation.scm | 226 -------------- guix/build/install.scm | 136 --------- guix/build/linux-initrd.scm | 702 -------------------------------------------- guix/build/vm.scm | 259 ---------------- guix/scripts/system.scm | 2 +- 16 files changed, 1350 insertions(+), 1349 deletions(-) create mode 100644 gnu/build/activation.scm create mode 100644 gnu/build/install.scm create mode 100644 gnu/build/linux-initrd.scm create mode 100644 gnu/build/vm.scm delete mode 100644 guix/build/activation.scm delete mode 100644 guix/build/install.scm delete mode 100644 guix/build/linux-initrd.scm delete mode 100644 guix/build/vm.scm (limited to 'guix') diff --git a/Makefile.am b/Makefile.am index 371b85c235..d25eeeb76e 100644 --- a/Makefile.am +++ b/Makefile.am @@ -62,7 +62,6 @@ MODULES = \ guix/build/git.scm \ guix/build/gnu-build-system.scm \ guix/build/gnu-dist.scm \ - guix/build/linux-initrd.scm \ guix/build/perl-build-system.scm \ guix/build/python-build-system.scm \ guix/build/utils.scm \ @@ -70,9 +69,6 @@ MODULES = \ guix/build/pull.scm \ guix/build/rpath.scm \ guix/build/svn.scm \ - guix/build/vm.scm \ - guix/build/install.scm \ - guix/build/activation.scm \ guix/build/syscalls.scm \ guix/build/emacs-utils.scm \ guix/packages.scm \ diff --git a/gnu-system.am b/gnu-system.am index abfb27fade..2c303ac109 100644 --- a/gnu-system.am +++ b/gnu-system.am @@ -28,7 +28,7 @@ GNU_SYSTEM_MODULES = \ gnu/packages/acl.scm \ gnu/packages/admin.scm \ gnu/packages/algebra.scm \ - gnu/packages/aidc.scm \ + gnu/packages/aidc.scm \ gnu/packages/apl.scm \ gnu/packages/apr.scm \ gnu/packages/asciidoc.scm \ @@ -58,7 +58,7 @@ GNU_SYSTEM_MODULES = \ gnu/packages/compression.scm \ gnu/packages/complexity.scm \ gnu/packages/conkeror.scm \ - gnu/packages/cook.scm \ + gnu/packages/cook.scm \ gnu/packages/cpio.scm \ gnu/packages/cppi.scm \ gnu/packages/cross-base.scm \ @@ -151,7 +151,7 @@ GNU_SYSTEM_MODULES = \ gnu/packages/lightning.scm \ gnu/packages/links.scm \ gnu/packages/linux.scm \ - gnu/packages/lisp.scm \ + gnu/packages/lisp.scm \ gnu/packages/lout.scm \ gnu/packages/lsh.scm \ gnu/packages/lsof.scm \ @@ -180,7 +180,7 @@ GNU_SYSTEM_MODULES = \ gnu/packages/nettle.scm \ gnu/packages/node.scm \ gnu/packages/noweb.scm \ - gnu/packages/nvi.scm \ + gnu/packages/nvi.scm \ gnu/packages/ocaml.scm \ gnu/packages/ocrad.scm \ gnu/packages/onc-rpc.scm \ @@ -277,7 +277,13 @@ GNU_SYSTEM_MODULES = \ gnu/system/linux.scm \ gnu/system/linux-initrd.scm \ gnu/system/shadow.scm \ - gnu/system/vm.scm + gnu/system/vm.scm \ + \ + gnu/build/activation.scm \ + gnu/build/install.scm \ + gnu/build/linux-initrd.scm \ + gnu/build/vm.scm + patchdir = $(guilemoduledir)/gnu/packages/patches dist_patch_DATA = \ diff --git a/gnu/build/activation.scm b/gnu/build/activation.scm new file mode 100644 index 0000000000..bb1165ac47 --- /dev/null +++ b/gnu/build/activation.scm @@ -0,0 +1,226 @@ +;;; GNU Guix --- Functional package management for GNU +;;; Copyright © 2013, 2014 Ludovic Courtès +;;; +;;; This file is part of GNU Guix. +;;; +;;; GNU Guix is free software; you can redistribute it and/or modify it +;;; under the terms of the GNU General Public License as published by +;;; the Free Software Foundation; either version 3 of the License, or (at +;;; your option) any later version. +;;; +;;; GNU Guix is distributed in the hope that it will be useful, but +;;; WITHOUT ANY WARRANTY; without even the implied warranty of +;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;;; GNU General Public License for more details. +;;; +;;; You should have received a copy of the GNU General Public License +;;; along with GNU Guix. If not, see . + +(define-module (gnu build activation) + #:use-module (gnu build linux-initrd) + #:use-module (guix build utils) + #:use-module (ice-9 ftw) + #:use-module (ice-9 match) + #:use-module (srfi srfi-1) + #:use-module (srfi srfi-26) + #:export (activate-users+groups + activate-etc + activate-setuid-programs + activate-current-system)) + +;;; Commentary: +;;; +;;; This module provides "activation" helpers. Activation is the process that +;;; consists in setting up system-wide files and directories so that an +;;; 'operating-system' configuration becomes active. +;;; +;;; Code: + +(define* (add-group name #:key gid password system? + (log-port (current-error-port))) + "Add NAME as a user group, with the given numeric GID if specified." + ;; Use 'groupadd' from the Shadow package. + (format log-port "adding group '~a'...~%" name) + (let ((args `(,@(if gid `("-g" ,(number->string gid)) '()) + ,@(if password `("-p" ,password) '()) + ,@(if system? `("--system") '()) + ,name))) + (zero? (apply system* "groupadd" args)))) + +(define* (add-user name group + #:key uid comment home shell password system? + (supplementary-groups '()) + (log-port (current-error-port))) + "Create an account for user NAME part of GROUP, with the specified +properties. Return #t on success." + (format log-port "adding user '~a'...~%" name) + + (if (and uid (zero? uid)) + + ;; 'useradd' fails with "Cannot determine your user name" if the root + ;; account doesn't exist. Thus, for bootstrapping purposes, create that + ;; one manually. + (begin + (call-with-output-file "/etc/shadow" + (cut format <> "~a::::::::~%" name)) + (call-with-output-file "/etc/passwd" + (cut format <> "~a:x:~a:~a:~a:~a:~a~%" + name "0" "0" comment home shell)) + (chmod "/etc/shadow" #o600) + #t) + + ;; Use 'useradd' from the Shadow package. + (let ((args `(,@(if uid `("-u" ,(number->string uid)) '()) + "-g" ,(if (number? group) (number->string group) group) + ,@(if (pair? supplementary-groups) + `("-G" ,(string-join supplementary-groups ",")) + '()) + ,@(if comment `("-c" ,comment) '()) + ,@(if home + (if (file-exists? home) + `("-d" ,home) ; avoid warning from 'useradd' + `("-d" ,home "--create-home")) + '()) + ,@(if shell `("-s" ,shell) '()) + ,@(if password `("-p" ,password) '()) + ,@(if system? '("--system") '()) + ,name))) + (zero? (apply system* "useradd" args))))) + +(define (activate-users+groups users groups) + "Make sure the accounts listed in USERS and the user groups listed in GROUPS +are all available. + +Each item in USERS is a list of all the characteristics of a user account; +each item in GROUPS is a tuple with the group name, group password or #f, and +numeric gid or #f." + (define (touch file) + (close-port (open-file file "a0b"))) + + (define activate-user + (match-lambda + ((name uid group supplementary-groups comment home shell password system?) + (unless (false-if-exception (getpwnam name)) + (let ((profile-dir (string-append "/var/guix/profiles/per-user/" + name))) + (add-user name group + #:uid uid + #:system? system? + #:supplementary-groups supplementary-groups + #:comment comment + #:home home + #:shell shell + #:password password) + + (unless system? + ;; Create the profile directory for the new account. + (let ((pw (getpwnam name))) + (mkdir-p profile-dir) + (chown profile-dir (passwd:uid pw) (passwd:gid pw))))))))) + + ;; 'groupadd' aborts if the file doesn't already exist. + (touch "/etc/group") + + ;; Create the root account so we can use 'useradd' and 'groupadd'. + (activate-user (find (match-lambda + ((name (? zero?) _ ...) #t) + (_ #f)) + users)) + + ;; Then create the groups. + (for-each (match-lambda + ((name password gid system?) + (unless (false-if-exception (getgrnam name)) + (add-group name + #:gid gid #:password password + #:system? system?)))) + groups) + + ;; Finally create the other user accounts. + (for-each activate-user users)) + +(define (activate-etc etc) + "Install ETC, a directory in the store, as the source of static files for +/etc." + + ;; /etc is a mixture of static and dynamic settings. Here is where we + ;; initialize it from the static part. + + (format #t "populating /etc from ~a...~%" etc) + (let ((rm-f (lambda (f) + (false-if-exception (delete-file f))))) + (rm-f "/etc/static") + (symlink etc "/etc/static") + (for-each (lambda (file) + ;; TODO: Handle 'shadow' specially so that changed + ;; password aren't lost. + (let ((target (string-append "/etc/" file)) + (source (string-append "/etc/static/" file))) + (rm-f target) + (symlink source target))) + (scandir etc + (lambda (file) + (not (member file '("." "..")))) + + ;; The default is 'string-locale)) + (scandir %setuid-directory + (lambda (file) + (not (member file '("." "..")))) + string +;;; +;;; This file is part of GNU Guix. +;;; +;;; GNU Guix is free software; you can redistribute it and/or modify it +;;; under the terms of the GNU General Public License as published by +;;; the Free Software Foundation; either version 3 of the License, or (at +;;; your option) any later version. +;;; +;;; GNU Guix is distributed in the hope that it will be useful, but +;;; WITHOUT ANY WARRANTY; without even the implied warranty of +;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;;; GNU General Public License for more details. +;;; +;;; You should have received a copy of the GNU General Public License +;;; along with GNU Guix. If not, see . + +(define-module (gnu build install) + #:use-module (guix build utils) + #:use-module (srfi srfi-26) + #:use-module (ice-9 match) + #:export (install-grub + populate-root-file-system + reset-timestamps + register-closure)) + +;;; Commentary: +;;; +;;; This module supports the installation of the GNU system on a hard disk. +;;; It is meant to be used both in a build environment (in derivations that +;;; build VM images), and on the bare metal (when really installing the +;;; system.) +;;; +;;; Code: + +(define* (install-grub grub.cfg device mount-point) + "Install GRUB with GRUB.CFG on DEVICE, which is assumed to be mounted on +MOUNT-POINT." + (let* ((target (string-append mount-point "/boot/grub/grub.cfg")) + (pivot (string-append target ".new"))) + (mkdir-p (dirname target)) + + ;; Copy GRUB.CFG instead of just symlinking it since it's not a GC root. + ;; Do that atomically. + (copy-file grub.cfg pivot) + (rename-file pivot target) + + (unless (zero? (system* "grub-install" "--no-floppy" + "--boot-directory" + (string-append mount-point "/boot") + device)) + (error "failed to install GRUB")))) + +(define (evaluate-populate-directive directive target) + "Evaluate DIRECTIVE, an sexp describing a file or directory to create under +directory TARGET." + (let loop ((directive directive)) + (match directive + (('directory name) + (mkdir-p (string-append target name))) + (('directory name uid gid) + (let ((dir (string-append target name))) + (mkdir-p dir) + (chown dir uid gid))) + (('directory name uid gid mode) + (loop `(directory ,name ,uid ,gid)) + (chmod (string-append target name) mode)) + ((new '-> old) + (symlink old (string-append target new)))))) + +(define (directives store) + "Return a list of directives to populate the root file system that will host +STORE." + `(;; Note: the store's GID is fixed precisely so we can set it here rather + ;; than at activation time. + (directory ,store 0 30000 #o1775) + + (directory "/etc") + (directory "/var/log") ; for dmd + (directory "/var/guix/gcroots") + (directory "/var/empty") ; for no-login accounts + (directory "/var/db") ; for dhclient, etc. + (directory "/var/run") + (directory "/run") + (directory "/mnt") + (directory "/var/guix/profiles/per-user/root" 0 0) + + ;; Link to the initial system generation. + ("/var/guix/profiles/system" -> "system-1-link") + + ("/var/guix/gcroots/booted-system" -> "/run/booted-system") + ("/var/guix/gcroots/current-system" -> "/run/current-system") + + (directory "/bin") + ("/bin/sh" -> "/run/current-system/profile/bin/bash") + (directory "/tmp" 0 0 #o1777) ; sticky bit + + (directory "/root" 0 0) ; an exception + (directory "/home" 0 0))) + +(define (populate-root-file-system system target) + "Make the essential non-store files and directories on TARGET. This +includes /etc, /var, /run, /bin/sh, etc., and all the symlinks to SYSTEM." + (for-each (cut evaluate-populate-directive <> target) + (directives (%store-directory))) + + ;; Add system generation 1. + (symlink system + (string-append target "/var/guix/profiles/system-1-link"))) + +(define (reset-timestamps directory) + "Reset the timestamps of all the files under DIRECTORY, so that they appear +as created and modified at the Epoch." + (display "clearing file timestamps...\n") + (for-each (lambda (file) + (let ((s (lstat file))) + ;; XXX: Guile uses libc's 'utime' function (not 'futime'), so + ;; the timestamp of symlinks cannot be changed, and there are + ;; symlinks here pointing to /gnu/store, which is the host, + ;; read-only store. + (unless (eq? (stat:type s) 'symlink) + (utime file 0 0 0 0)))) + (find-files directory ""))) + +(define (register-closure store closure) + "Register CLOSURE in STORE, where STORE is the directory name of the target +store and CLOSURE is the name of a file containing a reference graph as used +by 'guix-register'. As a side effect, this resets timestamps on store files." + (let ((status (system* "guix-register" "--prefix" store + closure))) + (unless (zero? status) + (error "failed to register store items" closure)))) + +;;; install.scm ends here diff --git a/gnu/build/linux-initrd.scm b/gnu/build/linux-initrd.scm new file mode 100644 index 0000000000..60c5eec10f --- /dev/null +++ b/gnu/build/linux-initrd.scm @@ -0,0 +1,702 @@ +;;; GNU Guix --- Functional package management for GNU +;;; Copyright © 2013, 2014 Ludovic Courtès +;;; +;;; This file is part of GNU Guix. +;;; +;;; GNU Guix is free software; you can redistribute it and/or modify it +;;; under the terms of the GNU General Public License as published by +;;; the Free Software Foundation; either version 3 of the License, or (at +;;; your option) any later version. +;;; +;;; GNU Guix is distributed in the hope that it will be useful, but +;;; WITHOUT ANY WARRANTY; without even the implied warranty of +;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;;; GNU General Public License for more details. +;;; +;;; You should have received a copy of the GNU General Public License +;;; along with GNU Guix. If not, see . + +(define-module (gnu build linux-initrd) + #:use-module (rnrs io ports) + #:use-module (rnrs bytevectors) + #:use-module (system foreign) + #:use-module (system repl error-handling) + #:autoload (system repl repl) (start-repl) + #:autoload (system base compile) (compile-file) + #:use-module (srfi srfi-1) + #:use-module (srfi srfi-26) + #:use-module (ice-9 match) + #:use-module (ice-9 rdelim) + #:use-module (ice-9 ftw) + #:use-module (guix build utils) + #:export (mount-essential-file-systems + linux-command-line + find-long-option + make-essential-device-nodes + configure-qemu-networking + + disk-partitions + partition-label-predicate + find-partition-by-label + canonicalize-device-spec + + mount-flags->bit-mask + check-file-system + mount-file-system + bind-mount + + load-linux-module* + device-number + boot-system)) + +;;; Commentary: +;;; +;;; Utility procedures useful in a Linux initial RAM disk (initrd). Note that +;;; many of these use procedures not yet available in vanilla Guile (`mount', +;;; `load-linux-module', etc.); these are provided by a Guile patch used in +;;; the GNU distribution. +;;; +;;; Code: + +(define* (mount-essential-file-systems #:key (root "/")) + "Mount /proc and /sys under ROOT." + (define (scope dir) + (string-append root + (if (string-suffix? "/" root) + "" + "/") + dir)) + + (unless (file-exists? (scope "proc")) + (mkdir (scope "proc"))) + (mount "none" (scope "proc") "proc") + + (unless (file-exists? (scope "sys")) + (mkdir (scope "sys"))) + (mount "none" (scope "sys") "sysfs")) + +(define (move-essential-file-systems root) + "Move currently mounted essential file systems to ROOT." + (for-each (lambda (dir) + (let ((target (string-append root dir))) + (unless (file-exists? target) + (mkdir target)) + (mount dir target "" MS_MOVE))) + '("/proc" "/sys"))) + +(define (linux-command-line) + "Return the Linux kernel command line as a list of strings." + (string-tokenize + (call-with-input-file "/proc/cmdline" + get-string-all))) + +(define (find-long-option option arguments) + "Find OPTION among ARGUMENTS, where OPTION is something like \"--load\". +Return the value associated with OPTION, or #f on failure." + (let ((opt (string-append option "="))) + (and=> (find (cut string-prefix? opt <>) + arguments) + (lambda (arg) + (substring arg (+ 1 (string-index arg #\=))))))) + +(define-syntax %ext2-endianness + ;; Endianness of ext2 file systems. + (identifier-syntax (endianness little))) + +;; Offset in bytes of interesting parts of an ext2 superblock. See +;; . +;; TODO: Use "packed structs" from Guile-OpenGL or similar. +(define-syntax %ext2-sblock-magic (identifier-syntax 56)) +(define-syntax %ext2-sblock-creator-os (identifier-syntax 72)) +(define-syntax %ext2-sblock-uuid (identifier-syntax 104)) +(define-syntax %ext2-sblock-volume-name (identifier-syntax 120)) + +(define (read-ext2-superblock device) + "Return the raw contents of DEVICE's ext2 superblock as a bytevector, or #f +if DEVICE does not contain an ext2 file system." + (define %ext2-magic + ;; The magic bytes that identify an ext2 file system. + #xef53) + + (define superblock-size + ;; Size of the interesting part of an ext2 superblock. + 264) + + (define block + ;; The superblock contents. + (make-bytevector superblock-size)) + + (call-with-input-file device + (lambda (port) + (seek port 1024 SEEK_SET) + + ;; Note: work around . + (and (eqv? superblock-size (get-bytevector-n! port block 0 + superblock-size)) + (let ((magic (bytevector-u16-ref block %ext2-sblock-magic + %ext2-endianness))) + (and (= magic %ext2-magic) + block)))))) + +(define (ext2-superblock-uuid sblock) + "Return the UUID of ext2 superblock SBLOCK as a 16-byte bytevector." + (let ((uuid (make-bytevector 16))) + (bytevector-copy! sblock %ext2-sblock-uuid uuid 0 16) + uuid)) + +(define (ext2-superblock-volume-name sblock) + "Return the volume name of SBLOCK as a string of at most 16 characters, or +#f if SBLOCK has no volume name." + (let ((bv (make-bytevector 16))) + (bytevector-copy! sblock %ext2-sblock-volume-name bv 0 16) + + ;; This is a Latin-1, nul-terminated string. + (let ((bytes (take-while (negate zero?) (bytevector->u8-list bv)))) + (if (null? bytes) + #f + (list->string (map integer->char bytes)))))) + +(define (disk-partitions) + "Return the list of device names corresponding to valid disk partitions." + (define (partition? major minor) + (let ((marker (format #f "/sys/dev/block/~a:~a/partition" major minor))) + (catch 'system-error + (lambda () + (not (zero? (call-with-input-file marker read)))) + (lambda args + (if (= ENOENT (system-error-errno args)) + #f + (apply throw args)))))) + + (call-with-input-file "/proc/partitions" + (lambda (port) + ;; Skip the two header lines. + (read-line port) + (read-line port) + + ;; Read each subsequent line, and extract the last space-separated + ;; field. + (let loop ((parts '())) + (let ((line (read-line port))) + (if (eof-object? line) + (reverse parts) + (match (string-tokenize line) + (((= string->number major) (= string->number minor) + blocks name) + (if (partition? major minor) + (loop (cons name parts)) + (loop parts)))))))))) + +(define (partition-label-predicate label) + "Return a procedure that, when applied to a partition name such as \"sda1\", +return #t if that partition's volume name is LABEL." + (lambda (part) + (let* ((device (string-append "/dev/" part)) + (sblock (catch 'system-error + (lambda () + (read-ext2-superblock device)) + (lambda args + ;; When running on the hand-made /dev, + ;; 'disk-partitions' could return partitions for which + ;; we have no /dev node. Handle that gracefully. + (if (= ENOENT (system-error-errno args)) + (begin + (format (current-error-port) + "warning: device '~a' not found~%" + device) + #f) + (apply throw args)))))) + (and sblock + (let ((volume (ext2-superblock-volume-name sblock))) + (and volume + (string=? volume label))))))) + +(define (find-partition-by-label label) + "Return the first partition found whose volume name is LABEL, or #f if none +were found." + (and=> (find (partition-label-predicate label) + (disk-partitions)) + (cut string-append "/dev/" <>))) + +(define* (canonicalize-device-spec spec #:optional (title 'any)) + "Return the device name corresponding to SPEC. TITLE is a symbol, one of +the following: + + • 'device', in which case SPEC is known to designate a device node--e.g., + \"/dev/sda1\"; + • 'label', in which case SPEC is known to designate a partition label--e.g., + \"my-root-part\"; + • 'any', in which case SPEC can be anything. +" + (define max-trials + ;; Number of times we retry partition label resolution, 1 second per + ;; trial. Note: somebody reported a delay of 16 seconds (!) before their + ;; USB key would be detected by the kernel, so we must wait for at least + ;; this long. + 20) + + (define canonical-title + ;; The realm of canonicalization. + (if (eq? title 'any) + (if (string-prefix? "/" spec) + 'device + 'label) + title)) + + (case canonical-title + ((device) + ;; Nothing to do. + spec) + ((label) + ;; Resolve the label. + (let loop ((count 0)) + (let ((device (find-partition-by-label spec))) + (or device + ;; Some devices take a bit of time to appear, most notably USB + ;; storage devices. Thus, wait for the device to appear. + (if (> count max-trials) + (error "failed to resolve partition label" spec) + (begin + (format #t "waiting for partition '~a' to appear...~%" + spec) + (sleep 1) + (loop (+ 1 count)))))))) + ;; TODO: Add support for UUIDs. + (else + (error "unknown device title" title)))) + +(define* (make-disk-device-nodes base major #:optional (minor 0)) + "Make the block device nodes around BASE (something like \"/root/dev/sda\") +with the given MAJOR number, starting with MINOR." + (mknod base 'block-special #o644 (device-number major minor)) + (let loop ((i 1)) + (when (< i 6) + (mknod (string-append base (number->string i)) + 'block-special #o644 (device-number major (+ minor i))) + (loop (+ i 1))))) + +(define* (make-essential-device-nodes #:key (root "/")) + "Make essential device nodes under ROOT/dev." + ;; The hand-made udev! + + (define (scope dir) + (string-append root + (if (string-suffix? "/" root) + "" + "/") + dir)) + + (unless (file-exists? (scope "dev")) + (mkdir (scope "dev"))) + + ;; Make the device nodes for SCSI disks. + (make-disk-device-nodes (scope "dev/sda") 8) + (make-disk-device-nodes (scope "dev/sdb") 8 16) + (make-disk-device-nodes (scope "dev/sdc") 8 32) + (make-disk-device-nodes (scope "dev/sdd") 8 48) + + ;; SCSI CD-ROM devices (aka. "/dev/sr0" etc.). + (mknod (scope "dev/scd0") 'block-special #o644 (device-number 11 0)) + (mknod (scope "dev/scd1") 'block-special #o644 (device-number 11 1)) + + ;; The virtio (para-virtualized) block devices, as supported by QEMU/KVM. + (make-disk-device-nodes (scope "dev/vda") 252) + + ;; Memory (used by Xorg's VESA driver.) + (mknod (scope "dev/mem") 'char-special #o640 (device-number 1 1)) + (mknod (scope "dev/kmem") 'char-special #o640 (device-number 1 2)) + + ;; Inputs (used by Xorg.) + (unless (file-exists? (scope "dev/input")) + (mkdir (scope "dev/input"))) + (mknod (scope "dev/input/mice") 'char-special #o640 (device-number 13 63)) + (mknod (scope "dev/input/mouse0") 'char-special #o640 (device-number 13 32)) + (mknod (scope "dev/input/event0") 'char-special #o640 (device-number 13 64)) + + ;; System console. This node is magically created by the kernel on the + ;; initrd's root, so don't try to create it in that case. + (unless (string=? root "/") + (mknod (scope "dev/console") 'char-special #o600 + (device-number 5 1))) + + ;; TTYs. + (mknod (scope "dev/tty") 'char-special #o600 + (device-number 5 0)) + (chmod (scope "dev/tty") #o666) + (let loop ((n 0)) + (and (< n 50) + (let ((name (format #f "dev/tty~a" n))) + (mknod (scope name) 'char-special #o600 + (device-number 4 n)) + (loop (+ 1 n))))) + + ;; Serial line. + (mknod (scope "dev/ttyS0") 'char-special #o660 + (device-number 4 64)) + + ;; Pseudo ttys. + (mknod (scope "dev/ptmx") 'char-special #o666 + (device-number 5 2)) + (chmod (scope "dev/ptmx") #o666) + + ;; Create /dev/pts; it will be mounted later, at boot time. + (unless (file-exists? (scope "dev/pts")) + (mkdir (scope "dev/pts"))) + + ;; Rendez-vous point for syslogd. + (mknod (scope "dev/log") 'socket #o666 0) + (mknod (scope "dev/kmsg") 'char-special #o600 (device-number 1 11)) + + ;; Other useful nodes, notably relied on by guix-daemon. + (for-each (match-lambda + ((file major minor) + (mknod (scope file) 'char-special #o666 + (device-number major minor)) + (chmod (scope file) #o666))) + '(("dev/null" 1 3) + ("dev/zero" 1 5) + ("dev/full" 1 7) + ("dev/random" 1 8) + ("dev/urandom" 1 9))) + + (symlink "/proc/self/fd" (scope "dev/fd")) + (symlink "/proc/self/fd/0" (scope "dev/stdin")) + (symlink "/proc/self/fd/1" (scope "dev/stdout")) + (symlink "/proc/self/fd/2" (scope "dev/stderr")) + + ;; Loopback devices. + (let loop ((i 0)) + (when (< i 8) + (mknod (scope (string-append "dev/loop" (number->string i))) + 'block-special #o660 + (device-number 7 i)) + (loop (+ 1 i)))) + + ;; File systems in user space (FUSE). + (mknod (scope "dev/fuse") 'char-special #o666 (device-number 10 229))) + +(define %host-qemu-ipv4-address + (inet-pton AF_INET "10.0.2.10")) + +(define* (configure-qemu-networking #:optional (interface "eth0")) + "Setup the INTERFACE network interface and /etc/resolv.conf according to +QEMU's default networking settings (see net/slirp.c in QEMU for default +networking values.) Return #t if INTERFACE is up, #f otherwise." + (display "configuring QEMU networking...\n") + (let* ((sock (socket AF_INET SOCK_STREAM 0)) + (address (make-socket-address AF_INET %host-qemu-ipv4-address 0)) + (flags (network-interface-flags sock interface))) + (set-network-interface-address sock interface address) + (set-network-interface-flags sock interface (logior flags IFF_UP)) + + ;; Hello! We used to create /etc/resolv.conf here, with "nameserver + ;; 10.0.2.3\n". However, with Linux-libre 3.16, we're getting ENOSPC. + ;; And since it's actually unnecessary, it's gone. + + (logand (network-interface-flags sock interface) IFF_UP))) + +;; Linux mount flags, from libc's . +(define MS_RDONLY 1) +(define MS_NOSUID 2) +(define MS_NODEV 4) +(define MS_NOEXEC 8) +(define MS_BIND 4096) +(define MS_MOVE 8192) + +(define (bind-mount source target) + "Bind-mount SOURCE at TARGET." + (mount source target "" MS_BIND)) + +(define (load-linux-module* file) + "Load Linux module from FILE, the name of a `.ko' file." + (define (slurp module) + (call-with-input-file file get-bytevector-all)) + + (load-linux-module (slurp file))) + +(define (device-number major minor) + "Return the device number for the device with MAJOR and MINOR, for use as +the last argument of `mknod'." + (+ (* major 256) minor)) + +(define (pidof program) + "Return the PID of the first presumed instance of PROGRAM." + (let ((program (basename program))) + (find (lambda (pid) + (let ((exe (format #f "/proc/~a/exe" pid))) + (and=> (false-if-exception (readlink exe)) + (compose (cut string=? program <>) basename)))) + (filter-map string->number (scandir "/proc"))))) + +(define* (mount-root-file-system root type + #:key volatile-root? (unionfs "unionfs")) + "Mount the root file system of type TYPE at device ROOT. If VOLATILE-ROOT? +is true, mount ROOT read-only and make it a union with a writable tmpfs using +UNIONFS." + (define (mark-as-not-killable pid) + ;; Tell the 'user-processes' dmd service that PID must be kept alive when + ;; shutting down. + (mkdir-p "/root/etc/dmd") + (let ((port (open-file "/root/etc/dmd/do-not-kill" "a"))) + (chmod port #o600) + (write pid port) + (newline port) + (close-port port))) + + (catch #t + (lambda () + (if volatile-root? + (begin + (mkdir-p "/real-root") + (mount root "/real-root" type MS_RDONLY) + (mkdir-p "/rw-root") + (mount "none" "/rw-root" "tmpfs") + + ;; We want read-write /dev nodes. + (make-essential-device-nodes #:root "/rw-root") + + ;; Make /root a union of the tmpfs and the actual root. Use + ;; 'max_files' to set a high RLIMIT_NOFILE for the unionfs process + ;; itself. Failing to do that, we quickly run out of file + ;; descriptors; see . + (unless (zero? (system* unionfs "-o" + "cow,allow_other,use_ino,suid,dev,max_files=65536" + "/rw-root=RW:/real-root=RO" + "/root")) + (error "unionfs failed")) + + ;; Make sure unionfs remains alive till the end. Because + ;; 'fuse_daemonize' doesn't tell the PID of the forked daemon, we + ;; have to resort to 'pidof' here. + (mark-as-not-killable (pidof unionfs))) + (begin + (check-file-system root type) + (mount root "/root" type)))) + (lambda args + (format (current-error-port) "exception while mounting '~a': ~s~%" + root args) + (start-repl))) + + (copy-file "/proc/mounts" "/root/etc/mtab")) + +(define (check-file-system device type) + "Run a file system check of TYPE on DEVICE." + (define fsck + (string-append "fsck." type)) + + (let ((status (system* fsck "-v" "-p" device))) + (match (status:exit-val status) + (0 + #t) + (1 + (format (current-error-port) "'~a' corrected errors on ~a; continuing~%" + fsck device)) + (2 + (format (current-error-port) "'~a' corrected errors on ~a; rebooting~%" + fsck device) + (sleep 3) + (reboot)) + (code + (format (current-error-port) "'~a' exited with code ~a on ~a; spawning REPL~%" + fsck code device) + (start-repl))))) + +(define (mount-flags->bit-mask flags) + "Return the number suitable for the 'flags' argument of 'mount' that +corresponds to the symbols listed in FLAGS." + (let loop ((flags flags)) + (match flags + (('read-only rest ...) + (logior MS_RDONLY (loop rest))) + (('bind-mount rest ...) + (logior MS_BIND (loop rest))) + (('no-suid rest ...) + (logior MS_NOSUID (loop rest))) + (('no-dev rest ...) + (logior MS_NODEV (loop rest))) + (('no-exec rest ...) + (logior MS_NOEXEC (loop rest))) + (() + 0)))) + +(define* (mount-file-system spec #:key (root "/root")) + "Mount the file system described by SPEC under ROOT. SPEC must have the +form: + + (DEVICE TITLE MOUNT-POINT TYPE (FLAGS ...) OPTIONS CHECK?) + +DEVICE, MOUNT-POINT, and TYPE must be strings; OPTIONS can be a string or #f; +FLAGS must be a list of symbols. CHECK? is a Boolean indicating whether to +run a file system check." + (match spec + ((source title mount-point type (flags ...) options check?) + (let ((source (canonicalize-device-spec source title)) + (mount-point (string-append root "/" mount-point))) + (when check? + (check-file-system source type)) + (mkdir-p mount-point) + (mount source mount-point type (mount-flags->bit-mask flags) + (if options + (string->pointer options) + %null-pointer)) + + ;; Update /etc/mtab. + (mkdir-p (string-append root "/etc")) + (let ((port (open-file (string-append root "/etc/mtab") "a"))) + (format port "~a ~a ~a ~a 0 0~%" + source mount-point type (or options "")) + (close-port port)))))) + +(define (switch-root root) + "Switch to ROOT as the root file system, in a way similar to what +util-linux' switch_root(8) does." + (move-essential-file-systems root) + (chdir root) + + ;; Since we're about to 'rm -rf /', try to make sure we're on an initrd. + ;; TODO: Use 'statfs' to check the fs type, like klibc does. + (when (or (not (file-exists? "/init")) (directory-exists? "/home")) + (format (current-error-port) + "The root file system is probably not an initrd; \ +bailing out.~%root contents: ~s~%" (scandir "/")) + (force-output (current-error-port)) + (exit 1)) + + ;; Delete files from the old root, without crossing mount points (assuming + ;; there are no mount points in sub-directories.) That means we're leaving + ;; the empty ROOT directory behind us, but that's OK. + (let ((root-device (stat:dev (stat "/")))) + (for-each (lambda (file) + (unless (member file '("." "..")) + (let* ((file (string-append "/" file)) + (device (stat:dev (lstat file)))) + (when (= device root-device) + (delete-file-recursively file))))) + (scandir "/"))) + + ;; Make ROOT the new root. + (mount root "/" "" MS_MOVE) + (chroot ".") + (chdir "/") + + (when (file-exists? "/dev/console") + ;; Close the standard file descriptors since they refer to the old + ;; /dev/console, and reopen them. + (let ((console (open-file "/dev/console" "r+b0"))) + (for-each close-fdes '(0 1 2)) + + (dup2 (fileno console) 0) + (dup2 (fileno console) 1) + (dup2 (fileno console) 2) + + (close-port console)))) + + +(define* (boot-system #:key + (linux-modules '()) + qemu-guest-networking? + guile-modules-in-chroot? + volatile-root? + (mounts '())) + "This procedure is meant to be called from an initrd. Boot a system by +first loading LINUX-MODULES, then setting up QEMU guest networking if +QEMU-GUEST-NETWORKING? is true, mounting the file systems specified in MOUNTS, +and finally booting into the new root if any. The initrd supports kernel +command-line options '--load', '--root', and '--repl'. + +Mount the root file system, specified by the '--root' command-line argument, +if any. + +MOUNTS must be a list suitable for 'mount-file-system'. + +When GUILE-MODULES-IN-CHROOT? is true, make core Guile modules available in +the new root. + +When VOLATILE-ROOT? is true, the root file system is writable but any changes +to it are lost." + (define root-mount-point? + (match-lambda + ((device _ "/" _ ...) #t) + (_ #f))) + + (define root-fs-type + (or (any (match-lambda + ((device _ "/" type _ ...) type) + (_ #f)) + mounts) + "ext4")) + + (display "Welcome, this is GNU's early boot Guile.\n") + (display "Use '--repl' for an initrd REPL.\n\n") + + (call-with-error-handling + (lambda () + (mount-essential-file-systems) + (let* ((args (linux-command-line)) + (to-load (find-long-option "--load" args)) + (root (find-long-option "--root" args))) + + (when (member "--repl" args) + (start-repl)) + + (display "loading kernel modules...\n") + (for-each (compose load-linux-module* + (cut string-append "/modules/" <>)) + linux-modules) + + (when qemu-guest-networking? + (unless (configure-qemu-networking) + (display "network interface is DOWN\n"))) + + ;; Make /dev nodes. + (make-essential-device-nodes) + + ;; Prepare the real root file system under /root. + (unless (file-exists? "/root") + (mkdir "/root")) + (if root + (mount-root-file-system (canonicalize-device-spec root) + root-fs-type + #:volatile-root? volatile-root?) + (mount "none" "/root" "tmpfs")) + + (unless (file-exists? "/root/dev") + (mkdir "/root/dev") + (make-essential-device-nodes #:root "/root")) + + ;; Mount the specified file systems. + (for-each mount-file-system + (remove root-mount-point? mounts)) + + (when guile-modules-in-chroot? + ;; Copy the directories that contain .scm and .go files so that the + ;; child process in the chroot can load modules (we would bind-mount + ;; them but for some reason that fails with EINVAL -- XXX). + (mkdir-p "/root/share") + (mkdir-p "/root/lib") + (mount "none" "/root/share" "tmpfs") + (mount "none" "/root/lib" "tmpfs") + (copy-recursively "/share" "/root/share" + #:log (%make-void-port "w")) + (copy-recursively "/lib" "/root/lib" + #:log (%make-void-port "w"))) + + (if to-load + (begin + (switch-root "/root") + (format #t "loading '~a'...\n" to-load) + + ;; TODO: Remove /lib, /share, and /loader.go. + (primitive-load to-load) + + (format (current-error-port) + "boot program '~a' terminated, rebooting~%" + to-load) + (sleep 2) + (reboot)) + (begin + (display "no boot file passed via '--load'\n") + (display "entering a warm and cozy REPL\n") + (start-repl))))))) + +;;; linux-initrd.scm ends here diff --git a/gnu/build/vm.scm b/gnu/build/vm.scm new file mode 100644 index 0000000000..d724ca3a55 --- /dev/null +++ b/gnu/build/vm.scm @@ -0,0 +1,259 @@ +;;; GNU Guix --- Functional package management for GNU +;;; Copyright © 2013, 2014 Ludovic Courtès +;;; +;;; This file is part of GNU Guix. +;;; +;;; GNU Guix is free software; you can redistribute it and/or modify it +;;; under the terms of the GNU General Public License as published by +;;; the Free Software Foundation; either version 3 of the License, or (at +;;; your option) any later version. +;;; +;;; GNU Guix is distributed in the hope that it will be useful, but +;;; WITHOUT ANY WARRANTY; without even the implied warranty of +;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;;; GNU General Public License for more details. +;;; +;;; You should have received a copy of the GNU General Public License +;;; along with GNU Guix. If not, see . + +(define-module (gnu build vm) + #:use-module (guix build utils) + #:use-module (gnu build linux-initrd) + #:use-module (gnu build install) + #:use-module (ice-9 match) + #:use-module (ice-9 regex) + #:use-module (ice-9 rdelim) + #:use-module (srfi srfi-1) + #:use-module (srfi srfi-26) + #:export (qemu-command + load-in-linux-vm + format-partition + initialize-root-partition + initialize-partition-table + initialize-hard-disk)) + +;;; Commentary: +;;; +;;; This module provides supporting code to run virtual machines and build +;;; virtual machine images using QEMU. +;;; +;;; Code: + +(define* (qemu-command #:optional (system %host-type)) + "Return the default name of the QEMU command for SYSTEM." + (let ((cpu (substring %host-type 0 + (string-index %host-type #\-)))) + (string-append "qemu-system-" + (if (string-match "^i[3456]86$" cpu) + "i386" + cpu)))) + +(define* (load-in-linux-vm builder + #:key + output + (qemu (qemu-command)) (memory-size 512) + linux initrd + make-disk-image? (disk-image-size 100) + (disk-image-format "qcow2") + (references-graphs '())) + "Run BUILDER, a Scheme file, into a VM running LINUX with INITRD, and copy +the result to OUTPUT. + +When MAKE-DISK-IMAGE? is true, OUTPUT will contain a VM image of +DISK-IMAGE-SIZE MiB resulting from the execution of BUILDER, which may access +it via /dev/hda. + +REFERENCES-GRAPHS can specify a list of reference-graph files as produced by +the #:references-graphs parameter of 'derivation'." + (define image-file + (string-append "image." disk-image-format)) + + (when make-disk-image? + (unless (zero? (system* "qemu-img" "create" "-f" disk-image-format + image-file + (number->string disk-image-size))) + (error "qemu-img failed"))) + + (mkdir "xchg") + + (match references-graphs + ((graph-files ...) + ;; Copy the reference-graph files under xchg/ so EXP can access it. + (map (lambda (file) + (copy-file file (string-append "xchg/" file))) + graph-files)) + (_ #f)) + + (unless (zero? + (apply system* qemu "-enable-kvm" "-nographic" "-no-reboot" + "-m" (number->string memory-size) + "-net" "nic,model=virtio" + "-virtfs" + (string-append "local,id=store_dev,path=" + (%store-directory) + ",security_model=none,mount_tag=store") + "-virtfs" + (string-append "local,id=xchg_dev,path=xchg" + ",security_model=none,mount_tag=xchg") + "-kernel" linux + "-initrd" initrd + "-append" (string-append "console=ttyS0 --load=" + builder) + (if make-disk-image? + `("-drive" ,(string-append "file=" image-file + ",if=virtio")) + '()))) + (error "qemu failed" qemu)) + + (if make-disk-image? + (copy-file image-file output) + (begin + (mkdir output) + (copy-recursively "xchg" output)))) + +(define (read-reference-graph port) + "Return a list of store paths from the reference graph at PORT. +The data at PORT is the format produced by #:references-graphs." + (let loop ((line (read-line port)) + (result '())) + (cond ((eof-object? line) + (delete-duplicates result)) + ((string-prefix? "/" line) + (loop (read-line port) + (cons line result))) + (else + (loop (read-line port) + result))))) + +(define* (initialize-partition-table device partition-size + #:key + (label-type "msdos") + (offset (expt 2 20))) + "Create on DEVICE a partition table of type LABEL-TYPE, with a single +partition of PARTITION-SIZE bytes starting at OFFSET bytes. Return #t on +success." + (format #t "creating partition table with a ~a B partition...\n" + partition-size) + (unless (zero? (system* "parted" device "mklabel" label-type + "mkpart" "primary" "ext2" + (format #f "~aB" offset) + (format #f "~aB" partition-size))) + (error "failed to create partition table"))) + +(define* (populate-store reference-graphs target) + "Populate the store under directory TARGET with the items specified in +REFERENCE-GRAPHS, a list of reference-graph files." + (define store + (string-append target (%store-directory))) + + (define (things-to-copy) + ;; Return the list of store files to copy to the image. + (define (graph-from-file file) + (call-with-input-file file read-reference-graph)) + + (delete-duplicates (append-map graph-from-file reference-graphs))) + + (mkdir-p store) + (chmod store #o1775) + (for-each (lambda (thing) + (copy-recursively thing + (string-append target thing))) + (things-to-copy))) + +(define MS_BIND 4096) ; again! + +(define* (format-partition partition type + #:key label) + "Create a file system TYPE on PARTITION. If LABEL is true, use that as the +volume name." + (format #t "creating ~a partition...\n" type) + (unless (zero? (apply system* (string-append "mkfs." type) + "-F" partition + (if label + `("-L" ,label) + '()))) + (error "failed to create partition"))) + +(define* (initialize-root-partition target-directory + #:key copy-closures? register-closures? + closures system-directory) + "Initialize the root partition mounted at TARGET-DIRECTORY." + (define target-store + (string-append target-directory (%store-directory))) + + (when copy-closures? + ;; Populate the store. + (populate-store (map (cut string-append "/xchg/" <>) closures) + target-directory)) + + ;; Populate /dev. + (make-essential-device-nodes #:root target-directory) + + ;; Optionally, register the inputs in the image's store. + (when register-closures? + (unless copy-closures? + ;; XXX: 'guix-register' wants to palpate the things it registers, so + ;; bind-mount the store on the target. + (mkdir-p target-store) + (mount (%store-directory) target-store "" MS_BIND)) + + (display "registering closures...\n") + (for-each (lambda (closure) + (register-closure target-directory + (string-append "/xchg/" closure))) + closures) + (unless copy-closures? + (system* "umount" target-store))) + + ;; Add the non-store directories and files. + (display "populating...\n") + (populate-root-file-system system-directory target-directory)) + +(define* (initialize-hard-disk device + #:key + system-directory + grub.cfg + disk-image-size + (file-system-type "ext4") + file-system-label + (closures '()) + copy-closures? + (register-closures? #t)) + "Initialize DEVICE, a disk of DISK-IMAGE-SIZE bytes, with a FILE-SYSTEM-TYPE +partition with (optionally) FILE-SYSTEM-LABEL as its volume name, and with +GRUB installed. If REGISTER-CLOSURES? is true, register all of CLOSURES is +the partition's store. If COPY-CLOSURES? is true, copy all of CLOSURES to the +partition. SYSTEM-DIRECTORY is the name of the directory of the 'system' +derivation." + (define target-directory + "/fs") + + (define partition + (string-append device "1")) + + (initialize-partition-table device + (- disk-image-size (* 5 (expt 2 20)))) + + (format-partition partition file-system-type + #:label file-system-label) + + (display "mounting partition...\n") + (mkdir target-directory) + (mount partition target-directory file-system-type) + + (initialize-root-partition target-directory + #:system-directory system-directory + #:copy-closures? copy-closures? + #:register-closures? register-closures? + #:closures closures) + + (install-grub grub.cfg device target-directory) + + ;; 'guix-register' resets timestamps and everything, so no need to do it + ;; once more in that case. + (unless register-closures? + (reset-timestamps target-directory)) + + (zero? (system* "umount" target-directory))) + +;;; vm.scm ends here diff --git a/gnu/services/base.scm b/gnu/services/base.scm index 578d4cba71..274b6f5b77 100644 --- a/gnu/services/base.scm +++ b/gnu/services/base.scm @@ -29,7 +29,7 @@ (define-module (gnu services base) #:use-module ((gnu packages base) #:select (canonical-package glibc)) #:use-module (gnu packages package-management) - #:use-module ((guix build linux-initrd) + #:use-module ((gnu build linux-initrd) #:select (mount-flags->bit-mask)) #:use-module (guix gexp) #:use-module (guix monads) diff --git a/gnu/services/dmd.scm b/gnu/services/dmd.scm index dfda2708f5..87bd67a8da 100644 --- a/gnu/services/dmd.scm +++ b/gnu/services/dmd.scm @@ -35,7 +35,7 @@ (define (dmd-configuration-file services) (define modules ;; Extra modules visible to dmd.conf. '((guix build syscalls) - (guix build linux-initrd) + (gnu build linux-initrd) (guix build utils))) (mlet %store-monad ((modules (imported-modules modules)) @@ -50,7 +50,7 @@ (define config (use-modules (ice-9 ftw) (guix build syscalls) (guix build utils) - ((guix build linux-initrd) + ((gnu build linux-initrd) #:select (check-file-system canonicalize-device-spec))) (register-services diff --git a/gnu/system.scm b/gnu/system.scm index 2469ade1f3..963f4864f8 100644 --- a/gnu/system.scm +++ b/gnu/system.scm @@ -362,9 +362,9 @@ (define (operating-system-activation-script os) stateful part of OS, including user accounts and groups, special directories, etc." (define %modules - '((guix build activation) - (guix build utils) - (guix build linux-initrd))) + '((gnu build activation) + (gnu build linux-initrd) + (guix build utils))) (define (service-activations services) ;; Return the activation scripts for SERVICES. @@ -399,7 +399,7 @@ (define group-specs (set! %load-compiled-path (cons #$compiled %load-compiled-path))) - (use-modules (guix build activation)) + (use-modules (gnu build activation)) ;; Populate /etc. (activate-etc #$etc) diff --git a/gnu/system/linux-initrd.scm b/gnu/system/linux-initrd.scm index e48b399a9d..6d9e23277f 100644 --- a/gnu/system/linux-initrd.scm +++ b/gnu/system/linux-initrd.scm @@ -75,7 +75,7 @@ (define (string->regexp str) (mlet* %store-monad ((source (imported-modules modules)) (compiled (compiled-modules modules))) (define builder - ;; TODO: Move most of this code to (guix build linux-initrd). + ;; TODO: Move most of this code to (gnu build linux-initrd). #~(begin (use-modules (guix build utils) (ice-9 pretty-print) @@ -277,7 +277,7 @@ (define helper-packages (expression->initrd #~(begin - (use-modules (guix build linux-initrd) + (use-modules (gnu build linux-initrd) (guix build utils) (srfi srfi-26)) @@ -293,7 +293,7 @@ (define helper-packages #:volatile-root? '#$volatile-root?)) #:name "base-initrd" #:modules '((guix build utils) - (guix build linux-initrd)) + (gnu build linux-initrd)) #:to-copy helper-packages #:linux linux-libre #:linux-modules linux-modules)) diff --git a/gnu/system/vm.scm b/gnu/system/vm.scm index 42fc23ee8f..be9cf6b457 100644 --- a/gnu/system/vm.scm +++ b/gnu/system/vm.scm @@ -23,7 +23,7 @@ (define-module (gnu system vm) #:use-module (guix derivations) #:use-module (guix packages) #:use-module (guix monads) - #:use-module ((guix build vm) + #:use-module ((gnu build vm) #:select (qemu-command)) #:use-module (gnu packages base) #:use-module (gnu packages guile) @@ -112,9 +112,9 @@ (define* (expression->derivation-in-linux-vm name exp (qemu qemu-headless) (env-vars '()) (modules - '((guix build vm) - (guix build install) - (guix build linux-initrd) + '((gnu build vm) + (gnu build install) + (gnu build linux-initrd) (guix build utils))) (guile-for-build (%guile-for-build)) @@ -164,7 +164,7 @@ (define builder ;; Code that launches the VM that evaluates EXP. #~(begin (use-modules (guix build utils) - (guix build vm)) + (gnu build vm)) (let ((inputs '#$(list qemu coreutils)) (linux (string-append #$linux "/bzImage")) @@ -222,7 +222,7 @@ (define* (qemu-image #:key (expression->derivation-in-linux-vm name #~(begin - (use-modules (guix build vm) + (use-modules (gnu build vm) (guix build utils)) (let ((inputs diff --git a/guix/build/activation.scm b/guix/build/activation.scm deleted file mode 100644 index b04b017881..0000000000 --- a/guix/build/activation.scm +++ /dev/null @@ -1,226 +0,0 @@ -;;; GNU Guix --- Functional package management for GNU -;;; Copyright © 2013, 2014 Ludovic Courtès -;;; -;;; This file is part of GNU Guix. -;;; -;;; GNU Guix is free software; you can redistribute it and/or modify it -;;; under the terms of the GNU General Public License as published by -;;; the Free Software Foundation; either version 3 of the License, or (at -;;; your option) any later version. -;;; -;;; GNU Guix is distributed in the hope that it will be useful, but -;;; WITHOUT ANY WARRANTY; without even the implied warranty of -;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -;;; GNU General Public License for more details. -;;; -;;; You should have received a copy of the GNU General Public License -;;; along with GNU Guix. If not, see . - -(define-module (guix build activation) - #:use-module (guix build utils) - #:use-module (guix build linux-initrd) - #:use-module (ice-9 ftw) - #:use-module (ice-9 match) - #:use-module (srfi srfi-1) - #:use-module (srfi srfi-26) - #:export (activate-users+groups - activate-etc - activate-setuid-programs - activate-current-system)) - -;;; Commentary: -;;; -;;; This module provides "activation" helpers. Activation is the process that -;;; consists in setting up system-wide files and directories so that an -;;; 'operating-system' configuration becomes active. -;;; -;;; Code: - -(define* (add-group name #:key gid password system? - (log-port (current-error-port))) - "Add NAME as a user group, with the given numeric GID if specified." - ;; Use 'groupadd' from the Shadow package. - (format log-port "adding group '~a'...~%" name) - (let ((args `(,@(if gid `("-g" ,(number->string gid)) '()) - ,@(if password `("-p" ,password) '()) - ,@(if system? `("--system") '()) - ,name))) - (zero? (apply system* "groupadd" args)))) - -(define* (add-user name group - #:key uid comment home shell password system? - (supplementary-groups '()) - (log-port (current-error-port))) - "Create an account for user NAME part of GROUP, with the specified -properties. Return #t on success." - (format log-port "adding user '~a'...~%" name) - - (if (and uid (zero? uid)) - - ;; 'useradd' fails with "Cannot determine your user name" if the root - ;; account doesn't exist. Thus, for bootstrapping purposes, create that - ;; one manually. - (begin - (call-with-output-file "/etc/shadow" - (cut format <> "~a::::::::~%" name)) - (call-with-output-file "/etc/passwd" - (cut format <> "~a:x:~a:~a:~a:~a:~a~%" - name "0" "0" comment home shell)) - (chmod "/etc/shadow" #o600) - #t) - - ;; Use 'useradd' from the Shadow package. - (let ((args `(,@(if uid `("-u" ,(number->string uid)) '()) - "-g" ,(if (number? group) (number->string group) group) - ,@(if (pair? supplementary-groups) - `("-G" ,(string-join supplementary-groups ",")) - '()) - ,@(if comment `("-c" ,comment) '()) - ,@(if home - (if (file-exists? home) - `("-d" ,home) ; avoid warning from 'useradd' - `("-d" ,home "--create-home")) - '()) - ,@(if shell `("-s" ,shell) '()) - ,@(if password `("-p" ,password) '()) - ,@(if system? '("--system") '()) - ,name))) - (zero? (apply system* "useradd" args))))) - -(define (activate-users+groups users groups) - "Make sure the accounts listed in USERS and the user groups listed in GROUPS -are all available. - -Each item in USERS is a list of all the characteristics of a user account; -each item in GROUPS is a tuple with the group name, group password or #f, and -numeric gid or #f." - (define (touch file) - (close-port (open-file file "a0b"))) - - (define activate-user - (match-lambda - ((name uid group supplementary-groups comment home shell password system?) - (unless (false-if-exception (getpwnam name)) - (let ((profile-dir (string-append "/var/guix/profiles/per-user/" - name))) - (add-user name group - #:uid uid - #:system? system? - #:supplementary-groups supplementary-groups - #:comment comment - #:home home - #:shell shell - #:password password) - - (unless system? - ;; Create the profile directory for the new account. - (let ((pw (getpwnam name))) - (mkdir-p profile-dir) - (chown profile-dir (passwd:uid pw) (passwd:gid pw))))))))) - - ;; 'groupadd' aborts if the file doesn't already exist. - (touch "/etc/group") - - ;; Create the root account so we can use 'useradd' and 'groupadd'. - (activate-user (find (match-lambda - ((name (? zero?) _ ...) #t) - (_ #f)) - users)) - - ;; Then create the groups. - (for-each (match-lambda - ((name password gid system?) - (unless (false-if-exception (getgrnam name)) - (add-group name - #:gid gid #:password password - #:system? system?)))) - groups) - - ;; Finally create the other user accounts. - (for-each activate-user users)) - -(define (activate-etc etc) - "Install ETC, a directory in the store, as the source of static files for -/etc." - - ;; /etc is a mixture of static and dynamic settings. Here is where we - ;; initialize it from the static part. - - (format #t "populating /etc from ~a...~%" etc) - (let ((rm-f (lambda (f) - (false-if-exception (delete-file f))))) - (rm-f "/etc/static") - (symlink etc "/etc/static") - (for-each (lambda (file) - ;; TODO: Handle 'shadow' specially so that changed - ;; password aren't lost. - (let ((target (string-append "/etc/" file)) - (source (string-append "/etc/static/" file))) - (rm-f target) - (symlink source target))) - (scandir etc - (lambda (file) - (not (member file '("." "..")))) - - ;; The default is 'string-locale)) - (scandir %setuid-directory - (lambda (file) - (not (member file '("." "..")))) - string -;;; -;;; This file is part of GNU Guix. -;;; -;;; GNU Guix is free software; you can redistribute it and/or modify it -;;; under the terms of the GNU General Public License as published by -;;; the Free Software Foundation; either version 3 of the License, or (at -;;; your option) any later version. -;;; -;;; GNU Guix is distributed in the hope that it will be useful, but -;;; WITHOUT ANY WARRANTY; without even the implied warranty of -;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -;;; GNU General Public License for more details. -;;; -;;; You should have received a copy of the GNU General Public License -;;; along with GNU Guix. If not, see . - -(define-module (guix build install) - #:use-module (guix build utils) - #:use-module (guix build install) - #:use-module (srfi srfi-26) - #:use-module (ice-9 match) - #:export (install-grub - populate-root-file-system - reset-timestamps - register-closure)) - -;;; Commentary: -;;; -;;; This module supports the installation of the GNU system on a hard disk. -;;; It is meant to be used both in a build environment (in derivations that -;;; build VM images), and on the bare metal (when really installing the -;;; system.) -;;; -;;; Code: - -(define* (install-grub grub.cfg device mount-point) - "Install GRUB with GRUB.CFG on DEVICE, which is assumed to be mounted on -MOUNT-POINT." - (let* ((target (string-append mount-point "/boot/grub/grub.cfg")) - (pivot (string-append target ".new"))) - (mkdir-p (dirname target)) - - ;; Copy GRUB.CFG instead of just symlinking it since it's not a GC root. - ;; Do that atomically. - (copy-file grub.cfg pivot) - (rename-file pivot target) - - (unless (zero? (system* "grub-install" "--no-floppy" - "--boot-directory" - (string-append mount-point "/boot") - device)) - (error "failed to install GRUB")))) - -(define (evaluate-populate-directive directive target) - "Evaluate DIRECTIVE, an sexp describing a file or directory to create under -directory TARGET." - (let loop ((directive directive)) - (match directive - (('directory name) - (mkdir-p (string-append target name))) - (('directory name uid gid) - (let ((dir (string-append target name))) - (mkdir-p dir) - (chown dir uid gid))) - (('directory name uid gid mode) - (loop `(directory ,name ,uid ,gid)) - (chmod (string-append target name) mode)) - ((new '-> old) - (symlink old (string-append target new)))))) - -(define (directives store) - "Return a list of directives to populate the root file system that will host -STORE." - `(;; Note: the store's GID is fixed precisely so we can set it here rather - ;; than at activation time. - (directory ,store 0 30000 #o1775) - - (directory "/etc") - (directory "/var/log") ; for dmd - (directory "/var/guix/gcroots") - (directory "/var/empty") ; for no-login accounts - (directory "/var/db") ; for dhclient, etc. - (directory "/var/run") - (directory "/run") - (directory "/mnt") - (directory "/var/guix/profiles/per-user/root" 0 0) - - ;; Link to the initial system generation. - ("/var/guix/profiles/system" -> "system-1-link") - - ("/var/guix/gcroots/booted-system" -> "/run/booted-system") - ("/var/guix/gcroots/current-system" -> "/run/current-system") - - (directory "/bin") - ("/bin/sh" -> "/run/current-system/profile/bin/bash") - (directory "/tmp" 0 0 #o1777) ; sticky bit - - (directory "/root" 0 0) ; an exception - (directory "/home" 0 0))) - -(define (populate-root-file-system system target) - "Make the essential non-store files and directories on TARGET. This -includes /etc, /var, /run, /bin/sh, etc., and all the symlinks to SYSTEM." - (for-each (cut evaluate-populate-directive <> target) - (directives (%store-directory))) - - ;; Add system generation 1. - (symlink system - (string-append target "/var/guix/profiles/system-1-link"))) - -(define (reset-timestamps directory) - "Reset the timestamps of all the files under DIRECTORY, so that they appear -as created and modified at the Epoch." - (display "clearing file timestamps...\n") - (for-each (lambda (file) - (let ((s (lstat file))) - ;; XXX: Guile uses libc's 'utime' function (not 'futime'), so - ;; the timestamp of symlinks cannot be changed, and there are - ;; symlinks here pointing to /gnu/store, which is the host, - ;; read-only store. - (unless (eq? (stat:type s) 'symlink) - (utime file 0 0 0 0)))) - (find-files directory ""))) - -(define (register-closure store closure) - "Register CLOSURE in STORE, where STORE is the directory name of the target -store and CLOSURE is the name of a file containing a reference graph as used -by 'guix-register'. As a side effect, this resets timestamps on store files." - (let ((status (system* "guix-register" "--prefix" store - closure))) - (unless (zero? status) - (error "failed to register store items" closure)))) - -;;; install.scm ends here diff --git a/guix/build/linux-initrd.scm b/guix/build/linux-initrd.scm deleted file mode 100644 index d37da9a6ff..0000000000 --- a/guix/build/linux-initrd.scm +++ /dev/null @@ -1,702 +0,0 @@ -;;; GNU Guix --- Functional package management for GNU -;;; Copyright © 2013, 2014 Ludovic Courtès -;;; -;;; This file is part of GNU Guix. -;;; -;;; GNU Guix is free software; you can redistribute it and/or modify it -;;; under the terms of the GNU General Public License as published by -;;; the Free Software Foundation; either version 3 of the License, or (at -;;; your option) any later version. -;;; -;;; GNU Guix is distributed in the hope that it will be useful, but -;;; WITHOUT ANY WARRANTY; without even the implied warranty of -;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -;;; GNU General Public License for more details. -;;; -;;; You should have received a copy of the GNU General Public License -;;; along with GNU Guix. If not, see . - -(define-module (guix build linux-initrd) - #:use-module (rnrs io ports) - #:use-module (rnrs bytevectors) - #:use-module (system foreign) - #:use-module (system repl error-handling) - #:autoload (system repl repl) (start-repl) - #:autoload (system base compile) (compile-file) - #:use-module (srfi srfi-1) - #:use-module (srfi srfi-26) - #:use-module (ice-9 match) - #:use-module (ice-9 rdelim) - #:use-module (ice-9 ftw) - #:use-module (guix build utils) - #:export (mount-essential-file-systems - linux-command-line - find-long-option - make-essential-device-nodes - configure-qemu-networking - - disk-partitions - partition-label-predicate - find-partition-by-label - canonicalize-device-spec - - mount-flags->bit-mask - check-file-system - mount-file-system - bind-mount - - load-linux-module* - device-number - boot-system)) - -;;; Commentary: -;;; -;;; Utility procedures useful in a Linux initial RAM disk (initrd). Note that -;;; many of these use procedures not yet available in vanilla Guile (`mount', -;;; `load-linux-module', etc.); these are provided by a Guile patch used in -;;; the GNU distribution. -;;; -;;; Code: - -(define* (mount-essential-file-systems #:key (root "/")) - "Mount /proc and /sys under ROOT." - (define (scope dir) - (string-append root - (if (string-suffix? "/" root) - "" - "/") - dir)) - - (unless (file-exists? (scope "proc")) - (mkdir (scope "proc"))) - (mount "none" (scope "proc") "proc") - - (unless (file-exists? (scope "sys")) - (mkdir (scope "sys"))) - (mount "none" (scope "sys") "sysfs")) - -(define (move-essential-file-systems root) - "Move currently mounted essential file systems to ROOT." - (for-each (lambda (dir) - (let ((target (string-append root dir))) - (unless (file-exists? target) - (mkdir target)) - (mount dir target "" MS_MOVE))) - '("/proc" "/sys"))) - -(define (linux-command-line) - "Return the Linux kernel command line as a list of strings." - (string-tokenize - (call-with-input-file "/proc/cmdline" - get-string-all))) - -(define (find-long-option option arguments) - "Find OPTION among ARGUMENTS, where OPTION is something like \"--load\". -Return the value associated with OPTION, or #f on failure." - (let ((opt (string-append option "="))) - (and=> (find (cut string-prefix? opt <>) - arguments) - (lambda (arg) - (substring arg (+ 1 (string-index arg #\=))))))) - -(define-syntax %ext2-endianness - ;; Endianness of ext2 file systems. - (identifier-syntax (endianness little))) - -;; Offset in bytes of interesting parts of an ext2 superblock. See -;; . -;; TODO: Use "packed structs" from Guile-OpenGL or similar. -(define-syntax %ext2-sblock-magic (identifier-syntax 56)) -(define-syntax %ext2-sblock-creator-os (identifier-syntax 72)) -(define-syntax %ext2-sblock-uuid (identifier-syntax 104)) -(define-syntax %ext2-sblock-volume-name (identifier-syntax 120)) - -(define (read-ext2-superblock device) - "Return the raw contents of DEVICE's ext2 superblock as a bytevector, or #f -if DEVICE does not contain an ext2 file system." - (define %ext2-magic - ;; The magic bytes that identify an ext2 file system. - #xef53) - - (define superblock-size - ;; Size of the interesting part of an ext2 superblock. - 264) - - (define block - ;; The superblock contents. - (make-bytevector superblock-size)) - - (call-with-input-file device - (lambda (port) - (seek port 1024 SEEK_SET) - - ;; Note: work around . - (and (eqv? superblock-size (get-bytevector-n! port block 0 - superblock-size)) - (let ((magic (bytevector-u16-ref block %ext2-sblock-magic - %ext2-endianness))) - (and (= magic %ext2-magic) - block)))))) - -(define (ext2-superblock-uuid sblock) - "Return the UUID of ext2 superblock SBLOCK as a 16-byte bytevector." - (let ((uuid (make-bytevector 16))) - (bytevector-copy! sblock %ext2-sblock-uuid uuid 0 16) - uuid)) - -(define (ext2-superblock-volume-name sblock) - "Return the volume name of SBLOCK as a string of at most 16 characters, or -#f if SBLOCK has no volume name." - (let ((bv (make-bytevector 16))) - (bytevector-copy! sblock %ext2-sblock-volume-name bv 0 16) - - ;; This is a Latin-1, nul-terminated string. - (let ((bytes (take-while (negate zero?) (bytevector->u8-list bv)))) - (if (null? bytes) - #f - (list->string (map integer->char bytes)))))) - -(define (disk-partitions) - "Return the list of device names corresponding to valid disk partitions." - (define (partition? major minor) - (let ((marker (format #f "/sys/dev/block/~a:~a/partition" major minor))) - (catch 'system-error - (lambda () - (not (zero? (call-with-input-file marker read)))) - (lambda args - (if (= ENOENT (system-error-errno args)) - #f - (apply throw args)))))) - - (call-with-input-file "/proc/partitions" - (lambda (port) - ;; Skip the two header lines. - (read-line port) - (read-line port) - - ;; Read each subsequent line, and extract the last space-separated - ;; field. - (let loop ((parts '())) - (let ((line (read-line port))) - (if (eof-object? line) - (reverse parts) - (match (string-tokenize line) - (((= string->number major) (= string->number minor) - blocks name) - (if (partition? major minor) - (loop (cons name parts)) - (loop parts)))))))))) - -(define (partition-label-predicate label) - "Return a procedure that, when applied to a partition name such as \"sda1\", -return #t if that partition's volume name is LABEL." - (lambda (part) - (let* ((device (string-append "/dev/" part)) - (sblock (catch 'system-error - (lambda () - (read-ext2-superblock device)) - (lambda args - ;; When running on the hand-made /dev, - ;; 'disk-partitions' could return partitions for which - ;; we have no /dev node. Handle that gracefully. - (if (= ENOENT (system-error-errno args)) - (begin - (format (current-error-port) - "warning: device '~a' not found~%" - device) - #f) - (apply throw args)))))) - (and sblock - (let ((volume (ext2-superblock-volume-name sblock))) - (and volume - (string=? volume label))))))) - -(define (find-partition-by-label label) - "Return the first partition found whose volume name is LABEL, or #f if none -were found." - (and=> (find (partition-label-predicate label) - (disk-partitions)) - (cut string-append "/dev/" <>))) - -(define* (canonicalize-device-spec spec #:optional (title 'any)) - "Return the device name corresponding to SPEC. TITLE is a symbol, one of -the following: - - • 'device', in which case SPEC is known to designate a device node--e.g., - \"/dev/sda1\"; - • 'label', in which case SPEC is known to designate a partition label--e.g., - \"my-root-part\"; - • 'any', in which case SPEC can be anything. -" - (define max-trials - ;; Number of times we retry partition label resolution, 1 second per - ;; trial. Note: somebody reported a delay of 16 seconds (!) before their - ;; USB key would be detected by the kernel, so we must wait for at least - ;; this long. - 20) - - (define canonical-title - ;; The realm of canonicalization. - (if (eq? title 'any) - (if (string-prefix? "/" spec) - 'device - 'label) - title)) - - (case canonical-title - ((device) - ;; Nothing to do. - spec) - ((label) - ;; Resolve the label. - (let loop ((count 0)) - (let ((device (find-partition-by-label spec))) - (or device - ;; Some devices take a bit of time to appear, most notably USB - ;; storage devices. Thus, wait for the device to appear. - (if (> count max-trials) - (error "failed to resolve partition label" spec) - (begin - (format #t "waiting for partition '~a' to appear...~%" - spec) - (sleep 1) - (loop (+ 1 count)))))))) - ;; TODO: Add support for UUIDs. - (else - (error "unknown device title" title)))) - -(define* (make-disk-device-nodes base major #:optional (minor 0)) - "Make the block device nodes around BASE (something like \"/root/dev/sda\") -with the given MAJOR number, starting with MINOR." - (mknod base 'block-special #o644 (device-number major minor)) - (let loop ((i 1)) - (when (< i 6) - (mknod (string-append base (number->string i)) - 'block-special #o644 (device-number major (+ minor i))) - (loop (+ i 1))))) - -(define* (make-essential-device-nodes #:key (root "/")) - "Make essential device nodes under ROOT/dev." - ;; The hand-made udev! - - (define (scope dir) - (string-append root - (if (string-suffix? "/" root) - "" - "/") - dir)) - - (unless (file-exists? (scope "dev")) - (mkdir (scope "dev"))) - - ;; Make the device nodes for SCSI disks. - (make-disk-device-nodes (scope "dev/sda") 8) - (make-disk-device-nodes (scope "dev/sdb") 8 16) - (make-disk-device-nodes (scope "dev/sdc") 8 32) - (make-disk-device-nodes (scope "dev/sdd") 8 48) - - ;; SCSI CD-ROM devices (aka. "/dev/sr0" etc.). - (mknod (scope "dev/scd0") 'block-special #o644 (device-number 11 0)) - (mknod (scope "dev/scd1") 'block-special #o644 (device-number 11 1)) - - ;; The virtio (para-virtualized) block devices, as supported by QEMU/KVM. - (make-disk-device-nodes (scope "dev/vda") 252) - - ;; Memory (used by Xorg's VESA driver.) - (mknod (scope "dev/mem") 'char-special #o640 (device-number 1 1)) - (mknod (scope "dev/kmem") 'char-special #o640 (device-number 1 2)) - - ;; Inputs (used by Xorg.) - (unless (file-exists? (scope "dev/input")) - (mkdir (scope "dev/input"))) - (mknod (scope "dev/input/mice") 'char-special #o640 (device-number 13 63)) - (mknod (scope "dev/input/mouse0") 'char-special #o640 (device-number 13 32)) - (mknod (scope "dev/input/event0") 'char-special #o640 (device-number 13 64)) - - ;; System console. This node is magically created by the kernel on the - ;; initrd's root, so don't try to create it in that case. - (unless (string=? root "/") - (mknod (scope "dev/console") 'char-special #o600 - (device-number 5 1))) - - ;; TTYs. - (mknod (scope "dev/tty") 'char-special #o600 - (device-number 5 0)) - (chmod (scope "dev/tty") #o666) - (let loop ((n 0)) - (and (< n 50) - (let ((name (format #f "dev/tty~a" n))) - (mknod (scope name) 'char-special #o600 - (device-number 4 n)) - (loop (+ 1 n))))) - - ;; Serial line. - (mknod (scope "dev/ttyS0") 'char-special #o660 - (device-number 4 64)) - - ;; Pseudo ttys. - (mknod (scope "dev/ptmx") 'char-special #o666 - (device-number 5 2)) - (chmod (scope "dev/ptmx") #o666) - - ;; Create /dev/pts; it will be mounted later, at boot time. - (unless (file-exists? (scope "dev/pts")) - (mkdir (scope "dev/pts"))) - - ;; Rendez-vous point for syslogd. - (mknod (scope "dev/log") 'socket #o666 0) - (mknod (scope "dev/kmsg") 'char-special #o600 (device-number 1 11)) - - ;; Other useful nodes, notably relied on by guix-daemon. - (for-each (match-lambda - ((file major minor) - (mknod (scope file) 'char-special #o666 - (device-number major minor)) - (chmod (scope file) #o666))) - '(("dev/null" 1 3) - ("dev/zero" 1 5) - ("dev/full" 1 7) - ("dev/random" 1 8) - ("dev/urandom" 1 9))) - - (symlink "/proc/self/fd" (scope "dev/fd")) - (symlink "/proc/self/fd/0" (scope "dev/stdin")) - (symlink "/proc/self/fd/1" (scope "dev/stdout")) - (symlink "/proc/self/fd/2" (scope "dev/stderr")) - - ;; Loopback devices. - (let loop ((i 0)) - (when (< i 8) - (mknod (scope (string-append "dev/loop" (number->string i))) - 'block-special #o660 - (device-number 7 i)) - (loop (+ 1 i)))) - - ;; File systems in user space (FUSE). - (mknod (scope "dev/fuse") 'char-special #o666 (device-number 10 229))) - -(define %host-qemu-ipv4-address - (inet-pton AF_INET "10.0.2.10")) - -(define* (configure-qemu-networking #:optional (interface "eth0")) - "Setup the INTERFACE network interface and /etc/resolv.conf according to -QEMU's default networking settings (see net/slirp.c in QEMU for default -networking values.) Return #t if INTERFACE is up, #f otherwise." - (display "configuring QEMU networking...\n") - (let* ((sock (socket AF_INET SOCK_STREAM 0)) - (address (make-socket-address AF_INET %host-qemu-ipv4-address 0)) - (flags (network-interface-flags sock interface))) - (set-network-interface-address sock interface address) - (set-network-interface-flags sock interface (logior flags IFF_UP)) - - ;; Hello! We used to create /etc/resolv.conf here, with "nameserver - ;; 10.0.2.3\n". However, with Linux-libre 3.16, we're getting ENOSPC. - ;; And since it's actually unnecessary, it's gone. - - (logand (network-interface-flags sock interface) IFF_UP))) - -;; Linux mount flags, from libc's . -(define MS_RDONLY 1) -(define MS_NOSUID 2) -(define MS_NODEV 4) -(define MS_NOEXEC 8) -(define MS_BIND 4096) -(define MS_MOVE 8192) - -(define (bind-mount source target) - "Bind-mount SOURCE at TARGET." - (mount source target "" MS_BIND)) - -(define (load-linux-module* file) - "Load Linux module from FILE, the name of a `.ko' file." - (define (slurp module) - (call-with-input-file file get-bytevector-all)) - - (load-linux-module (slurp file))) - -(define (device-number major minor) - "Return the device number for the device with MAJOR and MINOR, for use as -the last argument of `mknod'." - (+ (* major 256) minor)) - -(define (pidof program) - "Return the PID of the first presumed instance of PROGRAM." - (let ((program (basename program))) - (find (lambda (pid) - (let ((exe (format #f "/proc/~a/exe" pid))) - (and=> (false-if-exception (readlink exe)) - (compose (cut string=? program <>) basename)))) - (filter-map string->number (scandir "/proc"))))) - -(define* (mount-root-file-system root type - #:key volatile-root? (unionfs "unionfs")) - "Mount the root file system of type TYPE at device ROOT. If VOLATILE-ROOT? -is true, mount ROOT read-only and make it a union with a writable tmpfs using -UNIONFS." - (define (mark-as-not-killable pid) - ;; Tell the 'user-processes' dmd service that PID must be kept alive when - ;; shutting down. - (mkdir-p "/root/etc/dmd") - (let ((port (open-file "/root/etc/dmd/do-not-kill" "a"))) - (chmod port #o600) - (write pid port) - (newline port) - (close-port port))) - - (catch #t - (lambda () - (if volatile-root? - (begin - (mkdir-p "/real-root") - (mount root "/real-root" type MS_RDONLY) - (mkdir-p "/rw-root") - (mount "none" "/rw-root" "tmpfs") - - ;; We want read-write /dev nodes. - (make-essential-device-nodes #:root "/rw-root") - - ;; Make /root a union of the tmpfs and the actual root. Use - ;; 'max_files' to set a high RLIMIT_NOFILE for the unionfs process - ;; itself. Failing to do that, we quickly run out of file - ;; descriptors; see . - (unless (zero? (system* unionfs "-o" - "cow,allow_other,use_ino,suid,dev,max_files=65536" - "/rw-root=RW:/real-root=RO" - "/root")) - (error "unionfs failed")) - - ;; Make sure unionfs remains alive till the end. Because - ;; 'fuse_daemonize' doesn't tell the PID of the forked daemon, we - ;; have to resort to 'pidof' here. - (mark-as-not-killable (pidof unionfs))) - (begin - (check-file-system root type) - (mount root "/root" type)))) - (lambda args - (format (current-error-port) "exception while mounting '~a': ~s~%" - root args) - (start-repl))) - - (copy-file "/proc/mounts" "/root/etc/mtab")) - -(define (check-file-system device type) - "Run a file system check of TYPE on DEVICE." - (define fsck - (string-append "fsck." type)) - - (let ((status (system* fsck "-v" "-p" device))) - (match (status:exit-val status) - (0 - #t) - (1 - (format (current-error-port) "'~a' corrected errors on ~a; continuing~%" - fsck device)) - (2 - (format (current-error-port) "'~a' corrected errors on ~a; rebooting~%" - fsck device) - (sleep 3) - (reboot)) - (code - (format (current-error-port) "'~a' exited with code ~a on ~a; spawning REPL~%" - fsck code device) - (start-repl))))) - -(define (mount-flags->bit-mask flags) - "Return the number suitable for the 'flags' argument of 'mount' that -corresponds to the symbols listed in FLAGS." - (let loop ((flags flags)) - (match flags - (('read-only rest ...) - (logior MS_RDONLY (loop rest))) - (('bind-mount rest ...) - (logior MS_BIND (loop rest))) - (('no-suid rest ...) - (logior MS_NOSUID (loop rest))) - (('no-dev rest ...) - (logior MS_NODEV (loop rest))) - (('no-exec rest ...) - (logior MS_NOEXEC (loop rest))) - (() - 0)))) - -(define* (mount-file-system spec #:key (root "/root")) - "Mount the file system described by SPEC under ROOT. SPEC must have the -form: - - (DEVICE TITLE MOUNT-POINT TYPE (FLAGS ...) OPTIONS CHECK?) - -DEVICE, MOUNT-POINT, and TYPE must be strings; OPTIONS can be a string or #f; -FLAGS must be a list of symbols. CHECK? is a Boolean indicating whether to -run a file system check." - (match spec - ((source title mount-point type (flags ...) options check?) - (let ((source (canonicalize-device-spec source title)) - (mount-point (string-append root "/" mount-point))) - (when check? - (check-file-system source type)) - (mkdir-p mount-point) - (mount source mount-point type (mount-flags->bit-mask flags) - (if options - (string->pointer options) - %null-pointer)) - - ;; Update /etc/mtab. - (mkdir-p (string-append root "/etc")) - (let ((port (open-file (string-append root "/etc/mtab") "a"))) - (format port "~a ~a ~a ~a 0 0~%" - source mount-point type (or options "")) - (close-port port)))))) - -(define (switch-root root) - "Switch to ROOT as the root file system, in a way similar to what -util-linux' switch_root(8) does." - (move-essential-file-systems root) - (chdir root) - - ;; Since we're about to 'rm -rf /', try to make sure we're on an initrd. - ;; TODO: Use 'statfs' to check the fs type, like klibc does. - (when (or (not (file-exists? "/init")) (directory-exists? "/home")) - (format (current-error-port) - "The root file system is probably not an initrd; \ -bailing out.~%root contents: ~s~%" (scandir "/")) - (force-output (current-error-port)) - (exit 1)) - - ;; Delete files from the old root, without crossing mount points (assuming - ;; there are no mount points in sub-directories.) That means we're leaving - ;; the empty ROOT directory behind us, but that's OK. - (let ((root-device (stat:dev (stat "/")))) - (for-each (lambda (file) - (unless (member file '("." "..")) - (let* ((file (string-append "/" file)) - (device (stat:dev (lstat file)))) - (when (= device root-device) - (delete-file-recursively file))))) - (scandir "/"))) - - ;; Make ROOT the new root. - (mount root "/" "" MS_MOVE) - (chroot ".") - (chdir "/") - - (when (file-exists? "/dev/console") - ;; Close the standard file descriptors since they refer to the old - ;; /dev/console, and reopen them. - (let ((console (open-file "/dev/console" "r+b0"))) - (for-each close-fdes '(0 1 2)) - - (dup2 (fileno console) 0) - (dup2 (fileno console) 1) - (dup2 (fileno console) 2) - - (close-port console)))) - - -(define* (boot-system #:key - (linux-modules '()) - qemu-guest-networking? - guile-modules-in-chroot? - volatile-root? - (mounts '())) - "This procedure is meant to be called from an initrd. Boot a system by -first loading LINUX-MODULES, then setting up QEMU guest networking if -QEMU-GUEST-NETWORKING? is true, mounting the file systems specified in MOUNTS, -and finally booting into the new root if any. The initrd supports kernel -command-line options '--load', '--root', and '--repl'. - -Mount the root file system, specified by the '--root' command-line argument, -if any. - -MOUNTS must be a list suitable for 'mount-file-system'. - -When GUILE-MODULES-IN-CHROOT? is true, make core Guile modules available in -the new root. - -When VOLATILE-ROOT? is true, the root file system is writable but any changes -to it are lost." - (define root-mount-point? - (match-lambda - ((device _ "/" _ ...) #t) - (_ #f))) - - (define root-fs-type - (or (any (match-lambda - ((device _ "/" type _ ...) type) - (_ #f)) - mounts) - "ext4")) - - (display "Welcome, this is GNU's early boot Guile.\n") - (display "Use '--repl' for an initrd REPL.\n\n") - - (call-with-error-handling - (lambda () - (mount-essential-file-systems) - (let* ((args (linux-command-line)) - (to-load (find-long-option "--load" args)) - (root (find-long-option "--root" args))) - - (when (member "--repl" args) - (start-repl)) - - (display "loading kernel modules...\n") - (for-each (compose load-linux-module* - (cut string-append "/modules/" <>)) - linux-modules) - - (when qemu-guest-networking? - (unless (configure-qemu-networking) - (display "network interface is DOWN\n"))) - - ;; Make /dev nodes. - (make-essential-device-nodes) - - ;; Prepare the real root file system under /root. - (unless (file-exists? "/root") - (mkdir "/root")) - (if root - (mount-root-file-system (canonicalize-device-spec root) - root-fs-type - #:volatile-root? volatile-root?) - (mount "none" "/root" "tmpfs")) - - (unless (file-exists? "/root/dev") - (mkdir "/root/dev") - (make-essential-device-nodes #:root "/root")) - - ;; Mount the specified file systems. - (for-each mount-file-system - (remove root-mount-point? mounts)) - - (when guile-modules-in-chroot? - ;; Copy the directories that contain .scm and .go files so that the - ;; child process in the chroot can load modules (we would bind-mount - ;; them but for some reason that fails with EINVAL -- XXX). - (mkdir-p "/root/share") - (mkdir-p "/root/lib") - (mount "none" "/root/share" "tmpfs") - (mount "none" "/root/lib" "tmpfs") - (copy-recursively "/share" "/root/share" - #:log (%make-void-port "w")) - (copy-recursively "/lib" "/root/lib" - #:log (%make-void-port "w"))) - - (if to-load - (begin - (switch-root "/root") - (format #t "loading '~a'...\n" to-load) - - ;; TODO: Remove /lib, /share, and /loader.go. - (primitive-load to-load) - - (format (current-error-port) - "boot program '~a' terminated, rebooting~%" - to-load) - (sleep 2) - (reboot)) - (begin - (display "no boot file passed via '--load'\n") - (display "entering a warm and cozy REPL\n") - (start-repl))))))) - -;;; linux-initrd.scm ends here diff --git a/guix/build/vm.scm b/guix/build/vm.scm deleted file mode 100644 index 11e05f70be..0000000000 --- a/guix/build/vm.scm +++ /dev/null @@ -1,259 +0,0 @@ -;;; GNU Guix --- Functional package management for GNU -;;; Copyright © 2013, 2014 Ludovic Courtès -;;; -;;; This file is part of GNU Guix. -;;; -;;; GNU Guix is free software; you can redistribute it and/or modify it -;;; under the terms of the GNU General Public License as published by -;;; the Free Software Foundation; either version 3 of the License, or (at -;;; your option) any later version. -;;; -;;; GNU Guix is distributed in the hope that it will be useful, but -;;; WITHOUT ANY WARRANTY; without even the implied warranty of -;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -;;; GNU General Public License for more details. -;;; -;;; You should have received a copy of the GNU General Public License -;;; along with GNU Guix. If not, see . - -(define-module (guix build vm) - #:use-module (guix build utils) - #:use-module (guix build linux-initrd) - #:use-module (guix build install) - #:use-module (ice-9 match) - #:use-module (ice-9 regex) - #:use-module (ice-9 rdelim) - #:use-module (srfi srfi-1) - #:use-module (srfi srfi-26) - #:export (qemu-command - load-in-linux-vm - format-partition - initialize-root-partition - initialize-partition-table - initialize-hard-disk)) - -;;; Commentary: -;;; -;;; This module provides supporting code to run virtual machines and build -;;; virtual machine images using QEMU. -;;; -;;; Code: - -(define* (qemu-command #:optional (system %host-type)) - "Return the default name of the QEMU command for SYSTEM." - (let ((cpu (substring %host-type 0 - (string-index %host-type #\-)))) - (string-append "qemu-system-" - (if (string-match "^i[3456]86$" cpu) - "i386" - cpu)))) - -(define* (load-in-linux-vm builder - #:key - output - (qemu (qemu-command)) (memory-size 512) - linux initrd - make-disk-image? (disk-image-size 100) - (disk-image-format "qcow2") - (references-graphs '())) - "Run BUILDER, a Scheme file, into a VM running LINUX with INITRD, and copy -the result to OUTPUT. - -When MAKE-DISK-IMAGE? is true, OUTPUT will contain a VM image of -DISK-IMAGE-SIZE MiB resulting from the execution of BUILDER, which may access -it via /dev/hda. - -REFERENCES-GRAPHS can specify a list of reference-graph files as produced by -the #:references-graphs parameter of 'derivation'." - (define image-file - (string-append "image." disk-image-format)) - - (when make-disk-image? - (unless (zero? (system* "qemu-img" "create" "-f" disk-image-format - image-file - (number->string disk-image-size))) - (error "qemu-img failed"))) - - (mkdir "xchg") - - (match references-graphs - ((graph-files ...) - ;; Copy the reference-graph files under xchg/ so EXP can access it. - (map (lambda (file) - (copy-file file (string-append "xchg/" file))) - graph-files)) - (_ #f)) - - (unless (zero? - (apply system* qemu "-enable-kvm" "-nographic" "-no-reboot" - "-m" (number->string memory-size) - "-net" "nic,model=virtio" - "-virtfs" - (string-append "local,id=store_dev,path=" - (%store-directory) - ",security_model=none,mount_tag=store") - "-virtfs" - (string-append "local,id=xchg_dev,path=xchg" - ",security_model=none,mount_tag=xchg") - "-kernel" linux - "-initrd" initrd - "-append" (string-append "console=ttyS0 --load=" - builder) - (if make-disk-image? - `("-drive" ,(string-append "file=" image-file - ",if=virtio")) - '()))) - (error "qemu failed" qemu)) - - (if make-disk-image? - (copy-file image-file output) - (begin - (mkdir output) - (copy-recursively "xchg" output)))) - -(define (read-reference-graph port) - "Return a list of store paths from the reference graph at PORT. -The data at PORT is the format produced by #:references-graphs." - (let loop ((line (read-line port)) - (result '())) - (cond ((eof-object? line) - (delete-duplicates result)) - ((string-prefix? "/" line) - (loop (read-line port) - (cons line result))) - (else - (loop (read-line port) - result))))) - -(define* (initialize-partition-table device partition-size - #:key - (label-type "msdos") - (offset (expt 2 20))) - "Create on DEVICE a partition table of type LABEL-TYPE, with a single -partition of PARTITION-SIZE bytes starting at OFFSET bytes. Return #t on -success." - (format #t "creating partition table with a ~a B partition...\n" - partition-size) - (unless (zero? (system* "parted" device "mklabel" label-type - "mkpart" "primary" "ext2" - (format #f "~aB" offset) - (format #f "~aB" partition-size))) - (error "failed to create partition table"))) - -(define* (populate-store reference-graphs target) - "Populate the store under directory TARGET with the items specified in -REFERENCE-GRAPHS, a list of reference-graph files." - (define store - (string-append target (%store-directory))) - - (define (things-to-copy) - ;; Return the list of store files to copy to the image. - (define (graph-from-file file) - (call-with-input-file file read-reference-graph)) - - (delete-duplicates (append-map graph-from-file reference-graphs))) - - (mkdir-p store) - (chmod store #o1775) - (for-each (lambda (thing) - (copy-recursively thing - (string-append target thing))) - (things-to-copy))) - -(define MS_BIND 4096) ; again! - -(define* (format-partition partition type - #:key label) - "Create a file system TYPE on PARTITION. If LABEL is true, use that as the -volume name." - (format #t "creating ~a partition...\n" type) - (unless (zero? (apply system* (string-append "mkfs." type) - "-F" partition - (if label - `("-L" ,label) - '()))) - (error "failed to create partition"))) - -(define* (initialize-root-partition target-directory - #:key copy-closures? register-closures? - closures system-directory) - "Initialize the root partition mounted at TARGET-DIRECTORY." - (define target-store - (string-append target-directory (%store-directory))) - - (when copy-closures? - ;; Populate the store. - (populate-store (map (cut string-append "/xchg/" <>) closures) - target-directory)) - - ;; Populate /dev. - (make-essential-device-nodes #:root target-directory) - - ;; Optionally, register the inputs in the image's store. - (when register-closures? - (unless copy-closures? - ;; XXX: 'guix-register' wants to palpate the things it registers, so - ;; bind-mount the store on the target. - (mkdir-p target-store) - (mount (%store-directory) target-store "" MS_BIND)) - - (display "registering closures...\n") - (for-each (lambda (closure) - (register-closure target-directory - (string-append "/xchg/" closure))) - closures) - (unless copy-closures? - (system* "umount" target-store))) - - ;; Add the non-store directories and files. - (display "populating...\n") - (populate-root-file-system system-directory target-directory)) - -(define* (initialize-hard-disk device - #:key - system-directory - grub.cfg - disk-image-size - (file-system-type "ext4") - file-system-label - (closures '()) - copy-closures? - (register-closures? #t)) - "Initialize DEVICE, a disk of DISK-IMAGE-SIZE bytes, with a FILE-SYSTEM-TYPE -partition with (optionally) FILE-SYSTEM-LABEL as its volume name, and with -GRUB installed. If REGISTER-CLOSURES? is true, register all of CLOSURES is -the partition's store. If COPY-CLOSURES? is true, copy all of CLOSURES to the -partition. SYSTEM-DIRECTORY is the name of the directory of the 'system' -derivation." - (define target-directory - "/fs") - - (define partition - (string-append device "1")) - - (initialize-partition-table device - (- disk-image-size (* 5 (expt 2 20)))) - - (format-partition partition file-system-type - #:label file-system-label) - - (display "mounting partition...\n") - (mkdir target-directory) - (mount partition target-directory file-system-type) - - (initialize-root-partition target-directory - #:system-directory system-directory - #:copy-closures? copy-closures? - #:register-closures? register-closures? - #:closures closures) - - (install-grub grub.cfg device target-directory) - - ;; 'guix-register' resets timestamps and everything, so no need to do it - ;; once more in that case. - (unless register-closures? - (reset-timestamps target-directory)) - - (zero? (system* "umount" target-directory))) - -;;; vm.scm ends here diff --git a/guix/scripts/system.scm b/guix/scripts/system.scm index 4f1869af38..7c0dde9030 100644 --- a/guix/scripts/system.scm +++ b/guix/scripts/system.scm @@ -28,7 +28,7 @@ (define-module (guix scripts system) #:use-module (guix profiles) #:use-module (guix scripts build) #:use-module (guix build utils) - #:use-module (guix build install) + #:use-module (gnu build install) #:use-module (gnu system) #:use-module (gnu system vm) #:use-module (gnu system grub) -- cgit v1.2.3 From 39c4563aeaa525e71328d1ec08ef568c8a124152 Mon Sep 17 00:00:00 2001 From: Ludovic Courtès Date: Thu, 4 Sep 2014 21:35:57 +0200 Subject: profiles: Use a real arrow to denote upgrades in ASCII. Suggested by Alex Kost. * guix/profiles.scm (right-arrow): Fall back to "->". * tests/profiles.scm ("manifest-show-transaction"): Adjust accordingly. --- guix/profiles.scm | 2 +- tests/profiles.scm | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'guix') diff --git a/guix/profiles.scm b/guix/profiles.scm index 919f27d250..9dc9ab43b9 100644 --- a/guix/profiles.scm +++ b/guix/profiles.scm @@ -326,7 +326,7 @@ (define (right-arrow port) (lambda () (display arrow))))) (lambda (key . args) - ">"))))) + "->"))))) (define* (manifest-show-transaction store manifest transaction #:key dry-run?) diff --git a/tests/profiles.scm b/tests/profiles.scm index 879f71073f..99f1fd2763 100644 --- a/tests/profiles.scm +++ b/tests/profiles.scm @@ -167,7 +167,7 @@ (define glibc (with-error-to-string (lambda () (manifest-show-transaction store m t))))) - (string-match "guile\t1.8.8 > 2.0.9" + (string-match "guile\t1.8.8 -> 2.0.9" (with-fluids ((%default-port-encoding "ISO-8859-1")) (with-error-to-string (lambda () -- cgit v1.2.3 From 6fd1a7967481037560d2ab25f31da182822ef889 Mon Sep 17 00:00:00 2001 From: Ludovic Courtès Date: Thu, 4 Sep 2014 23:05:12 +0200 Subject: vm: Move store copy handling to (guix build store-copy). * gnu/build/vm.scm (read-reference-graph, populate-store): Move to... * guix/build/store-copy.scm: ... here. New file. * Makefile.am (MODULES): Add it. * gnu/system/vm.scm (expression->derivation-in-linux-vm): Adjust default #:modules values accordingly. * tests/gexp.scm ("gexp->derivation, store copy"): New test. --- Makefile.am | 1 + gnu/build/vm.scm | 37 +------------------------ gnu/system/vm.scm | 3 ++- guix/build/store-copy.scm | 69 +++++++++++++++++++++++++++++++++++++++++++++++ tests/gexp.scm | 38 ++++++++++++++++++++++++++ 5 files changed, 111 insertions(+), 37 deletions(-) create mode 100644 guix/build/store-copy.scm (limited to 'guix') diff --git a/Makefile.am b/Makefile.am index 156c560665..1f2c4db80d 100644 --- a/Makefile.am +++ b/Makefile.am @@ -64,6 +64,7 @@ MODULES = \ guix/build/gnu-dist.scm \ guix/build/perl-build-system.scm \ guix/build/python-build-system.scm \ + guix/build/store-copy.scm \ guix/build/utils.scm \ guix/build/union.scm \ guix/build/pull.scm \ diff --git a/gnu/build/vm.scm b/gnu/build/vm.scm index ad63a2240d..27ccd047b7 100644 --- a/gnu/build/vm.scm +++ b/gnu/build/vm.scm @@ -18,12 +18,11 @@ (define-module (gnu build vm) #:use-module (guix build utils) + #:use-module (guix build store-copy) #:use-module (gnu build linux-boot) #:use-module (gnu build install) #:use-module (ice-9 match) #:use-module (ice-9 regex) - #:use-module (ice-9 rdelim) - #:use-module (srfi srfi-1) #:use-module (srfi srfi-26) #:export (qemu-command load-in-linux-vm @@ -111,20 +110,6 @@ (define image-file (mkdir output) (copy-recursively "xchg" output)))) -(define (read-reference-graph port) - "Return a list of store paths from the reference graph at PORT. -The data at PORT is the format produced by #:references-graphs." - (let loop ((line (read-line port)) - (result '())) - (cond ((eof-object? line) - (delete-duplicates result)) - ((string-prefix? "/" line) - (loop (read-line port) - (cons line result))) - (else - (loop (read-line port) - result))))) - (define* (initialize-partition-table device partition-size #:key (label-type "msdos") @@ -140,26 +125,6 @@ (define* (initialize-partition-table device partition-size (format #f "~aB" partition-size))) (error "failed to create partition table"))) -(define* (populate-store reference-graphs target) - "Populate the store under directory TARGET with the items specified in -REFERENCE-GRAPHS, a list of reference-graph files." - (define store - (string-append target (%store-directory))) - - (define (things-to-copy) - ;; Return the list of store files to copy to the image. - (define (graph-from-file file) - (call-with-input-file file read-reference-graph)) - - (delete-duplicates (append-map graph-from-file reference-graphs))) - - (mkdir-p store) - (chmod store #o1775) - (for-each (lambda (thing) - (copy-recursively thing - (string-append target thing))) - (things-to-copy))) - (define MS_BIND 4096) ; again! (define* (format-partition partition type diff --git a/gnu/system/vm.scm b/gnu/system/vm.scm index d263edb4f1..624f2a680a 100644 --- a/gnu/system/vm.scm +++ b/gnu/system/vm.scm @@ -116,7 +116,8 @@ (define* (expression->derivation-in-linux-vm name exp (gnu build install) (gnu build linux-boot) (gnu build file-systems) - (guix build utils))) + (guix build utils) + (guix build store-copy))) (guile-for-build (%guile-for-build)) diff --git a/guix/build/store-copy.scm b/guix/build/store-copy.scm new file mode 100644 index 0000000000..a296bdf78f --- /dev/null +++ b/guix/build/store-copy.scm @@ -0,0 +1,69 @@ +;;; GNU Guix --- Functional package management for GNU +;;; Copyright © 2013, 2014 Ludovic Courtès +;;; +;;; This file is part of GNU Guix. +;;; +;;; GNU Guix is free software; you can redistribute it and/or modify it +;;; under the terms of the GNU General Public License as published by +;;; the Free Software Foundation; either version 3 of the License, or (at +;;; your option) any later version. +;;; +;;; GNU Guix is distributed in the hope that it will be useful, but +;;; WITHOUT ANY WARRANTY; without even the implied warranty of +;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;;; GNU General Public License for more details. +;;; +;;; You should have received a copy of the GNU General Public License +;;; along with GNU Guix. If not, see . + +(define-module (guix build store-copy) + #:use-module (guix build utils) + #:use-module (srfi srfi-1) + #:use-module (ice-9 rdelim) + #:export (read-reference-graph + populate-store)) + +;;; Commentary: +;;; +;;; This module provides the tools to copy store items and their dependencies +;;; to another store. It relies on the availability of "reference graph" +;;; files as produced by 'gexp->derivation' et al. with the +;;; #:references-graphs parameter. +;;; +;;; Code: + +(define (read-reference-graph port) + "Return a list of store paths from the reference graph at PORT. +The data at PORT is the format produced by #:references-graphs." + (let loop ((line (read-line port)) + (result '())) + (cond ((eof-object? line) + (delete-duplicates result)) + ((string-prefix? "/" line) + (loop (read-line port) + (cons line result))) + (else + (loop (read-line port) + result))))) + +(define* (populate-store reference-graphs target) + "Populate the store under directory TARGET with the items specified in +REFERENCE-GRAPHS, a list of reference-graph files." + (define store + (string-append target (%store-directory))) + + (define (things-to-copy) + ;; Return the list of store files to copy to the image. + (define (graph-from-file file) + (call-with-input-file file read-reference-graph)) + + (delete-duplicates (append-map graph-from-file reference-graphs))) + + (mkdir-p store) + (chmod store #o1775) + (for-each (lambda (thing) + (copy-recursively thing + (string-append target thing))) + (things-to-copy))) + +;;; store-copy.scm ends here diff --git a/tests/gexp.scm b/tests/gexp.scm index bf52401c66..a08164c484 100644 --- a/tests/gexp.scm +++ b/tests/gexp.scm @@ -324,6 +324,44 @@ (define (match-input thing) (return (string=? (derivation-file-name drv) (derivation-file-name xdrv))))) +(test-assertm "gexp->derivation, store copy" + (let ((build-one #~(call-with-output-file #$output + (lambda (port) + (display "This is the one." port)))) + (build-two (lambda (one) + #~(begin + (mkdir #$output) + (symlink #$one (string-append #$output "/one")) + (call-with-output-file (string-append #$output "/two") + (lambda (port) + (display "This is the second one." port)))))) + (build-drv (lambda (two) + #~(begin + (use-modules (guix build store-copy)) + + (mkdir #$output) + '#$two ;make it an input + (populate-store '("graph") #$output))))) + (mlet* %store-monad ((one (gexp->derivation "one" build-one)) + (two (gexp->derivation "two" (build-two one))) + (dir -> (derivation->output-path two)) + (drv (gexp->derivation "store-copy" (build-drv two) + #:references-graphs + `(("graph" . ,dir)) + #:modules + '((guix build store-copy) + (guix build utils)))) + (ok? (built-derivations (list drv))) + (out -> (derivation->output-path drv))) + (let ((one (derivation->output-path one)) + (two (derivation->output-path two))) + (return (and ok? + (file-exists? (string-append out "/" one)) + (file-exists? (string-append out "/" two)) + (file-exists? (string-append out "/" two "/two")) + (string=? (readlink (string-append out "/" two "/one")) + one))))))) + (define shebang (string-append "#!" (derivation->output-path (%guile-for-build)) "/bin/guile --no-auto-compile")) -- cgit v1.2.3 From b50c5b741891eabb16b83091d911c6ebd9b890d3 Mon Sep 17 00:00:00 2001 From: Ludovic Courtès Date: Fri, 5 Sep 2014 23:11:04 +0200 Subject: pull: Add a compilation progress report. * guix/build/pull.scm (report-build-progress): New procedure. (p-for-each): Add #:progress parameter. [loop]: Keep track of the number of completed processes. Tail-call PROGRESS at each loop iteration. (build-guix): Add #:debug-port parameter. Use it for verbose messages. Change 'tar' flags to 'xf'. Around 'compile-file' call, bind CURRENT-WARNING-PORT to DEBUG-PORT. * guix/scripts/pull.scm (unpack): Add #:verbose? parameter. [builder]: Pass #:debug-port to 'build-guix'. (guix-pull): Leave CURRENT-BUILD-OUTPUT-PORT unchanged. Pass #:verbose? to 'unpack'. --- guix/build/pull.scm | 77 ++++++++++++++++++++++++++++++++++++--------------- guix/scripts/pull.scm | 19 +++++++------ 2 files changed, 65 insertions(+), 31 deletions(-) (limited to 'guix') diff --git a/guix/build/pull.scm b/guix/build/pull.scm index e5b8797503..841787f0bb 100644 --- a/guix/build/pull.scm +++ b/guix/build/pull.scm @@ -21,6 +21,7 @@ (define-module (guix build pull) #:use-module (system base compile) #:use-module (ice-9 ftw) #:use-module (ice-9 match) + #:use-module (ice-9 format) #:use-module (srfi srfi-1) #:use-module (srfi srfi-11) #:use-module (srfi srfi-26) @@ -47,43 +48,70 @@ (define (call-with-process thunk) (pid #t))) +(define* (report-build-progress total completed cont + #:optional (log-port (current-error-port))) + "Report that COMPLETED out of TOTAL files have been completed, and call +CONT." + (display #\cr log-port) + (format log-port "compiling...\t~5,1f% of ~d files" ;FIXME: i18n + (* 100. (/ completed total)) total) + (force-output log-port) + (cont)) + (define* (p-for-each proc lst - #:optional (max-processes (current-processor-count))) + #:optional (max-processes (current-processor-count)) + #:key (progress report-build-progress)) "Invoke PROC for each element of LST in a separate process, using up to -MAX-PROCESSES processes in parallel. Raise an error if one of the processes -exit with non-zero." +MAX-PROCESSES processes in parallel. Call PROGRESS at each step, passing it +the continuation. Raise an error if one of the processes exit with non-zero." + (define total + (length lst)) + (define (wait-for-one-process) (match (waitpid WAIT_ANY) ((_ . status) (unless (zero? (status:exit-val status)) (error "process failed" proc status))))) - (let loop ((lst lst) - (running 0)) + (let loop ((lst lst) + (running 0) + (completed 0)) (match lst (() (or (zero? running) - (begin + (let ((running (- running 1)) + (completed (+ completed 1))) (wait-for-one-process) - (loop lst (- running 1))))) + (progress total completed + (lambda () + (loop lst running completed)))))) ((head . tail) (if (< running max-processes) - (begin + (let ((running (+ 1 running))) (call-with-process (cut proc head)) - (loop tail (+ running 1))) - (begin + (progress total completed + (lambda () + (loop tail running completed)))) + (let ((running (- running 1)) + (completed (+ completed 1))) (wait-for-one-process) - (loop lst (- running 1)))))))) + (progress total completed + (lambda () + (loop lst running completed))))))))) (define* (build-guix out tarball - #:key tar gzip gcrypt) - "Build and install Guix in directory OUT using source from TARBALL." + #:key tar gzip gcrypt + (debug-port (%make-void-port "w"))) + "Build and install Guix in directory OUT using source from TARBALL. Write +any debugging output to DEBUG-PORT." (setvbuf (current-output-port) _IOLBF) (setvbuf (current-error-port) _IOLBF) (setenv "PATH" (string-append tar "/bin:" gzip "/bin")) - (system* "tar" "xvf" tarball) + (format debug-port "extracting '~a'...~%" tarball) + (system* "tar" "xf" tarball) + (match (scandir "." (lambda (name) (and (not (member name '("." ".."))) (file-is-directory? name)))) @@ -92,11 +120,13 @@ (define* (build-guix out tarball (x (error "tarball did not produce a single source directory" x))) - (format #t "copying and compiling Guix to `~a'...~%" out) + (format #t "copying and compiling to '~a'...~%" out) ;; Copy everything under guix/ and gnu/ plus {guix,gnu}.scm. - (copy-recursively "guix" (string-append out "/guix")) - (copy-recursively "gnu" (string-append out "/gnu")) + (copy-recursively "guix" (string-append out "/guix") + #:log debug-port) + (copy-recursively "gnu" (string-append out "/gnu") + #:log debug-port) (copy-file "guix.scm" (string-append out "/guix.scm")) (copy-file "gnu.scm" (string-append out "/gnu.scm")) @@ -121,12 +151,12 @@ (define* (build-guix out tarball (p-for-each (lambda (file) (let ((go (string-append (string-drop-right file 4) ".go"))) - (format (current-error-port) - "compiling '~a'...~%" file) - (compile-file file - #:output-file go - #:opts - %auto-compilation-options))) + (format debug-port "~%compiling '~a'...~%" file) + (parameterize ((current-warning-port debug-port)) + (compile-file file + #:output-file go + #:opts + %auto-compilation-options)))) (filter (cut string-suffix? ".scm" <>) @@ -144,6 +174,7 @@ (define* (build-guix out tarball (delete-file (string-append out "/guix/config.scm")) (delete-file (string-append out "/guix/config.go")) + (newline) #t) ;;; pull.scm ends here diff --git a/guix/scripts/pull.scm b/guix/scripts/pull.scm index c2bf536e86..5dafb84f91 100644 --- a/guix/scripts/pull.scm +++ b/guix/scripts/pull.scm @@ -38,15 +38,21 @@ (define %snapshot-url "http://git.savannah.gnu.org/cgit/guix.git/snapshot/guix-master.tar.gz" ) -(define (unpack store tarball) +(define* (unpack store tarball #:key verbose?) "Return a derivation that unpacks TARBALL into STORE and compiles Scheme files." (define builder - '(begin + `(begin (use-modules (guix build pull)) (build-guix (assoc-ref %outputs "out") (assoc-ref %build-inputs "tarball") + + ;; XXX: This is not perfect, enabling VERBOSE? means + ;; building a different derivation. + #:debug-port (if ',verbose? + (current-error-port) + (%make-void-port "w")) #:tar (assoc-ref %build-inputs "tar") #:gzip (assoc-ref %build-inputs "gzip") #:gcrypt (assoc-ref %build-inputs "gcrypt")))) @@ -129,13 +135,10 @@ (define (parse-options) (package-derivation store (if (assoc-ref opts 'bootstrap?) %bootstrap-guile - (canonical-package guile-2.0)))) - (current-build-output-port - (if (assoc-ref opts 'verbose?) - (current-error-port) - (%make-void-port "w")))) + (canonical-package guile-2.0))))) (let* ((config-dir (config-directory)) - (source (unpack store tarball)) + (source (unpack store tarball + #:verbose? (assoc-ref opts 'verbose?))) (source-dir (derivation->output-path source))) (if (show-what-to-build store (list source)) (if (build-derivations store (list source)) -- cgit v1.2.3 From b53833b2ef36cf139f65193bec688396a734b0d0 Mon Sep 17 00:00:00 2001 From: Ludovic Courtès Date: Sat, 6 Sep 2014 15:45:32 +0200 Subject: gexp: Allow use of high-level objects in #:references-graphs. * guix/gexp.scm (lower-reference-graphs): New procedure. (gexp->derivation)[graphs-file-names]: New procedure. Use 'lower-reference-graphs', and augment #:inputs argument as a function of #:references-graphs. * doc/guix.texi (G-Expressions): Adjust 'gexp->derivation' documentation accordingly. * tests/gexp.scm ("gexp->derivation, store copy"): Remove reference to TWO in BUILD-DRV. Use TWO directly in #:references-graphs argument. ("gexp->derivation #:references-graphs"): New test. * gnu/system/vm.scm (qemu-image): Remove variable 'graph'; use INPUTS as the #:references-graphs argument to 'expression->derivation-in-linux-vm'. --- doc/guix.texi | 16 +++++++++++ gnu/system/vm.scm | 82 +++++++++++++++++++++++++++---------------------------- guix/gexp.scm | 51 ++++++++++++++++++++++++++++++++-- tests/gexp.scm | 52 +++++++++++++++++++++++++++++------ 4 files changed, 148 insertions(+), 53 deletions(-) (limited to 'guix') diff --git a/doc/guix.texi b/doc/guix.texi index 1f192bf0a7..e0251f5ffd 100644 --- a/doc/guix.texi +++ b/doc/guix.texi @@ -2278,6 +2278,22 @@ search path to be copied in the store, compiled, and made available in the load path during the execution of @var{exp}---e.g., @code{((guix build utils) (guix build gnu-build-system))}. +When @var{references-graphs} is true, it must be a list of tuples of one of the +following forms: + +@example +(@var{file-name} @var{package}) +(@var{file-name} @var{package} @var{output}) +(@var{file-name} @var{derivation}) +(@var{file-name} @var{derivation} @var{output}) +(@var{file-name} @var{store-item}) +@end example + +The right-hand-side of each element of @var{references-graphs} is automatically made +an input of the build process of @var{exp}. In the build environment, each +@var{file-name} contains the reference graph of the corresponding item, in a simple +text format. + The other arguments are as for @code{derivation} (@pxref{Derivations}). @end deffn diff --git a/gnu/system/vm.scm b/gnu/system/vm.scm index 624f2a680a..205bf2cb19 100644 --- a/gnu/system/vm.scm +++ b/gnu/system/vm.scm @@ -219,48 +219,46 @@ (define* (qemu-image #:key all of INPUTS into the image being built. When REGISTER-CLOSURES? is true, register INPUTS in the store database of the image so that Guix can be used in the image." - (mlet %store-monad - ((graph (sequence %store-monad (map input->name+output inputs)))) - (expression->derivation-in-linux-vm - name - #~(begin - (use-modules (gnu build vm) - (guix build utils)) - - (let ((inputs - '#$(append (list qemu parted grub e2fsprogs util-linux) - (map canonical-package - (list sed grep coreutils findutils gawk)) - (if register-closures? (list guix) '()))) - - ;; This variable is unused but allows us to add INPUTS-TO-COPY - ;; as inputs. - (to-register - '#$(map (match-lambda - ((name thing) thing) - ((name thing output) `(,thing ,output))) - inputs))) - - (set-path-environment-variable "PATH" '("bin" "sbin") inputs) - - (let ((graphs '#$(match inputs - (((names . _) ...) - names)))) - (initialize-hard-disk "/dev/vda" - #:system-directory #$os-derivation - #:grub.cfg #$grub-configuration - #:closures graphs - #:copy-closures? #$copy-inputs? - #:register-closures? #$register-closures? - #:disk-image-size #$disk-image-size - #:file-system-type #$file-system-type - #:file-system-label #$file-system-label) - (reboot)))) - #:system system - #:make-disk-image? #t - #:disk-image-size disk-image-size - #:disk-image-format disk-image-format - #:references-graphs graph))) + (expression->derivation-in-linux-vm + name + #~(begin + (use-modules (gnu build vm) + (guix build utils)) + + (let ((inputs + '#$(append (list qemu parted grub e2fsprogs util-linux) + (map canonical-package + (list sed grep coreutils findutils gawk)) + (if register-closures? (list guix) '()))) + + ;; This variable is unused but allows us to add INPUTS-TO-COPY + ;; as inputs. + (to-register + '#$(map (match-lambda + ((name thing) thing) + ((name thing output) `(,thing ,output))) + inputs))) + + (set-path-environment-variable "PATH" '("bin" "sbin") inputs) + + (let ((graphs '#$(match inputs + (((names . _) ...) + names)))) + (initialize-hard-disk "/dev/vda" + #:system-directory #$os-derivation + #:grub.cfg #$grub-configuration + #:closures graphs + #:copy-closures? #$copy-inputs? + #:register-closures? #$register-closures? + #:disk-image-size #$disk-image-size + #:file-system-type #$file-system-type + #:file-system-label #$file-system-label) + (reboot)))) + #:system system + #:make-disk-image? #t + #:disk-image-size disk-image-size + #:disk-image-format disk-image-format + #:references-graphs inputs)) ;;; diff --git a/guix/gexp.scm b/guix/gexp.scm index e31324e101..5401cbf96f 100644 --- a/guix/gexp.scm +++ b/guix/gexp.scm @@ -109,6 +109,17 @@ (define* (lower-inputs inputs (return input))) inputs)))) +(define* (lower-reference-graphs graphs #:key system target) + "Given GRAPHS, a list of (FILE-NAME INPUT ...) lists for use as a +#:reference-graphs argument, lower it such that each INPUT is replaced by the +corresponding derivation." + (match graphs + (((file-names . inputs) ...) + (mlet %store-monad ((inputs (lower-inputs inputs + #:system system + #:target target))) + (return (map cons file-names inputs)))))) + (define* (gexp->derivation name exp #:key system (target 'current) @@ -127,10 +138,38 @@ (define* (gexp->derivation name exp compiled, and made available in the load path during the execution of EXP---e.g., '((guix build utils) (guix build gnu-build-system)). +When REFERENCES-GRAPHS is true, it must be a list of tuples of one of the +following forms: + + (FILE-NAME PACKAGE) + (FILE-NAME PACKAGE OUTPUT) + (FILE-NAME DERIVATION) + (FILE-NAME DERIVATION OUTPUT) + (FILE-NAME STORE-ITEM) + +The right-hand-side of each element of REFERENCES-GRAPHS is automatically made +an input of the build process of EXP. In the build environment, each +FILE-NAME contains the reference graph of the corresponding item, in a simple +text format. + +In that case, the reference graph of each store path is exported in +the build environment in the corresponding file, in a simple text format. + The other arguments are as for 'derivation'." (define %modules modules) (define outputs (gexp-outputs exp)) + (define (graphs-file-names graphs) + ;; Return a list of (FILE-NAME . STORE-PATH) pairs made from GRAPHS. + (map (match-lambda + ((file-name (? derivation? drv)) + (cons file-name (derivation->output-path drv))) + ((file-name (? derivation? drv) sub-drv) + (cons file-name (derivation->output-path drv sub-drv))) + ((file-name thing) + (cons file-name thing))) + graphs)) + (mlet* %store-monad (;; The following binding is here to force ;; '%current-system' and '%current-target-system' to be ;; looked up at >>= time. @@ -162,6 +201,11 @@ (define outputs (gexp-outputs exp)) #:system system #:guile guile-for-build) (return #f))) + (graphs (if references-graphs + (lower-reference-graphs references-graphs + #:system system + #:target target) + (return #f))) (guile (if guile-for-build (return guile-for-build) (package->derivation (default-guile) @@ -182,9 +226,12 @@ (define outputs (gexp-outputs exp)) (,builder) ,@(if modules `((,modules) (,compiled) ,@inputs) - inputs)) + inputs) + ,@(match graphs + (((_ . inputs) ...) inputs) + (_ '()))) #:hash hash #:hash-algo hash-algo #:recursive? recursive? - #:references-graphs references-graphs + #:references-graphs (and=> graphs graphs-file-names) #:local-build? local-build?))) (define* (gexp-inputs exp #:optional (references gexp-references)) diff --git a/tests/gexp.scm b/tests/gexp.scm index a08164c484..ea4df48403 100644 --- a/tests/gexp.scm +++ b/tests/gexp.scm @@ -335,19 +335,16 @@ (define (match-input thing) (call-with-output-file (string-append #$output "/two") (lambda (port) (display "This is the second one." port)))))) - (build-drv (lambda (two) - #~(begin - (use-modules (guix build store-copy)) + (build-drv #~(begin + (use-modules (guix build store-copy)) - (mkdir #$output) - '#$two ;make it an input - (populate-store '("graph") #$output))))) + (mkdir #$output) + (populate-store '("graph") #$output)))) (mlet* %store-monad ((one (gexp->derivation "one" build-one)) (two (gexp->derivation "two" (build-two one))) - (dir -> (derivation->output-path two)) - (drv (gexp->derivation "store-copy" (build-drv two) + (drv (gexp->derivation "store-copy" build-drv #:references-graphs - `(("graph" . ,dir)) + `(("graph" ,two)) #:modules '((guix build store-copy) (guix build utils)))) @@ -362,6 +359,43 @@ (define (match-input thing) (string=? (readlink (string-append out "/" two "/one")) one))))))) +(test-assertm "gexp->derivation #:references-graphs" + (mlet* %store-monad + ((one (text-file "one" "hello, world")) + (two (gexp->derivation "two" + #~(symlink #$one #$output:chbouib))) + (drv (gexp->derivation "ref-graphs" + #~(begin + (use-modules (guix build store-copy)) + (with-output-to-file #$output + (lambda () + (write (call-with-input-file "guile" + read-reference-graph)))) + (with-output-to-file #$output:one + (lambda () + (write (call-with-input-file "one" + read-reference-graph)))) + (with-output-to-file #$output:two + (lambda () + (write (call-with-input-file "two" + read-reference-graph))))) + #:references-graphs `(("one" ,one) + ("two" ,two "chbouib") + ("guile" ,%bootstrap-guile)) + #:modules '((guix build store-copy) + (guix build utils)))) + (ok? (built-derivations (list drv))) + (guile-drv (package->derivation %bootstrap-guile)) + (g-one -> (derivation->output-path drv "one")) + (g-two -> (derivation->output-path drv "two")) + (g-guile -> (derivation->output-path drv))) + (return (and ok? + (equal? (call-with-input-file g-one read) (list one)) + (equal? (call-with-input-file g-two read) + (list one (derivation->output-path two "chbouib"))) + (equal? (call-with-input-file g-guile read) + (list (derivation->output-path guile-drv))))))) + (define shebang (string-append "#!" (derivation->output-path (%guile-for-build)) "/bin/guile --no-auto-compile")) -- cgit v1.2.3 From 4a4cbd0bdd2ad8c4f37c3ffdd69596ef1ef41d91 Mon Sep 17 00:00:00 2001 From: Ludovic Courtès Date: Sun, 7 Sep 2014 23:57:36 +0200 Subject: gexp: 'gexp->script' returns a script that can easily be compiled. * guix/gexp.scm (gexp->script): Produce an 'eval-when' form around assignments of %load-path and %load-compiled-path. --- guix/gexp.scm | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) (limited to 'guix') diff --git a/guix/gexp.scm b/guix/gexp.scm index 5401cbf96f..ff80e305db 100644 --- a/guix/gexp.scm +++ b/guix/gexp.scm @@ -496,14 +496,16 @@ (define* (gexp->script name exp (format port "#!~a/bin/guile --no-auto-compile~%!#~%" (ungexp guile)) + + ;; Write the 'eval-when' form so that it can be + ;; compiled. (write - '(set! %load-path - (cons (ungexp modules) %load-path)) - port) - (write - '(set! %load-compiled-path - (cons (ungexp compiled) - %load-compiled-path)) + '(eval-when (expand load eval) + (set! %load-path + (cons (ungexp modules) %load-path)) + (set! %load-compiled-path + (cons (ungexp compiled) + %load-compiled-path))) port) (write '(ungexp exp) port) (chmod port #o555))))))) -- cgit v1.2.3