98 lines
3.5 KiB
Scheme
98 lines
3.5 KiB
Scheme
|
;;; GNU Guix --- Functional package management for GNU
|
||
|
;;; Copyright © 2013, 2014 Ludovic Courtès <ludo@gnu.org>
|
||
|
;;;
|
||
|
;;; This file is part of GNU Guix.
|
||
|
;;;
|
||
|
;;; GNU Guix is free software; you can redistribute it and/or modify it
|
||
|
;;; under the terms of the GNU General Public License as published by
|
||
|
;;; the Free Software Foundation; either version 3 of the License, or (at
|
||
|
;;; your option) any later version.
|
||
|
;;;
|
||
|
;;; GNU Guix is distributed in the hope that it will be useful, but
|
||
|
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
|
;;; GNU General Public License for more details.
|
||
|
;;;
|
||
|
;;; You should have received a copy of the GNU General Public License
|
||
|
;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
|
||
|
|
||
|
(define-module (guix build vm)
|
||
|
#:use-module (ice-9 match)
|
||
|
#:use-module (guix build utils)
|
||
|
#:export (load-in-linux-vm))
|
||
|
|
||
|
;;; Commentary:
|
||
|
;;;
|
||
|
;;; This module provides supporting code to run virtual machines and build
|
||
|
;;; virtual machine images using QEMU.
|
||
|
;;;
|
||
|
;;; Code:
|
||
|
|
||
|
(define (qemu-command)
|
||
|
"Return the default name of the QEMU command for the current host."
|
||
|
(string-append "qemu-system-"
|
||
|
(substring %host-type 0
|
||
|
(string-index %host-type #\-))))
|
||
|
|
||
|
|
||
|
(define* (load-in-linux-vm builder
|
||
|
#:key
|
||
|
output
|
||
|
(qemu (qemu-command)) (memory-size 512)
|
||
|
linux initrd
|
||
|
make-disk-image? (disk-image-size 100)
|
||
|
(references-graphs '()))
|
||
|
"Run BUILDER, a Scheme file, into a VM running LINUX with INITRD, and copy
|
||
|
the result to OUTPUT.
|
||
|
|
||
|
When MAKE-DISK-IMAGE? is true, OUTPUT will contain a VM image of
|
||
|
DISK-IMAGE-SIZE MiB resulting from the execution of BUILDER, which may access
|
||
|
it via /dev/hda.
|
||
|
|
||
|
REFERENCES-GRAPHS can specify a list of reference-graph files as produced by
|
||
|
the #:references-graphs parameter of 'derivation'."
|
||
|
|
||
|
(when make-disk-image?
|
||
|
(unless (zero? (system* "qemu-img" "create" "-f" "qcow2" "image.qcow2"
|
||
|
(number->string disk-image-size)))
|
||
|
(error "qemu-img failed")))
|
||
|
|
||
|
(mkdir "xchg")
|
||
|
|
||
|
(match references-graphs
|
||
|
((graph-files ...)
|
||
|
;; Copy the reference-graph files under xchg/ so EXP can access it.
|
||
|
(map (lambda (file)
|
||
|
(copy-file file (string-append "xchg/" file)))
|
||
|
graph-files))
|
||
|
(_ #f))
|
||
|
|
||
|
(unless (zero?
|
||
|
(apply system* qemu "-enable-kvm" "-nographic" "-no-reboot"
|
||
|
"-m" (number->string memory-size)
|
||
|
"-net" "nic,model=virtio"
|
||
|
"-virtfs"
|
||
|
(string-append "local,id=store_dev,path="
|
||
|
(%store-directory)
|
||
|
",security_model=none,mount_tag=store")
|
||
|
"-virtfs"
|
||
|
(string-append "local,id=xchg_dev,path=xchg"
|
||
|
",security_model=none,mount_tag=xchg")
|
||
|
"-kernel" linux
|
||
|
"-initrd" initrd
|
||
|
"-append" (string-append "console=ttyS0 --load="
|
||
|
builder)
|
||
|
(if make-disk-image?
|
||
|
'("-hda" "image.qcow2")
|
||
|
'())))
|
||
|
(error "qemu failed" qemu))
|
||
|
|
||
|
(if make-disk-image?
|
||
|
(copy-file "image.qcow2" ; XXX: who mkdir'd OUTPUT?
|
||
|
output)
|
||
|
(begin
|
||
|
(mkdir output)
|
||
|
(copy-recursively "xchg" output))))
|
||
|
|
||
|
;;; vm.scm ends here
|