A method for handling virtual machine MAC address conflicts within a PVE platform
By scanning virtual machine configuration files within the PVE platform and monitoring events using hook functions, automatically detecting and generating unique MAC addresses, conflict problems when importing external mirrors are solved, and stability and efficiency are improved.
Patent Information
- Application Number
- CN202411779855.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-05
- Publication Date
- 2025-08-01
- Estimated Expiration
- 2044-12-05
AI Technical Summary
In the PVE platform, when importing external virtual machine images, MAC address conflicts are prone to occur, resulting in unstable network communication and difficult troubleshooting. The existing technology lacks effective automation solutions.
Design a method for handling conflict between virtual machines in PVE platform. By scanning virtual machine configuration files, using hook functions to monitor events, automatically detect and generate unique MAC addresses, replace conflict addresses, and record change logs.
Automate the resolution of MAC address conflicts, improve network communication stability, reduce troubleshooting time and cost, and improve the performance and availability of virtualized environments.
Smart Images

Figure CN119697162B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of virtual machines, and more particularly, to a method for handling MAC address conflicts of virtual machines within a Proxmox VE (PVE) platform. Background Art
[0002] Proxmox Virtual Environment, abbreviated as PVE, is an open-source virtualization platform based on KVM (Kernel-based Virtual Machine) and LXC (Linux Containers) technologies, supporting the operation of virtual machines and containers. In today's information technology field, the PVE platform is widely used in virtualization environments to improve resource utilization and management efficiency. However, when creating virtual machines in the PVE platform, there is a potential problem, namely, MAC address conflicts of virtual machines.
[0003] In a practical production environment, the deployment and management of virtual machines often involve complex and delicate operation processes. Especially in a multi-cluster architecture, resource sharing and reuse become an important means to improve efficiency. A common practice is to export pre-configured virtual machine images from other clusters as original templates, and then import these images into the target PVE platform through specific commands or tools to quickly create multiple virtual machine instances, so as to meet the purpose of rapid deployment and business expansion requirements.
[0004] However, behind this efficient deployment, there is a potential problem: MAC address conflicts. The PVE platform usually has a complete set of virtual machine network configuration management strategies, including automatically assigning unique MAC addresses to newly created virtual machines to ensure smooth network communication. This mechanism works well for virtual machines created within the cluster because these virtual machines have followed the cluster's MAC address allocation rules since their creation. However, when it comes to virtual machine images imported from the outside, the situation becomes complicated. Since these images are often configured in other environments, their MAC addresses may have been allocated and used in the original environment. During the import process, if the PVE platform does not verify or modify the MAC addresses of these imported virtual machines and directly uses the original MAC addresses in the images, it may lead to MAC address conflicts within the same PVE platform. MAC address conflicts not only affect the stability of network communication but may also cause abnormal communication between virtual machines and even affect the network environment of the entire cluster.
[0005] Seriously, due to the possible lack of sufficient awareness and vigilance of operators regarding MAC address conflict issues, and coupled with the fact that import operations often involve batch processing requirements, the risk of MAC address conflicts is further amplified. In a fast-paced production environment, operators are easily distracted from carefully checking the MAC addresses of virtual machines, thus leaving potential hazards.
[0006] Once a MAC address conflict occurs, it will lead to chaos and failures in network communication. Applications on virtual machines will also experience various failures, such as connection interruptions, data transmission errors, etc. More troublesome is that it is usually very difficult to locate such failures, which requires a large amount of time and effort.
[0007] Therefore, solving the MAC address conflict problem of virtual machines in the PVE cluster has important practical significance. It can not only improve the stability and reliability of network communication, but also reduce the time and cost of troubleshooting, and enhance the performance and availability of the entire virtualized environment.
[0008] In the prior art, in the PVE platform, the system usually provides an automatic and unique MAC address allocation mechanism for virtual machines created internally, which can effectively prevent address conflicts. However, for virtual machine images imported from the outside, the cluster currently lacks a dedicated MAC address conflict handling process. This leads to the need for operators to manually check the network card configuration files of each imported virtual machine to ensure that its MAC address does not conflict with other devices in the cluster. But manually checking the network card configuration files of each imported virtual machine has limitations. It is both cumbersome and error-prone, especially in a large-scale cluster environment where the efficiency is extremely low.
[0009] However, currently, there is no relevant solution in the market. Summary of the Invention
[0010] In view of the above technical problems in the related art, the present invention proposes a method for handling MAC address conflicts of virtual machines in a PVE platform, which can overcome the above-mentioned deficiencies existing in the prior art.
[0011] To achieve the above technical objectives, the technical solution of the present invention is implemented as follows:
[0012] A method for handling MAC address conflicts of virtual machines in a PVE platform includes the following steps:
[0013] S1 Existing address scanning and warehousing: Scan the configuration files of the generated virtual machines stored in the specified directory of the PVE host, extract information such as the MAC address and virtual machine IP in the configuration files, and store the extracted information in the database of the PVE host, where the database stores the recorded MAC addresses;
[0014] S2 Event Monitoring: For the case where the MAC address created or restored for a specified external image file already exists in the database, write a corresponding hook function to monitor events such as the creation or restoration of virtual machines using the image file; when the hook function detects an event, call the kernel of the PVE platform to interrupt the creation or restoration operation of the virtual machine and trigger the MAC address conflict detection program;
[0015] S3 Conflict Detection: The MAC address conflict detection program reads the MAC address information in the image file, accesses the database, and compares the MAC address in the image file with the recorded MAC addresses;
[0016] S4 Conflict Handling: If a MAC address conflict is found, proceed to S5; otherwise, continue to execute and complete the interrupted virtual machine creation or restoration operation, and jump to S8; [[ID=⑧]]
[0017] S5 Generate Unique MAC Address: Call the MAC address generator to generate a globally unique MAC address;
[0018] S6 Compare and Replace MAC Address: Compare the newly generated MAC address with the MAC address in the virtual machine to be created or restored; if they are not repeated, automatically replace it - that is, replace the MAC address in the virtual machine to be created or restored with the newly generated MAC address, and continue to S7; otherwise, it means that the MAC address is repeated and needs to be regenerated, so return to S5;
[0019] S7 Resume Interrupted Operation: After the MAC address replacement is completed, continue to execute the interrupted virtual machine creation or restoration operation to complete the creation or restoration of the virtual machine;
[0020] S8 Record Change Log: Record the MAC address change process for easy tracking and auditing.
[0021] Preferably, in S1, write an initialization scan script to scan the configuration files in the / etc / pve / qemu - server directory of the PVE host, read the MAC addresses and virtual machine - related information of all existing virtual machines, and store them in the virtual machine MAC address correspondence table stored in the database.
[0022] Preferably, in S2, the hook function is a special message - handling mechanism that can be used to monitor various message events in the system or process, intercept messages sent to the target window, and process them.
[0023] Preferably, in S2, the timing monitoring of the creation or restoration operation of the virtual machine is completed by implementing the customized hook function. When it is detected that the network resource allocation operation is executed, information such as the MAC address is captured, and the MAC address conflict detection program is called through the hook linked list to perform the conflict detection of the MAC address.
[0024] Preferably, in S5, the MAC address generator is generated by python. The uuid.uuid4() method in python can be used to randomly generate a 128-bit identifier, and then the integer part of this value is hashed by SHA-256 to obtain a 256-bit hash value. Data is extracted from the first 12 characters of the hash value, and every two characters represent one byte (a total of 6 bytes), which meets the length requirement of the MAC address. The 6 extracted bytes are separated by colons to form the standard MAC address format.
[0025] Preferably, the database is preferably mysql.
[0026] Preferably, in S5, the MAC address generator algorithm is generated based on the hash function to ensure randomness, thereby ensuring compliance with the MAC address rules.
[0027] Preferably, in S6, the comparison method is as follows: the MAC address conflict detection program loads the MAC addresses in the database and the content in the virtual machine correspondence table into the memory, and traverses in the memory to compare the captured MAC address with the MAC address information in the database one by one;
[0028] If a conflicting MAC address is detected, the MAC address generator is directly called to generate a unique MAC address.
[0029] Preferably, in S6, the specific steps for automatic replacement include:
[0030] S6.1: Replace the MAC address in the configuration file of the operated image with the generated MAC address. Before the replacement operation, duplicate verification of the MAC address is required;
[0031] S6.2: First, compare with the MAC address virtual machine correspondence table in the database. If it is repeated, the MAC address generator is called again to generate;
[0032] S6.3: Then, compare with the MAC address in the original image file. If it is repeated, the MAC address generator is called again to generate;
[0033] S6.4: Finally, if there is no repetition, the MAC address replacement is executed; otherwise, return to S5.
[0034] Preferably, in S8, during the MAC address change process, a log record is automatically generated.
[0035] Preferably, in S8, the log record includes information such as the change time, the original MAC address, the new MAC address, the operation type, and the virtual machine ID.
[0036] Preferably, the creation of a virtual machine refers to the PVE platform using the mirror file to create a new virtual machine.
[0037] Preferably, the restoration of a virtual machine refers to the PVE platform using the mirror file to restore the mirror file to its initial state before being modified, especially restoring the MAC address of the mirror file to the value before modification.
[0038] Advantages of the present disclosure: When the present disclosure designates an external mirror file and imports it into PVE as a mirror template, and creates or copies multiple virtual machines through commands, the MAC address of the virtual machine itself will not change, resulting in the situation where the MAC addresses of multiple virtual machines are the same.
[0039] The present disclosure also designs a MAC address scanning and detection mechanism in the PVE host server. When it detects that the MAC address of the virtual machine being created conflicts with the MAC address of an existing virtual machine in PVE, a unique MAC address is randomly generated and used to replace the MAC address in the virtual machine being created, thus resolving the risk of MAC address conflicts.
[0040] Through a unique technical solution, the present disclosure can not only automatically detect MAC address conflicts and automatically replace them, avoiding the possibility of conflicts caused by creating or restoring virtual machines using an external mirror as the source, but also reduce network storms caused by MAC address conflicts, improving the stability and reliability of network communication.
[0041] When importing a virtual machine mirror into the PVE platform, the MAC address of the virtual machine will be consistent with the virtual machine mirror address and will not be automatically modified. Through the detection mechanism designed by the present disclosure, after the virtual machine mirror is imported into the PVE platform, when creating or restoring a virtual machine using this mirror as the source mirror, it will automatically detect whether the MAC address of the virtual machine conflicts with the MAC addresses of other virtual machines in the PVE platform. If there is a conflict, a unique MAC address will be automatically generated for replacement. BRIEF DESCRIPTION OF THE DRAWINGS
[0042] To more clearly illustrate the technical solutions in the embodiments of the present invention or the prior art, the following will briefly introduce the drawings required in the embodiments. Obviously, the drawings in the following description are only some embodiments of the present invention. For those of ordinary skill in the art, without creative efforts, other drawings can be obtained based on these drawings.
[0043] Figure 1 It is a logic flowchart of a method for handling virtual machine MAC address conflicts in the PVE platform described in this disclosure. Specific implementation manners
[0044] The following will clearly and completely describe the technical solutions in the embodiments of the present invention with reference to the drawings in the embodiments of the present invention.
[0045] As Figure 1 shown, for the convenience of understanding the above technical solutions of the present invention, the following will detail the above technical solutions of the present invention in terms of specific usage methods.
[0046] A method for handling virtual machine MAC address conflicts in a PVE platform includes the following steps:
[0047] S1 Existing address scanning and warehousing: Scan the configuration files of the generated virtual machines stored in the specified directory of the PVE host, extract information such as the MAC address and virtual machine IP in the configuration files, and store the extracted information in the database of the PVE host. The database stores the recorded MAC addresses.
[0048] S2 Event monitoring setting: For the situation where the MAC address created or restored for a specified external image file already exists in the database, write a corresponding hook function to monitor events such as the creation or restoration of virtual machines using the image file. When the hook function detects the occurrence of an event, call the kernel of the PVE platform to interrupt the creation or restoration operation of the virtual machine and trigger the MAC address conflict detection program.
[0049] S3 Conflict detection: The MAC address conflict detection program reads the MAC address information in the image file, accesses the database, and compares the MAC address in the image file with the recorded MAC addresses.
[0050] S4 Conflict handling: If a MAC address conflict is found, proceed to S5; otherwise, continue to execute and complete the interrupted virtual machine creation or restoration operation, and jump to S8.
[0051] S5 Generate a unique MAC address: Call the MAC address generator to generate a globally unique MAC address.
[0052] S6 Compare and Replace MAC Address: Compare the newly generated MAC address with the MAC address in the virtual machine to be created or restored; if there is no duplication, automatically perform replacement - that is, replace the MAC address in the virtual machine to be created or restored with the newly generated MAC address, and continue with S7; otherwise, it indicates that a MAC address duplication has occurred and a new generation is required, so return to S5;
[0053] S7 Resume Interrupted Operation: After the MAC address replacement is completed, continue to execute the interrupted virtual machine creation or restoration operation to complete the creation or restoration of the virtual machine;
[0054] S8 Record Change Log: Record the MAC address change process for easy tracking and auditing.
[0055] In a certain embodiment, in S1, write an initialization scan script to scan the configuration files in the / etc / pve / qemu - server directory of the PVE host, read the MAC addresses and virtual machine - related information of all existing virtual machines, and store them in the virtual machine MAC address correspondence table stored in the database.
[0056] In a certain embodiment, in S2, the hook function is a special message - handling mechanism that can be used to monitor various message events in the system or process, intercept messages sent to the target window, and process them.
[0057] In a certain embodiment, in S2, complete the timing monitoring of the virtual machine creation or restoration operation by implementing the custom hook function. When it is detected that a network resource allocation operation is executed, capture information such as the MAC address, and point to the MAC address conflict detection program through the hook linked list to perform the MAC address conflict detection.
[0058] In a certain embodiment, in S5, the MAC address generator is generated by python. The uuid.uuid4() method in python can be used to randomly generate a 128 - bit identifier, and then the integer part of this value is hashed by SHA - 256 to obtain a 256 - bit hash value. Extract data from the first 12 characters of the hash value. Every two characters represent one byte (a total of 6 bytes), which meets the length requirement of the MAC address. Separate the extracted 6 bytes with colons to form the standard MAC address format.
[0059] In a certain embodiment, the database is preferably mysql.
[0060] In a certain embodiment, in S5, the MAC address generator algorithm is generated based on a hash function to ensure randomness, thus ensuring compliance with the MAC address rules.
[0061] In a certain embodiment, in S6, the comparison method is as follows: the MAC address conflict detection program loads the MAC addresses in the database and the content in the virtual machine correspondence table into the memory, and in the memory, traverses to compare the captured MAC address with the MAC address information in the database one by one;
[0062] If a conflicting MAC address is detected, directly call the MAC address generator to generate a unique MAC address.
[0063] In a certain embodiment, in S6, the specific steps for automatic replacement include:
[0064] S6.1: Replace the MAC address in the configuration file of the operated image with the generated MAC address. Before the replacement operation, duplicate verification of the MAC address is required;
[0065] S6.2: First, compare with the MAC address virtual machine correspondence table in the database. If it is repeated, call the MAC address generator again to generate;
[0066] S6.3: Then, compare with the MAC address in the original image file. If it is repeated, call the MAC address generator again to generate;
[0067] S6.4: Finally, if there is no repetition, perform the MAC address replacement; otherwise, return to S5.
[0068] In a certain embodiment, in S8, during the MAC address change process, a log record is automatically generated.
[0069] In a certain embodiment, in S8, the log record includes information such as the change time, original MAC address, new MAC address, operation type, virtual machine ID, etc.
[0070] In a certain embodiment, the creation of a virtual machine means that the PVE platform uses the image file to create a new virtual machine;
[0071] In a certain embodiment, the restoration of a virtual machine means that the PVE platform uses the image file to restore the image file to its initial state before being modified, especially restoring the MAC address of the image file to the value before modification.
[0072] Operating principles of each operation: (A). Scanning and storing the MAC address of the virtual machine into the database - A python initialization scanning script can be written to scan the configuration file information in the / etc / pve / qemu-server directory of the PVE host, read the MAC addresses and virtual machine-related information of all existing virtual machines, and store them in the virtual machine MAC address correspondence table in a database such as mysql.
[0073] (B). Create a MAC address monitoring and parsing tool: It is possible to call or rewrite the hook function in the PVE system. By means of the hook function, monitor the events occurring to the virtual machine. For example, when it is monitored that a virtual machine creation or restoration event based on an existing image file is executed through the command line and it proceeds to the network resource allocation phase, interrupt the process, parse the MAC address information from the corresponding configuration file, and at the same time trigger the call to the MAC address conflict detection program to perform conflict detection.
[0074] (C). Judge the uniqueness of the MAC address: The MAC address conflict detection program can load the MAC addresses in the Mysql database table and the content in the virtual machine corresponding table into the memory, and perform a comparison with the captured MAC address in the memory. Compare the captured MAC address with the MAC address information in the table one by one in a traversal manner.
[0075] (D). After detecting a conflicting MAC address, directly call the MAC address generator to generate a unique MAC address, replace the MAC address in the network resource configuration file in the original image, resume the interruption, and continue the original virtual machine creation or restoration operation.
[0076] (E). Call the MAC address generator: The MAC address generator can be generated through python. Use the uuid.uuid4() method in python to randomly generate a 128-bit identifier, and then perform a SHA-256 hash on the integer part of this value to obtain a 256-bit hash value. Extract data from the first 12 characters of the hash value. Every two characters represent one byte (a total of 6 bytes), which meets the length requirement of the MAC address. Separate the 6 extracted bytes with colons to form the standard MAC address format.
[0077] (F). Automatic replacement of the MAC address: It is possible to replace the MAC address in the configuration file of the image being operated on with the generated MAC address. Before the replacement operation, it is necessary to perform a duplicate check on the MAC address. First, compare it with the MAC address virtual machine corresponding table in the database. If it is a duplicate, call the MAC address generator again to generate. Then compare it with the MAC address in the original image file. If it is a duplicate, call the MAC address generator again to generate. If there is no duplicate, perform the MAC address replacement.
[0078] (G). After the MAC address replacement is completed, continue to execute the virtual machine creation or restoration operation to complete the creation or restoration of the virtual machine.
[0079] In summary, through the above unique design, the present disclosure designates an external mirror file and imports it into PVE as a mirror template. When creating or copying multiple virtual machines in batches through commands, the MAC address of the virtual machine itself will not change, resulting in the situation where multiple virtual machines have the same MAC address. The present disclosure also designs a MAC address scanning mechanism in the PVE host server. When it detects that the MAC address of the virtual machine being created conflicts with the MAC address of the virtual machine already existing in PVE, it randomly generates a unique MAC address and replaces the MAC address in the virtual machine being created to resolve the risk of MAC address conflicts.
[0080] The foregoing is only a preferred embodiment of the present invention and is not intended to limit the present invention. Any modifications, equivalent replacements, improvements, etc. made within the spirit and principles of the present invention shall be included within the protection scope of the present invention.
Claims
1. A method for handling virtual machine MAC address conflicts within a PVE platform, characterized in that, It includes the following steps: S1 Existing address scanning and warehousing: Scan the configuration files of the virtual machines that have been generated and stored in the specified directory of the PVE host, extract the MAC addresses and virtual machine IPs in the configuration files, and store the extracted information in the database of the PVE host. The database stores the recorded MAC addresses; S2 Setting event monitoring: For the case where the MAC addresses created or restored for the specified external image file already exist in the database, write the corresponding hook function to monitor the occurrence of events for the creation or restoration of virtual machines using the image file; when the hook function detects the occurrence of an event, call the kernel of the PVE platform to interrupt the creation or restoration operation of the virtual machine and trigger the MAC address conflict detection program; S3 Conflict detection: The MAC address conflict detection program reads the MAC address information in the image file, accesses the database, and compares the MAC address in the image file with the recorded MAC address; S4 Conflict handling: If a MAC address conflict is found, proceed to S5; otherwise, continue to execute and complete the interrupted virtual machine creation or restoration operation, and jump to S8; S5 Generating a unique MAC address: Call the MAC address generator to generate a globally unique MAC address; S6 Comparing and replacing the MAC address: Compare the newly generated MAC address with the MAC address in the virtual machine to be created or restored; if they are not repeated, automatically replace it - that is, replace the MAC address in the virtual machine to be created or restored with the newly generated MAC address, and continue to S7; Otherwise, it means that the MAC address is repeated and needs to be regenerated, so return to S5; S7 Restoring the interrupted operation: After the MAC address replacement is completed, continue to execute the interrupted virtual machine creation or restoration operation to complete the creation or restoration of the virtual machine; S8 Recording the change log: Record the MAC address change process for easy tracking and auditing.
2. The processing method according to claim 1, characterized in that, In S1, write an initialization scanning script to scan the configuration files in the / etc / pve / qemu-server directory of the PVE host, read the MAC addresses and virtual machine-related information of all existing virtual machines, and store them in the virtual machine MAC address correspondence table stored in the database.
3. The processing method according to claim 1, characterized in that, In S2, the hook function is a special message processing mechanism used to monitor various message events in the system or process, intercept the messages sent to the target window, and process them.
4. The processing method according to claim 1, characterized in that, In S2, complete the timing monitoring of the creation or restoration operation of the virtual machine by implementing the custom hook function. When the network resource allocation operation is detected to be executed, capture the MAC address, and point to the call of the MAC address conflict detection program through the hook linked list to perform MAC address conflict detection.
5. The processing method according to claim 1, characterized in that, In S5, the MAC address generator is generated by python; The database is mysql.
6. The processing method according to claim 1, characterized in that, In S5, the MAC address generator algorithm is generated using a hash function as the base value to ensure randomness, thus ensuring compliance with the MAC address rules.
7. The processing method according to claim 1, characterized in that In S6, the comparison method is as follows: the MAC address conflict detection program loads the MAC addresses in the database and the content in the virtual machine correspondence table into the memory, and in the memory, the captured MAC address is compared one by one with the MAC address information in the database through a traversal method; If a conflicting MAC address is detected, the MAC address generator is directly called to generate a unique MAC address.
8. The processing method according to claim 1, characterized in that, In S6, the specific steps for automatic replacement include: S6.1: Replace the MAC address in the configuration file of the operated image with the generated MAC address. Before the replacement operation, duplicate verification of the MAC address is required; S6.2: First, compare with the MAC address virtual machine correspondence table in the database. If it is a duplicate, call the MAC address generator again to generate; S6.3: Then, compare with the MAC address in the original image file. If it is a duplicate, call the MAC address generator again to generate; S6.4: Finally, if there is no duplication, perform the MAC address replacement; otherwise, return to S5.
9. The processing method according to claim 1, characterized in that In S8, during the MAC address change process, a log record is automatically generated; The log record includes the change time, the original MAC address, the new MAC address, the operation type, and the virtual machine ID.
10. The processing method according to claim 1, characterized in that, The creation of a virtual machine refers to the PVE platform using the image file to create a new virtual machine; The restoration of a virtual machine refers to the PVE platform using the image file to restore the image file to its initial state before it was modified, especially restoring the MAC address of the image file to the value before modification.
Citation Information
Patent Citations
Process protection method based on operating system drive
CN114462021A
Host machine, virtualization instance introspection method and storage medium
CN116991543A