1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2013, 2014, 2015, 2016 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2016 Chris Marusich <cmmarusich@gmail.com>
5 ;;; This file is part of GNU Guix.
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.
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.
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/>.
20 (define-module (gnu build install)
21 #:use-module (guix build utils)
22 #:use-module (guix build store-copy)
23 #:use-module (srfi srfi-26)
24 #:use-module (ice-9 match)
25 #:export (install-grub
27 populate-root-file-system
30 populate-single-profile-directory))
34 ;;; This module supports the installation of the GNU system on a hard disk.
35 ;;; It is meant to be used both in a build environment (in derivations that
36 ;;; build VM images), and on the bare metal (when really installing the
41 (define (install-grub grub.cfg device mount-point)
42 "Install GRUB with GRUB.CFG on DEVICE, which is assumed to be mounted on
45 Note that the caller must make sure that GRUB.CFG is registered as a GC root
46 so that the fonts, background images, etc. referred to by GRUB.CFG are not
48 (install-grub-config grub.cfg mount-point)
50 ;; Tell 'grub-install' that there might be a LUKS-encrypted /boot or root
52 (setenv "GRUB_ENABLE_CRYPTODISK" "y")
54 (unless (zero? (system* "grub-install" "--no-floppy"
56 (string-append mount-point "/boot")
58 (error "failed to install GRUB")))
60 (define (install-grub-config grub.cfg mount-point)
61 "Atomically copy GRUB.CFG into boot/grub/grub.cfg on the MOUNT-POINT. Note
62 that the caller must make sure that GRUB.CFG is registered as a GC root so
63 that the fonts, background images, etc. referred to by GRUB.CFG are not GC'd."
64 (let* ((target (string-append mount-point "/boot/grub/grub.cfg"))
65 (pivot (string-append target ".new")))
66 (mkdir-p (dirname target))
68 ;; Copy GRUB.CFG instead of just symlinking it, because symlinks won't
69 ;; work when /boot is on a separate partition. Do that atomically.
70 (copy-file grub.cfg pivot)
71 (rename-file pivot target)))
73 (define (evaluate-populate-directive directive target)
74 "Evaluate DIRECTIVE, an sexp describing a file or directory to create under
76 (let loop ((directive directive))
81 (mkdir-p (string-append target name)))
82 (('directory name uid gid)
83 (let ((dir (string-append target name)))
86 (('directory name uid gid mode)
87 (loop `(directory ,name ,uid ,gid))
88 (chmod (string-append target name) mode))
93 (symlink old (string-append target new)))
95 ;; When doing 'guix system init' on the current '/', some
96 ;; symlinks may already exists. Override them.
97 (if (= EEXIST (system-error-errno args))
99 (delete-file (string-append target new))
101 (apply throw args))))))))
103 ;; Usually we can only get here when installing to an existing root,
104 ;; as with 'guix system init foo.scm /'.
105 (format (current-error-port)
106 "error: failed to evaluate directive: ~s~%"
108 (apply throw args)))))
110 (define (directives store)
111 "Return a list of directives to populate the root file system that will host
113 `(;; Note: the store's GID is fixed precisely so we can set it here rather
114 ;; than at activation time.
115 (directory ,store 0 30000 #o1775)
118 (directory "/var/log") ; for shepherd
119 (directory "/var/guix/gcroots")
120 (directory "/var/empty") ; for no-login accounts
121 (directory "/var/db") ; for dhclient, etc.
122 (directory "/var/run")
125 (directory "/var/guix/profiles/per-user/root" 0 0)
127 ;; Link to the initial system generation.
128 ("/var/guix/profiles/system" -> "system-1-link")
130 ("/var/guix/gcroots/booted-system" -> "/run/booted-system")
131 ("/var/guix/gcroots/current-system" -> "/run/current-system")
133 ;; XXX: 'guix-register' creates this symlink with a wrong target, so
134 ;; create it upfront to be sure.
135 ("/var/guix/gcroots/profiles" -> "/var/guix/profiles")
138 (directory "/tmp" 0 0 #o1777) ; sticky bit
139 (directory "/var/tmp" 0 0 #o1777)
140 (directory "/var/lock" 0 0 #o1777)
142 (directory "/root" 0 0) ; an exception
143 (directory "/home" 0 0)))
145 (define (populate-root-file-system system target)
146 "Make the essential non-store files and directories on TARGET. This
147 includes /etc, /var, /run, /bin/sh, etc., and all the symlinks to SYSTEM."
148 (for-each (cut evaluate-populate-directive <> target)
149 (directives (%store-directory)))
151 ;; Add system generation 1.
152 (let ((generation-1 (string-append target
153 "/var/guix/profiles/system-1-link")))
157 (symlink system generation-1))
159 ;; If GENERATION-1 already exists, overwrite it.
160 (if (= EEXIST (system-error-errno args))
162 (delete-file generation-1)
164 (apply throw args)))))))
166 (define (reset-timestamps directory)
167 "Reset the timestamps of all the files under DIRECTORY, so that they appear
168 as created and modified at the Epoch."
169 (display "clearing file timestamps...\n")
170 (for-each (lambda (file)
171 (let ((s (lstat file)))
172 ;; XXX: Guile uses libc's 'utime' function (not 'futime'), so
173 ;; the timestamp of symlinks cannot be changed, and there are
174 ;; symlinks here pointing to /gnu/store, which is the host,
176 (unless (eq? (stat:type s) 'symlink)
177 (utime file 0 0 0 0))))
178 (find-files directory #:directories? #t)))
180 (define* (register-closure store closure
181 #:key (deduplicate? #t))
182 "Register CLOSURE in STORE, where STORE is the directory name of the target
183 store and CLOSURE is the name of a file containing a reference graph as used
184 by 'guix-register'. As a side effect, this resets timestamps on store files
185 and, if DEDUPLICATE? is true, deduplicates files common to CLOSURE and the
187 (let ((status (apply system* "guix-register" "--prefix" store
188 (append (if deduplicate? '() '("--no-deduplication"))
190 (unless (zero? status)
191 (error "failed to register store items" closure))))
193 (define* (populate-single-profile-directory directory
194 #:key profile closure
196 "Populate DIRECTORY with a store containing PROFILE, whose closure is given
197 in the file called CLOSURE (as generated by #:references-graphs.) DIRECTORY
198 is initialized to contain a single profile under /root pointing to PROFILE.
199 DEDUPLICATE? determines whether to deduplicate files in the store.
201 This is used to create the self-contained Guix tarball."
203 (string-append directory "/" file))
205 (define %root-profile
206 "/var/guix/profiles/per-user/root")
208 (define (mkdir-p* dir)
209 (mkdir-p (scope dir)))
211 (define (symlink* old new)
212 (symlink old (scope new)))
214 ;; Populate the store.
215 (populate-store (list closure) directory)
216 (register-closure (canonicalize-path directory) closure
217 #:deduplicate? deduplicate?)
219 ;; XXX: 'guix-register' registers profiles as GC roots but the symlink
220 ;; target uses $TMPDIR. Fix that.
221 (delete-file (scope "/var/guix/gcroots/profiles"))
222 (symlink* "/var/guix/profiles"
223 "/var/guix/gcroots/profiles")
225 ;; Make root's profile, which makes it a GC root.
226 (mkdir-p* %root-profile)
228 (string-append %root-profile "/guix-profile-1-link"))
229 (symlink* (string-append %root-profile "/guix-profile-1-link")
230 (string-append %root-profile "/guix-profile"))
233 (symlink* (string-append %root-profile "/guix-profile")
234 "/root/.guix-profile"))
236 ;;; install.scm ends here