Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
Signed-off-by: yaroslavborbat <[email protected]>
  • Loading branch information
yaroslavborbat committed Jan 18, 2025
1 parent af77881 commit 83e1f3c
Showing 1 changed file with 129 additions and 1 deletion.
130 changes: 129 additions & 1 deletion images/virt-artifact/patches/032-hotplug-container-disk.patch
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,106 @@ index c4822a0448..d6bb534249 100644
"dataVolume": {
"description": "DataVolume represents the dynamic creation a PVC for this volume as well as the process of populating that PVC with a disk image.",
"$ref": "#/definitions/v1.DataVolumeSource"
diff --git a/cmd/container-disk-v2alpha/main.c b/cmd/container-disk-v2alpha/main.c
index ba855e574b..24dd3ba336 100644
--- a/cmd/container-disk-v2alpha/main.c
+++ b/cmd/container-disk-v2alpha/main.c
@@ -14,6 +14,7 @@
#include <signal.h>
#include <fcntl.h>

+#include <dirent.h>
#include <time.h>

#define LISTEN_BACKLOG 50
@@ -179,4 +180,86 @@ int main(int argc, char **argv) {
}

socket_check(fd, (void *)copy_path);
+
+ int res = finalize();
+ exit(res);
+}
+
+int finalize() {
+ const char *container_count_str = getenv("CONTAINER_COUNT");
+ const char *end_dir = getenv("END_DIR");
+ const char *container_name = getenv("CONTAINER_NAME");
+
+ if (!container_count_str || !end_dir || !container_name || strlen(container_count_str) == 0 || strlen(end_dir) == 0 || strlen(container_name) == 0) {
+ // Missing or empty environment variables, exit without error
+ return 0;
+ }
+
+ int container_count = atoi(container_count_str);
+ if (container_count <= 0) {
+ error_log("Invalid CONTAINER_COUNT value\n");
+ return 1;
+ }
+
+ bool created = false;
+
+ while (1) {
+ int file_count = count_files_in_directory(end_dir);
+ if (file_count < 0) {
+ return 1;
+ }
+
+ if (file_count >= container_count) {
+ break;
+ }
+
+ if (!created) {
+ int res = create_finalize_file(end_dir, container_name);
+ if (res == 0) {
+ created = true;
+ } else {
+ return 1;
+ }
+ }
+
+ sleep(1); // Pause to prevent excessive file creation
+ }
+ return 0;
+}
+
+int count_files_in_directory(const char *dir_path) {
+ int file_count = 0;
+ struct dirent *entry;
+ DIR *dir = opendir(dir_path);
+
+ if (dir == NULL) {
+ error_log("Failed to open directory %s\n", dir_path);
+ return -1;
+ }
+
+ while ((entry = readdir(dir)) != NULL) {
+ if (entry->d_type == DT_REG) {
+ file_count++;
+ }
+ }
+
+ closedir(dir);
+ return file_count;
}
+
+#define MAX_FILENAME_LEN 256
+
+int create_finalize_file(const char *dir_path, const char *prefix) {
+ time_t current_time = time(NULL);
+ char file_path[MAX_FILENAME_LEN];
+ snprintf(file_path, MAX_FILENAME_LEN, "%s/%s_finalize_%ld.txt", dir_path, prefix, current_time);
+
+ FILE *file = fopen(file_path, "w");
+ if (file) {
+ fclose(file);
+ return 0;
+ }
+
+ error_log("Failed to create file at path %s\n", file_path);
+ return -1;
+}
\ No newline at end of file
diff --git a/cmd/virt-chroot/main.go b/cmd/virt-chroot/main.go
index e28daa07c7..7a69b7451b 100644
--- a/cmd/virt-chroot/main.go
Expand Down Expand Up @@ -422,7 +522,7 @@ index b5d62f5af5..6a3e2b4143 100644

// inject into VMI if ephemeral, else set as a request on the VM to both make permanent and hotplug.
diff --git a/pkg/virt-api/webhooks/validating-webhook/admitters/vmi-update-admitter.go b/pkg/virt-api/webhooks/validating-webhook/admitters/vmi-update-admitter.go
index 0af25f8074..803c0ed4cd 100644
index 0af25f8074..59a468730e 100644
--- a/pkg/virt-api/webhooks/validating-webhook/admitters/vmi-update-admitter.go
+++ b/pkg/virt-api/webhooks/validating-webhook/admitters/vmi-update-admitter.go
@@ -200,11 +200,11 @@ func verifyHotplugVolumes(newHotplugVolumeMap, oldHotplugVolumeMap map[string]v1
Expand All @@ -439,6 +539,34 @@ index 0af25f8074..803c0ed4cd 100644
},
})
}
@@ -219,19 +219,23 @@ func verifyHotplugVolumes(newHotplugVolumeMap, oldHotplugVolumeMap map[string]v1
})
}
disk := newDisks[k]
- if disk.Disk == nil && disk.LUN == nil {
+ if disk.Disk == nil && disk.LUN == nil && disk.CDRom == nil {
return webhookutils.ToAdmissionResponse([]metav1.StatusCause{
{
Type: metav1.CauseTypeFieldValueInvalid,
- Message: fmt.Sprintf("Disk %s requires diskDevice of type 'disk' or 'lun' to be hotplugged.", k),
+ Message: fmt.Sprintf("Disk %s requires diskDevice of type 'disk','lun' or cdrom to be hotplugged.", k),
},
})
}
- if (disk.Disk == nil || disk.Disk.Bus != "scsi") && (disk.LUN == nil || disk.LUN.Bus != "scsi") {
+ if (disk.Disk != nil && disk.Disk.Bus != v1.DiskBusSCSI) || (disk.LUN != nil && disk.LUN.Bus != v1.DiskBusSCSI) || (disk.CDRom != nil && disk.CDRom.Bus != v1.DiskBusSATA) {
+ bus := v1.DiskBusSCSI
+ if disk.CDRom != nil {
+ bus = v1.DiskBusSATA
+ }
return webhookutils.ToAdmissionResponse([]metav1.StatusCause{
{
Type: metav1.CauseTypeFieldValueInvalid,
- Message: fmt.Sprintf("hotplugged Disk %s does not use a scsi bus", k),
+ Message: fmt.Sprintf("hotplugged Disk %s does not use a %q bus", k, bus),
},
})

diff --git a/pkg/virt-api/webhooks/validating-webhook/admitters/vms-admitter.go b/pkg/virt-api/webhooks/validating-webhook/admitters/vms-admitter.go
index f7e4f92727..5869638965 100644
--- a/pkg/virt-api/webhooks/validating-webhook/admitters/vms-admitter.go
Expand Down

0 comments on commit 83e1f3c

Please sign in to comment.