2020-06-14 17:06:03 +08:00
|
|
|
{ pkgs
|
2020-06-20 19:52:20 +08:00
|
|
|
, diskImageSize ? "70G"
|
2020-06-11 16:22:35 +08:00
|
|
|
, windowsImage ? null
|
|
|
|
, autoUnattendParams ? {}
|
|
|
|
, impureMode ? false
|
|
|
|
, installCommands ? []
|
|
|
|
, users ? {}
|
|
|
|
, ...
|
|
|
|
}@attrs:
|
|
|
|
|
|
|
|
let
|
2020-06-14 17:06:03 +08:00
|
|
|
lib = pkgs.lib;
|
2020-06-15 00:28:31 +08:00
|
|
|
utils = import ./utils.nix { inherit pkgs; };
|
2020-06-11 16:22:35 +08:00
|
|
|
libguestfs = pkgs.libguestfs-with-appliance;
|
|
|
|
|
|
|
|
# p7zip on >20.03 has known vulns but we have no better option
|
|
|
|
p7zip = pkgs.p7zip.overrideAttrs(old: {
|
|
|
|
meta = old.meta // {
|
|
|
|
knownVulnerabilities = [];
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
runQemuCommand = name: command: (
|
2020-06-15 00:28:31 +08:00
|
|
|
pkgs.runCommandNoCC name { buildInputs = [ p7zip utils.qemu libguestfs ]; }
|
2020-06-11 16:22:35 +08:00
|
|
|
(
|
|
|
|
''
|
|
|
|
if ! test -f; then
|
|
|
|
echo "KVM not available, bailing out" >> /dev/stderr
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
'' + command
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
windowsIso = if windowsImage != null then windowsImage else pkgs.fetchurl {
|
|
|
|
url = "https://software-download.microsoft.com/download/sg/17763.107.101029-1455.rs5_release_svc_refresh_CLIENT_LTSC_EVAL_x64FRE_en-us.iso";
|
|
|
|
sha256 = "668fe1af70c2f7416328aee3a0bb066b12dc6bbd2576f40f812b95741e18bc3a";
|
|
|
|
};
|
|
|
|
|
|
|
|
openSshServerPackage = ./openssh/server-package.cab;
|
|
|
|
|
|
|
|
autounattend = import ./autounattend.nix (
|
|
|
|
attrs // {
|
|
|
|
inherit pkgs;
|
2020-06-15 00:28:31 +08:00
|
|
|
users = users // {
|
|
|
|
wfvm = {
|
|
|
|
password = "1234";
|
|
|
|
description = "WFVM Administrator";
|
|
|
|
groups = [
|
|
|
|
"Administrators"
|
|
|
|
];
|
|
|
|
};
|
|
|
|
};
|
2020-06-11 16:22:35 +08:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
bundleInstaller = pkgs.callPackage ./bundle {};
|
|
|
|
|
|
|
|
# Packages required to drive installation of other packages
|
2020-06-14 16:50:25 +08:00
|
|
|
bootstrapPkgs =
|
2020-06-11 16:22:35 +08:00
|
|
|
runQemuCommand "bootstrap-win-pkgs.img" ''
|
2020-06-14 19:26:43 +08:00
|
|
|
mkdir -p pkgs/fod
|
2020-06-11 16:22:35 +08:00
|
|
|
|
|
|
|
cp ${bundleInstaller} pkgs/"$(stripHash "${bundleInstaller}")"
|
|
|
|
|
|
|
|
# Install optional windows features
|
|
|
|
cp ${openSshServerPackage} pkgs/fod/OpenSSH-Server-Package~31bf3856ad364e35~amd64~~.cab
|
|
|
|
|
|
|
|
# SSH setup script goes here because windows XML parser sucks
|
|
|
|
cp ${autounattend.setupScript} pkgs/ssh-setup.ps1
|
|
|
|
|
|
|
|
virt-make-fs --partition --type=fat pkgs/ $out
|
|
|
|
'';
|
|
|
|
|
|
|
|
installScript = pkgs.writeScript "windows-install-script" (
|
|
|
|
let
|
2020-06-15 13:27:01 +08:00
|
|
|
qemuParams = utils.mkQemuFlags (lib.optional (!impureMode) "-display none" ++ [
|
2020-06-11 16:22:35 +08:00
|
|
|
# "CD" drive with bootstrap pkgs
|
|
|
|
"-drive"
|
|
|
|
"id=virtio-win,file=${bootstrapPkgs},if=none,format=raw,readonly=on"
|
|
|
|
"-device"
|
|
|
|
"usb-storage,drive=virtio-win"
|
|
|
|
# USB boot
|
|
|
|
"-drive"
|
|
|
|
"id=win-install,file=usbimage.img,if=none,format=raw,readonly=on"
|
|
|
|
"-device"
|
|
|
|
"usb-storage,drive=win-install"
|
|
|
|
# Output image
|
|
|
|
"-drive"
|
|
|
|
"file=c.img,index=0,media=disk,cache=unsafe"
|
2020-06-14 19:26:43 +08:00
|
|
|
# Network
|
|
|
|
"-netdev user,id=n1,net=192.168.1.0/24,restrict=on"
|
2020-06-15 00:28:31 +08:00
|
|
|
]);
|
2020-06-11 16:22:35 +08:00
|
|
|
in
|
|
|
|
''
|
|
|
|
#!${pkgs.runtimeShell}
|
|
|
|
set -euxo pipefail
|
2020-06-15 00:28:31 +08:00
|
|
|
export PATH=${lib.makeBinPath [ p7zip utils.qemu libguestfs ]}:$PATH
|
2020-06-11 16:22:35 +08:00
|
|
|
|
|
|
|
# Create a bootable "USB" image
|
|
|
|
# Booting in USB mode circumvents the "press any key to boot from cdrom" prompt
|
|
|
|
#
|
|
|
|
# Also embed the autounattend answer file in this image
|
|
|
|
mkdir -p win
|
|
|
|
mkdir -p win/nix-win
|
|
|
|
7z x -y ${windowsIso} -owin
|
|
|
|
|
|
|
|
cp ${autounattend.autounattendXML} win/autounattend.xml
|
|
|
|
|
|
|
|
virt-make-fs --partition --type=fat win/ usbimage.img
|
|
|
|
rm -rf win
|
|
|
|
|
|
|
|
# Qemu requires files to be rw
|
|
|
|
qemu-img create -f qcow2 c.img ${diskImageSize}
|
2020-06-15 00:28:31 +08:00
|
|
|
qemu-system-x86_64 ${lib.concatStringsSep " " qemuParams}
|
2020-06-11 16:22:35 +08:00
|
|
|
''
|
|
|
|
);
|
|
|
|
|
|
|
|
baseImage = pkgs.runCommandNoCC "windows.img" {} ''
|
|
|
|
${installScript}
|
|
|
|
mv c.img $out
|
|
|
|
'';
|
|
|
|
|
|
|
|
finalImage = builtins.foldl' (acc: v: pkgs.runCommandNoCC "${v.name}.img" {
|
2020-06-15 00:28:31 +08:00
|
|
|
buildInputs = with utils; [
|
|
|
|
qemu win-wait win-exec win-put
|
2020-06-13 18:15:38 +08:00
|
|
|
] ++ (v.buildInputs or []);
|
2020-06-11 16:22:35 +08:00
|
|
|
} (let
|
|
|
|
script = pkgs.writeScript "${v.name}-script" v.script;
|
2020-06-15 13:27:01 +08:00
|
|
|
qemuParams = utils.mkQemuFlags (lib.optional (!impureMode) "-display none" ++ [
|
2020-06-11 16:22:35 +08:00
|
|
|
# Output image
|
|
|
|
"-drive"
|
|
|
|
"file=c.img,index=0,media=disk,cache=unsafe"
|
2020-06-14 19:26:43 +08:00
|
|
|
# Network - enable SSH forwarding
|
|
|
|
"-netdev user,id=n1,net=192.168.1.0/24,restrict=on,hostfwd=tcp::2022-:22"
|
2020-06-15 00:28:31 +08:00
|
|
|
]);
|
2020-06-11 16:22:35 +08:00
|
|
|
|
|
|
|
in ''
|
|
|
|
# Create an image referencing the previous image in the chain
|
|
|
|
qemu-img create -f qcow2 -b ${acc} c.img
|
|
|
|
|
2020-06-14 12:23:02 +08:00
|
|
|
set -m
|
2020-06-11 16:22:35 +08:00
|
|
|
qemu-system-x86_64 ${lib.concatStringsSep " " qemuParams} &
|
|
|
|
|
2020-06-15 00:28:31 +08:00
|
|
|
win-wait
|
2020-06-11 16:22:35 +08:00
|
|
|
|
2020-06-15 00:28:31 +08:00
|
|
|
echo "Executing script to build layer..."
|
2020-06-11 16:22:35 +08:00
|
|
|
${script}
|
2020-06-15 00:28:31 +08:00
|
|
|
echo "Layer script done"
|
2020-06-11 16:22:35 +08:00
|
|
|
|
2020-06-14 12:23:02 +08:00
|
|
|
echo "Shutting down..."
|
2020-06-13 18:49:55 +08:00
|
|
|
win-exec 'shutdown /s'
|
2020-06-14 12:23:02 +08:00
|
|
|
echo "Waiting for VM to terminate..."
|
|
|
|
fg
|
|
|
|
echo "Done"
|
2020-06-11 16:22:35 +08:00
|
|
|
|
|
|
|
mv c.img $out
|
|
|
|
'')) baseImage installCommands;
|
|
|
|
|
|
|
|
in
|
|
|
|
|
|
|
|
# impureMode is meant for debugging the base image, not the full incremental build process
|
|
|
|
if !(impureMode) then finalImage else assert installCommands == []; installScript
|