Merge branch 'master' into core-updates
[jackhill/guix/guix.git] / gnu / system / install.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2014, 2015, 2016, 2017, 2018 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 bootloader u-boot)
26 #:use-module (guix gexp)
27 #:use-module (guix store)
28 #:use-module (guix monads)
29 #:use-module ((guix store) #:select (%store-prefix))
30 #:use-module (gnu services shepherd)
31 #:use-module (gnu services ssh)
32 #:use-module (gnu packages admin)
33 #:use-module (gnu packages bash)
34 #:use-module (gnu packages bootloaders)
35 #:use-module (gnu packages guile)
36 #:use-module (gnu packages linux)
37 #:use-module (gnu packages ssh)
38 #:use-module (gnu packages cryptsetup)
39 #:use-module (gnu packages package-management)
40 #:use-module (gnu packages disk)
41 #:use-module (gnu packages texinfo)
42 #:use-module (gnu packages compression)
43 #:use-module (gnu packages nvi)
44 #:use-module (ice-9 match)
45 #:use-module (srfi srfi-26)
46 #:export (installation-os
47 a20-olinuxino-lime-installation-os
48 a20-olinuxino-lime2-emmc-installation-os
49 a20-olinuxino-micro-installation-os
50 bananapi-m2-ultra-installation-os
51 beaglebone-black-installation-os
52 mx6cuboxi-installation-os
53 nintendo-nes-classic-edition-installation-os
54 novena-installation-os
55 pine64-plus-installation-os
56 pinebook-installation-os
57 rk3399-puma-installation-os
58 wandboard-installation-os
59 os-with-u-boot))
60
61 ;;; Commentary:
62 ;;;
63 ;;; This module provides an 'operating-system' definition for use on images
64 ;;; for USB sticks etc., for the installation of the GNU system.
65 ;;;
66 ;;; Code:
67
68 \f
69 (define (log-to-info)
70 "Return a script that spawns the Info reader on the right section of the
71 manual."
72 (program-file "log-to-info"
73 #~(begin
74 ;; 'gunzip' is needed to decompress the doc.
75 (setenv "PATH" (string-append #$gzip "/bin"))
76
77 (execl (string-append #$info-reader "/bin/info") "info"
78 "-d" "/run/current-system/profile/share/info"
79 "-f" (string-append #$guix "/share/info/guix.info")
80 "-n" "System Installation"))))
81
82 (define %backing-directory
83 ;; Sub-directory used as the backing store for copy-on-write.
84 "/tmp/guix-inst")
85
86 (define (make-cow-store target)
87 "Return a gexp that makes the store copy-on-write, using TARGET as the
88 backing store. This is useful when TARGET is on a hard disk, whereas the
89 current store is on a RAM disk."
90
91 (define (set-store-permissions directory)
92 ;; Set the right perms on DIRECTORY to use it as the store.
93 #~(begin
94 (chown #$directory 0 30000) ;use the fixed 'guixbuild' GID
95 (chmod #$directory #o1775)))
96
97 #~(begin
98 ;; Bind-mount TARGET's /tmp in case we need space to build things.
99 (let ((tmpdir (string-append #$target "/tmp")))
100 (mkdir-p tmpdir)
101 (mount tmpdir "/tmp" "none" MS_BIND))
102
103 (let* ((rw-dir (string-append target #$%backing-directory))
104 (work-dir (string-append rw-dir "/../.overlayfs-workdir")))
105 (mkdir-p rw-dir)
106 (mkdir-p work-dir)
107 (mkdir-p "/.rw-store")
108 #$(set-store-permissions #~rw-dir)
109 #$(set-store-permissions "/.rw-store")
110
111 ;; Mount the overlay, then atomically make it the store.
112 (mount "none" "/.rw-store" "overlay" 0
113 (string-append "lowerdir=" #$(%store-prefix) ","
114 "upperdir=" rw-dir ","
115 "workdir=" work-dir))
116 (mount "/.rw-store" #$(%store-prefix) "" MS_MOVE)
117 (rmdir "/.rw-store"))))
118
119 (define cow-store-service-type
120 (shepherd-service-type
121 'cow-store
122 (lambda _
123 (shepherd-service
124 (requirement '(root-file-system user-processes))
125 (provision '(cow-store))
126 (documentation
127 "Make the store copy-on-write, with writes going to \
128 the given target.")
129
130 ;; This is meant to be explicitly started by the user.
131 (auto-start? #f)
132
133 (start #~(case-lambda
134 ((target)
135 #$(make-cow-store #~target)
136 target)
137 (else
138 ;; Do nothing, and mark the service as stopped.
139 #f)))
140 (stop #~(lambda (target)
141 ;; Delete the temporary directory, but leave everything
142 ;; mounted as there may still be processes using it since
143 ;; 'user-processes' doesn't depend on us. The 'user-file-systems'
144 ;; service will unmount TARGET eventually.
145 (delete-file-recursively
146 (string-append target #$%backing-directory))))))))
147
148 (define (cow-store-service)
149 "Return a service that makes the store copy-on-write, such that writes go to
150 the user's target storage device rather than on the RAM disk."
151 ;; See <http://bugs.gnu.org/18061> for the initial report.
152 (service cow-store-service-type 'mooooh!))
153
154
155 (define (/etc/configuration-files _)
156 "Return a list of tuples representing configuration templates to add to
157 /etc."
158 (define (file f)
159 (local-file (string-append "examples/" f)))
160
161 (define directory
162 (computed-file "configuration-templates"
163 (with-imported-modules '((guix build utils))
164 #~(begin
165 (mkdir #$output)
166 (for-each (lambda (file target)
167 (copy-file file
168 (string-append #$output "/"
169 target)))
170 '(#$(file "bare-bones.tmpl")
171 #$(file "beaglebone-black.tmpl")
172 #$(file "desktop.tmpl")
173 #$(file "lightweight-desktop.tmpl"))
174 '("bare-bones.scm"
175 "beaglebone-black.scm"
176 "desktop.scm"
177 "lightweight-desktop.scm"))
178 #t))))
179
180 `(("configuration" ,directory)))
181
182 (define configuration-template-service-type
183 (service-type (name 'configuration-template)
184 (extensions
185 (list (service-extension etc-service-type
186 /etc/configuration-files)))))
187
188 (define %configuration-template-service
189 (service configuration-template-service-type #t))
190
191
192 (define %nscd-minimal-caches
193 ;; Minimal in-memory caching policy for nscd.
194 (list (nscd-cache (database 'hosts)
195 (positive-time-to-live (* 3600 12))
196
197 ;; Do not cache lookup failures at all since they are
198 ;; quite likely (for instance when someone tries to ping a
199 ;; host before networking is functional.)
200 (negative-time-to-live 0)
201
202 (persistent? #f)
203 (max-database-size (* 5 (expt 2 20)))))) ;5 MiB
204
205 (define %installation-services
206 ;; List of services of the installation system.
207 (let ((motd (plain-file "motd" "
208 \x1b[1;37mWelcome to the installation of the Guix System Distribution!\x1b[0m
209
210 \x1b[2mThere is NO WARRANTY, to the extent permitted by law. In particular, you may
211 LOSE ALL YOUR DATA as a side effect of the installation process. Furthermore,
212 it is 'beta' software, so it may contain bugs.
213
214 You have been warned. Thanks for being so brave.\x1b[0m
215 ")))
216 (define (normal-tty tty)
217 (mingetty-service (mingetty-configuration (tty tty)
218 (auto-login "root")
219 (login-pause? #t))))
220
221 (define bare-bones-os
222 (load "examples/bare-bones.tmpl"))
223
224 (list (service virtual-terminal-service-type)
225
226 (mingetty-service (mingetty-configuration
227 (tty "tty1")
228 (auto-login "root")))
229
230 (login-service (login-configuration
231 (motd motd)))
232
233 ;; Documentation. The manual is in UTF-8, but
234 ;; 'console-font-service' sets up Unicode support and loads a font
235 ;; with all the useful glyphs like em dash and quotation marks.
236 (mingetty-service (mingetty-configuration
237 (tty "tty2")
238 (auto-login "guest")
239 (login-program (log-to-info))))
240
241 ;; Documentation add-on.
242 %configuration-template-service
243
244 ;; A bunch of 'root' ttys.
245 (normal-tty "tty3")
246 (normal-tty "tty4")
247 (normal-tty "tty5")
248 (normal-tty "tty6")
249
250 ;; The usual services.
251 (syslog-service)
252
253 ;; The build daemon. Register the official server keys as trusted.
254 ;; This allows the installation process to use substitutes by
255 ;; default.
256 (guix-service (guix-configuration (authorize-key? #t)))
257
258 ;; Start udev so that useful device nodes are available.
259 ;; Use device-mapper rules for cryptsetup & co; enable the CRDA for
260 ;; regulations-compliant WiFi access.
261 (udev-service #:rules (list lvm2 crda))
262
263 ;; Add the 'cow-store' service, which users have to start manually
264 ;; since it takes the installation directory as an argument.
265 (cow-store-service)
266
267 ;; Install Unicode support and a suitable font. Use a font that
268 ;; doesn't have more than 256 glyphs so that we can use colors with
269 ;; varying brightness levels (see note in setfont(8)).
270 (service console-font-service-type
271 (map (lambda (tty)
272 (cons tty "lat9u-16"))
273 '("tty1" "tty2" "tty3" "tty4" "tty5" "tty6")))
274
275 ;; To facilitate copy/paste.
276 (gpm-service)
277
278 ;; Add an SSH server to facilitate remote installs.
279 (service openssh-service-type
280 (openssh-configuration
281 (port-number 22)
282 (permit-root-login #t)
283 ;; The root account is passwordless, so make sure
284 ;; a password is set before allowing logins.
285 (allow-empty-passwords? #f)
286 (password-authentication? #t)
287
288 ;; Don't start it upfront.
289 (%auto-start? #f)))
290
291 ;; Since this is running on a USB stick with a overlayfs as the root
292 ;; file system, use an appropriate cache configuration.
293 (nscd-service (nscd-configuration
294 (caches %nscd-minimal-caches)))
295
296 ;; Having /bin/sh is a good idea. In particular it allows Tramp
297 ;; connections to this system to work.
298 (service special-files-service-type
299 `(("/bin/sh" ,(file-append (canonical-package bash)
300 "/bin/sh"))))
301
302 ;; Loopback device, needed by OpenSSH notably.
303 (service static-networking-service-type
304 (list (static-networking (interface "lo")
305 (ip "127.0.0.1")
306 (requirement '())
307 (provision '(loopback)))))
308
309 ;; Keep a reference to BARE-BONES-OS to make sure it can be
310 ;; installed without downloading/building anything. Also keep the
311 ;; things needed by 'profile-derivation' to minimize the amount of
312 ;; download.
313 (service gc-root-service-type
314 (list bare-bones-os
315 glibc-utf8-locales
316 texinfo
317 (canonical-package guile-2.2))))))
318
319 (define %issue
320 ;; Greeting.
321 "
322 \x1b[1;37mThis is an installation image of the GNU system. Welcome.\x1b[0m
323
324 \x1b[1;33mUse Alt-F2 for documentation.\x1b[0m
325 ")
326
327 (define installation-os
328 ;; The operating system used on installation images for USB sticks etc.
329 (operating-system
330 (host-name "gnu")
331 (timezone "Europe/Paris")
332 (locale "en_US.utf8")
333 (bootloader (bootloader-configuration
334 (bootloader grub-bootloader)
335 (target "/dev/sda")))
336 (file-systems
337 ;; Note: the disk image build code overrides this root file system with
338 ;; the appropriate one.
339 (cons* (file-system
340 (mount-point "/")
341 (device (file-system-label "GuixSD_image"))
342 (type "ext4"))
343
344 ;; Make /tmp a tmpfs instead of keeping the overlayfs. This
345 ;; originally was used for unionfs because FUSE creates
346 ;; '.fuse_hiddenXYZ' files for each open file, and this confuses
347 ;; Guix's test suite, for instance (see
348 ;; <http://bugs.gnu.org/23056>). We keep this for overlayfs to be
349 ;; on the safe side.
350 (file-system
351 (mount-point "/tmp")
352 (device "none")
353 (type "tmpfs")
354 (check? #f))
355
356 ;; XXX: This should be %BASE-FILE-SYSTEMS but we don't need
357 ;; elogind's cgroup file systems.
358 (list %pseudo-terminal-file-system
359 %shared-memory-file-system
360 %immutable-store)))
361
362 (users (list (user-account
363 (name "guest")
364 (group "users")
365 (supplementary-groups '("wheel")) ; allow use of sudo
366 (password "")
367 (comment "Guest of GNU")
368 (home-directory "/home/guest"))))
369
370 (issue %issue)
371 (services %installation-services)
372
373 ;; We don't need setuid programs, except for 'passwd', which can be handy
374 ;; if one is to allow remote SSH login to the machine being installed.
375 (setuid-programs (list (file-append shadow "/bin/passwd")))
376
377 (pam-services
378 ;; Explicitly allow for empty passwords.
379 (base-pam-services #:allow-empty-passwords? #t))
380
381 (packages (cons* (canonical-package glibc) ;for 'tzselect' & co.
382 parted gptfdisk ddrescue
383 grub ;mostly so xrefs to its manual work
384 cryptsetup
385 mdadm
386 dosfstools ;mkfs.fat, for the UEFI boot partition
387 btrfs-progs
388 openssh ;we already have sshd, having ssh/scp can help
389 wireless-tools iw wpa-supplicant-minimal iproute
390 ;; XXX: We used to have GNU fdisk here, but as of version
391 ;; 2.0.0a, that pulls Guile 1.8, which takes unreasonable
392 ;; space; furthermore util-linux's fdisk is already
393 ;; available here, so we keep that.
394 bash-completion
395 nvi ;:wq!
396 %base-packages))))
397
398 (define* (os-with-u-boot os board #:key (bootloader-target "/dev/mmcblk0")
399 (triplet "arm-linux-gnueabihf"))
400 "Given OS, amend it with the u-boot bootloader for BOARD,
401 installed to BOOTLOADER-TARGET (a drive), compiled for TRIPLET.
402
403 If you want a serial console, make sure to specify one in your
404 operating-system's kernel-arguments (\"console=ttyS0\" or similar)."
405 (operating-system (inherit os)
406 (bootloader (bootloader-configuration
407 (bootloader (bootloader (inherit u-boot-bootloader)
408 (package (make-u-boot-package board triplet))))
409 (target bootloader-target)))))
410
411 (define* (embedded-installation-os bootloader bootloader-target tty
412 #:key (extra-modules '()))
413 "Return an installation os for embedded systems.
414 The initrd gets the extra modules EXTRA-MODULES.
415 A getty is provided on TTY.
416 The bootloader BOOTLOADER is installed to BOOTLOADER-TARGET."
417 (operating-system
418 (inherit installation-os)
419 (bootloader (bootloader-configuration
420 (bootloader bootloader)
421 (target bootloader-target)))
422 (kernel linux-libre)
423 (kernel-arguments
424 (cons (string-append "console=" tty)
425 (operating-system-user-kernel-arguments installation-os)))
426 (initrd-modules (append extra-modules %base-initrd-modules))))
427
428 (define beaglebone-black-installation-os
429 (embedded-installation-os u-boot-beaglebone-black-bootloader
430 "/dev/sda"
431 "ttyO0"
432 #:extra-modules
433 ;; This module is required to mount the sd card.
434 '("omap_hsmmc")))
435
436
437 (define a20-olinuxino-lime-installation-os
438 (embedded-installation-os u-boot-a20-olinuxino-lime-bootloader
439 "/dev/mmcblk0" ; SD card storage
440 "ttyS0"))
441
442 (define a20-olinuxino-lime2-emmc-installation-os
443 (embedded-installation-os u-boot-a20-olinuxino-lime2-bootloader
444 "/dev/mmcblk1" ; eMMC storage
445 "ttyS0"))
446
447 (define a20-olinuxino-micro-installation-os
448 (embedded-installation-os u-boot-a20-olinuxino-micro-bootloader
449 "/dev/mmcblk0" ; SD card storage
450 "ttyS0"))
451
452 (define bananapi-m2-ultra-installation-os
453 (embedded-installation-os u-boot-bananapi-m2-ultra-bootloader
454 "/dev/mmcblk1" ; eMMC storage
455 "ttyS0"))
456
457 (define mx6cuboxi-installation-os
458 (embedded-installation-os u-boot-mx6cuboxi-bootloader
459 "/dev/mmcblk0" ; SD card storage
460 "ttymxc0"))
461
462 (define novena-installation-os
463 (embedded-installation-os u-boot-novena-bootloader
464 "/dev/mmcblk1" ; SD card storage
465 "ttymxc1"))
466
467 (define nintendo-nes-classic-edition-installation-os
468 (embedded-installation-os u-boot-nintendo-nes-classic-edition-bootloader
469 "/dev/mmcblk0" ; SD card (solder it yourself)
470 "ttyS0"))
471
472 (define pine64-plus-installation-os
473 (embedded-installation-os u-boot-pine64-plus-bootloader
474 "/dev/mmcblk0" ; SD card storage
475 "ttyS0"))
476
477 (define pinebook-installation-os
478 (embedded-installation-os u-boot-pinebook-bootloader
479 "/dev/mmcblk0" ; SD card storage
480 "ttyS0"))
481
482 (define rk3399-puma-installation-os
483 (embedded-installation-os u-boot-puma-rk3399-bootloader
484 "/dev/mmcblk0" ; SD card storage
485 "ttyS0"))
486
487 (define wandboard-installation-os
488 (embedded-installation-os u-boot-wandboard-bootloader
489 "/dev/mmcblk0" ; SD card storage
490 "ttymxc0"))
491
492 ;; Return the default os here so 'guix system' can consume it directly.
493 installation-os
494
495 ;;; install.scm ends here