install.scm 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2013, 2014, 2015, 2016, 2017 Ludovic Courtès <ludo@gnu.org>
  3. ;;; Copyright © 2016 Chris Marusich <cmmarusich@gmail.com>
  4. ;;;
  5. ;;; This file is part of GNU Guix.
  6. ;;;
  7. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  8. ;;; under the terms of the GNU General Public License as published by
  9. ;;; the Free Software Foundation; either version 3 of the License, or (at
  10. ;;; your option) any later version.
  11. ;;;
  12. ;;; GNU Guix is distributed in the hope that it will be useful, but
  13. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;;; GNU General Public License for more details.
  16. ;;;
  17. ;;; You should have received a copy of the GNU General Public License
  18. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  19. (define-module (gnu build install)
  20. #:use-module (guix build utils)
  21. #:use-module (guix build store-copy)
  22. #:use-module (srfi srfi-26)
  23. #:use-module (ice-9 match)
  24. #:export (install-boot-config
  25. evaluate-populate-directive
  26. populate-root-file-system
  27. reset-timestamps
  28. register-closure
  29. populate-single-profile-directory))
  30. ;;; Commentary:
  31. ;;;
  32. ;;; This module supports the installation of the GNU system on a hard disk.
  33. ;;; It is meant to be used both in a build environment (in derivations that
  34. ;;; build VM images), and on the bare metal (when really installing the
  35. ;;; system.)
  36. ;;;
  37. ;;; Code:
  38. (define (install-boot-config bootcfg bootcfg-location mount-point)
  39. "Atomically copy BOOTCFG into BOOTCFG-LOCATION on the MOUNT-POINT. Note
  40. that the caller must make sure that BOOTCFG is registered as a GC root so
  41. that the fonts, background images, etc. referred to by BOOTCFG are not GC'd."
  42. (let* ((target (string-append mount-point bootcfg-location))
  43. (pivot (string-append target ".new")))
  44. (mkdir-p (dirname target))
  45. ;; Copy BOOTCFG instead of just symlinking it, because symlinks won't
  46. ;; work when /boot is on a separate partition. Do that atomically.
  47. (copy-file bootcfg pivot)
  48. (rename-file pivot target)))
  49. (define (evaluate-populate-directive directive target)
  50. "Evaluate DIRECTIVE, an sexp describing a file or directory to create under
  51. directory TARGET."
  52. (let loop ((directive directive))
  53. (catch 'system-error
  54. (lambda ()
  55. (match directive
  56. (('directory name)
  57. (mkdir-p (string-append target name)))
  58. (('directory name uid gid)
  59. (let ((dir (string-append target name)))
  60. (mkdir-p dir)
  61. (chown dir uid gid)))
  62. (('directory name uid gid mode)
  63. (loop `(directory ,name ,uid ,gid))
  64. (chmod (string-append target name) mode))
  65. ((new '-> old)
  66. (let try ()
  67. (catch 'system-error
  68. (lambda ()
  69. (symlink old (string-append target new)))
  70. (lambda args
  71. ;; When doing 'guix system init' on the current '/', some
  72. ;; symlinks may already exists. Override them.
  73. (if (= EEXIST (system-error-errno args))
  74. (begin
  75. (delete-file (string-append target new))
  76. (try))
  77. (apply throw args))))))))
  78. (lambda args
  79. ;; Usually we can only get here when installing to an existing root,
  80. ;; as with 'guix system init foo.scm /'.
  81. (format (current-error-port)
  82. "error: failed to evaluate directive: ~s~%"
  83. directive)
  84. (apply throw args)))))
  85. (define (directives store)
  86. "Return a list of directives to populate the root file system that will host
  87. STORE."
  88. `(;; Note: the store's GID is fixed precisely so we can set it here rather
  89. ;; than at activation time.
  90. (directory ,store 0 30000 #o1775)
  91. (directory "/etc")
  92. (directory "/var/log") ; for shepherd
  93. (directory "/var/guix/gcroots")
  94. (directory "/var/empty") ; for no-login accounts
  95. (directory "/var/db") ; for dhclient, etc.
  96. (directory "/var/run")
  97. (directory "/run")
  98. (directory "/mnt")
  99. (directory "/var/guix/profiles/per-user/root" 0 0)
  100. ;; Link to the initial system generation.
  101. ("/var/guix/profiles/system" -> "system-1-link")
  102. ("/var/guix/gcroots/booted-system" -> "/run/booted-system")
  103. ("/var/guix/gcroots/current-system" -> "/run/current-system")
  104. ;; XXX: 'guix-register' creates this symlink with a wrong target, so
  105. ;; create it upfront to be sure.
  106. ("/var/guix/gcroots/profiles" -> "/var/guix/profiles")
  107. (directory "/bin")
  108. (directory "/tmp" 0 0 #o1777) ; sticky bit
  109. (directory "/var/tmp" 0 0 #o1777)
  110. (directory "/var/lock" 0 0 #o1777)
  111. (directory "/root" 0 0) ; an exception
  112. (directory "/home" 0 0)))
  113. (define (populate-root-file-system system target)
  114. "Make the essential non-store files and directories on TARGET. This
  115. includes /etc, /var, /run, /bin/sh, etc., and all the symlinks to SYSTEM."
  116. (for-each (cut evaluate-populate-directive <> target)
  117. (directives (%store-directory)))
  118. ;; Add system generation 1.
  119. (let ((generation-1 (string-append target
  120. "/var/guix/profiles/system-1-link")))
  121. (let try ()
  122. (catch 'system-error
  123. (lambda ()
  124. (symlink system generation-1))
  125. (lambda args
  126. ;; If GENERATION-1 already exists, overwrite it.
  127. (if (= EEXIST (system-error-errno args))
  128. (begin
  129. (delete-file generation-1)
  130. (try))
  131. (apply throw args)))))))
  132. (define (reset-timestamps directory)
  133. "Reset the timestamps of all the files under DIRECTORY, so that they appear
  134. as created and modified at the Epoch."
  135. (display "clearing file timestamps...\n")
  136. (for-each (lambda (file)
  137. (let ((s (lstat file)))
  138. ;; XXX: Guile uses libc's 'utime' function (not 'futime'), so
  139. ;; the timestamp of symlinks cannot be changed, and there are
  140. ;; symlinks here pointing to /gnu/store, which is the host,
  141. ;; read-only store.
  142. (unless (eq? (stat:type s) 'symlink)
  143. (utime file 0 0 0 0))))
  144. (find-files directory #:directories? #t)))
  145. (define* (register-closure store closure
  146. #:key (deduplicate? #t))
  147. "Register CLOSURE in STORE, where STORE is the directory name of the target
  148. store and CLOSURE is the name of a file containing a reference graph as used
  149. by 'guix-register'. As a side effect, this resets timestamps on store files
  150. and, if DEDUPLICATE? is true, deduplicates files common to CLOSURE and the
  151. rest of STORE."
  152. (let ((status (apply system* "guix-register" "--prefix" store
  153. (append (if deduplicate? '() '("--no-deduplication"))
  154. (list closure)))))
  155. (unless (zero? status)
  156. (error "failed to register store items" closure))))
  157. (define* (populate-single-profile-directory directory
  158. #:key profile closure
  159. deduplicate?
  160. register?)
  161. "Populate DIRECTORY with a store containing PROFILE, whose closure is given
  162. in the file called CLOSURE (as generated by #:references-graphs.) DIRECTORY
  163. is initialized to contain a single profile under /root pointing to PROFILE.
  164. When REGISTER? is true, initialize DIRECTORY/var/guix/db to reflect the
  165. contents of the store; DEDUPLICATE? determines whether to deduplicate files in
  166. the store.
  167. This is used to create the self-contained tarballs with 'guix pack'."
  168. (define (scope file)
  169. (string-append directory "/" file))
  170. (define %root-profile
  171. "/var/guix/profiles/per-user/root")
  172. (define (mkdir-p* dir)
  173. (mkdir-p (scope dir)))
  174. (define (symlink* old new)
  175. (symlink old (scope new)))
  176. ;; Populate the store.
  177. (populate-store (list closure) directory)
  178. (when register?
  179. (register-closure (canonicalize-path directory) closure
  180. #:deduplicate? deduplicate?)
  181. ;; XXX: 'guix-register' registers profiles as GC roots but the symlink
  182. ;; target uses $TMPDIR. Fix that.
  183. (delete-file (scope "/var/guix/gcroots/profiles"))
  184. (symlink* "/var/guix/profiles"
  185. "/var/guix/gcroots/profiles"))
  186. ;; Make root's profile, which makes it a GC root.
  187. (mkdir-p* %root-profile)
  188. (symlink* profile
  189. (string-append %root-profile "/guix-profile-1-link"))
  190. (symlink* (string-append %root-profile "/guix-profile-1-link")
  191. (string-append %root-profile "/guix-profile"))
  192. (mkdir-p* "/root")
  193. (symlink* (string-append %root-profile "/guix-profile")
  194. "/root/.guix-profile"))
  195. ;;; install.scm ends here