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