Commit | Line | Data |
---|---|---|
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 | |
67 | input 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) | |
3c05b4bc LC |
93 | (options "trans=virtio") |
94 | (check? #f)) | |
83bcd0b8 LC |
95 | (file-system |
96 | (mount-point "/xchg") | |
97 | (device "xchg") | |
98 | (type "9p") | |
99 | (needed-for-boot? #t) | |
3c05b4bc LC |
100 | (options "trans=virtio") |
101 | (check? #f)))) | |
83bcd0b8 | 102 | |
d9f0a237 | 103 | (define* (expression->derivation-in-linux-vm name exp |
04086015 | 104 | #:key |
2455085a | 105 | (system (%current-system)) |
04086015 | 106 | (linux linux-libre) |
735c6dd7 | 107 | initrd |
f200b03e | 108 | (qemu qemu-headless) |
04086015 | 109 | (env-vars '()) |
1aa0033b | 110 | (modules |
ade5ce7a LC |
111 | '((guix build vm) |
112 | (guix build linux-initrd) | |
113 | (guix build utils))) | |
04086015 LC |
114 | (guile-for-build |
115 | (%guile-for-build)) | |
116 | ||
117 | (make-disk-image? #f) | |
ca85d7bc | 118 | (references-graphs #f) |
defa1b9b | 119 | (memory-size 256) |
04086015 LC |
120 | (disk-image-size |
121 | (* 100 (expt 2 20)))) | |
735c6dd7 | 122 | "Evaluate EXP in a QEMU virtual machine running LINUX with INITRD (a |
1aa0033b | 123 | derivation). In the virtual machine, EXP has access to all its inputs from the |
735c6dd7 | 124 | store; it should put its output files in the `/xchg' directory, which is |
defa1b9b LC |
125 | copied to the derivation's output when the VM terminates. The virtual machine |
126 | runs with MEMORY-SIZE MiB of memory. | |
04086015 LC |
127 | |
128 | When MAKE-DISK-IMAGE? is true, then create a QEMU disk image of | |
ca85d7bc LC |
129 | DISK-IMAGE-SIZE bytes and return it. |
130 | ||
1aa0033b | 131 | MODULES is the set of modules imported in the execution environment of EXP. |
ade5ce7a | 132 | |
ca85d7bc LC |
133 | When REFERENCES-GRAPHS is true, it must be a list of file name/store path |
134 | pairs, as for `derivation'. The files containing the reference graphs are | |
135 | made available under the /xchg CIFS share." | |
d9f0a237 | 136 | (mlet* %store-monad |
1aa0033b LC |
137 | ((module-dir (imported-modules modules)) |
138 | (compiled (compiled-modules modules)) | |
139 | (user-builder (gexp->file "builder-in-linux-vm" exp)) | |
02100028 LC |
140 | (loader (gexp->file "linux-vm-loader" |
141 | #~(begin | |
142 | (set! %load-path | |
143 | (cons #$module-dir %load-path)) | |
144 | (set! %load-compiled-path | |
145 | (cons #$compiled | |
146 | %load-compiled-path)) | |
147 | (primitive-load #$user-builder)))) | |
d9f0a237 | 148 | (coreutils -> (car (assoc-ref %final-inputs "coreutils"))) |
d4254711 | 149 | (initrd (if initrd ; use the default initrd? |
735c6dd7 | 150 | (return initrd) |
83bcd0b8 LC |
151 | (qemu-initrd %linux-vm-file-systems |
152 | #:guile-modules-in-chroot? #t)))) | |
1aa0033b LC |
153 | |
154 | (define builder | |
155 | ;; Code that launches the VM that evaluates EXP. | |
156 | #~(begin | |
157 | (use-modules (guix build utils) | |
158 | (guix build vm)) | |
159 | ||
160 | (let ((inputs '#$(list qemu coreutils)) | |
161 | (linux (string-append #$linux "/bzImage")) | |
162 | (initrd (string-append #$initrd "/initrd")) | |
163 | (loader #$loader) | |
164 | (graphs '#$(match references-graphs | |
165 | (((graph-files . _) ...) graph-files) | |
166 | (_ #f)))) | |
167 | ||
168 | (set-path-environment-variable "PATH" '("bin") inputs) | |
169 | ||
170 | (load-in-linux-vm loader | |
171 | #:output #$output | |
172 | #:linux linux #:initrd initrd | |
173 | #:memory-size #$memory-size | |
174 | #:make-disk-image? #$make-disk-image? | |
175 | #:disk-image-size #$disk-image-size | |
176 | #:references-graphs graphs)))) | |
177 | ||
178 | (gexp->derivation name builder | |
179 | ;; TODO: Require the "kvm" feature. | |
180 | #:system system | |
181 | #:env-vars env-vars | |
182 | #:modules `((guix build utils) | |
183 | (guix build vm) | |
184 | (guix build linux-initrd)) | |
185 | #:guile-for-build guile-for-build | |
186 | #:references-graphs references-graphs))) | |
d9f0a237 LC |
187 | |
188 | (define* (qemu-image #:key | |
04086015 LC |
189 | (name "qemu-image") |
190 | (system (%current-system)) | |
1aa0033b | 191 | (qemu qemu-headless) |
04086015 | 192 | (disk-image-size (* 100 (expt 2 20))) |
03ddfaf5 | 193 | (file-system-type "ext4") |
0e2ddecd | 194 | grub-configuration |
30f25b03 | 195 | (initialize-store? #f) |
785859d3 | 196 | (populate #f) |
002e5ba8 | 197 | (inputs-to-copy '())) |
03ddfaf5 LC |
198 | "Return a bootable, stand-alone QEMU image, with a root partition of type |
199 | FILE-SYSTEM-TYPE. The returned image is a full disk image, with a GRUB | |
200 | installation that uses GRUB-CONFIGURATION as its configuration | |
201 | file (GRUB-CONFIGURATION must be the name of a file in the VM.) | |
93d44bd8 LC |
202 | |
203 | INPUTS-TO-COPY is a list of inputs (as for packages) whose closure is copied | |
30f25b03 LC |
204 | into the image being built. When INITIALIZE-STORE? is true, initialize the |
205 | store database in the image so that Guix can be used in the image. | |
785859d3 | 206 | |
d5d0f286 LC |
207 | POPULATE is a list of directives stating directories or symlinks to be created |
208 | in the disk image partition. It is evaluated once the image has been | |
209 | populated with INPUTS-TO-COPY. It can be used to provide additional files, | |
210 | such as /etc files." | |
d9f0a237 LC |
211 | (mlet %store-monad |
212 | ((graph (sequence %store-monad | |
ef09fdfb | 213 | (map input->name+output inputs-to-copy)))) |
d9f0a237 | 214 | (expression->derivation-in-linux-vm |
1aa0033b LC |
215 | name |
216 | #~(begin | |
217 | (use-modules (guix build vm) | |
218 | (guix build utils)) | |
219 | ||
220 | (let ((inputs | |
221 | '#$(append (list qemu parted grub e2fsprogs util-linux) | |
222 | (map (compose car (cut assoc-ref %final-inputs <>)) | |
223 | '("sed" "grep" "coreutils" "findutils" "gawk")) | |
224 | (if initialize-store? (list guix) '()))) | |
225 | ||
226 | ;; This variable is unused but allows us to add INPUTS-TO-COPY | |
227 | ;; as inputs. | |
228 | (to-copy | |
229 | '#$(map (match-lambda | |
230 | ((name thing) thing) | |
231 | ((name thing output) `(,thing ,output))) | |
232 | inputs-to-copy))) | |
233 | ||
234 | (set-path-environment-variable "PATH" '("bin" "sbin") inputs) | |
235 | ||
236 | (let ((graphs '#$(match inputs-to-copy | |
237 | (((names . _) ...) | |
238 | names)))) | |
239 | (initialize-hard-disk #:grub.cfg #$grub-configuration | |
240 | #:closures-to-copy graphs | |
241 | #:disk-image-size #$disk-image-size | |
03ddfaf5 | 242 | #:file-system-type #$file-system-type |
1aa0033b LC |
243 | #:initialize-store? #$initialize-store? |
244 | #:directives '#$populate) | |
245 | (reboot)))) | |
d9f0a237 | 246 | #:system system |
d9f0a237 LC |
247 | #:make-disk-image? #t |
248 | #:disk-image-size disk-image-size | |
ade5ce7a | 249 | #:references-graphs graph))) |
04086015 LC |
250 | |
251 | \f | |
252 | ;;; | |
aedb72fb | 253 | ;;; Stand-alone VM image. |
04086015 LC |
254 | ;;; |
255 | ||
fd3bfc44 LC |
256 | (define (operating-system-build-gid os) |
257 | "Return as a monadic value the group id for build users of OS, or #f." | |
f3bde2ff LC |
258 | (mlet %store-monad ((services (operating-system-services os))) |
259 | (return (any (lambda (service) | |
260 | (and (equal? '(guix-daemon) | |
261 | (service-provision service)) | |
262 | (match (service-user-groups service) | |
263 | ((group) | |
264 | (user-group-id group))))) | |
265 | services)))) | |
fd3bfc44 LC |
266 | |
267 | (define (operating-system-default-contents os) | |
268 | "Return a list of directives suitable for 'system-qemu-image' describing the | |
269 | basic contents of the root file system of OS." | |
f6a9d048 | 270 | (mlet* %store-monad ((os-drv (operating-system-derivation os)) |
f6a9d048 | 271 | (build-gid (operating-system-build-gid os)) |
1aa0033b LC |
272 | (profile (operating-system-profile os))) |
273 | (return #~((directory #$(%store-prefix) 0 #$(or build-gid 0)) | |
274 | (directory "/etc") | |
275 | (directory "/var/log") ; for dmd | |
276 | (directory "/var/run/nscd") | |
277 | (directory "/var/guix/gcroots") | |
278 | ("/var/guix/gcroots/system" -> #$os-drv) | |
279 | (directory "/run") | |
280 | ("/run/current-system" -> #$profile) | |
281 | (directory "/bin") | |
282 | ("/bin/sh" -> "/run/current-system/bin/bash") | |
283 | (directory "/tmp") | |
284 | (directory "/var/guix/profiles/per-user/root" 0 0) | |
285 | ||
ab6a279a LC |
286 | (directory "/root" 0 0) ; an exception |
287 | (directory "/home" 0 0))))) | |
fd3bfc44 | 288 | |
0b14d1d7 | 289 | (define* (system-qemu-image os |
66f23d66 LC |
290 | #:key |
291 | (file-system-type "ext4") | |
292 | (disk-image-size (* 900 (expt 2 20)))) | |
293 | "Return the derivation of a freestanding QEMU image of DISK-IMAGE-SIZE bytes | |
294 | of the GNU system as described by OS." | |
295 | (let ((os (operating-system (inherit os) | |
296 | ;; The mounted file systems are under our control. | |
297 | (file-systems (list (file-system | |
298 | (mount-point "/") | |
299 | (device "/dev/sda1") | |
300 | (type file-system-type))))))) | |
301 | (mlet* %store-monad | |
302 | ((os-drv (operating-system-derivation os)) | |
303 | (os-dir -> (derivation->output-path os-drv)) | |
304 | (grub.cfg -> (string-append os-dir "/grub.cfg")) | |
305 | (populate (operating-system-default-contents os))) | |
306 | (qemu-image #:grub-configuration grub.cfg | |
307 | #:populate populate | |
308 | #:disk-image-size disk-image-size | |
309 | #:file-system-type file-system-type | |
310 | #:initialize-store? #t | |
311 | #:inputs-to-copy `(("system" ,os-drv)))))) | |
04086015 | 312 | |
83bcd0b8 LC |
313 | (define (virtualized-operating-system os) |
314 | "Return an operating system based on OS suitable for use in a virtualized | |
315 | environment with the store shared with the host." | |
316 | (operating-system (inherit os) | |
317 | (initrd (cut qemu-initrd <> #:volatile-root? #t)) | |
318 | (file-systems (list (file-system | |
319 | (mount-point "/") | |
320 | (device "/dev/vda1") | |
03ddfaf5 | 321 | (type "ext4")) |
83bcd0b8 LC |
322 | (file-system |
323 | (mount-point (%store-prefix)) | |
324 | (device "store") | |
325 | (type "9p") | |
326 | (needed-for-boot? #t) | |
3c05b4bc LC |
327 | (options "trans=virtio") |
328 | (check? #f)))))) | |
83bcd0b8 | 329 | |
fd3bfc44 | 330 | (define* (system-qemu-image/shared-store |
0b14d1d7 | 331 | os |
fd3bfc44 LC |
332 | #:key (disk-image-size (* 15 (expt 2 20)))) |
333 | "Return a derivation that builds a QEMU image of OS that shares its store | |
334 | with the host." | |
335 | (mlet* %store-monad | |
336 | ((os-drv (operating-system-derivation os)) | |
337 | (os-dir -> (derivation->output-path os-drv)) | |
338 | (grub.cfg -> (string-append os-dir "/grub.cfg")) | |
339 | (populate (operating-system-default-contents os))) | |
340 | ;; TODO: Initialize the database so Guix can be used in the guest. | |
341 | (qemu-image #:grub-configuration grub.cfg | |
342 | #:populate populate | |
343 | #:disk-image-size disk-image-size))) | |
344 | ||
345 | (define* (system-qemu-image/shared-store-script | |
0b14d1d7 | 346 | os |
fd3bfc44 | 347 | #:key |
1f3838ac | 348 | (qemu qemu) |
fd3bfc44 LC |
349 | (graphic? #t)) |
350 | "Return a derivation that builds a script to run a virtual machine image of | |
351 | OS that shares its store with the host." | |
c47f0d8b | 352 | (mlet* %store-monad |
83bcd0b8 | 353 | ((os -> (virtualized-operating-system os)) |
c47f0d8b | 354 | (os-drv (operating-system-derivation os)) |
c47f0d8b | 355 | (image (system-qemu-image/shared-store os))) |
fd3bfc44 | 356 | (define builder |
02100028 LC |
357 | #~(call-with-output-file #$output |
358 | (lambda (port) | |
359 | (display | |
360 | (string-append "#!" #$bash "/bin/sh | |
361 | exec " #$qemu "/bin/qemu-system-x86_64 -enable-kvm -no-reboot -net nic,model=virtio \ | |
362 | -virtfs local,path=" #$(%store-prefix) ",security_model=none,mount_tag=store \ | |
1f3838ac | 363 | -net user \ |
02100028 | 364 | -kernel " #$(operating-system-kernel os) "/bzImage \ |
83bcd0b8 | 365 | -initrd " #$os-drv "/initrd \ |
02100028 LC |
366 | -append \"" #$(if graphic? "" "console=ttyS0 ") |
367 | "--load=" #$os-drv "/boot --root=/dev/vda1\" \ | |
368 | -drive file=" #$image | |
fd3bfc44 | 369 | ",if=virtio,cache=writeback,werror=report,readonly\n") |
02100028 LC |
370 | port) |
371 | (chmod port #o555)))) | |
372 | ||
373 | (gexp->derivation "run-vm.sh" builder))) | |
fd3bfc44 | 374 | |
04086015 | 375 | ;;; vm.scm ends here |