vm.scm 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2013, 2014, 2015, 2016, 2017 Ludovic Courtès <ludo@gnu.org>
  3. ;;; Copyright © 2016 Christopher Allan Webber <cwebber@dustycloud.org>
  4. ;;; Copyright © 2016, 2017 Leo Famulari <leo@famulari.name>
  5. ;;; Copyright © 2017 Mathieu Othacehe <m.othacehe@gmail.com>
  6. ;;; Copyright © 2017 Marius Bakke <mbakke@fastmail.com>
  7. ;;;
  8. ;;; This file is part of GNU Guix.
  9. ;;;
  10. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  11. ;;; under the terms of the GNU General Public License as published by
  12. ;;; the Free Software Foundation; either version 3 of the License, or (at
  13. ;;; your option) any later version.
  14. ;;;
  15. ;;; GNU Guix is distributed in the hope that it will be useful, but
  16. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. ;;; GNU General Public License for more details.
  19. ;;;
  20. ;;; You should have received a copy of the GNU General Public License
  21. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  22. (define-module (gnu system vm)
  23. #:use-module (guix config)
  24. #:use-module (guix store)
  25. #:use-module (guix gexp)
  26. #:use-module (guix derivations)
  27. #:use-module (guix packages)
  28. #:use-module (guix monads)
  29. #:use-module (guix records)
  30. #:use-module (guix modules)
  31. #:use-module (guix utils)
  32. #:use-module ((gnu build vm)
  33. #:select (qemu-command))
  34. #:use-module (gnu packages base)
  35. #:use-module (gnu packages bootloaders)
  36. #:use-module (gnu packages cdrom)
  37. #:use-module (gnu packages guile)
  38. #:use-module (gnu packages gawk)
  39. #:use-module (gnu packages bash)
  40. #:use-module (gnu packages less)
  41. #:use-module (gnu packages virtualization)
  42. #:use-module (gnu packages disk)
  43. #:use-module (gnu packages zile)
  44. #:use-module (gnu packages linux)
  45. #:use-module (gnu packages package-management)
  46. #:use-module ((gnu packages make-bootstrap)
  47. #:select (%guile-static-stripped))
  48. #:use-module (gnu packages admin)
  49. #:use-module (gnu bootloader)
  50. #:use-module (gnu bootloader grub)
  51. #:use-module (gnu system shadow)
  52. #:use-module (gnu system pam)
  53. #:use-module (gnu system linux-initrd)
  54. #:use-module (gnu bootloader)
  55. #:use-module (gnu system file-systems)
  56. #:use-module (gnu system)
  57. #:use-module (gnu services)
  58. #:use-module (gnu system uuid)
  59. #:use-module (srfi srfi-1)
  60. #:use-module (srfi srfi-26)
  61. #:use-module (rnrs bytevectors)
  62. #:use-module (ice-9 match)
  63. #:export (expression->derivation-in-linux-vm
  64. qemu-image
  65. virtualized-operating-system
  66. system-qemu-image
  67. system-qemu-image/shared-store
  68. system-qemu-image/shared-store-script
  69. system-disk-image
  70. virtual-machine
  71. virtual-machine?))
  72. ;;; Commentary:
  73. ;;;
  74. ;;; Tools to evaluate build expressions within virtual machines.
  75. ;;;
  76. ;;; Code:
  77. (define %linux-vm-file-systems
  78. ;; File systems mounted for 'derivation-in-linux-vm'. The store and /xchg
  79. ;; directory are shared with the host over 9p.
  80. (list (file-system
  81. (mount-point (%store-prefix))
  82. (device "store")
  83. (type "9p")
  84. (needed-for-boot? #t)
  85. (options "trans=virtio")
  86. (check? #f))
  87. (file-system
  88. (mount-point "/xchg")
  89. (device "xchg")
  90. (type "9p")
  91. (needed-for-boot? #t)
  92. (options "trans=virtio")
  93. (check? #f))))
  94. (define* (expression->derivation-in-linux-vm name exp
  95. #:key
  96. (system (%current-system))
  97. (linux linux-libre)
  98. initrd
  99. (qemu qemu-minimal)
  100. (env-vars '())
  101. (guile-for-build
  102. (%guile-for-build))
  103. (single-file-output? #f)
  104. (make-disk-image? #f)
  105. (references-graphs #f)
  106. (memory-size 256)
  107. (disk-image-format "qcow2")
  108. (disk-image-size 'guess))
  109. "Evaluate EXP in a QEMU virtual machine running LINUX with INITRD (a
  110. derivation). The virtual machine runs with MEMORY-SIZE MiB of memory. In the
  111. virtual machine, EXP has access to all its inputs from the store; it should
  112. put its output file(s) in the '/xchg' directory.
  113. If SINGLE-FILE-OUTPUT? is true, copy a single file from '/xchg' to OUTPUT.
  114. Otherwise, copy the contents of /xchg to a new directory OUTPUT.
  115. When MAKE-DISK-IMAGE? is true, then create a QEMU disk image of type
  116. DISK-IMAGE-FORMAT (e.g., 'qcow2' or 'raw'), of DISK-IMAGE-SIZE bytes and
  117. return it. When DISK-IMAGE-SIZE is 'guess, estimate the image size based
  118. based on the size of the closure of REFERENCES-GRAPHS.
  119. When REFERENCES-GRAPHS is true, it must be a list of file name/store path
  120. pairs, as for `derivation'. The files containing the reference graphs are
  121. made available under the /xchg CIFS share."
  122. (mlet* %store-monad
  123. ((user-builder (gexp->file "builder-in-linux-vm" exp))
  124. (loader (gexp->file "linux-vm-loader"
  125. #~(primitive-load #$user-builder)))
  126. (coreutils -> (canonical-package coreutils))
  127. (initrd (if initrd ; use the default initrd?
  128. (return initrd)
  129. (base-initrd %linux-vm-file-systems
  130. #:linux linux
  131. #:virtio? #t
  132. #:qemu-networking? #t))))
  133. (define builder
  134. ;; Code that launches the VM that evaluates EXP.
  135. (with-imported-modules (source-module-closure '((guix build utils)
  136. (gnu build vm)))
  137. #~(begin
  138. (use-modules (guix build utils)
  139. (gnu build vm))
  140. (let* ((inputs '#$(list qemu coreutils))
  141. (linux (string-append #$linux "/"
  142. #$(system-linux-image-file-name)))
  143. (initrd (string-append #$initrd "/initrd"))
  144. (loader #$loader)
  145. (graphs '#$(match references-graphs
  146. (((graph-files . _) ...) graph-files)
  147. (_ #f)))
  148. (size #$(if (eq? 'guess disk-image-size)
  149. #~(+ (* 70 (expt 2 20)) ;ESP
  150. (estimated-partition-size graphs))
  151. disk-image-size)))
  152. (set-path-environment-variable "PATH" '("bin") inputs)
  153. (load-in-linux-vm loader
  154. #:output #$output
  155. #:linux linux #:initrd initrd
  156. #:memory-size #$memory-size
  157. #:make-disk-image? #$make-disk-image?
  158. #:single-file-output? #$single-file-output?
  159. ;; FIXME: ‘target-arm32?’ may not operate on
  160. ;; the right system/target values. Rewrite
  161. ;; using ‘let-system’ when available.
  162. #:target-arm32? #$(target-arm32?)
  163. #:disk-image-format #$disk-image-format
  164. #:disk-image-size size
  165. #:references-graphs graphs)))))
  166. (gexp->derivation name builder
  167. ;; TODO: Require the "kvm" feature.
  168. #:system system
  169. #:env-vars env-vars
  170. #:guile-for-build guile-for-build
  171. #:references-graphs references-graphs)))
  172. (define* (iso9660-image #:key
  173. (name "iso9660-image")
  174. file-system-label
  175. file-system-uuid
  176. (system (%current-system))
  177. (qemu qemu-minimal)
  178. os-drv
  179. bootcfg-drv
  180. bootloader
  181. register-closures?
  182. (inputs '()))
  183. "Return a bootable, stand-alone iso9660 image.
  184. INPUTS is a list of inputs (as for packages)."
  185. (expression->derivation-in-linux-vm
  186. name
  187. (with-imported-modules (source-module-closure '((gnu build vm)
  188. (guix build utils)))
  189. #~(begin
  190. (use-modules (gnu build vm)
  191. (guix build utils))
  192. (let ((inputs
  193. '#$(append (list qemu parted e2fsprogs dosfstools xorriso)
  194. (map canonical-package
  195. (list sed grep coreutils findutils gawk))
  196. (if register-closures? (list guix) '())))
  197. (graphs '#$(match inputs
  198. (((names . _) ...)
  199. names)))
  200. ;; This variable is unused but allows us to add INPUTS-TO-COPY
  201. ;; as inputs.
  202. (to-register
  203. '#$(map (match-lambda
  204. ((name thing) thing)
  205. ((name thing output) `(,thing ,output)))
  206. inputs)))
  207. (set-path-environment-variable "PATH" '("bin" "sbin") inputs)
  208. (make-iso9660-image #$(bootloader-package bootloader)
  209. #$bootcfg-drv
  210. #$os-drv
  211. "/xchg/guixsd.iso"
  212. #:register-closures? #$register-closures?
  213. #:closures graphs
  214. #:volume-id #$file-system-label
  215. #:volume-uuid #$(and=> file-system-uuid
  216. uuid-bytevector))
  217. (reboot))))
  218. #:system system
  219. #:make-disk-image? #f
  220. #:single-file-output? #t
  221. #:references-graphs inputs))
  222. (define* (qemu-image #:key
  223. (name "qemu-image")
  224. (system (%current-system))
  225. (qemu qemu-minimal)
  226. (disk-image-size 'guess)
  227. (disk-image-format "qcow2")
  228. (file-system-type "ext4")
  229. file-system-label
  230. file-system-uuid
  231. os-drv
  232. bootcfg-drv
  233. bootloader
  234. (register-closures? #t)
  235. (inputs '())
  236. copy-inputs?)
  237. "Return a bootable, stand-alone QEMU image of type DISK-IMAGE-FORMAT (e.g.,
  238. 'qcow2' or 'raw'), with a root partition of type FILE-SYSTEM-TYPE.
  239. Optionally, FILE-SYSTEM-LABEL can be specified as the volume name for the root
  240. partition; likewise FILE-SYSTEM-UUID, if true, specifies the UUID of the root
  241. partition (a UUID object).
  242. The returned image is a full disk image that runs OS-DERIVATION,
  243. with a GRUB installation that uses GRUB-CONFIGURATION as its configuration
  244. file (GRUB-CONFIGURATION must be the name of a file in the VM.)
  245. INPUTS is a list of inputs (as for packages). When COPY-INPUTS? is true, copy
  246. all of INPUTS into the image being built. When REGISTER-CLOSURES? is true,
  247. register INPUTS in the store database of the image so that Guix can be used in
  248. the image."
  249. (expression->derivation-in-linux-vm
  250. name
  251. (with-imported-modules (source-module-closure '((gnu build bootloader)
  252. (gnu build vm)
  253. (guix build utils)))
  254. #~(begin
  255. (use-modules (gnu build bootloader)
  256. (gnu build vm)
  257. (guix build utils)
  258. (srfi srfi-26)
  259. (ice-9 binary-ports))
  260. (let ((inputs
  261. '#$(append (list qemu parted e2fsprogs dosfstools)
  262. (map canonical-package
  263. (list sed grep coreutils findutils gawk))
  264. (if register-closures? (list guix) '())))
  265. ;; This variable is unused but allows us to add INPUTS-TO-COPY
  266. ;; as inputs.
  267. (to-register
  268. '#$(map (match-lambda
  269. ((name thing) thing)
  270. ((name thing output) `(,thing ,output)))
  271. inputs)))
  272. (set-path-environment-variable "PATH" '("bin" "sbin") inputs)
  273. (let* ((graphs '#$(match inputs
  274. (((names . _) ...)
  275. names)))
  276. (initialize (root-partition-initializer
  277. #:closures graphs
  278. #:copy-closures? #$copy-inputs?
  279. #:register-closures? #$register-closures?
  280. #:system-directory #$os-drv))
  281. (root-size #$(if (eq? 'guess disk-image-size)
  282. #~(max
  283. ;; Minimum 20 MiB root size
  284. (* 20 (expt 2 20))
  285. (estimated-partition-size
  286. (map (cut string-append "/xchg/" <>)
  287. graphs)))
  288. (- disk-image-size
  289. (* 50 (expt 2 20)))))
  290. (partitions
  291. (append
  292. (list (partition
  293. (size root-size)
  294. (label #$file-system-label)
  295. (uuid #$(and=> file-system-uuid
  296. uuid-bytevector))
  297. (file-system #$file-system-type)
  298. (flags '(boot))
  299. (initializer initialize)))
  300. ;; Append a small EFI System Partition for use with UEFI
  301. ;; bootloaders if we are not targeting ARM because UEFI
  302. ;; support in U-Boot is experimental.
  303. ;;
  304. ;; FIXME: ‘target-arm32?’ may be not operate on the right
  305. ;; system/target values. Rewrite using ‘let-system’ when
  306. ;; available.
  307. (if #$(target-arm32?)
  308. '()
  309. (list (partition
  310. ;; The standalone grub image is about 10MiB, but
  311. ;; leave some room for custom or multiple images.
  312. (size (* 40 (expt 2 20)))
  313. (label "GNU-ESP") ;cosmetic only
  314. ;; Use "vfat" here since this property is used
  315. ;; when mounting. The actual FAT-ness is based
  316. ;; on filesystem size (16 in this case).
  317. (file-system "vfat")
  318. (flags '(esp))))))))
  319. (initialize-hard-disk "/dev/vda"
  320. #:partitions partitions
  321. #:grub-efi #$grub-efi
  322. #:bootloader-package
  323. #$(bootloader-package bootloader)
  324. #:bootcfg #$bootcfg-drv
  325. #:bootcfg-location
  326. #$(bootloader-configuration-file bootloader)
  327. #:bootloader-installer
  328. #$(bootloader-installer bootloader))
  329. (reboot)))))
  330. #:system system
  331. #:make-disk-image? #t
  332. #:disk-image-size disk-image-size
  333. #:disk-image-format disk-image-format
  334. #:references-graphs inputs))
  335. ;;;
  336. ;;; VM and disk images.
  337. ;;;
  338. (define* (operating-system-uuid os #:optional (type 'dce))
  339. "Compute UUID object with a deterministic \"UUID\" for OS, of the given
  340. TYPE (one of 'iso9660 or 'dce). Return a UUID object."
  341. (if (eq? type 'iso9660)
  342. (let ((pad (compose (cut string-pad <> 2 #\0)
  343. number->string))
  344. (h (hash (operating-system-services os) 3600)))
  345. (bytevector->uuid
  346. (string->iso9660-uuid
  347. (string-append "1970-01-01-"
  348. (pad (hash (operating-system-host-name os) 24)) "-"
  349. (pad (quotient h 60)) "-"
  350. (pad (modulo h 60)) "-"
  351. (pad (hash (operating-system-file-systems os) 100))))
  352. 'iso9660))
  353. (bytevector->uuid
  354. (uint-list->bytevector
  355. (list (hash file-system-type
  356. (- (expt 2 32) 1))
  357. (hash (operating-system-host-name os)
  358. (- (expt 2 32) 1))
  359. (hash (operating-system-services os)
  360. (- (expt 2 32) 1))
  361. (hash (operating-system-file-systems os)
  362. (- (expt 2 32) 1)))
  363. (endianness little)
  364. 4)
  365. type)))
  366. (define* (system-disk-image os
  367. #:key
  368. (name "disk-image")
  369. (file-system-type "ext4")
  370. (disk-image-size (* 900 (expt 2 20)))
  371. (volatile? #t))
  372. "Return the derivation of a disk image of DISK-IMAGE-SIZE bytes of the
  373. system described by OS. Said image can be copied on a USB stick as is. When
  374. VOLATILE? is true, the root file system is made volatile; this is useful
  375. to USB sticks meant to be read-only."
  376. (define normalize-label
  377. ;; ISO labels are all-caps (case-insensitive), but since
  378. ;; 'find-partition-by-label' is case-sensitive, make it all-caps here.
  379. (if (string=? "iso9660" file-system-type)
  380. string-upcase
  381. identity))
  382. (define root-label
  383. ;; Volume name of the root file system.
  384. (normalize-label "GuixSD_image"))
  385. (define root-uuid
  386. ;; UUID of the root file system, computed in a deterministic fashion.
  387. ;; This is what we use to locate the root file system so it has to be
  388. ;; different from the user's own file system UUIDs.
  389. (operating-system-uuid os
  390. (if (string=? file-system-type "iso9660")
  391. 'iso9660
  392. 'dce)))
  393. (define file-systems-to-keep
  394. (remove (lambda (fs)
  395. (string=? (file-system-mount-point fs) "/"))
  396. (operating-system-file-systems os)))
  397. (let ((os (operating-system (inherit os)
  398. ;; Since this is meant to be used on real hardware, don't
  399. ;; install QEMU networking or anything like that. Assume USB
  400. ;; mass storage devices (usb-storage.ko) are available.
  401. (initrd (lambda (file-systems . rest)
  402. (apply (operating-system-initrd os)
  403. file-systems
  404. #:volatile-root? #t
  405. rest)))
  406. (bootloader (if (string=? "iso9660" file-system-type)
  407. (bootloader-configuration
  408. (inherit (operating-system-bootloader os))
  409. (bootloader grub-mkrescue-bootloader))
  410. (operating-system-bootloader os)))
  411. ;; Force our own root file system.
  412. (file-systems (cons (file-system
  413. (mount-point "/")
  414. (device root-uuid)
  415. (title 'uuid)
  416. (type file-system-type))
  417. file-systems-to-keep)))))
  418. (mlet* %store-monad ((os-drv (operating-system-derivation os))
  419. (bootcfg (operating-system-bootcfg os)))
  420. (if (string=? "iso9660" file-system-type)
  421. (iso9660-image #:name name
  422. #:file-system-label root-label
  423. #:file-system-uuid root-uuid
  424. #:os-drv os-drv
  425. #:register-closures? #t
  426. #:bootcfg-drv bootcfg
  427. #:bootloader (bootloader-configuration-bootloader
  428. (operating-system-bootloader os))
  429. #:inputs `(("system" ,os-drv)
  430. ("bootcfg" ,bootcfg)))
  431. (qemu-image #:name name
  432. #:os-drv os-drv
  433. #:bootcfg-drv bootcfg
  434. #:bootloader (bootloader-configuration-bootloader
  435. (operating-system-bootloader os))
  436. #:disk-image-size disk-image-size
  437. #:disk-image-format "raw"
  438. #:file-system-type file-system-type
  439. #:file-system-label root-label
  440. #:file-system-uuid root-uuid
  441. #:copy-inputs? #t
  442. #:register-closures? #t
  443. #:inputs `(("system" ,os-drv)
  444. ("bootcfg" ,bootcfg)))))))
  445. (define* (system-qemu-image os
  446. #:key
  447. (file-system-type "ext4")
  448. (disk-image-size (* 900 (expt 2 20))))
  449. "Return the derivation of a freestanding QEMU image of DISK-IMAGE-SIZE bytes
  450. of the GNU system as described by OS."
  451. (define file-systems-to-keep
  452. ;; Keep only file systems other than root and not normally bound to real
  453. ;; devices.
  454. (remove (lambda (fs)
  455. (let ((target (file-system-mount-point fs))
  456. (source (file-system-device fs)))
  457. (or (string=? target "/")
  458. (string-prefix? "/dev/" source))))
  459. (operating-system-file-systems os)))
  460. (let ((os (operating-system (inherit os)
  461. ;; Use an initrd with the whole QEMU shebang.
  462. (initrd (lambda (file-systems . rest)
  463. (apply (operating-system-initrd os)
  464. file-systems
  465. #:virtio? #t
  466. rest)))
  467. ;; Force our own root file system.
  468. (file-systems (cons (file-system
  469. (mount-point "/")
  470. (device "/dev/sda1")
  471. (type file-system-type))
  472. file-systems-to-keep)))))
  473. (mlet* %store-monad
  474. ((os-drv (operating-system-derivation os))
  475. (bootcfg (operating-system-bootcfg os)))
  476. (qemu-image #:os-drv os-drv
  477. #:bootcfg-drv bootcfg
  478. #:bootloader (bootloader-configuration-bootloader
  479. (operating-system-bootloader os))
  480. #:disk-image-size disk-image-size
  481. #:file-system-type file-system-type
  482. #:inputs `(("system" ,os-drv)
  483. ("bootcfg" ,bootcfg))
  484. #:copy-inputs? #t))))
  485. ;;;
  486. ;;; VMs that share file systems with the host.
  487. ;;;
  488. (define (file-system->mount-tag fs)
  489. "Return a 9p mount tag for host file system FS."
  490. ;; QEMU mount tags cannot contain slashes and cannot start with '_'.
  491. ;; Compute an identifier that corresponds to the rules.
  492. (string-append "TAG"
  493. (string-map (match-lambda
  494. (#\/ #\_)
  495. (chr chr))
  496. fs)))
  497. (define (mapping->file-system mapping)
  498. "Return a 9p file system that realizes MAPPING."
  499. (match mapping
  500. (($ <file-system-mapping> source target writable?)
  501. (file-system
  502. (mount-point target)
  503. (device (file-system->mount-tag source))
  504. (type "9p")
  505. (flags (if writable? '() '(read-only)))
  506. (options "trans=virtio,cache=loose")
  507. (check? #f)
  508. (create-mount-point? #t)))))
  509. (define* (virtualized-operating-system os mappings #:optional (full-boot? #f))
  510. "Return an operating system based on OS suitable for use in a virtualized
  511. environment with the store shared with the host. MAPPINGS is a list of
  512. <file-system-mapping> to realize in the virtualized OS."
  513. (define user-file-systems
  514. ;; Remove file systems that conflict with those added below, or that are
  515. ;; normally bound to real devices.
  516. (remove (lambda (fs)
  517. (let ((target (file-system-mount-point fs))
  518. (source (file-system-device fs)))
  519. (or (string=? target (%store-prefix))
  520. (string=? target "/")
  521. (and (eq? 'device (file-system-title fs))
  522. (string-prefix? "/dev/" source))
  523. ;; Labels and UUIDs are necessarily invalid in the VM.
  524. (and (file-system-mount? fs)
  525. (or (eq? 'label (file-system-title fs))
  526. (eq? 'uuid (file-system-title fs))
  527. (uuid? source))))))
  528. (operating-system-file-systems os)))
  529. (define virtual-file-systems
  530. (cons (file-system
  531. (mount-point "/")
  532. (device "/dev/vda1")
  533. (type "ext4"))
  534. (append (map mapping->file-system mappings)
  535. user-file-systems)))
  536. (operating-system (inherit os)
  537. ;; XXX: Until we run QEMU with UEFI support (with the OVMF firmware),
  538. ;; force the traditional i386/BIOS method.
  539. ;; See <https://bugs.gnu.org/28768>.
  540. (bootloader (bootloader-configuration
  541. (bootloader grub-bootloader)
  542. (target "/dev/vda")))
  543. (initrd (lambda (file-systems . rest)
  544. (apply (operating-system-initrd os)
  545. file-systems
  546. #:volatile-root? #t
  547. #:virtio? #t
  548. rest)))
  549. ;; Disable swap.
  550. (swap-devices '())
  551. ;; XXX: When FULL-BOOT? is true, do not add a 9p mount for /gnu/store
  552. ;; since that would lead the bootloader config to look for the kernel and
  553. ;; initrd in it.
  554. (file-systems (if full-boot?
  555. virtual-file-systems
  556. (cons
  557. (file-system
  558. (inherit (mapping->file-system %store-mapping))
  559. (needed-for-boot? #t))
  560. virtual-file-systems)))))
  561. (define* (system-qemu-image/shared-store
  562. os
  563. #:key
  564. full-boot?
  565. (disk-image-size (* (if full-boot? 500 30) (expt 2 20))))
  566. "Return a derivation that builds a QEMU image of OS that shares its store
  567. with the host.
  568. When FULL-BOOT? is true, return an image that does a complete boot sequence,
  569. bootloaded included; thus, make a disk image that contains everything the
  570. bootloader refers to: OS kernel, initrd, bootloader data, etc."
  571. (mlet* %store-monad ((os-drv (operating-system-derivation os))
  572. (bootcfg (operating-system-bootcfg os)))
  573. ;; XXX: When FULL-BOOT? is true, we end up creating an image that contains
  574. ;; BOOTCFG and all its dependencies, including the output of OS-DRV.
  575. ;; This is more than needed (we only need the kernel, initrd, GRUB for its
  576. ;; font, and the background image), but it's hard to filter that.
  577. (qemu-image #:os-drv os-drv
  578. #:bootcfg-drv bootcfg
  579. #:bootloader (bootloader-configuration-bootloader
  580. (operating-system-bootloader os))
  581. #:disk-image-size disk-image-size
  582. #:inputs (if full-boot?
  583. `(("bootcfg" ,bootcfg))
  584. '())
  585. ;; XXX: Passing #t here is too slow, so let it off by default.
  586. #:register-closures? #f
  587. #:copy-inputs? full-boot?)))
  588. (define* (common-qemu-options image shared-fs)
  589. "Return the a string-value gexp with the common QEMU options to boot IMAGE,
  590. with '-virtfs' options for the host file systems listed in SHARED-FS."
  591. (define (virtfs-option fs)
  592. #~(format #f "-virtfs local,path=~s,security_model=none,mount_tag=~s"
  593. #$fs #$(file-system->mount-tag fs)))
  594. #~(;; Only enable kvm if we see /dev/kvm exists.
  595. ;; This allows users without hardware virtualization to still use these
  596. ;; commands.
  597. #$@(if (file-exists? "/dev/kvm")
  598. '("-enable-kvm")
  599. '())
  600. "-no-reboot"
  601. "-net nic,model=virtio"
  602. "-object" "rng-random,filename=/dev/urandom,id=guixsd-vm-rng"
  603. "-device" "virtio-rng-pci,rng=guixsd-vm-rng"
  604. #$@(map virtfs-option shared-fs)
  605. "-vga std"
  606. (format #f "-drive file=~a,if=virtio,cache=writeback,werror=report,readonly"
  607. #$image)))
  608. (define* (system-qemu-image/shared-store-script os
  609. #:key
  610. (qemu qemu)
  611. (graphic? #t)
  612. (memory-size 256)
  613. (mappings '())
  614. full-boot?
  615. (disk-image-size
  616. (* (if full-boot? 500 70)
  617. (expt 2 20)))
  618. (options '()))
  619. "Return a derivation that builds a script to run a virtual machine image of
  620. OS that shares its store with the host. The virtual machine runs with
  621. MEMORY-SIZE MiB of memory.
  622. MAPPINGS is a list of <file-system-mapping> specifying mapping of host file
  623. systems into the guest.
  624. When FULL-BOOT? is true, the returned script runs everything starting from the
  625. bootloader; otherwise it directly starts the operating system kernel. The
  626. DISK-IMAGE-SIZE parameter specifies the size in bytes of the root disk image;
  627. it is mostly useful when FULL-BOOT? is true."
  628. (mlet* %store-monad ((os -> (virtualized-operating-system os mappings full-boot?))
  629. (os-drv (operating-system-derivation os))
  630. (image (system-qemu-image/shared-store
  631. os
  632. #:full-boot? full-boot?
  633. #:disk-image-size disk-image-size)))
  634. (define kernel-arguments
  635. #~(list #$@(if graphic? #~() #~("console=ttyS0"))
  636. #+@(operating-system-kernel-arguments os os-drv "/dev/vda1")))
  637. (define qemu-exec
  638. #~(list (string-append #$qemu "/bin/" #$(qemu-command (%current-system)))
  639. #$@(if full-boot?
  640. #~()
  641. #~("-kernel" #$(operating-system-kernel-file os)
  642. "-initrd" #$(file-append os-drv "/initrd")
  643. (format #f "-append ~s"
  644. (string-join #$kernel-arguments " "))))
  645. #$@(common-qemu-options image
  646. (map file-system-mapping-source
  647. (cons %store-mapping mappings)))
  648. "-m " (number->string #$memory-size)
  649. #$@options))
  650. (define builder
  651. #~(call-with-output-file #$output
  652. (lambda (port)
  653. (format port "#!~a~% exec ~a \"$@\"~%"
  654. #$(file-append bash "/bin/sh")
  655. (string-join #$qemu-exec " "))
  656. (chmod port #o555))))
  657. (gexp->derivation "run-vm.sh" builder)))
  658. ;;;
  659. ;;; High-level abstraction.
  660. ;;;
  661. (define-record-type* <virtual-machine> %virtual-machine
  662. make-virtual-machine
  663. virtual-machine?
  664. (operating-system virtual-machine-operating-system) ;<operating-system>
  665. (qemu virtual-machine-qemu ;<package>
  666. (default qemu))
  667. (graphic? virtual-machine-graphic? ;Boolean
  668. (default #f))
  669. (memory-size virtual-machine-memory-size ;integer (MiB)
  670. (default 256))
  671. (disk-image-size virtual-machine-disk-image-size ;integer (bytes)
  672. (default 'guess))
  673. (port-forwardings virtual-machine-port-forwardings ;list of integer pairs
  674. (default '())))
  675. (define-syntax virtual-machine
  676. (syntax-rules ()
  677. "Declare a virtual machine running the specified OS, with the given
  678. options."
  679. ((_ os) ;shortcut
  680. (%virtual-machine (operating-system os)))
  681. ((_ fields ...)
  682. (%virtual-machine fields ...))))
  683. (define (port-forwardings->qemu-options forwardings)
  684. "Return the QEMU option for the given port FORWARDINGS as a string, where
  685. FORWARDINGS is a list of host-port/guest-port pairs."
  686. (string-join
  687. (map (match-lambda
  688. ((host-port . guest-port)
  689. (string-append "hostfwd=tcp::"
  690. (number->string host-port)
  691. "-:" (number->string guest-port))))
  692. forwardings)
  693. ","))
  694. (define-gexp-compiler (virtual-machine-compiler (vm <virtual-machine>)
  695. system target)
  696. ;; XXX: SYSTEM and TARGET are ignored.
  697. (match vm
  698. (($ <virtual-machine> os qemu graphic? memory-size disk-image-size ())
  699. (system-qemu-image/shared-store-script os
  700. #:qemu qemu
  701. #:graphic? graphic?
  702. #:memory-size memory-size
  703. #:disk-image-size
  704. disk-image-size))
  705. (($ <virtual-machine> os qemu graphic? memory-size disk-image-size
  706. forwardings)
  707. (let ((options
  708. `("-net" ,(string-append
  709. "user,"
  710. (port-forwardings->qemu-options forwardings)))))
  711. (system-qemu-image/shared-store-script os
  712. #:qemu qemu
  713. #:graphic? graphic?
  714. #:memory-size memory-size
  715. #:disk-image-size
  716. disk-image-size
  717. #:options options)))))
  718. ;;; vm.scm ends here