Qemu VM with port forwarding

Posted on June 15, 2016

The aim is to run a Qemu VM backed by an LVM partition and access guest’s SSH using host’s port. Kernel and initrd are loaded from host’s filesystem; this requires that the root partition is already mounted. The guest OS is NixOS.

Guest user home directory is encrypted using encryptfs, but this has no bearing on the following script.

qemu-kvm \
    -m 1024 \
    -drive format=raw,file=/dev/NixGroup/nix-vm,media=disk \
    -enable-kvm \
    -netdev user,id=network0,hostfwd=tcp:127.0.0.1:60722-:22 \
    -device e1000,netdev=network0 \
    -nographic \
    -kernel /mounts/nix-vm/nix/store/43m82sa6lssfmk8s3sxq4ykcbw06kn27-linux-4.3/bzImage \
    -append 'systemConfig=/nix/store/lyk5b9habhxlsfyq1yciiyz373xz43js-nixos-15.09.764.5df488a init=/nix/store/lyk5b9habhxlsfyq1yciiyz373xz43js-nixos-15.09.764.5df488a/init loglevel=4' \
    -initrd /mounts/nix-vm/nix/store/3fvg6zrq2s8s3xdj48gx6wlg4mdfqn2a-initrd/initrd

configuration.nix FS configuration is:

fileSystems."/" = {
  device  = "/dev/sda";
  label   = "nix-tor";
  fsType  = "ext3";
  options = "async,noatime,rw";
};

and networking:

networking.interfaces."enp0s3" = {
  useDHCP = true;
};