How to import and export incus image in REST api way

root@x99:~# incus version
Client version: 6.1
Server version: 6.1

I have both unified images (compressed file or directory) and split images(two files).
I already understand how to import or export image in command way(incus image import or incus image export), but i need to import or export image in REST api way.

Section1.REST api way for import unified images.

i don’t know how to specify my local image file as a parameter.

Section2.REST api way for import split images.

Can this api pass two files, one for metadata_tarball_path and another for rootfs_tarball_path?

Section3.REST api way for export unified images.

I successfully called this API(just for unified images).

Section4.REST api way for export split images.

this api response one file named “export”,it’s weird.

Can anybody give me some demo?
I may not be sure how to correctly pass parameters.
Any help will be appreciated.

For a single-file image, it’s reasonably straightforward.

stgraber@dakara:~$ incus create images:alpine/edge a1
Creating a1
stgraber@dakara:~$ incus publish a1
Instance published with fingerprint: 2cf2e8890bd472ebe6f6a821c08fd10a610dfd94ff74681b3c3595fa5b3859a4
stgraber@dakara:~$ curl --unix-socket /var/lib/incus/unix.socket incus/1.0/images/2cf2e8890bd472ebe6f6a821c08fd10a610dfd94ff74681b3c3595fa5b3859a4/export -o image.tar.xz
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 3814k  100 3814k    0     0   596M      0 --:--:-- --:--:-- --:--:--  620M
stgraber@dakara:~$ file image.tar.xz 
image.tar.xz: gzip compressed data, from Unix, original size modulo 2^32 10086400
stgraber@dakara:~$ incus image delete 2cf2
stgraber@dakara:~$ curl --unix-socket /var/lib/incus/unix.socket -X POST -H 'Content-Type: application/octet-stream' --data-binary "@image.tar.xz" incus/1.0/images
{"type":"async","status":"Operation created","status_code":100,"operation":"/1.0/operations/cf74e3b9-02d7-4f4b-bf7a-ef971d506e06","error_code":0,"error":"","metadata":{"id":"cf74e3b9-02d7-4f4b-bf7a-ef971d506e06","class":"task","description":"Downloading image","created_at":"2024-06-27T00:39:47.721623949-04:00","updated_at":"2024-06-27T00:39:47.721623949-04:00","status":"Running","status_code":103,"resources":{},"metadata":null,"may_cancel":false,"err":"","location":"none"}}
stgraber@dakara:~$ incus image list 2cf2
| ALIAS | FINGERPRINT  | PUBLIC |               DESCRIPTION                | ARCHITECTURE |   TYPE    |  SIZE   |     UPLOAD DATE      |
|       | 2cf2e8890bd4 | no     | Alpinelinux edge x86_64 (20240626_13:00) | x86_64       | CONTAINER | 3.73MiB | 2024/06/27 00:39 EDT |

It’s a bit trickier with split images as for those you need to use a multipart http request with the correct filenames. For that case, the content-type becomes multipart/form-data and the two parts (form name) are metadata and rootfs (container) or rootfs.img (VM).

1 Like