1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2013, 2014, 2015 Ludovic Courtès <ludo@gnu.org>
4 ;;; This file is part of GNU Guix.
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.
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.
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/>.
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-grub
25 populate-root-file-system
28 populate-single-profile-directory))
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
39 (define* (install-grub grub.cfg device mount-point)
40 "Install GRUB with GRUB.CFG on DEVICE, which is assumed to be mounted on
43 Note that the caller must make sure that GRUB.CFG is registered as a GC root
44 so that the fonts, background images, etc. referred to by GRUB.CFG are not
46 (let* ((target (string-append mount-point "/boot/grub/grub.cfg"))
47 (pivot (string-append target ".new")))
48 (mkdir-p (dirname target))
50 ;; Copy GRUB.CFG instead of just symlinking it, because symlinks won't
51 ;; work when /boot is on a separate partition. Do that atomically.
52 (copy-file grub.cfg pivot)
53 (rename-file pivot target)
55 (unless (zero? (system* "grub-install" "--no-floppy"
57 (string-append mount-point "/boot")
59 (error "failed to install GRUB"))))
61 (define (evaluate-populate-directive directive target)
62 "Evaluate DIRECTIVE, an sexp describing a file or directory to create under
64 (let loop ((directive directive))
69 (mkdir-p (string-append target name)))
70 (('directory name uid gid)
71 (let ((dir (string-append target name)))
74 (('directory name uid gid mode)
75 (loop `(directory ,name ,uid ,gid))
76 (chmod (string-append target name) mode))
81 (symlink old (string-append target new)))
83 ;; When doing 'guix system init' on the current '/', some
84 ;; symlinks may already exists. Override them.
85 (if (= EEXIST (system-error-errno args))
87 (delete-file (string-append target new))
89 (apply throw args))))))))
91 ;; Usually we can only get here when installing to an existing root,
92 ;; as with 'guix system init foo.scm /'.
93 (format (current-error-port)
94 "error: failed to evaluate directive: ~s~%"
96 (apply throw args)))))
98 (define (directives store)
99 "Return a list of directives to populate the root file system that will host
101 `(;; Note: the store's GID is fixed precisely so we can set it here rather
102 ;; than at activation time.
103 (directory ,store 0 30000 #o1775)
106 (directory "/var/log") ; for dmd
107 (directory "/var/guix/gcroots")
108 (directory "/var/empty") ; for no-login accounts
109 (directory "/var/db") ; for dhclient, etc.
110 (directory "/var/run")
113 (directory "/var/guix/profiles/per-user/root" 0 0)
115 ;; Link to the initial system generation.
116 ("/var/guix/profiles/system" -> "system-1-link")
118 ("/var/guix/gcroots/booted-system" -> "/run/booted-system")
119 ("/var/guix/gcroots/current-system" -> "/run/current-system")
122 (directory "/tmp" 0 0 #o1777) ; sticky bit
123 (directory "/var/tmp" 0 0 #o1777)
124 (directory "/var/lock" 0 0 #o1777)
126 (directory "/root" 0 0) ; an exception
127 (directory "/home" 0 0)))
129 (define (populate-root-file-system system target)
130 "Make the essential non-store files and directories on TARGET. This
131 includes /etc, /var, /run, /bin/sh, etc., and all the symlinks to SYSTEM."
132 (for-each (cut evaluate-populate-directive <> target)
133 (directives (%store-directory)))
135 ;; Add system generation 1.
136 (let ((generation-1 (string-append target
137 "/var/guix/profiles/system-1-link")))
141 (symlink system generation-1))
143 ;; If GENERATION-1 already exists, overwrite it.
144 (if (= EEXIST (system-error-errno args))
146 (delete-file generation-1)
148 (apply throw args)))))))
150 (define (reset-timestamps directory)
151 "Reset the timestamps of all the files under DIRECTORY, so that they appear
152 as created and modified at the Epoch."
153 (display "clearing file timestamps...\n")
154 (for-each (lambda (file)
155 (let ((s (lstat file)))
156 ;; XXX: Guile uses libc's 'utime' function (not 'futime'), so
157 ;; the timestamp of symlinks cannot be changed, and there are
158 ;; symlinks here pointing to /gnu/store, which is the host,
160 (unless (eq? (stat:type s) 'symlink)
161 (utime file 0 0 0 0))))
162 (find-files directory "")))
164 (define (register-closure store closure)
165 "Register CLOSURE in STORE, where STORE is the directory name of the target
166 store and CLOSURE is the name of a file containing a reference graph as used
167 by 'guix-register'. As a side effect, this resets timestamps on store files."
168 (let ((status (system* "guix-register" "--prefix" store
170 (unless (zero? status)
171 (error "failed to register store items" closure))))
173 (define* (populate-single-profile-directory directory
174 #:key profile closure)
175 "Populate DIRECTORY with a store containing PROFILE, whose closure is given
176 in the file called CLOSURE (as generated by #:references-graphs.) DIRECTORY
177 is initialized to contain a single profile under /root pointing to PROFILE.
178 This is used to create the self-contained Guix tarball."
180 (string-append directory "/" file))
182 (define %root-profile
183 "/var/guix/profiles/per-user/root")
185 (define (mkdir-p* dir)
186 (mkdir-p (scope dir)))
188 (define (symlink* old new)
189 (symlink old (scope new)))
191 ;; Populate the store.
192 (populate-store (list closure) directory)
193 (register-closure (canonicalize-path directory) closure)
195 ;; XXX: 'guix-register' registers profiles as GC roots but the symlink
196 ;; target uses $TMPDIR. Fix that.
197 (delete-file (scope "/var/guix/gcroots/profiles"))
198 (symlink* "/var/guix/profiles"
199 "/var/guix/gcroots/profiles")
201 ;; Make root's profile, which makes it a GC root.
202 (mkdir-p* %root-profile)
204 (string-append %root-profile "/guix-profile-1-link"))
205 (symlink* (string-append %root-profile "/guix-profile-1-link")
206 (string-append %root-profile "/guix-profile"))
209 (symlink* (string-append %root-profile "/guix-profile")
210 "/root/.guix-profile"))
212 ;;; install.scm ends here