Skip to content

Instantly share code, notes, and snippets.

@cleverca22
Last active May 17, 2018 19:20
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save cleverca22/010456d1d1895f760bd8244fd62ffd9f to your computer and use it in GitHub Desktop.
Save cleverca22/010456d1d1895f760bd8244fd62ffd9f to your computer and use it in GitHub Desktop.
let
pkgs = import <nixpkgs> {};
lib = pkgs.lib;
dom0_config = { pkgs, ... }:
{
virtualisation.xen = {
enable = true;
bootParams = [ "loglvl=all guest_loglvl=all com1=38400,8n1 console=com1" ];
};
fileSystems = {
"/" = { device = "LABEL=nixos"; fsType = "ext4"; };
"/stuff" = { device = "/dev/sdb"; fsType = "ext4"; autoFormat = true; };
};
boot = {
loader.grub = {
enable = true;
device = "/dev/vda";
};
kernelParams = [ "console=hvc0 earlyprintk=xen" ];
};
services.mingetty.autologinUser = "root";
systemd.services.xentest = {
after = [ "xen-bridge.service" "xen-qemu.service" ];
description = "xen automated testing";
wantedBy = [ "multi-user.target" ];
path = with pkgs; [ xen e2fsprogs utillinux ];
script = ''
exec > /dev/hvc0 2>&1
xl list
touch /stuff/stuff
#shutdown -h now
'';
};
};
dom0_build = import <nixpkgs/nixos> { configuration = dom0_config; };
rootDisk = import <nixpkgs/nixos/lib/make-disk-image.nix> {
# ~5mins to build
name = "xen-rootfs";
inherit pkgs lib;
config = dom0_build.config;
partitioned = true;
installBootLoader = true;
diskSize = 4096;
fixValidity = false;
};
qemu-script = pkgs.runCommand "yes-im-insane" { buildInputs = [ pkgs.qemu ]; } ''
pwd
mkdir -pv $out/bin/
cat ${pkgs.xen}/boot/xen.gz | gunzip > $out/xen
cat > $out/bin/testit <<EOF
#!/bin/sh
export PATH=${pkgs.qemu}/bin:$PATH
rm root-clone.qcow2
qemu-img create -f qcow2 -b ${rootDisk}/nixos.img root-clone.qcow2
if ! test -e output.img; then
qemu-img create -f raw output.img 64M
fi
qemu-system-x86_64 -serial mon:stdio -m 1024 -no-reboot -drive index=1,file=root-clone.qcow2,media=disk -drive index=2,file=output.img,media=disk -enable-kvm
EOF
chmod +x $out/bin/testit
'';
qemu-test = pkgs.runCommand "crazy" { buildInputs = [ pkgs.qemu ]; } ''
mkdir $out
qemu-img create -f qcow2 -b ${rootDisk}/nixos.img root-clone.qcow2
qemu-img create -f raw $out/output.img 64M
qemu-system-x86_64 -serial stdio -m 1024 -no-reboot -display none -drive index=1,file=root-clone.qcow2,media=disk -drive index=2,file=$out/output.img,media=disk -enable-kvm | tee $out/logfile
'';
in {
inherit qemu-script qemu-test rootDisk;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment