fist commit - images lib in a working condition for debian
This commit is contained in:
commit
ad2092531c
12 changed files with 308 additions and 0 deletions
49
lib/images/commons/customizeImage.nix
Normal file
49
lib/images/commons/customizeImage.nix
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
# wrapper function around virt-customize to create custom OS image from an original OS image
|
||||
{ pkgs, lib, ... }:
|
||||
originalImage: {
|
||||
name,
|
||||
hostname ? "",
|
||||
nameToHostname ? true,
|
||||
diskSize ? "",
|
||||
smp ? 2,
|
||||
memSize ? 1024,
|
||||
install ? [],
|
||||
run ? "",
|
||||
commands ? "",
|
||||
osType ? "linux"
|
||||
}:
|
||||
let
|
||||
originalImageName = lib.strings.removeSuffix "-vmix" (lib.strings.removeSuffix ".qcow2" originalImage.name);
|
||||
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 "";
|
||||
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
|
||||
''
|
||||
5
lib/images/commons/default.nix
Normal file
5
lib/images/commons/default.nix
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
{ pkgs, lib, ... }: {
|
||||
# basic scripts and files used across various OS images
|
||||
customizeImage = (import ./customizeImage.nix) { inherit pkgs lib; };
|
||||
scriptsNFiles = (import ./scripts-n-files.nix) { inherit pkgs lib; };
|
||||
}
|
||||
59
lib/images/commons/scripts-n-files.nix
Normal file
59
lib/images/commons/scripts-n-files.nix
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
# minimal set of scripts and services by various images
|
||||
{ pkgs, lib, ... }: {
|
||||
# bring back simple interface names like eth0 eth1 etc
|
||||
grub-ifnames-0 = pkgs.writeText "grub-ifnames-0" ''
|
||||
GRUB_CMDLINE_LINUX="net.ifnames=0 biosdevname=0 $GRUB_CMDLINE_LINUX"
|
||||
'';
|
||||
|
||||
# no need for CPU microcode updating in VMs
|
||||
grub-disable-microcode = pkgs.writeText "grub-disable-microcode" ''
|
||||
GRUB_CMDLINE_LINUX="dis_ucode_ldr $GRUB_CMDLINE_LINUX"
|
||||
'';
|
||||
|
||||
# dhcp for eth0
|
||||
eth0-dhcp-network = pkgs.writeText "eth0-network" ''
|
||||
[Match]
|
||||
Name=eth0
|
||||
|
||||
[Network]
|
||||
DHCP=yes
|
||||
'';
|
||||
|
||||
# generate ssh host keys before starting sshd
|
||||
ssh-service-override-conf-create = pkgs.writeScript "ssh-override-conf-create.sh" ''
|
||||
mkdir -p /etc/systemd/system/ssh.service.d
|
||||
|
||||
cat > /etc/systemd/system/ssh.service.d/override.conf << EOF
|
||||
[Service]
|
||||
ExecStartPre=
|
||||
ExecStartPre=`which ssh-keygen` -A
|
||||
ExecStartPre=`which sshd` -t
|
||||
|
||||
EOF
|
||||
'';
|
||||
|
||||
# script to grow root partition
|
||||
grow-root-sh = pkgs.writeScript "grow-root-sh" ''
|
||||
#!/bin/bash
|
||||
set -e
|
||||
|
||||
command -v growpart >/dev/null || { >&2 echo "growpart not found. Install package cloud-guest-utils or cloud-utils."; exit 1; }
|
||||
|
||||
ROOTPART=$(findmnt / -o source -n)
|
||||
DISK=''${ROOTPART%[0-9]*}
|
||||
PARTNUM=''${ROOTPART##*[!0-9]}
|
||||
|
||||
# resize and grow if possible
|
||||
growpart "$DISK" "$PARTNUM" && resize2fs "$ROOTPART" || true
|
||||
'';
|
||||
|
||||
# service to grow root partition on boot
|
||||
grow-root-service = pkgs.writeText "grow-root-service" ''
|
||||
[Service]
|
||||
Type = oneshot
|
||||
ExecStart = /usr/local/sbin/grow-root.sh
|
||||
|
||||
[Install]
|
||||
WantedBy = multi-user.target
|
||||
'';
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue