system: vm: Use operating-system-kernel-arguments.
[jackhill/guix/guix.git] / gnu / system / vm.scm
index 03f7d6c..2c8b954 100644 (file)
@@ -2,6 +2,7 @@
 ;;; Copyright © 2013, 2014, 2015, 2016 Ludovic Courtès <ludo@gnu.org>
 ;;; Copyright © 2016 Christopher Allan Webber <cwebber@dustycloud.org>
 ;;; Copyright © 2016 Leo Famulari <leo@famulari.name>
+;;; Copyright © 2017 Mathieu Othacehe <m.othacehe@gmail.com>
 ;;;
 ;;; This file is part of GNU Guix.
 ;;;
@@ -31,6 +32,7 @@
   #:use-module ((gnu build vm)
                 #:select (qemu-command))
   #:use-module (gnu packages base)
+  #:use-module (gnu packages bootloaders)
   #:use-module (gnu packages guile)
   #:use-module (gnu packages gawk)
   #:use-module (gnu packages bash)
@@ -38,7 +40,6 @@
   #:use-module (gnu packages qemu)
   #:use-module (gnu packages disk)
   #:use-module (gnu packages zile)
-  #:use-module (gnu packages grub)
   #:use-module (gnu packages linux)
   #:use-module (gnu packages package-management)
   #:use-module ((gnu packages make-bootstrap)
@@ -141,7 +142,8 @@ made available under the /xchg CIFS share."
                          (gnu build vm))
 
             (let ((inputs  '#$(list qemu coreutils))
-                  (linux   (string-append #$linux "/bzImage"))
+                  (linux   (string-append #$linux "/"
+                                          #$(system-linux-image-file-name)))
                   (initrd  (string-append #$initrd "/initrd"))
                   (loader  #$loader)
                   (graphs  '#$(match references-graphs
@@ -283,10 +285,10 @@ to USB sticks meant to be read-only."
                                   file-systems-to-keep)))))
 
     (mlet* %store-monad ((os-drv   (operating-system-derivation os))
-                         (grub.cfg (operating-system-grub.cfg os)))
+                         (bootcfg  (operating-system-bootcfg os)))
       (qemu-image #:name name
                   #:os-derivation os-drv
-                  #:grub-configuration grub.cfg
+                  #:grub-configuration bootcfg
                   #:disk-image-size disk-image-size
                   #:disk-image-format "raw"
                   #:file-system-type file-system-type
@@ -294,7 +296,7 @@ to USB sticks meant to be read-only."
                   #:copy-inputs? #t
                   #:register-closures? #t
                   #:inputs `(("system" ,os-drv)
-                             ("grub.cfg" ,grub.cfg))))))
+                             ("bootcfg" ,bootcfg))))))
 
 (define* (system-qemu-image os
                             #:key
@@ -317,7 +319,6 @@ of the GNU system as described by OS."
               (initrd (lambda (file-systems . rest)
                         (apply base-initrd file-systems
                                #:virtio? #t
-                               #:qemu-networking? #t
                                rest)))
 
               ;; Force our own root file system.
@@ -328,13 +329,13 @@ of the GNU system as described by OS."
                                   file-systems-to-keep)))))
     (mlet* %store-monad
         ((os-drv      (operating-system-derivation os))
-         (grub.cfg    (operating-system-grub.cfg os)))
+         (bootcfg     (operating-system-bootcfg os)))
       (qemu-image  #:os-derivation os-drv
-                   #:grub-configuration grub.cfg
+                   #:grub-configuration bootcfg
                    #:disk-image-size disk-image-size
                    #:file-system-type file-system-type
                    #:inputs `(("system" ,os-drv)
-                              ("grub.cfg" ,grub.cfg))
+                              ("bootcfg" ,bootcfg))
                    #:copy-inputs? #t))))
 
 \f
@@ -365,7 +366,7 @@ of the GNU system as described by OS."
        (check? #f)
        (create-mount-point? #t)))))
 
-(define (virtualized-operating-system os mappings)
+(define* (virtualized-operating-system os mappings #:optional (full-boot? #f))
   "Return an operating system based on OS suitable for use in a virtualized
 environment with the store shared with the host.  MAPPINGS is a list of
 <file-system-mapping> to realize in the virtualized OS."
@@ -381,28 +382,35 @@ environment with the store shared with the host.  MAPPINGS is a list of
                          (string-prefix? "/dev/" source)))))
             (operating-system-file-systems os)))
 
+  (define virtual-file-systems
+    (cons (file-system
+            (mount-point "/")
+            (device "/dev/vda1")
+            (type "ext4"))
+
+          (append (map mapping->file-system mappings)
+                  user-file-systems)))
+
   (operating-system (inherit os)
     (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 (inherit
-                                       (mapping->file-system %store-mapping))
-                            (needed-for-boot? #t))
-
-                         (append (map mapping->file-system mappings)
-                                 user-file-systems)))))
+    ;; XXX: When FULL-BOOT? is true, do not add a 9p mount for /gnu/store
+    ;; since that would lead the bootloader config to look for the kernel and
+    ;; initrd in it.
+    (file-systems (if full-boot?
+                      virtual-file-systems
+                      (cons
+                       (file-system
+                         (inherit (mapping->file-system %store-mapping))
+                         (needed-for-boot? #t))
+                       virtual-file-systems)))))
 
 (define* (system-qemu-image/shared-store
           os
@@ -416,16 +424,16 @@ 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)))
+                       (bootcfg  (operating-system-bootcfg 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.
+    ;; BOOTCFG 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
+                #:grub-configuration bootcfg
                 #:disk-image-size disk-image-size
                 #:inputs (if full-boot?
-                             `(("grub.cfg" ,grub.cfg))
+                             `(("bootcfg" ,bootcfg))
                              '())
 
                 ;; XXX: Passing #t here is too slow, so let it off by default.
@@ -435,25 +443,26 @@ bootloader refers to: OS kernel, initrd, bootloader data, etc."
 (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)
-                     "\" "))
+    #~(format #f "-virtfs local,path=~s,security_model=none,mount_tag=~s"
+              #$fs #$(file-system->mount-tag fs)))
 
-  #~(string-append
-     ;; Only enable kvm if we see /dev/kvm exists.
+  #~(;; Only enable kvm if we see /dev/kvm exists.
      ;; This allows users without hardware virtualization to still use these
      ;; commands.
-     #$(if (file-exists? "/dev/kvm")
-           " -enable-kvm "
-           "")
-     " -no-reboot -net nic,model=virtio \
-  " #$@(map virtfs-option shared-fs) " \
-  -vga std \
-  -drive file=" #$image
-  ",if=virtio,cache=writeback,werror=report,readonly \
-  -m 256"))
+     #$@(if (file-exists? "/dev/kvm")
+            '("-enable-kvm")
+            '())
+
+     "-no-reboot"
+     "-net nic,model=virtio"
+
+     #$@(map virtfs-option shared-fs)
+     "-vga std"
+     (format #f "-drive file=~a,if=virtio,cache=writeback,werror=report,readonly"
+             #$image)
+     "-m 256"))
 
 (define* (system-qemu-image/shared-store-script os
                                                 #:key
@@ -474,31 +483,34 @@ 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))
+  (mlet* %store-monad ((os ->  (virtualized-operating-system os mappings full-boot?))
                        (os-drv (operating-system-derivation os))
                        (image  (system-qemu-image/shared-store
                                 os
                                 #:full-boot? full-boot?
                                 #:disk-image-size disk-image-size)))
+    (define kernel-arguments
+      #~(list #$@(if graphic? #~() #~("console=ttyS0"))
+              #+@(operating-system-kernel-arguments os os-drv "/dev/vda1")))
+
+    (define qemu-exec
+      #~(list (string-append #$qemu "/bin/" #$(qemu-command (%current-system)))
+              #$@(if full-boot?
+                     #~()
+                     #~("-kernel" #$(operating-system-kernel-file os)
+                        "-initrd" #$(file-append os-drv "/initrd")
+                        (format #f "-append ~s"
+                                (string-join #$kernel-arguments " "))))
+              #$@(common-qemu-options image
+                                      (map file-system-mapping-source
+                                           (cons %store-mapping mappings)))))
+
     (define builder
       #~(call-with-output-file #$output
           (lambda (port)
-            (display
-             (string-append "#!" #$bash "/bin/sh
-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)
+            (format port "#!~a~% exec ~a \"$@\"~%"
+                    #$(file-append bash "/bin/sh")
+                    (string-join #$qemu-exec " "))
             (chmod port #o555))))
 
     (gexp->derivation "run-vm.sh" builder)))