TECHNICAL FIELD

Embodiments of this disclosure relate to the field of virtualization technologies, and in particular, to an image file conversion method and apparatus.

BACKGROUND

When virtual machines are migrated in batches between two different platforms, for example, when the virtual machines are migrated from an original platform to a target platform, a virtual machine that has been deployed on the original platform needs to be exported as an image file, and then, the image file is imported to the target platform, so that the imported image file is used to create virtual machines in batches on the target platform. An image file of a virtual machine is a template for creating a virtual machine.

Usually, an original platform and a target platform use different drivers, and/or the original platform and the target platform support different file formats. Therefore, an original image file exported from the original platform cannot be normally used on the target platform. In the prior art, an original image file needs to be started as an original virtual machine first. Then, a driver of the original virtual machine is uninstalled manually, a driver of a target platform is written into the original virtual machine, and the original virtual machine is exported as the original image file in a file format of an original platform. Then, a software engineer converts the file format of the original image file into a target file format by using an image file format conversion tool, uses the original image file in the target file format to create a virtual machine on the target platform, and installs the target driver that has been written into the virtual machine, so as to obtain a target virtual machine on the target platform. Further, the target virtual machine is exported as a target image file, and the target image file is used to create target virtual machines in batches on the target platform.

When an original image file is converted into a target image file, the original image file needs to be started as a virtual machine. Therefore, resource consumption is relatively high, and conversion efficiency is relatively low.

SUMMARY

To resolve problems of relatively high resource consumption and relatively low image file conversion efficiency that are caused by that an original image file needs to be started as a virtual machine when the original image file is converted, embodiments of the present invention provide an image file conversion method and apparatus. The technical solutions are as follows:

According to a first aspect, an image file conversion method is provided, where the method includes: obtaining an original image file of a virtual machine on an original platform; detecting whether the original image file has a first file characteristic and a second file characteristic that are corresponding to a target platform, where the first file characteristic includes a target driver used when the virtual machine runs on the target platform, and the second file characteristic includes a target file format supported by the target platform; and changing, when the original image file does not have at least one of the first file characteristic or the second file characteristic, the original image file by calling a predetermined interface, to obtain a target image file of the virtual machine on the target platform, where the target image file has the first file characteristic and the second file characteristic.

In this implementation, when a conversion server detects that the original image file does not have at least one file characteristic corresponding to the target platform, it indicates that the original image file cannot be directly used on the target platform. In this case, the conversion server directly changes the original image file by calling the predetermined interface, without starting the original image file as a virtual machine before the change, and then, can obtain the target image file of the virtual machine on the target platform by means of conversion, so that the virtual machine runs normally on the target platform. Therefore, resources are saved and image file conversion efficiency is improved.

In a first possible implementation of the first aspect, the changing, when the original image file does not have at least one of the first file characteristic or the second file characteristic, the original image file by calling a predetermined interface includes: when the original image file does not have the first file characteristic, replacing an original driver in the original image file with the target driver by calling a first predetermined interface.

With reference to the first possible implementation of the first aspect, in a second possible implementation of the first aspect, the replacing an original driver in the original image file with the target driver by calling a first predetermined interface includes: determining a driver directory in the original image file according to an operating system type of an operating system in the original image file, where the driver directory is a default directory in which a driver of an operating system of the operating system type is located; and replacing the original driver in the driver directory with the target driver by calling the first predetermined interface.

In this optional implementation, when detecting that the original driver in the original image file is not the target driver used when the virtual machine runs on the target platform, the conversion server automatically changes the original driver in the original image file according to the target driver, without a need to manually change the original driver by a specialized engineer, so that image file conversion efficiency is improved, and a chance of an error caused in a manual operation process is also reduced.

With reference to any one of the first aspect, the first possible implementation of the first aspect, or the second possible implementation of the first aspect, in a third possible implementation of the first aspect, the detecting whether the original image file has a first file characteristic corresponding to a target platform includes: accessing the original image file by calling the first predetermined interface, to determine the operating system type of the operating system in the original image file; determining the target driver corresponding to both the operating system type and the target platform; detecting whether the original driver is the same as the target driver; and when the original driver is different from the target driver, determining that the original image file does not have the first file characteristic.

In this optional implementation, the conversion server automatically recognizes the operating system type of the operating system in the original image file, without a need to manually enter the operating system type by a user, so that an image file conversion error caused when the user manually enters an erroneous operating system type is avoided.

With reference to the first aspect, in a fourth possible implementation of the first aspect, the changing, when the original image file does not have at least one of the first file characteristic or the second file characteristic, the original image file by calling a predetermined interface includes: when the original image file does not have the second file characteristic, changing an original file format of the original image file to the target file format by calling a second predetermined interface.

In this optional implementation, when detecting that the original file format of the original image file is not the target file format supported by the target platform, the conversion server automatically changes the original file format of the original image file according to the target file format, without a need to manually change the original file format by a specialized engineer, so that image file conversion efficiency is improved, and a chance of an error caused in a manual operation process is also reduced.

With reference to the fourth possible implementation of the first aspect, in a fifth possible implementation of the first aspect, the detecting whether the original image file has a second file characteristic corresponding to a target platform includes: determining the target file format supported by the target platform; detecting whether the original file format is the same as the target file format; and when the original file format is different from the target file format, determining that the original image file does not have the second file characteristic.

With reference to the first aspect, in a sixth possible implementation of the first aspect, the obtaining an original image file of a virtual machine on an original platform includes: obtaining the original image file from an image repository, where the image repository is configured to store an image file uploaded by a user.

According to a second aspect, an image file conversion apparatus is provided, where the apparatus includes at least one unit, and the at least one unit in the apparatus is configured to implement the image file conversion method provided in the first aspect.

According to a third aspect, a conversion server is provided, where the conversion server includes a processor and a memory connected to the processor. The memory is configured to store one or more instructions, the instructions are indicated to be executed by the processor, and the processor is configured to implement the image file conversion method provided in the first aspect.

