From 3f2d66e44450b9b71bfa49d693f04011408774c2 Mon Sep 17 00:00:00 2001 From: Yariv Rachmani Date: Wed, 11 Dec 2024 19:08:27 +0200 Subject: [PATCH] Adding build script for kvm To reduce size and time od contianer quay.io/qm-images/kvm Build, qcow2 images is manipulated on host Signed-off-by: Yariv Rachmani --- subsystems/kvm/ContainerFile | 28 +++++++-------------------- subsystems/kvm/build_kvm_container.sh | 21 ++++++++++++++++++++ 2 files changed, 28 insertions(+), 21 deletions(-) create mode 100644 subsystems/kvm/build_kvm_container.sh diff --git a/subsystems/kvm/ContainerFile b/subsystems/kvm/ContainerFile index 83ec0b04..c142332d 100644 --- a/subsystems/kvm/ContainerFile +++ b/subsystems/kvm/ContainerFile @@ -3,30 +3,16 @@ # How to build # ================== # podman login quay.io -# podman build -t quay.io/qm-images/kvm:latest -f ContainerFile +# use build_kvm_container.sh to build container # podman push quay.io/qm-images/kvm:latest -FROM fedora:latest +FROM fedora-minimal:latest ENV PASSWORD_FEDORA_USER=fedora -RUN dnf -y install virt-install \ - libvirt-daemon \ - libvirt-daemon-qemu \ - libvirt-daemon-kvm \ - libvirt-daemon-config-network \ - guestfs-tools \ - wget \ - vim -y \ - && dnf clean all && rm -rf /var/cache/dnf +RUN dnf install qemu-system-$(arch) -y \ + && dnf clean all && rm -rf /var/cache/dnf -RUN wget -O /var/lib/libvirt/images/Fedora-Cloud-Base-Generic.qcow2 https://cofractal-ewr.mm.fcix.net/fedora/linux/releases/41/Cloud/x86_64/images/Fedora-Cloud-Base-Generic-41-1.4.x86_64.qcow2 +COPY ./Fedora-Cloud-Base-Generic.qcow2 /var/lib/libvirt/images/ -# Set fedora user pass as fedora -RUN export LIBGUESTFS_BACKEND=direct && \ - virt-customize -a /var/lib/libvirt/images/Fedora-Cloud-Base-Generic.qcow2 --password fedora:password:$PASSWORD_FEDORA_USER - -# Permission to qemu user/group -RUN chown qemu:qemu /var/lib/libvirt/images/Fedora-Cloud-Base-Generic.qcow2 - -# Set systemd as the init system for the container -ENTRYPOINT ["/usr/sbin/init"] +# Set container stay alive +ENTRYPOINT ["/usr/bin/qemu-system-x86_64","-smp","12","-enable-kvm","-m","2G","-machine","q35","-cpu","host","-device","virtio-net-pci,netdev=n0,mac=FE:30:26:a6:91:2d","-netdev","user,id=n0,net=10.0.2.0/24,hostfwd=tcp::2226-:22","-drive","file=/var/lib/libvirt/images/Fedora-Cloud-Base-Generic.qcow2,index=0,media=disk,format=qcow2,if=virtio,snapshot=off","-nographic"] diff --git a/subsystems/kvm/build_kvm_container.sh b/subsystems/kvm/build_kvm_container.sh new file mode 100644 index 00000000..ec533d91 --- /dev/null +++ b/subsystems/kvm/build_kvm_container.sh @@ -0,0 +1,21 @@ +#!/usr/bin/bash + +# Install required repos +dnf install guestfs-tools \ + curl \ + perl -y + +# Download fedora cloud image +curl -Lo ./Fedora-Cloud-Base-Generic.qcow2 https://download.fedoraproject.org/pub/fedora/linux/releases/41/Cloud/"$(arch)"/images/Fedora-Cloud-Base-Generic-41-1.4."$(arch)".qcow2 + +# Customize user:pass +export LIBGUESTFS_BACKEND=direct && \ + virt-customize -a ./Fedora-Cloud-Base-Generic.qcow2 \ + --edit '/etc/ssh/sshd_config: s/#PasswordAuthentication.*/PasswordAuthentication yes/' \ + --uninstall cloud-init \ + --firstboot-command "useradd -m -s /bin/bash -G wheel fedora" \ + --firstboot-command "echo 'fedora:fedora' | chpasswd" + +# Container build +podman build -t quay.io/qm-images/kvm:latest -f ContainerFile +