diff --git a/engine/storage/configdrive/src/main/java/org/apache/cloudstack/storage/configdrive/ConfigDriveBuilder.java b/engine/storage/configdrive/src/main/java/org/apache/cloudstack/storage/configdrive/ConfigDriveBuilder.java index 58cc341a87b..d57afbb0a23 100644 --- a/engine/storage/configdrive/src/main/java/org/apache/cloudstack/storage/configdrive/ConfigDriveBuilder.java +++ b/engine/storage/configdrive/src/main/java/org/apache/cloudstack/storage/configdrive/ConfigDriveBuilder.java @@ -126,7 +126,26 @@ public class ConfigDriveBuilder { File openStackFolder = new File(tempDirName + ConfigDrive.openStackConfigDriveName); - writeVendorEmptyJsonFile(openStackFolder); + /* + Try to find VM password in the vmData. + If it is found, then write it into vendor-data.json + */ + String vmPassword = ""; + for (String[] item : vmData) { + String dataType = item[CONFIGDATA_DIR]; + String fileName = item[CONFIGDATA_FILE]; + String content = item[CONFIGDATA_CONTENT]; + if (PASSWORD_FILE.equals(fileName)) { + vmPassword = content; + break; + } + } + if (vmPassword.equals("")) { + writeVendorDataJsonFile(openStackFolder); + } else { + writeVendorDataJsonFile(openStackFolder, vmPassword); + } + writeNetworkData(nics, supportedServices, openStackFolder); for (NicProfile nic: nics) { if (supportedServices.get(nic.getId()).contains(Network.Service.UserData)) { @@ -253,7 +272,7 @@ public class ConfigDriveBuilder { * * If the folder does not exist, and we cannot create it, we throw a {@link CloudRuntimeException}. */ - static void writeVendorEmptyJsonFile(File openStackFolder) { + static void writeVendorDataJsonFile(File openStackFolder) { if (openStackFolder.exists() || openStackFolder.mkdirs()) { writeFile(openStackFolder, "vendor_data.json", "{}"); } else { @@ -261,6 +280,26 @@ public class ConfigDriveBuilder { } } + /** + * Writes vendor data containing Cloudstack-generated password into vendor-data.json + * + * If the folder does not exist, and we cannot create it, we throw a {@link CloudRuntimeException}. + */ + static void writeVendorDataJsonFile(File openStackFolder, String password) { + if (openStackFolder.exists() || openStackFolder.mkdirs()) { + writeFile( + openStackFolder, + "vendor_data.json", + String.format( + "{\"cloud-init\": \"#cloud-config\\npassword: %s\\nchpasswd:\\n expire: False\"}", + password + ) + ); + } else { + throw new CloudRuntimeException("Failed to create folder " + openStackFolder); + } + } + /** * Creates the {@link JsonObject} with VM's metadata. The vmData is a list of arrays; we expect this list to have the following entries: *