@@ -36,13 +36,28 @@ let
3636 ${toString config.virtualisation.diskSize}M || exit 1
3737 fi
3838
39− # Create a directory for exchanging data with the VM.
39+ # Create a directory for storing temporary data of the running VM.
4040 if [ -z "$TMPDIR" -o -z "$USE_TMPDIR" ]; then
4141 TMPDIR=$(mktemp -d nix-vm.XXXXXXXXXX --tmpdir)
4242 fi
43− cd $TMPDIR
43+ # Create a directory for exchanging data with the VM.
4444 mkdir -p $TMPDIR/xchg
4545
46+ ${if cfg.useBootLoader then ''
47+ # Create a writable copy/snapshot of the boot disk
48+ # A writable boot disk can be booted from automatically
49+ ${pkgs.qemu_kvm}/bin/qemu-img create -f qcow2 -b ${bootDisk}/disk.img $TMPDIR/disk.img || exit 1
50+
51+ ${if cfg.useEFIBoot then ''
52+ # VM needs a writable flash BIOS
53+ cp ${bootDisk}/bios.bin $TMPDIR || exit 1
54+ chmod 0644 $TMPDIR/bios.bin || exit 1
55+ '' else ''
56+ ''}
57+ '' else ''
58+ ''}
59+
60+ cd $TMPDIR
4661 idx=2
4762 extraDisks=""
4863 ${flip concatMapStrings cfg.emptyDiskImages (size: ''
@@ -52,7 +67,6 @@ let
5267 '')}
5368
5469 # Start QEMU.
55− # "-boot menu=on" is there, because I don't know how to make qemu boot from 2nd hd.
5670 exec ${pkgs.qemu_kvm}/bin/qemu-kvm \
5771 -name ${vmName} \
5872 -m ${toString config.virtualisation.memorySize} \
@@ -63,8 +77,11 @@ let
6377 -virtfs local,path=''${SHARED_DIR:-$TMPDIR/xchg},security_model=none,mount_tag=shared \
6478 ${if cfg.useBootLoader then ''
6579 -drive index=0,id=drive1,file=$NIX_DISK_IMAGE,if=virtio,cache=writeback,werror=report \
66− -drive index=1,id=drive2,file=${bootDisk}/disk.img,if=virtio,readonly \
67− -boot menu=on \
80+ -drive index=1,id=drive2,file=$TMPDIR/disk.img,media=disk \
81+ ${if cfg.useEFIBoot then ''
82+ -pflash $TMPDIR/bios.bin \
83+ '' else ''
84+ ''}
6885 '' else ''
6986 -drive file=$NIX_DISK_IMAGE,if=virtio,cache=writeback,werror=report \
7087 -kernel ${config.system.build.toplevel}/kernel \
@@ -74,7 +91,8 @@ let
7491 $extraDisks \
7592 ${qemuGraphics} \
7693 ${toString config.virtualisation.qemu.options} \
77− $QEMU_OPTS
94+ $QEMU_OPTS \
95+ $@
7896 '';
7997
8098
@@ -98,23 +116,44 @@ let
98116 ''
99117 mkdir $out
100118 diskImage=$out/disk.img
101− ${pkgs.qemu_kvm}/bin/qemu-img create -f qcow2 $diskImage "32M"
119+ bootFlash=$out/bios.bin
120+ ${pkgs.qemu_kvm}/bin/qemu-img create -f qcow2 $diskImage "40M"
121+ ${if cfg.useEFIBoot then ''
122+ cp ${pkgs.OVMF-CSM}/FV/OVMF.fd $bootFlash
123+ chmod 0644 $bootFlash
124+ '' else ''
125+ ''}
102126 '';
103127 buildInputs = [ pkgs.utillinux ];
128+ QEMU_OPTS = if cfg.useEFIBoot
129+ then "-pflash $out/bios.bin -nographic -serial pty"
130+ else "-nographic -serial pty";
104131 }
105132 ''
106− # Create a single /boot partition.
107− ${pkgs.parted}/sbin/parted /dev/vda mklabel msdos
108− ${pkgs.parted}/sbin/parted /dev/vda -- mkpart primary ext2 1M -1s
109− . /sys/class/block/vda1/uevent
110− mknod /dev/vda1 b $MAJOR $MINOR
133+ # Create a /boot EFI partition with 40M
134+ ${pkgs.gptfdisk}/sbin/sgdisk -G /dev/vda
135+ ${pkgs.gptfdisk}/sbin/sgdisk -a 1 -n 1:34:2047 -c 1:"BIOS Boot Partition" -t 1:ef02 /dev/vda
136+ ${pkgs.gptfdisk}/sbin/sgdisk -a 512 -N 2 -c 2:"EFI System" -t 2:ef00 /dev/vda
137+ ${pkgs.gptfdisk}/sbin/sgdisk -A 1:set:1 /dev/vda
138+ ${pkgs.gptfdisk}/sbin/sgdisk -A 2:set:2 /dev/vda
139+ ${pkgs.gptfdisk}/sbin/sgdisk -h 2 /dev/vda
140+ ${pkgs.gptfdisk}/sbin/sgdisk -C /dev/vda
141+ ${pkgs.utillinux}/bin/sfdisk /dev/vda -A 2
142+ . /sys/class/block/vda2/uevent
143+ mknod /dev/vda2 b $MAJOR $MINOR
111144 . /sys/class/block/vda/uevent
112− ${pkgs.e2fsprogs}/sbin/mkfs.ext4 -L boot /dev/vda1
113− ${pkgs.e2fsprogs}/sbin/tune2fs -c 0 -i 0 /dev/vda1
114−
115− # Mount /boot.
145+ ${pkgs.dosfstools}/bin/mkfs.fat -F16 /dev/vda2
146+ export MTOOLS_SKIP_CHECK=1
147+ ${pkgs.mtools}/bin/mlabel -i /dev/vda2 ::boot
148+
149+ # Mount /boot; load necessary modules first.
150+ ${pkgs.module_init_tools}/sbin/insmod ${pkgs.linux}/lib/modules/*/kernel/fs/nls/nls_cp437.ko || true
151+ ${pkgs.module_init_tools}/sbin/insmod ${pkgs.linux}/lib/modules/*/kernel/fs/nls/nls_iso8859-1.ko || true
152+ ${pkgs.module_init_tools}/sbin/insmod ${pkgs.linux}/lib/modules/*/kernel/fs/fat/fat.ko || true
153+ ${pkgs.module_init_tools}/sbin/insmod ${pkgs.linux}/lib/modules/*/kernel/fs/fat/vfat.ko || true
154+ ${pkgs.module_init_tools}/sbin/insmod ${pkgs.linux}/lib/modules/*/kernel/fs/efivarfs/efivarfs.ko || true
116155 mkdir /boot
117− mount /dev/vda1 /boot
156+ mount /dev/vda2 /boot
118157
119158 # This is needed for GRUB 0.97, which doesn't know about virtio devices.
120159 mkdir /boot/grub
@@ -287,6 +326,17 @@ in
287326 '';
288327 };
289328
329+ virtualisation.useEFIBoot =
330+ mkOption {
331+ default = false;
332+ description =
333+ ''
334+ If enabled, the virtual machine will provide a EFI boot
335+ manager.
336+ useEFIBoot is ignored if useBootLoader == false.
337+ '';
338+ };
339+
290340 };
291341
292342 config = {
@@ -379,8 +429,8 @@ in
379429 };
380430 } // optionalAttrs cfg.useBootLoader
381431 { "/boot" =
382− { device = "/dev/disk/by-label/boot";
383− fsType = "ext4";
432+ { device = "/dev/vdb2";
433+ fsType = "vfat";
384434 options = "ro";
385435 noCheck = true; # fsck fails on a r/o filesystem
386436 };
@@ -413,6 +463,7 @@ in
413463
414464 # Wireless won't work in the VM.
415465 networking.wireless.enable = mkVMOverride false;
466+ networking.connman.enable = mkVMOverride false;
416467
417468 # Speed up booting by not waiting for ARP.
418469 networking.dhcpcd.extraConfig = "noarp";