gnu: vm: Remove unused 'qemu-image' argument.
[guix:guix.git] / gnu / system / vm.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2013 Ludovic Courtès <ludo@gnu.org>
3 ;;;
4 ;;; This file is part of GNU Guix.
5 ;;;
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.
10 ;;;
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.
15 ;;;
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/>.
18
19 (define-module (gnu system vm)
20   #:use-module (guix config)
21   #:use-module (guix store)
22   #:use-module (guix derivations)
23   #:use-module (guix packages)
24   #:use-module ((gnu packages base) #:select (%final-inputs
25                                               guile-final
26                                               coreutils))
27   #:use-module (gnu packages guile)
28   #:use-module (gnu packages bash)
29   #:use-module (gnu packages qemu)
30   #:use-module (gnu packages parted)
31   #:use-module (gnu packages grub)
32   #:use-module (gnu packages linux)
33   #:use-module (gnu packages linux-initrd)
34   #:use-module ((gnu packages make-bootstrap)
35                 #:select (%guile-static-stripped))
36   #:use-module ((gnu packages system)
37                 #:select (mingetty))
38   #:use-module (srfi srfi-1)
39   #:use-module (srfi srfi-26)
40   #:use-module (ice-9 match)
41   #:export (expression->derivation-in-linux-vm
42             qemu-image))
43
44 \f
45 ;;; Commentary:
46 ;;;
47 ;;; Tools to evaluate build expressions within virtual machines.
48 ;;;
49 ;;; Code:
50
51 (define* (expression->derivation-in-linux-vm store name exp
52                                              #:key
53                                              (system (%current-system))
54                                              (inputs '())
55                                              (linux linux-libre)
56                                              (initrd qemu-initrd)
57                                              (qemu qemu/smb-shares)
58                                              (env-vars '())
59                                              (modules '())
60                                              (guile-for-build
61                                               (%guile-for-build))
62
63                                              (make-disk-image? #f)
64                                              (references-graphs #f)
65                                              (disk-image-size
66                                               (* 100 (expt 2 20))))
67   "Evaluate EXP in a QEMU virtual machine running LINUX with INITRD.  In the
68 virtual machine, EXP has access to all of INPUTS from the store; it should put
69 its output files in the `/xchg' directory, which is copied to the derivation's
70 output when the VM terminates.
71
72 When MAKE-DISK-IMAGE? is true, then create a QEMU disk image of
73 DISK-IMAGE-SIZE bytes and return it.
74
75 When REFERENCES-GRAPHS is true, it must be a list of file name/store path
76 pairs, as for `derivation'.  The files containing the reference graphs are
77 made available under the /xchg CIFS share."
78   (define input-alist
79     (map (match-lambda
80           ((input (? package? package))
81            `(,input . ,(package-output store package "out" system)))
82           ((input (? package? package) sub-drv)
83            `(,input . ,(package-output store package sub-drv system)))
84           ((input (and (? string?) (? store-path?) file))
85            `(,input . ,file)))
86          inputs))
87
88   (define exp*
89     ;; EXP, but with INPUTS available.
90     `(let ((%build-inputs ',input-alist))
91        ,exp))
92
93   (define builder
94     ;; Code that launches the VM that evaluates EXP.
95     `(let ()
96        (use-modules (guix build utils)
97                     (srfi srfi-1)
98                     (ice-9 rdelim))
99
100        (let ((out     (assoc-ref %outputs "out"))
101              (cu      (string-append (assoc-ref %build-inputs "coreutils")
102                                      "/bin"))
103              (qemu    (string-append (assoc-ref %build-inputs "qemu")
104                                      "/bin/qemu-system-"
105                                      (car (string-split ,system #\-))))
106              (img     (string-append (assoc-ref %build-inputs "qemu")
107                                      "/bin/qemu-img"))
108              (linux   (string-append (assoc-ref %build-inputs "linux")
109                                      "/bzImage"))
110              (initrd  (string-append (assoc-ref %build-inputs "initrd")
111                                      "/initrd"))
112              (builder (assoc-ref %build-inputs "builder")))
113
114          ;; XXX: QEMU uses "rm -rf" when it's done to remove the temporary SMB
115          ;; directory, so it really needs `rm' in $PATH.
116          (setenv "PATH" cu)
117
118          ,(if make-disk-image?
119               `(zero? (system* img "create" "image.qcow2"
120                                ,(number->string disk-image-size)))
121               '(begin))
122
123          (mkdir "xchg")
124
125          ;; Copy the reference-graph files under xchg/ so EXP can access it.
126          (begin
127            ,@(match references-graphs
128                (((graph-files . _) ...)
129                 (map (lambda (file)
130                        `(copy-file ,file
131                                    ,(string-append "xchg/" file)))
132                      graph-files))
133                (#f '())))
134
135          (and (zero?
136                (system* qemu "-nographic" "-no-reboot"
137                         "-net" "nic,model=e1000"
138                         "-net" (string-append "user,smb=" (getcwd))
139                         "-kernel" linux
140                         "-initrd" initrd
141                         "-append" (string-append "console=ttyS0 --load="
142                                                  builder)
143                         ,@(if make-disk-image?
144                               '("-hda" "image.qcow2")
145                               '())))
146               ,(if make-disk-image?
147                    '(copy-file "image.qcow2"      ; XXX: who mkdir'd OUT?
148                                out)
149                    '(begin
150                       (mkdir out)
151                       (copy-recursively "xchg" out)))))))
152
153   (let ((user-builder (add-text-to-store store "builder-in-linux-vm"
154                                          (object->string exp*)
155                                          '()))
156         (->drv        (cut package-derivation store <> system))
157         (coreutils    (car (assoc-ref %final-inputs "coreutils"))))
158     (build-expression->derivation store name system builder
159                                   `(("qemu" ,(->drv qemu))
160                                     ("linux" ,(->drv linux))
161                                     ("initrd" ,(->drv initrd))
162                                     ("coreutils" ,(->drv coreutils))
163                                     ("builder" ,user-builder)
164                                     ,@(map (match-lambda
165                                             ((name (? package? package)
166                                                    sub-drv ...)
167                                              `(,name ,(->drv package)
168                                                      ,@sub-drv))
169                                             ((name (? string? file))
170                                              `(,name ,file)))
171                                            inputs))
172                                   #:env-vars env-vars
173                                   #:modules (delete-duplicates
174                                              `((guix build utils)
175                                                ,@modules))
176                                   #:guile-for-build guile-for-build
177                                   #:references-graphs references-graphs)))
178
179 (define* (qemu-image store #:key
180                      (name "qemu-image")
181                      (system (%current-system))
182                      (disk-image-size (* 100 (expt 2 20)))
183                      (linux linux-libre)
184                      (linux-arguments '())
185                      (initrd qemu-initrd)
186                      (inputs '())
187                      (inputs-to-copy '()))
188   "Return a bootable, stand-alone QEMU image.  The returned image is a full
189 disk image, with a GRUB installation whose default entry boots LINUX, with the
190 arguments LINUX-ARGUMENTS, and using INITRD as its initial RAM disk.
191
192 INPUTS-TO-COPY is a list of inputs (as for packages) whose closure is copied
193 into the image being built."
194   (define input->name+derivation
195     (match-lambda
196      ((name (? package? package))
197       `(,name . ,(derivation-path->output-path
198                   (package-derivation store package system))))
199      ((name (? package? package) sub-drv)
200       `(,name . ,(derivation-path->output-path
201                   (package-derivation store package system)
202                   sub-drv)))
203      ((input (and (? string?) (? store-path?) file))
204       `(,input . ,file))))
205
206   (expression->derivation-in-linux-vm
207    store "qemu-image"
208    `(let ()
209       (use-modules (ice-9 rdelim)
210                    (srfi srfi-1)
211                    (guix build utils)
212                    (guix build linux-initrd))
213
214       (let ((parted  (string-append (assoc-ref %build-inputs "parted")
215                                     "/sbin/parted"))
216             (mkfs    (string-append (assoc-ref %build-inputs "e2fsprogs")
217                                     "/sbin/mkfs.ext3"))
218             (grub    (string-append (assoc-ref %build-inputs "grub")
219                                     "/sbin/grub-install"))
220             (umount  (string-append (assoc-ref %build-inputs "util-linux")
221                                     "/bin/umount")) ; XXX: add to Guile
222             (initrd  (string-append (assoc-ref %build-inputs "initrd")
223                                     "/initrd"))
224             (linux   (string-append (assoc-ref %build-inputs "linux")
225                                     "/bzImage")))
226
227         (define (read-reference-graph port)
228           ;; Return a list of store paths from the reference graph at PORT.
229           ;; The data at PORT is the format produced by #:references-graphs.
230           (let loop ((line   (read-line port))
231                      (result '()))
232             (cond ((eof-object? line)
233                    (delete-duplicates result))
234                   ((string-prefix? "/" line)
235                    (loop (read-line port)
236                          (cons line result)))
237                   (else
238                    (loop (read-line port)
239                          result)))))
240
241         (define (things-to-copy)
242           ;; Return the list of store files to copy to the image.
243           (define (graph-from-file file)
244             (call-with-input-file file
245               read-reference-graph))
246
247           ,(match inputs-to-copy
248              (((graph-files . _) ...)
249               `(let* ((graph-files ',(map (cut string-append "/xchg/" <>)
250                                           graph-files))
251                       (paths       (append-map graph-from-file graph-files)))
252                  (delete-duplicates paths)))
253              (#f ''())))
254
255         ;; GRUB is full of shell scripts.
256         (setenv "PATH"
257                 (string-append (dirname grub) ":"
258                                (assoc-ref %build-inputs "coreutils") "/bin:"
259                                (assoc-ref %build-inputs "findutils") "/bin:"
260                                (assoc-ref %build-inputs "sed") "/bin:"
261                                (assoc-ref %build-inputs "grep") "/bin:"
262                                (assoc-ref %build-inputs "gawk") "/bin"))
263
264         (display "creating partition table...\n")
265         (and (zero? (system* parted "/dev/vda" "mklabel" "msdos"
266                              "mkpart" "primary" "ext2" "1MiB"
267                              ,(format #f "~aB"
268                                       (- disk-image-size
269                                          (* 5 (expt 2 20))))))
270              (begin
271                (display "creating ext3 partition...\n")
272                (and (zero? (system* mkfs "-F" "/dev/vda1"))
273                     (begin
274                       (display "mounting partition...\n")
275                       (mkdir "/fs")
276                       (mount "/dev/vda1" "/fs" "ext3")
277                       (mkdir-p "/fs/boot/grub")
278                       (copy-file linux "/fs/boot/bzImage")
279                       (copy-file initrd "/fs/boot/initrd")
280
281                       ;; Populate the image's store.
282                       (mkdir-p (string-append "/fs" ,%store-directory))
283                       (for-each (lambda (thing)
284                                   (copy-recursively thing
285                                                     (string-append "/fs"
286                                                                    thing)))
287                                 (things-to-copy))
288
289                       ;; Populate /dev.
290                       (make-essential-device-nodes #:root "/fs")
291
292                       (call-with-output-file "/fs/boot/grub/grub.cfg"
293                         (lambda (p)
294                           (format p "
295 set default=1
296 set timeout=5
297 search.file /boot/bzImage
298
299 menuentry \"Boot-to-Guile! (GNU System technology preview)\" {
300   linux /boot/bzImage ~a
301   initrd /boot/initrd
302 }"
303                                   ,(string-join linux-arguments))))
304                       (and (zero?
305                             (system* grub "--no-floppy"
306                                      "--boot-directory" "/fs/boot"
307                                      "/dev/vda"))
308                            (zero?
309                             (system* umount "/fs"))
310                            (reboot))))))))
311    #:system system
312    #:inputs `(("parted" ,parted)
313               ("grub" ,grub)
314               ("e2fsprogs" ,e2fsprogs)
315               ("linux" ,linux-libre)
316               ("initrd" ,initrd)
317
318               ;; For shell scripts.
319               ("sed" ,(car (assoc-ref %final-inputs "sed")))
320               ("grep" ,(car (assoc-ref %final-inputs "grep")))
321               ("coreutils" ,(car (assoc-ref %final-inputs "coreutils")))
322               ("findutils" ,(car (assoc-ref %final-inputs "findutils")))
323               ("gawk" ,(car (assoc-ref %final-inputs "gawk")))
324               ("util-linux" ,util-linux)
325
326               ,@inputs-to-copy)
327    #:make-disk-image? #t
328    #:disk-image-size disk-image-size
329    #:references-graphs (map input->name+derivation inputs-to-copy)
330    #:modules '((guix build utils)
331                (guix build linux-initrd))))
332
333 \f
334 ;;;
335 ;;; Guile 2.0 potluck examples.
336 ;;;
337
338 (define (example1)
339   (let ((store #f))
340     (dynamic-wind
341       (lambda ()
342         (set! store (open-connection)))
343       (lambda ()
344         (parameterize ((%guile-for-build (package-derivation store guile-final)))
345           (expression->derivation-in-linux-vm
346            store "vm-test"
347            '(begin
348               (display "hello from boot!\n")
349               (call-with-output-file "/xchg/hello"
350                 (lambda (p)
351                   (display "world" p)))))))
352       (lambda ()
353         (close-connection store)))))
354
355 (define (example2)
356   (let ((store #f))
357     (dynamic-wind
358       (lambda ()
359         (set! store (open-connection)))
360       (lambda ()
361         (parameterize ((%guile-for-build (package-derivation store guile-final)))
362           (let* ((out   (derivation-path->output-path
363                          (package-derivation store mingetty)))
364                  (getty (string-append out "/sbin/mingetty"))
365                  (boot  (add-text-to-store store "boot"
366                                            (object->string
367                                             `(begin
368                                                ;; Become the session leader,
369                                                ;; so that mingetty can do
370                                                ;; 'TIOCSCTTY'.
371                                                (setsid)
372
373                                                ;; Directly into mingetty.
374                                                (execl ,getty "mingetty"
375                                                       "--noclear" "tty1")))
376                                            (list out))))
377            (qemu-image store
378                        #:initrd gnu-system-initrd
379                        #:linux-arguments `("--root=/dev/vda1"
380                                            ,(string-append "--load=" boot))
381                        #:disk-image-size (* 400 (expt 2 20))
382                        #:inputs-to-copy `(("boot" ,boot)
383                                           ("coreutils" ,coreutils)
384                                           ("bash" ,bash)
385                                           ("guile" ,guile-2.0)
386                                           ("mingetty" ,mingetty))))))
387       (lambda ()
388         (close-connection store)))))
389
390 ;;; vm.scm ends here