From 0fd72034d928f477c322daa65ad25dc3f37f6b9c Mon Sep 17 00:00:00 2001 From: Ben Oukhanov Date: Thu, 11 Jan 2024 17:05:35 +0200 Subject: [PATCH] feat: prepare disk image Prepare disk image by running virt-sysprep before uploading it to container registry. Jira-Url: https://issues.redhat.com/browse/CNV-34270 Signed-off-by: Ben Oukhanov --- Dockerfile | 2 +- README.md | 2 +- examples/kubevirt-disk-uploader-tekton.yaml | 11 +++++++++++ kubevirt-disk-uploader.yaml | 2 +- run-uploader.sh | 20 ++++++++++++++++++++ 5 files changed, 34 insertions(+), 3 deletions(-) diff --git a/Dockerfile b/Dockerfile index 627bc02..9d4281b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -13,7 +13,7 @@ RUN cd /usr/bin && \ chmod +x virtctl && \ curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl" && \ chmod +x kubectl && \ - microdnf install -y gzip qemu-img && \ + microdnf install -y gzip qemu-img libguestfs-tools-c && \ microdnf clean all -y COPY --from=builder /app/kubevirt-disk-uploader /usr/local/bin/kubevirt-disk-uploader COPY run-uploader.sh /usr/local/bin/run-uploader.sh diff --git a/README.md b/README.md index 970c5c7..d2bf124 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ KubeVirt Disk Uploader -> Download VM Disk -> Build New Container Disk -> Push T **Prerequisites** 1. Ensure Virtual Machine (VM) is powered off. Data from VM can be exported only when it is not used. -2. Modify [kubevirt-disk-uploader](https://github.com/codingben/kubevirt-disk-uploader/blob/main/kubevirt-disk-uploader.yaml#L58) arguments (VM Name, New Container Disk Name, and Disk File). +2. Modify [kubevirt-disk-uploader](https://github.com/codingben/kubevirt-disk-uploader/blob/main/kubevirt-disk-uploader.yaml#L58) arguments (VM Name, New Container Disk Name, Disk File, and Enable or Disable System Preparation). 3. Modify [kubevirt-disk-uploader-credentials](https://github.com/codingben/kubevirt-disk-uploader/blob/main/kubevirt-disk-uploader.yaml#L65-L74) of the external container registry (Username, Password and Hostname). Deploy `kubevirt-disk-uploader` within the same namespace as the Virtual Machine (VM): diff --git a/examples/kubevirt-disk-uploader-tekton.yaml b/examples/kubevirt-disk-uploader-tekton.yaml index 7832d13..c83b2b6 100644 --- a/examples/kubevirt-disk-uploader-tekton.yaml +++ b/examples/kubevirt-disk-uploader-tekton.yaml @@ -163,6 +163,9 @@ spec: - name: DISK_IMAGE description: The name of the disk image type: string + - name: ENABLE_VIRT_SYSPREP + description: Enable or disable preparation of disk image + type: string steps: - name: kubevirt-disk-uploader-step image: quay.io/boukhano/kubevirt-disk-uploader:latest @@ -187,6 +190,7 @@ spec: - $(params.VM_NAME) - $(params.EXPORTED_IMAGE) - $(params.DISK_IMAGE) + - $(params.ENABLE_VIRT_SYSPREP) resources: requests: memory: "3Gi" @@ -208,6 +212,9 @@ spec: - name: DISK_IMAGE description: "Name of the disk image" type: string + - name: ENABLE_VIRT_SYSPREP + description: "Enable or disable preparation of disk image" + type: string tasks: - name: deploy-example-vm taskRef: @@ -224,6 +231,8 @@ spec: value: "$(params.EXPORTED_IMAGE)" - name: DISK_IMAGE value: "$(params.DISK_IMAGE)" + - name: ENABLE_VIRT_SYSPREP + value: "$(params.ENABLE_VIRT_SYSPREP)" - name: deploy-example-vm-exported taskRef: name: example-vm-exported-task @@ -244,4 +253,6 @@ spec: value: example-vm-tekton-exported:latest - name: DISK_IMAGE value: disk.img.gz + - name: ENABLE_VIRT_SYSPREP + value: true serviceAccountName: kubevirt-disk-uploader-tekton diff --git a/kubevirt-disk-uploader.yaml b/kubevirt-disk-uploader.yaml index a16d194..097e7e5 100644 --- a/kubevirt-disk-uploader.yaml +++ b/kubevirt-disk-uploader.yaml @@ -55,7 +55,7 @@ spec: name: kubevirt-disk-uploader-credentials key: registryHostname command: ["/usr/local/bin/run-uploader.sh"] - # args: ["", "", ""] + # args: ["", "", "", ""] resources: requests: memory: 3Gi diff --git a/run-uploader.sh b/run-uploader.sh index 29220a7..e04af21 100755 --- a/run-uploader.sh +++ b/run-uploader.sh @@ -4,6 +4,7 @@ VM_NAME=$1 CONTAINER_DISK_NAME=$2 DISK_FILE=$3 +ENABLE_VIRT_SYSPREP=$4 # Variables OUTPUT_PATH=./tmp @@ -24,6 +25,11 @@ validate_arguments() { echo "Disk file to extract is missing. Please provide a valid disk file." exit 1 fi + + if [ -z "$ENABLE_VIRT_SYSPREP" ]; then + echo "ENABLE_VIRT_SYSPREP is missing or empty. Please provide a valid value (true or false)." + exit 1 + fi } apply_vmexport() { @@ -69,6 +75,19 @@ convert_disk_img() { fi } +prep_disk_img() { + if [ "$ENABLE_VIRT_SYSPREP" = "true" ]; then + echo "Preparing disk image..." + + DISK_PATH=$OUTPUT_PATH/disk.qcow2 + export LIBGUESTFS_BACKEND=direct + + virt-sysprep --format qcow2 -a $DISK_PATH + else + echo "Skipping disk image preparation." + fi +} + upload_container_img() { echo "Building and uploading new container image with exported disk image..." @@ -85,6 +104,7 @@ main() { apply_vmexport download_disk_img convert_disk_img + prep_disk_img upload_container_img echo "Succesfully extracted disk image and uploaded it in a new container image to container registry."