linux-initrd.scm 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2013, 2014, 2015, 2018, 2020 Ludovic Courtès <ludo@gnu.org>
  3. ;;;
  4. ;;; This file is part of GNU Guix.
  5. ;;;
  6. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  7. ;;; under the terms of the GNU General Public License as published by
  8. ;;; the Free Software Foundation; either version 3 of the License, or (at
  9. ;;; your option) any later version.
  10. ;;;
  11. ;;; GNU Guix is distributed in the hope that it will be useful, but
  12. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;;; GNU General Public License for more details.
  15. ;;;
  16. ;;; You should have received a copy of the GNU General Public License
  17. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  18. (define-module (gnu build linux-initrd)
  19. #:use-module ((guix cpio) #:prefix cpio:)
  20. #:use-module (guix build utils)
  21. #:use-module (guix build store-copy)
  22. #:use-module (system base compile)
  23. #:use-module (rnrs bytevectors)
  24. #:use-module ((system foreign) #:select (sizeof))
  25. #:use-module (ice-9 ftw)
  26. #:export (write-cpio-archive
  27. build-initrd))
  28. ;;; Commentary:
  29. ;;;
  30. ;;; Tools to create Linux initial RAM disks ("initrds"). Initrds are
  31. ;;; essentially gzipped cpio archives, with a '/init' executable that the
  32. ;;; kernel runs at boot time.
  33. ;;;
  34. ;;; Code:
  35. (define* (write-cpio-archive output directory
  36. #:key
  37. (compress? #t)
  38. (gzip "gzip"))
  39. "Write a cpio archive containing DIRECTORY to file OUTPUT, with reset
  40. timestamps in the archive. When COMPRESS? is true, compress it using GZIP.
  41. On success, return OUTPUT."
  42. ;; Note: as per `ramfs-rootfs-initramfs.txt', always add directory entries
  43. ;; before the files that are inside of it: "The Linux kernel cpio
  44. ;; extractor won't create files in a directory that doesn't exist, so the
  45. ;; directory entries must go before the files that go in those
  46. ;; directories."
  47. (define files
  48. ;; Use 'sort' so that (1) the order of files is deterministic, and (2)
  49. ;; directories appear before the files they contain.
  50. (sort (file-system-fold (const #t) ;enter?
  51. (lambda (file stat result) ;leaf
  52. (cons file result))
  53. (lambda (dir stat result) ;down
  54. (if (string=? dir directory)
  55. result
  56. (cons dir result)))
  57. (lambda (file stat result)
  58. result)
  59. (const #f) ;skip
  60. (const #f) ;error
  61. '()
  62. directory)
  63. string<?))
  64. (call-with-output-file output
  65. (lambda (port)
  66. (cpio:write-cpio-archive files port
  67. #:file->header cpio:file->cpio-header*)))
  68. (if compress?
  69. ;; Gzip insists on adding a '.gz' suffix and does nothing if the input
  70. ;; file already has that suffix. Shuffle files around to placate it.
  71. (let* ((gz-suffix? (string-suffix? ".gz" output))
  72. (sans-gz (if gz-suffix?
  73. (string-drop-right output 3)
  74. output)))
  75. (when gz-suffix?
  76. (rename-file output sans-gz))
  77. ;; Use '--no-name' so that gzip records neither a file name nor a time
  78. ;; stamp in its output.
  79. (and (zero? (system* gzip "--best" "--no-name" sans-gz))
  80. (begin
  81. (unless gz-suffix?
  82. (rename-file (string-append output ".gz") output))
  83. output)))
  84. output))
  85. (define (cache-compiled-file-name file)
  86. "Return the file name of the in-cache .go file for FILE, relative to the
  87. current directory.
  88. This is similar to what 'compiled-file-name' in (system base compile) does."
  89. (let loop ((file file))
  90. (let ((target (false-if-exception (readlink file))))
  91. (if target
  92. (loop target)
  93. (format #f ".cache/guile/ccache/~a-~a-~a-~a/~a"
  94. (effective-version)
  95. (if (eq? (native-endianness) (endianness little))
  96. "LE"
  97. "BE")
  98. (sizeof '*)
  99. (effective-version)
  100. file)))))
  101. (define (compile-to-cache file)
  102. "Compile FILE to the cache."
  103. (let ((compiled-file (cache-compiled-file-name file)))
  104. (mkdir-p (dirname compiled-file))
  105. (compile-file file
  106. #:opts %auto-compilation-options
  107. #:output-file compiled-file)))
  108. (define* (build-initrd output
  109. #:key
  110. guile init
  111. (references-graphs '())
  112. (gzip "gzip"))
  113. "Write an initial RAM disk (initrd) to OUTPUT. The initrd starts the script
  114. at INIT, running GUILE. It contains all the items referred to by
  115. REFERENCES-GRAPHS."
  116. (mkdir "contents")
  117. ;; Copy the closures of all the items referenced in REFERENCES-GRAPHS.
  118. (populate-store references-graphs "contents"
  119. #:deduplicate? #f)
  120. (with-directory-excursion "contents"
  121. ;; Make '/init'.
  122. (symlink init "init")
  123. ;; Compile it.
  124. (compile-to-cache "init")
  125. ;; Allow Guile to find out where it is (XXX). See
  126. ;; 'guile-relocatable.patch'.
  127. (mkdir-p "proc/self")
  128. (symlink (string-append guile "/bin/guile") "proc/self/exe")
  129. (readlink "proc/self/exe")
  130. (write-cpio-archive output "." #:gzip gzip))
  131. ;; Make sure directories are writable so we can delete files.
  132. (for-each make-file-writable
  133. (find-files "contents"
  134. (lambda (file stat)
  135. (eq? 'directory (stat:type stat)))
  136. #:directories? #t))
  137. (delete-file-recursively "contents"))
  138. ;;; linux-initrd.scm ends here