1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2018, 2020 Mathieu Othacehe <m.othacehe@gmail.com>
3 ;;; Copyright © 2019, 2020 Ludovic Courtès <ludo@gnu.org>
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 installer final)
21 #:use-module (gnu installer newt page)
22 #:use-module (gnu installer steps)
23 #:use-module (gnu installer utils)
24 #:use-module (gnu installer user)
25 #:use-module (gnu services herd)
26 #:use-module (guix build syscalls)
27 #:use-module (guix build utils)
28 #:use-module (gnu build accounts)
29 #:use-module (gnu build install)
30 #:use-module (gnu build linux-container)
31 #:use-module ((gnu system shadow) #:prefix sys:)
32 #:use-module (rnrs io ports)
33 #:use-module (srfi srfi-1)
34 #:use-module (ice-9 ftw)
35 #:use-module (ice-9 popen)
36 #:use-module (ice-9 match)
37 #:use-module (ice-9 format)
38 #:use-module (ice-9 rdelim)
39 #:export (install-system))
43 (logxor (getpid) (car (gettimeofday)))))
45 (define (integer->alphanumeric-char n)
46 "Map N, an integer in the [0..62] range, to an alphanumeric character."
48 (integer->char (+ (char->integer #\0) n)))
50 (integer->char (+ (char->integer #\A) (- n 10))))
52 (integer->char (+ (char->integer #\a) (- n 36))))
54 (error "integer out of bounds" n))))
56 (define (random-string len)
57 "Compute a random string of size LEN where each character is alphanumeric."
58 (let loop ((chars '())
62 (let ((n (random 62 %seed)))
63 (loop (cons (integer->alphanumeric-char n) chars)
66 (define (create-user-database users root)
67 "Create /etc/passwd, /etc/shadow, and /etc/group under ROOT for the given
70 (string-append root "/etc"))
73 ;; "$6" gives us a SHA512 password hash; the random string must be taken
74 ;; from the './0-9A-Za-z' alphabet (info "(libc) Passphrase Storage").
75 (string-append "$6$" (random-string 10)))
80 (string=? "root" (user-name user)))
82 (sys:user-account (name (user-name user))
83 (comment (user-real-name user))
87 (user-home-directory user))
88 (password (crypt (user-password user)
91 ;; We need a string here, not a file-like, hence
94 "/run/current-system/profile/bin/bash")))
97 (define-values (group password shadow)
98 (user+group-databases users* sys:%base-groups
101 #:current-shadow '()))
104 (write-group group (string-append etc "/group"))
105 (write-passwd password (string-append etc "/passwd"))
106 (write-shadow shadow (string-append etc "/shadow")))
108 (define (call-with-mnt-container thunk)
109 "This is a variant of call-with-container. Run THUNK in a new container
110 process, within a separate MNT namespace. The container is not jailed so that
111 it can interact with the rest of the system."
112 (let ((pid (run-container "/" '() '(mnt) 1 thunk)))
113 ;; Catch SIGINT and kill the container process.
117 (kill pid SIGKILL))))
120 ((_ . status) status))))
122 (define* (install-system locale #:key (users '()))
123 "Create /etc/shadow and /etc/passwd on the installation target for USERS.
124 Start COW-STORE service on target directory and launch guix install command in
125 a subshell. LOCALE must be the locale name under which that command will run,
126 or #f. Return #t on success and #f on failure."
127 (define backing-directory
128 ;; Sub-directory used as the backing store for copy-on-write.
131 (define (assert-exit x)
132 (primitive-exit (if x 0 1)))
134 (let* ((options (catch 'system-error
136 ;; If this file exists, it can provide
137 ;; additional command-line options.
138 (call-with-input-file
139 "/tmp/installer-system-init-options"
142 (install-command (append (list "guix" "system" "init"
145 (list (%installer-configuration-file)
146 (%installer-target-dir))))
147 (database-dir "/var/guix/db")
148 (database-file (string-append database-dir "/db.sqlite"))
149 (saved-database (string-append database-dir "/db.save"))
151 (mkdir-p (%installer-target-dir))
153 ;; We want to initialize user passwords but we don't want to store them in
154 ;; the config file since the password hashes would end up world-readable
155 ;; in the store. Thus, create /etc/shadow & co. here such that, on the
156 ;; first boot, the activation snippet that creates accounts will reuse the
157 ;; passwords that we've put in there.
158 (create-user-database users (%installer-target-dir))
160 ;; When the store overlay is mounted, other processes such as kmscon, udev
161 ;; and guix-daemon may open files from the store, preventing the
162 ;; underlying install support from being umounted. See:
163 ;; https://lists.gnu.org/archive/html/guix-devel/2018-12/msg00161.html.
165 ;; To avoid this situation, mount the store overlay inside a container,
166 ;; and run the installation from within that container.
168 (call-with-mnt-container
172 ;; Save the database, so that it can be restored once the
173 ;; cow-store is umounted.
174 (copy-file database-file saved-database)
175 (mount-cow-store (%installer-target-dir) backing-directory))
177 ;; We need to drag the guix-daemon to the container MNT
178 ;; namespace, so that it can operate on the cow-store.
179 (stop-service 'guix-daemon)
180 (start-service 'guix-daemon (list (number->string (getpid))))
182 (setvbuf (current-output-port) 'none)
183 (setvbuf (current-error-port) 'none)
185 ;; If there are any connected clients, assume that we are running
186 ;; installation tests. In that case, dump the standard and error
187 ;; outputs to syslog.
189 (if (not (null? (current-clients)))
190 (with-output-to-file "/dev/console"
192 (with-error-to-file "/dev/console"
194 (run-command install-command)))))
195 (run-command install-command))))
197 ;; Restart guix-daemon so that it does no keep the MNT namespace
199 (restart-service 'guix-daemon)
200 (copy-file saved-database database-file)
202 ;; Finally umount the cow-store and exit the container.
203 (unmount-cow-store (%installer-target-dir) backing-directory)
204 (assert-exit ret))))))))