2013-02-15 21:28:26 -05:00
|
|
|
|
;;; GNU Guix --- Functional package management for GNU
|
2024-01-20 05:47:47 -05:00
|
|
|
|
;;; Copyright © 2013-2024 Ludovic Courtès <ludo@gnu.org>
|
2021-08-15 14:15:37 -04:00
|
|
|
|
;;; Copyright © 2016 Christine Lemmer-Webber <cwebber@dustycloud.org>
|
2017-12-02 20:17:45 -05:00
|
|
|
|
;;; Copyright © 2016, 2017 Leo Famulari <leo@famulari.name>
|
2017-04-15 08:22:24 -04:00
|
|
|
|
;;; Copyright © 2017 Mathieu Othacehe <m.othacehe@gmail.com>
|
2017-05-07 09:31:30 -04:00
|
|
|
|
;;; Copyright © 2017 Marius Bakke <mbakke@fastmail.com>
|
2018-03-15 00:09:12 -04:00
|
|
|
|
;;; Copyright © 2018 Chris Marusich <cmmarusich@gmail.com>
|
2024-03-19 11:18:36 -04:00
|
|
|
|
;;; Copyright © 2024 Zheng Junjie <873216071@qq.com>
|
2013-02-15 21:28:26 -05:00
|
|
|
|
;;;
|
|
|
|
|
;;; 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 (gnu system vm)
|
2013-08-31 17:01:56 -04:00
|
|
|
|
#:use-module (guix config)
|
2013-02-15 21:28:26 -05:00
|
|
|
|
#:use-module (guix store)
|
2014-04-26 10:36:48 -04:00
|
|
|
|
#:use-module (guix gexp)
|
2013-02-15 21:28:26 -05:00
|
|
|
|
#:use-module (guix derivations)
|
|
|
|
|
#:use-module (guix packages)
|
gnu: vm: Rewrite helper functions as monadic functions.
* gnu/system/dmd.scm (host-name-service, nscd-service, mingetty-service,
syslog-service, guix-service, static-networking-service): Rewrite as
monadic functions.
(dmd-configuration-file): Use 'text-file' instead of
'add-text-to-store'.
* gnu/system/grub.scm (grub-configuration-file): Rewrite as a monadic
function.
* gnu/system/linux.scm (pam-services->directory): Likewise.
* gnu/system/shadow.scm (group-file, passwd-file, guix-build-accounts):
Likewise.
* gnu/system/vm.scm (expression->derivation-in-linux-vm, qemu-image,
union, system-qemu-image): Likewise.
2013-10-03 15:30:30 -04:00
|
|
|
|
#:use-module (guix monads)
|
2014-11-20 17:32:54 -05:00
|
|
|
|
#:use-module (guix records)
|
2016-09-04 17:42:50 -04:00
|
|
|
|
#:use-module (guix modules)
|
2017-11-29 09:12:01 -05:00
|
|
|
|
#:use-module (guix utils)
|
Switch to Guile-Gcrypt.
This removes (guix hash) and (guix pk-crypto), which now live as part of
Guile-Gcrypt (version 0.1.0.)
* guix/gcrypt.scm, guix/hash.scm, guix/pk-crypto.scm,
tests/hash.scm, tests/pk-crypto.scm: Remove.
* configure.ac: Test for Guile-Gcrypt. Remove LIBGCRYPT and
LIBGCRYPT_LIBDIR assignments.
* m4/guix.m4 (GUIX_ASSERT_LIBGCRYPT_USABLE): Remove.
* README: Add Guile-Gcrypt to the dependencies; move libgcrypt as
"required unless --disable-daemon".
* doc/guix.texi (Requirements): Likewise.
* gnu/packages/bash.scm, guix/derivations.scm, guix/docker.scm,
guix/git.scm, guix/http-client.scm, guix/import/cpan.scm,
guix/import/cran.scm, guix/import/crate.scm, guix/import/elpa.scm,
guix/import/gnu.scm, guix/import/hackage.scm,
guix/import/texlive.scm, guix/import/utils.scm, guix/nar.scm,
guix/pki.scm, guix/scripts/archive.scm,
guix/scripts/authenticate.scm, guix/scripts/download.scm,
guix/scripts/hash.scm, guix/scripts/pack.scm,
guix/scripts/publish.scm, guix/scripts/refresh.scm,
guix/scripts/substitute.scm, guix/store.scm,
guix/store/deduplication.scm, guix/tests.scm, tests/base32.scm,
tests/builders.scm, tests/challenge.scm, tests/cpan.scm,
tests/crate.scm, tests/derivations.scm, tests/gem.scm,
tests/nar.scm, tests/opam.scm, tests/pki.scm,
tests/publish.scm, tests/pypi.scm, tests/store-deduplication.scm,
tests/store.scm, tests/substitute.scm: Adjust imports.
* gnu/system/vm.scm: Likewise.
(guile-sqlite3&co): Rename to...
(gcrypt-sqlite3&co): ... this. Add GUILE-GCRYPT.
(expression->derivation-in-linux-vm)[config]: Remove.
(iso9660-image)[config]: Remove.
(qemu-image)[config]: Remove.
(system-docker-image)[config]: Remove.
* guix/scripts/pack.scm: Adjust imports.
(guile-sqlite3&co): Rename to...
(gcrypt-sqlite3&co): ... this. Add GUILE-GCRYPT.
(self-contained-tarball)[build]: Call 'make-config.scm' without
#:libgcrypt argument.
(squashfs-image)[libgcrypt]: Remove.
[build]: Call 'make-config.scm' without #:libgcrypt.
(docker-image)[config, json]: Remove.
[build]: Add GUILE-GCRYPT to the extensions Remove (guix config) from
the imported modules.
* guix/self.scm (specification->package): Remove "libgcrypt", add
"guile-gcrypt".
(compiled-guix): Remove #:libgcrypt.
[guile-gcrypt]: New variable.
[dependencies]: Add it.
[*core-modules*]: Remove #:libgcrypt from 'make-config.scm' call.
Add #:extensions.
[*config*]: Remove #:libgcrypt from 'make-config.scm' call.
(%dependency-variables): Remove %libgcrypt.
(make-config.scm): Remove #:libgcrypt.
* build-aux/build-self.scm (guile-gcrypt): New variable.
(make-config.scm): Remove #:libgcrypt.
(build-program)[fake-gcrypt-hash]: New variable.
Add (gcrypt hash) to the imported modules. Adjust load path
assignments.
* gnu/packages/package-management.scm (guix)[propagated-inputs]: Add
GUILE-GCRYPT.
[arguments]: In 'wrap-program' phase, add GUILE-GCRYPT to the search
path.
2018-08-31 11:07:07 -04:00
|
|
|
|
#:use-module (gcrypt hash)
|
2018-03-08 05:55:06 -05:00
|
|
|
|
#:use-module (guix base32)
|
2018-06-06 17:58:18 -04:00
|
|
|
|
#:use-module ((guix self) #:select (make-config.scm))
|
2014-11-20 17:32:54 -05:00
|
|
|
|
|
2021-12-16 07:32:11 -05:00
|
|
|
|
#:use-module ((gnu build marionette)
|
2014-07-13 10:07:26 -04:00
|
|
|
|
#:select (qemu-command))
|
gnu: Split (gnu packages base), adding (gnu packages commencement).
* gnu/packages/base.scm (gnu-make-boot0, diffutils-boot0,
findutils-boot0, %boot0-inputs, nix-system->gnu-triplet, boot-triplet,
binutils-boot0, gcc-boot0, perl-boot0, linux-libre-headers-boot0,
texinfo-boot0, %boot1-inputs, glibc-final-with-bootstrap-bash,
cross-gcc-wrapper, static-bash-for-glibc, glibc-final,
gcc-boot0-wrapped, %boot2-inputs, binutils-final, libstdc++,
gcc-final, ld-wrapper-boot3, %boot3-inputs, bash-final, %boot4-inputs,
guile-final, gnu-make-final, ld-wrapper, coreutils-final, grep-final,
%boot5-inputs, %final-inputs, canonical-package, gcc-toolchain,
gcc-toolchain-4.8, gcc-toolchain-4.9): Move to...
* gnu/packages/commencement.scm: ... here. New file.
* gnu-system.am (GNU_SYSTEM_MODULES): Add it.
* build-aux/check-final-inputs-self-contained.scm: Adjust accordingly.
* gnu/packages/cross-base.scm: Likewise.
* gnu/packages/make-bootstrap.scm: Likewise.
* guix/build-system/cmake.scm (cmake-build): Likewise.
* guix/build-system/gnu.scm (standard-packages, gnu-build,
gnu-cross-build): Likewise.
* guix/build-system/perl.scm (perl-build): Likewise.
* guix/build-system/python.scm (python-build): Likewise.
* guix/build-system/trivial.scm (guile-for-build): Likewise.
* guix/download.scm (url-fetch): Likewise.
* guix/gexp.scm (default-guile): Likewise.
* guix/git-download.scm (git-fetch): Likewise.
* guix/monads.scm (run-with-store): Likewise.
* guix/packages.scm (default-guile): Likewise.
* guix/scripts/package.scm (guix-package): Likewise.
* guix/scripts/refresh.scm: Likewise.
* guix/svn-download.scm (svn-fetch): Likewise.
* tests/builders.scm (%bootstrap-inputs, %bootstrap-search-paths):
Likewise.
* tests/packages.scm ("GNU Make, bootstrap"): Likewise.
* tests/guix-package.sh: Likewise.
* gnu/services/base.scm: Use 'canonical-package' instead of xxx-final.
* gnu/services/xorg.scm: Likewise.
* gnu/system/vm.scm: Likewise.
* guix/scripts/pull.scm (guix-pull): Likewise.
2014-08-26 18:25:17 -04:00
|
|
|
|
#:use-module (gnu packages base)
|
2017-02-09 13:46:47 -05:00
|
|
|
|
#:use-module (gnu packages bootloaders)
|
2017-07-03 04:05:03 -04:00
|
|
|
|
#:use-module (gnu packages cdrom)
|
2018-02-18 23:45:03 -05:00
|
|
|
|
#:use-module (gnu packages compression)
|
2013-09-04 18:45:53 -04:00
|
|
|
|
#:use-module (gnu packages guile)
|
Switch to Guile-Gcrypt.
This removes (guix hash) and (guix pk-crypto), which now live as part of
Guile-Gcrypt (version 0.1.0.)
* guix/gcrypt.scm, guix/hash.scm, guix/pk-crypto.scm,
tests/hash.scm, tests/pk-crypto.scm: Remove.
* configure.ac: Test for Guile-Gcrypt. Remove LIBGCRYPT and
LIBGCRYPT_LIBDIR assignments.
* m4/guix.m4 (GUIX_ASSERT_LIBGCRYPT_USABLE): Remove.
* README: Add Guile-Gcrypt to the dependencies; move libgcrypt as
"required unless --disable-daemon".
* doc/guix.texi (Requirements): Likewise.
* gnu/packages/bash.scm, guix/derivations.scm, guix/docker.scm,
guix/git.scm, guix/http-client.scm, guix/import/cpan.scm,
guix/import/cran.scm, guix/import/crate.scm, guix/import/elpa.scm,
guix/import/gnu.scm, guix/import/hackage.scm,
guix/import/texlive.scm, guix/import/utils.scm, guix/nar.scm,
guix/pki.scm, guix/scripts/archive.scm,
guix/scripts/authenticate.scm, guix/scripts/download.scm,
guix/scripts/hash.scm, guix/scripts/pack.scm,
guix/scripts/publish.scm, guix/scripts/refresh.scm,
guix/scripts/substitute.scm, guix/store.scm,
guix/store/deduplication.scm, guix/tests.scm, tests/base32.scm,
tests/builders.scm, tests/challenge.scm, tests/cpan.scm,
tests/crate.scm, tests/derivations.scm, tests/gem.scm,
tests/nar.scm, tests/opam.scm, tests/pki.scm,
tests/publish.scm, tests/pypi.scm, tests/store-deduplication.scm,
tests/store.scm, tests/substitute.scm: Adjust imports.
* gnu/system/vm.scm: Likewise.
(guile-sqlite3&co): Rename to...
(gcrypt-sqlite3&co): ... this. Add GUILE-GCRYPT.
(expression->derivation-in-linux-vm)[config]: Remove.
(iso9660-image)[config]: Remove.
(qemu-image)[config]: Remove.
(system-docker-image)[config]: Remove.
* guix/scripts/pack.scm: Adjust imports.
(guile-sqlite3&co): Rename to...
(gcrypt-sqlite3&co): ... this. Add GUILE-GCRYPT.
(self-contained-tarball)[build]: Call 'make-config.scm' without
#:libgcrypt argument.
(squashfs-image)[libgcrypt]: Remove.
[build]: Call 'make-config.scm' without #:libgcrypt.
(docker-image)[config, json]: Remove.
[build]: Add GUILE-GCRYPT to the extensions Remove (guix config) from
the imported modules.
* guix/self.scm (specification->package): Remove "libgcrypt", add
"guile-gcrypt".
(compiled-guix): Remove #:libgcrypt.
[guile-gcrypt]: New variable.
[dependencies]: Add it.
[*core-modules*]: Remove #:libgcrypt from 'make-config.scm' call.
Add #:extensions.
[*config*]: Remove #:libgcrypt from 'make-config.scm' call.
(%dependency-variables): Remove %libgcrypt.
(make-config.scm): Remove #:libgcrypt.
* build-aux/build-self.scm (guile-gcrypt): New variable.
(make-config.scm): Remove #:libgcrypt.
(build-program)[fake-gcrypt-hash]: New variable.
Add (gcrypt hash) to the imported modules. Adjust load path
assignments.
* gnu/packages/package-management.scm (guix)[propagated-inputs]: Add
GUILE-GCRYPT.
[arguments]: In 'wrap-program' phase, add GUILE-GCRYPT to the search
path.
2018-08-31 11:07:07 -04:00
|
|
|
|
#:autoload (gnu packages gnupg) (guile-gcrypt)
|
gnu: Split (gnu packages base), adding (gnu packages commencement).
* gnu/packages/base.scm (gnu-make-boot0, diffutils-boot0,
findutils-boot0, %boot0-inputs, nix-system->gnu-triplet, boot-triplet,
binutils-boot0, gcc-boot0, perl-boot0, linux-libre-headers-boot0,
texinfo-boot0, %boot1-inputs, glibc-final-with-bootstrap-bash,
cross-gcc-wrapper, static-bash-for-glibc, glibc-final,
gcc-boot0-wrapped, %boot2-inputs, binutils-final, libstdc++,
gcc-final, ld-wrapper-boot3, %boot3-inputs, bash-final, %boot4-inputs,
guile-final, gnu-make-final, ld-wrapper, coreutils-final, grep-final,
%boot5-inputs, %final-inputs, canonical-package, gcc-toolchain,
gcc-toolchain-4.8, gcc-toolchain-4.9): Move to...
* gnu/packages/commencement.scm: ... here. New file.
* gnu-system.am (GNU_SYSTEM_MODULES): Add it.
* build-aux/check-final-inputs-self-contained.scm: Adjust accordingly.
* gnu/packages/cross-base.scm: Likewise.
* gnu/packages/make-bootstrap.scm: Likewise.
* guix/build-system/cmake.scm (cmake-build): Likewise.
* guix/build-system/gnu.scm (standard-packages, gnu-build,
gnu-cross-build): Likewise.
* guix/build-system/perl.scm (perl-build): Likewise.
* guix/build-system/python.scm (python-build): Likewise.
* guix/build-system/trivial.scm (guile-for-build): Likewise.
* guix/download.scm (url-fetch): Likewise.
* guix/gexp.scm (default-guile): Likewise.
* guix/git-download.scm (git-fetch): Likewise.
* guix/monads.scm (run-with-store): Likewise.
* guix/packages.scm (default-guile): Likewise.
* guix/scripts/package.scm (guix-package): Likewise.
* guix/scripts/refresh.scm: Likewise.
* guix/svn-download.scm (svn-fetch): Likewise.
* tests/builders.scm (%bootstrap-inputs, %bootstrap-search-paths):
Likewise.
* tests/packages.scm ("GNU Make, bootstrap"): Likewise.
* tests/guix-package.sh: Likewise.
* gnu/services/base.scm: Use 'canonical-package' instead of xxx-final.
* gnu/services/xorg.scm: Likewise.
* gnu/system/vm.scm: Likewise.
* guix/scripts/pull.scm (guix-pull): Likewise.
2014-08-26 18:25:17 -04:00
|
|
|
|
#:use-module (gnu packages gawk)
|
2013-09-04 18:45:53 -04:00
|
|
|
|
#:use-module (gnu packages bash)
|
2017-07-12 08:35:57 -04:00
|
|
|
|
#:use-module (gnu packages virtualization)
|
2014-06-03 16:12:05 -04:00
|
|
|
|
#:use-module (gnu packages disk)
|
2013-02-15 21:28:26 -05:00
|
|
|
|
#:use-module (gnu packages linux)
|
2013-09-11 16:36:50 -04:00
|
|
|
|
|
2017-05-09 04:52:02 -04:00
|
|
|
|
#:use-module (gnu bootloader)
|
2017-10-19 17:11:36 -04:00
|
|
|
|
#:use-module (gnu bootloader grub)
|
2024-03-19 11:18:37 -04:00
|
|
|
|
#:use-module (gnu bootloader u-boot)
|
2021-12-16 04:11:53 -05:00
|
|
|
|
#:use-module (gnu image)
|
|
|
|
|
#:use-module (gnu system image)
|
2019-03-22 12:48:37 -04:00
|
|
|
|
#:use-module (gnu system linux-container)
|
2014-01-29 07:04:00 -05:00
|
|
|
|
#:use-module (gnu system linux-initrd)
|
2017-05-15 16:24:18 -04:00
|
|
|
|
#:use-module (gnu bootloader)
|
2014-05-20 15:59:08 -04:00
|
|
|
|
#:use-module (gnu system file-systems)
|
2013-12-09 15:32:36 -05:00
|
|
|
|
#:use-module (gnu system)
|
2014-02-19 14:58:24 -05:00
|
|
|
|
#:use-module (gnu services)
|
2019-04-16 17:15:02 -04:00
|
|
|
|
#:use-module (gnu services base)
|
2017-09-06 03:28:28 -04:00
|
|
|
|
#:use-module (gnu system uuid)
|
2013-09-11 16:36:50 -04:00
|
|
|
|
|
2021-12-16 04:11:53 -05:00
|
|
|
|
#:use-module ((srfi srfi-1) #:hide (partition))
|
2024-01-20 05:47:47 -05:00
|
|
|
|
#:use-module (srfi srfi-19)
|
2013-02-15 21:28:26 -05:00
|
|
|
|
#:use-module (srfi srfi-26)
|
2017-09-06 17:16:09 -04:00
|
|
|
|
#:use-module (rnrs bytevectors)
|
2013-02-15 21:28:26 -05:00
|
|
|
|
#:use-module (ice-9 match)
|
2013-09-11 16:36:50 -04:00
|
|
|
|
|
2021-12-16 07:32:11 -05:00
|
|
|
|
#:export (virtualized-operating-system
|
2014-05-22 17:12:36 -04:00
|
|
|
|
system-qemu-image/shared-store-script
|
2017-07-18 04:36:21 -04:00
|
|
|
|
|
2024-01-20 08:55:46 -05:00
|
|
|
|
linux-image-startup-command
|
|
|
|
|
|
2017-07-18 04:36:21 -04:00
|
|
|
|
virtual-machine
|
2024-01-20 05:49:12 -05:00
|
|
|
|
virtual-machine?
|
|
|
|
|
virtual-machine-operating-system
|
|
|
|
|
virtual-machine-qemu
|
2024-01-20 08:54:34 -05:00
|
|
|
|
virtual-machine-cpu-count
|
2024-01-20 05:49:12 -05:00
|
|
|
|
virtual-machine-volatile?
|
|
|
|
|
virtual-machine-graphic?
|
|
|
|
|
virtual-machine-memory-size
|
|
|
|
|
virtual-machine-disk-image-size
|
|
|
|
|
virtual-machine-port-forwardings
|
|
|
|
|
virtual-machine-date))
|
2013-02-15 21:28:26 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
|
;;;
|
|
|
|
|
;;; Tools to evaluate build expressions within virtual machines.
|
|
|
|
|
;;;
|
|
|
|
|
;;; Code:
|
|
|
|
|
|
2021-04-06 17:37:33 -04:00
|
|
|
|
;; By default, the msize value is 8 KiB, which according to QEMU is
|
|
|
|
|
;; insufficient and would degrade performance. The msize value should roughly
|
|
|
|
|
;; match the bandwidth of the system's IO (see:
|
|
|
|
|
;; https://wiki.qemu.org/Documentation/9psetup#msize). Use 100 MiB as a
|
|
|
|
|
;; conservative default.
|
|
|
|
|
(define %default-msize-value (* 100 (expt 2 20))) ;100 MiB
|
|
|
|
|
|
2014-11-20 17:32:54 -05:00
|
|
|
|
;;;
|
|
|
|
|
;;; VMs that share file systems with the host.
|
|
|
|
|
;;;
|
|
|
|
|
|
2014-11-20 16:48:18 -05:00
|
|
|
|
(define (file-system->mount-tag fs)
|
|
|
|
|
"Return a 9p mount tag for host file system FS."
|
2018-03-08 05:55:06 -05:00
|
|
|
|
;; QEMU mount tags must be ASCII, at most 31-byte long, cannot contain
|
|
|
|
|
;; slashes, and cannot start with '_'. Compute an identifier that
|
|
|
|
|
;; corresponds to the rules.
|
2014-11-20 16:48:18 -05:00
|
|
|
|
(string-append "TAG"
|
2018-03-08 05:55:06 -05:00
|
|
|
|
(string-drop (bytevector->base32-string
|
|
|
|
|
(sha1 (string->utf8 fs)))
|
|
|
|
|
4)))
|
2014-11-20 16:48:18 -05:00
|
|
|
|
|
2014-11-20 17:32:54 -05:00
|
|
|
|
(define (mapping->file-system mapping)
|
|
|
|
|
"Return a 9p file system that realizes MAPPING."
|
|
|
|
|
(match mapping
|
|
|
|
|
(($ <file-system-mapping> source target writable?)
|
|
|
|
|
(file-system
|
|
|
|
|
(mount-point target)
|
|
|
|
|
(device (file-system->mount-tag source))
|
|
|
|
|
(type "9p")
|
|
|
|
|
(flags (if writable? '() '(read-only)))
|
2023-09-20 15:57:33 -04:00
|
|
|
|
|
|
|
|
|
;; The 9p documentation says that cache=loose is "intended for
|
|
|
|
|
;; exclusive, read-only mounts", without additional details. It's
|
|
|
|
|
;; faster than the default cache=none, especially when copying and
|
|
|
|
|
;; registering store items. Thus, use cache=loose, except for writable
|
|
|
|
|
;; mounts, to ensure consistency.
|
2020-08-31 08:19:22 -04:00
|
|
|
|
(options (string-append "trans=virtio"
|
2021-04-06 17:37:33 -04:00
|
|
|
|
(if writable? "" ",cache=loose")
|
|
|
|
|
",msize=" (number->string %default-msize-value)))
|
2014-11-20 17:32:54 -05:00
|
|
|
|
(check? #f)
|
|
|
|
|
(create-mount-point? #t)))))
|
|
|
|
|
|
2024-01-20 08:55:46 -05:00
|
|
|
|
(define* (virtualized-operating-system os
|
|
|
|
|
#:optional (mappings '())
|
2024-03-19 11:18:37 -04:00
|
|
|
|
#:key (full-boot? #f) volatile?
|
|
|
|
|
(system (%current-system))
|
|
|
|
|
(target (%current-target-system)))
|
2014-05-02 18:26:07 -04:00
|
|
|
|
"Return an operating system based on OS suitable for use in a virtualized
|
2014-11-20 17:32:54 -05:00
|
|
|
|
environment with the store shared with the host. MAPPINGS is a list of
|
|
|
|
|
<file-system-mapping> to realize in the virtualized OS."
|
|
|
|
|
(define user-file-systems
|
|
|
|
|
;; Remove file systems that conflict with those added below, or that are
|
|
|
|
|
;; normally bound to real devices.
|
|
|
|
|
(remove (lambda (fs)
|
|
|
|
|
(let ((target (file-system-mount-point fs))
|
|
|
|
|
(source (file-system-device fs)))
|
|
|
|
|
(or (string=? target (%store-prefix))
|
|
|
|
|
(string=? target "/")
|
2018-05-18 07:43:07 -04:00
|
|
|
|
(and (string? source)
|
2017-11-30 10:14:12 -05:00
|
|
|
|
(string-prefix? "/dev/" source))
|
|
|
|
|
|
|
|
|
|
;; Labels and UUIDs are necessarily invalid in the VM.
|
|
|
|
|
(and (file-system-mount? fs)
|
2018-05-18 07:43:07 -04:00
|
|
|
|
(or (file-system-label? source)
|
2017-11-30 10:14:12 -05:00
|
|
|
|
(uuid? source))))))
|
2014-11-20 17:32:54 -05:00
|
|
|
|
(operating-system-file-systems os)))
|
|
|
|
|
|
2017-02-14 10:28:33 -05:00
|
|
|
|
(define virtual-file-systems
|
|
|
|
|
(cons (file-system
|
|
|
|
|
(mount-point "/")
|
|
|
|
|
(device "/dev/vda1")
|
|
|
|
|
(type "ext4"))
|
|
|
|
|
|
|
|
|
|
(append (map mapping->file-system mappings)
|
|
|
|
|
user-file-systems)))
|
|
|
|
|
|
2024-03-19 11:18:37 -04:00
|
|
|
|
(operating-system
|
|
|
|
|
(inherit os)
|
2017-10-19 17:11:36 -04:00
|
|
|
|
;; XXX: Until we run QEMU with UEFI support (with the OVMF firmware),
|
|
|
|
|
;; force the traditional i386/BIOS method.
|
|
|
|
|
;; See <https://bugs.gnu.org/28768>.
|
|
|
|
|
(bootloader (bootloader-configuration
|
2024-03-19 11:18:37 -04:00
|
|
|
|
(inherit (operating-system-bootloader os))
|
|
|
|
|
(bootloader
|
|
|
|
|
(if (target-riscv64? (or target system))
|
|
|
|
|
u-boot-qemu-riscv64-bootloader
|
|
|
|
|
grub-bootloader))
|
|
|
|
|
(targets '("/dev/vda"))))
|
2017-10-19 17:11:36 -04:00
|
|
|
|
|
2014-09-22 15:29:41 -04:00
|
|
|
|
(initrd (lambda (file-systems . rest)
|
2017-11-24 11:35:12 -05:00
|
|
|
|
(apply (operating-system-initrd os)
|
|
|
|
|
file-systems
|
2021-12-16 04:11:53 -05:00
|
|
|
|
#:volatile-root? volatile?
|
2014-09-22 15:29:41 -04:00
|
|
|
|
rest)))
|
2023-10-28 20:00:00 -04:00
|
|
|
|
;; The (QEMU-only) "cirrus" graphics driver is still expected by some
|
|
|
|
|
;; VPS with old QEMU versions. See <https://bugs.gnu.org/36069>.
|
|
|
|
|
(initrd-modules (let ((modules (operating-system-initrd-modules os)))
|
|
|
|
|
(if (member "cirrus" modules)
|
|
|
|
|
modules
|
|
|
|
|
(cons "cirrus" modules))))
|
2014-11-05 03:25:59 -05:00
|
|
|
|
|
|
|
|
|
;; Disable swap.
|
|
|
|
|
(swap-devices '())
|
|
|
|
|
|
2017-02-14 10:28:33 -05:00
|
|
|
|
;; XXX: When FULL-BOOT? is true, do not add a 9p mount for /gnu/store
|
|
|
|
|
;; since that would lead the bootloader config to look for the kernel and
|
|
|
|
|
;; initrd in it.
|
|
|
|
|
(file-systems (if full-boot?
|
|
|
|
|
virtual-file-systems
|
|
|
|
|
(cons
|
|
|
|
|
(file-system
|
|
|
|
|
(inherit (mapping->file-system %store-mapping))
|
|
|
|
|
(needed-for-boot? #t))
|
|
|
|
|
virtual-file-systems)))))
|
2014-05-02 18:26:07 -04:00
|
|
|
|
|
2021-12-16 04:11:53 -05:00
|
|
|
|
(define* (common-qemu-options image shared-fs
|
2024-03-19 11:18:41 -04:00
|
|
|
|
#:key
|
|
|
|
|
rw-image?
|
|
|
|
|
(target (%current-target-system)))
|
2014-11-20 16:48:18 -05:00
|
|
|
|
"Return the a string-value gexp with the common QEMU options to boot IMAGE,
|
|
|
|
|
with '-virtfs' options for the host file systems listed in SHARED-FS."
|
2017-02-14 10:28:32 -05:00
|
|
|
|
|
2014-11-20 16:48:18 -05:00
|
|
|
|
(define (virtfs-option fs)
|
2017-02-14 10:28:32 -05:00
|
|
|
|
#~(format #f "-virtfs local,path=~s,security_model=none,mount_tag=~s"
|
|
|
|
|
#$fs #$(file-system->mount-tag fs)))
|
2014-11-20 16:48:18 -05:00
|
|
|
|
|
2017-02-14 10:28:32 -05:00
|
|
|
|
#~(;; Only enable kvm if we see /dev/kvm exists.
|
2016-02-22 14:23:14 -05:00
|
|
|
|
;; This allows users without hardware virtualization to still use these
|
|
|
|
|
;; commands.
|
2024-03-19 11:18:41 -04:00
|
|
|
|
#$@(if (and (not target) (file-exists? "/dev/kvm"))
|
2017-02-14 10:28:32 -05:00
|
|
|
|
'("-enable-kvm")
|
|
|
|
|
'())
|
|
|
|
|
|
|
|
|
|
"-no-reboot"
|
2020-09-20 15:14:52 -04:00
|
|
|
|
"-object" "rng-random,filename=/dev/urandom,id=guix-vm-rng"
|
|
|
|
|
"-device" "virtio-rng-pci,rng=guix-vm-rng"
|
2017-02-14 10:28:32 -05:00
|
|
|
|
|
|
|
|
|
#$@(map virtfs-option shared-fs)
|
2021-12-16 04:11:53 -05:00
|
|
|
|
#$@(if rw-image?
|
2022-12-06 09:06:35 -05:00
|
|
|
|
#~((format #f "-drive file=~a,format=qcow2,if=virtio" #$image))
|
|
|
|
|
#~((format #f "-drive file=~a,format=raw,if=virtio,cache=writeback,werror=report,readonly=on"
|
2021-12-16 04:11:53 -05:00
|
|
|
|
#$image)))))
|
2014-11-06 16:58:12 -05:00
|
|
|
|
|
2014-11-07 16:43:33 -05:00
|
|
|
|
(define* (system-qemu-image/shared-store-script os
|
|
|
|
|
#:key
|
2020-05-27 17:09:49 -04:00
|
|
|
|
(system (%current-system))
|
|
|
|
|
(target (%current-target-system))
|
2014-11-07 16:43:33 -05:00
|
|
|
|
(qemu qemu)
|
|
|
|
|
(graphic? #t)
|
2021-12-16 04:11:53 -05:00
|
|
|
|
(volatile? #t)
|
2022-01-06 04:43:14 -05:00
|
|
|
|
(memory-size 512)
|
2014-11-20 17:32:54 -05:00
|
|
|
|
(mappings '())
|
2014-11-08 08:49:13 -05:00
|
|
|
|
full-boot?
|
|
|
|
|
(disk-image-size
|
2017-05-20 15:28:20 -04:00
|
|
|
|
(* (if full-boot? 500 70)
|
2017-07-18 04:36:21 -04:00
|
|
|
|
(expt 2 20)))
|
|
|
|
|
(options '()))
|
2014-01-31 08:36:48 -05:00
|
|
|
|
"Return a derivation that builds a script to run a virtual machine image of
|
2017-06-15 13:22:00 -04:00
|
|
|
|
OS that shares its store with the host. The virtual machine runs with
|
|
|
|
|
MEMORY-SIZE MiB of memory.
|
2014-11-08 08:49:13 -05:00
|
|
|
|
|
2014-11-20 17:32:54 -05:00
|
|
|
|
MAPPINGS is a list of <file-system-mapping> specifying mapping of host file
|
|
|
|
|
systems into the guest.
|
|
|
|
|
|
2014-11-08 08:49:13 -05:00
|
|
|
|
When FULL-BOOT? is true, the returned script runs everything starting from the
|
2021-12-16 04:11:53 -05:00
|
|
|
|
bootloader; otherwise it directly starts the operating system kernel. When
|
|
|
|
|
VOLATILE? is true, an overlay is created on top of a read-only
|
|
|
|
|
storage. Otherwise the storage is made persistent. The DISK-IMAGE-SIZE
|
|
|
|
|
parameter specifies the size in bytes of the root disk image; it is mostly
|
|
|
|
|
useful when FULL-BOOT? is true."
|
|
|
|
|
(mlet* %store-monad ((os -> (virtualized-operating-system
|
|
|
|
|
os mappings
|
2014-11-08 08:49:13 -05:00
|
|
|
|
#:full-boot? full-boot?
|
2024-03-19 11:18:37 -04:00
|
|
|
|
#:volatile? volatile?
|
|
|
|
|
#:system system
|
|
|
|
|
#:target target))
|
2021-12-16 04:11:53 -05:00
|
|
|
|
(base-image -> (system-image
|
|
|
|
|
(image
|
|
|
|
|
(inherit
|
|
|
|
|
(raw-with-offset-disk-image))
|
|
|
|
|
(operating-system os)
|
|
|
|
|
(size disk-image-size)
|
|
|
|
|
(shared-store?
|
|
|
|
|
(and (not full-boot?) volatile?))
|
|
|
|
|
(volatile-root? volatile?)))))
|
2017-02-14 10:28:32 -05:00
|
|
|
|
(define kernel-arguments
|
2017-04-21 08:37:11 -04:00
|
|
|
|
#~(list #$@(if graphic? #~() #~("console=ttyS0"))
|
2024-03-19 11:18:36 -04:00
|
|
|
|
#$@(operating-system-kernel-arguments os "/dev/vda1")))
|
2017-02-14 10:28:32 -05:00
|
|
|
|
|
2021-12-16 04:11:53 -05:00
|
|
|
|
(define rw-image
|
2022-01-13 05:44:54 -05:00
|
|
|
|
#~(format #f "/tmp/guix-image-~a" (basename #$base-image)))
|
2021-12-16 04:11:53 -05:00
|
|
|
|
|
2017-02-14 10:28:32 -05:00
|
|
|
|
(define qemu-exec
|
2024-04-16 13:02:17 -04:00
|
|
|
|
#~(list #+(with-parameters ((%current-system %system)
|
|
|
|
|
(%current-target-system #f))
|
|
|
|
|
;; Override %CURRENT-SYSTEM to always use a native emulator.
|
|
|
|
|
(file-append qemu "/bin/"
|
|
|
|
|
(qemu-command (or target system))))
|
2021-12-02 14:18:56 -05:00
|
|
|
|
;; Tells qemu to use the terminal it was started in for IO.
|
|
|
|
|
#$@(if graphic? '() #~("-nographic"))
|
2017-02-14 10:28:32 -05:00
|
|
|
|
#$@(if full-boot?
|
|
|
|
|
#~()
|
|
|
|
|
#~("-kernel" #$(operating-system-kernel-file os)
|
2018-11-15 08:36:16 -05:00
|
|
|
|
"-initrd" #$(file-append os "/initrd")
|
2017-02-14 10:28:32 -05:00
|
|
|
|
(format #f "-append ~s"
|
|
|
|
|
(string-join #$kernel-arguments " "))))
|
2024-03-19 11:18:40 -04:00
|
|
|
|
;; Default qemu-riscv64 have not PCI, virt have it, so we set it.
|
|
|
|
|
#$@(if (target-riscv64? (or target system))
|
|
|
|
|
#~("-M" "virt")
|
|
|
|
|
#~())
|
2021-12-16 04:11:53 -05:00
|
|
|
|
#$@(common-qemu-options (if volatile? base-image rw-image)
|
2017-02-14 10:28:32 -05:00
|
|
|
|
(map file-system-mapping-source
|
2021-12-16 04:11:53 -05:00
|
|
|
|
(cons %store-mapping mappings))
|
2024-03-19 11:18:41 -04:00
|
|
|
|
#:rw-image? (not volatile?)
|
|
|
|
|
#:target target)
|
2017-07-18 04:36:21 -04:00
|
|
|
|
"-m " (number->string #$memory-size)
|
|
|
|
|
#$@options))
|
2017-02-14 10:28:32 -05:00
|
|
|
|
|
2022-12-06 09:06:35 -05:00
|
|
|
|
(define copy-image
|
|
|
|
|
;; Script that "copies" BASE-IMAGE to /tmp. Make a copy-on-write image,
|
|
|
|
|
;; which is much cheaper than actually copying it.
|
|
|
|
|
(program-file "copy-image"
|
|
|
|
|
(with-imported-modules '((guix build utils))
|
|
|
|
|
#~(begin
|
|
|
|
|
(use-modules (guix build utils))
|
|
|
|
|
(unless (file-exists? #$rw-image)
|
|
|
|
|
(invoke #+(file-append qemu "/bin/qemu-img")
|
|
|
|
|
"create" "-b" #$base-image
|
|
|
|
|
"-F" "raw" "-f" "qcow2" #$rw-image))))))
|
|
|
|
|
|
2014-01-31 08:36:48 -05:00
|
|
|
|
(define builder
|
2014-04-26 10:36:48 -04:00
|
|
|
|
#~(call-with-output-file #$output
|
|
|
|
|
(lambda (port)
|
2021-12-16 04:11:53 -05:00
|
|
|
|
(format port "#!~a~%"
|
|
|
|
|
#+(file-append bash "/bin/sh"))
|
2022-12-06 09:06:35 -05:00
|
|
|
|
#$@(if volatile?
|
|
|
|
|
#~()
|
|
|
|
|
#~((format port "~a~%" #+copy-image)))
|
2021-12-16 04:11:53 -05:00
|
|
|
|
(format port "exec ~a \"$@\"~%"
|
2017-02-14 10:28:32 -05:00
|
|
|
|
(string-join #$qemu-exec " "))
|
2014-04-26 10:36:48 -04:00
|
|
|
|
(chmod port #o555))))
|
|
|
|
|
|
|
|
|
|
(gexp->derivation "run-vm.sh" builder)))
|
2014-01-31 08:36:48 -05:00
|
|
|
|
|
2024-01-20 08:55:46 -05:00
|
|
|
|
(define* (linux-image-startup-command image
|
|
|
|
|
#:key
|
|
|
|
|
(system (%current-system))
|
|
|
|
|
(target #f)
|
|
|
|
|
(qemu qemu-minimal)
|
|
|
|
|
(graphic? #f)
|
|
|
|
|
(cpu "max")
|
|
|
|
|
(cpu-count 1)
|
|
|
|
|
(memory-size 1024)
|
|
|
|
|
(port-forwardings '())
|
|
|
|
|
(date #f))
|
|
|
|
|
"Return a list-valued gexp representing the command to start QEMU to run
|
|
|
|
|
IMAGE, assuming it uses the Linux kernel, and not sharing the store with the
|
|
|
|
|
host."
|
|
|
|
|
(define os
|
|
|
|
|
;; Note: 'image-operating-system' would return the wrong OS, before
|
|
|
|
|
;; its root partition has been assigned a UUID.
|
|
|
|
|
(operating-system-for-image image))
|
|
|
|
|
|
|
|
|
|
(define kernel-arguments
|
|
|
|
|
#~(list #$@(if graphic? #~() #~("console=ttyS0"))
|
2024-03-19 11:18:36 -04:00
|
|
|
|
#$@(operating-system-kernel-arguments os "/dev/vda1")))
|
2024-01-20 08:55:46 -05:00
|
|
|
|
|
|
|
|
|
#~`(#+(file-append qemu "/bin/"
|
|
|
|
|
(qemu-command (or target system)))
|
|
|
|
|
,@(if (access? "/dev/kvm" (logior R_OK W_OK))
|
|
|
|
|
'("-enable-kvm")
|
|
|
|
|
'())
|
|
|
|
|
|
|
|
|
|
"-cpu" #$cpu
|
|
|
|
|
#$@(if (> cpu-count 1)
|
|
|
|
|
#~("-smp" #$(string-append "cpus=" (number->string cpu-count)))
|
|
|
|
|
#~())
|
|
|
|
|
"-m" #$(number->string memory-size)
|
|
|
|
|
"-nic" #$(string-append
|
|
|
|
|
"user,model=virtio-net-pci,"
|
|
|
|
|
(port-forwardings->qemu-options port-forwardings))
|
|
|
|
|
"-kernel" #$(operating-system-kernel-file os)
|
|
|
|
|
"-initrd" #$(file-append os "/initrd")
|
|
|
|
|
"-append" ,(string-join #$kernel-arguments)
|
|
|
|
|
"-serial" "stdio"
|
|
|
|
|
|
|
|
|
|
#$@(if date
|
|
|
|
|
#~("-rtc"
|
|
|
|
|
#$(string-append "base=" (date->string date "~5")))
|
|
|
|
|
#~())
|
|
|
|
|
|
|
|
|
|
"-object" "rng-random,filename=/dev/urandom,id=guix-vm-rng"
|
|
|
|
|
"-device" "virtio-rng-pci,rng=guix-vm-rng"
|
|
|
|
|
|
|
|
|
|
"-drive"
|
|
|
|
|
,(string-append "file=" #$(system-image image)
|
|
|
|
|
",format=qcow2,if=virtio,"
|
|
|
|
|
"cache=writeback,werror=report,readonly=off")
|
|
|
|
|
"-snapshot"
|
|
|
|
|
"-no-reboot"))
|
|
|
|
|
|
2017-07-18 04:36:21 -04:00
|
|
|
|
|
|
|
|
|
;;;
|
|
|
|
|
;;; High-level abstraction.
|
|
|
|
|
;;;
|
|
|
|
|
|
|
|
|
|
(define-record-type* <virtual-machine> %virtual-machine
|
|
|
|
|
make-virtual-machine
|
|
|
|
|
virtual-machine?
|
|
|
|
|
(operating-system virtual-machine-operating-system) ;<operating-system>
|
|
|
|
|
(qemu virtual-machine-qemu ;<package>
|
2021-11-20 16:37:44 -05:00
|
|
|
|
(default qemu-minimal))
|
2024-01-20 08:54:34 -05:00
|
|
|
|
(cpu-count virtual-machine-cpu-count ;integer
|
|
|
|
|
(default 1))
|
2021-12-16 04:11:53 -05:00
|
|
|
|
(volatile? virtual-machine-volatile? ;Boolean
|
|
|
|
|
(default #t))
|
2017-07-18 04:36:21 -04:00
|
|
|
|
(graphic? virtual-machine-graphic? ;Boolean
|
|
|
|
|
(default #f))
|
|
|
|
|
(memory-size virtual-machine-memory-size ;integer (MiB)
|
|
|
|
|
(default 256))
|
2017-09-28 13:57:18 -04:00
|
|
|
|
(disk-image-size virtual-machine-disk-image-size ;integer (bytes)
|
|
|
|
|
(default 'guess))
|
2017-07-18 04:36:21 -04:00
|
|
|
|
(port-forwardings virtual-machine-port-forwardings ;list of integer pairs
|
2024-01-20 05:47:47 -05:00
|
|
|
|
(default '()))
|
|
|
|
|
(date virtual-machine-date ;SRFI-19 date | #f
|
|
|
|
|
(default #f)))
|
2017-07-18 04:36:21 -04:00
|
|
|
|
|
|
|
|
|
(define-syntax virtual-machine
|
|
|
|
|
(syntax-rules ()
|
|
|
|
|
"Declare a virtual machine running the specified OS, with the given
|
|
|
|
|
options."
|
|
|
|
|
((_ os) ;shortcut
|
|
|
|
|
(%virtual-machine (operating-system os)))
|
|
|
|
|
((_ fields ...)
|
|
|
|
|
(%virtual-machine fields ...))))
|
|
|
|
|
|
|
|
|
|
(define (port-forwardings->qemu-options forwardings)
|
|
|
|
|
"Return the QEMU option for the given port FORWARDINGS as a string, where
|
|
|
|
|
FORWARDINGS is a list of host-port/guest-port pairs."
|
|
|
|
|
(string-join
|
|
|
|
|
(map (match-lambda
|
|
|
|
|
((host-port . guest-port)
|
|
|
|
|
(string-append "hostfwd=tcp::"
|
|
|
|
|
(number->string host-port)
|
|
|
|
|
"-:" (number->string guest-port))))
|
|
|
|
|
forwardings)
|
|
|
|
|
","))
|
|
|
|
|
|
|
|
|
|
(define-gexp-compiler (virtual-machine-compiler (vm <virtual-machine>)
|
|
|
|
|
system target)
|
|
|
|
|
(match vm
|
2024-01-20 08:54:34 -05:00
|
|
|
|
(($ <virtual-machine> os qemu cpus volatile? graphic? memory-size
|
2024-01-20 05:47:47 -05:00
|
|
|
|
disk-image-size forwardings date)
|
2017-07-18 04:36:21 -04:00
|
|
|
|
(let ((options
|
2024-01-20 05:47:47 -05:00
|
|
|
|
(append (if (null? forwardings)
|
|
|
|
|
'()
|
|
|
|
|
`("-nic" ,(string-append
|
|
|
|
|
"user,model=virtio-net-pci,"
|
|
|
|
|
(port-forwardings->qemu-options
|
|
|
|
|
forwardings))))
|
2024-01-20 08:54:34 -05:00
|
|
|
|
(if (> cpus 1)
|
|
|
|
|
`("-smp" ,(string-append "cpus="
|
|
|
|
|
(number->string cpus)))
|
|
|
|
|
'())
|
2024-01-20 05:47:47 -05:00
|
|
|
|
(if date
|
|
|
|
|
`("-rtc"
|
|
|
|
|
,(string-append
|
|
|
|
|
"base=" (date->string date "~5")))
|
|
|
|
|
'()))))
|
2017-07-18 04:36:21 -04:00
|
|
|
|
(system-qemu-image/shared-store-script os
|
2020-05-27 17:09:49 -04:00
|
|
|
|
#:system system
|
|
|
|
|
#:target target
|
2017-07-18 04:36:21 -04:00
|
|
|
|
#:qemu qemu
|
|
|
|
|
#:graphic? graphic?
|
2021-12-16 04:11:53 -05:00
|
|
|
|
#:volatile? volatile?
|
2017-07-18 04:36:21 -04:00
|
|
|
|
#:memory-size memory-size
|
2017-09-28 13:57:18 -04:00
|
|
|
|
#:disk-image-size
|
|
|
|
|
disk-image-size
|
2017-07-18 04:36:21 -04:00
|
|
|
|
#:options options)))))
|
|
|
|
|
|
2013-02-15 21:28:26 -05:00
|
|
|
|
;;; vm.scm ends here
|