aboutsummaryrefslogtreecommitdiff
;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2020 Ludovic Courtès <ludo@gnu.org>
;;; Copyright © 2020 Mathieu Othacehe <m.othacehe@gmail.com>
;;;
;;; This file is part of GNU Guix.
;;;
;;; GNU Guix is free software; you can redistribute it and/or modify it
;;; under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 3 of the License, or (at
;;; your option) any later version.
;;;
;;; GNU Guix is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with GNU Guix.  If not, see <http://www.gnu.org/licenses/>.

(define-module (gnu installer tests)
  #:use-module (srfi srfi-1)
  #:use-module (srfi srfi-34)
  #:use-module (srfi srfi-35)
  #:use-module (ice-9 match)
  #:use-module (ice-9 regex)
  #:use-module (ice-9 pretty-print)
  #:export (&pattern-not-matched
            pattern-not-matched?

            %installer-socket-file
            open-installer-socket

            converse
            conversation-log-port

            choose-locale+keyboard
            enter-host-name+passwords
            choose-services
            choose-partitioning
            start-installation
            complete-installation

            edit-configuration-file))

;;; Commentary:
;;;
;;; This module provides tools to test the guided "graphical" installer in a
;;; non-interactive fashion.  The core of it is 'converse': it allows you to
;;; state Expect-style dialogues, which happen over the Unix-domain socket the
;;; installer listens to.  Higher-level procedures such as
;;; 'choose-locale+keyboard' are provided to perform specific parts of the
;;; dialogue.
;;;
;;; Code:

(define %installer-socket-file
  ;; Socket the installer listens to.
  "/var/guix/installer-socket")

(define* (open-installer-socket #:optional (file %installer-socket-file))
  "Return a socket connected to the installer."
  (let ((sock (socket AF_UNIX SOCK_STREAM 0)))
    (connect sock AF_UNIX file)
    sock))

(define-condition-type &pattern-not-matched &error
  pattern-not-matched?
  (pattern pattern-not-matched-pattern)
  (sexp    pattern-not-matched-sexp))

(define (pattern-error pattern sexp)
  (raise (condition
          (&pattern-not-matched
           (pattern pattern) (sexp sexp)))))

(define conversation-log-port
  ;; Port where debugging info is logged
  (make-parameter (current-error-port)))

(define (converse-debug pattern)
  (format (conversation-log-port)
          "conversation expecting pattern ~s~%"
          pattern))

(define-syntax converse
  (lambda (s)
    "Convert over PORT: read sexps from there, match them against each
PATTERN, and send the corresponding REPLY.  Raise to '&pattern-not-matched'
when one of the PATTERNs is not matched."

    ;; XXX: Strings that appear in PATTERNs must be in the language the
    ;; installer is running in.  In the future, we should add support to allow
    ;; writing English strings in PATTERNs and have the pattern matcher
    ;; automatically translate them.

    ;; Here we emulate 'pmatch' syntax on top of 'match'.  This is ridiculous
    ;; but that's because 'pmatch' compares objects with 'eq?', making it
    ;; pretty useless, and it doesn't support ellipses and such.

    (define (quote-pattern s)
      ;; Rewrite the pattern S from pmatch style (a ,b) to match style like
      ;; ('a b).
      (with-ellipsis :::
        (syntax-case s (unquote _ ...)
          ((unquote id) #'id)
          (_ #'_)
          (... #'...)
          (id
           (identifier? #'id)
           #''id)
          ((lst :::) (map quote-pattern #'(lst :::)))
          (pattern #'pattern))))

    (define (match-pattern s)
      ;; Match one pattern without a guard.
      (syntax-case s ()
        ((port (pattern reply) continuation)
         (with-syntax ((pattern (quote-pattern #'pattern)))
           #'(let ((pat 'pattern))
               (converse-debug pat)
               (match (read port)
                 (pattern
                  (let ((data (call-with-values (lambda () reply)
                                list)))
                    (for-each (lambda (obj)
                                (write obj port)
                                (newline port))
                              data)
                    (force-output port)
                    (continuation port)))
                 (sexp
                  (pattern-error pat sexp))))))))

    (syntax-case s ()
      ((_ port (pattern reply) rest ...)
       (match-pattern #'(port (pattern reply)
                              (lambda (port)
                                (converse port rest ...)))))
      ((_ port (pattern guard reply) rest ...)
       #`(let ((skip? (not guard))
               (next  (lambda (p)
                        (converse p rest ...))))
           (if skip?
               (next port)
               #,(match-pattern #'(port (pattern reply) next)))))
      ((_ port)
       #t))))

(define* (choose-locale+keyboard port
                                 #:key
                                 (language "English")
                                 (location "Hong Kong")
                                 (timezone '("Europe" "Zagreb"))
                                 (keyboard
                                  '("English (US)"
                                    "English (intl., with AltGr dead keys)")))
  "Converse over PORT with the guided installer to choose the specified
LANGUAGE, LOCATION, TIMEZONE, and KEYBOARD."
  (converse port
    ((list-selection (title "Locale language")
                     (multiple-choices? #f)
                     (items _))
     language)
    ((list-selection (title "Locale location")
                     (multiple-choices? #f)
                     (items _))
     location)
    ((menu (title "GNU Guix install")
           (text _)
           (items (,guided _ ...)))           ;"Guided graphical installation"
     guided)
    ((list-selection (title "Timezone")
                     (multiple-choices? #f)
                     (items _))
     (first timezone))
    ((list-selection (title "Timezone")
                     (multiple-choices? #f)
                     (items _))
     (second timezone))
    ((list-selection (title "Layout")
                     (multiple-choices? #f)
                     (items _))
     (first keyboard))
    ((list-selection (title "Variant")
                     (multiple-choices? #f)
                     (items _))
     (second keyboard))))

(define* (enter-host-name+passwords port
                                    #:key
                                    (host-name "guix")
                                    (root-password "foo")
                                    (users '(("alice" "pass1")
                                             ("bob" "pass2")
                                             ("charlie" "pass3"))))
  "Converse over PORT with the guided installer to choose HOST-NAME,
ROOT-PASSWORD, and USERS."
  (converse port
    ((input (title "Hostname") (text _) (default _))
     host-name)
    ((input (title "System administrator password") (text _) (default _))
     root-password)
    ((input (title "Password confirmation required") (text _) (default _))
     root-password)
    ((add-users)
     (match users
       (((names passwords) ...)
        (map (lambda (name password)
               `(user (name ,name) (real-name ,(string-titlecase name))
                      (home-directory ,(string-append "/home/" name))
                      (password ,password)))
             names passwords))))))

(define* (choose-services port
                          #:key
                          (choose-desktop-environment? (const #f))
                          (choose-network-service?
                           (lambda (service)
                             (or (string-contains service "SSH")
                                 (string-contains service "NSS"))))
                          (choose-network-management-tool?
                           (lambda (service)
                             (string-contains service "DHCP")))
                          (choose-misc-service?
                           (lambda (service)
                             (string-contains service "NTP")))
                          (choose-other-service? (const #f)))

  "Converse over PORT to choose services."
  (define desktop-environments '())

  (converse port
    ((checkbox-list (title "Desktop environment") (text _)
                    (items ,services))
     (let ((desktops (filter choose-desktop-environment? services)))
       (set! desktop-environments desktops)
       desktops))
    ((checkbox-list (title "Network service") (text _)
                    (items ,services))
     (filter choose-network-service? services))

    ;; The "Network management" dialog shows up only when no desktop
    ;; environments have been selected, hence the guard.
    ((list-selection (title "Network management")
                     (multiple-choices? #f)
                     (items ,services))
     (null? desktop-environments)
     (find choose-network-management-tool? services))

    ((checkbox-list (title "Console services") (text _)
                    (items ,services))
     (null? desktop-environments)
     (filter choose-misc-service? services))

    ((checkbox-list (title "Printing and document services") (text _)
                    (items ,services))
     (filter choose-other-service? services))))

(define (edit-configuration-file file)
  "Edit FILE, an operating system configuration file generated by the
installer, by adding a marionette service such that the installed OS is
instrumented for further testing."
  (define (read-expressions port)
    (let loop ((result '()))
      (match (read port)
        ((? eof-object?)
         (reverse result))
        (exp
         (loop (cons exp result))))))

  (define (edit exp)
    (match exp
      (('operating-system _ ...)
       `(marionette-operating-system ,exp
                                     #:imported-modules
                                     '((gnu services herd)
                                       (guix build utils)
                                       (guix combinators))))
      (_
       exp)))

  (let ((content (call-with-input-file file read-expressions)))
    ;; XXX: Remove the file before re-writing it, to be sure there are no
    ;; leftovers.  We shouldn't have to do that as CALL-WITH-OUTPUT-FILE uses
    ;; the O_TRUNC flag by default.
    (delete-file file)
    (call-with-output-file file
      (lambda (port)
        (format port "\
;; Operating system configuration edited for automated testing.~%~%")

        (pretty-print '(use-modules (gnu tests)) port)
        (for-each (lambda (exp)
                    (pretty-print (edit exp) port)
                    (newline port))
                  content)))

    #t))

(define* (choose-partitioning port
                              #:key
                              (encrypted? #t)
                              (uefi-support? #f)
                              (passphrase "thepassphrase")
                              (edit-configuration-file
                               edit-configuration-file))
  "Converse over PORT to choose the partitioning method.  When ENCRYPTED? is
true, choose full-disk encryption with PASSPHRASE as the LUKS passphrase.

When UEFI-SUPPORT? is true, assume that we are running the installation tests
on an UEFI capable machine.

This conversation stops when the user partitions have been formatted, right
before the installer generates the configuration file and shows it in a dialog
box. "
  (converse port
    ((list-selection (title "Partitioning method")
                     (multiple-choices? #f)
                     (items (,not-encrypted ,encrypted _ ...)))
     (if encrypted?
         encrypted
         not-encrypted))
    ((list-selection (title "Disk") (multiple-choices? #f)
                     (items (,disks ...)))
     ;; When running the installation from an ISO image, the CD/DVD drive
     ;; shows up in the list.  Avoid it.
     (find (lambda (disk)
             (not (or (string-contains disk "DVD")
                      (string-contains disk "CD-ROM"))))
           disks))

    ;; The "Partition table" dialog pops up only if there's not already a
    ;; partition table and if the system does not support UEFI.
    ((list-selection (title "Partition table")
                     (multiple-choices? #f)
                     (items _))
     ;; When UEFI is supported, the partition is forced to GPT by the
     ;; installer.
     (not uefi-support?)
     "gpt")

    ((list-selection (title "Partition scheme")
                     (multiple-choices? #f)
                     (items (,one-partition _ ...)))
     one-partition)
    ((list-selection (title "Guided partitioning")
                     (multiple-choices? #f)
                     (items (,disk _ ...)))
     disk)
    ((input (title "Password required")
            (text _) (default #f))
     encrypted?                                   ;only when ENCRYPTED?
     passphrase)
    ((input (title "Password confirmation required")
            (text _) (default #f))
     encrypted?
     passphrase)
    ((confirmation (title "Format disk?") (text _))
     #t)
    ((info (title "Preparing partitions") _ ...)
     (values))                                    ;nothing to return
    ((starting-final-step)
     ;; Do not return anything.  The reply will be sent by
     ;; 'conclude-installation' and in the meantime the installer just waits
     ;; for us, giving us a chance to do things such as changing partition
     ;; UUIDs before it generates the configuration file.
     (values))))

(define (start-installation port)
  "Start the installation by checking over PORT that we get the generated
configuration file, accepting it and starting the installation, and then
receiving the pause message once the 'guix system init' process has
completed."
  ;; Assume the previous message received was 'starting-final-step'; here we
  ;; send the reply to that message, which lets the installer continue.
  (write #t port)
  (newline port)
  (force-output port)

  (converse port
    ((file-dialog (title "Configuration file")
                  (text _)
                  (file ,configuration-file))
     (edit-configuration-file configuration-file))
    ((pause)                                      ;"Press Enter to continue."
     (values))))

(define (complete-installation port)
  "Complete the installation by replying to the installer pause message and
waiting for the installation-complete message."
  ;; Assume the previous message received was 'pause'; here we send the reply
  ;; to that message, which lets the installer continue.
  (write #t port)
  (newline port)
  (force-output port)

  (converse port
    ((installation-complete)
     (values))))

;;; Local Variables:
;;; eval: (put 'converse 'scheme-indent-function 1)
;;; eval: (put 'with-ellipsis 'scheme-indent-function 1)
;;; End:
;; Use the Avahi daemon to discover substitute servers on the local ;; network. It can be faster than fetching from remote servers. (service avahi-service-type) ;; The build daemon. (service guix-service-type (guix-configuration ;; Register the default substitute server key(s) as ;; trusted to allow the installation process to use ;; substitutes by default. (authorize-key? #t) ;; Install and run the current Guix rather than an older ;; snapshot. (guix (current-guix)))) ;; Start udev so that useful device nodes are available. ;; Use device-mapper rules for cryptsetup & co; enable the CRDA for ;; regulations-compliant WiFi access. (service udev-service-type (udev-configuration (rules (list lvm2 crda)))) ;; Add the 'cow-store' service, which users have to start manually ;; since it takes the installation directory as an argument. (cow-store-service) ;; Install Unicode support and a suitable font. (service console-font-service-type (map (match-lambda ("tty2" ;; Use a font that contains characters such as ;; curly quotes as found in the manual. '("tty2" . "LatGrkCyr-8x16")) (tty ;; Use a font that doesn't have more than 256 ;; glyphs so that we can use colors with varying ;; brightness levels (see note in setfont(8)). `(,tty . "lat9u-16"))) '("tty1" "tty2" "tty3" "tty4" "tty5" "tty6"))) ;; To facilitate copy/paste. (service gpm-service-type) ;; Add an SSH server to facilitate remote installs. (service openssh-service-type (openssh-configuration (port-number 22) (permit-root-login #t) ;; The root account is passwordless, so make sure ;; a password is set before allowing logins. (allow-empty-passwords? #f) (password-authentication? #t) ;; Don't start it upfront. (%auto-start? #f))) ;; Since this is running on a USB stick with a overlayfs as the root ;; file system, use an appropriate cache configuration. (service nscd-service-type (nscd-configuration (caches %nscd-minimal-caches))) ;; Having /bin/sh is a good idea. In particular it allows Tramp ;; connections to this system to work. (service special-files-service-type `(("/bin/sh" ,(file-append bash "/bin/sh")))) ;; Loopback device, needed by OpenSSH notably. (service static-networking-service-type (list %loopback-static-networking)) (service wpa-supplicant-service-type) (service dbus-root-service-type) (service connman-service-type (connman-configuration (disable-vpn? #t))) ;; Keep a reference to BARE-BONES-OS to make sure it can be ;; installed without downloading/building anything. Also keep the ;; things needed by 'profile-derivation' to minimize the amount of ;; download. (service gc-root-service-type (append (list bare-bones-os (libc-utf8-locales-for-target system) texinfo guile-3.0) %default-locale-libcs))) ;; Specific system services ;; Machines without Kernel Mode Setting (those with many old and ;; current AMD GPUs, SiS GPUs, ...) need uvesafb to show the GUI ;; installer. Some may also need a kernel parameter like nomodeset ;; or vga=793, but we leave that for the user to specify in GRUB. `(,@(if (supported-package? v86d system) (list (service uvesafb-service-type)) '()))))) (define %issue ;; Greeting. " \x1b[1;37mThis is an installation image of the GNU system. Welcome.\x1b[0m \x1b[1;33mUse Alt-F2 for documentation.\x1b[0m ") (define %installer-disk-utilities ;; A well-rounded set of packages for interacting with disks, partitions and ;; file systems, included with the Guix installation image. (list parted gptfdisk ddrescue ;; Use the static LVM2 because it's already pulled in by the installer. lvm2-static ;; We used to provide fdisk from GNU fdisk, but as of version 2.0.0a ;; it pulls Guile 1.8, which takes unreasonable space; furthermore ;; util-linux's fdisk is already available, in %base-packages-linux. cryptsetup mdadm dosfstools btrfs-progs e2fsprogs f2fs-tools jfsutils xfsprogs)) (define installation-os ;; The operating system used on installation images for USB sticks etc. (operating-system (host-name "gnu") (timezone "Europe/Paris") (locale "en_US.utf8") (name-service-switch %mdns-host-lookup-nss) (bootloader (bootloader-configuration (bootloader grub-bootloader) (targets '("/dev/sda")))) (label (string-append "GNU Guix installation " (or (getenv "GUIX_DISPLAYED_VERSION") (package-version guix)))) ;; XXX: The AMD Radeon driver is reportedly broken, which makes kmscon ;; non-functional: ;; <https://lists.gnu.org/archive/html/guix-devel/2019-03/msg00441.html>. ;; Thus, blacklist it. (kernel-arguments '("quiet" "modprobe.blacklist=radeon,amdgpu")) (file-systems ;; Note: the disk image build code overrides this root file system with ;; the appropriate one. (append %base-live-file-systems ;; XXX: This should be %BASE-FILE-SYSTEMS but we don't need ;; elogind's cgroup file systems. (list %pseudo-terminal-file-system %shared-memory-file-system %efivars-file-system %immutable-store))) (users (list (user-account (name "guest") (group "users") (supplementary-groups '("wheel")) ; allow use of sudo (password "") (comment "Guest of GNU")))) (issue %issue) (services (%installation-services)) ;; We don't need setuid programs, except for 'passwd', which can be handy ;; if one is to allow remote SSH login to the machine being installed. (privileged-programs (list (privileged-program (program (file-append shadow "/bin/passwd")) (setuid? #t)))) (pam-services ;; Explicitly allow for empty passwords. (base-pam-services #:allow-empty-passwords? #t)) (packages (append (list glibc ; for 'tzselect' & co. fontconfig font-dejavu font-gnu-unifont grub) ; mostly so xrefs to its manual work %installer-disk-utilities %base-packages)))) (define* (os-with-u-boot os board #:key (bootloader-target "/dev/mmcblk0") (triplet "arm-linux-gnueabihf")) "Given OS, amend it with the u-boot bootloader for BOARD, installed to BOOTLOADER-TARGET (a drive), compiled for TRIPLET. If you want a serial console, make sure to specify one in your operating-system's kernel-arguments (\"console=ttyS0\" or similar)." (operating-system (inherit os) (bootloader (bootloader-configuration (bootloader (bootloader (inherit u-boot-bootloader) (package (make-u-boot-package board triplet)))) (targets (list bootloader-target)))))) (define* (embedded-installation-os bootloader bootloader-target tty #:key (extra-modules '())) "Return an installation os for embedded systems. The initrd gets the extra modules EXTRA-MODULES. A getty is provided on TTY. The bootloader BOOTLOADER is installed to BOOTLOADER-TARGET." (operating-system (inherit installation-os) (bootloader (bootloader-configuration (bootloader bootloader) (targets (list bootloader-target)))) (kernel linux-libre) (kernel-arguments (cons (string-append "console=" tty) (operating-system-user-kernel-arguments installation-os))) (initrd-modules (append extra-modules %base-initrd-modules)))) (define beaglebone-black-installation-os (embedded-installation-os u-boot-beaglebone-black-bootloader "/dev/sda" "ttyO0" #:extra-modules ;; This module is required to mount the sd card. '("omap_hsmmc"))) (define a20-olinuxino-lime-installation-os (embedded-installation-os u-boot-a20-olinuxino-lime-bootloader "/dev/mmcblk0" ; SD card storage "ttyS0")) (define a20-olinuxino-lime2-emmc-installation-os (embedded-installation-os u-boot-a20-olinuxino-lime2-bootloader "/dev/mmcblk1" ; eMMC storage "ttyS0")) (define a20-olinuxino-micro-installation-os (embedded-installation-os u-boot-a20-olinuxino-micro-bootloader "/dev/mmcblk0" ; SD card storage "ttyS0")) (define bananapi-m2-ultra-installation-os (embedded-installation-os u-boot-bananapi-m2-ultra-bootloader "/dev/mmcblk1" ; eMMC storage "ttyS0")) (define firefly-rk3399-installation-os (embedded-installation-os u-boot-firefly-rk3399-bootloader "/dev/mmcblk0" ; SD card/eMMC (SD priority) storage "ttyS2")) ; UART2 connected on the Pi2 bus (define mx6cuboxi-installation-os (embedded-installation-os u-boot-mx6cuboxi-bootloader "/dev/mmcblk0" ; SD card storage "ttymxc0")) (define novena-installation-os (embedded-installation-os u-boot-novena-bootloader "/dev/mmcblk1" ; SD card storage "ttymxc1")) (define nintendo-nes-classic-edition-installation-os (embedded-installation-os u-boot-nintendo-nes-classic-edition-bootloader "/dev/mmcblk0" ; SD card (solder it yourself) "ttyS0")) (define orangepi-r1-plus-lts-rk3328-installation-os (embedded-installation-os u-boot-orangepi-r1-plus-lts-rk3328-bootloader "/dev/mmcblk0" ; SD card storage "ttyS0")) (define pine64-plus-installation-os (embedded-installation-os u-boot-pine64-plus-bootloader "/dev/mmcblk0" ; SD card storage "ttyS0")) (define pinebook-installation-os (embedded-installation-os u-boot-pinebook-bootloader "/dev/mmcblk0" ; SD card storage "ttyS0")) (define rock64-installation-os (embedded-installation-os u-boot-rock64-rk3328-bootloader "/dev/mmcblk0" ; SD card/eMMC (SD priority) storage "ttyS2")) ; UART2 connected on the Pi2 bus (define rockpro64-installation-os (embedded-installation-os u-boot-rockpro64-rk3399-bootloader "/dev/mmcblk0" ; SD card/eMMC (SD priority) storage "ttyS2")) ; UART2 connected on the Pi2 bus (define rk3399-puma-installation-os (embedded-installation-os u-boot-puma-rk3399-bootloader "/dev/mmcblk0" ; SD card storage "ttyS0")) (define wandboard-installation-os (embedded-installation-os u-boot-wandboard-bootloader "/dev/mmcblk0" ; SD card storage "ttymxc0")) ;; Return the default os here so 'guix system' can consume it directly. installation-os ;;; install.scm ends here