doc: Minor improvements in "G-Expressions".
[jackhill/guix/guix.git] / gnu / system / vm.scm
CommitLineData
04086015 1;;; GNU Guix --- Functional package management for GNU
735c6dd7 2;;; Copyright © 2013, 2014 Ludovic Courtès <ludo@gnu.org>
04086015
LC
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)
93d44bd8 20 #:use-module (guix config)
04086015 21 #:use-module (guix store)
02100028 22 #:use-module (guix gexp)
04086015
LC
23 #:use-module (guix derivations)
24 #:use-module (guix packages)
d9f0a237 25 #:use-module (guix monads)
9f84f12f 26 #:use-module ((gnu packages base)
7bd9604c 27 #:select (%final-inputs))
1b89a66e
LC
28 #:use-module (gnu packages guile)
29 #:use-module (gnu packages bash)
4f62d8d6 30 #:use-module (gnu packages less)
04086015
LC
31 #:use-module (gnu packages qemu)
32 #:use-module (gnu packages parted)
5b16ff09 33 #:use-module (gnu packages zile)
04086015
LC
34 #:use-module (gnu packages grub)
35 #:use-module (gnu packages linux)
30f25b03 36 #:use-module (gnu packages package-management)
04086015
LC
37 #:use-module ((gnu packages make-bootstrap)
38 #:select (%guile-static-stripped))
9de46ffb 39 #:use-module (gnu packages admin)
0ded70f3
LC
40
41 #:use-module (gnu system shadow)
42 #:use-module (gnu system linux)
735c6dd7 43 #:use-module (gnu system linux-initrd)
0ded70f3 44 #:use-module (gnu system grub)
033adfe7 45 #:use-module (gnu system)
db4fdc04 46 #:use-module (gnu services)
0ded70f3 47
ca85d7bc 48 #:use-module (srfi srfi-1)
04086015
LC
49 #:use-module (srfi srfi-26)
50 #:use-module (ice-9 match)
0ded70f3 51
04086015 52 #:export (expression->derivation-in-linux-vm
aedb72fb 53 qemu-image
fd3bfc44
LC
54 system-qemu-image
55 system-qemu-image/shared-store
56 system-qemu-image/shared-store-script))
04086015
LC
57
58\f
59;;; Commentary:
60;;;
61;;; Tools to evaluate build expressions within virtual machines.
62;;;
63;;; Code:
64
ef09fdfb
LC
65(define* (input->name+output tuple #:key (system (%current-system)))
66 "Return as a monadic value a name/file-name pair corresponding to TUPLE, an
67input tuple. The output file name is when building for SYSTEM."
68 (with-monad %store-monad
69 (match tuple
70 ((input (? package? package))
71 (mlet %store-monad ((out (package-file package #:system system)))
72 (return `(,input . ,out))))
73 ((input (? package? package) sub-drv)
74 (mlet %store-monad ((out (package-file package
75 #:output sub-drv
76 #:system system)))
77 (return `(,input . ,out))))
78 ((input (? derivation? drv))
79 (return `(,input . ,(derivation->output-path drv))))
80 ((input (? derivation? drv) sub-drv)
81 (return `(,input . ,(derivation->output-path drv sub-drv))))
82 ((input (and (? string?) (? store-path?) file))
83 (return `(,input . ,file))))))
84
83bcd0b8
LC
85(define %linux-vm-file-systems
86 ;; File systems mounted for 'derivation-in-linux-vm'. The store and /xchg
87 ;; directory are shared with the host over 9p.
88 (list (file-system
89 (mount-point (%store-prefix))
90 (device "store")
91 (type "9p")
92 (needed-for-boot? #t)
93 (options "trans=virtio"))
94 (file-system
95 (mount-point "/xchg")
96 (device "xchg")
97 (type "9p")
98 (needed-for-boot? #t)
99 (options "trans=virtio"))))
100
d9f0a237 101(define* (expression->derivation-in-linux-vm name exp
04086015 102 #:key
2455085a 103 (system (%current-system))
04086015 104 (linux linux-libre)
735c6dd7 105 initrd
f200b03e 106 (qemu qemu-headless)
04086015 107 (env-vars '())
1aa0033b 108 (modules
ade5ce7a
LC
109 '((guix build vm)
110 (guix build linux-initrd)
111 (guix build utils)))
04086015
LC
112 (guile-for-build
113 (%guile-for-build))
114
115 (make-disk-image? #f)
ca85d7bc 116 (references-graphs #f)
defa1b9b 117 (memory-size 256)
04086015
LC
118 (disk-image-size
119 (* 100 (expt 2 20))))
735c6dd7 120 "Evaluate EXP in a QEMU virtual machine running LINUX with INITRD (a
1aa0033b 121derivation). In the virtual machine, EXP has access to all its inputs from the
735c6dd7 122store; it should put its output files in the `/xchg' directory, which is
defa1b9b
LC
123copied to the derivation's output when the VM terminates. The virtual machine
124runs with MEMORY-SIZE MiB of memory.
04086015
LC
125
126When MAKE-DISK-IMAGE? is true, then create a QEMU disk image of
ca85d7bc
LC
127DISK-IMAGE-SIZE bytes and return it.
128
1aa0033b 129MODULES is the set of modules imported in the execution environment of EXP.
ade5ce7a 130
ca85d7bc
LC
131When REFERENCES-GRAPHS is true, it must be a list of file name/store path
132pairs, as for `derivation'. The files containing the reference graphs are
133made available under the /xchg CIFS share."
d9f0a237 134 (mlet* %store-monad
1aa0033b
LC
135 ((module-dir (imported-modules modules))
136 (compiled (compiled-modules modules))
137 (user-builder (gexp->file "builder-in-linux-vm" exp))
02100028
LC
138 (loader (gexp->file "linux-vm-loader"
139 #~(begin
140 (set! %load-path
141 (cons #$module-dir %load-path))
142 (set! %load-compiled-path
143 (cons #$compiled
144 %load-compiled-path))
145 (primitive-load #$user-builder))))
d9f0a237 146 (coreutils -> (car (assoc-ref %final-inputs "coreutils")))
d4254711 147 (initrd (if initrd ; use the default initrd?
735c6dd7 148 (return initrd)
83bcd0b8
LC
149 (qemu-initrd %linux-vm-file-systems
150 #:guile-modules-in-chroot? #t))))
1aa0033b
LC
151
152 (define builder
153 ;; Code that launches the VM that evaluates EXP.
154 #~(begin
155 (use-modules (guix build utils)
156 (guix build vm))
157
158 (let ((inputs '#$(list qemu coreutils))
159 (linux (string-append #$linux "/bzImage"))
160 (initrd (string-append #$initrd "/initrd"))
161 (loader #$loader)
162 (graphs '#$(match references-graphs
163 (((graph-files . _) ...) graph-files)
164 (_ #f))))
165
166 (set-path-environment-variable "PATH" '("bin") inputs)
167
168 (load-in-linux-vm loader
169 #:output #$output
170 #:linux linux #:initrd initrd
171 #:memory-size #$memory-size
172 #:make-disk-image? #$make-disk-image?
173 #:disk-image-size #$disk-image-size
174 #:references-graphs graphs))))
175
176 (gexp->derivation name builder
177 ;; TODO: Require the "kvm" feature.
178 #:system system
179 #:env-vars env-vars
180 #:modules `((guix build utils)
181 (guix build vm)
182 (guix build linux-initrd))
183 #:guile-for-build guile-for-build
184 #:references-graphs references-graphs)))
d9f0a237
LC
185
186(define* (qemu-image #:key
04086015
LC
187 (name "qemu-image")
188 (system (%current-system))
1aa0033b 189 (qemu qemu-headless)
04086015 190 (disk-image-size (* 100 (expt 2 20)))
0e2ddecd 191 grub-configuration
30f25b03 192 (initialize-store? #f)
785859d3 193 (populate #f)
002e5ba8 194 (inputs-to-copy '()))
1b89a66e 195 "Return a bootable, stand-alone QEMU image. The returned image is a full
0e2ddecd 196disk image, with a GRUB installation that uses GRUB-CONFIGURATION as its
033adfe7 197configuration file (GRUB-CONFIGURATION must be the name of a file in the VM.)
93d44bd8
LC
198
199INPUTS-TO-COPY is a list of inputs (as for packages) whose closure is copied
30f25b03
LC
200into the image being built. When INITIALIZE-STORE? is true, initialize the
201store database in the image so that Guix can be used in the image.
785859d3 202
d5d0f286
LC
203POPULATE is a list of directives stating directories or symlinks to be created
204in the disk image partition. It is evaluated once the image has been
205populated with INPUTS-TO-COPY. It can be used to provide additional files,
206such as /etc files."
d9f0a237
LC
207 (mlet %store-monad
208 ((graph (sequence %store-monad
ef09fdfb 209 (map input->name+output inputs-to-copy))))
d9f0a237 210 (expression->derivation-in-linux-vm
1aa0033b
LC
211 name
212 #~(begin
213 (use-modules (guix build vm)
214 (guix build utils))
215
216 (let ((inputs
217 '#$(append (list qemu parted grub e2fsprogs util-linux)
218 (map (compose car (cut assoc-ref %final-inputs <>))
219 '("sed" "grep" "coreutils" "findutils" "gawk"))
220 (if initialize-store? (list guix) '())))
221
222 ;; This variable is unused but allows us to add INPUTS-TO-COPY
223 ;; as inputs.
224 (to-copy
225 '#$(map (match-lambda
226 ((name thing) thing)
227 ((name thing output) `(,thing ,output)))
228 inputs-to-copy)))
229
230 (set-path-environment-variable "PATH" '("bin" "sbin") inputs)
231
232 (let ((graphs '#$(match inputs-to-copy
233 (((names . _) ...)
234 names))))
235 (initialize-hard-disk #:grub.cfg #$grub-configuration
236 #:closures-to-copy graphs
237 #:disk-image-size #$disk-image-size
238 #:initialize-store? #$initialize-store?
239 #:directives '#$populate)
240 (reboot))))
d9f0a237 241 #:system system
d9f0a237
LC
242 #:make-disk-image? #t
243 #:disk-image-size disk-image-size
ade5ce7a 244 #:references-graphs graph)))
04086015
LC
245
246\f
247;;;
aedb72fb 248;;; Stand-alone VM image.
04086015
LC
249;;;
250
fd3bfc44
LC
251(define (operating-system-build-gid os)
252 "Return as a monadic value the group id for build users of OS, or #f."
253 (anym %store-monad
254 (lambda (service)
255 (and (equal? '(guix-daemon)
256 (service-provision service))
257 (match (service-user-groups service)
258 ((group)
259 (user-group-id group)))))
260 (operating-system-services os)))
261
262(define (operating-system-default-contents os)
263 "Return a list of directives suitable for 'system-qemu-image' describing the
264basic contents of the root file system of OS."
682b6599
LC
265 (define (user-directories user)
266 (let ((home (user-account-home-directory user))
267 ;; XXX: Deal with automatically allocated ids.
268 (uid (or (user-account-uid user) 0))
269 (gid (or (user-account-gid user) 0))
2e4e01ee 270 (root (string-append "/var/guix/profiles/per-user/"
682b6599 271 (user-account-name user))))
1aa0033b
LC
272 #~((directory #$root #$uid #$gid)
273 (directory #$home #$uid #$gid))))
682b6599 274
f6a9d048 275 (mlet* %store-monad ((os-drv (operating-system-derivation os))
f6a9d048 276 (build-gid (operating-system-build-gid os))
1aa0033b
LC
277 (profile (operating-system-profile os)))
278 (return #~((directory #$(%store-prefix) 0 #$(or build-gid 0))
279 (directory "/etc")
280 (directory "/var/log") ; for dmd
281 (directory "/var/run/nscd")
282 (directory "/var/guix/gcroots")
283 ("/var/guix/gcroots/system" -> #$os-drv)
284 (directory "/run")
285 ("/run/current-system" -> #$profile)
286 (directory "/bin")
287 ("/bin/sh" -> "/run/current-system/bin/bash")
288 (directory "/tmp")
289 (directory "/var/guix/profiles/per-user/root" 0 0)
290
291 (directory "/root" 0 0) ; an exception
292 #$@(append-map user-directories
293 (operating-system-users os))))))
fd3bfc44 294
0b14d1d7 295(define* (system-qemu-image os
22dd0438
LC
296 #:key (disk-image-size (* 900 (expt 2 20))))
297 "Return the derivation of a QEMU image of DISK-IMAGE-SIZE bytes of the GNU
298system as described by OS."
0b8a376b 299 (mlet* %store-monad
033adfe7
LC
300 ((os-drv (operating-system-derivation os))
301 (os-dir -> (derivation->output-path os-drv))
302 (grub.cfg -> (string-append os-dir "/grub.cfg"))
fd3bfc44 303 (populate (operating-system-default-contents os)))
d9f0a237
LC
304 (qemu-image #:grub-configuration grub.cfg
305 #:populate populate
22dd0438 306 #:disk-image-size disk-image-size
d9f0a237 307 #:initialize-store? #t
033adfe7 308 #:inputs-to-copy `(("system" ,os-drv)))))
04086015 309
83bcd0b8
LC
310(define (virtualized-operating-system os)
311 "Return an operating system based on OS suitable for use in a virtualized
312environment with the store shared with the host."
313 (operating-system (inherit os)
314 (initrd (cut qemu-initrd <> #:volatile-root? #t))
315 (file-systems (list (file-system
316 (mount-point "/")
317 (device "/dev/vda1")
318 (type "ext3"))
319 (file-system
320 (mount-point (%store-prefix))
321 (device "store")
322 (type "9p")
323 (needed-for-boot? #t)
324 (options "trans=virtio"))))))
325
fd3bfc44 326(define* (system-qemu-image/shared-store
0b14d1d7 327 os
fd3bfc44
LC
328 #:key (disk-image-size (* 15 (expt 2 20))))
329 "Return a derivation that builds a QEMU image of OS that shares its store
330with the host."
331 (mlet* %store-monad
332 ((os-drv (operating-system-derivation os))
333 (os-dir -> (derivation->output-path os-drv))
334 (grub.cfg -> (string-append os-dir "/grub.cfg"))
335 (populate (operating-system-default-contents os)))
336 ;; TODO: Initialize the database so Guix can be used in the guest.
337 (qemu-image #:grub-configuration grub.cfg
338 #:populate populate
339 #:disk-image-size disk-image-size)))
340
341(define* (system-qemu-image/shared-store-script
0b14d1d7 342 os
fd3bfc44 343 #:key
1f3838ac 344 (qemu qemu)
fd3bfc44
LC
345 (graphic? #t))
346 "Return a derivation that builds a script to run a virtual machine image of
347OS that shares its store with the host."
c47f0d8b 348 (mlet* %store-monad
83bcd0b8 349 ((os -> (virtualized-operating-system os))
c47f0d8b 350 (os-drv (operating-system-derivation os))
c47f0d8b 351 (image (system-qemu-image/shared-store os)))
fd3bfc44 352 (define builder
02100028
LC
353 #~(call-with-output-file #$output
354 (lambda (port)
355 (display
356 (string-append "#!" #$bash "/bin/sh
357exec " #$qemu "/bin/qemu-system-x86_64 -enable-kvm -no-reboot -net nic,model=virtio \
358 -virtfs local,path=" #$(%store-prefix) ",security_model=none,mount_tag=store \
1f3838ac 359 -net user \
02100028 360 -kernel " #$(operating-system-kernel os) "/bzImage \
83bcd0b8 361 -initrd " #$os-drv "/initrd \
02100028
LC
362-append \"" #$(if graphic? "" "console=ttyS0 ")
363 "--load=" #$os-drv "/boot --root=/dev/vda1\" \
364 -drive file=" #$image
fd3bfc44 365 ",if=virtio,cache=writeback,werror=report,readonly\n")
02100028
LC
366 port)
367 (chmod port #o555))))
368
369 (gexp->derivation "run-vm.sh" builder)))
fd3bfc44 370
04086015 371;;; vm.scm ends here