installer: Do not set the locale in run-command.
[jackhill/guix/guix.git] / gnu / installer / final.scm
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>
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
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))
40
41 (define %seed
42 (seed->random-state
43 (logxor (getpid) (car (gettimeofday)))))
44
45 (define (integer->alphanumeric-char n)
46 "Map N, an integer in the [0..62] range, to an alphanumeric character."
47 (cond ((< n 10)
48 (integer->char (+ (char->integer #\0) n)))
49 ((< n 36)
50 (integer->char (+ (char->integer #\A) (- n 10))))
51 ((< n 62)
52 (integer->char (+ (char->integer #\a) (- n 36))))
53 (else
54 (error "integer out of bounds" n))))
55
56 (define (random-string len)
57 "Compute a random string of size LEN where each character is alphanumeric."
58 (let loop ((chars '())
59 (len len))
60 (if (zero? len)
61 (list->string chars)
62 (let ((n (random 62 %seed)))
63 (loop (cons (integer->alphanumeric-char n) chars)
64 (- len 1))))))
65
66 (define (create-user-database users root)
67 "Create /etc/passwd, /etc/shadow, and /etc/group under ROOT for the given
68 USERS."
69 (define etc
70 (string-append root "/etc"))
71
72 (define (salt)
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)))
76
77 (define users*
78 (map (lambda (user)
79 (define root?
80 (string=? "root" (user-name user)))
81
82 (sys:user-account (name (user-name user))
83 (comment (user-real-name user))
84 (group "users")
85 (uid (if root? 0 #f))
86 (home-directory
87 (user-home-directory user))
88 (password (crypt (user-password user)
89 (salt)))
90
91 ;; We need a string here, not a file-like, hence
92 ;; this choice.
93 (shell
94 "/run/current-system/profile/bin/bash")))
95 users))
96
97 (define-values (group password shadow)
98 (user+group-databases users* sys:%base-groups
99 #:current-passwd '()
100 #:current-groups '()
101 #:current-shadow '()))
102
103 (mkdir-p etc)
104 (write-group group (string-append etc "/group"))
105 (write-passwd password (string-append etc "/passwd"))
106 (write-shadow shadow (string-append etc "/shadow")))
107
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.
114 (sigaction SIGINT
115 (lambda (signum)
116 (false-if-exception
117 (kill pid SIGKILL))))
118
119 (match (waitpid pid)
120 ((_ . status) status))))
121
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.
129 "/tmp/guix-inst")
130
131 (define (assert-exit x)
132 (primitive-exit (if x 0 1)))
133
134 (let* ((options (catch 'system-error
135 (lambda ()
136 ;; If this file exists, it can provide
137 ;; additional command-line options.
138 (call-with-input-file
139 "/tmp/installer-system-init-options"
140 read))
141 (const '())))
142 (install-command (append (list "guix" "system" "init"
143 "--fallback")
144 options
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"))
150 (ret #f))
151 (mkdir-p (%installer-target-dir))
152
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))
159
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.
164 ;;
165 ;; To avoid this situation, mount the store overlay inside a container,
166 ;; and run the installation from within that container.
167 (zero?
168 (call-with-mnt-container
169 (lambda ()
170 (dynamic-wind
171 (lambda ()
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))
176 (lambda ()
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))))
181
182 (setvbuf (current-output-port) 'none)
183 (setvbuf (current-error-port) 'none)
184
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.
188 (set! ret
189 (if (not (null? (current-clients)))
190 (with-output-to-file "/dev/console"
191 (lambda ()
192 (with-error-to-file "/dev/console"
193 (lambda ()
194 (run-command install-command)))))
195 (run-command install-command))))
196 (lambda ()
197 ;; Restart guix-daemon so that it does no keep the MNT namespace
198 ;; alive.
199 (restart-service 'guix-daemon)
200 (copy-file saved-database database-file)
201
202 ;; Finally umount the cow-store and exit the container.
203 (unmount-cow-store (%installer-target-dir) backing-directory)
204 (assert-exit ret))))))))