2020-06-29 14:12:10 +03:00
/*
* A Proxmox Backup Server C interface , intended for use inside Qemu
*
* Copyright ( C ) 2019 Proxmox Server Solutions GmbH
*
* Authors :
* Dietmar Maurer ( dietmar @ proxmox . com )
*
* This work is licensed under the terms of the GNU GPL , version 2 or later .
*
*
* NOTE : Async Commands
*
* Most commands are asynchronous ( marked as _async ) . They run in a
* separate thread and have the following parameters :
*
* callback : extern " C " fn ( * mut c_void ) ,
* callback_data : * mut c_void ,
* result : * mut c_int ,
* error : * mut * mut c_char ,
*
2024-06-20 10:55:55 +03:00
* The callback function is called when the async function is ready .
* Possible errors are returned in ' error ' .
2020-06-29 14:12:10 +03:00
*/
# ifndef PROXMOX_BACKUP_QEMU_H
# define PROXMOX_BACKUP_QEMU_H
# include <stdarg.h>
# include <stdbool.h>
# include <stdint.h>
# include <stdlib.h>
# define PROXMOX_BACKUP_DEFAULT_CHUNK_SIZE ((1024 * 1024) * 4)
/**
* Opaque handle for backups jobs
*/
2021-07-05 21:55:24 +03:00
typedef struct ProxmoxBackupHandle {
2020-06-29 14:12:10 +03:00
} ProxmoxBackupHandle ;
/**
* Opaque handle for restore jobs
*/
2021-07-05 21:55:24 +03:00
typedef struct ProxmoxRestoreHandle {
2020-06-29 14:12:10 +03:00
} ProxmoxRestoreHandle ;
/**
2021-07-05 21:55:24 +03:00
* Return a read - only pointer to a string containing the version of the library .
2020-06-29 14:12:10 +03:00
*/
2021-07-05 21:55:24 +03:00
const char * proxmox_backup_qemu_version ( void ) ;
2020-06-29 14:12:10 +03:00
/**
2021-07-05 21:55:24 +03:00
* Free returned error messages
2020-06-29 14:12:10 +03:00
*
2021-07-05 21:55:24 +03:00
* All calls can return error messages , but they are allocated using
* the rust standard library . This call moves ownership back to rust
* and free the allocated memory .
2020-06-29 14:12:10 +03:00
*/
2021-07-05 21:55:24 +03:00
void proxmox_backup_free_error ( char * ptr ) ;
2020-06-29 14:12:10 +03:00
2020-07-06 14:31:31 +03:00
/**
2021-07-05 21:55:24 +03:00
* Returns the text presentation ( relative path ) for a backup snapshot
2020-07-06 14:31:31 +03:00
*
2024-06-20 10:55:55 +03:00
* The returned value is allocated with strdup ( ) , and can be freed
2021-07-05 21:55:24 +03:00
* with free ( ) .
2020-06-29 14:12:10 +03:00
*/
2021-07-05 21:55:24 +03:00
const char * proxmox_backup_snapshot_string ( const char * backup_type ,
const char * backup_id ,
int64_t backup_time ,
char * * error ) ;
2020-06-29 14:12:10 +03:00
/**
2022-04-26 16:59:58 +03:00
* DEPRECATED : Create a new instance in the root namespace .
2020-06-29 14:12:10 +03:00
*
2021-07-05 21:55:24 +03:00
* Uses ` PROXMOX_BACKUP_DEFAULT_CHUNK_SIZE ` if ` chunk_size ` is zero .
2022-04-26 16:59:58 +03:00
*
* Deprecated in favor of ` proxmox_backup_new_ns ` which includes a namespace parameter .
2020-06-29 14:12:10 +03:00
*/
2021-07-05 21:55:24 +03:00
struct ProxmoxBackupHandle * proxmox_backup_new ( const char * repo ,
const char * backup_id ,
uint64_t backup_time ,
uint64_t chunk_size ,
const char * password ,
const char * keyfile ,
const char * key_password ,
const char * master_keyfile ,
bool compress ,
bool encrypt ,
const char * fingerprint ,
char * * error ) ;
2020-06-29 14:12:10 +03:00
2022-04-26 16:59:58 +03:00
/**
* Create a new instance .
*
* ` backup_ns ` may be NULL and defaults to the root namespace .
*
* Uses ` PROXMOX_BACKUP_DEFAULT_CHUNK_SIZE ` if ` chunk_size ` is zero .
*/
struct ProxmoxBackupHandle * proxmox_backup_new_ns ( const char * repo ,
const char * backup_ns ,
const char * backup_id ,
uint64_t backup_time ,
uint64_t chunk_size ,
const char * password ,
const char * keyfile ,
const char * key_password ,
const char * master_keyfile ,
bool compress ,
bool encrypt ,
const char * fingerprint ,
char * * error ) ;
2020-06-29 14:12:10 +03:00
/**
* Open connection to the backup server ( sync )
*
* Returns :
2024-06-20 10:55:55 +03:00
* 0 . . . Success ( no previous backup )
2020-06-29 14:12:10 +03:00
* 1 . . . Success ( found previous backup )
* - 1 . . . Error
*/
2021-07-05 21:55:24 +03:00
int proxmox_backup_connect ( struct ProxmoxBackupHandle * handle , char * * error ) ;
2020-06-29 14:12:10 +03:00
/**
* Open connection to the backup server
*
* Returns :
2024-06-20 10:55:55 +03:00
* 0 . . . Success ( no previous backup )
2020-06-29 14:12:10 +03:00
* 1 . . . Success ( found previous backup )
* - 1 . . . Error
*/
2021-07-05 21:55:24 +03:00
void proxmox_backup_connect_async ( struct ProxmoxBackupHandle * handle ,
2020-06-29 14:12:10 +03:00
void ( * callback ) ( void * ) ,
void * callback_data ,
int * result ,
char * * error ) ;
/**
2021-07-05 21:55:24 +03:00
* Abort a running backup task
2020-06-29 14:12:10 +03:00
*
2021-07-05 21:55:24 +03:00
* This stops the current backup task . It is still necessary to call
* proxmox_backup_disconnect ( ) to close the connection and free
* allocated memory .
2020-06-29 14:12:10 +03:00
*/
2021-07-05 21:55:24 +03:00
void proxmox_backup_abort ( struct ProxmoxBackupHandle * handle , const char * reason ) ;
2020-06-29 14:12:10 +03:00
/**
2021-07-05 21:55:24 +03:00
* Check if we can do incremental backups .
2020-06-29 14:12:10 +03:00
*
2021-07-05 21:55:24 +03:00
* This method compares the csum from last backup manifest with the
* checksum stored locally .
2020-11-24 18:41:21 +03:00
*/
2021-07-05 21:55:24 +03:00
int proxmox_backup_check_incremental ( struct ProxmoxBackupHandle * handle ,
const char * device_name ,
uint64_t size ) ;
2020-11-24 18:41:21 +03:00
2020-06-29 14:12:10 +03:00
/**
* Register a backup image ( sync )
*/
2021-07-05 21:55:24 +03:00
int proxmox_backup_register_image ( struct ProxmoxBackupHandle * handle ,
2020-06-29 14:12:10 +03:00
const char * device_name ,
uint64_t size ,
bool incremental ,
char * * error ) ;
/**
* Register a backup image
*
* Create a new image archive on the backup server
* ( ' < device_name > . img . fidx ' ) . The returned integer is the dev_id
* parameter for the proxmox_backup_write_data_async ( ) method .
*/
2021-07-05 21:55:24 +03:00
void proxmox_backup_register_image_async ( struct ProxmoxBackupHandle * handle ,
2020-06-29 14:12:10 +03:00
const char * device_name ,
uint64_t size ,
bool incremental ,
void ( * callback ) ( void * ) ,
void * callback_data ,
int * result ,
char * * error ) ;
2020-07-06 13:26:31 +03:00
/**
2021-07-05 21:55:24 +03:00
* Add a configuration blob to the backup ( sync )
*/
int proxmox_backup_add_config ( struct ProxmoxBackupHandle * handle ,
const char * name ,
const uint8_t * data ,
uint64_t size ,
char * * error ) ;
/**
* Add a configuration blob to the backup
2020-07-06 13:26:31 +03:00
*
2021-07-05 21:55:24 +03:00
* Create and upload a data blob " <name>.blob " .
2020-07-06 13:26:31 +03:00
*/
2021-07-05 21:55:24 +03:00
void proxmox_backup_add_config_async ( struct ProxmoxBackupHandle * handle ,
const char * name ,
const uint8_t * data ,
uint64_t size ,
void ( * callback ) ( void * ) ,
void * callback_data ,
int * result ,
char * * error ) ;
2020-07-06 13:26:31 +03:00
2020-06-29 14:12:10 +03:00
/**
* Write data to into a registered image ( sync )
2020-07-01 12:34:52 +03:00
*
* Upload a chunk of data for the < dev_id > image .
*
2020-07-02 11:34:10 +03:00
* The data pointer may be NULL in order to write the zero chunk
* ( only allowed if size = = chunk_size )
*
* Returns :
* - 1 : on error
2024-06-20 10:55:55 +03:00
* 0 : successful , chunk already exists on server , so it was reused
2020-07-02 11:34:10 +03:00
* size : successful , chunk uploaded
2020-06-29 14:12:10 +03:00
*/
2021-07-05 21:55:24 +03:00
int proxmox_backup_write_data ( struct ProxmoxBackupHandle * handle ,
2020-06-29 14:12:10 +03:00
uint8_t dev_id ,
const uint8_t * data ,
uint64_t offset ,
uint64_t size ,
char * * error ) ;
/**
* Write data to into a registered image
*
* Upload a chunk of data for the < dev_id > image .
2020-07-01 12:34:52 +03:00
*
2020-07-02 08:12:51 +03:00
* The data pointer may be NULL in order to write the zero chunk
* ( only allowed if size = = chunk_size )
*
* Note : The data pointer needs to be valid until the async
2024-06-20 10:55:55 +03:00
* operation is finished .
2020-07-02 11:34:10 +03:00
*
* Returns :
* - 1 : on error
2024-06-20 10:55:55 +03:00
* 0 : successful , chunk already exists on server , so it was reused
2020-07-02 11:34:10 +03:00
* size : successful , chunk uploaded
2020-06-29 14:12:10 +03:00
*/
2021-07-05 21:55:24 +03:00
void proxmox_backup_write_data_async ( struct ProxmoxBackupHandle * handle ,
2020-06-29 14:12:10 +03:00
uint8_t dev_id ,
const uint8_t * data ,
uint64_t offset ,
uint64_t size ,
void ( * callback ) ( void * ) ,
void * callback_data ,
int * result ,
char * * error ) ;
2020-10-22 18:34:19 +03:00
/**
2021-07-05 21:55:24 +03:00
* Close a registered image ( sync )
*/
int proxmox_backup_close_image ( struct ProxmoxBackupHandle * handle , uint8_t dev_id , char * * error ) ;
/**
* Close a registered image
2020-10-22 18:34:19 +03:00
*
2021-07-05 21:55:24 +03:00
* Mark the image as closed . Further writes are not possible .
2020-10-22 18:34:19 +03:00
*/
2021-07-05 21:55:24 +03:00
void proxmox_backup_close_image_async ( struct ProxmoxBackupHandle * handle ,
uint8_t dev_id ,
void ( * callback ) ( void * ) ,
void * callback_data ,
int * result ,
char * * error ) ;
2020-10-22 18:34:19 +03:00
/**
2021-07-05 21:55:24 +03:00
* Finish the backup ( sync )
2020-10-22 18:34:19 +03:00
*/
2021-07-05 21:55:24 +03:00
int proxmox_backup_finish ( struct ProxmoxBackupHandle * handle , char * * error ) ;
2020-10-22 18:34:19 +03:00
/**
2021-07-05 21:55:24 +03:00
* Finish the backup
*
* Finish the backup by creating and uploading the backup manifest .
* All registered images have to be closed before calling this .
2020-10-22 18:34:19 +03:00
*/
2021-07-05 21:55:24 +03:00
void proxmox_backup_finish_async ( struct ProxmoxBackupHandle * handle ,
void ( * callback ) ( void * ) ,
void * callback_data ,
int * result ,
char * * error ) ;
/**
* Disconnect and free allocated memory
*
* The handle becomes invalid after this call .
*/
void proxmox_backup_disconnect ( struct ProxmoxBackupHandle * handle ) ;
/**
2024-06-20 10:55:55 +03:00
* DEPRECATED : Connect to the backup server for restore ( sync )
2022-04-26 16:59:58 +03:00
*
* Deprecated in favor of ` proxmox_restore_new_ns ` which includes a namespace parameter .
* Also , it used " lossy " utf8 decoding on the snapshot name which is not the case in the new
* version anymore .
2021-07-05 21:55:24 +03:00
*/
struct ProxmoxRestoreHandle * proxmox_restore_new ( const char * repo ,
const char * snapshot ,
const char * password ,
const char * keyfile ,
const char * key_password ,
const char * fingerprint ,
char * * error ) ;
2020-10-22 18:34:19 +03:00
2022-04-26 16:59:58 +03:00
/**
2024-06-20 10:55:55 +03:00
* Connect to the backup server for restore ( sync )
2022-04-26 16:59:58 +03:00
*/
struct ProxmoxRestoreHandle * proxmox_restore_new_ns ( const char * repo ,
const char * snapshot ,
const char * namespace_ ,
const char * password ,
const char * keyfile ,
const char * key_password ,
const char * fingerprint ,
char * * error ) ;
2020-06-29 14:12:10 +03:00
/**
2020-07-06 09:05:21 +03:00
* Open connection to the backup server ( sync )
*
* Returns :
2024-06-20 10:55:55 +03:00
* 0 . . . Success ( no previous backup )
2020-07-06 09:05:21 +03:00
* - 1 . . . Error
*/
2021-07-05 21:55:24 +03:00
int proxmox_restore_connect ( struct ProxmoxRestoreHandle * handle , char * * error ) ;
2020-07-06 09:05:21 +03:00
/**
* Open connection to the backup server ( async )
2020-06-29 14:12:10 +03:00
*
2020-07-06 09:05:21 +03:00
* Returns :
2024-06-20 10:55:55 +03:00
* 0 . . . Success ( no previous backup )
2020-07-06 09:05:21 +03:00
* - 1 . . . Error
2020-06-29 14:12:10 +03:00
*/
2021-07-05 21:55:24 +03:00
void proxmox_restore_connect_async ( struct ProxmoxRestoreHandle * handle ,
2020-07-06 09:05:21 +03:00
void ( * callback ) ( void * ) ,
void * callback_data ,
int * result ,
char * * error ) ;
2020-06-29 14:12:10 +03:00
/**
* Disconnect and free allocated memory
*
* The handle becomes invalid after this call .
*/
2021-07-05 21:55:24 +03:00
void proxmox_restore_disconnect ( struct ProxmoxRestoreHandle * handle ) ;
2020-07-06 09:05:21 +03:00
/**
* Restore an image ( sync )
2020-06-29 14:12:10 +03:00
*
* Image data is downloaded and sequentially dumped to the callback .
*/
2021-07-05 21:55:24 +03:00
int proxmox_restore_image ( struct ProxmoxRestoreHandle * handle ,
2020-06-29 14:12:10 +03:00
const char * archive_name ,
int ( * callback ) ( void * , uint64_t , const unsigned char * , uint64_t ) ,
void * callback_data ,
char * * error ,
bool verbose ) ;
2020-07-06 09:05:21 +03:00
/**
* Retrieve the ID of a handle used to access data in the given archive ( sync )
*/
2021-07-05 21:55:24 +03:00
int proxmox_restore_open_image ( struct ProxmoxRestoreHandle * handle ,
2020-07-06 09:05:21 +03:00
const char * archive_name ,
char * * error ) ;
/**
* Retrieve the ID of a handle used to access data in the given archive ( async )
*/
2021-07-05 21:55:24 +03:00
void proxmox_restore_open_image_async ( struct ProxmoxRestoreHandle * handle ,
2020-07-06 09:05:21 +03:00
const char * archive_name ,
void ( * callback ) ( void * ) ,
void * callback_data ,
int * result ,
char * * error ) ;
2021-07-05 21:55:24 +03:00
/**
* Retrieve the length of a given archive handle in bytes
*/
long proxmox_restore_get_image_length ( struct ProxmoxRestoreHandle * handle ,
uint8_t aid ,
char * * error ) ;
2020-07-06 09:05:21 +03:00
/**
* Read data from the backup image at the given offset ( sync )
*
* Reads up to size bytes from handle aid at offset . On success ,
* returns the number of bytes read . ( a return of zero indicates end
* of file ) .
*
* Note : It is not an error for a successful call to transfer fewer
* bytes than requested .
*/
2021-07-05 21:55:24 +03:00
int proxmox_restore_read_image_at ( struct ProxmoxRestoreHandle * handle ,
2020-07-06 09:05:21 +03:00
uint8_t aid ,
uint8_t * data ,
uint64_t offset ,
uint64_t size ,
char * * error ) ;
/**
* Read data from the backup image at the given offset ( async )
*
* Reads up to size bytes from handle aid at offset . On success ,
* returns the number of bytes read . ( a return of zero indicates end
* of file ) .
*
* Note : The data pointer needs to be valid until the async
2024-06-20 10:55:55 +03:00
* operation is finished .
2020-07-06 09:05:21 +03:00
*
2020-07-22 16:56:25 +03:00
* Note : The call will only ever transfer less than ' size ' bytes if
* the end of the file has been reached .
2020-07-06 09:05:21 +03:00
*/
2021-07-05 21:55:24 +03:00
void proxmox_restore_read_image_at_async ( struct ProxmoxRestoreHandle * handle ,
2020-07-06 09:05:21 +03:00
uint8_t aid ,
uint8_t * data ,
uint64_t offset ,
uint64_t size ,
void ( * callback ) ( void * ) ,
void * callback_data ,
int * result ,
char * * error ) ;
2021-07-05 21:55:24 +03:00
/**
* Serialize all state data into a byte buffer . Can be loaded again with
* proxmox_import_state . Use for migration for example .
*
* Length of the returned buffer is written to buf_size . Returned buffer must
* be freed with proxmox_free_state_buf .
*/
uint8_t * proxmox_export_state ( uintptr_t * buf_size ) ;
/**
* Load state serialized by proxmox_export_state . If loading fails , a message
* will be logged to stderr , but the function will not fail .
*/
void proxmox_import_state ( const uint8_t * buf , uintptr_t buf_size ) ;
/**
* Free a buffer acquired from proxmox_export_state .
*/
void proxmox_free_state_buf ( uint8_t * buf ) ;
2020-06-29 14:12:10 +03:00
# endif /* PROXMOX_BACKUP_QEMU_H */