blob: 6bb6d08a59f6172ad1fe6eb88a69ae586da92b66 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
{ pkgs ? import <nixpkgs> {}
, kernelPackages ? pkgs.linuxPackages_latest
}:
let
inherit (pkgs) lib;
nixos = import (pkgs.path + "/nixos") {
inherit (pkgs) system;
configuration = { modulesPath, ... }: {
imports = [
(modulesPath + "/profiles/minimal.nix")
(modulesPath + "/profiles/perlless.nix")
];
system.stateVersion = lib.trivial.release;
boot.kernelPackages = kernelPackages;
boot.initrd.allowMissingModules = true; # e.g. ahci, efivarfs with a cut-down kernel
boot.initrd.kernelModules = [ "virtio_mmio" "virtio_blk" "erofs" ];
boot.kernelParams = [
"console=ttyS0" "reboot=k" "panic=1"
"8250.nr_uarts=1" # only ttyS0 exists (stops 0x2f8/0x3e8/0x2e8 probing)
];
fileSystems."/" = {
fsType = "tmpfs";
options = [ "mode=755" ];
};
fileSystems."/nix/store" = {
device = "/dev/vda";
fsType = "erofs";
options = [ "ro" ];
};
services.getty.autologinUser = "root";
networking.useDHCP = false;
boot.bcache.enable = false;
boot.enableContainers = false;
boot.initrd.systemd.tpm2.enable = false;
boot.kexec.enable = false;
networking.firewall.enable = false;
nix.enable = false;
powerManagement.enable = false;
programs.fuse.enable = false;
programs.nano.enable = false;
security.sudo.enable = false;
services.fstrim.enable = false;
services.lvm.enable = false;
services.nscd.enable = false;
services.timesyncd.enable = false;
system.nssModules = lib.mkForce [ ];
system.switch.enable = false;
systemd.coredump.enable = false;
systemd.oomd.enable = false;
systemd.tpm2.enable = false;
users.manageLingering = false;
};
};
inherit (nixos.config.system.build) toplevel kernel initialRamdisk;
config = pkgs.writeText "firecracker.json" (builtins.toJSON {
boot-source = {
kernel_image_path = "${kernel.dev}/vmlinux";
initrd_path = "${initialRamdisk}/${nixos.config.system.boot.loader.initrdFile}";
boot_args = toString (nixos.config.boot.kernelParams ++ [ "init=${toplevel}/init" ]);
};
drives = [{
drive_id = "store";
path_on_host = "@STORE_IMAGE@";
is_root_device = false;
is_read_only = true;
}];
machine-config = {
vcpu_count = 2;
mem_size_mib = 1024;
};
});
in
pkgs.writeShellScriptBin "run-firecracker-vm" ''
# usage: run-firecracker-vm [STORE-PATH...]
# Packs the closure of the guest system (plus any given store paths) into
# a temporary erofs image, which the guest mounts read-only at /nix/store.
set -efu
export PATH=${lib.makeBinPath [ pkgs.coreutils pkgs.gnused pkgs.gnutar pkgs.erofs-utils pkgs.nix pkgs.firecracker ]}
tmp=$(mktemp -d)
trap 'rm -rf "$tmp"' EXIT
nix-store -qR ${toplevel} "$@" > "$tmp"/store-paths
tar -c -P --verbatim-files-from \
--transform 'flags=rSh;s|/nix/store/||' \
--files-from "$tmp"/store-paths \
| mkfs.erofs --quiet --tar=f --force-uid=0 --force-gid=0 --hard-dereference -T 0 "$tmp"/store.img
sed "s|@STORE_IMAGE@|$tmp/store.img|" ${config} > "$tmp"/config.json
firecracker --no-api --config-file "$tmp"/config.json
''
|