453b0bdd6d28359f29efff43eddb4a0ad55690ff
[jackhill/guix/guix.git] / gnu / system / install.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2014, 2015, 2016, 2017, 2018, 2019 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2015 Mark H Weaver <mhw@netris.org>
4 ;;; Copyright © 2016 Andreas Enge <andreas@enge.fr>
5 ;;; Copyright © 2017 Marius Bakke <mbakke@fastmail.com>
6 ;;; Copyright © 2017 Tobias Geerinckx-Rice <me@tobias.gr>
7 ;;;
8 ;;; This file is part of GNU Guix.
9 ;;;
10 ;;; GNU Guix is free software; you can redistribute it and/or modify it
11 ;;; under the terms of the GNU General Public License as published by
12 ;;; the Free Software Foundation; either version 3 of the License, or (at
13 ;;; your option) any later version.
14 ;;;
15 ;;; GNU Guix is distributed in the hope that it will be useful, but
16 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;;; GNU General Public License for more details.
19 ;;;
20 ;;; You should have received a copy of the GNU General Public License
21 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
22
23 (define-module (gnu system install)
24 #:use-module (gnu)
25 #:use-module (gnu system)
26 #:use-module (gnu bootloader u-boot)
27 #:use-module (guix gexp)
28 #:use-module (guix store)
29 #:use-module (guix monads)
30 #:use-module ((guix packages) #:select (package-version))
31 #:use-module ((guix store) #:select (%store-prefix))
32 #:use-module (gnu installer)
33 #:use-module (gnu services dbus)
34 #:use-module (gnu services networking)
35 #:use-module (gnu services shepherd)
36 #:use-module (gnu services ssh)
37 #:use-module (gnu packages admin)
38 #:use-module (gnu packages bash)
39 #:use-module (gnu packages bootloaders)
40 #:use-module (gnu packages certs)
41 #:use-module (gnu packages fonts)
42 #:use-module (gnu packages fontutils)
43 #:use-module (gnu packages guile)
44 #:use-module (gnu packages linux)
45 #:use-module (gnu packages ssh)
46 #:use-module (gnu packages cryptsetup)
47 #:use-module (gnu packages package-management)
48 #:use-module (gnu packages disk)
49 #:use-module (gnu packages texinfo)
50 #:use-module (gnu packages compression)
51 #:use-module (gnu packages nvi)
52 #:use-module (ice-9 match)
53 #:use-module (srfi srfi-26)
54 #:export (installation-os
55 a20-olinuxino-lime-installation-os
56 a20-olinuxino-lime2-emmc-installation-os
57 a20-olinuxino-micro-installation-os
58 bananapi-m2-ultra-installation-os
59 beaglebone-black-installation-os
60 mx6cuboxi-installation-os
61 nintendo-nes-classic-edition-installation-os
62 novena-installation-os
63 pine64-plus-installation-os
64 pinebook-installation-os
65 rk3399-puma-installation-os
66 wandboard-installation-os
67 os-with-u-boot))
68
69 ;;; Commentary:
70 ;;;
71 ;;; This module provides an 'operating-system' definition for use on images
72 ;;; for USB sticks etc., for the installation of the GNU system.
73 ;;;
74 ;;; Code:
75
76 \f
77 ;;;
78 ;;; Documentation service.
79 ;;;
80
81 (define %installation-node-names
82 ;; Translated name of the "System Installation" node of the manual. Ideally
83 ;; we'd extract it from the 'guix-manual' gettext domain, but that one is
84 ;; usually not available at run time, hence this hack.
85 '(("de" . "Systeminstallation")
86 ("en" . "System Installation")
87 ("es" . "Instalación del sistema")
88 ("fr" . "Installation du système")
89 ("ru" . "Установка системы")))
90
91 (define (log-to-info tty user)
92 "Return a script that spawns the Info reader on the right section of the
93 manual."
94 (program-file "log-to-info"
95 #~(let* ((tty (open-file #$(string-append "/dev/" tty)
96 "r0+"))
97 (locale (cadr (command-line)))
98 (language (string-take locale
99 (string-index locale #\_)))
100 (infodir "/run/current-system/profile/share/info")
101 (per-lang (string-append infodir "/guix." language
102 ".info.gz"))
103 (file (if (file-exists? per-lang)
104 per-lang
105 (string-append infodir "/guix.info")))
106 (node (or (assoc-ref '#$%installation-node-names
107 language)
108 "System Installation")))
109 (redirect-port tty (current-output-port))
110 (redirect-port tty (current-error-port))
111 (redirect-port tty (current-input-port))
112
113 (let ((pw (getpwnam #$user)))
114 (setgid (passwd:gid pw))
115 (setuid (passwd:uid pw)))
116
117 ;; 'gunzip' is needed to decompress the doc.
118 (setenv "PATH" (string-append #$gzip "/bin"))
119
120 ;; Change this process' locale so that command-line
121 ;; arguments to 'info' are properly encoded.
122 (catch #t
123 (lambda ()
124 (setlocale LC_ALL locale)
125 (setenv "LC_ALL" locale))
126 (lambda _
127 ;; Sometimes LOCALE itself is not available. In that
128 ;; case pick the one UTF-8 locale that's known to work
129 ;; instead of failing.
130 (setlocale LC_ALL "en_US.utf8")
131 (setenv "LC_ALL" "en_US.utf8")))
132
133 (execl #$(file-append info-reader "/bin/info")
134 "info" "-d" infodir "-f" file "-n" node))))
135
136 (define (documentation-shepherd-service tty)
137 (list (shepherd-service
138 (provision (list (symbol-append 'term- (string->symbol tty))))
139 (requirement '(user-processes host-name udev virtual-terminal))
140 (start #~(lambda* (#:optional (locale "en_US.utf8"))
141 (fork+exec-command
142 (list #$(log-to-info tty "documentation") locale)
143 #:environment-variables
144 `("GUIX_LOCPATH=/run/current-system/locale"
145 "TERM=linux"))))
146 (stop #~(make-kill-destructor)))))
147
148 (define %documentation-users
149 ;; User account for the Info viewer.
150 (list (user-account (name "documentation")
151 (system? #t)
152 (group "nogroup")
153 (home-directory "/var/empty"))))
154
155 (define documentation-service-type
156 ;; Documentation viewer service.
157 (service-type (name 'documentation)
158 (extensions
159 (list (service-extension shepherd-root-service-type
160 documentation-shepherd-service)
161 (service-extension account-service-type
162 (const %documentation-users))))
163 (description "Run the Info reader on a tty.")))
164
165 \f
166 (define %backing-directory
167 ;; Sub-directory used as the backing store for copy-on-write.
168 "/tmp/guix-inst")
169
170 (define (make-cow-store target)
171 "Return a gexp that makes the store copy-on-write, using TARGET as the
172 backing store. This is useful when TARGET is on a hard disk, whereas the
173 current store is on a RAM disk."
174
175 (define (set-store-permissions directory)
176 ;; Set the right perms on DIRECTORY to use it as the store.
177 #~(begin
178 (chown #$directory 0 30000) ;use the fixed 'guixbuild' GID
179 (chmod #$directory #o1775)))
180
181 #~(begin
182 ;; Bind-mount TARGET's /tmp in case we need space to build things.
183 (let ((tmpdir (string-append #$target "/tmp")))
184 (mkdir-p tmpdir)
185 (mount tmpdir "/tmp" "none" MS_BIND))
186
187 (let* ((rw-dir (string-append target #$%backing-directory))
188 (work-dir (string-append rw-dir "/../.overlayfs-workdir")))
189 (mkdir-p rw-dir)
190 (mkdir-p work-dir)
191 (mkdir-p "/.rw-store")
192 #$(set-store-permissions #~rw-dir)
193 #$(set-store-permissions "/.rw-store")
194
195 ;; Mount the overlay, then atomically make it the store.
196 (mount "none" "/.rw-store" "overlay" 0
197 (string-append "lowerdir=" #$(%store-prefix) ","
198 "upperdir=" rw-dir ","
199 "workdir=" work-dir))
200 (mount "/.rw-store" #$(%store-prefix) "" MS_MOVE)
201 (rmdir "/.rw-store"))))
202
203 (define cow-store-service-type
204 (shepherd-service-type
205 'cow-store
206 (lambda _
207 (shepherd-service
208 (requirement '(root-file-system user-processes))
209 (provision '(cow-store))
210 (documentation
211 "Make the store copy-on-write, with writes going to \
212 the given target.")
213
214 ;; This is meant to be explicitly started by the user.
215 (auto-start? #f)
216
217 (start #~(case-lambda
218 ((target)
219 #$(make-cow-store #~target)
220 target)
221 (else
222 ;; Do nothing, and mark the service as stopped.
223 #f)))
224 (stop #~(lambda (target)
225 ;; Delete the temporary directory, but leave everything
226 ;; mounted as there may still be processes using it since
227 ;; 'user-processes' doesn't depend on us. The 'user-file-systems'
228 ;; service will unmount TARGET eventually.
229 (delete-file-recursively
230 (string-append target #$%backing-directory))))))))
231
232 (define (cow-store-service)
233 "Return a service that makes the store copy-on-write, such that writes go to
234 the user's target storage device rather than on the RAM disk."
235 ;; See <http://bugs.gnu.org/18061> for the initial report.
236 (service cow-store-service-type 'mooooh!))
237
238
239 (define (/etc/configuration-files _)
240 "Return a list of tuples representing configuration templates to add to
241 /etc."
242 (define (file f)
243 (local-file (string-append "examples/" f)))
244
245 (define directory
246 (computed-file "configuration-templates"
247 (with-imported-modules '((guix build utils))
248 #~(begin
249 (mkdir #$output)
250 (for-each (lambda (file target)
251 (copy-file file
252 (string-append #$output "/"
253 target)))
254 '(#$(file "bare-bones.tmpl")
255 #$(file "beaglebone-black.tmpl")
256 #$(file "desktop.tmpl")
257 #$(file "lightweight-desktop.tmpl"))
258 '("bare-bones.scm"
259 "beaglebone-black.scm"
260 "desktop.scm"
261 "lightweight-desktop.scm"))
262 #t))))
263
264 `(("configuration" ,directory)))
265
266 (define configuration-template-service-type
267 (service-type (name 'configuration-template)
268 (extensions
269 (list (service-extension etc-service-type
270 /etc/configuration-files)))))
271
272 (define %configuration-template-service
273 (service configuration-template-service-type #t))
274
275
276 (define %nscd-minimal-caches
277 ;; Minimal in-memory caching policy for nscd.
278 (list (nscd-cache (database 'hosts)
279 (positive-time-to-live (* 3600 12))
280
281 ;; Do not cache lookup failures at all since they are
282 ;; quite likely (for instance when someone tries to ping a
283 ;; host before networking is functional.)
284 (negative-time-to-live 0)
285
286 (persistent? #f)
287 (max-database-size (* 5 (expt 2 20)))))) ;5 MiB
288
289 (define %installation-services
290 ;; List of services of the installation system.
291 (let ((motd (plain-file "motd" "
292 \x1b[1;37mWelcome to the installation of GNU Guix!\x1b[0m
293
294 \x1b[2m\
295 Using this shell, you can carry out the installation process \"manually.\"
296 Access documentation at any time by pressing Alt-F2.\x1b[0m
297 ")))
298 (define (normal-tty tty)
299 (mingetty-service (mingetty-configuration (tty tty)
300 (auto-login "root")
301 (login-pause? #t))))
302
303 (define bare-bones-os
304 (load "examples/bare-bones.tmpl"))
305
306 (list (service virtual-terminal-service-type)
307
308 (service kmscon-service-type
309 (kmscon-configuration
310 (virtual-terminal "tty1")
311 (login-program (installer-program))))
312
313 (login-service (login-configuration
314 (motd motd)))
315
316 ;; Documentation. The manual is in UTF-8, but
317 ;; 'console-font-service' sets up Unicode support and loads a font
318 ;; with all the useful glyphs like em dash and quotation marks.
319 (service documentation-service-type "tty2")
320
321 ;; Documentation add-on.
322 %configuration-template-service
323
324 ;; A bunch of 'root' ttys.
325 (normal-tty "tty3")
326 (normal-tty "tty4")
327 (normal-tty "tty5")
328 (normal-tty "tty6")
329
330 ;; The usual services.
331 (syslog-service)
332
333 ;; The build daemon. Register the hydra.gnu.org key as trusted.
334 ;; This allows the installation process to use substitutes by
335 ;; default.
336 (service guix-service-type
337 (guix-configuration (authorize-key? #t)))
338
339 ;; Start udev so that useful device nodes are available.
340 ;; Use device-mapper rules for cryptsetup & co; enable the CRDA for
341 ;; regulations-compliant WiFi access.
342 (udev-service #:rules (list lvm2 crda))
343
344 ;; Add the 'cow-store' service, which users have to start manually
345 ;; since it takes the installation directory as an argument.
346 (cow-store-service)
347
348 ;; Install Unicode support and a suitable font.
349 (service console-font-service-type
350 (map (match-lambda
351 ("tty2"
352 ;; Use a font that contains characters such as
353 ;; curly quotes as found in the manual.
354 '("tty2" . "LatGrkCyr-8x16"))
355 (tty
356 ;; Use a font that doesn't have more than 256
357 ;; glyphs so that we can use colors with varying
358 ;; brightness levels (see note in setfont(8)).
359 `(,tty . "lat9u-16")))
360 '("tty1" "tty2" "tty3" "tty4" "tty5" "tty6")))
361
362 ;; To facilitate copy/paste.
363 (service gpm-service-type)
364
365 ;; Add an SSH server to facilitate remote installs.
366 (service openssh-service-type
367 (openssh-configuration
368 (port-number 22)
369 (permit-root-login #t)
370 ;; The root account is passwordless, so make sure
371 ;; a password is set before allowing logins.
372 (allow-empty-passwords? #f)
373 (password-authentication? #t)
374
375 ;; Don't start it upfront.
376 (%auto-start? #f)))
377
378 ;; Since this is running on a USB stick with a overlayfs as the root
379 ;; file system, use an appropriate cache configuration.
380 (nscd-service (nscd-configuration
381 (caches %nscd-minimal-caches)))
382
383 ;; Having /bin/sh is a good idea. In particular it allows Tramp
384 ;; connections to this system to work.
385 (service special-files-service-type
386 `(("/bin/sh" ,(file-append (canonical-package bash)
387 "/bin/sh"))))
388
389 ;; Loopback device, needed by OpenSSH notably.
390 (service static-networking-service-type
391 (list (static-networking (interface "lo")
392 (ip "127.0.0.1")
393 (requirement '())
394 (provision '(loopback)))))
395
396 (service wpa-supplicant-service-type)
397 (dbus-service)
398 (service connman-service-type
399 (connman-configuration
400 (disable-vpn? #t)))
401
402 ;; Keep a reference to BARE-BONES-OS to make sure it can be
403 ;; installed without downloading/building anything. Also keep the
404 ;; things needed by 'profile-derivation' to minimize the amount of
405 ;; download.
406 (service gc-root-service-type
407 (list bare-bones-os
408 glibc-utf8-locales
409 texinfo
410 (canonical-package guile-2.2))))))
411
412 (define %issue
413 ;; Greeting.
414 "
415 \x1b[1;37mThis is an installation image of the GNU system. Welcome.\x1b[0m
416
417 \x1b[1;33mUse Alt-F2 for documentation.\x1b[0m
418 ")
419
420 (define installation-os
421 ;; The operating system used on installation images for USB sticks etc.
422 (operating-system
423 (host-name "gnu")
424 (timezone "Europe/Paris")
425 (locale "en_US.utf8")
426 (bootloader (bootloader-configuration
427 (bootloader grub-bootloader)
428 (target "/dev/sda")))
429 (label (string-append "GNU Guix installation "
430 (package-version guix)))
431
432 ;; XXX: The AMD Radeon driver is reportedly broken, which makes kmscon
433 ;; non-functional:
434 ;; <https://lists.gnu.org/archive/html/guix-devel/2019-03/msg00441.html>.
435 ;; Thus, blacklist it.
436 (kernel-arguments '("quiet" "modprobe.blacklist=radeon"))
437
438 (file-systems
439 ;; Note: the disk image build code overrides this root file system with
440 ;; the appropriate one.
441 (cons* (file-system
442 (mount-point "/")
443 (device (file-system-label "Guix_image"))
444 (type "ext4"))
445
446 ;; Make /tmp a tmpfs instead of keeping the overlayfs. This
447 ;; originally was used for unionfs because FUSE creates
448 ;; '.fuse_hiddenXYZ' files for each open file, and this confuses
449 ;; Guix's test suite, for instance (see
450 ;; <http://bugs.gnu.org/23056>). We keep this for overlayfs to be
451 ;; on the safe side.
452 (file-system
453 (mount-point "/tmp")
454 (device "none")
455 (type "tmpfs")
456 (check? #f))
457
458 ;; XXX: This should be %BASE-FILE-SYSTEMS but we don't need
459 ;; elogind's cgroup file systems.
460 (list %pseudo-terminal-file-system
461 %shared-memory-file-system
462 %immutable-store)))
463
464 (users (list (user-account
465 (name "guest")
466 (group "users")
467 (supplementary-groups '("wheel")) ; allow use of sudo
468 (password "")
469 (comment "Guest of GNU"))))
470
471 (issue %issue)
472 (services %installation-services)
473
474 ;; We don't need setuid programs, except for 'passwd', which can be handy
475 ;; if one is to allow remote SSH login to the machine being installed.
476 (setuid-programs (list (file-append shadow "/bin/passwd")))
477
478 (pam-services
479 ;; Explicitly allow for empty passwords.
480 (base-pam-services #:allow-empty-passwords? #t))
481
482 (packages (cons* (canonical-package glibc) ;for 'tzselect' & co.
483 parted gptfdisk ddrescue
484 fontconfig
485 font-dejavu font-gnu-unifont
486 grub ;mostly so xrefs to its manual work
487 cryptsetup
488 mdadm
489 dosfstools ;mkfs.fat, for the UEFI boot partition
490 btrfs-progs
491 openssh ;we already have sshd, having ssh/scp can help
492 wireless-tools iw wpa-supplicant-minimal iproute
493 ;; XXX: We used to have GNU fdisk here, but as of version
494 ;; 2.0.0a, that pulls Guile 1.8, which takes unreasonable
495 ;; space; furthermore util-linux's fdisk is already
496 ;; available here, so we keep that.
497 bash-completion
498 nvi ;:wq!
499 nss-certs ; To access HTTPS, use git, etc.
500 %base-packages))))
501
502 (define* (os-with-u-boot os board #:key (bootloader-target "/dev/mmcblk0")
503 (triplet "arm-linux-gnueabihf"))
504 "Given OS, amend it with the u-boot bootloader for BOARD,
505 installed to BOOTLOADER-TARGET (a drive), compiled for TRIPLET.
506
507 If you want a serial console, make sure to specify one in your
508 operating-system's kernel-arguments (\"console=ttyS0\" or similar)."
509 (operating-system (inherit os)
510 (bootloader (bootloader-configuration
511 (bootloader (bootloader (inherit u-boot-bootloader)
512 (package (make-u-boot-package board triplet))))
513 (target bootloader-target)))))
514
515 (define* (embedded-installation-os bootloader bootloader-target tty
516 #:key (extra-modules '()))
517 "Return an installation os for embedded systems.
518 The initrd gets the extra modules EXTRA-MODULES.
519 A getty is provided on TTY.
520 The bootloader BOOTLOADER is installed to BOOTLOADER-TARGET."
521 (operating-system
522 (inherit installation-os)
523 (bootloader (bootloader-configuration
524 (bootloader bootloader)
525 (target bootloader-target)))
526 (kernel linux-libre)
527 (kernel-arguments
528 (cons (string-append "console=" tty)
529 (operating-system-user-kernel-arguments installation-os)))
530 (initrd-modules (append extra-modules %base-initrd-modules))))
531
532 (define beaglebone-black-installation-os
533 (embedded-installation-os u-boot-beaglebone-black-bootloader
534 "/dev/sda"
535 "ttyO0"
536 #:extra-modules
537 ;; This module is required to mount the sd card.
538 '("omap_hsmmc")))
539
540
541 (define a20-olinuxino-lime-installation-os
542 (embedded-installation-os u-boot-a20-olinuxino-lime-bootloader
543 "/dev/mmcblk0" ; SD card storage
544 "ttyS0"))
545
546 (define a20-olinuxino-lime2-emmc-installation-os
547 (embedded-installation-os u-boot-a20-olinuxino-lime2-bootloader
548 "/dev/mmcblk1" ; eMMC storage
549 "ttyS0"))
550
551 (define a20-olinuxino-micro-installation-os
552 (embedded-installation-os u-boot-a20-olinuxino-micro-bootloader
553 "/dev/mmcblk0" ; SD card storage
554 "ttyS0"))
555
556 (define bananapi-m2-ultra-installation-os
557 (embedded-installation-os u-boot-bananapi-m2-ultra-bootloader
558 "/dev/mmcblk1" ; eMMC storage
559 "ttyS0"))
560
561 (define mx6cuboxi-installation-os
562 (embedded-installation-os u-boot-mx6cuboxi-bootloader
563 "/dev/mmcblk0" ; SD card storage
564 "ttymxc0"))
565
566 (define novena-installation-os
567 (embedded-installation-os u-boot-novena-bootloader
568 "/dev/mmcblk1" ; SD card storage
569 "ttymxc1"))
570
571 (define nintendo-nes-classic-edition-installation-os
572 (embedded-installation-os u-boot-nintendo-nes-classic-edition-bootloader
573 "/dev/mmcblk0" ; SD card (solder it yourself)
574 "ttyS0"))
575
576 (define pine64-plus-installation-os
577 (embedded-installation-os u-boot-pine64-plus-bootloader
578 "/dev/mmcblk0" ; SD card storage
579 "ttyS0"))
580
581 (define pinebook-installation-os
582 (embedded-installation-os u-boot-pinebook-bootloader
583 "/dev/mmcblk0" ; SD card storage
584 "ttyS0"))
585
586 (define rk3399-puma-installation-os
587 (embedded-installation-os u-boot-puma-rk3399-bootloader
588 "/dev/mmcblk0" ; SD card storage
589 "ttyS0"))
590
591 (define wandboard-installation-os
592 (embedded-installation-os u-boot-wandboard-bootloader
593 "/dev/mmcblk0" ; SD card storage
594 "ttymxc0"))
595
596 ;; Return the default os here so 'guix system' can consume it directly.
597 installation-os
598
599 ;;; install.scm ends here