1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2016, 2017 Ludovic Courtès <ludo@gnu.org>
4 ;;; This file is part of GNU Guix.
6 ;;; GNU Guix is free software; you can redistribute it and/or modify it
7 ;;; under the terms of the GNU General Public License as published by
8 ;;; the Free Software Foundation; either version 3 of the License, or (at
9 ;;; your option) any later version.
11 ;;; GNU Guix is distributed in the hope that it will be useful, but
12 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ;;; GNU General Public License for more details.
16 ;;; You should have received a copy of the GNU General Public License
17 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
19 (define-module (gnu tests install)
21 #:use-module (gnu bootloader extlinux)
22 #:use-module (gnu tests)
23 #:use-module (gnu tests base)
24 #:use-module (gnu system)
25 #:use-module (gnu system install)
26 #:use-module (gnu system vm)
27 #:use-module ((gnu build vm) #:select (qemu-command))
28 #:use-module (gnu packages bootloaders)
29 #:use-module (gnu packages ocr)
30 #:use-module (gnu packages package-management)
31 #:use-module (gnu packages virtualization)
32 #:use-module (guix store)
33 #:use-module (guix monads)
34 #:use-module (guix packages)
35 #:use-module (guix grafts)
36 #:use-module (guix gexp)
37 #:use-module (guix utils)
38 #:export (%test-installed-os
39 %test-installed-extlinux-os
40 %test-iso-image-installer
41 %test-separate-store-os
42 %test-separate-home-os
49 ;;; Test the installation of GuixSD using the documented approach at the
54 (define-os-with-source (%minimal-os %minimal-os-source)
55 ;; The OS we want to install.
56 (use-modules (gnu) (gnu tests) (srfi srfi-1))
59 (host-name "liberigilo")
60 (timezone "Europe/Paris")
61 (locale "en_US.UTF-8")
63 (bootloader (grub-configuration (target "/dev/vdb")))
64 (kernel-arguments '("console=ttyS0"))
65 (file-systems (cons (file-system
71 (users (cons (user-account
73 (comment "Bob's sister")
75 (supplementary-groups '("wheel" "audio" "video"))
76 (home-directory "/home/alice"))
78 (services (cons (service marionette-service-type
79 (marionette-configuration
80 (imported-modules '((gnu services herd)
81 (guix combinators)))))
84 (define (operating-system-add-packages os packages)
85 "Append PACKAGES to OS packages list."
88 (packages (append packages (operating-system-packages os)))))
90 (define-os-with-source (%minimal-extlinux-os
91 %minimal-extlinux-os-source)
92 (use-modules (gnu) (gnu tests) (gnu bootloader extlinux)
96 (host-name "liberigilo")
97 (timezone "Europe/Paris")
98 (locale "en_US.UTF-8")
100 (bootloader (bootloader-configuration
101 (bootloader extlinux-bootloader-gpt)
102 (target "/dev/vdb")))
103 (kernel-arguments '("console=ttyS0"))
104 (file-systems (cons (file-system
110 (services (cons (service marionette-service-type
111 (marionette-configuration
112 (imported-modules '((gnu services herd)
113 (guix combinators)))))
116 (define (operating-system-with-current-guix os)
117 "Return a variant of OS that uses the current Guix."
120 (services (modify-services (operating-system-user-services os)
121 (guix-service-type config =>
124 (guix (current-guix))))))))
126 (define (operating-system-with-gc-roots os roots)
127 "Return a variant of OS where ROOTS are registered as GC roots."
131 ;; We use this procedure for the installation OS, which already defines GC
132 ;; roots. Add ROOTS to those.
133 (services (cons (simple-service 'extra-root
134 gc-root-service-type roots)
135 (operating-system-user-services os)))))
138 (define MiB (expt 2 20))
140 (define %simple-installation-script
141 ;; Shell script of a simple installation.
147 export GUIX_BUILD_OPTIONS=--no-grafts
149 parted --script /dev/vdb mklabel gpt \\
150 mkpart primary ext2 1M 3M \\
151 mkpart primary ext2 3M 1G \\
154 mkfs.ext4 -L my-root /dev/vdb2
157 herd start cow-store /mnt
159 cp /etc/target-config.scm /mnt/etc/config.scm
160 guix system init /mnt/etc/config.scm /mnt --no-substitutes
164 (define %extlinux-gpt-installation-script
165 ;; Shell script of a simple installation.
166 ;; As syslinux 6.0.3 does not handle 64bits ext4 partitions,
167 ;; we make sure to pass -O '^64bit' to mkfs.
173 export GUIX_BUILD_OPTIONS=--no-grafts
175 parted --script /dev/vdb mklabel gpt \\
178 mkfs.ext4 -L my-root -O '^64bit' /dev/vdb1
181 herd start cow-store /mnt
183 cp /etc/target-config.scm /mnt/etc/config.scm
184 guix system init /mnt/etc/config.scm /mnt --no-substitutes
188 (define* (run-install target-os target-os-source
190 (script %simple-installation-script)
192 (os (marionette-operating-system
194 ;; Since the image has no network access, use the
195 ;; current Guix so the store items we need are in
196 ;; the image and add packages provided.
197 (inherit (operating-system-add-packages
198 (operating-system-with-current-guix
201 (kernel-arguments '("console=ttyS0")))
202 #:imported-modules '((gnu services herd)
203 (guix combinators))))
204 (installation-disk-image-file-system-type "ext4")
205 (target-size (* 1200 MiB)))
206 "Run SCRIPT (a shell script following the GuixSD installation procedure) in
207 OS to install TARGET-OS. Return a VM image of TARGET-SIZE bytes containing
208 the installed system. The packages specified in PACKAGES will be appended to
209 packages defined in installation-os."
211 (mlet* %store-monad ((_ (set-grafting #f))
212 (system (current-system))
213 (target (operating-system-derivation target-os))
215 ;; Since the installation system has no network access,
216 ;; we cheat a little bit by adding TARGET to its GC
217 ;; roots. This way, we know 'guix system init' will
219 (image (system-disk-image
220 (operating-system-with-gc-roots
222 #:disk-image-size (* 1500 MiB)
224 installation-disk-image-file-system-type)))
226 (with-imported-modules '((guix build utils)
227 (gnu build marionette))
229 (use-modules (guix build utils)
230 (gnu build marionette))
232 (set-path-environment-variable "PATH" '("bin")
233 (list #$qemu-minimal))
235 (system* "qemu-img" "create" "-f" "qcow2"
236 #$output #$(number->string target-size))
240 `(,(which #$(qemu-command system))
244 ((string=? "ext4" installation-disk-image-file-system-type)
246 ,(string-append "file=" #$image
247 ",if=virtio,readonly")))
248 ((string=? "iso9660" installation-disk-image-file-system-type)
249 #~("-cdrom" #$image))
252 "unsupported installation-disk-image-file-system-type:"
253 installation-disk-image-file-system-type)))
255 ,(string-append "file=" #$output ",if=virtio")
256 ,@(if (file-exists? "/dev/kvm")
260 (pk 'uname (marionette-eval '(uname) marionette))
263 (marionette-eval '(begin
264 (use-modules (gnu services herd))
268 (marionette-eval '(call-with-output-file "/etc/target-config.scm"
270 (write '#$target-os-source port)))
273 (exit (marionette-eval '(zero? (system #$script))
276 (gexp->derivation "installation" install)))
278 (define* (qemu-command/writable-image image #:key (memory-size 256))
279 "Return as a monadic value the command to run QEMU on a writable copy of
280 IMAGE, a disk image. The QEMU VM is has access to MEMORY-SIZE MiB of RAM."
281 (mlet %store-monad ((system (current-system)))
282 (return #~(let ((image #$image))
283 ;; First we need a writable copy of the image.
284 (format #t "creating writable image from '~a'...~%" image)
285 (unless (zero? (system* #+(file-append qemu-minimal
287 "create" "-f" "qcow2"
289 (string-append "backing_file=" image)
291 (error "failed to create writable QEMU image" image))
293 (chmod "disk.img" #o644)
294 `(,(string-append #$qemu-minimal "/bin/"
295 #$(qemu-command system))
296 ,@(if (file-exists? "/dev/kvm")
299 "-no-reboot" "-m" #$(number->string memory-size)
300 "-drive" "file=disk.img,if=virtio")))))
302 (define %test-installed-os
304 (name "installed-os")
306 "Test basic functionality of an OS installed like one would do by hand.
307 This test is expensive in terms of CPU and storage usage since we need to
308 build (current-guix) and then store a couple of full system images.")
310 (mlet* %store-monad ((image (run-install %minimal-os %minimal-os-source))
311 (command (qemu-command/writable-image image)))
312 (run-basic-test %minimal-os command
315 (define %test-installed-extlinux-os
317 (name "installed-extlinux-os")
319 "Test basic functionality of an OS booted with an extlinux bootloader. As
320 per %test-installed-os, this test is expensive in terms of CPU and storage.")
322 (mlet* %store-monad ((image (run-install %minimal-extlinux-os
323 %minimal-extlinux-os-source
327 %extlinux-gpt-installation-script))
328 (command (qemu-command/writable-image image)))
329 (run-basic-test %minimal-extlinux-os command
330 "installed-extlinux-os")))))
334 ;;; Installation through an ISO image.
337 (define-os-with-source (%minimal-os-on-vda %minimal-os-on-vda-source)
338 ;; The OS we want to install.
339 (use-modules (gnu) (gnu tests) (srfi srfi-1))
342 (host-name "liberigilo")
343 (timezone "Europe/Paris")
344 (locale "en_US.UTF-8")
346 (bootloader (grub-configuration (target "/dev/vda")))
347 (kernel-arguments '("console=ttyS0"))
348 (file-systems (cons (file-system
354 (users (cons (user-account
356 (comment "Bob's sister")
358 (supplementary-groups '("wheel" "audio" "video"))
359 (home-directory "/home/alice"))
360 %base-user-accounts))
361 (services (cons (service marionette-service-type
362 (marionette-configuration
363 (imported-modules '((gnu services herd)
364 (guix combinators)))))
367 (define %simple-installation-script-for-/dev/vda
368 ;; Shell script of a simple installation.
374 export GUIX_BUILD_OPTIONS=--no-grafts
376 parted --script /dev/vda mklabel gpt \\
377 mkpart primary ext2 1M 3M \\
378 mkpart primary ext2 3M 1G \\
381 mkfs.ext4 -L my-root /dev/vda2
384 herd start cow-store /mnt
386 cp /etc/target-config.scm /mnt/etc/config.scm
387 guix system init /mnt/etc/config.scm /mnt --no-substitutes
391 (define %test-iso-image-installer
393 (name "iso-image-installer")
397 (mlet* %store-monad ((image (run-install
399 %minimal-os-on-vda-source
401 %simple-installation-script-for-/dev/vda
402 #:installation-disk-image-file-system-type
404 (command (qemu-command/writable-image image)))
405 (run-basic-test %minimal-os-on-vda command name)))))
412 (define-os-with-source (%separate-home-os %separate-home-os-source)
413 ;; The OS we want to install.
414 (use-modules (gnu) (gnu tests) (srfi srfi-1))
417 (host-name "liberigilo")
418 (timezone "Europe/Paris")
419 (locale "en_US.utf8")
421 (bootloader (grub-configuration (target "/dev/vdb")))
422 (kernel-arguments '("console=ttyS0"))
423 (file-systems (cons* (file-system
432 (mount-point "/home")
435 (users (cons* (user-account
438 (home-directory "/home/alice"))
442 (home-directory "/home/charlie"))
443 %base-user-accounts))
444 (services (cons (service marionette-service-type
445 (marionette-configuration
446 (imported-modules '((gnu services herd)
447 (guix combinators)))))
450 (define %test-separate-home-os
452 (name "separate-home-os")
454 "Test basic functionality of an installed OS with a separate /home
455 partition. In particular, home directories must be correctly created (see
456 <https://bugs.gnu.org/21108>).")
458 (mlet* %store-monad ((image (run-install %separate-home-os
459 %separate-home-os-source
461 %simple-installation-script))
462 (command (qemu-command/writable-image image)))
463 (run-basic-test %separate-home-os command "separate-home-os")))))
467 ;;; Separate /gnu/store partition.
470 (define-os-with-source (%separate-store-os %separate-store-os-source)
471 ;; The OS we want to install.
472 (use-modules (gnu) (gnu tests) (srfi srfi-1))
475 (host-name "liberigilo")
476 (timezone "Europe/Paris")
477 (locale "en_US.UTF-8")
479 (bootloader (grub-configuration (target "/dev/vdb")))
480 (kernel-arguments '("console=ttyS0"))
481 (file-systems (cons* (file-system
492 (users %base-user-accounts)
493 (services (cons (service marionette-service-type
494 (marionette-configuration
495 (imported-modules '((gnu services herd)
496 (guix combinators)))))
499 (define %separate-store-installation-script
500 ;; Installation with a separate /gnu partition.
506 export GUIX_BUILD_OPTIONS=--no-grafts
508 parted --script /dev/vdb mklabel gpt \\
509 mkpart primary ext2 1M 3M \\
510 mkpart primary ext2 3M 100M \\
511 mkpart primary ext2 100M 1G \\
514 mkfs.ext4 -L root-fs /dev/vdb2
515 mkfs.ext4 -L store-fs /dev/vdb3
518 mount /dev/vdb3 /mnt/gnu
520 herd start cow-store /mnt
522 cp /etc/target-config.scm /mnt/etc/config.scm
523 guix system init /mnt/etc/config.scm /mnt --no-substitutes
527 (define %test-separate-store-os
529 (name "separate-store-os")
531 "Test basic functionality of an OS installed like one would do by hand,
532 where /gnu lives on a separate partition.")
534 (mlet* %store-monad ((image (run-install %separate-store-os
535 %separate-store-os-source
537 %separate-store-installation-script))
538 (command (qemu-command/writable-image image)))
539 (run-basic-test %separate-store-os command "separate-store-os")))))
543 ;;; RAID root device.
546 (define-os-with-source (%raid-root-os %raid-root-os-source)
547 ;; An OS whose root partition is a RAID partition.
548 (use-modules (gnu) (gnu tests))
551 (host-name "raidified")
552 (timezone "Europe/Paris")
553 (locale "en_US.utf8")
555 (bootloader (grub-configuration (target "/dev/vdb")))
556 (kernel-arguments '("console=ttyS0"))
557 (initrd (lambda (file-systems . rest)
558 ;; Add a kernel module for RAID-0 (aka. "stripe").
559 (apply base-initrd file-systems
560 #:extra-modules '("raid0")
562 (mapped-devices (list (mapped-device
563 (source (list "/dev/vda2" "/dev/vda3"))
565 (type raid-device-mapping))))
566 (file-systems (cons (file-system
571 (dependencies mapped-devices))
573 (users %base-user-accounts)
574 (services (cons (service marionette-service-type
575 (marionette-configuration
576 (imported-modules '((gnu services herd)
577 (guix combinators)))))
580 (define %raid-root-installation-script
581 ;; Installation with a separate /gnu partition. See
582 ;; <https://raid.wiki.kernel.org/index.php/RAID_setup> for more on RAID and
589 export GUIX_BUILD_OPTIONS=--no-grafts
590 parted --script /dev/vdb mklabel gpt \\
591 mkpart primary ext2 1M 3M \\
592 mkpart primary ext2 3M 600M \\
593 mkpart primary ext2 600M 1200M \\
596 mdadm --create /dev/md0 --verbose --level=stripe --raid-devices=2 \\
598 mkfs.ext4 -L root-fs /dev/md0
601 herd start cow-store /mnt
603 cp /etc/target-config.scm /mnt/etc/config.scm
604 guix system init /mnt/etc/config.scm /mnt --no-substitutes
608 (define %test-raid-root-os
610 (name "raid-root-os")
612 "Test functionality of an OS installed with a RAID root partition managed
615 (mlet* %store-monad ((image (run-install %raid-root-os
618 %raid-root-installation-script
619 #:target-size (* 1300 MiB)))
620 (command (qemu-command/writable-image image)))
621 (run-basic-test %raid-root-os
622 `(,@command) "raid-root-os")))))
626 ;;; LUKS-encrypted root file system.
629 (define-os-with-source (%encrypted-root-os %encrypted-root-os-source)
630 ;; The OS we want to install.
631 (use-modules (gnu) (gnu tests) (srfi srfi-1))
634 (host-name "liberigilo")
635 (timezone "Europe/Paris")
636 (locale "en_US.UTF-8")
638 (bootloader (grub-configuration (target "/dev/vdb")))
640 ;; Note: Do not pass "console=ttyS0" so we can use our passphrase prompt
641 ;; detection logic in 'enter-luks-passphrase'.
643 (mapped-devices (list (mapped-device
644 (source (uuid "12345678-1234-1234-1234-123456789abc"))
645 (target "the-root-device")
646 (type luks-device-mapping))))
647 (file-systems (cons (file-system
648 (device "/dev/mapper/the-root-device")
653 (users (cons (user-account
656 (home-directory "/home/charlie")
657 (supplementary-groups '("wheel" "audio" "video")))
658 %base-user-accounts))
659 (services (cons (service marionette-service-type
660 (marionette-configuration
661 (imported-modules '((gnu services herd)
662 (guix combinators)))))
665 (define %encrypted-root-installation-script
666 ;; Shell script of a simple installation.
672 export GUIX_BUILD_OPTIONS=--no-grafts
673 ls -l /run/current-system/gc-roots
674 parted --script /dev/vdb mklabel gpt \\
675 mkpart primary ext2 1M 3M \\
676 mkpart primary ext2 3M 1G \\
679 echo -n thepassphrase | \\
680 cryptsetup luksFormat --uuid=12345678-1234-1234-1234-123456789abc -q /dev/vdb2 -
681 echo -n thepassphrase | \\
682 cryptsetup open --type luks --key-file - /dev/vdb2 the-root-device
683 mkfs.ext4 -L my-root /dev/mapper/the-root-device
684 mount LABEL=my-root /mnt
685 herd start cow-store /mnt
687 cp /etc/target-config.scm /mnt/etc/config.scm
688 guix system build /mnt/etc/config.scm
689 guix system init /mnt/etc/config.scm /mnt --no-substitutes
693 (define (enter-luks-passphrase marionette)
694 "Return a gexp to be inserted in the basic system test running on MARIONETTE
695 to enter the LUKS passphrase."
696 (let ((ocrad (file-append ocrad "/bin/ocrad")))
698 (define (passphrase-prompt? text)
699 (string-contains (pk 'screen-text text) "Enter pass"))
701 (define (bios-boot-screen? text)
702 ;; Return true if TEXT corresponds to the boot screen, before GRUB's
704 (string-prefix? "SeaBIOS" text))
706 (test-assert "enter LUKS passphrase for GRUB"
708 ;; At this point we have no choice but to use OCR to determine
709 ;; when the passphrase should be entered.
710 (wait-for-screen-text #$marionette passphrase-prompt?
712 (marionette-type "thepassphrase\n" #$marionette)
714 ;; Now wait until we leave the boot screen. This is necessary so
715 ;; we can then be sure we match the "Enter passphrase" prompt from
716 ;; 'cryptsetup', in the initrd.
717 (wait-for-screen-text #$marionette (negate bios-boot-screen?)
721 (test-assert "enter LUKS passphrase for the initrd"
723 ;; XXX: Here we use OCR as well but we could instead use QEMU
724 ;; '-serial stdio' and run it in an input pipe,
725 (wait-for-screen-text #$marionette passphrase-prompt?
728 (marionette-type "thepassphrase\n" #$marionette)
730 ;; Take a screenshot for debugging purposes.
731 (marionette-control (string-append "screendump " #$output
732 "/post-initrd-passphrase.ppm")
735 (define %test-encrypted-os
737 (name "encrypted-root-os")
739 "Test basic functionality of an OS installed like one would do by hand.
740 This test is expensive in terms of CPU and storage usage since we need to
741 build (current-guix) and then store a couple of full system images.")
743 (mlet* %store-monad ((image (run-install %encrypted-root-os
744 %encrypted-root-os-source
746 %encrypted-root-installation-script))
747 (command (qemu-command/writable-image image)))
748 (run-basic-test %encrypted-root-os command "encrypted-root-os"
749 #:initialization enter-luks-passphrase)))))
753 ;;; Btrfs root file system.
756 (define-os-with-source (%btrfs-root-os %btrfs-root-os-source)
757 ;; The OS we want to install.
758 (use-modules (gnu) (gnu tests) (srfi srfi-1))
761 (host-name "liberigilo")
762 (timezone "Europe/Paris")
763 (locale "en_US.UTF-8")
765 (bootloader (grub-configuration (target "/dev/vdb")))
766 (kernel-arguments '("console=ttyS0"))
767 (file-systems (cons (file-system
773 (users (cons (user-account
776 (home-directory "/home/charlie")
777 (supplementary-groups '("wheel" "audio" "video")))
778 %base-user-accounts))
779 (services (cons (service marionette-service-type
780 (marionette-configuration
781 (imported-modules '((gnu services herd)
782 (guix combinators)))))
785 (define %btrfs-root-installation-script
786 ;; Shell script of a simple installation.
792 export GUIX_BUILD_OPTIONS=--no-grafts
793 ls -l /run/current-system/gc-roots
794 parted --script /dev/vdb mklabel gpt \\
795 mkpart primary ext2 1M 3M \\
796 mkpart primary ext2 3M 1G \\
799 mkfs.btrfs -L my-root /dev/vdb2
801 btrfs subvolume create /mnt/home
802 herd start cow-store /mnt
804 cp /etc/target-config.scm /mnt/etc/config.scm
805 guix system build /mnt/etc/config.scm
806 guix system init /mnt/etc/config.scm /mnt --no-substitutes
810 (define %test-btrfs-root-os
812 (name "btrfs-root-os")
814 "Test basic functionality of an OS installed like one would do by hand.
815 This test is expensive in terms of CPU and storage usage since we need to
816 build (current-guix) and then store a couple of full system images.")
818 (mlet* %store-monad ((image (run-install %btrfs-root-os
819 %btrfs-root-os-source
821 %btrfs-root-installation-script))
822 (command (qemu-command/writable-image image)))
823 (run-basic-test %btrfs-root-os command "btrfs-root-os")))))
825 ;;; install.scm ends here