services: mingetty-service: Use <mingetty-configuration> objects.
[jackhill/guix/guix.git] / gnu / system / vm.scm
index a15c4c3..dfb6996 100644 (file)
@@ -1,5 +1,5 @@
 ;;; GNU Guix --- Functional package management for GNU
-;;; Copyright © 2013, 2014 Ludovic Courtès <ludo@gnu.org>
+;;; Copyright © 2013, 2014, 2015 Ludovic Courtès <ludo@gnu.org>
 ;;;
 ;;; This file is part of GNU Guix.
 ;;;
   #:use-module (guix derivations)
   #:use-module (guix packages)
   #:use-module (guix monads)
-  #:use-module ((gnu packages base)
-                #:select (%final-inputs))
+  #:use-module (guix records)
+
+  #:use-module ((gnu build vm)
+                #:select (qemu-command))
+  #:use-module (gnu packages base)
   #:use-module (gnu packages guile)
+  #:use-module (gnu packages gawk)
   #:use-module (gnu packages bash)
   #:use-module (gnu packages less)
   #:use-module (gnu packages qemu)
-  #:use-module (gnu packages parted)
+  #:use-module (gnu packages disk)
   #:use-module (gnu packages zile)
   #:use-module (gnu packages grub)
   #:use-module (gnu packages linux)
@@ -53,6 +57,7 @@
   #:export (expression->derivation-in-linux-vm
             qemu-image
             system-qemu-image
+
             system-qemu-image/shared-store
             system-qemu-image/shared-store-script
             system-disk-image))
 ;;;
 ;;; Code:
 
-(define* (input->name+output tuple #:key (system (%current-system)))
-  "Return as a monadic value a name/file-name pair corresponding to TUPLE, an
-input tuple.  The output file name is when building for SYSTEM."
-  (with-monad %store-monad
-    (match tuple
-      ((input (? package? package))
-       (mlet %store-monad ((out (package-file package #:system system)))
-         (return `(,input . ,out))))
-      ((input (? package? package) sub-drv)
-       (mlet %store-monad ((out (package-file package
-                                              #:output sub-drv
-                                              #:system system)))
-         (return `(,input . ,out))))
-      ((input (? derivation? drv))
-       (return `(,input . ,(derivation->output-path drv))))
-      ((input (? derivation? drv) sub-drv)
-       (return `(,input . ,(derivation->output-path drv sub-drv))))
-      ((input (and (? string?) (? store-path?) file))
-       (return `(,input . ,file))))))
-
 (define %linux-vm-file-systems
   ;; File systems mounted for 'derivation-in-linux-vm'.  The store and /xchg
   ;; directory are shared with the host over 9p.
@@ -110,10 +95,16 @@ input tuple.  The output file name is when building for SYSTEM."
                                              (qemu qemu-headless)
                                              (env-vars '())
                                              (modules
-                                              '((guix build vm)
-                                                (guix build install)
-                                                (guix build linux-initrd)
-                                                (guix build utils)))
+                                              '((gnu build vm)
+                                                (gnu build install)
+                                                (gnu build linux-boot)
+                                                (gnu build linux-modules)
+                                                (gnu build file-systems)
+                                                (guix elf)
+                                                (guix records)
+                                                (guix build utils)
+                                                (guix build syscalls)
+                                                (guix build store-copy)))
                                              (guile-for-build
                                               (%guile-for-build))
 
@@ -150,17 +141,19 @@ made available under the /xchg CIFS share."
                                            (cons #$compiled
                                                  %load-compiled-path))
                                      (primitive-load #$user-builder))))
-       (coreutils -> (car (assoc-ref %final-inputs "coreutils")))
+       (coreutils -> (canonical-package coreutils))
        (initrd       (if initrd                   ; use the default initrd?
                          (return initrd)
-                         (qemu-initrd %linux-vm-file-systems
-                                      #:guile-modules-in-chroot? #t))))
+                         (base-initrd %linux-vm-file-systems
+                                      #:linux linux
+                                      #:virtio? #t
+                                      #:qemu-networking? #t))))
 
     (define builder
       ;; Code that launches the VM that evaluates EXP.
       #~(begin
           (use-modules (guix build utils)
-                       (guix build vm))
+                       (gnu build vm))
 
           (let ((inputs  '#$(list qemu coreutils))
                 (linux   (string-append #$linux "/bzImage"))
@@ -196,60 +189,69 @@ made available under the /xchg CIFS share."
                      (disk-image-size (* 100 (expt 2 20)))
                      (disk-image-format "qcow2")
                      (file-system-type "ext4")
+                     file-system-label
+                     os-derivation
                      grub-configuration
                      (register-closures? #t)
                      (inputs '())
                      copy-inputs?)
   "Return a bootable, stand-alone QEMU image of type DISK-IMAGE-FORMAT (e.g.,
-'qcow2' or 'raw'), with a root partition of type FILE-SYSTEM-TYPE.  The
-returned image is a full disk image, with a GRUB installation that uses
-GRUB-CONFIGURATION as its configuration file (GRUB-CONFIGURATION must be the
-name of a file in the VM.)
+'qcow2' or 'raw'), with a root partition of type FILE-SYSTEM-TYPE.
+Optionally, FILE-SYSTEM-LABEL can be specified as the volume name for the root
+partition.  The returned image is a full disk image that runs OS-DERIVATION,
+with a GRUB installation that uses GRUB-CONFIGURATION as its configuration
+file (GRUB-CONFIGURATION must be the name of a file in the VM.)
 
 INPUTS is a list of inputs (as for packages).  When COPY-INPUTS? is true, copy
 all of INPUTS into the image being built.  When REGISTER-CLOSURES? is true,
 register INPUTS in the store database of the image so that Guix can be used in
 the image."
-  (mlet %store-monad
-      ((graph (sequence %store-monad (map input->name+output inputs))))
-   (expression->derivation-in-linux-vm
-    name
-    #~(begin
-        (use-modules (guix build vm)
-                     (guix build utils))
-
-        (let ((inputs
-               '#$(append (list qemu parted grub e2fsprogs util-linux)
-                          (map (compose car (cut assoc-ref %final-inputs <>))
-                               '("sed" "grep" "coreutils" "findutils" "gawk"))
-                          (if register-closures? (list guix) '())))
-
-              ;; This variable is unused but allows us to add INPUTS-TO-COPY
-              ;; as inputs.
-              (to-register
-                '#$(map (match-lambda
-                         ((name thing) thing)
-                         ((name thing output) `(,thing ,output)))
-                        inputs)))
-
-          (set-path-environment-variable "PATH" '("bin" "sbin") inputs)
-
-          (let ((graphs '#$(match inputs
-                             (((names . _) ...)
-                              names))))
-            (initialize-hard-disk "/dev/vda"
-                                  #:grub.cfg #$grub-configuration
-                                  #:closures graphs
-                                  #:copy-closures? #$copy-inputs?
-                                  #:register-closures? #$register-closures?
-                                  #:disk-image-size #$disk-image-size
-                                  #:file-system-type #$file-system-type)
-            (reboot))))
-    #:system system
-    #:make-disk-image? #t
-    #:disk-image-size disk-image-size
-    #:disk-image-format disk-image-format
-    #:references-graphs graph)))
+  (expression->derivation-in-linux-vm
+   name
+   #~(begin
+       (use-modules (gnu build vm)
+                    (guix build utils))
+
+       (let ((inputs
+              '#$(append (list qemu parted grub e2fsprogs)
+                         (map canonical-package
+                              (list sed grep coreutils findutils gawk))
+                         (if register-closures? (list guix) '())))
+
+             ;; This variable is unused but allows us to add INPUTS-TO-COPY
+             ;; as inputs.
+             (to-register
+              '#$(map (match-lambda
+                       ((name thing) thing)
+                       ((name thing output) `(,thing ,output)))
+                      inputs)))
+
+         (set-path-environment-variable "PATH" '("bin" "sbin") inputs)
+
+         (let* ((graphs     '#$(match inputs
+                                 (((names . _) ...)
+                                  names)))
+                (initialize (root-partition-initializer
+                             #:closures graphs
+                             #:copy-closures? #$copy-inputs?
+                             #:register-closures? #$register-closures?
+                             #:system-directory #$os-derivation))
+                (partitions (list (partition
+                                   (size #$(- disk-image-size
+                                              (* 10 (expt 2 20))))
+                                   (label #$file-system-label)
+                                   (file-system #$file-system-type)
+                                   (bootable? #t)
+                                   (initializer initialize)))))
+           (initialize-hard-disk "/dev/vda"
+                                 #:partitions partitions
+                                 #:grub.cfg #$grub-configuration)
+           (reboot))))
+   #:system system
+   #:make-disk-image? #t
+   #:disk-image-size disk-image-size
+   #:disk-image-format disk-image-format
+   #:references-graphs inputs))
 
 \f
 ;;;
@@ -258,6 +260,7 @@ the image."
 
 (define* (system-disk-image os
                             #:key
+                            (name "disk-image")
                             (file-system-type "ext4")
                             (disk-image-size (* 900 (expt 2 20)))
                             (volatile? #t))
@@ -265,31 +268,43 @@ the image."
 system described by OS.  Said image can be copied on a USB stick as is.  When
 VOLATILE? is true, the root file system is made volatile; this is useful
 to USB sticks meant to be read-only."
+  (define root-label
+    ;; Volume name of the root file system.  Since we don't know which device
+    ;; will hold it, we use the volume name to find it (using the UUID would
+    ;; be even better, but somewhat less convenient.)
+    "gnu-disk-image")
+
   (define file-systems-to-keep
     (remove (lambda (fs)
               (string=? (file-system-mount-point fs) "/"))
             (operating-system-file-systems os)))
 
   (let ((os (operating-system (inherit os)
-              ;; Since this is meant to be used on real hardware, don't set up
-              ;; QEMU networking.
-              (initrd (cut qemu-initrd <>
-                           #:volatile-root? volatile?
-                           #:qemu-networking? #f))
+              ;; Since this is meant to be used on real hardware, don't
+              ;; install QEMU networking or anything like that.  Assume USB
+              ;; mass storage devices (usb-storage.ko) are available.
+              (initrd (lambda (file-systems . rest)
+                        (apply base-initrd file-systems
+                               #:volatile-root? #t
+                               rest)))
 
               ;; Force our own root file system.
               (file-systems (cons (file-system
                                     (mount-point "/")
-                                    (device "/dev/sda1")
+                                    (device root-label)
+                                    (title 'label)
                                     (type file-system-type))
                                   file-systems-to-keep)))))
 
     (mlet* %store-monad ((os-drv   (operating-system-derivation os))
                          (grub.cfg (operating-system-grub.cfg os)))
-      (qemu-image #:grub-configuration grub.cfg
+      (qemu-image #:name name
+                  #:os-derivation os-drv
+                  #:grub-configuration grub.cfg
                   #:disk-image-size disk-image-size
                   #:disk-image-format "raw"
                   #:file-system-type file-system-type
+                  #:file-system-label root-label
                   #:copy-inputs? #t
                   #:register-closures? #t
                   #:inputs `(("system" ,os-drv)
@@ -312,6 +327,13 @@ of the GNU system as described by OS."
             (operating-system-file-systems os)))
 
   (let ((os (operating-system (inherit os)
+              ;; Use an initrd with the whole QEMU shebang.
+              (initrd (lambda (file-systems . rest)
+                        (apply base-initrd file-systems
+                               #:virtio? #t
+                               #:qemu-networking? #t
+                               rest)))
+
               ;; Force our own root file system.
               (file-systems (cons (file-system
                                     (mount-point "/")
@@ -321,82 +343,169 @@ of the GNU system as described by OS."
     (mlet* %store-monad
         ((os-drv      (operating-system-derivation os))
          (grub.cfg    (operating-system-grub.cfg os)))
-      (qemu-image  #:grub-configuration grub.cfg
+      (qemu-image  #:os-derivation os-drv
+                   #:grub-configuration grub.cfg
                    #:disk-image-size disk-image-size
                    #:file-system-type file-system-type
                    #:inputs `(("system" ,os-drv)
                               ("grub.cfg" ,grub.cfg))
                    #:copy-inputs? #t))))
 
-(define (virtualized-operating-system os)
+\f
+;;;
+;;; VMs that share file systems with the host.
+;;;
+
+(define (file-system->mount-tag fs)
+  "Return a 9p mount tag for host file system FS."
+  ;; QEMU mount tags cannot contain slashes and cannot start with '_'.
+  ;; Compute an identifier that corresponds to the rules.
+  (string-append "TAG"
+                 (string-map (match-lambda
+                              (#\/ #\_)
+                              (chr chr))
+                             fs)))
+
+(define (mapping->file-system mapping)
+  "Return a 9p file system that realizes MAPPING."
+  (match mapping
+    (($ <file-system-mapping> source target writable?)
+     (file-system
+       (mount-point target)
+       (device (file-system->mount-tag source))
+       (type "9p")
+       (flags (if writable? '() '(read-only)))
+       (options (string-append "trans=virtio"))
+       (check? #f)
+       (create-mount-point? #t)))))
+
+(define (virtualized-operating-system os mappings)
   "Return an operating system based on OS suitable for use in a virtualized
-environment with the store shared with the host."
+environment with the store shared with the host.  MAPPINGS is a list of
+<file-system-mapping> to realize in the virtualized OS."
+  (define user-file-systems
+    ;; Remove file systems that conflict with those added below, or that are
+    ;; normally bound to real devices.
+    (remove (lambda (fs)
+              (let ((target (file-system-mount-point fs))
+                    (source (file-system-device fs)))
+                (or (string=? target (%store-prefix))
+                    (string=? target "/")
+                    (string-prefix? "/dev/" source))))
+            (operating-system-file-systems os)))
+
   (operating-system (inherit os)
-    (initrd (cut qemu-initrd <> #:volatile-root? #t))
+    (initrd (lambda (file-systems . rest)
+              (apply base-initrd file-systems
+                     #:volatile-root? #t
+                     #:virtio? #t
+                     #:qemu-networking? #t
+                     rest)))
+
+    ;; Disable swap.
+    (swap-devices '())
+
     (file-systems (cons* (file-system
                            (mount-point "/")
                            (device "/dev/vda1")
                            (type "ext4"))
-                         (file-system
-                           (mount-point (%store-prefix))
-                           (device "store")
-                           (type "9p")
-                           (needed-for-boot? #t)
-                           (options "trans=virtio")
-                           (check? #f))
-
-                         ;; Remove file systems that conflict with those
-                         ;; above, or that are normally bound to real devices.
-                         (remove (lambda (fs)
-                                   (let ((target (file-system-mount-point fs))
-                                         (source (file-system-device fs)))
-                                     (or (string=? target (%store-prefix))
-                                         (string=? target "/")
-                                         (string-prefix? "/dev/" source))))
-                                 (operating-system-file-systems os))))))
+
+                         (file-system (inherit
+                                       (mapping->file-system %store-mapping))
+                            (needed-for-boot? #t))
+
+                         (append (map mapping->file-system mappings)
+                                 user-file-systems)))))
 
 (define* (system-qemu-image/shared-store
           os
-          #:key (disk-image-size (* 15 (expt 2 20))))
+          #:key
+          full-boot?
+          (disk-image-size (* (if full-boot? 500 15) (expt 2 20))))
   "Return a derivation that builds a QEMU image of OS that shares its store
-with the host."
-  (mlet* %store-monad
-      ((os-drv      (operating-system-derivation os))
-       (grub.cfg    (operating-system-grub.cfg os)))
-    (qemu-image #:grub-configuration grub.cfg
+with the host.
+
+When FULL-BOOT? is true, return an image that does a complete boot sequence,
+bootloaded included; thus, make a disk image that contains everything the
+bootloader refers to: OS kernel, initrd, bootloader data, etc."
+  (mlet* %store-monad ((os-drv   (operating-system-derivation os))
+                       (grub.cfg (operating-system-grub.cfg os)))
+    ;; XXX: When FULL-BOOT? is true, we end up creating an image that contains
+    ;; GRUB.CFG and all its dependencies, including the output of OS-DRV.
+    ;; This is more than needed (we only need the kernel, initrd, GRUB for its
+    ;; font, and the background image), but it's hard to filter that.
+    (qemu-image #:os-derivation os-drv
+                #:grub-configuration grub.cfg
                 #:disk-image-size disk-image-size
-                #:inputs `(("system" ,os-drv))
+                #:inputs (if full-boot?
+                             `(("grub.cfg" ,grub.cfg))
+                             '())
 
                 ;; XXX: Passing #t here is too slow, so let it off by default.
                 #:register-closures? #f
-                #:copy-inputs? #f)))
-
-(define* (system-qemu-image/shared-store-script
-          os
-          #:key
-          (qemu qemu)
-          (graphic? #t))
+                #:copy-inputs? full-boot?)))
+
+(define* (common-qemu-options image shared-fs)
+  "Return the a string-value gexp with the common QEMU options to boot IMAGE,
+with '-virtfs' options for the host file systems listed in SHARED-FS."
+  (define (virtfs-option fs)
+    #~(string-append "-virtfs local,path=\"" #$fs
+                     "\",security_model=none,mount_tag=\""
+                     #$(file-system->mount-tag fs)
+                     "\" "))
+
+  #~(string-append
+     " -enable-kvm -no-reboot -net nic,model=virtio \
+  " #$@(map virtfs-option shared-fs) " \
+  -net user \
+  -serial stdio -vga std \
+  -drive file=" #$image
+  ",if=virtio,cache=writeback,werror=report,readonly \
+  -m 256"))
+
+(define* (system-qemu-image/shared-store-script os
+                                                #:key
+                                                (qemu qemu)
+                                                (graphic? #t)
+                                                (mappings '())
+                                                full-boot?
+                                                (disk-image-size
+                                                 (* (if full-boot? 500 15)
+                                                    (expt 2 20))))
   "Return a derivation that builds a script to run a virtual machine image of
-OS that shares its store with the host."
-  (mlet* %store-monad
-      ((os ->  (virtualized-operating-system os))
-       (os-drv (operating-system-derivation os))
-       (image  (system-qemu-image/shared-store os)))
+OS that shares its store with the host.
+
+MAPPINGS is a list of <file-system-mapping> specifying mapping of host file
+systems into the guest.
+
+When FULL-BOOT? is true, the returned script runs everything starting from the
+bootloader; otherwise it directly starts the operating system kernel.  The
+DISK-IMAGE-SIZE parameter specifies the size in bytes of the root disk image;
+it is mostly useful when FULL-BOOT?  is true."
+  (mlet* %store-monad ((os ->  (virtualized-operating-system os mappings))
+                       (os-drv (operating-system-derivation os))
+                       (image  (system-qemu-image/shared-store
+                                os
+                                #:full-boot? full-boot?
+                                #:disk-image-size disk-image-size)))
     (define builder
       #~(call-with-output-file #$output
           (lambda (port)
             (display
              (string-append "#!" #$bash "/bin/sh
-exec " #$qemu "/bin/qemu-system-x86_64 -enable-kvm -no-reboot -net nic,model=virtio \
-  -virtfs local,path=" #$(%store-prefix) ",security_model=none,mount_tag=store \
-  -net user \
-  -kernel " #$(operating-system-kernel os) "/bzImage \
-  -initrd " #$os-drv "/initrd \
--append \"" #$(if graphic? "" "console=ttyS0 ")
-  "--system=" #$os-drv " --load=" #$os-drv "/boot --root=/dev/vda1\" \
-  -serial stdio \
-  -drive file=" #$image
-  ",if=virtio,cache=writeback,werror=report,readonly\n")
+exec " #$qemu "/bin/" #$(qemu-command (%current-system))
+
+#$@(if full-boot?
+       #~()
+       #~(" -kernel " #$(operating-system-kernel os) "/bzImage \
+            -initrd " #$os-drv "/initrd \
+            -append \"" #$(if graphic? "" "console=ttyS0 ")
+            "--system=" #$os-drv " --load=" #$os-drv "/boot --root=/dev/vda1 "
+            (string-join (list #+@(operating-system-kernel-arguments os))) "\" "))
+#$(common-qemu-options image
+                       (map file-system-mapping-source
+                            (cons %store-mapping mappings)))
+" \"$@\"\n")
              port)
             (chmod port #o555))))