1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2013 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 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
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)
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
47 ;;; Tools to evaluate build expressions within virtual machines.
51 (define* (expression->derivation-in-linux-vm store name exp
53 (system (%current-system))
57 (qemu qemu/smb-shares)
64 (references-graphs #f)
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.
72 When MAKE-DISK-IMAGE? is true, then create a QEMU disk image of
73 DISK-IMAGE-SIZE bytes and return it.
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."
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))
89 ;; EXP, but with INPUTS available.
90 `(let ((%build-inputs ',input-alist))
94 ;; Code that launches the VM that evaluates EXP.
96 (use-modules (guix build utils)
100 (let ((out (assoc-ref %outputs "out"))
101 (cu (string-append (assoc-ref %build-inputs "coreutils")
103 (qemu (string-append (assoc-ref %build-inputs "qemu")
105 (car (string-split ,system #\-))))
106 (img (string-append (assoc-ref %build-inputs "qemu")
108 (linux (string-append (assoc-ref %build-inputs "linux")
110 (initrd (string-append (assoc-ref %build-inputs "initrd")
112 (builder (assoc-ref %build-inputs "builder")))
114 ;; XXX: QEMU uses "rm -rf" when it's done to remove the temporary SMB
115 ;; directory, so it really needs `rm' in $PATH.
118 ,(if make-disk-image?
119 `(zero? (system* img "create" "image.qcow2"
120 ,(number->string disk-image-size)))
125 ;; Copy the reference-graph files under xchg/ so EXP can access it.
127 ,@(match references-graphs
128 (((graph-files . _) ...)
131 ,(string-append "xchg/" file)))
136 (system* qemu "-nographic" "-no-reboot"
137 "-net" "nic,model=e1000"
138 "-net" (string-append "user,smb=" (getcwd))
141 "-append" (string-append "console=ttyS0 --load="
143 ,@(if make-disk-image?
144 '("-hda" "image.qcow2")
146 ,(if make-disk-image?
147 '(copy-file "image.qcow2" ; XXX: who mkdir'd OUT?
151 (copy-recursively "xchg" out)))))))
153 (let ((user-builder (add-text-to-store store "builder-in-linux-vm"
154 (object->string exp*)
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)
165 ((name (? package? package)
167 `(,name ,(->drv package)
169 ((name (? string? file))
173 #:modules (delete-duplicates
176 #:guile-for-build guile-for-build
177 #:references-graphs references-graphs)))
179 (define* (qemu-image store #:key
181 (system (%current-system))
182 (disk-image-size (* 100 (expt 2 20)))
184 (linux-arguments '())
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.
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
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)
203 ((input (and (? string?) (? store-path?) file))
206 (expression->derivation-in-linux-vm
209 (use-modules (ice-9 rdelim)
212 (guix build linux-initrd))
214 (let ((parted (string-append (assoc-ref %build-inputs "parted")
216 (mkfs (string-append (assoc-ref %build-inputs "e2fsprogs")
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")
224 (linux (string-append (assoc-ref %build-inputs "linux")
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))
232 (cond ((eof-object? line)
233 (delete-duplicates result))
234 ((string-prefix? "/" line)
235 (loop (read-line port)
238 (loop (read-line port)
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))
247 ,(match inputs-to-copy
248 (((graph-files . _) ...)
249 `(let* ((graph-files ',(map (cut string-append "/xchg/" <>)
251 (paths (append-map graph-from-file graph-files)))
252 (delete-duplicates paths)))
255 ;; GRUB is full of shell scripts.
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"))
264 (display "creating partition table...\n")
265 (and (zero? (system* parted "/dev/vda" "mklabel" "msdos"
266 "mkpart" "primary" "ext2" "1MiB"
269 (* 5 (expt 2 20))))))
271 (display "creating ext3 partition...\n")
272 (and (zero? (system* mkfs "-F" "/dev/vda1"))
274 (display "mounting partition...\n")
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")
281 ;; Populate the image's store.
282 (mkdir-p (string-append "/fs" ,%store-directory))
283 (for-each (lambda (thing)
284 (copy-recursively thing
290 (make-essential-device-nodes #:root "/fs")
292 (call-with-output-file "/fs/boot/grub/grub.cfg"
297 search.file /boot/bzImage
299 menuentry \"Boot-to-Guile! (GNU System technology preview)\" {
300 linux /boot/bzImage ~a
303 ,(string-join linux-arguments))))
305 (system* grub "--no-floppy"
306 "--boot-directory" "/fs/boot"
309 (system* umount "/fs"))
312 #:inputs `(("parted" ,parted)
314 ("e2fsprogs" ,e2fsprogs)
315 ("linux" ,linux-libre)
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)
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))))
335 ;;; Guile 2.0 potluck examples.
342 (set! store (open-connection)))
344 (parameterize ((%guile-for-build (package-derivation store guile-final)))
345 (expression->derivation-in-linux-vm
348 (display "hello from boot!\n")
349 (call-with-output-file "/xchg/hello"
351 (display "world" p)))))))
353 (close-connection store)))))
359 (set! store (open-connection)))
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"
368 ;; Become the session leader,
369 ;; so that mingetty can do
373 ;; Directly into mingetty.
374 (execl ,getty "mingetty"
375 "--noclear" "tty1")))
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)
386 ("mingetty" ,mingetty))))))
388 (close-connection store)))))