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