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