;;; GNU Guix --- Functional package management for GNU ;;; Copyright © 2015-2023 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 (test-graph) #:use-module (guix tests) #:use-module (guix graph) #:use-module (guix scripts graph) #:use-module (guix packages) #:use-module (guix derivations) #:use-module (guix store) #:use-module (guix monads) #:use-module (guix build-
aboutsummaryrefslogtreecommitdiff
blob: c65b5aacfa0778f48548a0c551e020049a975a0b (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2013, 2014, 2015 Ludovic Courtès <ludo@gnu.org>
;;;
;;; 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 <http://www.gnu.org/licenses/>.

(define-module (gnu build linux-initrd)
  #:use-module ((guix cpio) #:prefix cpio:)
  #:use-module (guix build utils)
  #:use-module (guix build store-copy)
  #:use-module (system base compile)
  #:use-module (rnrs bytevectors)
  #:use-module ((system foreign) #:select (sizeof))
  #:use-module (ice-9 ftw)
  #:export (write-cpio-archive
            build-initrd))

;;; Commentary:
;;;
;;; Tools to create Linux initial RAM disks ("initrds").  Initrds are
;;; essentially gzipped cpio archives, with a '/init' executable that the
;;; kernel runs at boot time.
;;;
;;; Code:

(define* (write-cpio-archive output directory
                             #:key
                             (compress? #t)
                             (gzip "gzip"))
  "Write a cpio archive containing DIRECTORY to file OUTPUT.  When
COMPRESS? is true, compress it using GZIP.  On success, return OUTPUT."

  ;; Note: as per `ramfs-rootfs-initramfs.txt', always add directory entries
  ;; before the files that are inside of it: "The Linux kernel cpio
  ;; extractor won't create files in a directory that doesn't exist, so the
  ;; directory entries must go before the files that go in those
  ;; directories."

  (define files
    ;; Use 'sort' so that (1) the order of files is deterministic, and (2)
    ;; directories appear before the files they contain.
    (sort (file-system-fold (const #t)                 ;enter?
                            (lambda (file stat result) ;leaf
                              (cons file result))
                            (lambda (dir stat result)  ;down
                              (if (string=? dir directory)
                                  result
                                  (cons dir result)))
                            (lambda (file stat result)
                              result)
                            (const #f)                 ;skip
                            (const #f)                 ;error
                            '()
                            directory)
          string<?))

  (call-with-output-file output
    (lambda (port)
      (cpio:write-cpio-archive files port
                               #:file->header cpio:file->cpio-header*)))

  (or (not compress?)
      ;; Use '--no-name' so that gzip records neither a file name nor a time
      ;; stamp in its output.
      (and (zero? (system* gzip "--best" "--no-name" output))
           (rename-file (string-append output ".gz")
                        output))
      output))

(define (cache-compiled-file-name file)
  "Return the file name of the in-cache .go file for FILE, relative to the
current directory.

This is similar to what 'compiled-file-name' in (system base compile) does."
  (let loop ((file file))
    (let ((target (false-if-exception (readlink file))))
     (if target
         (loop target)
         (format #f ".cache/guile/ccache/~a-~a-~a-~a/~a"
                 (effective-version)
                 (if (eq? (native-endianness) (endianness little))
                     "LE"
                     "BE")
                 (sizeof '*)
                 (effective-version)
                 file)))))

(define (compile-to-cache file)
  "Compile FILE to the cache."
  (let ((compiled-file (cache-compiled-file-name file)))
    (mkdir-p (dirname compiled-file))
    (compile-file file
                  #:opts %auto-compilation-options
                  #:output-file compiled-file)))

(define* (build-initrd output
                       #:key
                       guile init
                       (references-graphs '())
                       (gzip "gzip"))
  "Write an initial RAM disk (initrd) to OUTPUT.  The initrd starts the script
at INIT, running GUILE.  It contains all the items referred to by
REFERENCES-GRAPHS."
  (mkdir "contents")

  ;; Copy the closures of all the items referenced in REFERENCES-GRAPHS.
  (populate-store references-graphs "contents")

  (with-directory-excursion "contents"
    ;; Make '/init'.
    (symlink init "init")

    ;; Compile it.
    (compile-to-cache "init")

    ;; Allow Guile to find out where it is (XXX).  See
    ;; 'guile-relocatable.patch'.
    (mkdir-p "proc/self")
    (symlink (string-append guile "/bin/guile") "proc/self/exe")
    (readlink "proc/self/exe")

    ;; Reset the timestamps of all the files that will make it in the initrd.
    (for-each (lambda (file)
                (unless (eq? 'symlink (stat:type (lstat file)))
                  (utime file 0 0 0 0)))
              (find-files "." ".*"))

    (write-cpio-archive output "." #:gzip gzip))

  (delete-file-recursively "contents"))

;;; linux-initrd.scm ends here
(derivation-file-name drv)) (basename txt)))))))))))) (test-assert "reference DAG" (let-values (((backend nodes+edges) (make-recording-backend))) (run-with-store %store (mlet* %store-monad ((txt (text-file "text-file" "Hello!")) (guile (package->derivation %bootstrap-guile)) (drv (gexp->derivation "output" #~(symlink #$txt #$output) #:guile-for-build guile)) (out -> (derivation->output-path drv))) ;; We should see only OUT and TXT, with an edge from the former to the ;; latter. (mbegin %store-monad (built-derivations (list drv)) (export-graph (list (derivation->output-path drv)) 'port #:node-type %reference-node-type #:backend backend) (let-values (((nodes edges) (nodes+edges))) (return (and (equal? (match nodes (((ids labels) ...) ids)) (list out txt)) (equal? edges `((,out ,txt))))))))))) (test-assert "referrer DAG" (let-values (((backend nodes+edges) (make-recording-backend))) (run-with-store %store (mlet* %store-monad ((txt (text-file "referrer-node" (random-text))) (drv (gexp->derivation "referrer" #~(symlink #$txt #$output))) (out -> (derivation->output-path drv))) ;; We should see only TXT and OUT, with an edge from the former to the ;; latter. (mbegin %store-monad (built-derivations (list drv)) (export-graph (list txt) 'port #:node-type %referrer-node-type #:backend backend) (let-values (((nodes edges) (nodes+edges))) (return (and (equal? (match nodes (((ids labels) ...) ids)) (list txt out)) (equal? edges `((,txt ,out))))))))))) (test-assert "module graph" (let-values (((backend nodes+edges) (make-recording-backend))) (run-with-store %store (export-graph '((gnu packages guile)) 'port #:node-type %module-node-type #:backend backend)) (let-values (((nodes edges) (nodes+edges))) (and (member '(gnu packages guile) (match nodes (((ids labels) ...) ids))) (->bool (and (member (list '(gnu packages guile) '(gnu packages libunistring)) edges) (member (list '(gnu packages guile) '(gnu packages bdw-gc)) edges))))))) (test-assert "node-edges" (run-with-store %store (let ((packages (fold-packages cons '()))) (mlet %store-monad ((edges (node-edges %package-node-type packages))) (return (and (null? (edges hello)) (lset= eq? (edges guile-2.0) (match (package-direct-inputs guile-2.0) (((labels packages _ ...) ...) packages))))))))) (test-equal "node-transitive-edges + node-back-edges" '() (run-with-store %store (let ((packages (fold-packages cons '())) (bootstrap? (lambda (package) (string-contains (location-file (package-location package)) "bootstrap.scm"))) (trivial? (lambda (package) (eq? (package-build-system package) trivial-build-system))) (system-specific? (lambda (package) (memq #:system (package-arguments package))))) (mlet %store-monad ((edges (node-back-edges %bag-node-type packages))) (let* ((glibc (canonical-package glibc)) (dependents (node-transitive-edges (list glibc) edges)) (diff (lset-difference eq? packages dependents))) ;; All the packages depend on libc, except bootstrap packages, some ;; packages that use TRIVIAL-BUILD-SYSTEM, and some that target a ;; specific system and thus may depend on a different libc package ;; object. (return (remove (lambda (package) (or (trivial? package) (bootstrap? package) (system-specific? package))) diff))))))) (test-assert "node-transitive-edges, no duplicates" (run-with-store %store (let* ((p0 (dummy-package "p0")) (p1a (dummy-package "p1a" (inputs `(("p0" ,p0))))) (p1b (dummy-package "p1b" (inputs `(("p0" ,p0))))) (p2 (dummy-package "p2" (inputs `(("p1a" ,p1a) ("p1b" ,p1b)))))) (mlet %store-monad ((edges (node-edges %package-node-type (list p2 p1a p1b p0)))) (return (lset= eq? (node-transitive-edges (list p2) edges) (list p1a p1b p0))))))) (test-assert "node-transitive-edges, references" (run-with-store %store (mlet* %store-monad ((d0 (package->derivation %bootstrap-guile)) (d1 (gexp->derivation "d1" #~(begin (mkdir #$output) (symlink #$%bootstrap-guile (string-append #$output "/l"))))) (d2 (gexp->derivation "d2" #~(begin (mkdir #$output) (symlink #$d1 (string-append #$output "/l"))))) (_ (built-derivations (list d2))) (->node -> (node-type-convert %reference-node-type)) (o2 (->node (derivation->output-path d2))) (o1 (->node (derivation->output-path d1))) (o0 (->node (derivation->output-path d0))) (edges (node-edges %reference-node-type (append o0 o1 o2))) (reqs ((store-lift requisites) o2))) (return (lset= string=? (append o2 (node-transitive-edges o2 edges)) reqs))))) (test-equal "node-reachable-count" '(3 3) (run-with-store %store (let* ((p0 (dummy-package "p0")) (p1a (dummy-package "p1a" (inputs `(("p0" ,p0))))) (p1b (dummy-package "p1b" (inputs `(("p0" ,p0))))) (p2 (dummy-package "p2" (inputs `(("p1a" ,p1a) ("p1b" ,p1b)))))) (mlet* %store-monad ((all -> (list p2 p1a p1b p0)) (edges (node-edges %package-node-type all)) (back (node-back-edges %package-node-type all))) (return (list (node-reachable-count (list p2) edges) (node-reachable-count (list p0) back))))))) (test-equal "shortest-path, packages + derivations" '(("p5" "p4" "p1" "p0") ("p3" "p2" "p1" "p0") #f ("p5-0.drv" "p4-0.drv" "p1-0.drv" "p0-0.drv")) (run-with-store %store (let* ((p0 (dummy-package "p0")) (p1 (dummy-package "p1" (inputs `(("p0" ,p0))))) (p2 (dummy-package "p2" (inputs `(("p1" ,p1))))) (p3 (dummy-package "p3" (inputs `(("p2" ,p2))))) (p4 (dummy-package "p4" (inputs `(("p1" ,p1))))) (p5 (dummy-package "p5" (inputs `(("p4" ,p4) ("p3" ,p3)))))) (mlet* %store-monad ((path1 (shortest-path p5 p0 %package-node-type)) (path2 (shortest-path p3 p0 %package-node-type)) (nope (shortest-path p3 p4 %package-node-type)) (drv5 (package->derivation p5)) (drv0 (package->derivation p0)) (path3 (shortest-path drv5 drv0 %derivation-node-type))) (return (append (map (lambda (path) (and path (map package-name path))) (list path1 path2 nope)) (list (map (node-type-label %derivation-node-type) path3)))))))) (test-equal "shortest-path, reverse packages" '("libffi" "guile" "guile-json") (run-with-store %store (mlet %store-monad ((path (shortest-path (specification->package "libffi") guile-json-1 %reverse-package-node-type))) (return (map package-name path))))) (test-equal "shortest-path, references" `(("d2" "d1" ,(package-full-name %bootstrap-guile "-")) (,(package-full-name %bootstrap-guile "-") "d1" "d2")) (run-with-store %store (mlet* %store-monad ((d0 (package->derivation %bootstrap-guile)) (d1 (gexp->derivation "d1" #~(begin (mkdir #$output) (symlink #$%bootstrap-guile (string-append #$output "/l"))))) (d2 (gexp->derivation "d2" #~(begin (mkdir #$output) (symlink #$d1 (string-append #$output "/l"))))) (_ (built-derivations (list d2))) (->node -> (node-type-convert %reference-node-type)) (o2 (->node (derivation->output-path d2))) (o0 (->node (derivation->output-path d0))) (path (shortest-path (first o2) (first o0) %reference-node-type)) (rpath (shortest-path (first o0) (first o2) %referrer-node-type))) (return (list (map (node-type-label %reference-node-type) path) (map (node-type-label %referrer-node-type) rpath)))))) (test-end "graph")