From 5e3c958428b606053a1c29bdb68424e393fe3c07 Mon Sep 17 00:00:00 2001 From: Sagar Ch Date: Sat, 25 May 2024 02:04:28 +0000 Subject: [PATCH] allow calling customizeImage without a name --- lib/images/commons/customizeImage.nix | 56 +++++++++++++++------------ 1 file changed, 31 insertions(+), 25 deletions(-) diff --git a/lib/images/commons/customizeImage.nix b/lib/images/commons/customizeImage.nix index f6b2f41..1d9309b 100644 --- a/lib/images/commons/customizeImage.nix +++ b/lib/images/commons/customizeImage.nix @@ -1,7 +1,7 @@ # wrapper function around virt-customize to create custom OS image from an original OS image { pkgs, lib, ... }: originalImage: { - name, + name ? "", hostname ? "", nameToHostname ? true, diskSize ? "", @@ -14,36 +14,42 @@ }: let originalImageName = lib.strings.removeSuffix "-vmix" (lib.strings.removeSuffix ".qcow2" originalImage.name); + customImageName = (if name != "" then name else "custom") + "-${originalImageName}-vmix.qcow2"; resultImg = "./disk.qcow2"; + qemuWrapperScript = (pkgs.writeShellScript "qemu-wrapper-script" '' export PATH="${pkgs.qemu}/bin:$PATH" exec qemu-kvm -nic user,model=virtio-net-pci "$@" ''); + setHostname = if hostname != "" then hostname else if nameToHostname then name else ""; virtCustomizeArgsHostname = if setHostname != "" then "--hostname '${setHostname}'" else ""; + virtCustomizeArgsInstall = if install != [] then "--install '${lib.strings.concatStringsSep "," install }'" else ""; - virtCustomizeArgsCommandsFile = if commands != "" then ("--commands-from-file " + pkgs.writeText "${name}-vmix-virt-customize-commands-file" commands) else ""; - virtCustomizeArgsRun = if run != "" then ("--run " + pkgs.writeScript "${name}-vmix-virt-customize-run-script" "${run}") else ""; + virtCustomizeArgsCommandsFile = if commands != "" then ("--commands-from-file " + pkgs.writeText "${name}-virt-customize-commands-file" commands) else ""; + virtCustomizeArgsRun = if run != "" then ("--run " + pkgs.writeScript "${name}-virt-customize-run-script" "${run}") else ""; + + builderCommand = '' + export PATH="${pkgs.qemu}/bin:${pkgs.curl}/bin:$PATH" + + # create resulting image backed by original image + qemu-img create -f qcow2 -b ${originalImage} -F qcow2 ${resultImg} + [ -n "${diskSize}" ] && qemu-img resize ${resultImg} ${diskSize} + + # run script inside image using virt-customize + export LIBGUESTFS_APPEND="ipv6.disable=1" + #export LIBGUESTFS_HV="${qemuWrapperScript}" + + ${pkgs.guestfs-tools}/bin/virt-customize \ + -a ${resultImg} \ + --smp ${builtins.toString smp} \ + --memsize ${builtins.toString memSize} \ + ${virtCustomizeArgsHostname} \ + ${virtCustomizeArgsInstall} \ + ${virtCustomizeArgsCommandsFile} \ + ${virtCustomizeArgsRun} + + mv ${resultImg} $out + ''; in - pkgs.runCommand "${name}-${originalImageName}-vmix.qcow2" { __noChroot = true; } '' - export PATH="${pkgs.qemu}/bin:${pkgs.curl}/bin:$PATH" - - # create resulting image backed by original image - qemu-img create -f qcow2 -b ${originalImage} -F qcow2 ${resultImg} - [ -n "${diskSize}" ] && qemu-img resize ${resultImg} ${diskSize} - - # run script inside image using virt-customize - export LIBGUESTFS_APPEND="ipv6.disable=1" - #export LIBGUESTFS_HV="${qemuWrapperScript}" - - ${pkgs.guestfs-tools}/bin/virt-customize \ - -a ${resultImg} \ - --smp ${builtins.toString smp} \ - --memsize ${builtins.toString memSize} \ - ${virtCustomizeArgsHostname} \ - ${virtCustomizeArgsInstall} \ - ${virtCustomizeArgsCommandsFile} \ - ${virtCustomizeArgsRun} - - mv ${resultImg} $out - '' \ No newline at end of file + pkgs.runCommand customImageName { __noChroot = true; } builderCommand \ No newline at end of file