Skip to content

Instantly share code, notes, and snippets.

Created December 11, 2016 14:09
Show Gist options
  • Save anonymous/e6bbd456adc3855235cbf92a9ed2f859 to your computer and use it in GitHub Desktop.
Save anonymous/e6bbd456adc3855235cbf92a9ed2f859 to your computer and use it in GitHub Desktop.
{ pkgs ? (import <nixpkgs> {
config.packageOverrides = pkgs:
{
qemu = pkgs.qemu.overrideDerivation (super: {
src = pkgs.fetchurl {
url = "http://wiki.qemu-project.org/download/qemu-2.4.1.tar.bz2";
sha256 = "0xx1wc7lj5m3r2ab7f0axlfknszvbd8rlclpqz4jk48zid6czmg3";
};
patches = super.patches ++ [ (pkgs.fetchurl {
url = "https://github.com/SnabbCo/qemu/commit/f393aea2301734647fdf470724433f44702e3fb9.patch";
sha256 = "0hpnfdk96rrdaaf6qr4m4pgv40dw7r53mg95f22axj7nsyr8d72x";
})];
});
};
})
}:
with pkgs;
with lib;
with vmTools;
let
lib = pkgs.lib;
# modules and NixOS config for plain qemu image
modules = [
<nixpkgs/nixos/modules/profiles/qemu-guest.nix>
({config, pkgs, ...}: {
environment.systemPackages = with pkgs; [ inetutils screen python pciutils ethtool tcpdump netcat iperf iptables ];
fileSystems."/".device = "/dev/vda1";
boot.loader.grub.device = "/dev/vda";
# settings needed by tests
networking.firewall.enable = mkOverride 150 false;
services.mingetty.autologinUser = "root";
users.extraUsers.root.initialHashedPassword = mkOverride 150 "";
networking.usePredictableInterfaceNames = false;
})
({config, pkgs, lib, ...}:
let
dpdk_bind = pkgs.fetchurl {
url = "https://raw.githubusercontent.com/scylladb/dpdk/8ea56fadc9a49c575bee6bb3892bc17dd9ec4ab6/tools/dpdk_nic_bind.py";
sha256 = "0z8big9gh49q9kh0jjg1p9g5ywwvb130r3bmhhpbgx7blhk9zb7f";
};
in {
systemd.services.dpdk = {
wantedBy = [ "multi-user.target" ];
after = [ "network.target" ];
path = with pkgs; [ kmod python pciutils iproute utillinux ];
script = ''
mkdir -p /hugetlbfs
mount -t hugetlbfs nodev /hugetlbfs
echo 64 > /sys/devices/system/node/node0/hugepages/hugepages-2048kB/nr_hugepages
MODULE_DIR=/run/current-system/kernel-modules/lib/modules modprobe uio
insmod ${config.boot.kernelPackages.dpdk.kmod}/lib/modules/${config.boot.kernelPackages.kernel.modDirVersion}/kernel/drivers/net/igb_uio.ko
#insmod ${config.boot.kernelPackages.dpdk}/kmod/igb_uio.ko
python ${dpdk_bind} --bind=igb_uio 00:03.0
${config.boot.kernelPackages.dpdk.examples}/bin/l2fwd -c 0x1 -n1 -- -p 0x1
'';
};
}
)
];
config = (import <nixpkgs/nixos/lib/eval-config.nix> { inherit modules; }).config;
in rec {
# files needed for some tests
test_env = runCommand "test_env" (rec {
passthru.config = config;
qemu_img = lib.makeOverridable (import <nixpkgs/nixos/lib/make-disk-image.nix>) {
name = "snabb-nixos-minimal";
inherit lib config pkgs;
diskSize = 2 * 1024;
};
}) ''
mkdir -p $out
ln -s $qemu_img/nixos.img $out/qemu.img
ln -s ${config.system.build.toplevel}/kernel $out/bzImage
ln -s ${config.system.build.toplevel}/initrd $out/initrd
'';
vm1 = runCommand "vm1" {
__noChroot = true;
} ''
mkdir -p $out
cp ${test_env}/qemu.img .
ls -l ${test_env}
/var/setuid-wrappers/sudo -E ${numactl}/bin/numactl --cpunodebind=0 --membind=0 ${qemu}/bin/qemu-system-x86_64 \
-initrd ${test_env}/initrd \
-kernel ${test_env}/bzImage \
-append "earlyprintk root=/dev/vda init=${config.system.build.toplevel}/init rw console=ttyS0 ip=fe80::5054:ff:fe00:0000" \
-m 512 -numa node,memdev=mem -object memory-backend-file,id=mem,size=512M,mem-path=/hugetlbfs,share=on \
-netdev type=vhost-user,id=net0,chardev=char0 -chardev socket,id=char0,path=/home/luke/A.socket,server \
-device virtio-net-pci,netdev=net0,mac=00:00:00:00:00:01,mq=off,vectors=3 \
-M pc -smp 1 -cpu host --enable-kvm \
-serial telnet:localhost:5000,server,nowait \
-drive if=virtio,file=qemu.img \
-nographic
'';
vm2 = runCommand "vm2" {
__noChroot = true;
} ''
mkdir -p $out
cp ${test_env}/qemu.img .
/var/setuid-wrappers/sudo -E ${numactl}/bin/numactl --cpunodebind=0 --membind=0 ${qemu}/bin/qemu-system-x86_64 \
-initrd ${test_env}/initrd \
-kernel ${test_env}/bzImage \
-append "earlyprintk root=/dev/vda init=${config.system.build.toplevel}/init rw console=ttyS0 ip=fe80::5054:ff:fe00:0000" \
-m 512 -numa node,memdev=mem -object memory-backend-file,id=mem,size=512M,mem-path=/hugetlbfs,share=on \
-netdev type=vhost-user,id=net0,chardev=char0 -chardev socket,id=char0,path=/home/luke/B.socket,server \
-device virtio-net-pci,netdev=net0,mac=00:00:00:00:00:02,mq=off,vectors=3 \
-M pc -smp 1 -cpu host --enable-kvm \
-serial telnet:localhost:5001,server,nowait \
-drive if=virtio,file=qemu.img \
-nographic
'';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment