Attach và detach volume
/v2.1/{project_id}/servers/{server_id}/os-volume_attachments/v2.1/{project_id}/servers/{server_id}/os-volume_attachments/v2.1/{project_id}/servers/{server_id}/os-volume_attachments/{attachment_id}Endpoint này thuộc service compute và gắn một volume vào VM.
Attachment có ID riêng, không phải volume ID. Để detach, dùng attachment ID trả về từ lệnh attach hoặc lệnh list.
Attach volume
Request
project_idstringbắt buộcpathUUID của project.
server_idstringbắt buộcpathUUID của VM.
volumeAttachmentobjectbắt buộcbodyObject bọc attachment chứa device và volumeId.
4 trường
volumeIdstringbắt buộcbodyUUID của volume cần attach. Volume phải ở trạng thái available.
devicestringbắt buộcbodyTên device tuỳ chọn, ví dụ /dev/vdb. Bỏ trống hoặc đặt null để tự gán. Driver libvirt không dùng tên device do user cung cấp, nên đường dẫn cuối cùng trong guest do driver gán.
tagstringbắt buộcbodyTag vai trò device tuỳ chọn. Guest có thể đọc từ metadata API hoặc config drive. Không hỗ trợ với VM đang shelved-offloaded.
delete_on_terminationbooleanbắt buộcbodyXoá volume khi VM bị huỷ. Mặc định: false.
Request mẫu
curl -ks -H 'Content-Type: application/json' -H 'X-Auth-Token: gAAAAA<...>' \
-d '{
"volumeAttachment": {
"volumeId": "16cd801e-f3c1-4cac-aa6c-aecf22642a89",
"device": "/dev/vdb"
}
}' https://api.vietnix.cloud:8774/v2.1/{project_id}/servers/{server_id}/os-volume_attachments
Response
{
"volumeAttachment": {
"id": "a3d7c1e0-9b1f-4c2e-8a55-7e6d5c4b3a21",
"serverId": "393210ad-152b-4ac3-a0f0-798ee4e93620",
"volumeId": "16cd801e-f3c1-4cac-aa6c-aecf22642a89",
"device": "/dev/vdb",
"tag": null
}
}
| Mã trạng thái | Mô tả |
|---|---|
| 200 - OK | Volume đã được attach. |
| 400 - Bad Request | Volume không ở trạng thái available, đã được attach, hoặc request sai định dạng. |
| 401 - Unauthorized | User phải xác thực trước khi gửi request. |
| 403 - Forbidden | Policy không cho phép user hiện tại thực hiện thao tác này. |
| 404 - Not Found | VM hoặc volume không tồn tại. |
| 409 - Conflict | VM đang ở trạng thái không cho phép attach. |
Liệt kê attachment
curl -ks -H 'Content-Type: application/json' -H 'X-Auth-Token: gAAAAA<...>' \
https://api.vietnix.cloud:8774/v2.1/{project_id}/servers/{server_id}/os-volume_attachments
JSON body
{
"volumeAttachments": [
{
"id": "a3d7c1e0-9b1f-4c2e-8a55-7e6d5c4b3a21",
"serverId": "393210ad-152b-4ac3-a0f0-798ee4e93620",
"volumeId": "16cd801e-f3c1-4cac-aa6c-aecf22642a89",
"device": "/dev/vdb",
"tag": null
}
]
}
Detach volume
curl -ks -X DELETE -H 'Content-Type: application/json' -H 'X-Auth-Token: gAAAAA<...>' \
https://api.vietnix.cloud:8774/v2.1/{project_id}/servers/{server_id}/os-volume_attachments/{attachment_id}
| Mã trạng thái | Mô tả |
|---|---|
| 202 - Accepted | Yêu cầu detach đã được chấp nhận và đang được xử lý. |
| 401 - Unauthorized | User phải xác thực trước khi gửi request. |
| 403 - Forbidden | Policy không cho phép user hiện tại thực hiện thao tác này. |
| 404 - Not Found | VM hoặc attachment không tồn tại. |
| 409 - Conflict | VM đang ở trạng thái không cho phép detach. |
Attach và detach là bất đồng bộ. Poll trạng thái volume qua volume API cho đến khi attachment hoàn tất. Detach không xoá volume; volume trở về available và giữ nguyên dữ liệu.