Chuyển tới nội dung chính

Attach và detach volume

POST/v2.1/{project_id}/servers/{server_id}/os-volume_attachments
GET/v2.1/{project_id}/servers/{server_id}/os-volume_attachments
DELETE/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ộcpath

UUID của project.

server_idstringbắt buộcpath

UUID của VM.

volumeAttachmentobjectbắt buộcbody

Object bọc attachment chứa device và volumeId.

4 trường
volumeIdstringbắt buộcbody

UUID của volume cần attach. Volume phải ở trạng thái available.

devicestringbắt buộcbody

Tê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ộcbody

Tag 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ộcbody

Xoá 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áiMô tả
200 - OKVolume đã được attach.
400 - Bad RequestVolume không ở trạng thái available, đã được attach, hoặc request sai định dạng.
401 - UnauthorizedUser phải xác thực trước khi gửi request.
403 - ForbiddenPolicy không cho phép user hiện tại thực hiện thao tác này.
404 - Not FoundVM hoặc volume không tồn tại.
409 - ConflictVM đ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áiMô tả
202 - AcceptedYêu cầu detach đã được chấp nhận và đang được xử lý.
401 - UnauthorizedUser phải xác thực trước khi gửi request.
403 - ForbiddenPolicy không cho phép user hiện tại thực hiện thao tác này.
404 - Not FoundVM hoặc attachment không tồn tại.
409 - ConflictVM đang ở trạng thái không cho phép detach.
note

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.