Skip to content

Commit

Permalink
feat: prepare disk image
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
codingben committed Jan 17, 2024
1 parent 5f2c7a1 commit 0fd7203
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
11 changes: 11 additions & 0 deletions examples/kubevirt-disk-uploader-tekton.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -187,6 +190,7 @@ spec:
- $(params.VM_NAME)
- $(params.EXPORTED_IMAGE)
- $(params.DISK_IMAGE)
- $(params.ENABLE_VIRT_SYSPREP)
resources:
requests:
memory: "3Gi"
Expand All @@ -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:
Expand All @@ -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
Expand All @@ -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
2 changes: 1 addition & 1 deletion kubevirt-disk-uploader.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ spec:
name: kubevirt-disk-uploader-credentials
key: registryHostname
command: ["/usr/local/bin/run-uploader.sh"]
# args: ["<VM_NAME>", "<CONTAINER_DISK_NAME>", "<DISK_FILE>"]
# args: ["<VM_NAME>", "<CONTAINER_DISK_NAME>", "<DISK_FILE>", "<ENABLE_VIRT_SYSPREP>"]
resources:
requests:
memory: 3Gi
Expand Down
20 changes: 20 additions & 0 deletions run-uploader.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
VM_NAME=$1
CONTAINER_DISK_NAME=$2
DISK_FILE=$3
ENABLE_VIRT_SYSPREP=$4

# Variables
OUTPUT_PATH=./tmp
Expand All @@ -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() {
Expand Down Expand Up @@ -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..."

Expand All @@ -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."
Expand Down

0 comments on commit 0fd7203

Please sign in to comment.