|
|
|
@ -11,77 +11,6 @@ let
|
|
|
|
|
|
|
|
|
|
consoles = lib.concatMapStringsSep " " (c: "console=${c}") cfg.qemu.consoles;
|
|
|
|
|
|
|
|
|
|
driveOpts = { ... }: {
|
|
|
|
|
|
|
|
|
|
options = {
|
|
|
|
|
|
|
|
|
|
file = mkOption {
|
|
|
|
|
type = types.str;
|
|
|
|
|
description = "The file image used for this drive.";
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
driveExtraOpts = mkOption {
|
|
|
|
|
type = types.attrsOf types.str;
|
|
|
|
|
default = { };
|
|
|
|
|
description = "Extra options passed to drive flag.";
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
deviceExtraOpts = mkOption {
|
|
|
|
|
type = types.attrsOf types.str;
|
|
|
|
|
default = { };
|
|
|
|
|
description = "Extra options passed to device flag.";
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
name = mkOption {
|
|
|
|
|
type = types.nullOr types.str;
|
|
|
|
|
default = null;
|
|
|
|
|
description =
|
|
|
|
|
"A name for the drive. Must be unique in the drives list. Not passed to qemu.";
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
driveCmdline = idx:
|
|
|
|
|
{ file, driveExtraOpts, deviceExtraOpts, ... }:
|
|
|
|
|
let
|
|
|
|
|
drvId = "drive${toString idx}";
|
|
|
|
|
mkKeyValue = generators.mkKeyValueDefault { } "=";
|
|
|
|
|
mkOpts = opts: concatStringsSep "," (mapAttrsToList mkKeyValue opts);
|
|
|
|
|
driveOpts = mkOpts (driveExtraOpts // {
|
|
|
|
|
index = idx;
|
|
|
|
|
id = drvId;
|
|
|
|
|
"if" = "none";
|
|
|
|
|
inherit file;
|
|
|
|
|
});
|
|
|
|
|
deviceOpts = mkOpts (deviceExtraOpts // { drive = drvId; });
|
|
|
|
|
device = if cfg.qemu.diskInterface == "scsi" then
|
|
|
|
|
"-device lsi53c895a -device scsi-hd,${deviceOpts}"
|
|
|
|
|
else
|
|
|
|
|
"-device virtio-blk-pci,${deviceOpts}";
|
|
|
|
|
in "-drive ${driveOpts} ${device}";
|
|
|
|
|
|
|
|
|
|
drivesCmdLine = drives: concatStringsSep " " (imap1 driveCmdline drives);
|
|
|
|
|
|
|
|
|
|
# Creates a device name from a 1-based a numerical index, e.g.
|
|
|
|
|
# * `driveDeviceName 1` -> `/dev/vda`
|
|
|
|
|
# * `driveDeviceName 2` -> `/dev/vdb`
|
|
|
|
|
driveDeviceName = idx:
|
|
|
|
|
let letter = elemAt lowerChars (idx - 1);
|
|
|
|
|
in if cfg.qemu.diskInterface == "scsi" then
|
|
|
|
|
"/dev/sd${letter}"
|
|
|
|
|
else
|
|
|
|
|
"/dev/vd${letter}";
|
|
|
|
|
|
|
|
|
|
lookupDriveDeviceName = driveName: driveList:
|
|
|
|
|
(findSingle (drive: drive.name == driveName)
|
|
|
|
|
(throw "Drive ${driveName} not found")
|
|
|
|
|
(throw "Multiple drives named ${driveName}") driveList).device;
|
|
|
|
|
|
|
|
|
|
addDeviceNames =
|
|
|
|
|
imap1 (idx: drive: drive // { device = driveDeviceName idx; });
|
|
|
|
|
|
|
|
|
|
efiPrefix = if (pkgs.stdenv.isi686 || pkgs.stdenv.isx86_64) then
|
|
|
|
|
"${pkgs.buildPackages.OVMF.fd}/FV/OVMF"
|
|
|
|
|
else if pkgs.stdenv.isAarch64 then
|
|
|
|
@ -94,144 +23,38 @@ let
|
|
|
|
|
# Shell script to start the VM.
|
|
|
|
|
startVM = ''
|
|
|
|
|
#! ${pkgs.buildPackages.runtimeShell}
|
|
|
|
|
|
|
|
|
|
'' + lib.optionalString (config.virtualisation.diskImage != null) ''
|
|
|
|
|
NIX_DISK_IMAGE=$(readlink -f ''${NIX_DISK_IMAGE:-${config.virtualisation.diskImage}})
|
|
|
|
|
|
|
|
|
|
if ! test -w "$NIX_DISK_IMAGE"; then
|
|
|
|
|
${qemu}/bin/qemu-img create -f qcow2 -b $NIX_DISK_IMAGE $TMPDIR/disk.img || exit 1
|
|
|
|
|
NIX_DISK_IMAGE=$TMPDIR/disk.img
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
if ! test -e "$NIX_DISK_IMAGE"; then
|
|
|
|
|
${qemu}/bin/qemu-img create -f qcow2 "$NIX_DISK_IMAGE" \
|
|
|
|
|
${toString config.virtualisation.diskSize}M || exit 1
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
'' + ''
|
|
|
|
|
# Create a directory for storing temporary data of the running VM.
|
|
|
|
|
if [ -z "$TMPDIR" -o -z "$USE_TMPDIR" ]; then
|
|
|
|
|
TMPDIR=$(mktemp -d nix-vm.XXXXXXXXXX --tmpdir)
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
# Create a directory for exchanging data with the VM.
|
|
|
|
|
mkdir -p $TMPDIR/xchg
|
|
|
|
|
|
|
|
|
|
${if cfg.useBootLoader then ''
|
|
|
|
|
# Create a writable copy/snapshot of the boot disk.
|
|
|
|
|
# A writable boot disk can be booted from automatically.
|
|
|
|
|
${qemu}/bin/qemu-img create -f qcow2 -b ${bootDisk}/disk.img $TMPDIR/disk.img || exit 1
|
|
|
|
|
|
|
|
|
|
NIX_EFI_VARS=$(readlink -f ''${NIX_EFI_VARS:-${cfg.efiVars}})
|
|
|
|
|
|
|
|
|
|
${if cfg.useEFIBoot then ''
|
|
|
|
|
# VM needs writable EFI vars
|
|
|
|
|
if ! test -e "$NIX_EFI_VARS"; then
|
|
|
|
|
cp ${bootDisk}/efi-vars.fd "$NIX_EFI_VARS" || exit 1
|
|
|
|
|
chmod 0644 "$NIX_EFI_VARS" || exit 1
|
|
|
|
|
fi
|
|
|
|
|
'' else
|
|
|
|
|
""}
|
|
|
|
|
'' else
|
|
|
|
|
""}
|
|
|
|
|
|
|
|
|
|
cd $TMPDIR
|
|
|
|
|
idx=0
|
|
|
|
|
${flip concatMapStrings cfg.emptyDiskImages (size: ''
|
|
|
|
|
if ! test -e "empty$idx.qcow2"; then
|
|
|
|
|
${qemu}/bin/qemu-img create -f qcow2 "empty$idx.qcow2" "${
|
|
|
|
|
toString size
|
|
|
|
|
}M"
|
|
|
|
|
fi
|
|
|
|
|
idx=$((idx + 1))
|
|
|
|
|
'')}
|
|
|
|
|
|
|
|
|
|
# Start QEMU.
|
|
|
|
|
set -v
|
|
|
|
|
exec ${qemuBinary qemu} \
|
|
|
|
|
-name ${config.system.name} \
|
|
|
|
|
-m ${toString config.virtualisation.memorySize} \
|
|
|
|
|
-smp ${toString config.virtualisation.cores} \
|
|
|
|
|
-device virtio-rng-pci \
|
|
|
|
|
${concatStringsSep " " config.virtualisation.qemu.networkingOptions} \
|
|
|
|
|
-virtfs local,path=/nix/store,security_model=none,mount_tag=store \
|
|
|
|
|
-virtfs local,path=$TMPDIR/xchg,security_model=none,mount_tag=xchg \
|
|
|
|
|
-virtfs local,path=''${SHARED_DIR:-$TMPDIR/xchg},security_model=none,mount_tag=shared \
|
|
|
|
|
${drivesCmdLine config.virtualisation.qemu.drives} \
|
|
|
|
|
${toString config.virtualisation.qemu.options} \
|
|
|
|
|
$NIX_DISK_IMAGE \
|
|
|
|
|
$QEMU_OPTS \
|
|
|
|
|
"$@"
|
|
|
|
|
'';
|
|
|
|
|
|
|
|
|
|
regInfo =
|
|
|
|
|
pkgs.closureInfo { rootPaths = config.virtualisation.pathsInNixDB; };
|
|
|
|
|
|
|
|
|
|
# Generate a hard disk image containing a /boot partition and GRUB
|
|
|
|
|
# in the MBR. Used when the `useBootLoader' option is set.
|
|
|
|
|
# Uses `runInLinuxVM` to create the image in a throwaway VM.
|
|
|
|
|
# See note [Disk layout with `useBootLoader`].
|
|
|
|
|
# FIXME: use nixos/lib/make-disk-image.nix.
|
|
|
|
|
bootDisk = pkgs.vmTools.runInLinuxVM (pkgs.runCommand "nixos-boot-disk" {
|
|
|
|
|
preVM = ''
|
|
|
|
|
mkdir $out
|
|
|
|
|
diskImage=$out/disk.img
|
|
|
|
|
${qemu}/bin/qemu-img create -f qcow2 $diskImage "60M"
|
|
|
|
|
${if cfg.useEFIBoot then ''
|
|
|
|
|
efiVars=$out/efi-vars.fd
|
|
|
|
|
cp ${efiVarsDefault} $efiVars
|
|
|
|
|
chmod 0644 $efiVars
|
|
|
|
|
'' else
|
|
|
|
|
""}
|
|
|
|
|
'';
|
|
|
|
|
buildInputs = [ pkgs.utillinux ];
|
|
|
|
|
QEMU_OPTS = "-nographic -serial stdio -monitor none"
|
|
|
|
|
+ lib.optionalString cfg.useEFIBoot
|
|
|
|
|
(" -drive if=pflash,format=raw,unit=0,readonly=on,file=${efiFirmware}"
|
|
|
|
|
+ " -drive if=pflash,format=raw,unit=1,file=$efiVars");
|
|
|
|
|
} ''
|
|
|
|
|
# Create a /boot EFI partition with 60M and arbitrary but fixed GUIDs for reproducibility
|
|
|
|
|
${pkgs.gptfdisk}/bin/sgdisk \
|
|
|
|
|
--set-alignment=1 --new=1:34:2047 --change-name=1:BIOSBootPartition --typecode=1:ef02 \
|
|
|
|
|
--set-alignment=512 --largest-new=2 --change-name=2:EFISystem --typecode=2:ef00 \
|
|
|
|
|
--attributes=1:set:1 \
|
|
|
|
|
--attributes=2:set:2 \
|
|
|
|
|
--disk-guid=97FD5997-D90B-4AA3-8D16-C1723AEA73C1 \
|
|
|
|
|
--partition-guid=1:1C06F03B-704E-4657-B9CD-681A087A2FDC \
|
|
|
|
|
--partition-guid=2:970C694F-AFD0-4B99-B750-CDB7A329AB6F \
|
|
|
|
|
--hybrid 2 \
|
|
|
|
|
--recompute-chs /dev/vda
|
|
|
|
|
|
|
|
|
|
${optionalString (config.boot.loader.grub.device != "/dev/vda")
|
|
|
|
|
# In this throwaway VM, we only have the /dev/vda disk, but the
|
|
|
|
|
# actual VM described by `config` (used by `switch-to-configuration`
|
|
|
|
|
# below) may set `boot.loader.grub.device` to a different device
|
|
|
|
|
# that's nonexistent in the throwaway VM.
|
|
|
|
|
# Create a symlink for that device, so that the `grub-install`
|
|
|
|
|
# by `switch-to-configuration` will hit /dev/vda anyway.
|
|
|
|
|
''
|
|
|
|
|
ln -s /dev/vda ${config.boot.loader.grub.device}
|
|
|
|
|
''}
|
|
|
|
|
|
|
|
|
|
${pkgs.dosfstools}/bin/mkfs.fat -F16 /dev/vda2
|
|
|
|
|
export MTOOLS_SKIP_CHECK=1
|
|
|
|
|
${pkgs.mtools}/bin/mlabel -i /dev/vda2 ::boot
|
|
|
|
|
|
|
|
|
|
mkdir /boot
|
|
|
|
|
mount /dev/vda2 /boot
|
|
|
|
|
|
|
|
|
|
${optionalString config.boot.loader.efi.canTouchEfiVariables ''
|
|
|
|
|
mount -t efivarfs efivarfs /sys/firmware/efi/efivars
|
|
|
|
|
''}
|
|
|
|
|
|
|
|
|
|
# This is needed for GRUB 0.97, which doesn't know about virtio devices.
|
|
|
|
|
mkdir /boot/grub
|
|
|
|
|
echo '(hd0) /dev/vda' > /boot/grub/device.map
|
|
|
|
|
|
|
|
|
|
# This is needed for systemd-boot to find ESP, and udev is not available here to create this
|
|
|
|
|
mkdir -p /dev/block
|
|
|
|
|
ln -s /dev/vda2 /dev/block/254:2
|
|
|
|
|
|
|
|
|
|
# Install bootloader
|
|
|
|
|
touch /etc/NIXOS
|
|
|
|
|
export NIXOS_INSTALL_BOOTLOADER=1
|
|
|
|
|
${config.system.build.toplevel}/bin/switch-to-configuration boot
|
|
|
|
|
|
|
|
|
|
umount /boot
|
|
|
|
|
'' # */
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
in {
|
|
|
|
|
options = {
|
|
|
|
|
|
|
|
|
@ -250,7 +73,8 @@ in {
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
virtualisation.diskImage = mkOption {
|
|
|
|
|
default = "./${config.system.name}.qcow2";
|
|
|
|
|
type = with types; nullOr path;
|
|
|
|
|
default = null;
|
|
|
|
|
description = ''
|
|
|
|
|
Path to the disk image containing the root filesystem.
|
|
|
|
|
The image will be created on startup if it does not
|
|
|
|
@ -258,14 +82,6 @@ in {
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
virtualisation.bootDevice = mkOption {
|
|
|
|
|
type = types.str;
|
|
|
|
|
example = "/dev/vda";
|
|
|
|
|
description = ''
|
|
|
|
|
The disk to be used for the root filesystem.
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
virtualisation.emptyDiskImages = mkOption {
|
|
|
|
|
default = [ ];
|
|
|
|
|
type = types.listOf types.int;
|
|
|
|
@ -384,26 +200,28 @@ in {
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
drives = mkOption {
|
|
|
|
|
type = types.listOf (types.submodule driveOpts);
|
|
|
|
|
description = "Drives passed to qemu.";
|
|
|
|
|
apply = addDeviceNames;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
diskInterface = mkOption {
|
|
|
|
|
default = "virtio";
|
|
|
|
|
example = "scsi";
|
|
|
|
|
type = types.enum [ "virtio" "scsi" "ide" ];
|
|
|
|
|
default = "ahci";
|
|
|
|
|
example = "usb";
|
|
|
|
|
type = types.enum [ "ahci" "usb" "virtio" ];
|
|
|
|
|
description = "The interface used for the virtual hard disks.";
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
guestAgent.enable = mkOption {
|
|
|
|
|
default = true;
|
|
|
|
|
type = types.bool;
|
|
|
|
|
description = ''
|
|
|
|
|
Enable the Qemu guest agent.
|
|
|
|
|
'';
|
|
|
|
|
kernel = mkOption {
|
|
|
|
|
type = types.path;
|
|
|
|
|
description = "Guest kernel.";
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
initrd = mkOption {
|
|
|
|
|
type = types.path;
|
|
|
|
|
description = "Guest initrd.";
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
cmdline = mkOption {
|
|
|
|
|
type = types.str;
|
|
|
|
|
description = "Command line options to pass to guest.";
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
virtualisation.useBootLoader = mkOption {
|
|
|
|
@ -449,73 +267,8 @@ in {
|
|
|
|
|
|
|
|
|
|
config = {
|
|
|
|
|
|
|
|
|
|
# Note [Disk layout with `useBootLoader`]
|
|
|
|
|
#
|
|
|
|
|
# If `useBootLoader = true`, we configure 2 drives:
|
|
|
|
|
# `/dev/?da` for the root disk, and `/dev/?db` for the boot disk
|
|
|
|
|
# which has the `/boot` partition and the boot loader.
|
|
|
|
|
# Concretely:
|
|
|
|
|
#
|
|
|
|
|
# * The second drive's image `disk.img` is created in `bootDisk = ...`
|
|
|
|
|
# using a throwaway VM. Note that there the disk is always `/dev/vda`,
|
|
|
|
|
# even though in the final VM it will be at `/dev/*b`.
|
|
|
|
|
# * The disks are attached in `virtualisation.qemu.drives`.
|
|
|
|
|
# Their order makes them appear as devices `a`, `b`, etc.
|
|
|
|
|
# * `fileSystems."/boot"` is adjusted to be on device `b`.
|
|
|
|
|
|
|
|
|
|
# If `useBootLoader`, GRUB goes to the second disk, see
|
|
|
|
|
# note [Disk layout with `useBootLoader`].
|
|
|
|
|
boot.loader.grub.device = mkVMOverride (if cfg.useBootLoader then
|
|
|
|
|
driveDeviceName 2 # second disk
|
|
|
|
|
else
|
|
|
|
|
cfg.bootDevice);
|
|
|
|
|
|
|
|
|
|
boot.initrd.extraUtilsCommands = ''
|
|
|
|
|
# We need mke2fs in the initrd.
|
|
|
|
|
copy_bin_and_libs ${pkgs.e2fsprogs}/bin/mke2fs
|
|
|
|
|
'';
|
|
|
|
|
|
|
|
|
|
boot.initrd.postDeviceCommands = ''
|
|
|
|
|
# If the disk image appears to be empty, run mke2fs to
|
|
|
|
|
# initialise.
|
|
|
|
|
FSTYPE=$(blkid -o value -s TYPE ${cfg.bootDevice} || true)
|
|
|
|
|
if test -z "$FSTYPE"; then
|
|
|
|
|
mke2fs -t ext4 ${cfg.bootDevice}
|
|
|
|
|
fi
|
|
|
|
|
'';
|
|
|
|
|
|
|
|
|
|
boot.initrd.postMountCommands = ''
|
|
|
|
|
# Mark this as a NixOS machine.
|
|
|
|
|
mkdir -p $targetRoot/etc
|
|
|
|
|
echo -n > $targetRoot/etc/NIXOS
|
|
|
|
|
|
|
|
|
|
# Fix the permissions on /tmp.
|
|
|
|
|
chmod 1777 $targetRoot/tmp
|
|
|
|
|
|
|
|
|
|
mkdir -p $targetRoot/boot
|
|
|
|
|
|
|
|
|
|
${optionalString cfg.writableStore ''
|
|
|
|
|
echo "mounting overlay filesystem on /nix/store..."
|
|
|
|
|
mkdir -p 0755 $targetRoot/nix/.rw-store/store $targetRoot/nix/.rw-store/work $targetRoot/nix/store
|
|
|
|
|
mount -t overlay overlay $targetRoot/nix/store \
|
|
|
|
|
-o lowerdir=$targetRoot/nix/.ro-store,upperdir=$targetRoot/nix/.rw-store/store,workdir=$targetRoot/nix/.rw-store/work || fail
|
|
|
|
|
''}
|
|
|
|
|
'';
|
|
|
|
|
|
|
|
|
|
virtualisation.bootDevice = mkDefault (driveDeviceName 1);
|
|
|
|
|
|
|
|
|
|
# FIXME: Consolidate this one day.
|
|
|
|
|
virtualisation.qemu.options = mkMerge [
|
|
|
|
|
(mkIf (pkgs.stdenv.isi686 || pkgs.stdenv.isx86_64) [
|
|
|
|
|
"-usb"
|
|
|
|
|
"-device usb-tablet,bus=usb-bus.0"
|
|
|
|
|
])
|
|
|
|
|
(mkIf (pkgs.stdenv.isAarch32 || pkgs.stdenv.isAarch64) [
|
|
|
|
|
"-device virtio-gpu-pci"
|
|
|
|
|
"-device usb-ehci,id=usb0"
|
|
|
|
|
"-device usb-kbd"
|
|
|
|
|
"-device usb-tablet"
|
|
|
|
|
])
|
|
|
|
|
(mkIf cfg.useEFIBoot [
|
|
|
|
|
"-drive if=pflash,format=raw,unit=0,readonly,file=${efiFirmware}"
|
|
|
|
|
"-drive if=pflash,format=raw,unit=1,file=$NIX_EFI_VARS"
|
|
|
|
@ -524,29 +277,6 @@ in {
|
|
|
|
|
(mkIf (!cfg.graphics) [ "-nographic" ])
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
virtualisation.qemu.drives = mkMerge [
|
|
|
|
|
[{
|
|
|
|
|
name = "root";
|
|
|
|
|
file = "$NIX_DISK_IMAGE";
|
|
|
|
|
driveExtraOpts.cache = "writeback";
|
|
|
|
|
driveExtraOpts.werror = "report";
|
|
|
|
|
}]
|
|
|
|
|
(mkIf cfg.useBootLoader [
|
|
|
|
|
# The order of this list determines the device names, see
|
|
|
|
|
# note [Disk layout with `useBootLoader`].
|
|
|
|
|
{
|
|
|
|
|
name = "boot";
|
|
|
|
|
file = "$TMPDIR/disk.img";
|
|
|
|
|
driveExtraOpts.media = "disk";
|
|
|
|
|
deviceExtraOpts.bootindex = "1";
|
|
|
|
|
}
|
|
|
|
|
])
|
|
|
|
|
(imap0 (idx: _: {
|
|
|
|
|
file = "$(pwd)/empty${toString idx}.qcow2";
|
|
|
|
|
driveExtraOpts.werror = "report";
|
|
|
|
|
}) cfg.emptyDiskImages)
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
system.build.vm = pkgs.runCommand "nixos-vm" { preferLocalBuild = true; } ''
|
|
|
|
|
mkdir -p $out/bin
|
|
|
|
|
ln -s ${
|
|
|
|
|