linux-initrd.scm 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2013, 2014, 2015 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. When
  40. COMPRESS? is true, compress it using GZIP. On success, return OUTPUT."
  41. ;; Note: as per `ramfs-rootfs-initramfs.txt', always add directory entries
  42. ;; before the files that are inside of it: "The Linux kernel cpio
  43. ;; extractor won't create files in a directory that doesn't exist, so the
  44. ;; directory entries must go before the files that go in those
  45. ;; directories."
  46. (define files
  47. ;; Use 'sort' so that (1) the order of files is deterministic, and (2)
  48. ;; directories appear before the files they contain.
  49. (sort (file-system-fold (const #t) ;enter?
  50. (lambda (file stat result) ;leaf
  51. (cons file result))
  52. (lambda (dir stat result) ;down
  53. (if (string=? dir directory)
  54. result
  55. (cons dir result)))
  56. (lambda (file stat result)
  57. result)
  58. (const #f) ;skip
  59. (const #f) ;error
  60. '()
  61. directory)
  62. string<?))
  63. (call-with-output-file output
  64. (lambda (port)
  65. (cpio:write-cpio-archive files port
  66. #:file->header cpio:file->cpio-header*)))
  67. (or (not compress?)
  68. ;; Use '--no-name' so that gzip records neither a file name nor a time
  69. ;; stamp in its output.
  70. (and (zero? (system* gzip "--best" "--no-name" output))
  71. (rename-file (string-append output ".gz")
  72. output))
  73. output))
  74. (define (cache-compiled-file-name file)
  75. "Return the file name of the in-cache .go file for FILE, relative to the
  76. current directory.
  77. This is similar to what 'compiled-file-name' in (system base compile) does."
  78. (let loop ((file file))
  79. (let ((target (false-if-exception (readlink file))))
  80. (if target
  81. (loop target)
  82. (format #f ".cache/guile/ccache/~a-~a-~a-~a/~a"
  83. (effective-version)
  84. (if (eq? (native-endianness) (endianness little))
  85. "LE"
  86. "BE")
  87. (sizeof '*)
  88. (effective-version)
  89. file)))))
  90. (define (compile-to-cache file)
  91. "Compile FILE to the cache."
  92. (let ((compiled-file (cache-compiled-file-name file)))
  93. (mkdir-p (dirname compiled-file))
  94. (compile-file file
  95. #:opts %auto-compilation-options
  96. #:output-file compiled-file)))
  97. (define* (build-initrd output
  98. #:key
  99. guile init
  100. (references-graphs '())
  101. (gzip "gzip"))
  102. "Write an initial RAM disk (initrd) to OUTPUT. The initrd starts the script
  103. at INIT, running GUILE. It contains all the items referred to by
  104. REFERENCES-GRAPHS."
  105. (mkdir "contents")
  106. ;; Copy the closures of all the items referenced in REFERENCES-GRAPHS.
  107. (populate-store references-graphs "contents")
  108. (with-directory-excursion "contents"
  109. ;; Make '/init'.
  110. (symlink init "init")
  111. ;; Compile it.
  112. (compile-to-cache "init")
  113. ;; Allow Guile to find out where it is (XXX). See
  114. ;; 'guile-relocatable.patch'.
  115. (mkdir-p "proc/self")
  116. (symlink (string-append guile "/bin/guile") "proc/self/exe")
  117. (readlink "proc/self/exe")
  118. ;; Reset the timestamps of all the files that will make it in the initrd.
  119. (for-each (lambda (file)
  120. (unless (eq? 'symlink (stat:type (lstat file)))
  121. (utime file 0 0 0 0)))
  122. (find-files "." ".*"))
  123. (write-cpio-archive output "." #:gzip gzip))
  124. (delete-file-recursively "contents"))
  125. ;;; linux-initrd.scm ends here