guix system: Clarify what happens where service upgrade fails.
[jackhill/guix/guix.git] / guix / scripts / system.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2014, 2015, 2016, 2017, 2018, 2019, 2020 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2016 Alex Kost <alezost@gmail.com>
4 ;;; Copyright © 2016, 2017, 2018 Chris Marusich <cmmarusich@gmail.com>
5 ;;; Copyright © 2017, 2019 Mathieu Othacehe <m.othacehe@gmail.com>
6 ;;; Copyright © 2018 Ricardo Wurmus <rekado@elephly.net>
7 ;;; Copyright © 2019 Christopher Baines <mail@cbaines.net>
8 ;;; Copyright © 2020 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
9 ;;;
10 ;;; This file is part of GNU Guix.
11 ;;;
12 ;;; GNU Guix is free software; you can redistribute it and/or modify it
13 ;;; under the terms of the GNU General Public License as published by
14 ;;; the Free Software Foundation; either version 3 of the License, or (at
15 ;;; your option) any later version.
16 ;;;
17 ;;; GNU Guix is distributed in the hope that it will be useful, but
18 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;;; GNU General Public License for more details.
21 ;;;
22 ;;; You should have received a copy of the GNU General Public License
23 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
24
25 (define-module (guix scripts system)
26 #:use-module (guix config)
27 #:use-module (guix ui)
28 #:use-module ((guix status) #:select (with-status-verbosity))
29 #:use-module (guix store)
30 #:autoload (guix store database) (register-path)
31 #:use-module (guix describe)
32 #:use-module (guix grafts)
33 #:use-module (guix gexp)
34 #:use-module (guix derivations)
35 #:use-module (guix packages)
36 #:use-module (guix utils)
37 #:use-module (guix monads)
38 #:use-module (guix records)
39 #:use-module (guix profiles)
40 #:use-module (guix scripts)
41 #:use-module (guix channels)
42 #:use-module (guix scripts build)
43 #:autoload (guix scripts package) (delete-generations
44 delete-matching-generations)
45 #:autoload (guix scripts pull) (channel-commit-hyperlink)
46 #:use-module (guix graph)
47 #:use-module (guix scripts graph)
48 #:use-module (guix scripts system reconfigure)
49 #:use-module (guix build utils)
50 #:use-module (guix progress)
51 #:use-module ((guix build syscalls) #:select (terminal-columns))
52 #:use-module (gnu build install)
53 #:autoload (gnu build file-systems)
54 (find-partition-by-label find-partition-by-uuid)
55 #:autoload (gnu build linux-modules)
56 (device-module-aliases matching-modules)
57 #:use-module (gnu system linux-initrd)
58 #:use-module (gnu image)
59 #:use-module (gnu system)
60 #:use-module (gnu bootloader)
61 #:use-module (gnu system file-systems)
62 #:use-module (gnu system image)
63 #:use-module (gnu system mapped-devices)
64 #:use-module (gnu system linux-container)
65 #:use-module (gnu system uuid)
66 #:use-module (gnu system vm)
67 #:use-module (gnu services)
68 #:use-module (gnu services shepherd)
69 #:use-module (gnu services herd)
70 #:use-module (srfi srfi-1)
71 #:use-module (srfi srfi-11)
72 #:use-module (srfi srfi-19)
73 #:use-module (srfi srfi-26)
74 #:use-module (srfi srfi-34)
75 #:use-module (srfi srfi-35)
76 #:use-module (srfi srfi-37)
77 #:use-module (ice-9 format)
78 #:use-module (ice-9 match)
79 #:use-module (rnrs bytevectors)
80 #:export (guix-system
81 read-operating-system))
82
83 \f
84 ;;;
85 ;;; Operating system declaration.
86 ;;;
87
88 (define %user-module
89 ;; Module in which the machine description file is loaded.
90 (make-user-module '((gnu system)
91 (gnu services)
92 (gnu system shadow))))
93
94 (define (read-operating-system file)
95 "Read the operating-system declaration from FILE and return it."
96 (load* file %user-module))
97
98 \f
99 ;;;
100 ;;; Installation.
101 ;;;
102
103 (define-syntax-rule (save-load-path-excursion body ...)
104 "Save the current values of '%load-path' and '%load-compiled-path', run
105 BODY..., and restore them."
106 (let ((path %load-path)
107 (cpath %load-compiled-path))
108 (dynamic-wind
109 (const #t)
110 (lambda ()
111 body ...)
112 (lambda ()
113 (set! %load-path path)
114 (set! %load-compiled-path cpath)))))
115
116 (define-syntax-rule (save-environment-excursion body ...)
117 "Save the current environment variables, run BODY..., and restore them."
118 (let ((env (environ)))
119 (dynamic-wind
120 (const #t)
121 (lambda ()
122 body ...)
123 (lambda ()
124 (environ env)))))
125
126 (define topologically-sorted*
127 (store-lift topologically-sorted))
128
129
130 (define* (copy-item item references target
131 #:key (log-port (current-error-port)))
132 "Copy ITEM to the store under root directory TARGET and register it with
133 REFERENCES as its set of references."
134 (let ((dest (string-append target item))
135 (state (string-append target "/var/guix")))
136 (format log-port "copying '~a'...~%" item)
137
138 ;; Remove DEST if it exists to make sure that (1) we do not fail badly
139 ;; while trying to overwrite it (see <http://bugs.gnu.org/20722>), and
140 ;; (2) we end up with the right contents.
141 (when (false-if-exception (lstat dest))
142 (for-each make-file-writable
143 (find-files dest (lambda (file stat)
144 (eq? 'directory (stat:type stat)))
145 #:directories? #t))
146 (delete-file-recursively dest))
147
148 (copy-recursively item dest
149 #:log (%make-void-port "w"))
150
151 ;; Register ITEM; as a side-effect, it resets timestamps, etc.
152 ;; Explicitly use "TARGET/var/guix" as the state directory, to avoid
153 ;; reproducing the user's current settings; see
154 ;; <http://bugs.gnu.org/18049>.
155 (unless (register-path item
156 #:prefix target
157 #:state-directory state
158 #:references references)
159 (leave (G_ "failed to register '~a' under '~a'~%")
160 item target))))
161
162 (define* (copy-closure item target
163 #:key (log-port (current-error-port)))
164 "Copy ITEM and all its dependencies to the store under root directory
165 TARGET, and register them."
166 (mlet* %store-monad ((to-copy (topologically-sorted* (list item)))
167 (refs (mapm %store-monad references* to-copy))
168 (info (mapm %store-monad query-path-info*
169 (delete-duplicates
170 (append to-copy (concatenate refs)))))
171 (size -> (reduce + 0 (map path-info-nar-size info))))
172 (define progress-bar
173 (progress-reporter/bar (length to-copy)
174 (format #f (G_ "copying to '~a'...")
175 target)))
176
177 (check-available-space size target)
178
179 (call-with-progress-reporter progress-bar
180 (lambda (report)
181 (let ((void (%make-void-port "w")))
182 (for-each (lambda (item refs)
183 (copy-item item refs target #:log-port void)
184 (report))
185 to-copy refs))))
186
187 (return *unspecified*)))
188
189 (define* (install os-drv target
190 #:key (log-port (current-output-port))
191 install-bootloader? bootloader bootcfg)
192 "Copy the closure of BOOTCFG, which includes the output of OS-DRV, to
193 directory TARGET. TARGET must be an absolute directory name since that's what
194 'register-path' expects.
195
196 When INSTALL-BOOTLOADER? is true, install bootloader using BOOTCFG."
197 (define (maybe-copy to-copy)
198 (with-monad %store-monad
199 (if (string=? target "/")
200 (begin
201 (warning (G_ "initializing the current root file system~%"))
202 (return #t))
203 (begin
204 ;; Make sure the target store exists.
205 (mkdir-p (string-append target (%store-prefix)))
206
207 ;; Copy items to the new store.
208 (copy-closure to-copy target #:log-port log-port)))))
209
210 ;; Make sure TARGET is root-owned when running as root, but still allow
211 ;; non-root uses (useful for testing.) See
212 ;; <http://lists.gnu.org/archive/html/guix-devel/2015-05/msg00452.html>.
213 (if (zero? (geteuid))
214 (chown target 0 0)
215 (warning (G_ "not running as 'root', so \
216 the ownership of '~a' may be incorrect!~%")
217 target))
218
219 ;; If a previous installation was attempted, make sure we start anew; in
220 ;; particular, we don't want to keep a store database that might not
221 ;; correspond to what we're actually putting in the store.
222 (let ((state (string-append target "/var/guix")))
223 (when (file-exists? state)
224 (delete-file-recursively state)))
225
226 (chmod target #o755)
227 (let ((os-dir (derivation->output-path os-drv))
228 (format (lift format %store-monad))
229 (populate (lift2 populate-root-file-system %store-monad)))
230
231 (mlet %store-monad ((bootcfg (lower-object bootcfg)))
232 (mbegin %store-monad
233 ;; Copy the closure of BOOTCFG, which includes OS-DIR,
234 ;; eventual background image and so on.
235 (maybe-copy (derivation->output-path bootcfg))
236
237 ;; Create a bunch of additional files.
238 (format log-port "populating '~a'...~%" target)
239 (populate os-dir target)
240
241 (mwhen install-bootloader?
242 (install-bootloader local-eval bootloader bootcfg
243 #:target target)
244 (return
245 (info (G_ "bootloader successfully installed on '~a'~%")
246 (bootloader-configuration-target bootloader))))))))
247
248 \f
249 ;;;
250 ;;; Reconfiguration.
251 ;;;
252
253 (define %system-profile
254 ;; The system profile.
255 (string-append %state-directory "/profiles/system"))
256
257 (define-syntax-rule (with-shepherd-error-handling mbody ...)
258 "Catch and report Shepherd errors that arise when binding MBODY, a monadic
259 expression in %STORE-MONAD."
260 (lambda (store)
261 (catch 'system-error
262 (lambda ()
263 (guard (c ((shepherd-error? c)
264 (values (report-shepherd-error c) store)))
265 (values (run-with-store store (mbegin %store-monad mbody ...))
266 store)))
267 (lambda (key proc format-string format-args errno . rest)
268 (warning (G_ "while talking to shepherd: ~a~%")
269 (apply format #f format-string format-args))
270 (values #f store)))))
271
272 (define (report-shepherd-error error)
273 "Report ERROR, a '&shepherd-error' error condition object."
274 (when error
275 (cond ((service-not-found-error? error)
276 (warning (G_ "service '~a' could not be found~%")
277 (service-not-found-error-service error)))
278 ((action-not-found-error? error)
279 (warning (G_ "service '~a' does not have an action '~a'~%")
280 (action-not-found-error-service error)
281 (action-not-found-error-action error)))
282 ((action-exception-error? error)
283 (warning (G_ "exception caught while executing '~a' \
284 on service '~a':~%")
285 (action-exception-error-action error)
286 (action-exception-error-service error))
287 (print-exception (current-error-port) #f
288 (action-exception-error-key error)
289 (action-exception-error-arguments error)))
290 ((unknown-shepherd-error? error)
291 (warning (G_ "something went wrong: ~s~%")
292 (unknown-shepherd-error-sexp error)))
293 ((shepherd-error? error)
294 (warning (G_ "shepherd error~%"))))
295
296 ;; Don't leave users out in the cold and explain what that means and what
297 ;; they can do.
298 (warning (G_ "some services could not be upgraded~%"))
299 (display-hint (G_ "To allow changes to all the system services to take
300 effect, you will need to reboot."))))
301
302 (define-syntax-rule (unless-file-not-found exp)
303 (catch 'system-error
304 (lambda ()
305 exp)
306 (lambda args
307 (if (= ENOENT (system-error-errno args))
308 #f
309 (apply throw args)))))
310
311 (define (seconds->string seconds)
312 "Return a string representing the date for SECONDS."
313 (let ((time (make-time time-utc 0 seconds)))
314 (date->string (time-utc->date time)
315 "~Y-~m-~d ~H:~M")))
316
317 (define* (profile-boot-parameters #:optional (profile %system-profile)
318 (numbers
319 (reverse (generation-numbers profile))))
320 "Return a list of 'boot-parameters' for the generations of PROFILE specified
321 by NUMBERS, which is a list of generation numbers. The list is ordered from
322 the most recent to the oldest profiles."
323 (define (system->boot-parameters system number time)
324 (unless-file-not-found
325 (let* ((params (read-boot-parameters-file system))
326 (label (boot-parameters-label params)))
327 (boot-parameters
328 (inherit params)
329 (label (string-append label " (#"
330 (number->string number) ", "
331 (seconds->string time) ")"))))))
332 (let* ((systems (map (cut generation-file-name profile <>)
333 numbers))
334 (times (map (lambda (system)
335 (unless-file-not-found
336 (stat:mtime (lstat system))))
337 systems)))
338 (filter-map system->boot-parameters systems numbers times)))
339
340 \f
341 ;;;
342 ;;; Roll-back.
343 ;;;
344 (define (roll-back-system store)
345 "Roll back the system profile to its previous generation. STORE is an open
346 connection to the store."
347 (switch-to-system-generation store "-1"))
348
349 \f
350 ;;;
351 ;;; Switch generations.
352 ;;;
353 (define (switch-to-system-generation store spec)
354 "Switch the system profile to the generation specified by SPEC, and
355 re-install bootloader with a configuration file that uses the specified system
356 generation as its default entry. STORE is an open connection to the store."
357 (let ((number (relative-generation-spec->number %system-profile spec)))
358 (if number
359 (begin
360 (reinstall-bootloader store number)
361 (switch-to-generation* %system-profile number))
362 (leave (G_ "cannot switch to system generation '~a'~%") spec))))
363
364 (define* (system-bootloader-name #:optional (system %system-profile))
365 "Return the bootloader name stored in SYSTEM's \"parameters\" file."
366 (let ((params (unless-file-not-found
367 (read-boot-parameters-file system))))
368 (boot-parameters-bootloader-name params)))
369
370 (define (reinstall-bootloader store number)
371 "Re-install bootloader for existing system profile generation NUMBER.
372 STORE is an open connection to the store."
373 (let* ((generation (generation-file-name %system-profile number))
374 ;; Detect the bootloader used in %system-profile.
375 (bootloader (lookup-bootloader-by-name (system-bootloader-name)))
376
377 ;; Use the detected bootloader with default configuration.
378 ;; It will be enough to allow the system to boot.
379 (bootloader-config (bootloader-configuration
380 (bootloader bootloader)))
381
382 ;; Make the specified system generation the default entry.
383 (params (first (profile-boot-parameters %system-profile
384 (list number))))
385 (old-generations
386 (delv number (reverse (generation-numbers %system-profile))))
387 (old-params (profile-boot-parameters
388 %system-profile old-generations))
389 (entries (cons (boot-parameters->menu-entry params)
390 (boot-parameters-bootloader-menu-entries params)))
391 (old-entries (map boot-parameters->menu-entry old-params)))
392 (run-with-store store
393 (mlet* %store-monad
394 ((bootcfg (lower-object
395 ((bootloader-configuration-file-generator bootloader)
396 bootloader-config entries
397 #:old-entries old-entries)))
398 (drvs -> (list bootcfg)))
399 (mbegin %store-monad
400 (built-derivations drvs)
401 ;; Only install bootloader configuration file.
402 (install-bootloader local-eval bootloader-config bootcfg
403 #:run-installer? #f))))))
404
405 \f
406 ;;;
407 ;;; Graphs.
408 ;;;
409
410 (define (service-node-label service)
411 "Return a label to represent SERVICE."
412 (let ((type (service-kind service))
413 (value (service-value service)))
414 (string-append (symbol->string (service-type-name type))
415 (cond ((or (number? value) (symbol? value))
416 (string-append " " (object->string value)))
417 ((string? value)
418 (string-append " " value))
419 ((file-system? value)
420 (string-append " " (file-system-mount-point value)))
421 (else
422 "")))))
423
424 (define (service-node-type services)
425 "Return a node type for SERVICES. Since <service> instances are not
426 self-contained (they express dependencies on service types, not on services),
427 we have to create the 'edges' procedure dynamically as a function of the full
428 list of services."
429 (node-type
430 (name "service")
431 (description "the DAG of services")
432 (identifier (lift1 object-address %store-monad))
433 (label service-node-label)
434 (edges (lift1 (service-back-edges services) %store-monad))))
435
436 (define (shepherd-service-node-label service)
437 "Return a label for a node representing a <shepherd-service>."
438 (string-join (map symbol->string (shepherd-service-provision service))))
439
440 (define (shepherd-service-node-type services)
441 "Return a node type for SERVICES, a list of <shepherd-service>."
442 (node-type
443 (name "shepherd-service")
444 (description "the dependency graph of shepherd services")
445 (identifier (lift1 shepherd-service-node-label %store-monad))
446 (label shepherd-service-node-label)
447 (edges (lift1 (shepherd-service-back-edges services) %store-monad))))
448
449 \f
450 ;;;
451 ;;; Generations.
452 ;;;
453
454 (define* (display-system-generation number
455 #:optional (profile %system-profile))
456 "Display a summary of system generation NUMBER in a human-readable format."
457 (define (display-channel channel)
458 (format #t " ~a:~%" (channel-name channel))
459 (format #t (G_ " repository URL: ~a~%") (channel-url channel))
460 (when (channel-branch channel)
461 (format #t (G_ " branch: ~a~%") (channel-branch channel)))
462 (format #t (G_ " commit: ~a~%")
463 (if (supports-hyperlinks?)
464 (channel-commit-hyperlink channel)
465 (channel-commit channel))))
466
467 (unless (zero? number)
468 (let* ((generation (generation-file-name profile number))
469 (params (read-boot-parameters-file generation))
470 (label (boot-parameters-label params))
471 (bootloader-name (boot-parameters-bootloader-name params))
472 (root (boot-parameters-root-device params))
473 (root-device (if (bytevector? root)
474 (uuid->string root)
475 root))
476 (kernel (boot-parameters-kernel params))
477 (multiboot-modules (boot-parameters-multiboot-modules params)))
478 (define-values (channels config-file)
479 (system-provenance generation))
480
481 (display-generation profile number)
482 (format #t (G_ " file name: ~a~%") generation)
483 (format #t (G_ " canonical file name: ~a~%") (readlink* generation))
484 ;; TRANSLATORS: Please preserve the two-space indentation.
485 (format #t (G_ " label: ~a~%") label)
486 (format #t (G_ " bootloader: ~a~%") bootloader-name)
487
488 ;; TRANSLATORS: The '~[', '~;', and '~]' sequences in this string must
489 ;; be preserved. They denote conditionals, such that the result will
490 ;; look like:
491 ;; root device: UUID: 12345-678
492 ;; or:
493 ;; root device: label: "my-root"
494 ;; or just:
495 ;; root device: /dev/sda3
496 (format #t (G_ " root device: ~[UUID: ~a~;label: ~s~;~a~]~%")
497 (cond ((uuid? root-device) 0)
498 ((file-system-label? root-device) 1)
499 (else 2))
500 (file-system-device->string root-device))
501
502 (format #t (G_ " kernel: ~a~%") kernel)
503
504 (match multiboot-modules
505 (() #f)
506 (((modules . _) ...)
507 (format #t (G_ " multiboot: ~a~%")
508 (string-join modules "\n "))))
509
510 (unless (null? channels)
511 ;; TRANSLATORS: Here "channel" is the same terminology as used in
512 ;; "guix describe" and "guix pull --channels".
513 (format #t (G_ " channels:~%"))
514 (for-each display-channel channels))
515 (when config-file
516 (format #t (G_ " configuration file: ~a~%")
517 (if (supports-hyperlinks?)
518 (file-hyperlink config-file)
519 config-file))))))
520
521 (define* (list-generations pattern #:optional (profile %system-profile))
522 "Display in a human-readable format all the system generations matching
523 PATTERN, a string. When PATTERN is #f, display all the system generations."
524 (cond ((not (file-exists? profile)) ; XXX: race condition
525 (raise (condition (&profile-not-found-error
526 (profile profile)))))
527 ((not pattern)
528 (for-each display-system-generation (profile-generations profile)))
529 ((matching-generations pattern profile)
530 =>
531 (lambda (numbers)
532 (if (null-list? numbers)
533 (exit 1)
534 (leave-on-EPIPE
535 (for-each display-system-generation numbers)))))))
536
537 \f
538 ;;;
539 ;;; File system declaration checks.
540 ;;;
541
542 (define (check-file-system-availability file-systems)
543 "Check whether the UUIDs or partition labels that FILE-SYSTEMS refer to, if
544 any, are available. Raise an error if they're not."
545 (define relevant
546 (filter (lambda (fs)
547 (and (file-system-mount? fs)
548 (not (member (file-system-type fs)
549 %pseudo-file-system-types))
550 ;; Don't try to validate network file systems.
551 (not (string-prefix? "nfs" (file-system-type fs)))
552 (not (memq 'bind-mount (file-system-flags fs)))))
553 file-systems))
554
555 (define labeled
556 (filter (lambda (fs)
557 (file-system-label? (file-system-device fs)))
558 relevant))
559
560 (define literal
561 (filter (lambda (fs)
562 (string? (file-system-device fs)))
563 relevant))
564
565 (define uuid
566 (filter (lambda (fs)
567 (uuid? (file-system-device fs)))
568 relevant))
569
570 (define fail? #f)
571
572 (define (file-system-location* fs)
573 (and=> (file-system-location fs)
574 source-properties->location))
575
576 (let-syntax ((error (syntax-rules ()
577 ((_ args ...)
578 (begin
579 (set! fail? #t)
580 (report-error args ...))))))
581 (for-each (lambda (fs)
582 (catch 'system-error
583 (lambda ()
584 (stat (file-system-device fs)))
585 (lambda args
586 (let ((errno (system-error-errno args))
587 (device (file-system-device fs)))
588 (error (file-system-location* fs)
589 (G_ "device '~a' not found: ~a~%")
590 device (strerror errno))
591 (unless (string-prefix? "/" device)
592 (display-hint (format #f (G_ "If '~a' is a file system
593 label, write @code{(file-system-label ~s)} in your @code{device} field.")
594 device device)))))))
595 literal)
596 (for-each (lambda (fs)
597 (let ((label (file-system-label->string
598 (file-system-device fs))))
599 (unless (find-partition-by-label label)
600 (error (file-system-location* fs)
601 (G_ "file system with label '~a' not found~%")
602 label))))
603 labeled)
604 (for-each (lambda (fs)
605 (unless (find-partition-by-uuid (file-system-device fs))
606 (error (file-system-location* fs)
607 (G_ "file system with UUID '~a' not found~%")
608 (uuid->string (file-system-device fs)))))
609 uuid)
610
611 (when fail?
612 ;; Better be safe than sorry.
613 (exit 1))))
614
615 (define (check-mapped-devices os)
616 "Check that each of MAPPED-DEVICES is valid according to the 'check'
617 procedure of its type."
618 (define boot-mapped-devices
619 (operating-system-boot-mapped-devices os))
620
621 (define (needed-for-boot? md)
622 (memq md boot-mapped-devices))
623
624 (define initrd-modules
625 (operating-system-initrd-modules os))
626
627 (for-each (lambda (md)
628 (let ((check (mapped-device-kind-check
629 (mapped-device-type md))))
630 ;; We expect CHECK to raise an exception with a detailed
631 ;; '&message' if something goes wrong.
632 (check md
633 #:needed-for-boot? (needed-for-boot? md)
634 #:initrd-modules initrd-modules)))
635 (operating-system-mapped-devices os)))
636
637 (define (check-initrd-modules os)
638 "Check that modules needed by 'needed-for-boot' file systems in OS are
639 available in the initrd. Note that mapped devices are responsible for
640 checking this by themselves in their 'check' procedure."
641 (define (file-system-/dev fs)
642 (let ((device (file-system-device fs)))
643 (match device
644 ((? string?)
645 device)
646 ((? uuid?)
647 (find-partition-by-uuid device))
648 ((? file-system-label?)
649 (find-partition-by-label (file-system-label->string device))))))
650
651 (define file-systems
652 (filter file-system-needed-for-boot?
653 (operating-system-file-systems os)))
654
655 (for-each (lambda (fs)
656 (check-device-initrd-modules (file-system-/dev fs)
657 (operating-system-initrd-modules os)
658 (source-properties->location
659 (file-system-location fs))))
660 file-systems))
661
662 \f
663 ;;;
664 ;;; Action.
665 ;;;
666
667 (define* (system-derivation-for-action os base-image action
668 #:key image-size file-system-type
669 full-boot? container-shared-network?
670 mappings)
671 "Return as a monadic value the derivation for OS according to ACTION."
672 (case action
673 ((build init reconfigure)
674 (operating-system-derivation os))
675 ((container)
676 (container-script
677 os
678 #:mappings mappings
679 #:shared-network? container-shared-network?))
680 ((vm-image)
681 (system-qemu-image os #:disk-image-size image-size))
682 ((vm)
683 (system-qemu-image/shared-store-script os
684 #:full-boot? full-boot?
685 #:disk-image-size
686 (if full-boot?
687 image-size
688 (* 70 (expt 2 20)))
689 #:mappings mappings))
690 ((disk-image)
691 (lower-object
692 (system-image
693 (image
694 (inherit base-image)
695 (size image-size)
696 (operating-system os)))))
697 ((docker-image)
698 (system-docker-image os #:shared-network? container-shared-network?))))
699
700 (define (maybe-suggest-running-guix-pull)
701 "Suggest running 'guix pull' if this has never been done before."
702 ;; Check whether we're running a 'guix pull'-provided 'guix' command. When
703 ;; 'current-profile' returns #f, we may be running the globally-installed
704 ;; 'guix' and thus run the risk of deploying an older 'guix'. See
705 ;; <https://lists.gnu.org/archive/html/guix-devel/2014-08/msg00057.html>
706 (unless (or (current-profile) (getenv "GUIX_UNINSTALLED"))
707 (warning (G_ "Consider running 'guix pull' before 'reconfigure'.~%"))
708 (warning (G_ "Failing to do that may downgrade your system!~%"))))
709
710 (define (bootloader-installer-script installer
711 bootloader device target)
712 "Return a file calling INSTALLER gexp with given BOOTLOADER, DEVICE
713 and TARGET arguments."
714 (scheme-file "bootloader-installer"
715 (with-imported-modules '((gnu build bootloader)
716 (guix build utils))
717 #~(begin
718 (use-modules (gnu build bootloader)
719 (guix build utils)
720 (ice-9 binary-ports)
721 (srfi srfi-34)
722 (srfi srfi-35))
723
724 (guard (c ((message-condition? c) ;XXX: i18n
725 (format (current-error-port) "error: ~a~%"
726 (condition-message c))
727 (exit 1)))
728 (#$installer #$bootloader #$device #$target)
729 (info (G_ "bootloader successfully installed on '~a'~%")
730 #$device))))))
731
732 (define (local-eval exp)
733 "Evaluate EXP, a G-Expression, in-place."
734 (mlet* %store-monad ((lowered (lower-gexp exp))
735 (_ (built-derivations (lowered-gexp-inputs lowered))))
736 (save-load-path-excursion
737 (set! %load-path (lowered-gexp-load-path lowered))
738 (set! %load-compiled-path (lowered-gexp-load-compiled-path lowered))
739 (return (primitive-eval (lowered-gexp-sexp lowered))))))
740
741 (define* (perform-action action os
742 #:key
743 (validate-reconfigure ensure-forward-reconfigure)
744 save-provenance?
745 skip-safety-checks?
746 install-bootloader?
747 dry-run? derivations-only?
748 use-substitutes? bootloader-target target
749 image-size file-system-type full-boot?
750 container-shared-network?
751 (mappings '())
752 (gc-root #f))
753 "Perform ACTION for OS. INSTALL-BOOTLOADER? specifies whether to install
754 bootloader; BOOTLOADER-TAGET is the target for the bootloader; TARGET is the
755 target root directory; IMAGE-SIZE is the size of the image to be built, for
756 the 'vm-image' and 'disk-image' actions. The root file system is created as a
757 FILE-SYSTEM-TYPE file system. FULL-BOOT? is used for the 'vm' action; it
758 determines whether to boot directly to the kernel or to the bootloader.
759 CONTAINER-SHARED-NETWORK? determines if the container will use a separate
760 network namespace.
761
762 When DERIVATIONS-ONLY? is true, print the derivation file name(s) without
763 building anything.
764
765 When GC-ROOT is a path, also make that path an indirect root of the build
766 output when building a system derivation, such as a disk image.
767
768 When SKIP-SAFETY-CHECKS? is true, skip the file system and initrd module
769 static checks."
770 (define println
771 (cut format #t "~a~%" <>))
772
773 (define menu-entries
774 (if (eq? 'init action)
775 '()
776 (map boot-parameters->menu-entry (profile-boot-parameters))))
777
778 (define bootloader
779 (operating-system-bootloader os))
780
781 (define bootcfg
782 (and (memq action '(init reconfigure))
783 (operating-system-bootcfg os menu-entries)))
784
785 (when (eq? action 'reconfigure)
786 (maybe-suggest-running-guix-pull)
787 (check-forward-update validate-reconfigure))
788
789 ;; Check whether the declared file systems exist. This is better than
790 ;; instantiating a broken configuration. Assume that we can only check if
791 ;; running as root.
792 (when (and (not skip-safety-checks?)
793 (memq action '(init reconfigure)))
794 (check-mapped-devices os)
795 (when (zero? (getuid))
796 (check-file-system-availability (operating-system-file-systems os))
797 (check-initrd-modules os)))
798
799 (mlet* %store-monad
800 ((target* (current-target-system))
801 (image -> (find-image file-system-type target*))
802 (sys (system-derivation-for-action os image action
803 #:file-system-type file-system-type
804 #:image-size image-size
805 #:full-boot? full-boot?
806 #:container-shared-network? container-shared-network?
807 #:mappings mappings))
808
809 ;; For 'init' and 'reconfigure', always build BOOTCFG, even if
810 ;; --no-bootloader is passed, because we then use it as a GC root.
811 ;; See <http://bugs.gnu.org/21068>.
812 (drvs (mapm/accumulate-builds lower-object
813 (if (memq action '(init reconfigure))
814 (list sys bootcfg)
815 (list sys))))
816 (% (if derivations-only?
817 (return (for-each (compose println derivation-file-name)
818 drvs))
819 (built-derivations drvs))))
820
821 (if (or dry-run? derivations-only?)
822 (return #f)
823 (begin
824 (for-each (compose println derivation->output-path)
825 drvs)
826
827 (case action
828 ((reconfigure)
829 (newline)
830 (format #t (G_ "activating system...~%"))
831 (mbegin %store-monad
832 (switch-to-system local-eval os)
833 (mwhen install-bootloader?
834 (install-bootloader local-eval bootloader bootcfg
835 #:target (or target "/"))
836 (return
837 (info (G_ "bootloader successfully installed on '~a'~%")
838 (bootloader-configuration-target bootloader))))
839 (with-shepherd-error-handling
840 (upgrade-shepherd-services local-eval os)
841 (return (format #t (G_ "\
842 To complete the upgrade, run 'herd restart SERVICE' to stop,
843 upgrade, and restart each service that was not automatically restarted.\n")))
844 (return (format #t (G_ "\
845 Run 'herd status' to view the list of services on your system.\n"))))))
846 ((init)
847 (newline)
848 (format #t (G_ "initializing operating system under '~a'...~%")
849 target)
850 (install sys (canonicalize-path target)
851 #:install-bootloader? install-bootloader?
852 #:bootloader bootloader
853 #:bootcfg bootcfg))
854 (else
855 ;; All we had to do was to build SYS and maybe register an
856 ;; indirect GC root.
857 (let ((output (derivation->output-path sys)))
858 (mbegin %store-monad
859 (mwhen gc-root
860 (register-root* (list output) gc-root))
861 (return output)))))))))
862
863 (define (export-extension-graph os port)
864 "Export the service extension graph of OS to PORT."
865 (let* ((services (operating-system-services os))
866 (system (find (lambda (service)
867 (eq? (service-kind service) system-service-type))
868 services)))
869 (export-graph (list system) (current-output-port)
870 #:node-type (service-node-type services)
871 #:reverse-edges? #t)))
872
873 (define (export-shepherd-graph os port)
874 "Export the graph of shepherd services of OS to PORT."
875 (let* ((services (operating-system-services os))
876 (pid1 (fold-services services
877 #:target-type shepherd-root-service-type))
878 (shepherds (service-value pid1)) ;list of <shepherd-service>
879 (sinks (filter (lambda (service)
880 (null? (shepherd-service-requirement service)))
881 shepherds)))
882 (export-graph sinks (current-output-port)
883 #:node-type (shepherd-service-node-type shepherds)
884 #:reverse-edges? #t)))
885
886 \f
887 ;;;
888 ;;; Options.
889 ;;;
890
891 (define (show-help)
892 (display (G_ "Usage: guix system [OPTION ...] ACTION [ARG ...] [FILE]
893 Build the operating system declared in FILE according to ACTION.
894 Some ACTIONS support additional ARGS.\n"))
895 (newline)
896 (display (G_ "The valid values for ACTION are:\n"))
897 (newline)
898 (display (G_ "\
899 search search for existing service types\n"))
900 (display (G_ "\
901 reconfigure switch to a new operating system configuration\n"))
902 (display (G_ "\
903 roll-back switch to the previous operating system configuration\n"))
904 (display (G_ "\
905 describe describe the current system\n"))
906 (display (G_ "\
907 list-generations list the system generations\n"))
908 (display (G_ "\
909 switch-generation switch to an existing operating system configuration\n"))
910 (display (G_ "\
911 delete-generations delete old system generations\n"))
912 (display (G_ "\
913 build build the operating system without installing anything\n"))
914 (display (G_ "\
915 container build a container that shares the host's store\n"))
916 (display (G_ "\
917 vm build a virtual machine image that shares the host's store\n"))
918 (display (G_ "\
919 vm-image build a freestanding virtual machine image\n"))
920 (display (G_ "\
921 disk-image build a disk image, suitable for a USB stick\n"))
922 (display (G_ "\
923 docker-image build a Docker image\n"))
924 (display (G_ "\
925 init initialize a root file system to run GNU\n"))
926 (display (G_ "\
927 extension-graph emit the service extension graph in Dot format\n"))
928 (display (G_ "\
929 shepherd-graph emit the graph of shepherd services in Dot format\n"))
930
931 (show-build-options-help)
932 (display (G_ "
933 -d, --derivation return the derivation of the given system"))
934 (display (G_ "
935 -e, --expression=EXPR consider the operating-system EXPR evaluates to
936 instead of reading FILE, when applicable"))
937 (display (G_ "
938 --allow-downgrades for 'reconfigure', allow downgrades to earlier
939 channel revisions"))
940 (display (G_ "
941 --on-error=STRATEGY
942 apply STRATEGY (one of nothing-special, backtrace,
943 or debug) when an error occurs while reading FILE"))
944 (display (G_ "
945 --file-system-type=TYPE
946 for 'disk-image', produce a root file system of TYPE
947 (one of 'ext4', 'iso9660')"))
948 (display (G_ "
949 --image-size=SIZE for 'vm-image', produce an image of SIZE"))
950 (display (G_ "
951 --no-bootloader for 'init', do not install a bootloader"))
952 (display (G_ "
953 --save-provenance save provenance information"))
954 (display (G_ "
955 --share=SPEC for 'vm', share host file system according to SPEC"))
956 (display (G_ "
957 --expose=SPEC for 'vm', expose host file system according to SPEC"))
958 (display (G_ "
959 -N, --network for 'container', allow containers to access the network"))
960 (display (G_ "
961 -r, --root=FILE for 'vm', 'vm-image', 'disk-image', 'container',
962 and 'build', make FILE a symlink to the result, and
963 register it as a garbage collector root"))
964 (display (G_ "
965 --full-boot for 'vm', make a full boot sequence"))
966 (display (G_ "
967 --skip-checks skip file system and initrd module safety checks"))
968 (display (G_ "
969 --target=TRIPLET cross-build for TRIPLET--e.g., \"armel-linux-gnu\""))
970 (display (G_ "
971 -v, --verbosity=LEVEL use the given verbosity LEVEL"))
972 (newline)
973 (display (G_ "
974 -h, --help display this help and exit"))
975 (display (G_ "
976 -V, --version display version information and exit"))
977 (newline)
978 (show-bug-report-information))
979
980 (define %options
981 ;; Specifications of the command-line options.
982 (cons* (option '(#\h "help") #f #f
983 (lambda args
984 (show-help)
985 (exit 0)))
986 (option '(#\V "version") #f #f
987 (lambda args
988 (show-version-and-exit "guix system")))
989 (option '(#\e "expression") #t #f
990 (lambda (opt name arg result)
991 (alist-cons 'expression arg result)))
992 (option '(#\d "derivation") #f #f
993 (lambda (opt name arg result)
994 (alist-cons 'derivations-only? #t result)))
995 (option '("allow-downgrades") #f #f
996 (lambda (opt name arg result)
997 (alist-cons 'validate-reconfigure
998 warn-about-backward-reconfigure
999 result)))
1000 (option '("on-error") #t #f
1001 (lambda (opt name arg result)
1002 (alist-cons 'on-error (string->symbol arg)
1003 result)))
1004 (option '(#\t "file-system-type") #t #f
1005 (lambda (opt name arg result)
1006 (alist-cons 'file-system-type arg
1007 result)))
1008 (option '("image-size") #t #f
1009 (lambda (opt name arg result)
1010 (alist-cons 'image-size (size->number arg)
1011 result)))
1012 (option '(#\N "network") #f #f
1013 (lambda (opt name arg result)
1014 (alist-cons 'container-shared-network? #t result)))
1015 (option '("no-bootloader" "no-grub") #f #f
1016 (lambda (opt name arg result)
1017 (alist-cons 'install-bootloader? #f result)))
1018 (option '("full-boot") #f #f
1019 (lambda (opt name arg result)
1020 (alist-cons 'full-boot? #t result)))
1021 (option '("save-provenance") #f #f
1022 (lambda (opt name arg result)
1023 (alist-cons 'save-provenance? #t result)))
1024 (option '("skip-checks") #f #f
1025 (lambda (opt name arg result)
1026 (alist-cons 'skip-safety-checks? #t result)))
1027
1028 (option '("share") #t #f
1029 (lambda (opt name arg result)
1030 (alist-cons 'file-system-mapping
1031 (specification->file-system-mapping arg #t)
1032 result)))
1033 (option '("expose") #t #f
1034 (lambda (opt name arg result)
1035 (alist-cons 'file-system-mapping
1036 (specification->file-system-mapping arg #f)
1037 result)))
1038
1039 (option '(#\n "dry-run") #f #f
1040 (lambda (opt name arg result)
1041 (alist-cons 'dry-run? #t result)))
1042 (option '(#\v "verbosity") #t #f
1043 (lambda (opt name arg result)
1044 (let ((level (string->number* arg)))
1045 (alist-cons 'verbosity level
1046 (alist-delete 'verbosity result)))))
1047 (option '(#\s "system") #t #f
1048 (lambda (opt name arg result)
1049 (alist-cons 'system arg
1050 (alist-delete 'system result eq?))))
1051 (option '("target") #t #f
1052 (lambda (opt name arg result)
1053 (alist-cons 'target arg
1054 (alist-delete 'target result eq?))))
1055 (option '(#\r "root") #t #f
1056 (lambda (opt name arg result)
1057 (alist-cons 'gc-root arg result)))
1058 %standard-build-options))
1059
1060 (define %default-options
1061 ;; Alist of default option values.
1062 `((system . ,(%current-system))
1063 (target . #f)
1064 (substitutes? . #t)
1065 (offload? . #t)
1066 (print-build-trace? . #t)
1067 (print-extended-build-trace? . #t)
1068 (multiplexed-build-output? . #t)
1069 (graft? . #t)
1070 (debug . 0)
1071 (verbosity . #f) ;default
1072 (validate-reconfigure . ,ensure-forward-reconfigure)
1073 (file-system-type . "ext4")
1074 (image-size . guess)
1075 (install-bootloader? . #t)))
1076
1077 (define (verbosity-level opts)
1078 "Return the verbosity level based on OPTS, the alist of parsed options."
1079 (or (assoc-ref opts 'verbosity)
1080 (if (eq? (assoc-ref opts 'action) 'build)
1081 2 1)))
1082
1083 \f
1084 ;;;
1085 ;;; Entry point.
1086 ;;;
1087
1088 (define (process-action action args opts)
1089 "Process ACTION, a sub-command, with the arguments are listed in ARGS.
1090 ACTION must be one of the sub-commands that takes an operating system
1091 declaration as an argument (a file name.) OPTS is the raw alist of options
1092 resulting from command-line parsing."
1093 (define (ensure-operating-system file-or-exp obj)
1094 (unless (operating-system? obj)
1095 (leave (G_ "'~a' does not return an operating system~%")
1096 file-or-exp))
1097 obj)
1098
1099 (define save-provenance?
1100 (or (assoc-ref opts 'save-provenance?)
1101 (memq action '(init reconfigure))))
1102
1103 (let* ((file (match args
1104 (() #f)
1105 ((x . _) x)))
1106 (expr (assoc-ref opts 'expression))
1107 (system (assoc-ref opts 'system))
1108 (target (assoc-ref opts 'target))
1109 (transform (if save-provenance?
1110 (cut operating-system-with-provenance <> file)
1111 identity))
1112 (os (transform
1113 (ensure-operating-system
1114 (or file expr)
1115 (cond
1116 ((and expr file)
1117 (leave
1118 (G_ "both file and expression cannot be specified~%")))
1119 (expr
1120 (read/eval expr))
1121 (file
1122 (load* file %user-module
1123 #:on-error (assoc-ref opts 'on-error)))
1124 (else
1125 (leave (G_ "no configuration specified~%")))))))
1126
1127 (dry? (assoc-ref opts 'dry-run?))
1128 (bootloader? (assoc-ref opts 'install-bootloader?))
1129 (target-file (match args
1130 ((first second) second)
1131 (_ #f)))
1132 (bootloader-target
1133 (and bootloader?
1134 (bootloader-configuration-target
1135 (operating-system-bootloader os)))))
1136
1137 (with-store store
1138 (set-build-options-from-command-line store opts)
1139
1140 (with-build-handler (build-notifier #:use-substitutes?
1141 (assoc-ref opts 'substitutes?)
1142 #:verbosity
1143 (verbosity-level opts)
1144 #:dry-run?
1145 (assoc-ref opts 'dry-run?))
1146 (run-with-store store
1147 (mbegin %store-monad
1148 (set-guile-for-build (default-guile))
1149 (case action
1150 ((extension-graph)
1151 (export-extension-graph os (current-output-port)))
1152 ((shepherd-graph)
1153 (export-shepherd-graph os (current-output-port)))
1154 (else
1155 (unless (memq action '(build init))
1156 (warn-about-old-distro #:suggested-command
1157 "guix system reconfigure"))
1158
1159 (perform-action action os
1160 #:dry-run? dry?
1161 #:derivations-only? (assoc-ref opts
1162 'derivations-only?)
1163 #:use-substitutes? (assoc-ref opts 'substitutes?)
1164 #:skip-safety-checks?
1165 (assoc-ref opts 'skip-safety-checks?)
1166 #:validate-reconfigure
1167 (assoc-ref opts 'validate-reconfigure)
1168 #:file-system-type (assoc-ref opts 'file-system-type)
1169 #:image-size (assoc-ref opts 'image-size)
1170 #:full-boot? (assoc-ref opts 'full-boot?)
1171 #:container-shared-network?
1172 (assoc-ref opts 'container-shared-network?)
1173 #:mappings (filter-map (match-lambda
1174 (('file-system-mapping . m)
1175 m)
1176 (_ #f))
1177 opts)
1178 #:install-bootloader? bootloader?
1179 #:target target-file
1180 #:bootloader-target bootloader-target
1181 #:gc-root (assoc-ref opts 'gc-root)))))
1182 #:target target
1183 #:system system)))
1184 (warn-about-disk-space)))
1185
1186 (define (resolve-subcommand name)
1187 (let ((module (resolve-interface
1188 `(guix scripts system ,(string->symbol name))))
1189 (proc (string->symbol (string-append "guix-system-" name))))
1190 (module-ref module proc)))
1191
1192 (define (process-command command args opts)
1193 "Process COMMAND, one of the 'guix system' sub-commands. ARGS is its
1194 argument list and OPTS is the option alist."
1195 (define-syntax-rule (with-store* store exp ...)
1196 (with-store store
1197 (set-build-options-from-command-line store opts)
1198 exp ...))
1199
1200 (case command
1201 ;; The following commands do not need to use the store, and they do not need
1202 ;; an operating system configuration file.
1203 ((list-generations)
1204 (let ((pattern (match args
1205 (() #f)
1206 ((pattern) pattern)
1207 (x (leave (G_ "wrong number of arguments~%"))))))
1208 (list-generations pattern)))
1209 ((describe)
1210 (match (generation-number %system-profile)
1211 (0
1212 (error (G_ "no system generation, nothing to describe~%")))
1213 (generation
1214 (display-system-generation generation))))
1215 ((search)
1216 (apply (resolve-subcommand "search") args))
1217 ;; The following commands need to use the store, but they do not need an
1218 ;; operating system configuration file.
1219 ((delete-generations)
1220 (let ((pattern (match args
1221 (() #f)
1222 ((pattern) pattern)
1223 (x (leave (G_ "wrong number of arguments~%"))))))
1224 (with-store* store
1225 (delete-matching-generations store %system-profile pattern)
1226 (reinstall-bootloader store (generation-number %system-profile)))))
1227 ((switch-generation)
1228 (let ((pattern (match args
1229 ((pattern) pattern)
1230 (x (leave (G_ "wrong number of arguments~%"))))))
1231 (with-store* store
1232 (switch-to-system-generation store pattern))))
1233 ((roll-back)
1234 (let ((pattern (match args
1235 (() "")
1236 (x (leave (G_ "wrong number of arguments~%"))))))
1237 (with-store* store
1238 (roll-back-system store))))
1239 ;; The following commands need to use the store, and they also
1240 ;; need an operating system configuration file.
1241 (else (process-action command args opts))))
1242
1243 (define (guix-system . args)
1244 (define (parse-sub-command arg result)
1245 ;; Parse sub-command ARG and augment RESULT accordingly.
1246 (if (assoc-ref result 'action)
1247 (alist-cons 'argument arg result)
1248 (let ((action (string->symbol arg)))
1249 (case action
1250 ((build container vm vm-image disk-image reconfigure init
1251 extension-graph shepherd-graph
1252 list-generations describe
1253 delete-generations roll-back
1254 switch-generation search docker-image)
1255 (alist-cons 'action action result))
1256 (else (leave (G_ "~a: unknown action~%") action))))))
1257
1258 (define (match-pair car)
1259 ;; Return a procedure that matches a pair with CAR.
1260 (match-lambda
1261 ((head . tail)
1262 (and (eq? car head) tail))
1263 (_ #f)))
1264
1265 (define (option-arguments opts)
1266 ;; Extract the plain arguments from OPTS.
1267 (let* ((args (reverse (filter-map (match-pair 'argument) opts)))
1268 (count (length args))
1269 (action (assoc-ref opts 'action))
1270 (expr (assoc-ref opts 'expression)))
1271 (define (fail)
1272 (leave (G_ "wrong number of arguments for action '~a'~%")
1273 action))
1274
1275 (unless action
1276 (format (current-error-port)
1277 (G_ "guix system: missing command name~%"))
1278 (format (current-error-port)
1279 (G_ "Try 'guix system --help' for more information.~%"))
1280 (exit 1))
1281
1282 (case action
1283 ((build container vm vm-image disk-image docker-image reconfigure)
1284 (unless (or (= count 1)
1285 (and expr (= count 0)))
1286 (fail)))
1287 ((init)
1288 (unless (= count 2)
1289 (fail))))
1290 args))
1291
1292 (with-error-handling
1293 (let* ((opts (parse-command-line args %options
1294 (list %default-options)
1295 #:argument-handler
1296 parse-sub-command))
1297 (args (option-arguments opts))
1298 (command (assoc-ref opts 'action)))
1299 (parameterize ((%graft? (assoc-ref opts 'graft?)))
1300 (with-status-verbosity (verbosity-level opts)
1301 (process-command command args opts))))))
1302
1303 ;;; Local Variables:
1304 ;;; eval: (put 'with-store* 'scheme-indent-function 1)
1305 ;;; End:
1306
1307 ;;; system.scm ends here