Help Center/ Elastic Cloud Server/ API Reference/ APIs (Recommended)/ Status Management/ Changing an ECS OS (Using an Image with Cloud-Init Installed)
Updated on 2023-05-29 GMT+08:00

Changing an ECS OS (Using an Image with Cloud-Init Installed)

Function

This API is used to change an ECS OS. During the system disk reinstallation using a new image, the data disks of the ECS remain unchanged.

After this API is called, the system uninstalls the system disk, uses the new image to create a system disk, and attaches it to the ECS. In this way, the OS is changed.

Constraints

  • You can only use an image with Cloud-Init or Cloudbase-Init installed. If the image has no Cloudbase-Init or Cloudbase-init installed, use the API described in Changing an ECS OS (Using an Image Without Cloud-Init Installed).
  • Only an ECS with a system disk supports changing OS.
  • You are not allowed to perform other operations when changing the OS. Otherwise, changing the OS will fail.

URI

POST /v2/{project_id}/cloudservers/{server_id}/changeos

Table 1 describes the parameters in the URI.
Table 1 Parameter description

Parameter

Mandatory

Description

project_id

Yes

Specifies the project ID.

For details about how to obtain the ID, see Obtaining a Project ID.

server_id

Yes

Specifies the ECS ID.

Request

Table 2 describes the request parameters.
Table 2 Request parameters

Parameter

Mandatory

Type

Description

os-change

Yes

Object

Changes an ECS OS. For details, see Table 3.

Table 3 os-change field description

Parameter

Mandatory

Type

Description

adminpass

No

String

Specifies the initial password of the ECS administrator.

The Linux administrator username is root.

The password must meet the following requirements:

  • 8 to 26 characters
  • The password must contain at least three of the following character types: uppercase letters, lowercase letters, digits, and special characters (!@$%^-_=+[{}]:,./?~#*).
NOTE:
  • Linux ECSs can use user_data to inject passwords. In such a case, adminpass is unavailable.
  • Specify either adminpass or keyname, not both of them.
  • If both adminpass and keyname are empty, Linux ECSs can use user_data specified in metadata.
  • adminpass, keyname, and the user_data in metadata can be empty only when a private image password is used or when a password is set after the OS is reinstalled. Additionally, the following requirements must be met:

    If you need to set the password after the OS change, ensure that the __os_feature_list field of the image contains {"onekey_resetpasswd": "true"}. Reset the ECS password after the OS change.

  • If you use this field to change the OS of an ECS with Cloud-Init installed, the region in which the ECS is deployed does not support password-authenticated OS changing. In such a case, use key pair authentication.

keyname

No

String

Specifies the key pair name.

Keys can be created using the key creating API (Creating and Importing an SSH Key Pair) or obtained using the SSH key query API (Querying SSH Key Pairs).

userid

No

String

Specifies the user ID. When the keyname parameter is being specified, the value of this parameter is used preferentially. If this parameter is left blank, the user ID in the token is used by default.

imageid

Yes

String

Specifies the ID of the new image in UUID format.

You can obtain the image ID from the console or by following the instructions provided in "Querying Images" in Image Management Service API Reference.

metadata

No

Object

Specifies the metadata of the ECS for which the OS is to be changed.

For more information, see Table 4.

mode

No

String

Specifies whether the ECS supports OS change when the ECS is running.

If the parameter value is withStopServer, the ECS supports this feature. The system automatically stops the ECS and then changes its OS.

Table 4 metadata field description

Parameter

Mandatory

Type

Description

user_data

No

String

Specifies the user data to be injected to the ECS during the creation. Text and text files can be injected.

NOTE:
  • The content of user_data must be encoded with base64.
  • The maximum size of the content to be injected (before encoding) is 32 KB.

For more details, see Injecting User Data into ECSs.

Examples

Before base64 encoding:

  • Linux
    #! /bin/bash
    echo user_test >> /home/user.txt

After base64 encoding:

  • Linux
    IyEgL2Jpbi9iYXNoDQplY2hvIHVzZXJfdGVzdCAmZ3Q7Jmd0OyAvaG9tZS91c2VyLnR4dA==

Response

See Responses (Task).

Example Request

  • Example URL request
    POST https://{endpoint}/v2/{project_id}/cloudservers/{server_id}/changeos
  • Example request 1 (using a password to remotely log in to an ECS with OS changed)
    {
        "os-change": {
            "adminpass": "1qazXSW@", 
            "userid": "7e25b1da389f4697a79df3a0e5bd494e", 
            "imageid": "e215580f-73ad-429d-b6f2-5433947433b0",
            "mode": "withStopServer"
        }
    }
  • Example request 2 (using a key to remotely log in to an ECS with OS changed)
    {
        "os-change": {
            "keyname": "KeyPair-350b", 
            "userid": "7e25b1da389f4697a79df3a0e5bd494e", 
            "imageid": "e215580f-73ad-429d-b6f2-5433947433b0"
        }
    }

Example Response

See Responses (Task).

{      
    "job_id": "70a599e0-31e7-49b7-b260-868f441e862b" 
}

Error Codes

See Error Codes.