> For the complete documentation index, see [llms.txt](https://docs.console.zenlayer.com/api-reference/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.console.zenlayer.com/api-reference/cn/bmc/bare-metal-instance/reinstallinstance.md).

# ReinstallInstance

## 1. 接口描述

本接口(ReinstallInstance)用于本接口用于重装一个实例。

#### 准备工作

* 查询镜像：调用[`DescribeImages`](/api-reference/cn/bmc/bare-metal-instance/describeimages.md)可以查询到镜像信息。
* 查询密钥对：调用[`DescribeKeyPairs`](https://github.com/zenlayer/zenlayercloud-api-doc-cn/tree/main/security/key-service/key-pair/describekeypairs.md)可以查询到密钥对ID信息。

{% hint style="info" %}
**注意事项**

* 如果指定了`imageId`参数，则使用指定的镜像重装；否则按照实例当前使用的镜像进行重装。
* 在回收站中或者正在安装中的实例不支持该操作。
* 系统盘将会被格式化并重置，请确保系统盘中无重要文件。
* 如果密码不指定将会通过邮箱下发随机密码。
* 重装实例后进入`INSTALLING`(安装中)的状态。如果实例的最新状态变为`RUNNING`(运行中)，则代表操作成功；如果实例的状态为`INSTALL_FAILED`，则代表安装失败。如果出现安装失败，请联系Support进行解决。实例的状态可以通过调用 `DescribeInstances` 接口查询。
  {% endhint %}

## 2. 请求参数

以下请求参数列表仅列出了接口中需要的请求参数

| 参数名称       | 必选 | 类型                                                                     | 描述                                                                                                                                                                              |
| ---------- | -- | ---------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| instanceId | 是  | String                                                                 | <p>待操作的实例ID。</p><p>可通过DescribeInstances接口返回值中的instanceId获取。</p>                                                                                                                 |
| imageId    | 否  | String                                                                 | <p>指定有效的镜像ID。</p><p>可通过以下方式获取可用的镜像ID：通过调用接口 DescribeImages ，传入instanceTypeId获取当前机型支持的镜像列表，取返回信息中的imageId字段；也可以不指定镜像，如果不指定镜像，后续可以通过IPMI进行安装。使用iPXE安装镜像，请指定ipxeUrl字段，且该字段不必传。</p> |
| hostname   | 否  | String                                                                 | <p>实例的主机名。</p><p>不得超过64个字符。仅支持输入字母、数字、-和英文句点(.)。</p><p>默认值为hostname。</p>                                                                                                        |
| password   | 否  | String                                                                 | <p>实例的密码。</p><p>必须是 8-16 个字符，包含大写字母、小写字母、数字和特殊字符。特殊符号可以是：1\~!@$^\*-\_=+。该密码也是作为IPMI登录的密码，请妥善保管。</p><p>密钥与密码必须并且只能指定其中一个。</p>                                                    |
| keyId      | 否  | String                                                                 | <p>密钥ID。与password必须指定其中的一种。</p><p>可调用接口DescribeKeyPairs来获得最新的密钥对信息。</p><p>关联密钥后，就可以通过对应的私钥来访问实例；密钥与密码不能同时指定，同时Windows操作系统不支持指定密钥。</p><p>示例值：key-YWD2QFOl</p>                    |
| raidConfig | 否  | [RaidConfig](/api-reference/cn/bmc/datastructure.md#raidconfig)        | 磁盘阵列配置。                                                                                                                                                                         |
| partitions | 否  | Array of [Partition](/api-reference/cn/bmc/datastructure.md#partition) | <p>分区配置。</p><p>如果未安装操作系统，将不能设置分区。</p>                                                                                                                                           |
| nic        | 否  | [Nic](/api-reference/cn/bmc/datastructure.md#nic)                      | 网卡的配置。                                                                                                                                                                          |
| ipxeUrl    | 否  | String                                                                 | <p>iPXE URL 地址。</p><p>传入参数后，将根据指定URL进行iPXE安装， 如果指定为netboot，将使用netboot iPXE方式进行安装。相关帮助文档：Deploy a Custom Image Using iPXE</p>                                                    |
| userData   | 否  | String                                                                 | <p>用户数据。</p><p>在安装实例时可以通过指定用户数据进行配置实例。当实例首次启动时，用户数据将以文本的方式传递到云服务器中，并执行该文本。支持的最大数据大小为 32KB。</p>                                                                                  |

## 3. 响应结果

| 参数名称      | 类型     | 描述                                                       |
| --------- | ------ | -------------------------------------------------------- |
| requestId | String | <p>唯一请求 ID。</p><p>每次请求都会返回。定位问题时需要提供该次请求的 requestId。</p> |

## 4. 代码示例

{% tabs %}
{% tab title="示例" %}
**1.默认重装指定ID的实例** 用默认值重装ID为`instanceId`的实例。

```json
POST /api/v2/bmc HTTP/1.1
Host: console.zenlayer.com
Content-Type: application/json
X-ZC-Action: ReinstallInstance
Request：
{
    "instanceId": "instanceId1"
}
Response:
{
  "requestId": "TC1748D3E-452D-4F74-8485-7AA73718E453",
  "response": {
    "requestId": "TC1748D3E-452D-4F74-8485-7AA73718E453"
  }
}
```

**2.自定义重装指定ID的实例** 重装ID为`instanceId`的实例，并且指定了镜像为`bd5faa16-39d2-4bbf-a5c8-b95a193b1626`，hostname为`myhostname`，密码为`Aa_12345`，磁盘阵列配置为raid0，并且进行两个分区，公网网卡名称配置为`wan0`，内网网卡名称配置为`lan0`。

```json
POST /api/v2/bmc HTTP/1.1
Host: console.zenlayer.com
Content-Type: application/json
X-TC-Action: ReinstallInstance
Request：
{
    "instanceId": "instanceId",
    "imageId": "bd5faa16-39d2-4bbf-a5c8-b95a193b1626",
    "hostname": "myhostname",
    "password": "Aa_12345",
    "raidConfig": {
        "raidType": 0
    },
    "partitions": [
        {
            "fsPath": "/",
            "fsType": "ext2",
            "size": 1100
        },
        {
            "fsPath": "/a",
            "fsType": "ext2",
            "size": 1100
        }
    ],
    "nic": {
        "wanName": "wan0",
        "lanName": "lan0"
    }
}
Response:
{
  "requestId": "TC1748D3E-452D-4F74-8485-7AA73718E453",
  "response": {
    "requestId": "TC1748D3E-452D-4F74-8485-7AA73718E453"
  }
}
```

{% endtab %}
{% endtabs %}

## 5. 开发者工具

Zenlayer Cloud API 2.0 提供了配套的[开发工具集（SDK）](/api-reference/cn/api-introduction/toolkit.md)，未来会陆续支持更多开发语言，方便快速接入和使用Zenlayer的产品和服务。

## 6. 错误码

下面包含业务逻辑中遇到的错误码，其他错误码见[公共错误码](/api-reference/cn/api-introduction/instruction/commonerrorcode.md)

| HTTP状态码 | 错误码 | 说明 |
| ------- | --- | -- |
