Skip to content

Commit b5c0b6d

Browse files
committed
qemu: pick hostPackages/buildPackages depending on cpu emulation
Follow-up to Github PR #339. Fixes Github issue #369.
1 parent e422b39 commit b5c0b6d

File tree

3 files changed

+63
-37
lines changed

3 files changed

+63
-37
lines changed

lib/default.nix

Lines changed: 41 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -29,42 +29,48 @@ rec {
2929
lib.drop offset lib.strings.lowerChars
3030
));
3131

32-
createVolumesScript = pkgs: pkgs.lib.concatMapStringsSep "\n" (
33-
{ image
34-
, label
35-
, size ? throw "Specify a size for volume ${image} or use autoCreate = false"
36-
, mkfsExtraArgs
37-
, fsType ? defaultFsType
38-
, autoCreate ? true
39-
, ...
40-
}: pkgs.lib.warnIf
41-
(label != null && !autoCreate) "Volume is not automatically labeled unless autoCreate is true. Volume has to be labeled manually, otherwise it will not be identified"
42-
(let labelOption =
43-
if autoCreate then
44-
(if builtins.elem fsType ["ext2" "ext3" "ext4" "xfs" "btrfs"] then "-L"
45-
else if fsType == "vfat" then "-n"
46-
else (pkgs.lib.warnIf (label != null)
47-
"Will not label volume ${label} with filesystem type ${fsType}. Open an issue on the microvm.nix project to request a fix."
48-
null))
49-
else null;
50-
labelArgument =
51-
if (labelOption != null && label != null) then "${labelOption} '${label}'"
52-
else "";
53-
mkfsExtraArgsString =
54-
if mkfsExtraArgs != null
55-
then lib.escapeShellArgs mkfsExtraArgs
56-
else " ";
57-
in (lib.optionalString autoCreate ''
58-
PATH=$PATH:${with pkgs; lib.makeBinPath [ coreutils util-linux e2fsprogs xfsprogs dosfstools btrfs-progs ]}
32+
createVolumesScript = pkgs: volumes:
33+
lib.optionalString (volumes != []) (
34+
lib.optionalString (lib.any (v: v.autoCreate) volumes) ''
35+
PATH=$PATH:${with pkgs.buildPackages; lib.makeBinPath [ coreutils util-linux e2fsprogs xfsprogs dosfstools btrfs-progs ]}
36+
'' +
37+
pkgs.lib.concatMapStringsSep "\n" (
38+
{ image
39+
, label
40+
, size ? throw "Specify a size for volume ${image} or use autoCreate = false"
41+
, mkfsExtraArgs
42+
, fsType ? defaultFsType
43+
, autoCreate ? true
44+
, ...
45+
}: pkgs.lib.warnIf
46+
(label != null && !autoCreate) "Volume is not automatically labeled unless autoCreate is true. Volume has to be labeled manually, otherwise it will not be identified"
47+
(let labelOption =
48+
if autoCreate then
49+
(if builtins.elem fsType ["ext2" "ext3" "ext4" "xfs" "btrfs"] then "-L"
50+
else if fsType == "vfat" then "-n"
51+
else (pkgs.lib.warnIf (label != null)
52+
"Will not label volume ${label} with filesystem type ${fsType}. Open an issue on the microvm.nix project to request a fix."
53+
null))
54+
else null;
55+
labelArgument =
56+
if (labelOption != null && label != null) then "${labelOption} '${label}'"
57+
else "";
58+
mkfsExtraArgsString =
59+
if mkfsExtraArgs != null
60+
then lib.escapeShellArgs mkfsExtraArgs
61+
else " ";
62+
in (lib.optionalString autoCreate ''
5963
60-
if [ ! -e '${image}' ]; then
61-
touch '${image}'
62-
# Mark NOCOW
63-
chattr +C '${image}' || true
64-
truncate -s ${toString size}M '${image}'
65-
mkfs.${fsType} ${labelArgument} ${mkfsExtraArgsString} '${image}'
66-
fi
67-
'')));
64+
if [ ! -e '${image}' ]; then
65+
touch '${image}'
66+
# Mark NOCOW
67+
chattr +C '${image}' || true
68+
truncate -s ${toString size}M '${image}'
69+
mkfs.${fsType} ${labelArgument} ${mkfsExtraArgsString} '${image}'
70+
fi
71+
''))
72+
) volumes
73+
);
6874

6975
buildRunner = import ./runner.nix;
7076

lib/runner.nix

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,22 @@ let
2626
execArg = lib.optionalString microvmConfig.prettyProcnames
2727
''-a "microvm@${hostName}"'';
2828

29+
vmHostPackages =
30+
if microvmConfig.cpu == null
31+
then
32+
# When cross-compiling for a target host, select packages for
33+
# the target:
34+
pkgs.hostPackages
35+
else
36+
# When cross-compiling for CPU emulation in qemu, select
37+
# packages for the host:
38+
pkgs.buildPackages;
39+
2940
binScripts = microvmConfig.binScripts // {
3041
microvm-run = ''
3142
set -eou pipefail
3243
${preStart}
33-
${createVolumesScript pkgs microvmConfig.volumes}
44+
${createVolumesScript vmHostPackages microvmConfig.volumes}
3445
${lib.optionalString (hypervisorConfig.requiresMacvtapAsFds or false) openMacvtapFds}
3546
3647
exec ${execArg} ${command}

lib/runners/qemu.nix

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,16 @@ let
3636
++ lib.optional microvmConfig.optimize.enable minimizeQemuClosureSize
3737
);
3838

39-
qemu = overrideQemu pkgs.qemu_kvm;
39+
qemuPkg =
40+
if microvmConfig.cpu == null
41+
then
42+
# When cross-compiling for a target host, select qemu for the target:
43+
pkgs.hostPackages.qemu_kvm
44+
else
45+
# When cross-compiling for CPU emulation, select qemu for the host:
46+
pkgs.buildPackages.qemu;
47+
48+
qemu = overrideQemu qemuPkg;
4049

4150
inherit (microvmConfig) hostName vcpu mem balloon initialBalloonMem deflateOnOOM hotplugMem hotpluggedMem user interfaces shares socket forwardPorts devices vsock graphics storeOnDisk kernel initrdPath storeDisk;
4251
inherit (microvmConfig.qemu) machine extraArgs serialConsole;

0 commit comments

Comments
 (0)