;;; Copyright © 2017, 2019 Mathieu Othacehe <m.othacehe@gmail.com>
;;; Copyright © 2018 Ricardo Wurmus <rekado@elephly.net>
;;; Copyright © 2019 Christopher Baines <mail@cbaines.net>
+;;; Copyright © 2020 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
+;;; Copyright © 2020 Julien Lepiller <julien@lepiller.eu>
+;;; Copyright © 2020 Efraim Flashner <efraim@flashner.co.il>
;;;
;;; This file is part of GNU Guix.
;;;
#:use-module (guix ui)
#:use-module ((guix status) #:select (with-status-verbosity))
#:use-module (guix store)
- #:autoload (guix store database) (register-path)
+ #:autoload (guix base16) (bytevector->base16-string)
+ #:autoload (guix store database)
+ (sqlite-register store-database-file call-with-database)
+ #:autoload (guix build store-copy) (copy-store-item)
#:use-module (guix describe)
#:use-module (guix grafts)
#:use-module (guix gexp)
#:autoload (gnu build linux-modules)
(device-module-aliases matching-modules)
#:use-module (gnu system linux-initrd)
+ #:use-module (gnu image)
#:use-module (gnu system)
#:use-module (gnu bootloader)
#:use-module (gnu system file-systems)
+ #:use-module (gnu system image)
#:use-module (gnu system mapped-devices)
#:use-module (gnu system linux-container)
#:use-module (gnu system uuid)
#:use-module (srfi srfi-34)
#:use-module (srfi srfi-35)
#:use-module (srfi srfi-37)
+ #:use-module (ice-9 format)
#:use-module (ice-9 match)
#:use-module (rnrs bytevectors)
#:export (guix-system
(store-lift topologically-sorted))
-(define* (copy-item item references target
+(define* (copy-item item info target db
#:key (log-port (current-error-port)))
- "Copy ITEM to the store under root directory TARGET and register it with
-REFERENCES as its set of references."
- (let ((dest (string-append target item))
- (state (string-append target "/var/guix")))
+ "Copy ITEM to the store under root directory TARGET and populate DB with the
+given INFO, a <path-info> record."
+ (let ((dest (string-append target item)))
(format log-port "copying '~a'...~%" item)
;; Remove DEST if it exists to make sure that (1) we do not fail badly
#:directories? #t))
(delete-file-recursively dest))
- (copy-recursively item dest
- #:log (%make-void-port "w"))
+ (copy-store-item item target
+ #:deduplicate? #t)
- ;; Register ITEM; as a side-effect, it resets timestamps, etc.
- ;; Explicitly use "TARGET/var/guix" as the state directory, to avoid
- ;; reproducing the user's current settings; see
- ;; <http://bugs.gnu.org/18049>.
- (unless (register-path item
- #:prefix target
- #:state-directory state
- #:references references)
- (leave (G_ "failed to register '~a' under '~a'~%")
- item target))))
+ (sqlite-register db
+ #:path item
+ #:references (path-info-references info)
+ #:deriver (path-info-deriver info)
+ #:hash (string-append
+ "sha256:"
+ (bytevector->base16-string (path-info-hash info)))
+ #:nar-size (path-info-nar-size info))))
(define* (copy-closure item target
#:key (log-port (current-error-port)))
"Copy ITEM and all its dependencies to the store under root directory
TARGET, and register them."
(mlet* %store-monad ((to-copy (topologically-sorted* (list item)))
- (refs (mapm %store-monad references* to-copy))
- (info (mapm %store-monad query-path-info*
- (delete-duplicates
- (append to-copy (concatenate refs)))))
+ (info (mapm %store-monad query-path-info* to-copy))
(size -> (reduce + 0 (map path-info-nar-size info))))
(define progress-bar
(progress-reporter/bar (length to-copy)
(format #f (G_ "copying to '~a'...")
target)))
+ (define state
+ (string-append target "/var/guix"))
+
(check-available-space size target)
- (call-with-progress-reporter progress-bar
- (lambda (report)
- (let ((void (%make-void-port "w")))
- (for-each (lambda (item refs)
- (copy-item item refs target #:log-port void)
- (report))
- to-copy refs))))
+ ;; Explicitly use "TARGET/var/guix" as the state directory to avoid
+ ;; reproducing the user's current settings; see
+ ;; <http://bugs.gnu.org/18049>.
+ (call-with-database (store-database-file #:prefix target
+ #:state-directory state)
+ (lambda (db)
+ (call-with-progress-reporter progress-bar
+ (lambda (report)
+ (let ((void (%make-void-port "w")))
+ (for-each (lambda (item info)
+ (copy-item item info target db #:log-port void)
+ (report))
+ to-copy info))))))
(return *unspecified*)))
(lambda ()
(guard (c ((shepherd-error? c)
(values (report-shepherd-error c) store)))
- (values (run-with-store store (begin mbody ...))
+ (values (run-with-store store (mbegin %store-monad mbody ...))
store)))
(lambda (key proc format-string format-args errno . rest)
(warning (G_ "while talking to shepherd: ~a~%")
(define (report-shepherd-error error)
"Report ERROR, a '&shepherd-error' error condition object."
- (cond ((service-not-found-error? error)
- (report-error (G_ "service '~a' could not be found~%")
- (service-not-found-error-service error)))
- ((action-not-found-error? error)
- (report-error (G_ "service '~a' does not have an action '~a'~%")
- (action-not-found-error-service error)
- (action-not-found-error-action error)))
- ((action-exception-error? error)
- (report-error (G_ "exception caught while executing '~a' \
+ (when error
+ (cond ((service-not-found-error? error)
+ (warning (G_ "service '~a' could not be found~%")
+ (service-not-found-error-service error)))
+ ((action-not-found-error? error)
+ (warning (G_ "service '~a' does not have an action '~a'~%")
+ (action-not-found-error-service error)
+ (action-not-found-error-action error)))
+ ((action-exception-error? error)
+ (warning (G_ "exception caught while executing '~a' \
on service '~a':~%")
- (action-exception-error-action error)
- (action-exception-error-service error))
- (print-exception (current-error-port) #f
- (action-exception-error-key error)
- (action-exception-error-arguments error)))
- ((unknown-shepherd-error? error)
- (report-error (G_ "something went wrong: ~s~%")
- (unknown-shepherd-error-sexp error)))
- ((shepherd-error? error)
- (report-error (G_ "shepherd error~%")))
- ((not error) ;not an error
- #t)))
+ (action-exception-error-action error)
+ (action-exception-error-service error))
+ (print-exception (current-error-port) #f
+ (action-exception-error-key error)
+ (action-exception-error-arguments error)))
+ ((unknown-shepherd-error? error)
+ (warning (G_ "something went wrong: ~s~%")
+ (unknown-shepherd-error-sexp error)))
+ ((shepherd-error? error)
+ (warning (G_ "shepherd error~%"))))
+
+ ;; Don't leave users out in the cold and explain what that means and what
+ ;; they can do.
+ (warning (G_ "some services could not be upgraded~%"))
+ (display-hint (G_ "To allow changes to all the system services to take
+effect, you will need to reboot."))))
(define-syntax-rule (unless-file-not-found exp)
(catch 'system-error
;; Make the specified system generation the default entry.
(params (first (profile-boot-parameters %system-profile
(list number))))
+ (locale (boot-parameters-locale params))
+ (store-crypto-devices (boot-parameters-store-crypto-devices params))
+ (store-directory-prefix
+ (boot-parameters-store-directory-prefix params))
(old-generations
(delv number (reverse (generation-numbers %system-profile))))
(old-params (profile-boot-parameters
((bootcfg (lower-object
((bootloader-configuration-file-generator bootloader)
bootloader-config entries
+ #:locale locale
+ #:store-crypto-devices store-crypto-devices
+ #:store-directory-prefix store-directory-prefix
#:old-entries old-entries)))
(drvs -> (list bootcfg)))
(mbegin %store-monad
;;; Generations.
;;;
-(define (sexp->channel sexp)
- "Return the channel corresponding to SEXP, an sexp as found in the
-\"provenance\" file produced by 'provenance-service-type'."
- (match sexp
- (('channel ('name name)
- ('url url)
- ('branch branch)
- ('commit commit))
- (channel (name name) (url url)
- (branch branch) (commit commit)))))
-
(define* (display-system-generation number
#:optional (profile %system-profile))
"Display a summary of system generation NUMBER in a human-readable format."
(uuid->string root)
root))
(kernel (boot-parameters-kernel params))
- (provenance (catch 'system-error
- (lambda ()
- (call-with-input-file
- (string-append generation "/provenance")
- read))
- (const #f))))
+ (multiboot-modules (boot-parameters-multiboot-modules params)))
+ (define-values (channels config-file)
+ (system-provenance generation))
+
(display-generation profile number)
(format #t (G_ " file name: ~a~%") generation)
(format #t (G_ " canonical file name: ~a~%") (readlink* generation))
(format #t (G_ " kernel: ~a~%") kernel)
- (match provenance
- (#f #t)
- (('provenance ('version 0)
- ('channels channels ...)
- ('configuration-file config-file))
- (unless (null? channels)
- ;; TRANSLATORS: Here "channel" is the same terminology as used in
- ;; "guix describe" and "guix pull --channels".
- (format #t (G_ " channels:~%"))
- (for-each display-channel (map sexp->channel channels)))
- (when config-file
- (format #t (G_ " configuration file: ~a~%")
- (if (supports-hyperlinks?)
- (file-hyperlink config-file)
- config-file))))))))
+ (match multiboot-modules
+ (() #f)
+ (((modules . _) ...)
+ (format #t (G_ " multiboot: ~a~%")
+ (string-join modules "\n "))))
+
+ (unless (null? channels)
+ ;; TRANSLATORS: Here "channel" is the same terminology as used in
+ ;; "guix describe" and "guix pull --channels".
+ (format #t (G_ " channels:~%"))
+ (for-each display-channel channels))
+ (when config-file
+ (format #t (G_ " configuration file: ~a~%")
+ (if (supports-hyperlinks?)
+ (file-hyperlink config-file)
+ config-file))))))
(define* (list-generations pattern #:optional (profile %system-profile))
"Display in a human-readable format all the system generations matching
(define fail? #f)
(define (file-system-location* fs)
- (location->string
- (source-properties->location
- (file-system-location fs))))
+ (and=> (file-system-location fs)
+ source-properties->location))
(let-syntax ((error (syntax-rules ()
((_ args ...)
(begin
(set! fail? #t)
- (format (current-error-port)
- args ...))))))
+ (report-error args ...))))))
(for-each (lambda (fs)
(catch 'system-error
(lambda ()
(lambda args
(let ((errno (system-error-errno args))
(device (file-system-device fs)))
- (error (G_ "~a: error: device '~a' not found: ~a~%")
- (file-system-location* fs) device
- (strerror errno))
+ (error (file-system-location* fs)
+ (G_ "device '~a' not found: ~a~%")
+ device (strerror errno))
(unless (string-prefix? "/" device)
(display-hint (format #f (G_ "If '~a' is a file system
label, write @code{(file-system-label ~s)} in your @code{device} field.")
(let ((label (file-system-label->string
(file-system-device fs))))
(unless (find-partition-by-label label)
- (error (G_ "~a: error: file system with label '~a' not found~%")
- (file-system-location* fs) label))))
+ (error (file-system-location* fs)
+ (G_ "file system with label '~a' not found~%")
+ label))))
labeled)
(for-each (lambda (fs)
(unless (find-partition-by-uuid (file-system-device fs))
- (error (G_ "~a: error: file system with UUID '~a' not found~%")
- (file-system-location* fs)
+ (error (file-system-location* fs)
+ (G_ "file system with UUID '~a' not found~%")
(uuid->string (file-system-device fs)))))
uuid)
;;;
(define* (system-derivation-for-action os action
- #:key image-size file-system-type
+ #:key image-size image-type
full-boot? container-shared-network?
- mappings)
+ mappings label
+ volatile-root?)
"Return as a monadic value the derivation for OS according to ACTION."
- (case action
- ((build init reconfigure)
- (operating-system-derivation os))
- ((container)
- (container-script
- os
- #:mappings mappings
- #:shared-network? container-shared-network?))
- ((vm-image)
- (system-qemu-image os #:disk-image-size image-size))
- ((vm)
- (system-qemu-image/shared-store-script os
- #:full-boot? full-boot?
- #:disk-image-size
- (if full-boot?
- image-size
- (* 70 (expt 2 20)))
- #:mappings mappings))
- ((disk-image)
- (system-disk-image os
- #:name (match file-system-type
- ("iso9660" "image.iso")
- (_ "disk-image"))
- #:disk-image-size image-size
- #:file-system-type file-system-type))
- ((docker-image)
- (system-docker-image os))))
+ (mlet %store-monad ((target (current-target-system)))
+ (case action
+ ((build init reconfigure)
+ (operating-system-derivation os))
+ ((container)
+ (container-script
+ os
+ #:mappings mappings
+ #:shared-network? container-shared-network?))
+ ((vm-image)
+ (system-qemu-image os #:disk-image-size image-size))
+ ((vm)
+ (system-qemu-image/shared-store-script os
+ #:full-boot? full-boot?
+ #:disk-image-size
+ (if full-boot?
+ image-size
+ (* 70 (expt 2 20)))
+ #:mappings mappings))
+ ((disk-image)
+ (let* ((base-image (os->image os #:type image-type))
+ (base-target (image-target base-image)))
+ (lower-object
+ (system-image
+ (image
+ (inherit (if label
+ (image-with-label base-image label)
+ base-image))
+ (target (or base-target target))
+ (size image-size)
+ (operating-system os)
+ (volatile-root? volatile-root?))))))
+ ((docker-image)
+ (system-docker-image os
+ #:shared-network? container-shared-network?)))))
(define (maybe-suggest-running-guix-pull)
"Suggest running 'guix pull' if this has never been done before."
(define* (perform-action action os
#:key
+ (validate-reconfigure ensure-forward-reconfigure)
save-provenance?
skip-safety-checks?
install-bootloader?
dry-run? derivations-only?
use-substitutes? bootloader-target target
- image-size file-system-type full-boot?
- container-shared-network?
+ image-size image-type
+ volatile-root?
+ full-boot? label container-shared-network?
(mappings '())
(gc-root #f))
"Perform ACTION for OS. INSTALL-BOOTLOADER? specifies whether to install
bootloader; BOOTLOADER-TAGET is the target for the bootloader; TARGET is the
target root directory; IMAGE-SIZE is the size of the image to be built, for
-the 'vm-image' and 'disk-image' actions. The root file system is created as a
-FILE-SYSTEM-TYPE file system. FULL-BOOT? is used for the 'vm' action; it
-determines whether to boot directly to the kernel or to the bootloader.
-CONTAINER-SHARED-NETWORK? determines if the container will use a separate
-network namespace.
+the 'vm-image' and 'disk-image' actions. IMAGE-TYPE is the type of image to
+be built. When VOLATILE-ROOT? is #t, the root file system is mounted
+volatile.
+
+FULL-BOOT? is used for the 'vm' action; it determines whether to
+boot directly to the kernel or to the bootloader. CONTAINER-SHARED-NETWORK?
+determines if the container will use a separate network namespace.
When DERIVATIONS-ONLY? is true, print the derivation file name(s) without
building anything.
(operating-system-bootcfg os menu-entries)))
(when (eq? action 'reconfigure)
- (maybe-suggest-running-guix-pull))
+ (maybe-suggest-running-guix-pull)
+ (check-forward-update validate-reconfigure))
;; Check whether the declared file systems exist. This is better than
;; instantiating a broken configuration. Assume that we can only check if
(mlet* %store-monad
((sys (system-derivation-for-action os action
- #:file-system-type file-system-type
+ #:label label
+ #:image-type image-type
#:image-size image-size
+ #:volatile-root? volatile-root?
#:full-boot? full-boot?
#:container-shared-network? container-shared-network?
#:mappings mappings))
(info (G_ "bootloader successfully installed on '~a'~%")
(bootloader-configuration-target bootloader))))
(with-shepherd-error-handling
- (upgrade-shepherd-services local-eval os))))
+ (upgrade-shepherd-services local-eval os)
+ (return (format #t (G_ "\
+To complete the upgrade, run 'herd restart SERVICE' to stop,
+upgrade, and restart each service that was not automatically restarted.\n")))
+ (return (format #t (G_ "\
+Run 'herd status' to view the list of services on your system.\n"))))))
((init)
(newline)
(format #t (G_ "initializing operating system under '~a'...~%")
#:reverse-edges? #t)))
\f
+;;;
+;;; Images.
+;;;
+
+(define (list-image-types)
+ "Print the available image types."
+ (display (G_ "The available image types are:\n"))
+ (newline)
+ (format #t "~{ - ~a ~%~}" (map image-type-name (force %image-types))))
+
+\f
;;;
;;; Options.
;;;
(display (G_ "
-e, --expression=EXPR consider the operating-system EXPR evaluates to
instead of reading FILE, when applicable"))
+ (display (G_ "
+ --allow-downgrades for 'reconfigure', allow downgrades to earlier
+ channel revisions"))
(display (G_ "
--on-error=STRATEGY
apply STRATEGY (one of nothing-special, backtrace,
or debug) when an error occurs while reading FILE"))
(display (G_ "
- --file-system-type=TYPE
- for 'disk-image', produce a root file system of TYPE
- (one of 'ext4', 'iso9660')"))
+ --list-image-types list available image types"))
+ (display (G_ "
+ -t, --image-type=TYPE for 'disk-image', produce an image of TYPE"))
(display (G_ "
--image-size=SIZE for 'vm-image', produce an image of SIZE"))
(display (G_ "
--no-bootloader for 'init', do not install a bootloader"))
+ (display (G_ "
+ --volatile for 'disk-image', make the root file system volatile"))
+ (display (G_ "
+ --label=LABEL for 'disk-image', label disk image with LABEL"))
(display (G_ "
--save-provenance save provenance information"))
(display (G_ "
- --share=SPEC for 'vm', share host file system according to SPEC"))
+ --share=SPEC for 'vm' and 'container', share host file system with
+ read/write access according to SPEC"))
(display (G_ "
- --expose=SPEC for 'vm', expose host file system according to SPEC"))
+ --expose=SPEC for 'vm' and 'container', expose host file system
+ directory as read-only according to SPEC"))
(display (G_ "
-N, --network for 'container', allow containers to access the network"))
(display (G_ "
(option '(#\d "derivation") #f #f
(lambda (opt name arg result)
(alist-cons 'derivations-only? #t result)))
+ (option '("allow-downgrades") #f #f
+ (lambda (opt name arg result)
+ (alist-cons 'validate-reconfigure
+ warn-about-backward-reconfigure
+ result)))
(option '("on-error") #t #f
(lambda (opt name arg result)
(alist-cons 'on-error (string->symbol arg)
result)))
- (option '(#\t "file-system-type") #t #f
+ (option '(#\t "image-type") #t #f
(lambda (opt name arg result)
- (alist-cons 'file-system-type arg
+ (alist-cons 'image-type (string->symbol arg)
result)))
+ (option '("list-image-types") #f #f
+ (lambda (opt name arg result)
+ (list-image-types)
+ (exit 0)))
(option '("image-size") #t #f
(lambda (opt name arg result)
(alist-cons 'image-size (size->number arg)
(option '("no-bootloader" "no-grub") #f #f
(lambda (opt name arg result)
(alist-cons 'install-bootloader? #f result)))
+ (option '("volatile") #f #f
+ (lambda (opt name arg result)
+ (alist-cons 'volatile-root? #t result)))
+ (option '("label") #t #f
+ (lambda (opt name arg result)
+ (alist-cons 'label arg result)))
(option '("full-boot") #f #f
(lambda (opt name arg result)
(alist-cons 'full-boot? #t result)))
(graft? . #t)
(debug . 0)
(verbosity . #f) ;default
- (file-system-type . "ext4")
+ (validate-reconfigure . ,ensure-forward-reconfigure)
+ (image-type . raw)
(image-size . guess)
- (install-bootloader? . #t)))
+ (install-bootloader? . #t)
+ (label . #f)
+ (volatile-root? . #f)))
+
+(define (verbosity-level opts)
+ "Return the verbosity level based on OPTS, the alist of parsed options."
+ (or (assoc-ref opts 'verbosity)
+ (if (eq? (assoc-ref opts 'action) 'build)
+ 2 1)))
\f
;;;
(dry? (assoc-ref opts 'dry-run?))
(bootloader? (assoc-ref opts 'install-bootloader?))
+ (label (assoc-ref opts 'label))
(target-file (match args
((first second) second)
(_ #f)))
(with-build-handler (build-notifier #:use-substitutes?
(assoc-ref opts 'substitutes?)
+ #:verbosity
+ (verbosity-level opts)
#:dry-run?
(assoc-ref opts 'dry-run?))
(run-with-store store
#:use-substitutes? (assoc-ref opts 'substitutes?)
#:skip-safety-checks?
(assoc-ref opts 'skip-safety-checks?)
- #:file-system-type (assoc-ref opts 'file-system-type)
+ #:validate-reconfigure
+ (assoc-ref opts 'validate-reconfigure)
+ #:image-type (lookup-image-type-by-name
+ (assoc-ref opts 'image-type))
#:image-size (assoc-ref opts 'image-size)
+ #:volatile-root?
+ (assoc-ref opts 'volatile-root?)
#:full-boot? (assoc-ref opts 'full-boot?)
#:container-shared-network?
(assoc-ref opts 'container-shared-network?)
(_ #f))
opts)
#:install-bootloader? bootloader?
+ #:label label
#:target target-file
#:bootloader-target bootloader-target
#:gc-root (assoc-ref opts 'gc-root)))))
;; need an operating system configuration file.
(else (process-action command args opts))))
-(define (guix-system . args)
+(define-command (guix-system . args)
+ (synopsis "build and deploy full operating systems")
+
(define (parse-sub-command arg result)
;; Parse sub-command ARG and augment RESULT accordingly.
(if (assoc-ref result 'action)
(args (option-arguments opts))
(command (assoc-ref opts 'action)))
(parameterize ((%graft? (assoc-ref opts 'graft?)))
- (with-status-verbosity (or (assoc-ref opts 'verbosity)
- (if (eq? command 'build) 2 1))
+ (with-status-verbosity (verbosity-level opts)
(process-command command args opts))))))
;;; Local Variables: