;; Self-contained VM with the 'hello' package and an SSH server. ;; ;; Unlike a system container -- which bind-mounts the WHOLE host ;; /gnu/store read-only into the guest -- a disk image created with ;; ;; guix system image -t qcow2 hello-vm.scm ;; ;; contains only the closure of this system. The guest has its own ;; kernel and sees no host file systems at all. ;; ;; The image root partition is labelled "Guix_image" (hardcoded in ;; gnu/system/image.scm for the qcow2 image type). Host SSH keys are ;; generated at first boot and persist in the (writable) image, so the ;; host key is stable without pinning. ;; ;; Run it (hostfwd maps host port 2223 to guest port 22): ;; ;; qemu-system-x86_64 -m 1024 \ ;; -drive file=/gnu/store/...-image.qcow2,format=qcow2 \ ;; -netdev user,id=n0,hostfwd=tcp:127.0.0.1:2223-:22 \ ;; -device virtio-net-pci,netdev=n0 \ ;; -nographic ;; ;; then connect: ;; ;; ssh -p 2223 -i ../etc/hello-container-key root@127.0.0.1 (use-modules (gnu) (gnu packages base) (gnu services base) (gnu services shepherd) (srfi srfi-1)) (use-package-modules base bash admin guile gawk compression linux) (use-service-modules ssh) (operating-system (host-name "hello-vm") (timezone "Europe/Amsterdam") (locale "en_US.utf8") (kernel-arguments '("console=ttyS0,115200")) (bootloader (bootloader-configuration (bootloader grub-bootloader) (targets '("/dev/vda")))) ;; The qcow2 image type creates a root partition labelled "Guix_image". (file-systems (cons (file-system (device (file-system-label "Guix_image")) (mount-point "/") (type "ext4")) %base-file-systems)) ;; Users are mandatory in an OS declaration. Give root a password ;; so we can also log in on the serial console ("hello"). (users (cons (user-account (name "root") (uid 0) (group "root") (password (crypt "hello" "$6$abc$0123456789abcdef$"))) %base-user-accounts)) ;; A SLIM package set: just the administration basics (bash, ;; coreutils, editors-free). %base-packages would pull in ~400 ;; binaries (nano, man-db, sudo, inetutils -- which provides ftp and ;; telnet --, wget, pciutils, ...). The ssh client/server tools come ;; in via the openssh service, and 'herd' via the essential shepherd ;; service, so they stay regardless. (packages (list bash coreutils findutils grep sed gawk tar gzip shadow procps guile-3.0-latest hello)) (services (append (list ;; Password-less root login with etc/hello-container-key on the ;; guest's port 22; the VM runner forwards a host port to it. (service openssh-service-type (openssh-configuration (permit-root-login #t) (password-authentication? #f) (authorized-keys `(("root" ,(local-file "../etc/hello-container-key.pub" "authorized_keys")))))) ;; QEMU user-mode network stack: guest gets 10.0.2.15, gateway ;; 10.0.2.2 (see %qemu-static-networking in gnu/services/base.scm). (service static-networking-service-type (list %qemu-static-networking)) ;; Serial console login, for debugging when ssh does not work. (service agetty-service-type (agetty-configuration (extra-options '("-L")) (baud-rate "115200") (term "vt100") (tty "ttyS0")))) (modify-services %base-services ;; No guix-daemon in the VM: it is not needed (images are built ;; on the host), and its service would pull guix, guix-daemon ;; etc. into the system profile. (delete guix-service-type)))))