public inbox for pve-devel@lists.proxmox.com
 help / color / mirror / Atom feed
* [pve-devel] [RFC qemu/storage/qemu-server/container/manager v2 00/25] backup provider API
@ 2024-08-13 13:28 Fiona Ebner
  2024-08-13 13:28 ` [pve-devel] [PATCH qemu v2 01/25] block/reqlist: allow adding overlapping requests Fiona Ebner
                   ` (25 more replies)
  0 siblings, 26 replies; 44+ messages in thread
From: Fiona Ebner @ 2024-08-13 13:28 UTC (permalink / raw)
  To: pve-devel

Changes in v2:
* Add 'block-device' backup mechansim for VMs. The NBD export is
  mounted by Proxmox VE and only the block device path (as well as a
  callback to get the next dirty range for bitmaps) is passed to the
  backup provider.
* Add POC example for Borg - note that I tested with borg 1.2.4 in
  Debian and only tested with a local repository, not SSH yet.
* Merge hook API into a single function for backup and for jobs.
* Add restore_vm_init() and restore_vm_cleanup() for better
  flexibility to allow preparing the whole restore. Question is
  if restore_vm_volume_init() and restore_vm_volume_cleanup() should
  be dropped (but certain providers might prefer using only those)?
  Having both is more flexible, but makes the API longer of course.
* Switch to backup_vm() (was per-volume backup_vm_volume() before) and
  backup_container(), passing along the configuration files, rather
  than having dedicated methods for the configuration files, for
  giving the backup provider more flexibility.
* Some renames in API methods/params to improve clarity.
* Pass backup time to backup 'start' hook and use that in the
  directory example rather than the job start time.
* Use POD for base plugin documentation and flesh out documentation.
* Use 'BackupProvider::Plugin::' namespace.
* Various smaller improvements in the directory provider example.

======

A backup provider needs to implement a storage plugin as well as a
backup provider plugin. The storage plugin is for integration in
Proxmox VE's front-end, so users can manage the backups via
UI/API/CLI. The backup provider plugin is for interfacing with the
backup provider's backend to integrate backup and restore with that
backend into Proxmox VE.

This is an initial draft of an API and required changes to the backup
stack in Proxmox VE to make it work. Depending on feedback from other
developers and interested parties, it can still substantially change.

======

The backup provider API is split into two parts, both of which again
need different implementations for VM and LXC guests:

1. Backup API

There are two hook callback functions, namely:
1. job_hook() is called during the start/end/abort phases of the
   whole backup job.
2. backup_hook() is called during the start/end/abort phases of the
   backup of an individual guest.

The backup_get_mechanism() method is used to decide on the backup
mechanism. Currently, 'block-device' or 'nbd' for VMs, and 'directory'
for containers is possible. The method also let's the plugin indicate
whether to use a bitmap for incremental VM backup or not. It is enough
to implement one mechanism for VMs and one mechanism for containers.

Next, there are methods for backing up the guest's configuration and
data, backup_vm() for VM backup and backup_container() for container
backup.

Finally, some helpers like getting the provider name or volume ID for
the backup target, as well as for handling the backup log.

1.1 Backup Mechanisms

VM:

Access to the data on the VM's disk from the time the backup started
is made available via a so-called "snapshot access". This is either
the full image, or in case a bitmap is used, the dirty parts of the
image since the last time the bitmap was used for a successful backup.
Reading outside of the dirty parts will result in an error. After
backing up each part of the disk, it should be discarded in the export
to avoid unnecessary space usage on the Proxmox VE side (there is an
associated fleecing image).

VM mechanism 'block-device':

The snapshot access is exposed as a block device. If used, a bitmap is
passed along.

VM mechanism 'nbd':

The snapshot access and, if used, bitmap are exported via NBD.

Container mechanism 'directory':

A copy or snapshot of the container's filesystem state is made
available as a directory.

2. Restore API

The restore_get_mechanism() method is used to decide on the restore
mechanism. Currently, 'qemu-img' for VMs, and 'directory' or 'tar' for
containers are possible. It is enough to implement one mechanism for
VMs and one mechanism for containers.

Next, methods for extracting the guest and firewall configuration and
the implementations of the restore mechanism via a pair of methods: an
init method, for making the data available to Proxmox VE and a cleanup
method that is called after restore.

For VMs, there also is a restore_vm_get_device_info() helper required,
to get the disks included in the backup and their sizes.

2.1. Restore Mechanisms

VM mechanism 'qemu-img':

The backup provider gives a path to the disk image that will be
restored. The path needs to be something 'qemu-img' can deal with,
e.g. can also be an NBD URI or similar.

Container mechanism 'directory':

The backup provider gives the path to a directory with the full
filesystem structure of the container.

Container mechanism 'directory':

The backup provider gives the path to a (potentially compressed) tar
archive with the full filesystem structure of the container.

See the PVE::BackupProvider::Plugin module for the full API
documentation.

======

This series adapts the backup stack in Proxmox VE to allow using the
above API. For QEMU, backup access setup and teardown QMP commands are
implemented to be able to provide access to a consistent disk state to
the backup provider.

The series also provides an example implementation for a backup
provider as a proof-of-concept, exposing the different features.

======

Open questions:

Should the backup provider plugin system also follow the same API
age+version schema with a Custom/ directory for external plugins
derived from the base plugin?

Should the bitmap action be passed directly to the backup provider?
I.e. have 'not-used', 'not-used-removed', 'new', 'used', 'invalid',
instead of only 'none', 'new' and 'reuse'. It makes API slightly more
complicated. Is there any situation where backup provider could care
if bitmap is new, because it was the first or bitmap is new because
previous was invalid? Both cases require the backup provider to do a
full backup.

======

The patches marked as PATCH rather than RFC can make sense
independently, with QEMU patches 02 and 03 having been sent already
before (touching same code, so included here):

https://lore.proxmox.com/pve-devel/20240625133551.210636-1-f.ebner@proxmox.com/#r

======

Feedback is very welcome, especially from people wishing to implement
such a backup provider plugin! Please tell me what issues you see with
the proposed API, what would and wouldn't work from your perspective?

======

Dependencies: pve-manager, pve-container and qemu-server all depend on
new libpve-storage-perl. pve-manager also build-depends on the new
libpve-storage-perl for its tests. To keep things clean, pve-manager
should also depend on new pve-container and qemu-server.

In qemu-server, there is no version guard added yet, as that depends
on the QEMU version the feature will land in.

======

qemu:

Fiona Ebner (9):
  block/reqlist: allow adding overlapping requests
  PVE backup: fixup error handling for fleecing
  PVE backup: factor out setting up snapshot access for fleecing
  PVE backup: save device name in device info structure
  PVE backup: include device name in error when setting up snapshot
    access fails
  PVE backup: add target ID in backup state
  PVE backup: get device info: allow caller to specify filter for which
    devices use fleecing
  PVE backup: implement backup access setup and teardown API for
    external providers
  PVE backup: implement bitmap support for external backup access

 block/copy-before-write.c |   3 +-
 block/reqlist.c           |   2 -
 pve-backup.c              | 620 +++++++++++++++++++++++++++++++++-----
 pve-backup.h              |  16 +
 qapi/block-core.json      |  61 ++++
 system/runstate.c         |   6 +
 6 files changed, 637 insertions(+), 71 deletions(-)
 create mode 100644 pve-backup.h


storage:

Fiona Ebner (4):
  plugin: introduce new_backup_provider() method
  extract backup config: delegate to backup provider if there is one
  add backup provider example
  WIP Borg plugin

 src/PVE/BackupProvider/Makefile               |    3 +
 src/PVE/BackupProvider/Plugin/Base.pm         | 1149 +++++++++++++++++
 src/PVE/BackupProvider/Plugin/Borg.pm         |  373 ++++++
 .../BackupProvider/Plugin/DirectoryExample.pm |  694 ++++++++++
 src/PVE/BackupProvider/Plugin/Makefile        |    5 +
 src/PVE/Makefile                              |    1 +
 src/PVE/Storage.pm                            |   24 +-
 src/PVE/Storage/BorgBackupPlugin.pm           |  506 ++++++++
 .../Custom/BackupProviderDirExamplePlugin.pm  |  306 +++++
 src/PVE/Storage/Custom/Makefile               |    5 +
 src/PVE/Storage/Makefile                      |    2 +
 src/PVE/Storage/Plugin.pm                     |   15 +
 12 files changed, 3081 insertions(+), 2 deletions(-)
 create mode 100644 src/PVE/BackupProvider/Makefile
 create mode 100644 src/PVE/BackupProvider/Plugin/Base.pm
 create mode 100644 src/PVE/BackupProvider/Plugin/Borg.pm
 create mode 100644 src/PVE/BackupProvider/Plugin/DirectoryExample.pm
 create mode 100644 src/PVE/BackupProvider/Plugin/Makefile
 create mode 100644 src/PVE/Storage/BorgBackupPlugin.pm
 create mode 100644 src/PVE/Storage/Custom/BackupProviderDirExamplePlugin.pm
 create mode 100644 src/PVE/Storage/Custom/Makefile


qemu-server:

Fiona Ebner (8):
  move nbd_stop helper to QMPHelpers module
  backup: move cleanup of fleecing images to cleanup method
  backup: cleanup: check if VM is running before issuing QMP commands
  backup: keep track of block-node size instead of volume size
  backup: allow adding fleecing images also for EFI and TPM
  backup: implement backup for external providers
  restore: die early when there is no size for a device
  backup: implement restore for external providers

 PVE/API2/Qemu.pm             |  32 ++-
 PVE/CLI/qm.pm                |   3 +-
 PVE/QemuServer.pm            | 146 +++++++++++++-
 PVE/QemuServer/QMPHelpers.pm |   6 +
 PVE/VZDump/QemuServer.pm     | 370 ++++++++++++++++++++++++++++++++---
 5 files changed, 519 insertions(+), 38 deletions(-)


container:

Fiona Ebner (2):
  backup: implement backup for external providers
  backup: implement restore for external providers

 src/PVE/LXC/Create.pm | 141 ++++++++++++++++++++++++++++++++++++++++++
 src/PVE/VZDump/LXC.pm |  22 ++++++-
 2 files changed, 162 insertions(+), 1 deletion(-)


manager:

Fiona Ebner (2):
  ui: backup: also check for backup subtype to classify archive
  backup: implement backup for external providers

 PVE/VZDump.pm                      | 62 ++++++++++++++++++++++++++----
 test/vzdump_new_test.pl            |  3 ++
 www/manager6/Utils.js              | 10 +++--
 www/manager6/grid/BackupView.js    |  4 +-
 www/manager6/storage/BackupView.js |  4 +-
 5 files changed, 68 insertions(+), 15 deletions(-)


Summary over all repositories:
  30 files changed, 4467 insertions(+), 127 deletions(-)

-- 
Generated by git-murpp 0.5.0


_______________________________________________
pve-devel mailing list
pve-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel


^ permalink raw reply	[flat|nested] 44+ messages in thread

* [pve-devel] [PATCH qemu v2 01/25] block/reqlist: allow adding overlapping requests
  2024-08-13 13:28 [pve-devel] [RFC qemu/storage/qemu-server/container/manager v2 00/25] backup provider API Fiona Ebner
@ 2024-08-13 13:28 ` Fiona Ebner
  2024-08-13 13:28 ` [pve-devel] [PATCH qemu v2 02/25] PVE backup: fixup error handling for fleecing Fiona Ebner
                   ` (24 subsequent siblings)
  25 siblings, 0 replies; 44+ messages in thread
From: Fiona Ebner @ 2024-08-13 13:28 UTC (permalink / raw)
  To: pve-devel

Allow overlapping request by removing the assert that made it
impossible. There are only two callers:

1. block_copy_task_create()

It already asserts the very same condition before calling
reqlist_init_req().

2. cbw_snapshot_read_lock()

There is no need to have read requests be non-overlapping in
copy-before-write when used for snapshot-access. In fact, there was no
protection against two callers of cbw_snapshot_read_lock() calling
reqlist_init_req() with overlapping ranges and this could lead to an
assertion failure [1].

In particular, with the reproducer script below [0], two
cbw_co_snapshot_block_status() callers could race, with the second
calling reqlist_init_req() before the first one finishes and removes
its conflicting request.

[0]:

> #!/bin/bash -e
> dd if=/dev/urandom of=/tmp/disk.raw bs=1M count=1024
> ./qemu-img create /tmp/fleecing.raw -f raw 1G
> (
> ./qemu-system-x86_64 --qmp stdio \
> --blockdev raw,node-name=node0,file.driver=file,file.filename=/tmp/disk.raw \
> --blockdev raw,node-name=node1,file.driver=file,file.filename=/tmp/fleecing.raw \
> <<EOF
> {"execute": "qmp_capabilities"}
> {"execute": "blockdev-add", "arguments": { "driver": "copy-before-write", "file": "node0", "target": "node1", "node-name": "node3" } }
> {"execute": "blockdev-add", "arguments": { "driver": "snapshot-access", "file": "node3", "node-name": "snap0" } }
> {"execute": "nbd-server-start", "arguments": {"addr": { "type": "unix", "data": { "path": "/tmp/nbd.socket" } } } }
> {"execute": "block-export-add", "arguments": {"id": "exp0", "node-name": "snap0", "type": "nbd", "name": "exp0"}}
> EOF
> ) &
> sleep 5
> while true; do
> ./qemu-nbd -d /dev/nbd0
> ./qemu-nbd -c /dev/nbd0 nbd:unix:/tmp/nbd.socket:exportname=exp0 -f raw -r
> nbdinfo --map 'nbd+unix:///exp0?socket=/tmp/nbd.socket'
> done

[1]:

> #5  0x000071e5f0088eb2 in __GI___assert_fail (...) at ./assert/assert.c:101
> #6  0x0000615285438017 in reqlist_init_req (...) at ../block/reqlist.c:23
> #7  0x00006152853e2d98 in cbw_snapshot_read_lock (...) at ../block/copy-before-write.c:237
> #8  0x00006152853e3068 in cbw_co_snapshot_block_status (...) at ../block/copy-before-write.c:304
> #9  0x00006152853f4d22 in bdrv_co_snapshot_block_status (...) at ../block/io.c:3726
> #10 0x000061528543a63e in snapshot_access_co_block_status (...) at ../block/snapshot-access.c:48
> #11 0x00006152853f1a0a in bdrv_co_do_block_status (...) at ../block/io.c:2474
> #12 0x00006152853f2016 in bdrv_co_common_block_status_above (...) at ../block/io.c:2652
> #13 0x00006152853f22cf in bdrv_co_block_status_above (...) at ../block/io.c:2732
> #14 0x00006152853d9a86 in blk_co_block_status_above (...) at ../block/block-backend.c:1473
> #15 0x000061528538da6c in blockstatus_to_extents (...) at ../nbd/server.c:2374
> #16 0x000061528538deb1 in nbd_co_send_block_status (...) at ../nbd/server.c:2481
> #17 0x000061528538f424 in nbd_handle_request (...) at ../nbd/server.c:2978
> #18 0x000061528538f906 in nbd_trip (...) at ../nbd/server.c:3121
> #19 0x00006152855a7caf in coroutine_trampoline (...) at ../util/coroutine-ucontext.c:175

Cc: qemu-stable@nongnu.org
Suggested-by: Vladimir Sementsov-Ogievskiy <vsementsov@yandex-team.ru>
Signed-off-by: Fiona Ebner <f.ebner@proxmox.com>
Reviewed-by: Vladimir Sementsov-Ogievskiy <vsementsov@yandex-team.ru>
---

No changes in v2.

 block/copy-before-write.c | 3 ++-
 block/reqlist.c           | 2 --
 2 files changed, 2 insertions(+), 3 deletions(-)

diff --git a/block/copy-before-write.c b/block/copy-before-write.c
index 50cc4c7aae..a5bb4d14f6 100644
--- a/block/copy-before-write.c
+++ b/block/copy-before-write.c
@@ -67,7 +67,8 @@ typedef struct BDRVCopyBeforeWriteState {
 
     /*
      * @frozen_read_reqs: current read requests for fleecing user in bs->file
-     * node. These areas must not be rewritten by guest.
+     * node. These areas must not be rewritten by guest. There can be multiple
+     * overlapping read requests.
      */
     BlockReqList frozen_read_reqs;
 
diff --git a/block/reqlist.c b/block/reqlist.c
index 08cb57cfa4..098e807378 100644
--- a/block/reqlist.c
+++ b/block/reqlist.c
@@ -20,8 +20,6 @@
 void reqlist_init_req(BlockReqList *reqs, BlockReq *req, int64_t offset,
                       int64_t bytes)
 {
-    assert(!reqlist_find_conflict(reqs, offset, bytes));
-
     *req = (BlockReq) {
         .offset = offset,
         .bytes = bytes,
-- 
2.39.2



_______________________________________________
pve-devel mailing list
pve-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel


^ permalink raw reply	[flat|nested] 44+ messages in thread

* [pve-devel] [PATCH qemu v2 02/25] PVE backup: fixup error handling for fleecing
  2024-08-13 13:28 [pve-devel] [RFC qemu/storage/qemu-server/container/manager v2 00/25] backup provider API Fiona Ebner
  2024-08-13 13:28 ` [pve-devel] [PATCH qemu v2 01/25] block/reqlist: allow adding overlapping requests Fiona Ebner
@ 2024-08-13 13:28 ` Fiona Ebner
  2024-08-13 13:28 ` [pve-devel] [PATCH qemu v2 03/25] PVE backup: factor out setting up snapshot access " Fiona Ebner
                   ` (23 subsequent siblings)
  25 siblings, 0 replies; 44+ messages in thread
From: Fiona Ebner @ 2024-08-13 13:28 UTC (permalink / raw)
  To: pve-devel

The drained section needs to be terminated before breaking out of the
loop in the error scenarios. Otherwise, guest IO on the drive would
become stuck.

If the job is created successfully, then the job completion callback
will clean up the snapshot access block nodes. In case failure
happened before the job is created, there was no cleanup for the
snapshot access block nodes yet. Add it.

Signed-off-by: Fiona Ebner <f.ebner@proxmox.com>
---

No changes in v2.

 pve-backup.c | 38 +++++++++++++++++++++++++-------------
 1 file changed, 25 insertions(+), 13 deletions(-)

diff --git a/pve-backup.c b/pve-backup.c
index 4e730aa3da..c4178758b3 100644
--- a/pve-backup.c
+++ b/pve-backup.c
@@ -357,22 +357,23 @@ static void coroutine_fn pvebackup_co_complete_stream(void *opaque)
     qemu_co_mutex_unlock(&backup_state.backup_mutex);
 }
 
+static void cleanup_snapshot_access(PVEBackupDevInfo *di)
+{
+    if (di->fleecing.snapshot_access) {
+        bdrv_unref(di->fleecing.snapshot_access);
+        di->fleecing.snapshot_access = NULL;
+    }
+    if (di->fleecing.cbw) {
+        bdrv_cbw_drop(di->fleecing.cbw);
+        di->fleecing.cbw = NULL;
+    }
+}
+
 static void pvebackup_complete_cb(void *opaque, int ret)
 {
     PVEBackupDevInfo *di = opaque;
     di->completed_ret = ret;
 
-    /*
-     * Handle block-graph specific cleanup (for fleecing) outside of the coroutine, because the work
-     * won't be done as a coroutine anyways:
-     * - For snapshot_access, allows doing bdrv_unref() directly. Doing it via bdrv_co_unref() would
-     *   just spawn a BH calling bdrv_unref().
-     * - For cbw, draining would need to spawn a BH.
-     */
-    if (di->fleecing.snapshot_access) {
-        bdrv_unref(di->fleecing.snapshot_access);
-        di->fleecing.snapshot_access = NULL;
-    }
     if (di->fleecing.cbw) {
         /*
          * With fleecing, failure for cbw does not fail the guest write, but only sets the snapshot
@@ -383,10 +384,17 @@ static void pvebackup_complete_cb(void *opaque, int ret)
         if (di->completed_ret == -EACCES && snapshot_error) {
             di->completed_ret = snapshot_error;
         }
-        bdrv_cbw_drop(di->fleecing.cbw);
-        di->fleecing.cbw = NULL;
     }
 
+    /*
+     * Handle block-graph specific cleanup (for fleecing) outside of the coroutine, because the work
+     * won't be done as a coroutine anyways:
+     * - For snapshot_access, allows doing bdrv_unref() directly. Doing it via bdrv_co_unref() would
+     *   just spawn a BH calling bdrv_unref().
+     * - For cbw, draining would need to spawn a BH.
+     */
+    cleanup_snapshot_access(di);
+
     /*
      * Needs to happen outside of coroutine, because it takes the graph write lock.
      */
@@ -587,6 +595,7 @@ static void create_backup_jobs_bh(void *opaque) {
             if (!di->fleecing.cbw) {
                 error_setg(errp, "appending cbw node for fleecing failed: %s",
                            local_err ? error_get_pretty(local_err) : "unknown error");
+                bdrv_drained_end(di->bs);
                 break;
             }
 
@@ -599,6 +608,8 @@ static void create_backup_jobs_bh(void *opaque) {
             if (!di->fleecing.snapshot_access) {
                 error_setg(errp, "setting up snapshot access for fleecing failed: %s",
                            local_err ? error_get_pretty(local_err) : "unknown error");
+                cleanup_snapshot_access(di);
+                bdrv_drained_end(di->bs);
                 break;
             }
             source_bs = di->fleecing.snapshot_access;
@@ -637,6 +648,7 @@ static void create_backup_jobs_bh(void *opaque) {
         }
 
         if (!job || local_err) {
+            cleanup_snapshot_access(di);
             error_setg(errp, "backup_job_create failed: %s",
                        local_err ? error_get_pretty(local_err) : "null");
             break;
-- 
2.39.2



_______________________________________________
pve-devel mailing list
pve-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel


^ permalink raw reply	[flat|nested] 44+ messages in thread

* [pve-devel] [PATCH qemu v2 03/25] PVE backup: factor out setting up snapshot access for fleecing
  2024-08-13 13:28 [pve-devel] [RFC qemu/storage/qemu-server/container/manager v2 00/25] backup provider API Fiona Ebner
  2024-08-13 13:28 ` [pve-devel] [PATCH qemu v2 01/25] block/reqlist: allow adding overlapping requests Fiona Ebner
  2024-08-13 13:28 ` [pve-devel] [PATCH qemu v2 02/25] PVE backup: fixup error handling for fleecing Fiona Ebner
@ 2024-08-13 13:28 ` Fiona Ebner
  2024-08-13 13:28 ` [pve-devel] [PATCH qemu v2 04/25] PVE backup: save device name in device info structure Fiona Ebner
                   ` (22 subsequent siblings)
  25 siblings, 0 replies; 44+ messages in thread
From: Fiona Ebner @ 2024-08-13 13:28 UTC (permalink / raw)
  To: pve-devel

Avoids some line bloat in the create_backup_jobs_bh() function and is
in preparation for setting up the snapshot access independently of
fleecing, in particular that will be useful for providing access to
the snapshot via NBD.

Signed-off-by: Fiona Ebner <f.ebner@proxmox.com>
---

No changes in v2.

 pve-backup.c | 95 ++++++++++++++++++++++++++++++++--------------------
 1 file changed, 58 insertions(+), 37 deletions(-)

diff --git a/pve-backup.c b/pve-backup.c
index c4178758b3..051ebffe48 100644
--- a/pve-backup.c
+++ b/pve-backup.c
@@ -525,6 +525,62 @@ static int coroutine_fn pvebackup_co_add_config(
     goto out;
 }
 
+/*
+ * Setup a snapshot-access block node for a device with associated fleecing image.
+ */
+static int setup_snapshot_access(PVEBackupDevInfo *di, Error **errp)
+{
+    Error *local_err = NULL;
+
+    if (!di->fleecing.bs) {
+        error_setg(errp, "no associated fleecing image");
+        return -1;
+    }
+
+    QDict *cbw_opts = qdict_new();
+    qdict_put_str(cbw_opts, "driver", "copy-before-write");
+    qdict_put_str(cbw_opts, "file", bdrv_get_node_name(di->bs));
+    qdict_put_str(cbw_opts, "target", bdrv_get_node_name(di->fleecing.bs));
+
+    if (di->bitmap) {
+        /*
+         * Only guest writes to parts relevant for the backup need to be intercepted with
+         * old data being copied to the fleecing image.
+         */
+        qdict_put_str(cbw_opts, "bitmap.node", bdrv_get_node_name(di->bs));
+        qdict_put_str(cbw_opts, "bitmap.name", bdrv_dirty_bitmap_name(di->bitmap));
+    }
+    /*
+     * Fleecing storage is supposed to be fast and it's better to break backup than guest
+     * writes. Certain guest drivers like VirtIO-win have 60 seconds timeout by default, so
+     * abort a bit before that.
+     */
+    qdict_put_str(cbw_opts, "on-cbw-error", "break-snapshot");
+    qdict_put_int(cbw_opts, "cbw-timeout", 45);
+
+    di->fleecing.cbw = bdrv_insert_node(di->bs, cbw_opts, BDRV_O_RDWR, &local_err);
+
+    if (!di->fleecing.cbw) {
+        error_setg(errp, "appending cbw node for fleecing failed: %s",
+                   local_err ? error_get_pretty(local_err) : "unknown error");
+        return -1;
+    }
+
+    QDict *snapshot_access_opts = qdict_new();
+    qdict_put_str(snapshot_access_opts, "driver", "snapshot-access");
+    qdict_put_str(snapshot_access_opts, "file", bdrv_get_node_name(di->fleecing.cbw));
+
+    di->fleecing.snapshot_access =
+        bdrv_open(NULL, NULL, snapshot_access_opts, BDRV_O_RDWR | BDRV_O_UNMAP, &local_err);
+    if (!di->fleecing.snapshot_access) {
+        error_setg(errp, "setting up snapshot access for fleecing failed: %s",
+                   local_err ? error_get_pretty(local_err) : "unknown error");
+        return -1;
+    }
+
+    return 0;
+}
+
 /*
  * backup_job_create can *not* be run from a coroutine, so this can't either.
  * The caller is responsible that backup_mutex is held nonetheless.
@@ -569,49 +625,14 @@ static void create_backup_jobs_bh(void *opaque) {
         const char *job_id = bdrv_get_device_name(di->bs);
         bdrv_graph_co_rdunlock();
         if (di->fleecing.bs) {
-            QDict *cbw_opts = qdict_new();
-            qdict_put_str(cbw_opts, "driver", "copy-before-write");
-            qdict_put_str(cbw_opts, "file", bdrv_get_node_name(di->bs));
-            qdict_put_str(cbw_opts, "target", bdrv_get_node_name(di->fleecing.bs));
-
-            if (di->bitmap) {
-                /*
-                 * Only guest writes to parts relevant for the backup need to be intercepted with
-                 * old data being copied to the fleecing image.
-                 */
-                qdict_put_str(cbw_opts, "bitmap.node", bdrv_get_node_name(di->bs));
-                qdict_put_str(cbw_opts, "bitmap.name", bdrv_dirty_bitmap_name(di->bitmap));
-            }
-            /*
-             * Fleecing storage is supposed to be fast and it's better to break backup than guest
-             * writes. Certain guest drivers like VirtIO-win have 60 seconds timeout by default, so
-             * abort a bit before that.
-             */
-            qdict_put_str(cbw_opts, "on-cbw-error", "break-snapshot");
-            qdict_put_int(cbw_opts, "cbw-timeout", 45);
-
-            di->fleecing.cbw = bdrv_insert_node(di->bs, cbw_opts, BDRV_O_RDWR, &local_err);
-
-            if (!di->fleecing.cbw) {
-                error_setg(errp, "appending cbw node for fleecing failed: %s",
-                           local_err ? error_get_pretty(local_err) : "unknown error");
-                bdrv_drained_end(di->bs);
-                break;
-            }
-
-            QDict *snapshot_access_opts = qdict_new();
-            qdict_put_str(snapshot_access_opts, "driver", "snapshot-access");
-            qdict_put_str(snapshot_access_opts, "file", bdrv_get_node_name(di->fleecing.cbw));
-
-            di->fleecing.snapshot_access =
-                bdrv_open(NULL, NULL, snapshot_access_opts, BDRV_O_RDWR | BDRV_O_UNMAP, &local_err);
-            if (!di->fleecing.snapshot_access) {
+            if (setup_snapshot_access(di, &local_err) < 0) {
                 error_setg(errp, "setting up snapshot access for fleecing failed: %s",
                            local_err ? error_get_pretty(local_err) : "unknown error");
                 cleanup_snapshot_access(di);
                 bdrv_drained_end(di->bs);
                 break;
             }
+
             source_bs = di->fleecing.snapshot_access;
             discard_source = true;
 
-- 
2.39.2



_______________________________________________
pve-devel mailing list
pve-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel


^ permalink raw reply	[flat|nested] 44+ messages in thread

* [pve-devel] [PATCH qemu v2 04/25] PVE backup: save device name in device info structure
  2024-08-13 13:28 [pve-devel] [RFC qemu/storage/qemu-server/container/manager v2 00/25] backup provider API Fiona Ebner
                   ` (2 preceding siblings ...)
  2024-08-13 13:28 ` [pve-devel] [PATCH qemu v2 03/25] PVE backup: factor out setting up snapshot access " Fiona Ebner
@ 2024-08-13 13:28 ` Fiona Ebner
  2024-08-13 13:28 ` [pve-devel] [PATCH qemu v2 05/25] PVE backup: include device name in error when setting up snapshot access fails Fiona Ebner
                   ` (21 subsequent siblings)
  25 siblings, 0 replies; 44+ messages in thread
From: Fiona Ebner @ 2024-08-13 13:28 UTC (permalink / raw)
  To: pve-devel

The device name needs to be queried while holding the graph read lock
and since it doesn't change during the whole operation, just get it
once during setup and avoid the need to query it again in different
places.

Also in preparation to use it more often in error messages and for the
upcoming external backup access API.

Signed-off-by: Fiona Ebner <f.ebner@proxmox.com>
---

No changes in v2.

 pve-backup.c | 29 +++++++++++++++--------------
 1 file changed, 15 insertions(+), 14 deletions(-)

diff --git a/pve-backup.c b/pve-backup.c
index 051ebffe48..33c23e53c2 100644
--- a/pve-backup.c
+++ b/pve-backup.c
@@ -94,6 +94,7 @@ typedef struct PVEBackupDevInfo {
     size_t size;
     uint64_t block_size;
     uint8_t dev_id;
+    char* device_name;
     int completed_ret; // INT_MAX if not completed
     BdrvDirtyBitmap *bitmap;
     BlockDriverState *target;
@@ -327,6 +328,8 @@ static void coroutine_fn pvebackup_co_complete_stream(void *opaque)
     }
 
     di->bs = NULL;
+    g_free(di->device_name);
+    di->device_name = NULL;
 
     assert(di->target == NULL);
 
@@ -621,9 +624,6 @@ static void create_backup_jobs_bh(void *opaque) {
 
         BlockDriverState *source_bs = di->bs;
         bool discard_source = false;
-        bdrv_graph_co_rdlock();
-        const char *job_id = bdrv_get_device_name(di->bs);
-        bdrv_graph_co_rdunlock();
         if (di->fleecing.bs) {
             if (setup_snapshot_access(di, &local_err) < 0) {
                 error_setg(errp, "setting up snapshot access for fleecing failed: %s",
@@ -654,7 +654,7 @@ static void create_backup_jobs_bh(void *opaque) {
         }
 
         BlockJob *job = backup_job_create(
-            job_id, source_bs, di->target, backup_state.speed, sync_mode, di->bitmap,
+            di->device_name, source_bs, di->target, backup_state.speed, sync_mode, di->bitmap,
             bitmap_mode, false, discard_source, NULL, &perf, BLOCKDEV_ON_ERROR_REPORT,
             BLOCKDEV_ON_ERROR_REPORT, JOB_DEFAULT, pvebackup_complete_cb, di, backup_state.txn,
             &local_err);
@@ -751,6 +751,7 @@ static GList coroutine_fn GRAPH_RDLOCK *get_device_info(
             }
             PVEBackupDevInfo *di = g_new0(PVEBackupDevInfo, 1);
             di->bs = bs;
+            di->device_name = g_strdup(bdrv_get_device_name(bs));
 
             if (fleecing && device_uses_fleecing(*d)) {
                 g_autofree gchar *fleecing_devid = g_strconcat(*d, "-fleecing", NULL);
@@ -789,6 +790,7 @@ static GList coroutine_fn GRAPH_RDLOCK *get_device_info(
 
             PVEBackupDevInfo *di = g_new0(PVEBackupDevInfo, 1);
             di->bs = bs;
+            di->device_name = g_strdup(bdrv_get_device_name(bs));
             di_list = g_list_append(di_list, di);
         }
     }
@@ -956,9 +958,6 @@ UuidInfo coroutine_fn *qmp_backup(
 
             di->block_size = dump_cb_block_size;
 
-            bdrv_graph_co_rdlock();
-            const char *devname = bdrv_get_device_name(di->bs);
-            bdrv_graph_co_rdunlock();
             PBSBitmapAction action = PBS_BITMAP_ACTION_NOT_USED;
             size_t dirty = di->size;
 
@@ -973,7 +972,8 @@ UuidInfo coroutine_fn *qmp_backup(
                     }
                     action = PBS_BITMAP_ACTION_NEW;
                 } else {
-                    expect_only_dirty = proxmox_backup_check_incremental(pbs, devname, di->size) != 0;
+                    expect_only_dirty =
+                        proxmox_backup_check_incremental(pbs, di->device_name, di->size) != 0;
                 }
 
                 if (expect_only_dirty) {
@@ -997,7 +997,8 @@ UuidInfo coroutine_fn *qmp_backup(
                 }
             }
 
-            int dev_id = proxmox_backup_co_register_image(pbs, devname, di->size, expect_only_dirty, errp);
+            int dev_id = proxmox_backup_co_register_image(pbs, di->device_name, di->size,
+                                                          expect_only_dirty, errp);
             if (dev_id < 0) {
                 goto err_mutex;
             }
@@ -1009,7 +1010,7 @@ UuidInfo coroutine_fn *qmp_backup(
             di->dev_id = dev_id;
 
             PBSBitmapInfo *info = g_malloc(sizeof(*info));
-            info->drive = g_strdup(devname);
+            info->drive = g_strdup(di->device_name);
             info->action = action;
             info->size = di->size;
             info->dirty = dirty;
@@ -1034,10 +1035,7 @@ UuidInfo coroutine_fn *qmp_backup(
                 goto err_mutex;
             }
 
-            bdrv_graph_co_rdlock();
-            const char *devname = bdrv_get_device_name(di->bs);
-            bdrv_graph_co_rdunlock();
-            di->dev_id = vma_writer_register_stream(vmaw, devname, di->size);
+            di->dev_id = vma_writer_register_stream(vmaw, di->device_name, di->size);
             if (di->dev_id <= 0) {
                 error_set(errp, ERROR_CLASS_GENERIC_ERROR,
                           "register_stream failed");
@@ -1148,6 +1146,9 @@ err:
             bdrv_co_unref(di->target);
         }
 
+        g_free(di->device_name);
+        di->device_name = NULL;
+
         g_free(di);
     }
     g_list_free(di_list);
-- 
2.39.2



_______________________________________________
pve-devel mailing list
pve-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel


^ permalink raw reply	[flat|nested] 44+ messages in thread

* [pve-devel] [PATCH qemu v2 05/25] PVE backup: include device name in error when setting up snapshot access fails
  2024-08-13 13:28 [pve-devel] [RFC qemu/storage/qemu-server/container/manager v2 00/25] backup provider API Fiona Ebner
                   ` (3 preceding siblings ...)
  2024-08-13 13:28 ` [pve-devel] [PATCH qemu v2 04/25] PVE backup: save device name in device info structure Fiona Ebner
@ 2024-08-13 13:28 ` Fiona Ebner
  2024-08-13 13:28 ` [pve-devel] [RFC qemu v2 06/25] PVE backup: add target ID in backup state Fiona Ebner
                   ` (20 subsequent siblings)
  25 siblings, 0 replies; 44+ messages in thread
From: Fiona Ebner @ 2024-08-13 13:28 UTC (permalink / raw)
  To: pve-devel

Signed-off-by: Fiona Ebner <f.ebner@proxmox.com>
---

No changes in v2.

 pve-backup.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/pve-backup.c b/pve-backup.c
index 33c23e53c2..d931746453 100644
--- a/pve-backup.c
+++ b/pve-backup.c
@@ -626,7 +626,8 @@ static void create_backup_jobs_bh(void *opaque) {
         bool discard_source = false;
         if (di->fleecing.bs) {
             if (setup_snapshot_access(di, &local_err) < 0) {
-                error_setg(errp, "setting up snapshot access for fleecing failed: %s",
+                error_setg(errp, "%s - setting up snapshot access for fleecing failed: %s",
+                           di->device_name,
                            local_err ? error_get_pretty(local_err) : "unknown error");
                 cleanup_snapshot_access(di);
                 bdrv_drained_end(di->bs);
-- 
2.39.2



_______________________________________________
pve-devel mailing list
pve-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel


^ permalink raw reply	[flat|nested] 44+ messages in thread

* [pve-devel] [RFC qemu v2 06/25] PVE backup: add target ID in backup state
  2024-08-13 13:28 [pve-devel] [RFC qemu/storage/qemu-server/container/manager v2 00/25] backup provider API Fiona Ebner
                   ` (4 preceding siblings ...)
  2024-08-13 13:28 ` [pve-devel] [PATCH qemu v2 05/25] PVE backup: include device name in error when setting up snapshot access fails Fiona Ebner
@ 2024-08-13 13:28 ` Fiona Ebner
  2024-08-13 13:28 ` [pve-devel] [RFC qemu v2 07/25] PVE backup: get device info: allow caller to specify filter for which devices use fleecing Fiona Ebner
                   ` (19 subsequent siblings)
  25 siblings, 0 replies; 44+ messages in thread
From: Fiona Ebner @ 2024-08-13 13:28 UTC (permalink / raw)
  To: pve-devel

In preparation for allowing multiple backup providers. Each backup
target can then have its own dirty bitmap and there can be additional
checks that the current backup state is actually associated to the
expected target.

Signed-off-by: Fiona Ebner <f.ebner@proxmox.com>
---

No changes in v2.

 pve-backup.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/pve-backup.c b/pve-backup.c
index d931746453..e8031bb89c 100644
--- a/pve-backup.c
+++ b/pve-backup.c
@@ -70,6 +70,7 @@ static struct PVEBackupState {
     JobTxn *txn;
     CoMutex backup_mutex;
     CoMutex dump_callback_mutex;
+    char *target_id;
 } backup_state;
 
 static void pvebackup_init(void)
@@ -848,7 +849,7 @@ UuidInfo coroutine_fn *qmp_backup(
 
     if (backup_state.di_list) {
         error_set(errp, ERROR_CLASS_GENERIC_ERROR,
-                  "previous backup not finished");
+                  "previous backup by provider '%s' not finished", backup_state.target_id);
         qemu_co_mutex_unlock(&backup_state.backup_mutex);
         return NULL;
     }
@@ -1100,6 +1101,11 @@ UuidInfo coroutine_fn *qmp_backup(
     backup_state.vmaw = vmaw;
     backup_state.pbs = pbs;
 
+    if (backup_state.target_id) {
+        g_free(backup_state.target_id);
+    }
+    backup_state.target_id = g_strdup("Proxmox");
+
     backup_state.di_list = di_list;
 
     uuid_info = g_malloc0(sizeof(*uuid_info));
-- 
2.39.2



_______________________________________________
pve-devel mailing list
pve-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel


^ permalink raw reply	[flat|nested] 44+ messages in thread

* [pve-devel] [RFC qemu v2 07/25] PVE backup: get device info: allow caller to specify filter for which devices use fleecing
  2024-08-13 13:28 [pve-devel] [RFC qemu/storage/qemu-server/container/manager v2 00/25] backup provider API Fiona Ebner
                   ` (5 preceding siblings ...)
  2024-08-13 13:28 ` [pve-devel] [RFC qemu v2 06/25] PVE backup: add target ID in backup state Fiona Ebner
@ 2024-08-13 13:28 ` Fiona Ebner
  2024-08-13 13:28 ` [pve-devel] [RFC qemu v2 08/25] PVE backup: implement backup access setup and teardown API for external providers Fiona Ebner
                   ` (18 subsequent siblings)
  25 siblings, 0 replies; 44+ messages in thread
From: Fiona Ebner @ 2024-08-13 13:28 UTC (permalink / raw)
  To: pve-devel

For providing snapshot-access to external backup providers, EFI and
TPM also need an associated fleecing image. The new caller will thus
need a different filter.

Signed-off-by: Fiona Ebner <f.ebner@proxmox.com>
---

No changes in v2.

 pve-backup.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/pve-backup.c b/pve-backup.c
index e8031bb89c..d0593fc581 100644
--- a/pve-backup.c
+++ b/pve-backup.c
@@ -717,7 +717,7 @@ static void create_backup_jobs_bh(void *opaque) {
 /*
  * EFI disk and TPM state are small and it's just not worth setting up fleecing for them.
  */
-static bool device_uses_fleecing(const char *device_id)
+static bool fleecing_no_efi_tpm(const char *device_id)
 {
     return strncmp(device_id, "drive-efidisk", 13) && strncmp(device_id, "drive-tpmstate", 14);
 }
@@ -729,7 +729,7 @@ static bool device_uses_fleecing(const char *device_id)
  */
 static GList coroutine_fn GRAPH_RDLOCK *get_device_info(
     const char *devlist,
-    bool fleecing,
+    bool (*device_uses_fleecing)(const char*),
     Error **errp)
 {
     gchar **devs = NULL;
@@ -755,7 +755,7 @@ static GList coroutine_fn GRAPH_RDLOCK *get_device_info(
             di->bs = bs;
             di->device_name = g_strdup(bdrv_get_device_name(bs));
 
-            if (fleecing && device_uses_fleecing(*d)) {
+            if (device_uses_fleecing && device_uses_fleecing(*d)) {
                 g_autofree gchar *fleecing_devid = g_strconcat(*d, "-fleecing", NULL);
                 BlockBackend *fleecing_blk = blk_by_name(fleecing_devid);
                 if (!fleecing_blk) {
@@ -858,7 +858,8 @@ UuidInfo coroutine_fn *qmp_backup(
     format = has_format ? format : BACKUP_FORMAT_VMA;
 
     bdrv_graph_co_rdlock();
-    di_list = get_device_info(devlist, has_fleecing && fleecing, &local_err);
+    di_list = get_device_info(devlist, (has_fleecing && fleecing) ? fleecing_no_efi_tpm : NULL,
+                              &local_err);
     bdrv_graph_co_rdunlock();
     if (local_err) {
         error_propagate(errp, local_err);
-- 
2.39.2



_______________________________________________
pve-devel mailing list
pve-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel


^ permalink raw reply	[flat|nested] 44+ messages in thread

* [pve-devel] [RFC qemu v2 08/25] PVE backup: implement backup access setup and teardown API for external providers
  2024-08-13 13:28 [pve-devel] [RFC qemu/storage/qemu-server/container/manager v2 00/25] backup provider API Fiona Ebner
                   ` (6 preceding siblings ...)
  2024-08-13 13:28 ` [pve-devel] [RFC qemu v2 07/25] PVE backup: get device info: allow caller to specify filter for which devices use fleecing Fiona Ebner
@ 2024-08-13 13:28 ` Fiona Ebner
  2024-08-13 13:28 ` [pve-devel] [RFC qemu v2 09/25] PVE backup: implement bitmap support for external backup access Fiona Ebner
                   ` (17 subsequent siblings)
  25 siblings, 0 replies; 44+ messages in thread
From: Fiona Ebner @ 2024-08-13 13:28 UTC (permalink / raw)
  To: pve-devel

For external backup providers, the state of the VM's disk images at
the time the backup is started is preserved via a snapshot-access
block node. Old data is moved to the fleecing image when new guest
writes come in. The snapshot-access block node, as well as the
associated bitmap in case of incremental backup, will be exported via
NBD to the external provider. The NBD export will be done by the
management layer, the missing functionality is setting up and tearing
down the snapshot-access block nodes, which this patch adds.

It is necessary to also set up fleecing for EFI and TPM disks, so that
old data can be moved out of the way when a new guest write comes in.

There can only be one regular backup or one active backup access at
a time, because both require replacing the original block node of the
drive. Thus the backup state is re-used, and checks are added to
prohibit regular backup while snapshot access is active and vice
versa.

The block nodes added by the backup-access-setup QMP call are not
tracked anywhere else (there is no job they are associated to like for
regular backup). This requires adding a callback for teardown when
QEMU exits, i.e. in qemu_cleanup(). Otherwise, there will be an
assertion failure that the block graph is not empty when QEMU exits
before the backup-access-teardown QMP command is called.

The code for the qmp_backup_access_setup() was based on the existing
qmp_backup() routine.

The return value for the setup QMP command contains information about
the snapshot-access block nodes that can be used by the management
layer to set up the NBD exports.

Signed-off-by: Fiona Ebner <f.ebner@proxmox.com>
---

Changes in v2:
* Also return the size of the block devices in the setup call.

 pve-backup.c         | 273 +++++++++++++++++++++++++++++++++++++++++++
 pve-backup.h         |  16 +++
 qapi/block-core.json |  45 +++++++
 system/runstate.c    |   6 +
 4 files changed, 340 insertions(+)
 create mode 100644 pve-backup.h

diff --git a/pve-backup.c b/pve-backup.c
index d0593fc581..d3370d6744 100644
--- a/pve-backup.c
+++ b/pve-backup.c
@@ -1,4 +1,5 @@
 #include "proxmox-backup-client.h"
+#include "pve-backup.h"
 #include "vma.h"
 
 #include "qemu/osdep.h"
@@ -585,6 +586,37 @@ static int setup_snapshot_access(PVEBackupDevInfo *di, Error **errp)
     return 0;
 }
 
+static void setup_all_snapshot_access_bh(void *opaque)
+{
+    assert(!qemu_in_coroutine());
+
+    CoCtxData *data = (CoCtxData*)opaque;
+    Error **errp = (Error**)data->data;
+
+    Error *local_err = NULL;
+
+    GList *l =  backup_state.di_list;
+    while (l) {
+        PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
+        l = g_list_next(l);
+
+        bdrv_drained_begin(di->bs);
+
+        if (setup_snapshot_access(di, &local_err) < 0) {
+            cleanup_snapshot_access(di);
+            bdrv_drained_end(di->bs);
+            error_setg(errp, "%s - setting up snapshot access failed: %s", di->device_name,
+                       local_err ? error_get_pretty(local_err) : "unknown error");
+            break;
+        }
+
+        bdrv_drained_end(di->bs);
+    }
+
+    /* return */
+    aio_co_enter(data->ctx, data->co);
+}
+
 /*
  * backup_job_create can *not* be run from a coroutine, so this can't either.
  * The caller is responsible that backup_mutex is held nonetheless.
@@ -722,6 +754,11 @@ static bool fleecing_no_efi_tpm(const char *device_id)
     return strncmp(device_id, "drive-efidisk", 13) && strncmp(device_id, "drive-tpmstate", 14);
 }
 
+static bool fleecing_all(const char *device_id)
+{
+    return true;
+}
+
 /*
  * Returns a list of device infos, which needs to be freed by the caller. In
  * case of an error, errp will be set, but the returned value might still be a
@@ -810,6 +847,242 @@ err:
     return di_list;
 }
 
+BackupAccessInfoList *coroutine_fn qmp_backup_access_setup(
+    const char *target_id,
+    const char *devlist,
+    Error **errp)
+{
+    assert(qemu_in_coroutine());
+
+    qemu_co_mutex_lock(&backup_state.backup_mutex);
+
+    Error *local_err = NULL;
+    GList *di_list = NULL;
+    GList *l;
+
+    if (backup_state.di_list) {
+        error_set(errp, ERROR_CLASS_GENERIC_ERROR,
+                  "previous backup by provider '%s' not finished", backup_state.target_id);
+        qemu_co_mutex_unlock(&backup_state.backup_mutex);
+        return NULL;
+    }
+
+    bdrv_graph_co_rdlock();
+    di_list = get_device_info(devlist, fleecing_all, &local_err);
+    bdrv_graph_co_rdunlock();
+    if (local_err) {
+        error_propagate(errp, local_err);
+        goto err;
+    }
+    assert(di_list);
+
+    size_t total = 0;
+
+    l = di_list;
+    while (l) {
+        PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
+        l = g_list_next(l);
+
+        ssize_t size = bdrv_getlength(di->bs);
+        if (size < 0) {
+            error_setg_errno(errp, -size, "bdrv_getlength failed");
+            goto err;
+        }
+        di->size = size;
+        total += size;
+
+        di->completed_ret = INT_MAX;
+    }
+
+    qemu_mutex_lock(&backup_state.stat.lock);
+    backup_state.stat.reused = 0;
+
+    /* clear previous backup's bitmap_list */
+    if (backup_state.stat.bitmap_list) {
+        GList *bl = backup_state.stat.bitmap_list;
+        while (bl) {
+            g_free(((PBSBitmapInfo *)bl->data)->drive);
+            g_free(bl->data);
+            bl = g_list_next(bl);
+        }
+        g_list_free(backup_state.stat.bitmap_list);
+        backup_state.stat.bitmap_list = NULL;
+    }
+
+    /* initialize global backup_state now */
+
+    if (backup_state.stat.error) {
+        error_free(backup_state.stat.error);
+        backup_state.stat.error = NULL;
+    }
+
+    backup_state.stat.start_time = time(NULL);
+    backup_state.stat.end_time = 0;
+
+    if (backup_state.stat.backup_file) {
+        g_free(backup_state.stat.backup_file);
+    }
+    backup_state.stat.backup_file = NULL;
+
+    if (backup_state.target_id) {
+        g_free(backup_state.target_id);
+    }
+    backup_state.target_id = g_strdup(target_id);
+
+    /*
+     * The stats will never update, because there is no internal backup job. Initialize them anyway
+     * for completeness.
+     */
+    backup_state.stat.total = total;
+    backup_state.stat.dirty = total - backup_state.stat.reused;
+    backup_state.stat.transferred = 0;
+    backup_state.stat.zero_bytes = 0;
+    backup_state.stat.finishing = false;
+    backup_state.stat.starting = false; // there's no associated QEMU job
+
+    qemu_mutex_unlock(&backup_state.stat.lock);
+
+    backup_state.vmaw = NULL;
+    backup_state.pbs = NULL;
+
+    backup_state.di_list = di_list;
+
+    /* Run setup_all_snapshot_access_bh outside of coroutine (in BH) but keep
+    * backup_mutex locked. This is fine, a CoMutex can be held across yield
+    * points, and we'll release it as soon as the BH reschedules us.
+    */
+    CoCtxData waker = {
+        .co = qemu_coroutine_self(),
+        .ctx = qemu_get_current_aio_context(),
+        .data = &local_err,
+    };
+    aio_bh_schedule_oneshot(waker.ctx, setup_all_snapshot_access_bh, &waker);
+    qemu_coroutine_yield();
+
+    if (local_err) {
+        error_propagate(errp, local_err);
+        goto err;
+    }
+
+    qemu_co_mutex_unlock(&backup_state.backup_mutex);
+
+    BackupAccessInfoList *bai_head = NULL, **p_bai_next = &bai_head;
+
+    l = di_list;
+    while (l) {
+        PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
+        l = g_list_next(l);
+
+        BackupAccessInfoList *info = g_malloc0(sizeof(*info));
+        info->value = g_malloc0(sizeof(*info->value));
+        info->value->node_name = g_strdup(bdrv_get_node_name(di->fleecing.snapshot_access));
+        info->value->device = g_strdup(di->device_name);
+        info->value->size = di->size;
+
+        *p_bai_next = info;
+        p_bai_next = &info->next;
+    }
+
+    return bai_head;
+
+err:
+
+    l = di_list;
+    while (l) {
+        PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
+        l = g_list_next(l);
+
+        g_free(di->device_name);
+        di->device_name = NULL;
+
+        g_free(di);
+    }
+    g_list_free(di_list);
+    backup_state.di_list = NULL;
+
+    qemu_co_mutex_unlock(&backup_state.backup_mutex);
+    return NULL;
+}
+
+/*
+ * Caller needs to hold the backup mutex or the BQL.
+ */
+void backup_access_teardown(void)
+{
+    GList *l = backup_state.di_list;
+
+    while (l) {
+        PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
+        l = g_list_next(l);
+
+        if (di->fleecing.snapshot_access) {
+            bdrv_unref(di->fleecing.snapshot_access);
+            di->fleecing.snapshot_access = NULL;
+        }
+        if (di->fleecing.cbw) {
+            bdrv_cbw_drop(di->fleecing.cbw);
+            di->fleecing.cbw = NULL;
+        }
+
+        g_free(di->device_name);
+        di->device_name = NULL;
+
+        g_free(di);
+    }
+    g_list_free(backup_state.di_list);
+    backup_state.di_list = NULL;
+}
+
+// Not done in a coroutine, because bdrv_co_unref() and cbw_drop() would just spawn BHs anyways.
+// Caller needs to hold the backup_state.backup_mutex lock
+static void backup_access_teardown_bh(void *opaque)
+{
+    CoCtxData *data = (CoCtxData*)opaque;
+
+    backup_access_teardown();
+
+    /* return */
+    aio_co_enter(data->ctx, data->co);
+}
+
+void coroutine_fn qmp_backup_access_teardown(const char *target_id, Error **errp)
+{
+    assert(qemu_in_coroutine());
+
+    qemu_co_mutex_lock(&backup_state.backup_mutex);
+
+    if (!backup_state.target_id) { // nothing to do
+        qemu_co_mutex_unlock(&backup_state.backup_mutex);
+        return;
+    }
+
+    /*
+     * Continue with target_id == NULL, used by the callback registered for qemu_cleanup()
+     */
+    if (target_id && strcmp(backup_state.target_id, target_id)) {
+        error_setg(errp, "cannot teardown backup access - got provider %s instead of %s",
+                   target_id, backup_state.target_id);
+        qemu_co_mutex_unlock(&backup_state.backup_mutex);
+        return;
+    }
+
+    if (!strcmp(backup_state.target_id, "Proxmox VE")) {
+        error_setg(errp, "cannot teardown backup access for PVE - use backup-cancel instead");
+        qemu_co_mutex_unlock(&backup_state.backup_mutex);
+        return;
+    }
+
+    CoCtxData waker = {
+        .co = qemu_coroutine_self(),
+        .ctx = qemu_get_current_aio_context(),
+    };
+    aio_bh_schedule_oneshot(waker.ctx, backup_access_teardown_bh, &waker);
+    qemu_coroutine_yield();
+
+    qemu_co_mutex_unlock(&backup_state.backup_mutex);
+    return;
+}
+
 UuidInfo coroutine_fn *qmp_backup(
     const char *backup_file,
     const char *password,
diff --git a/pve-backup.h b/pve-backup.h
new file mode 100644
index 0000000000..4033bc848f
--- /dev/null
+++ b/pve-backup.h
@@ -0,0 +1,16 @@
+/*
+ * Bacup code used by Proxmox VE
+ *
+ * Copyright (C) Proxmox Server Solutions
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ *
+ */
+
+#ifndef PVE_BACKUP_H
+#define PVE_BACKUP_H
+
+void backup_access_teardown(void);
+
+#endif /* PVE_BACKUP_H */
diff --git a/qapi/block-core.json b/qapi/block-core.json
index ff441d4258..68f8da3144 100644
--- a/qapi/block-core.json
+++ b/qapi/block-core.json
@@ -1098,6 +1098,51 @@
 ##
 { 'command': 'query-pbs-bitmap-info', 'returns': ['PBSBitmapInfo'] }
 
+##
+# @BackupAccessInfo:
+#
+# Info associated to a snapshot access for backup.  For more information about
+# the bitmap see @BackupAccessBitmapMode.
+#
+# @node-name: the block node name of the snapshot-access node.
+#
+# @device: the device on top of which the snapshot access was created.
+#
+# @size: the size of the block device in bytes.
+#
+##
+{ 'struct': 'BackupAccessInfo',
+  'data': { 'node-name': 'str', 'device': 'str', 'size': 'size' } }
+
+##
+# @backup-access-setup:
+#
+# Set up snapshot access to VM drives for external backup provider.  No other
+# backup or backup access can be done before tearing down the backup access.
+#
+# @target-id: the ID of the external backup provider.
+#
+# @devlist: list of block device names (separated by ',', ';' or ':'). By
+#     default the backup includes all writable block devices.
+#
+# Returns: a list of @BackupAccessInfo, one for each device.
+#
+##
+{ 'command': 'backup-access-setup',
+  'data': { 'target-id': 'str', '*devlist': 'str' },
+  'returns': [ 'BackupAccessInfo' ], 'coroutine': true }
+
+##
+# @backup-access-teardown:
+#
+# Tear down previously setup snapshot access for the same provider.
+#
+# @target-id: the ID of the external backup provider.
+#
+##
+{ 'command': 'backup-access-teardown', 'data': { 'target-id': 'str' },
+  'coroutine': true }
+
 ##
 # @BlockDeviceTimedStats:
 #
diff --git a/system/runstate.c b/system/runstate.c
index d6ab860eca..7e641e4484 100644
--- a/system/runstate.c
+++ b/system/runstate.c
@@ -60,6 +60,7 @@
 #include "sysemu/sysemu.h"
 #include "sysemu/tpm.h"
 #include "trace.h"
+#include "pve-backup.h"
 
 static NotifierList exit_notifiers =
     NOTIFIER_LIST_INITIALIZER(exit_notifiers);
@@ -868,6 +869,11 @@ void qemu_cleanup(int status)
      * requests happening from here on anyway.
      */
     bdrv_drain_all_begin();
+    /*
+     * The backup access is set up by a QMP command, but is neither owned by a monitor nor
+     * associated to a BlockBackend. Need to tear it down manually here.
+     */
+    backup_access_teardown();
     job_cancel_sync_all();
     bdrv_close_all();
 
-- 
2.39.2



_______________________________________________
pve-devel mailing list
pve-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel


^ permalink raw reply	[flat|nested] 44+ messages in thread

* [pve-devel] [RFC qemu v2 09/25] PVE backup: implement bitmap support for external backup access
  2024-08-13 13:28 [pve-devel] [RFC qemu/storage/qemu-server/container/manager v2 00/25] backup provider API Fiona Ebner
                   ` (7 preceding siblings ...)
  2024-08-13 13:28 ` [pve-devel] [RFC qemu v2 08/25] PVE backup: implement backup access setup and teardown API for external providers Fiona Ebner
@ 2024-08-13 13:28 ` Fiona Ebner
  2024-08-13 13:28 ` [pve-devel] [RFC storage v2 10/25] plugin: introduce new_backup_provider() method Fiona Ebner
                   ` (16 subsequent siblings)
  25 siblings, 0 replies; 44+ messages in thread
From: Fiona Ebner @ 2024-08-13 13:28 UTC (permalink / raw)
  To: pve-devel

There can be one dirty bitmap for each backup target ID (which are
tracked in the backup_access_bitmaps hash table). The QMP user can
specify the ID of the bitmap it likes to use. This ID is then compared
to the current one for the given target. If they match, the bitmap is
re-used (should it still exist on the drive, otherwise re-created). If
there is a mismatch, the old bitmap is removed and a new one is
created.

The return value of the QMP command includes information about what
bitmap action was taken. Similar to what the query-backup QMP command
returns for regular backup. It also includes the bitmap name and
associated block node, so the management layer can then set up an NBD
export with the bitmap.

While the backup access is active, a background bitmap is also
required. This is necessary to implement bitmap handling according to
the original reference [0]. In particular:

- in the error case, new writes since the backup access was set up are
  in the background bitmap. Because of failure, the previously tracked
  writes from the backup access bitmap are still required too. Thus,
  the bitmap is merged with the background bitmap to get all new
  writes since the last backup.

- in the success case, continue tracking for the next incremental
  backup in the backup access bitmap. New writes since the backup
  access was set up are in the background bitmap. Because the backup
  was successfully, clear the backup access bitmap and merge back the
  background bitmap to get only the new writes.

Since QEMU cannot know if the backup was successful or not (except if
failure already happens during the setup QMP command), the management
layer needs to tell it via the teardown QMP command.

The bitmap action is also recorded in the device info now.

[0]: https://lore.kernel.org/qemu-devel/b68833dd-8864-4d72-7c61-c134a9835036@ya.ru/

Signed-off-by: Fiona Ebner <f.ebner@proxmox.com>
---

No changes in v2.

 pve-backup.c         | 175 ++++++++++++++++++++++++++++++++++++++++++-
 pve-backup.h         |   2 +-
 qapi/block-core.json |  22 +++++-
 system/runstate.c    |   2 +-
 4 files changed, 193 insertions(+), 8 deletions(-)

diff --git a/pve-backup.c b/pve-backup.c
index d3370d6744..5f8dd396d5 100644
--- a/pve-backup.c
+++ b/pve-backup.c
@@ -15,6 +15,7 @@
 #include "qapi/qmp/qdict.h"
 #include "qapi/qmp/qerror.h"
 #include "qemu/cutils.h"
+#include "qemu/error-report.h"
 
 #if defined(CONFIG_MALLOC_TRIM)
 #include <malloc.h>
@@ -41,6 +42,7 @@
  */
 
 const char *PBS_BITMAP_NAME = "pbs-incremental-dirty-bitmap";
+const char *BACKGROUND_BITMAP_NAME = "backup-access-background-bitmap";
 
 static struct PVEBackupState {
     struct {
@@ -72,6 +74,7 @@ static struct PVEBackupState {
     CoMutex backup_mutex;
     CoMutex dump_callback_mutex;
     char *target_id;
+    GHashTable *backup_access_bitmaps; // key=target_id, value=bitmap_name
 } backup_state;
 
 static void pvebackup_init(void)
@@ -99,6 +102,8 @@ typedef struct PVEBackupDevInfo {
     char* device_name;
     int completed_ret; // INT_MAX if not completed
     BdrvDirtyBitmap *bitmap;
+    BdrvDirtyBitmap *background_bitmap; // used for external backup access
+    PBSBitmapAction bitmap_action;
     BlockDriverState *target;
     BlockJob *job;
 } PVEBackupDevInfo;
@@ -362,6 +367,67 @@ static void coroutine_fn pvebackup_co_complete_stream(void *opaque)
     qemu_co_mutex_unlock(&backup_state.backup_mutex);
 }
 
+/*
+ * New writes since the backup access was set up are in the background bitmap. Because of failure,
+ * the previously tracked writes in di->bitmap are still required too. Thus, merge with the
+ * background bitmap to get all new writes since the last backup.
+ */
+static void handle_backup_access_bitmaps_in_error_case(PVEBackupDevInfo *di)
+{
+    Error *local_err = NULL;
+
+    if (di->bs && di->background_bitmap) {
+        bdrv_drained_begin(di->bs);
+        if (di->bitmap) {
+            bdrv_enable_dirty_bitmap(di->bitmap);
+            if (!bdrv_merge_dirty_bitmap(di->bitmap, di->background_bitmap, NULL, &local_err)) {
+                warn_report("backup access: %s - could not merge bitmaps in error path - %s",
+                            di->device_name,
+                            local_err ? error_get_pretty(local_err) : "unknown error");
+                /*
+                 * Could not merge, drop original bitmap too.
+                 */
+                bdrv_release_dirty_bitmap(di->bitmap);
+            }
+        } else {
+            warn_report("backup access: %s - expected bitmap not present", di->device_name);
+        }
+        bdrv_release_dirty_bitmap(di->background_bitmap);
+        bdrv_drained_end(di->bs);
+    }
+}
+
+/*
+ * Continue tracking for next incremental backup in di->bitmap. New writes since the backup access
+ * was set up are in the background bitmap. Because the backup was successful, clear di->bitmap and
+ * merge back the background bitmap to get only the new writes.
+ */
+static void handle_backup_access_bitmaps_after_success(PVEBackupDevInfo *di)
+{
+    Error *local_err = NULL;
+
+    if (di->bs && di->background_bitmap) {
+        bdrv_drained_begin(di->bs);
+        if (di->bitmap) {
+            bdrv_enable_dirty_bitmap(di->bitmap);
+            bdrv_clear_dirty_bitmap(di->bitmap, NULL);
+            if (!bdrv_merge_dirty_bitmap(di->bitmap, di->background_bitmap, NULL, &local_err)) {
+                warn_report("backup access: %s - could not merge bitmaps after backup - %s",
+                            di->device_name,
+                            local_err ? error_get_pretty(local_err) : "unknown error");
+                /*
+                 * Could not merge, drop original bitmap too.
+                 */
+                bdrv_release_dirty_bitmap(di->bitmap);
+            }
+        } else {
+            warn_report("backup access: %s - expected bitmap not present", di->device_name);
+        }
+        bdrv_release_dirty_bitmap(di->background_bitmap);
+        bdrv_drained_end(di->bs);
+    }
+}
+
 static void cleanup_snapshot_access(PVEBackupDevInfo *di)
 {
     if (di->fleecing.snapshot_access) {
@@ -602,6 +668,21 @@ static void setup_all_snapshot_access_bh(void *opaque)
 
         bdrv_drained_begin(di->bs);
 
+        if (di->bitmap) {
+            BdrvDirtyBitmap *background_bitmap =
+                bdrv_create_dirty_bitmap(di->bs, PROXMOX_BACKUP_DEFAULT_CHUNK_SIZE,
+                                         BACKGROUND_BITMAP_NAME, &local_err);
+            if (!background_bitmap) {
+                error_setg(errp, "%s - creating background bitmap for backup access failed: %s",
+                           di->device_name,
+                           local_err ? error_get_pretty(local_err) : "unknown error");
+                bdrv_drained_end(di->bs);
+                break;
+            }
+            di->background_bitmap = background_bitmap;
+            bdrv_disable_dirty_bitmap(di->bitmap);
+        }
+
         if (setup_snapshot_access(di, &local_err) < 0) {
             cleanup_snapshot_access(di);
             bdrv_drained_end(di->bs);
@@ -850,6 +931,7 @@ err:
 BackupAccessInfoList *coroutine_fn qmp_backup_access_setup(
     const char *target_id,
     const char *devlist,
+    const char *bitmap_name,
     Error **errp)
 {
     assert(qemu_in_coroutine());
@@ -909,6 +991,77 @@ BackupAccessInfoList *coroutine_fn qmp_backup_access_setup(
         backup_state.stat.bitmap_list = NULL;
     }
 
+    if (!backup_state.backup_access_bitmaps) {
+        backup_state.backup_access_bitmaps =
+            g_hash_table_new_full(g_str_hash, g_str_equal, free, free);
+    }
+
+    /* create bitmaps if requested */
+    l = di_list;
+    while (l) {
+        PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
+        l = g_list_next(l);
+
+        di->block_size = PROXMOX_BACKUP_DEFAULT_CHUNK_SIZE;
+
+        PBSBitmapAction action = PBS_BITMAP_ACTION_NOT_USED;
+        size_t dirty = di->size;
+
+        const char *old_bitmap_name =
+            (const char*)g_hash_table_lookup(backup_state.backup_access_bitmaps, target_id);
+
+        bool same_bitmap_name =
+            old_bitmap_name && bitmap_name && strcmp(bitmap_name, old_bitmap_name) == 0;
+
+        if (old_bitmap_name && !same_bitmap_name) {
+            BdrvDirtyBitmap *old_bitmap = bdrv_find_dirty_bitmap(di->bs, old_bitmap_name);
+            if (!old_bitmap) {
+                warn_report("setup backup access: expected old bitmap '%s' not found for drive "
+                            "'%s'", old_bitmap_name, di->device_name);
+            } else {
+                g_hash_table_remove(backup_state.backup_access_bitmaps, target_id);
+                bdrv_release_dirty_bitmap(old_bitmap);
+                action = PBS_BITMAP_ACTION_NOT_USED_REMOVED;
+            }
+        }
+
+        BdrvDirtyBitmap *bitmap = NULL;
+        if (bitmap_name) {
+            bitmap = bdrv_find_dirty_bitmap(di->bs, bitmap_name);
+            if (!bitmap) {
+                bitmap = bdrv_create_dirty_bitmap(di->bs, PROXMOX_BACKUP_DEFAULT_CHUNK_SIZE,
+                                                  bitmap_name, errp);
+                if (!bitmap) {
+                    qemu_mutex_unlock(&backup_state.stat.lock);
+                    goto err;
+                }
+                bdrv_set_dirty_bitmap(bitmap, 0, di->size);
+                action = same_bitmap_name ? PBS_BITMAP_ACTION_INVALID : PBS_BITMAP_ACTION_NEW;
+            } else {
+                /* track clean chunks as reused */
+                dirty = MIN(bdrv_get_dirty_count(bitmap), di->size);
+                backup_state.stat.reused += di->size - dirty;
+                action = PBS_BITMAP_ACTION_USED;
+            }
+
+            if (!same_bitmap_name) {
+                g_hash_table_insert(backup_state.backup_access_bitmaps,
+                                    strdup(target_id), strdup(bitmap_name));
+            }
+
+        }
+
+        PBSBitmapInfo *info = g_malloc(sizeof(*info));
+        info->drive = g_strdup(di->device_name);
+        info->action = action;
+        info->size = di->size;
+        info->dirty = dirty;
+        backup_state.stat.bitmap_list = g_list_append(backup_state.stat.bitmap_list, info);
+
+        di->bitmap = bitmap;
+        di->bitmap_action = action;
+    }
+
     /* initialize global backup_state now */
 
     if (backup_state.stat.error) {
@@ -978,6 +1131,12 @@ BackupAccessInfoList *coroutine_fn qmp_backup_access_setup(
         info->value->node_name = g_strdup(bdrv_get_node_name(di->fleecing.snapshot_access));
         info->value->device = g_strdup(di->device_name);
         info->value->size = di->size;
+        if (bitmap_name) {
+            info->value->bitmap_node_name = g_strdup(bdrv_get_node_name(di->bs));
+            info->value->bitmap_name = g_strdup(bitmap_name);
+            info->value->bitmap_action = di->bitmap_action;
+            info->value->has_bitmap_action = true;
+        }
 
         *p_bai_next = info;
         p_bai_next = &info->next;
@@ -992,6 +1151,8 @@ err:
         PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
         l = g_list_next(l);
 
+        handle_backup_access_bitmaps_in_error_case(di);
+
         g_free(di->device_name);
         di->device_name = NULL;
 
@@ -1007,7 +1168,7 @@ err:
 /*
  * Caller needs to hold the backup mutex or the BQL.
  */
-void backup_access_teardown(void)
+void backup_access_teardown(bool success)
 {
     GList *l = backup_state.di_list;
 
@@ -1024,6 +1185,12 @@ void backup_access_teardown(void)
             di->fleecing.cbw = NULL;
         }
 
+        if (success) {
+            handle_backup_access_bitmaps_after_success(di);
+        } else {
+            handle_backup_access_bitmaps_in_error_case(di);
+        }
+
         g_free(di->device_name);
         di->device_name = NULL;
 
@@ -1039,13 +1206,13 @@ static void backup_access_teardown_bh(void *opaque)
 {
     CoCtxData *data = (CoCtxData*)opaque;
 
-    backup_access_teardown();
+    backup_access_teardown(*((bool*)data->data));
 
     /* return */
     aio_co_enter(data->ctx, data->co);
 }
 
-void coroutine_fn qmp_backup_access_teardown(const char *target_id, Error **errp)
+void coroutine_fn qmp_backup_access_teardown(const char *target_id, bool success, Error **errp)
 {
     assert(qemu_in_coroutine());
 
@@ -1075,6 +1242,7 @@ void coroutine_fn qmp_backup_access_teardown(const char *target_id, Error **errp
     CoCtxData waker = {
         .co = qemu_coroutine_self(),
         .ctx = qemu_get_current_aio_context(),
+        .data = &success,
     };
     aio_bh_schedule_oneshot(waker.ctx, backup_access_teardown_bh, &waker);
     qemu_coroutine_yield();
@@ -1284,6 +1452,7 @@ UuidInfo coroutine_fn *qmp_backup(
             }
 
             di->dev_id = dev_id;
+            di->bitmap_action = action;
 
             PBSBitmapInfo *info = g_malloc(sizeof(*info));
             info->drive = g_strdup(di->device_name);
diff --git a/pve-backup.h b/pve-backup.h
index 4033bc848f..9ebeef7c8f 100644
--- a/pve-backup.h
+++ b/pve-backup.h
@@ -11,6 +11,6 @@
 #ifndef PVE_BACKUP_H
 #define PVE_BACKUP_H
 
-void backup_access_teardown(void);
+void backup_access_teardown(bool success);
 
 #endif /* PVE_BACKUP_H */
diff --git a/qapi/block-core.json b/qapi/block-core.json
index 68f8da3144..2de777c86b 100644
--- a/qapi/block-core.json
+++ b/qapi/block-core.json
@@ -1110,9 +1110,17 @@
 #
 # @size: the size of the block device in bytes.
 #
+# @bitmap-node-name: the block node name the dirty bitmap is associated to.
+#
+# @bitmap-name: the name of the dirty bitmap associated to the backup access.
+#
+# @bitmap-action: the action taken on the dirty bitmap.
+#
 ##
 { 'struct': 'BackupAccessInfo',
-  'data': { 'node-name': 'str', 'device': 'str', 'size': 'size' } }
+  'data': { 'node-name': 'str', 'device': 'str', 'size': 'size',
+            '*bitmap-node-name': 'str', '*bitmap-name': 'str',
+            '*bitmap-action': 'PBSBitmapAction' } }
 
 ##
 # @backup-access-setup:
@@ -1125,11 +1133,16 @@
 # @devlist: list of block device names (separated by ',', ';' or ':'). By
 #     default the backup includes all writable block devices.
 #
+# @bitmap-name: use/create a bitmap with this name. Re-using the same name
+#     allows for making incremental backups. Check the @bitmap-action in the
+#     result to see if you can actually re-use the bitmap or if it had to be
+#     newly created.
+#
 # Returns: a list of @BackupAccessInfo, one for each device.
 #
 ##
 { 'command': 'backup-access-setup',
-  'data': { 'target-id': 'str', '*devlist': 'str' },
+  'data': { 'target-id': 'str', '*devlist': 'str', '*bitmap-name': 'str' },
   'returns': [ 'BackupAccessInfo' ], 'coroutine': true }
 
 ##
@@ -1139,8 +1152,11 @@
 #
 # @target-id: the ID of the external backup provider.
 #
+# @success: whether the backup done by the external provider was successful.
+#
 ##
-{ 'command': 'backup-access-teardown', 'data': { 'target-id': 'str' },
+{ 'command': 'backup-access-teardown',
+  'data': { 'target-id': 'str', 'success': 'bool' },
   'coroutine': true }
 
 ##
diff --git a/system/runstate.c b/system/runstate.c
index 7e641e4484..b61996dd7a 100644
--- a/system/runstate.c
+++ b/system/runstate.c
@@ -873,7 +873,7 @@ void qemu_cleanup(int status)
      * The backup access is set up by a QMP command, but is neither owned by a monitor nor
      * associated to a BlockBackend. Need to tear it down manually here.
      */
-    backup_access_teardown();
+    backup_access_teardown(false);
     job_cancel_sync_all();
     bdrv_close_all();
 
-- 
2.39.2



_______________________________________________
pve-devel mailing list
pve-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel


^ permalink raw reply	[flat|nested] 44+ messages in thread

* [pve-devel] [RFC storage v2 10/25] plugin: introduce new_backup_provider() method
  2024-08-13 13:28 [pve-devel] [RFC qemu/storage/qemu-server/container/manager v2 00/25] backup provider API Fiona Ebner
                   ` (8 preceding siblings ...)
  2024-08-13 13:28 ` [pve-devel] [RFC qemu v2 09/25] PVE backup: implement bitmap support for external backup access Fiona Ebner
@ 2024-08-13 13:28 ` Fiona Ebner
  2024-09-12 12:43   ` Fabian Grünbichler
  2024-08-13 13:28 ` [pve-devel] [RFC storage v2 11/25] extract backup config: delegate to backup provider if there is one Fiona Ebner
                   ` (15 subsequent siblings)
  25 siblings, 1 reply; 44+ messages in thread
From: Fiona Ebner @ 2024-08-13 13:28 UTC (permalink / raw)
  To: pve-devel

The new_backup_provider() method can be used by storage plugins for
external backup providers. If the method returns a provider, Proxmox
VE will use callbacks to that provider for backups and restore instead
of using its usual backup/restore mechanisms.

API age and version are both bumped.

The backup provider API is split into two parts, both of which again
need different implementations for VM and LXC guests:

1. Backup API

There are two hook callback functions, namely:
1. job_hook() is called during the start/end/abort phases of the
   whole backup job.
2. backup_hook() is called during the start/end/abort phases of the
   backup of an individual guest.

The backup_get_mechanism() method is used to decide on the backup
mechanism. Currently, 'block-device' or 'nbd' for VMs, and 'directory'
for containers is possible. The method also let's the plugin indicate
whether to use a bitmap for incremental VM backup or not. It is enough
to implement one mechanism for VMs and one mechanism for containers.

Next, there are methods for backing up the guest's configuration and
data, backup_vm() for VM backup and backup_container() for container
backup.

Finally, some helpers like getting the provider name or volume ID for
the backup target, as well as for handling the backup log.

1.1 Backup Mechanisms

VM:

Access to the data on the VM's disk from the time the backup started
is made available via a so-called "snapshot access". This is either
the full image, or in case a bitmap is used, the dirty parts of the
image since the last time the bitmap was used for a successful backup.
Reading outside of the dirty parts will result in an error. After
backing up each part of the disk, it should be discarded in the export
to avoid unnecessary space usage on the Proxmox VE side (there is an
associated fleecing image).

VM mechanism 'block-device':

The snapshot access is exposed as a block device. If used, a bitmap is
passed along.

VM mechanism 'nbd':

The snapshot access and, if used, bitmap are exported via NBD.

Container mechanism 'directory':

A copy or snapshot of the container's filesystem state is made
available as a directory.

2. Restore API

The restore_get_mechanism() method is used to decide on the restore
mechanism. Currently, 'qemu-img' for VMs, and 'directory' or 'tar' for
containers are possible. It is enough to implement one mechanism for
VMs and one mechanism for containers.

Next, methods for extracting the guest and firewall configuration and
the implementations of the restore mechanism via a pair of methods: an
init method, for making the data available to Proxmox VE and a cleanup
method that is called after restore.

For VMs, there also is a restore_vm_get_device_info() helper required,
to get the disks included in the backup and their sizes.

2.1. Restore Mechanisms

VM mechanism 'qemu-img':

The backup provider gives a path to the disk image that will be
restored. The path needs to be something 'qemu-img' can deal with,
e.g. can also be an NBD URI or similar.

Container mechanism 'directory':

The backup provider gives the path to a directory with the full
filesystem structure of the container.

Container mechanism 'directory':

The backup provider gives the path to a (potentially compressed) tar
archive with the full filesystem structure of the container.

See the PVE::BackupProvider::Plugin module for the full API
documentation.

Signed-off-by: Fiona Ebner <f.ebner@proxmox.com>
---

Changes in v2:
* Merge hook API into a single function for backup and for jobs.
* Add restore_vm_init() and restore_vm_cleanup() for better
  flexibility to allow preparing the whole restore. Question is
  if restore_vm_volume_init() and restore_vm_volume_cleanup() should
  be dropped (but certain providers might prefer using only those)?
  Having both is more flexible, but makes the API longer of course.
* Switch to backup_vm() (was per-volume backup_vm_volume() before) and
  backup_container(), passing along the configuration files, rather
  than having dedicated methods for the configuration files, for
  giving the backup provider more flexibility.
* Pass backup time to backup 'start' hook and use that in the
  directory example rather than the job start time.
* Use POD for base plugin documentation and flesh out documentation.
* Use 'BackupProvider::Plugin::' namespace.
* Rename $exclude_paths to $exclude_patterns and clarify what should
  be supported.
* Rename backup_get_target() method to backup_get_archive_name() for
  clarity.
* Rename extract_guest_config() to restore_get_guest_config() for
  better consistency with other (restore) API methods.

 src/PVE/BackupProvider/Makefile        |    3 +
 src/PVE/BackupProvider/Plugin/Base.pm  | 1149 ++++++++++++++++++++++++
 src/PVE/BackupProvider/Plugin/Makefile |    5 +
 src/PVE/Makefile                       |    1 +
 src/PVE/Storage.pm                     |   12 +-
 src/PVE/Storage/Plugin.pm              |   15 +
 6 files changed, 1183 insertions(+), 2 deletions(-)
 create mode 100644 src/PVE/BackupProvider/Makefile
 create mode 100644 src/PVE/BackupProvider/Plugin/Base.pm
 create mode 100644 src/PVE/BackupProvider/Plugin/Makefile

diff --git a/src/PVE/BackupProvider/Makefile b/src/PVE/BackupProvider/Makefile
new file mode 100644
index 0000000..f018cef
--- /dev/null
+++ b/src/PVE/BackupProvider/Makefile
@@ -0,0 +1,3 @@
+.PHONY: install
+install:
+	make -C Plugin install
diff --git a/src/PVE/BackupProvider/Plugin/Base.pm b/src/PVE/BackupProvider/Plugin/Base.pm
new file mode 100644
index 0000000..99e1ca8
--- /dev/null
+++ b/src/PVE/BackupProvider/Plugin/Base.pm
@@ -0,0 +1,1149 @@
+package PVE::BackupProvider::Plugin::Base;
+
+use strict;
+use warnings;
+
+=pod
+
+=head1 NAME
+
+PVE::BackupProvider::Plugin::Base - Base Plugin for Backup Provider API
+
+=head1 SYNOPSIS
+
+    use base qw(PVE::BackupProvider::Plugin::Base);
+
+=head1 DESCRIPTION
+
+This module serves as the base for any module implementing the API that Proxmox
+VE uses to interface with external backup providers. The API is used for
+creating and restoring backups. A backup provider also needs to provide a
+storage plugin for integration with the front-end. The API here is used by the
+backup stack in the backend.
+
+1. Backup API
+
+There are two hook callback functions, namely:
+
+=over
+
+=item C<job_hook()>
+
+Called during the start/end/abort phases of the whole backup job.
+
+=item C<backup_hook()>
+
+Called during the start/end/abort phases of the backup of an
+individual guest.
+
+=back
+
+The backup_get_mechanism() method is used to decide on the backup mechanism.
+Currently, 'block-device' or 'nbd' for VMs, and 'directory' for containers is
+possible. The method also let's the plugin indicate whether to use a bitmap for
+incremental VM backup or not. It is enough to implement one mechanism for VMs
+and one mechanism for containers.
+
+Next, there are methods for backing up the guest's configuration and data,
+backup_vm() for VM backup and backup_container() for container backup.
+
+Finally, some helpers like getting the provider name or volume ID for the backup
+target, as well as for handling the backup log.
+
+1.1 Backup Mechanisms
+
+VM:
+
+Access to the data on the VM's disk from the time the backup started is made
+available via a so-called "snapshot access". This is either the full image, or
+in case a bitmap is used, the dirty parts of the image since the last time the
+bitmap was used for a successful backup. Reading outside of the dirty parts will
+result in an error. After backing up each part of the disk, it should be
+discarded in the export to avoid unnecessary space usage on the Proxmox VE side
+(there is an associated fleecing image).
+
+VM mechanism 'block-device':
+
+The snapshot access is exposed as a block device. If used, a bitmap is passed
+along.
+
+VM mechanism 'nbd':
+
+The snapshot access and, if used, bitmap are exported via NBD.
+
+Container mechanism 'directory':
+
+A copy or snapshot of the container's filesystem state is made available as a
+directory.
+
+2. Restore API
+
+The restore_get_mechanism() method is used to decide on the restore mechanism.
+Currently, 'qemu-img' for VMs, and 'directory' or 'tar' for containers are
+possible. It is enough to implement one mechanism for VMs and one mechanism for
+containers.
+
+Next, methods for extracting the guest and firewall configuration and the
+implementations of the restore mechanism via a pair of methods: an init method,
+for making the data available to Proxmox VE and a cleanup method that is called
+after restore.
+
+For VMs, there also is a restore_vm_get_device_info() helper required, to get
+the disks included in the backup and their sizes.
+
+2.1. Restore Mechanisms
+
+VM mechanism 'qemu-img':
+
+The backup provider gives a path to the disk image that will be restored. The
+path needs to be something 'qemu-img' can deal with, e.g. can also be an NBD URI
+or similar.
+
+Container mechanism 'directory':
+
+The backup provider gives the path to a directory with the full filesystem
+structure of the container.
+
+Container mechanism 'directory':
+
+The backup provider gives the path to a (potentially compressed) tar archive
+with the full filesystem structure of the container.
+
+=head1 METHODS
+
+=cut
+
+# plugin methods
+
+=pod
+
+=over
+
+=item C<new>
+
+The constructor. Returns a blessed instance of the backup provider class.
+
+Parameters:
+
+=over
+
+=item C<$storage_plugin>
+
+The associated storage plugin class.
+
+=item C<$scfg>
+
+The storage configuration of the associated storage.
+
+=item C<$storeid>
+
+The storage ID of the associated storage.
+
+=item C<$log_function>
+
+The function signature is C<$log_function($log_level, $message)>. This log
+function can be used to write to the backup task log in Proxmox VE.
+
+=over
+
+=item C<$log_level>
+
+Either C<info>, C<warn> or C<err> for informational messages, warnings or error
+messages.
+
+=item C<$message>
+
+The message to be printed.
+
+=back
+
+=back
+
+=back
+
+=cut
+sub new {
+    my ($class, $storage_plugin, $scfg, $storeid, $log_function) = @_;
+
+    die "implement me in subclass";
+}
+
+=pod
+
+=over
+
+=item C<provider_name>
+
+Returns the name of the backup provider. It will be printed in some log lines.
+
+=back
+
+=cut
+sub provider_name {
+    my ($self) = @_;
+
+    die "implement me in subclass";
+}
+
+=pod
+
+=over
+
+=item C<job_hook>
+
+The job hook function. Is called during various phases of the backup job.
+Intended for doing preparations and cleanup. In the future, additional phases
+might get added, so it's best to ignore an unknown phase.
+
+Parameters:
+
+=over
+
+=item C<$phase>
+
+The phase during which the function is called.
+
+=over
+
+=item C<start>
+
+When the job starts, before the first backup is made.
+
+=item C<end>
+
+When the job ends, after all backups are finished, even if some backups
+failed.
+
+=item C<abort>
+
+When the job is aborted (e.g. interrupted by signal, other fundamental failure).
+
+=back
+
+=item C<$info>
+
+A hash reference containing additional parameters depending on the C<$phase>:
+
+=over
+
+=item C<start>
+
+=over
+
+=item C<< $info->{'start-time'} >>
+
+Unix time-stamp of when the job started.
+
+=back
+
+=item C<end>
+
+No additional information.
+
+=item C<abort>
+
+=over
+
+=item C<< $info->{error} >>
+
+The error message indicating the failure.
+
+=back
+
+=back
+
+=back
+
+=back
+
+=cut
+sub job_hook {
+    my ($self, $phase, $info) = @_;
+
+    die "implement me in subclass";
+}
+
+=pod
+
+=over
+
+=item C<backup_hook>
+
+The backup hook function. Is called during various phases during the backup of a
+given guest. Intended for doing preparations and cleanup. In the future,
+additional phases might get added, so it's best to ignore an unknown phase.
+
+Parameters:
+
+=over
+
+=item C<$phase>
+
+The phase during which the function is called.
+
+=over
+
+=item C<start>
+
+Before the backup of the given guest is made.
+
+=item C<end>
+
+After the backup of the given guest finished successfully.
+
+=item C<abort>
+
+After the backup of the given guest encountered an error or was aborted.
+
+=back
+
+=item C<$vmid>
+
+The ID of the guest being backed up.
+
+=item C<$vmtype>
+
+The type of the guest being backed up. Currently, either C<qemu> or C<lxc>.
+Might be C<undef> in phase C<abort> for certain error scenarios.
+
+=item C<$info>
+
+A hash reference containing additional parameters depending on the C<$phase>:
+
+=over
+
+=item C<start>
+
+=over
+
+=item C<< $info->{'start-time'} >>
+
+Unix time-stamp of when the guest backup started.
+
+=back
+
+=item C<end>
+
+No additional information.
+
+=item C<abort>
+
+=over
+
+=item C<< $info->{error} >>
+
+The error message indicating the failure.
+
+=back
+
+=back
+
+=back
+
+=back
+
+=cut
+sub backup_hook {
+    my ($self, $phase, $vmid, $vmtype, $info) = @_;
+
+    die "implement me in subclass";
+}
+
+=pod
+
+=over
+
+=item C<backup_get_mechanism>
+
+Tell the caller what mechanism to use for backing up the guest. The backup
+method for the guest, i.e. C<backup_vm> for guest type C<qemu> or
+C<backup_container> for guest type C<lxc>, will later be called with
+mechanism-specific information. See those methods for more information. Returns
+C<($mechanism, $bitmap_id)>:
+
+=over
+
+=item C<$mechanism>
+
+Currently C<nbd> and C<block-device> for guest type C<qemu> and C<directory>
+for guest type C<lxc> are possible. If there is no support for one of the guest
+types, the method should either C<die> or return C<undef>.
+
+=item C<$bitmap_id>
+
+If the backup provider supports backing up with a bitmap, the ID of the bitmap
+to use. Return C<undef> otherwise. Re-use the same ID multiple times for
+incremental backup.
+
+=back
+
+Parameters:
+
+=over
+
+=item C<$vmid>
+
+The ID of the guest being backed up.
+
+=item C<$vmtype>
+
+The type of the guest being backed up. Currently, either C<qemu> or C<lxc>.
+
+=back
+
+=back
+
+=cut
+sub backup_get_mechanism {
+    my ($self, $vmid, $vmtype) = @_;
+
+    die "implement me in subclass";
+}
+
+=pod
+
+=over
+
+=item C<backup_get_archive_name>
+
+The archive name of the backup archive that will be created by the current
+backup. The returned value needs to be the volume name that the archive can
+later be accessed by via the corresponding storage plugin, i.e. C<$archive_name>
+in the volume ID C<"${storeid}:backup/${archive_name}">.
+
+Parameters:
+
+=over
+
+=item C<$vmid>
+
+The ID of the guest being backed up.
+
+=item C<$vmtype>
+
+The type of the guest being backed up. Currently, either C<qemu> or C<lxc>.
+
+=item C<$backup_time>
+
+Unix time-stamp of when the guest backup started.
+
+=back
+
+=back
+
+=cut
+sub backup_get_archive_name {
+    my ($self, $vmid, $vmtype, $backup_time) = @_;
+
+    die "implement me in subclass";
+}
+
+=pod
+
+=over
+
+=item C<backup_get_task_size>
+
+Returns the size of the backup after completion.
+
+Parameters:
+
+=over
+
+=item C<$vmid>
+
+The ID of the guest being backed up.
+
+=back
+
+=back
+
+=cut
+sub backup_get_task_size {
+    my ($self, $vmid) = @_;
+
+    die "implement me in subclass";
+}
+
+=pod
+
+=over
+
+=item C<backup_handle_log_file>
+
+Handle the backup's log file which contains the task log for the backup. For
+example, a provider might want to upload a copy to the backup server.
+
+Parameters:
+
+=over
+
+=item C<$vmid>
+
+The ID of the guest being backed up.
+
+=item C<$filename>
+
+Path to the file with the backup log.
+
+=back
+
+=back
+
+=cut
+sub backup_handle_log_file {
+    my ($self, $vmid, $filename) = @_;
+
+    die "implement me in subclass";
+}
+
+=pod
+
+=over
+
+=item C<backup_vm>
+
+Used when the guest type is C<qemu>. Back up the virtual machine's configuration
+and volumes that were made available according to the mechanism returned by
+C<backup_get_mechanism>. Returns when done backing up. Ideally, the method
+should log the progress during backup.
+
+Parameters:
+
+=over
+
+=item C<$vmid>
+
+The ID of the guest being backed up.
+
+=item C<$config_filename>
+
+Path to the file with the guest configuration.
+
+=item C<$volumes>
+
+Hash reference with information about the VM's volumes. Some parameters are
+mechanism-specific.
+
+=over
+
+=item C<< $volumes->{$devicename} >>
+
+Hash reference with information about the VM volume associated to
+the device C<$devicename>. The device name needs to be remembered for restoring.
+The device name is also the name of the NBD export when the C<nbd> mechanism is
+used.
+
+=item C<< $volumes->{$devicename}->{size} >>
+
+Size of the volume in bytes.
+
+=item C<< $volumes->{$devicename}->{'bitmap-mode'} >>
+
+How a bitmap is used for the current volume.
+
+=over
+
+=item C<none>
+
+No bitmap is used.
+
+=item C<new>
+
+A bitmap has been newly created on the volume.
+
+=item C<reuse>
+
+The bitmap with the same ID as requested is being re-used.
+
+=back
+
+=back
+
+Mechansims-specific parameters for mechanism:
+
+=over
+
+=item C<block-device>
+
+=over
+
+=item C<< $volumes->{$devicename}->{path} >>
+
+Path to the block device with the backup data.
+
+=item C<< $volumes->{$devicename}->{'next-dirty-region'} >>
+
+A function that will return the offset and length of the next dirty region as a
+two-element list. After the last dirty region, it will return C<undef>. If no
+bitmap is used, it will return C<(0, $size)> and then C<undef>. If a bitmap is
+used, these are the dirty regions according to the bitmap.
+
+=back
+
+=item C<nbd>
+
+=over
+
+=item C<< $volumes->{$devicename}->{'nbd-path'} >>
+
+The path to the Unix socket providing the NBD export with the backup data and,
+if a bitmap is used, bitmap data.
+
+=item C<< $volumes->{$devicename}->{'bitmap-name'} >>
+
+The name of the bitmap in case a bitmap is used.
+
+=back
+
+=back
+
+=item C<$info>
+
+A hash reference containing optional parameters.
+
+Optional parameters:
+
+=over
+
+=item C<< $info->{'bandwidth-limit'} >>
+
+The requested bandwith limit. The value is in bytes/second. The backup provider
+is expected to honor this rate limit for IO on the backup source and network
+traffic. A value of C<0>, C<undef> or if there is no such key in the hash all
+mean that there is no limit.
+
+=item C<< $info->{'firewall-config'} >>
+
+Present if the firewall configuration exists. Path to the file with the guest's
+firewall configuration.
+
+=back
+
+=back
+
+=back
+
+=cut
+sub backup_vm {
+    my ($self, $vmid, $config_filename, $volumes, $info) = @_;
+
+    die "implement me in subclass";
+}
+
+=pod
+
+=over
+
+=item C<backup_container>
+
+Used when the guest type is C<lxc>. Back up the container filesystem structure
+that is made available for the mechanism returned by C<backup_get_mechanism>.
+Returns when done backing up. Ideally, the method should log the progress during
+backup.
+
+Parameters:
+
+=over
+
+=item C<$vmid>
+
+The ID of the guest being backed up.
+
+=item C<$config_filename>
+
+Path to the file with the guest configuration.
+
+=item C<$id_map>
+
+A list of UID/GID mappings for the container, each mapping is itself a list with
+four entries, e.g. C<["u", "0", "100000", "65536"]>, namely:
+
+1. a character: C<'u'> (for a user mapping) or C<'g'> (for a group mapping)
+
+2. the first userid in the user namespace
+
+3. the first userid as seen on the host
+
+4. the number of ids to be mapped.
+
+=item C<$exclude_patterns>
+
+A list of glob patterns of files and directories to be excluded. C<**> is used
+to match current directory and subdirectories. See also the following (note
+that PBS implements more than required here, like explicit inclusion when
+starting with a C<!>):
+L<vzdump documentation|https://pve.proxmox.com/pve-docs/chapter-vzdump.html#_file_exclusions>
+and
+L<PBS documentation|https://pbs.proxmox.com/docs/backup-client.html#excluding-files-directories-from-a-backup>
+
+=item C<$info>
+
+A hash reference containing optional and mechanism-specific parameters.
+
+Optional parameters:
+
+=over
+
+=item C<< $info->{'bandwidth-limit'} >>
+
+The requested bandwith limit. The value is in bytes/second. The backup provider
+is expected to honor this rate limit for IO on the backup source and network
+traffic. A value of C<0>, C<undef> or if there is no such key in the hash all
+mean that there is no limit.
+
+=item C<< $info->{'firewall-config'} >>
+
+Present if the firewall configuration exists. Path to the file with the guest's
+firewall configuration.
+
+=back
+
+Mechansims-specific parameters for mechanism:
+
+=over
+
+=item C<directory>
+
+=over
+
+=item C<< $info->{directory} >>
+
+Path to the directory with the container's file system structure.
+
+=item C<< $info->{sources} >>
+
+List of paths (for separate mount points, including "." for the root) inside the
+directory to be backed up.
+
+=back
+
+=back
+
+=back
+
+=back
+
+=cut
+sub backup_container {
+    my ($self, $vmid, $config_filename, $id_map, $exclude_patterns, $info) = @_;
+
+    die "implement me in subclass";
+}
+
+=pod
+
+=over
+
+=item C<restore_get_mechanism>
+
+Tell the caller what mechanism to use for restoring the guest. The restore
+methods for the guest, i.e. C<restore_qemu_img_init> and
+C<restore_qemu_img_cleanup> for guest type C<qemu>, or C<restore_container_init>
+and C<restore_container_cleanup> for guest type C<lxc> will be called with
+mechanism-specific information and their return value might also depend on the
+mechanism. See those methods for more information. Returns
+C<($mechanism, $vmtype)>:
+
+=over
+
+=item C<$mechanism>
+
+Currently, C<'qemu-img'> for guest type C<'qemu'> and either C<'tar'> or
+C<'directory'> for type C<'lxc'> are possible.
+
+=item C<$vmtype>
+
+Either C<qemu> or C<lxc> depending on what type the guest in the backed-up
+archive is.
+
+=back
+
+Parameters:
+
+=over
+
+=item C<$volname>
+
+The volume ID of the archive being restored.
+
+=item C<$storeid>
+
+The storage ID of the backup storage.
+
+=back
+
+=back
+
+=cut
+sub restore_get_mechanism {
+    my ($self, $volname, $storeid) = @_;
+
+    die "implement me in subclass";
+}
+
+=pod
+
+=over
+
+=item C<restore_get_guest_config>
+
+Extract the guest configuration from the given backup. Returns the raw contents
+of the backed-up configuration file.
+
+Parameters:
+
+=over
+
+=item C<$volname>
+
+The volume ID of the archive being restored.
+
+=item C<$storeid>
+
+The storage ID of the backup storage.
+
+=back
+
+=back
+
+=cut
+sub restore_get_guest_config {
+    my ($self, $volname, $storeid) = @_;
+
+    die "implement me in subclass";
+}
+
+=pod
+
+=over
+
+=item C<restore_get_firewall_config>
+
+Extract the guest's firewall configuration from the given backup. Returns the
+raw contents of the backed-up configuration file. Returns C<undef> if there is
+no firewall config in the archive, C<die> if the configuration can't be
+extracted.
+
+Parameters:
+
+=over
+
+=item C<$volname>
+
+The volume ID of the archive being restored.
+
+=item C<$storeid>
+
+The storage ID of the backup storage.
+
+=back
+
+=back
+
+=cut
+sub restore_get_firewall_config {
+    my ($self, $volname, $storeid) = @_;
+
+    die "implement me in subclass";
+}
+
+=pod
+
+=over
+
+=item C<restore_vm_init>
+
+Prepare a VM archive for restore. Returns the basic information about the
+volumes in the backup as a hash reference with the following structure:
+
+    {
+	$devicenameA => { size => $sizeA },
+	$devicenameB => { size => $sizeB },
+	...
+    }
+
+=over
+
+=item C<$devicename>
+
+The device name that was given as an argument to the backup routine when the
+backup was created.
+
+=item C<$size>
+
+The virtual size of the VM volume that was backed up. A volume with this size is
+created for the restore operation. In particular, for the C<qemu-img> mechanism,
+this should be the size of the block device referenced by the C<qemu-img-path>
+returned by C<restore_vm_volume>.
+
+=back
+
+Parameters:
+
+=over
+
+=item C<$volname>
+
+The volume ID of the archive being restored.
+
+=item C<$storeid>
+
+The storage ID of the backup storage.
+
+=back
+
+=back
+
+=cut
+sub restore_vm_init {
+    my ($self, $volname, $storeid) = @_;
+
+    die "implement me in subclass";
+}
+
+=pod
+
+=over
+
+=item C<restore_vm_cleanup>
+
+For VM backups, clean up after the restore. Called in both, success and
+failure scenarios.
+
+Parameters:
+
+=over
+
+=item C<$volname>
+
+The volume ID of the archive being restored.
+
+=item C<$storeid>
+
+The storage ID of the backup storage.
+
+=back
+
+=back
+
+=cut
+sub restore_vm_cleanup {
+    my ($self, $volname, $storeid) = @_;
+
+    die "implement me in subclass";
+}
+
+=pod
+
+=over
+
+=item C<restore_vm_volume_init>
+
+Prepare a VM volume in the archive for restore. Returns a hash reference with
+the mechanism-specific information for the restore:
+
+=over
+
+=item C<qemu-img>
+
+    { 'qemu-img-path' => $path }
+
+The volume will be restored using the C<qemu-img convert> command.
+
+=over
+
+=item C<$path>
+
+A path to the volume that C<qemu-img> can use as a source for the
+C<qemu-img convert> command. E.g. this could also be an NBD URI.
+
+=back
+
+=back
+
+Parameters:
+
+=over
+
+=item C<$volname>
+
+The volume ID of the archive being restored.
+
+=item C<$storeid>
+
+The storage ID of the backup storage.
+
+=item C<$devicename>
+
+The device name associated to the volume that should be prepared for the
+restore. Same as the argument to the backup routine when the backup was created.
+
+=item C<$info>
+
+A hash reference with optional and mechanism-specific parameters. Currently
+empty.
+
+=back
+
+=back
+
+=cut
+sub restore_vm_volume_init {
+    my ($self, $volname, $storeid, $devicename, $info) = @_;
+
+    die "implement me in subclass";
+}
+
+=pod
+
+=over
+
+=item C<restore_vm_volume_cleanup>
+
+For VM backups, clean up after the restore of a given volume. Called in both,
+success and failure scenarios.
+
+Parameters:
+
+=over
+
+=item C<$volname>
+
+The volume ID of the archive being restored.
+
+=item C<$storeid>
+
+The storage ID of the backup storage.
+
+=item C<$devicename>
+
+The device name associated to the volume that should be prepared for the
+restore. Same as the argument to the backup routine when the backup was created.
+
+=item C<$info>
+
+A hash reference with optional and mechanism-specific parameters. Currently
+empty.
+
+=back
+
+=back
+
+=cut
+sub restore_vm_volume_cleanup {
+    my ($self, $volname, $storeid, $devicename, $info) = @_;
+
+    die "implement me in subclass";
+}
+
+=pod
+
+=over
+
+=item C<restore_container_init>
+
+Prepare a container archive for restore. Returns a hash reference with the
+mechanism-specific information for the restore:
+
+=over
+
+=item C<tar>
+
+    { 'tar-path' => $path }
+
+The archive will be restored via the C<tar> command.
+
+=over
+
+=item C<$path>
+
+The path to the tar archive containing the full filesystem structure of the
+container.
+
+=back
+
+=item C<directory>
+
+    { 'archive-directory' => $path }
+
+The archive will be restored via C<rsync> from a directory containing the full
+filesystem structure of the container.
+
+=over
+
+=item C<$path>
+
+The path to the directory containing the full filesystem structure of the
+container.
+
+=back
+
+=back
+
+Parameters:
+
+=over
+
+=item C<$volname>
+
+The volume ID of the archive being restored.
+
+=item C<$storeid>
+
+The storage ID of the backup storage.
+
+=item C<$info>
+
+A hash reference with optional and mechanism-specific parameters. Currently
+empty.
+
+=back
+
+=back
+
+=cut
+sub restore_container_init {
+    my ($self, $volname, $storeid, $info) = @_;
+
+    die "implement me in subclass";
+}
+
+=pod
+
+=over
+
+=item C<restore_container_cleanup>
+
+For container backups, clean up after the restore. Called in both, success and
+failure scenarios.
+
+Parameters:
+
+=over
+
+=item C<$volname>
+
+The volume ID of the archive being restored.
+
+=item C<$storeid>
+
+The storage ID of the backup storage.
+
+=item C<$info>
+
+A hash reference with optional and mechanism-specific parameters. Currently
+empty.
+
+=back
+
+=back
+
+=cut
+sub restore_container_cleanup {
+    my ($self, $volname, $storeid, $info) = @_;
+
+    die "implement me in subclass";
+}
+
+1;
diff --git a/src/PVE/BackupProvider/Plugin/Makefile b/src/PVE/BackupProvider/Plugin/Makefile
new file mode 100644
index 0000000..bbd7431
--- /dev/null
+++ b/src/PVE/BackupProvider/Plugin/Makefile
@@ -0,0 +1,5 @@
+SOURCES = Base.pm
+
+.PHONY: install
+install:
+	for i in ${SOURCES}; do install -D -m 0644 $$i ${DESTDIR}${PERLDIR}/PVE/BackupProvider/Plugin/$$i; done
diff --git a/src/PVE/Makefile b/src/PVE/Makefile
index d438804..8605a40 100644
--- a/src/PVE/Makefile
+++ b/src/PVE/Makefile
@@ -5,6 +5,7 @@ install:
 	install -D -m 0644 Storage.pm ${DESTDIR}${PERLDIR}/PVE/Storage.pm
 	install -D -m 0644 Diskmanage.pm ${DESTDIR}${PERLDIR}/PVE/Diskmanage.pm
 	install -D -m 0644 CephConfig.pm ${DESTDIR}${PERLDIR}/PVE/CephConfig.pm
+	make -C BackupProvider install
 	make -C Storage install
 	make -C API2 install
 	make -C CLI install
diff --git a/src/PVE/Storage.pm b/src/PVE/Storage.pm
index 57b2038..aea57ab 100755
--- a/src/PVE/Storage.pm
+++ b/src/PVE/Storage.pm
@@ -42,11 +42,11 @@ use PVE::Storage::BTRFSPlugin;
 use PVE::Storage::ESXiPlugin;
 
 # Storage API version. Increment it on changes in storage API interface.
-use constant APIVER => 10;
+use constant APIVER => 11;
 # Age is the number of versions we're backward compatible with.
 # This is like having 'current=APIVER' and age='APIAGE' in libtool,
 # see https://www.gnu.org/software/libtool/manual/html_node/Libtool-versioning.html
-use constant APIAGE => 1;
+use constant APIAGE => 2;
 
 our $KNOWN_EXPORT_FORMATS = ['raw+size', 'tar+size', 'qcow2+size', 'vmdk+size', 'zfs', 'btrfs'];
 
@@ -1994,6 +1994,14 @@ sub volume_export_start {
     PVE::Tools::run_command($cmds, %$run_command_params);
 }
 
+sub new_backup_provider {
+    my ($cfg, $storeid, $log_function) = @_;
+
+    my $scfg = storage_config($cfg, $storeid);
+    my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
+    return $plugin->new_backup_provider($scfg, $storeid, $log_function);
+}
+
 # bash completion helper
 
 sub complete_storage {
diff --git a/src/PVE/Storage/Plugin.pm b/src/PVE/Storage/Plugin.pm
index 6444390..d5b76ae 100644
--- a/src/PVE/Storage/Plugin.pm
+++ b/src/PVE/Storage/Plugin.pm
@@ -1755,6 +1755,21 @@ sub rename_volume {
     return "${storeid}:${base}${target_vmid}/${target_volname}";
 }
 
+# Used by storage plugins for external backup providers. See PVE::BackupProvider::Plugin for the API
+# the provider needs to implement.
+#
+# $scfg - the storage configuration
+# $storeid - the storage ID
+# $log_function($log_level, $message) - this log function can be used to write to the backup task
+#   log in Proxmox VE. $log_level is 'info', 'warn' or 'err', $message is the message to be printed.
+#
+# Returns a blessed reference to the backup provider class.
+sub new_backup_provider {
+    my ($class, $scfg, $storeid, $log_function) = @_;
+
+    return;
+}
+
 sub config_aware_base_mkdir {
     my ($class, $scfg, $path) = @_;
 
-- 
2.39.2



_______________________________________________
pve-devel mailing list
pve-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel


^ permalink raw reply	[flat|nested] 44+ messages in thread

* [pve-devel] [RFC storage v2 11/25] extract backup config: delegate to backup provider if there is one
  2024-08-13 13:28 [pve-devel] [RFC qemu/storage/qemu-server/container/manager v2 00/25] backup provider API Fiona Ebner
                   ` (9 preceding siblings ...)
  2024-08-13 13:28 ` [pve-devel] [RFC storage v2 10/25] plugin: introduce new_backup_provider() method Fiona Ebner
@ 2024-08-13 13:28 ` Fiona Ebner
  2024-08-13 13:28 ` [pve-devel] [POC storage v2 12/25] add backup provider example Fiona Ebner
                   ` (14 subsequent siblings)
  25 siblings, 0 replies; 44+ messages in thread
From: Fiona Ebner @ 2024-08-13 13:28 UTC (permalink / raw)
  To: pve-devel

Signed-off-by: Fiona Ebner <f.ebner@proxmox.com>
---

Changes in v2:
* Adapt to method rename.

 src/PVE/Storage.pm | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/src/PVE/Storage.pm b/src/PVE/Storage.pm
index aea57ab..8993ba7 100755
--- a/src/PVE/Storage.pm
+++ b/src/PVE/Storage.pm
@@ -1726,6 +1726,16 @@ sub extract_vzdump_config {
 	    storage_check_enabled($cfg, $storeid);
 	    return PVE::Storage::PBSPlugin->extract_vzdump_config($scfg, $volname, $storeid);
 	}
+
+	my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
+	my $log_function = sub {
+	    my ($log_level, $message) = @_;
+	    my $prefix = $log_level eq 'err' ? 'ERROR' : uc($log_level);
+	    print "$prefix: $message\n";
+	};
+	if (my $backup_provider = $plugin->new_backup_provider($scfg, $storeid, $log_function)) {
+	    return $backup_provider->restore_get_guest_config($volname, $storeid);
+	}
     }
 
     my $archive = abs_filesystem_path($cfg, $volid);
-- 
2.39.2



_______________________________________________
pve-devel mailing list
pve-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel


^ permalink raw reply	[flat|nested] 44+ messages in thread

* [pve-devel] [POC storage v2 12/25] add backup provider example
  2024-08-13 13:28 [pve-devel] [RFC qemu/storage/qemu-server/container/manager v2 00/25] backup provider API Fiona Ebner
                   ` (10 preceding siblings ...)
  2024-08-13 13:28 ` [pve-devel] [RFC storage v2 11/25] extract backup config: delegate to backup provider if there is one Fiona Ebner
@ 2024-08-13 13:28 ` Fiona Ebner
  2024-08-13 13:28 ` [pve-devel] [POC storage v2 13/25] Borg plugin Fiona Ebner
                   ` (13 subsequent siblings)
  25 siblings, 0 replies; 44+ messages in thread
From: Fiona Ebner @ 2024-08-13 13:28 UTC (permalink / raw)
  To: pve-devel

The example uses a simple directory structure to save the backups,
grouped by guest ID. VM backups are saved as configuration files and
qcow2 images, with backing files when doing incremental backups.
Container backups are saved as configuration files and a tar file or
squashfs image (added to test the 'directory' restore mechanism).

Whether to use incremental VM backups and which backup mechanisms to
use can be configured in the storage configuration.

The 'nbdinfo' binary from the 'libnbd-bin' package is required for
backup mechanism 'nbd' for VM backups, the 'mksquashfs' binary from
the 'squashfs-tools' package is required for backup mechanism
'squashfs' for containers.

Signed-off-by: Fiona Ebner <f.ebner@proxmox.com>
---

Changes in v2:
* Adapt to API changes.
* Add log function helpers.
* Use make_path and remove_tree instead of deprecated variants.
* Add support for 'block-device' backup mechanism for VMs.
* Use backup time for guest rather than the job start time in the
  archive name.

 .../BackupProvider/Plugin/DirectoryExample.pm | 694 ++++++++++++++++++
 src/PVE/BackupProvider/Plugin/Makefile        |   2 +-
 .../Custom/BackupProviderDirExamplePlugin.pm  | 306 ++++++++
 src/PVE/Storage/Custom/Makefile               |   5 +
 src/PVE/Storage/Makefile                      |   1 +
 5 files changed, 1007 insertions(+), 1 deletion(-)
 create mode 100644 src/PVE/BackupProvider/Plugin/DirectoryExample.pm
 create mode 100644 src/PVE/Storage/Custom/BackupProviderDirExamplePlugin.pm
 create mode 100644 src/PVE/Storage/Custom/Makefile

diff --git a/src/PVE/BackupProvider/Plugin/DirectoryExample.pm b/src/PVE/BackupProvider/Plugin/DirectoryExample.pm
new file mode 100644
index 0000000..b01ad02
--- /dev/null
+++ b/src/PVE/BackupProvider/Plugin/DirectoryExample.pm
@@ -0,0 +1,694 @@
+package PVE::BackupProvider::Plugin::DirectoryExample;
+
+use strict;
+use warnings;
+
+use Fcntl qw(SEEK_SET);
+use File::Path qw(make_path remove_tree);
+use IO::File;
+use IPC::Open3;
+
+use PVE::Storage::Plugin;
+use PVE::Tools qw(file_get_contents file_read_firstline file_set_contents run_command);
+
+use base qw(PVE::BackupProvider::Plugin::Base);
+
+use constant {
+    BLKDISCARD => 0x1277, # see linux/fs.h
+};
+
+# Private helpers
+
+my sub log_info {
+    my ($self, $message) = @_;
+
+    $self->{'log-function'}->('info', $message);
+}
+
+my sub log_warning {
+    my ($self, $message) = @_;
+
+    $self->{'log-function'}->('warn', $message);
+}
+
+my sub log_error {
+    my ($self, $message) = @_;
+
+    $self->{'log-function'}->('err', $message);
+}
+
+# Try to use the same bitmap ID as last time for incremental backup if the storage is configured for
+# incremental VM backup. Need to start fresh if there is no previous ID or the associated backup
+# doesn't exist.
+my sub get_bitmap_id {
+    my ($self, $vmid, $vmtype) = @_;
+
+    return if $self->{'storage-plugin'}->get_vm_backup_mode($self->{scfg}) ne 'incremental';
+
+    my $previous_info_dir = "$self->{scfg}->{path}/$vmid/";
+
+    my $previous_info_file = "$previous_info_dir/previous-info";
+    my $info = file_read_firstline($previous_info_file) // '';
+    $self->{$vmid}->{'old-previous-info'} = $info;
+    my ($bitmap_id, $previous_backup_id) = $info =~ m/^(\d+)\s+(\d+)$/;
+    my $previous_backup_dir =
+	$previous_backup_id ? "$self->{scfg}->{path}/$vmid/$vmtype-$previous_backup_id" : undef;
+
+    if ($bitmap_id && -d $previous_backup_dir) {
+	$self->{$vmid}->{'previous-backup-dir'} = $previous_backup_dir;
+    } else {
+	# need to start fresh if there is no previous ID or the associated backup doesn't exist
+	$bitmap_id = $self->{$vmid}->{'backup-time'};
+    }
+
+    $self->{$vmid}->{'bitmap-id'} = $bitmap_id;
+    make_path($previous_info_dir);
+    die "unable to create directory $previous_info_dir\n" if !-d $previous_info_dir;
+    file_set_contents($previous_info_file, "$bitmap_id $self->{$vmid}->{'backup-time'}");
+
+    return $bitmap_id;
+}
+
+# Backup Provider API
+
+sub new {
+    my ($class, $storage_plugin, $scfg, $storeid, $log_function) = @_;
+
+    my $self = bless {
+	scfg => $scfg,
+	storeid => $storeid,
+	'storage-plugin' => $storage_plugin,
+	'log-function' => $log_function,
+    }, $class;
+
+    return $self;
+}
+
+sub provider_name {
+    my ($self) = @_;
+
+    return 'dir provider example';
+}
+
+# Hooks
+
+my sub job_start {
+    my ($self, $start_time) = @_;
+
+    log_info($self, "job start hook called");
+
+    run_command(["modprobe", "nbd"]);
+
+    log_info($self, "backup provider initialized successfully for new job $start_time");
+}
+
+sub job_hook {
+    my ($self, $phase, $info) = @_;
+
+    if ($phase eq 'start') {
+	job_start($self, $info->{'start-time'});
+    } elsif ($phase eq 'end') {
+	log_info($self, "job end hook called");
+    } elsif ($phase eq 'abort') {
+	log_info($self, "job abort hook called with error - $info->{error}");
+    }
+
+    # ignore unknown phase
+
+    return;
+}
+
+my sub backup_start {
+    my ($self, $vmid, $vmtype, $backup_time) = @_;
+
+    log_info($self, "backup start hook called");
+
+    my $backup_dir = $self->{scfg}->{path} . "/" . $self->{$vmid}->{archive};
+
+    make_path($backup_dir);
+    die "unable to create directory $backup_dir\n" if !-d $backup_dir;
+
+    $self->{$vmid}->{'backup-time'} = $backup_time;
+    $self->{$vmid}->{'backup-dir'} = $backup_dir;
+    $self->{$vmid}->{'task-size'} = 0;
+}
+
+my sub backup_abort {
+    my ($self, $vmid, $error) = @_;
+
+    log_info($self, "backup abort hook called");
+
+    $self->{$vmid}->{failed} = 1;
+
+
+    if (my $dir = $self->{$vmid}->{'backup-dir'}) {
+	eval { remove_tree($dir) };
+	$self->{'log-warning'}->("unable to clean up $dir - $@") if $@;
+    }
+
+    # Restore old previous-info so next attempt can re-use bitmap again
+    if (my $info = $self->{$vmid}->{'old-previous-info'}) {
+	my $previous_info_dir = "$self->{scfg}->{path}/$vmid/";
+	my $previous_info_file = "$previous_info_dir/previous-info";
+	file_set_contents($previous_info_file, $info);
+    }
+}
+
+sub backup_hook {
+    my ($self, $phase, $vmid, $vmtype, $info) = @_;
+
+    if ($phase eq 'start') {
+	backup_start($self, $vmid, $vmtype, $info->{'start-time'});
+    } elsif ($phase eq 'end') {
+	log_info($self, "backup end hook called");
+    } elsif ($phase eq 'abort') {
+	backup_abort($self, $vmid, $info->{error});
+    }
+
+    # ignore unknown phase
+
+    return;
+}
+
+sub backup_get_mechanism {
+    my ($self, $vmid, $vmtype) = @_;
+
+    return ('directory', undef) if $vmtype eq 'lxc';
+
+    if ($vmtype eq 'qemu') {
+	my $backup_mechanism = $self->{'storage-plugin'}->get_vm_backup_mechanism($self->{scfg});
+	return ($backup_mechanism, get_bitmap_id($self, $vmid, $vmtype));
+    }
+
+    die "unsupported guest type '$vmtype'\n";
+}
+
+sub backup_get_archive_name {
+    my ($self, $vmid, $vmtype, $backup_time) = @_;
+
+    return $self->{$vmid}->{archive} = "${vmid}/${vmtype}-${backup_time}";
+}
+
+sub backup_get_task_size {
+    my ($self, $vmid) = @_;
+
+    return $self->{$vmid}->{'task-size'};
+}
+
+sub backup_handle_log_file {
+    my ($self, $vmid, $filename) = @_;
+
+    my $log_dir = $self->{$vmid}->{'backup-dir'};
+    if ($self->{$vmid}->{failed}) {
+	$log_dir .= ".failed";
+    }
+    make_path($log_dir);
+    die "unable to create directory $log_dir\n" if !-d $log_dir;
+
+    my $data = file_get_contents($filename);
+    my $target = "${log_dir}/backup.log";
+    file_set_contents($target, $data);
+}
+
+my sub backup_block_device {
+    my ($self, $vmid, $devicename, $size, $path, $bitmap_mode, $next_dirty_region, $bandwidth_limit) = @_;
+
+    # TODO honor bandwidth_limit
+
+    my $previous_backup_dir = $self->{$vmid}->{'previous-backup-dir'};
+    my $incremental = $previous_backup_dir && $bitmap_mode eq 'reuse';
+    my $target = "$self->{$vmid}->{'backup-dir'}/${devicename}.qcow2";
+    my $target_base = $incremental ? "${previous_backup_dir}/${devicename}.qcow2" : undef;
+    my $create_cmd = ["qemu-img", "create", "-f", "qcow2", $target, $size];
+    push $create_cmd->@*, "-b", $target_base, "-F", "qcow2" if $target_base;
+    run_command($create_cmd);
+
+    eval {
+	# allows to easily write to qcow2 target
+	run_command(["qemu-nbd", "-c", "/dev/nbd15", $target, "--format=qcow2"]);
+
+	my $block_size = 4 * 1024 * 1024; # 4 MiB
+
+	my $in_fh = IO::File->new($path, "r+")
+	    or die "unable to open NBD backup source - $!\n";
+	my $out_fh = IO::File->new("/dev/nbd15", "r+")
+	    or die "unable to open NBD backup target - $!\n";
+
+	my $buffer = '';
+
+	while (scalar((my $region_offset, my $region_length) = $next_dirty_region->())) {
+	    sysseek($in_fh, $region_offset, SEEK_SET)
+		// die "unable to seek '$region_offset' in NBD backup source - $!";
+	    sysseek($out_fh, $region_offset, SEEK_SET)
+		// die "unable to seek '$region_offset' in NBD backup target - $!";
+
+	    my $local_offset = 0; # within the region
+	    while ($local_offset < $region_length) {
+		my $remaining = $region_length - $local_offset;
+		my $request_size = $remaining < $block_size ? $remaining : $block_size;
+		my $offset = $region_offset + $local_offset;
+
+		my $read = sysread($in_fh, $buffer, $request_size);
+
+		die "failed to read from backup source - $!\n" if !defined($read);
+		die "premature EOF while reading backup source\n" if $read == 0;
+
+		my $written = 0;
+		while ($written < $read) {
+		    my $res = syswrite($out_fh, $buffer, $request_size - $written, $written);
+		    die "failed to write to backup target - $!\n" if !defined($res);
+		    die "unable to progress writing to backup target\n" if $res == 0;
+		    $written += $res;
+		}
+
+		ioctl($in_fh, BLKDISCARD, pack('QQ', int($offset), int($request_size)));
+
+		$local_offset += $request_size;
+	    }
+	}
+    };
+    my $err = $@;
+
+    eval { run_command(["qemu-nbd", "-d", "/dev/nbd15" ]); };
+    $self->{'log-warning'}->("unable to disconnect NBD backup target - $@") if $@;
+
+    die $err if $err;
+}
+
+my sub backup_nbd {
+    my ($self, $vmid, $devicename, $size, $nbd_path, $bitmap_mode, $bitmap_name, $bandwidth_limit) = @_;
+
+    # TODO honor bandwidth_limit
+
+    die "need 'nbdinfo' binary from package libnbd-bin\n" if !-e "/usr/bin/nbdinfo";
+
+    my $nbd_info_uri = "nbd+unix:///${devicename}?socket=${nbd_path}";
+    my $qemu_nbd_uri = "nbd:unix:${nbd_path}:exportname=${devicename}";
+
+    my $cpid;
+    my $error_fh;
+    my $next_dirty_region;
+
+    # If there is no dirty bitmap, it can be treated as if there's a full dirty one. The output of
+    # nbdinfo is a list of tuples with offset, length, type, description. The first bit of 'type' is
+    # set when the bitmap is dirty, see QEMU's docs/interop/nbd.txt
+    my $dirty_bitmap = [];
+    if ($bitmap_mode ne 'none') {
+	my $input = IO::File->new();
+	my $info = IO::File->new();
+	$error_fh = IO::File->new();
+	my $nbdinfo_cmd = ["nbdinfo", $nbd_info_uri, "--map=qemu:dirty-bitmap:${bitmap_name}"];
+	$cpid = open3($input, $info, $error_fh, $nbdinfo_cmd->@*)
+	    or die "failed to spawn nbdinfo child - $!\n";
+
+	$next_dirty_region = sub {
+	    my ($offset, $length, $type);
+	    do {
+		my $line = <$info>;
+		return if !$line;
+		die "unexpected output from nbdinfo - $line\n"
+		    if $line !~ m/^\s*(\d+)\s*(\d+)\s*(\d+)/; # also untaints
+		($offset, $length, $type) = ($1, $2, $3);
+	    } while (($type & 0x1) == 0); # not dirty
+	    return ($offset, $length);
+	};
+    } else {
+	my $done = 0;
+	$next_dirty_region = sub {
+	    return if $done;
+	    $done = 1;
+	    return (0, $size);
+	};
+    }
+
+    eval {
+	run_command(["qemu-nbd", "-c", "/dev/nbd0", $qemu_nbd_uri, "--format=raw", "--discard=on"]);
+
+	backup_block_device(
+	    $self,
+	    $vmid,
+	    $devicename,
+	    $size,
+	    '/dev/nbd0',
+	    $bitmap_mode,
+	    $next_dirty_region,
+	    $bandwidth_limit,
+	);
+    };
+    my $err = $@;
+
+    eval { run_command(["qemu-nbd", "-d", "/dev/nbd0" ]); };
+    $self->{'log-warning'}->("unable to disconnect NBD backup source - $@") if $@;
+
+    if ($cpid) {
+	my $waited;
+	my $wait_limit = 5;
+	for ($waited = 0; $waited < $wait_limit && waitpid($cpid, POSIX::WNOHANG) == 0; $waited++) {
+	    kill 15, $cpid if $waited == 0;
+	    sleep 1;
+	}
+	if ($waited == $wait_limit) {
+	    kill 9, $cpid;
+	    sleep 1;
+	    $self->{'log-warning'}->("unable to collect nbdinfo child process")
+		if waitpid($cpid, POSIX::WNOHANG) == 0;
+	}
+    }
+
+    die $err if $err;
+}
+
+my sub backup_vm_volume {
+    my ($self, $vmid, $devicename, $info, $bandwidth_limit) = @_;
+
+    my $backup_mechanism = $self->{'storage-plugin'}->get_vm_backup_mechanism($self->{scfg});
+
+    if ($backup_mechanism eq 'nbd') {
+	backup_nbd(
+	    $self,
+	    $vmid,
+	    $devicename,
+	    $info->{size},
+	    $info->{'nbd-path'},
+	    $info->{'bitmap-mode'},
+	    $info->{'bitmap-name'},
+	    $bandwidth_limit,
+	);
+    } elsif ($backup_mechanism eq 'block-device') {
+	backup_block_device(
+	    $self,
+	    $vmid,
+	    $devicename,
+	    $info->{size},
+	    $info->{path},
+	    $info->{'bitmap-mode'},
+	    $info->{'next-dirty-region'},
+	    $bandwidth_limit,
+	);
+    } else {
+	die "internal error - unknown VM backup mechansim '$backup_mechanism'\n";
+    }
+}
+
+sub backup_vm {
+    my ($self, $vmid, $config_filename, $volumes, $info) = @_;
+
+    my $data = file_get_contents($config_filename);
+    my $target = "$self->{$vmid}->{'backup-dir'}/guest.conf";
+    file_set_contents($target, $data);
+
+    $self->{$vmid}->{'task-size'} += -s $target;
+
+    if (my $firewall_file = $info->{'firewall-config'}) {
+	$data = file_get_contents($firewall_file);
+	$target = "$self->{$vmid}->{'backup-dir'}/firewall.conf";
+	file_set_contents($target, $data);
+
+	$self->{$vmid}->{'task-size'} += -s $target;
+    }
+
+    for my $devicename (sort keys $volumes->%*) {
+	backup_vm_volume(
+	    $self, $vmid, $devicename, $volumes->{$devicename}, $info->{'bandwidth-limit'});
+    }
+}
+
+my sub backup_directory_tar {
+    my ($self, $vmid, $directory, $userns_cmd, $exclude_patterns, $sources, $bandwidth_limit) = @_;
+
+    # essentially copied from PVE/VZDump/LXC.pm' archive()
+
+    # copied from PVE::Storage::Plugin::COMMON_TAR_FLAGS
+    my @tar_flags = qw(
+	--one-file-system
+	-p --sparse --numeric-owner --acls
+	--xattrs --xattrs-include=user.* --xattrs-include=security.capability
+	--warning=no-file-ignored --warning=no-xattr-write
+    );
+
+    my $tar = [$userns_cmd->@*, 'tar', 'cpf', '-', '--totals', @tar_flags];
+
+    push @$tar, "--directory=$directory";
+
+    my @exclude_no_anchored = ();
+    my @exclude_anchored = ();
+    for my $pattern ($exclude_patterns->@*) {
+	if ($pattern !~ m|^/|) {
+	    push @exclude_no_anchored, $pattern;
+	} else {
+	    push @exclude_anchored, $pattern;
+	}
+    }
+
+    push @$tar, '--no-anchored';
+    push @$tar, '--exclude=lost+found' if scalar($userns_cmd->@*) > 0;
+    push @$tar, map { "--exclude=$_" } @exclude_no_anchored;
+
+    push @$tar, '--anchored';
+    push @$tar, map { "--exclude=.$_" } @exclude_anchored;
+
+    push @$tar, $sources->@*;
+
+    my $cmd = [ $tar ];
+
+    push @$cmd, [ 'cstream', '-t', $bandwidth_limit * 1024 ] if $bandwidth_limit;
+
+    my $target = "$self->{$vmid}->{'backup-dir'}/archive.tar";
+    push @{$cmd->[-1]}, \(">" . PVE::Tools::shellquote($target));
+
+    my $logfunc = sub {
+	my $line = shift;
+	log_info($self, "tar: $line");
+    };
+
+    PVE::Tools::run_command($cmd, logfunc => $logfunc);
+
+    return;
+};
+
+# NOTE This only serves as an example to illustrate the 'directory' restore mechanism. It is not
+# fleshed out properly, e.g. I didn't check if exclusion is compatible with
+# proxmox-backup-client/rsync or xattrs/ACL/etc. work as expected!
+my sub backup_directory_squashfs {
+    my ($self, $vmid, $directory, $exclude_patterns, $bandwidth_limit) = @_;
+
+    my $target = "$self->{$vmid}->{'backup-dir'}/archive.sqfs";
+
+    my $mksquashfs = ['mksquashfs', $directory, $target, '-quiet', '-no-progress'];
+
+    push $mksquashfs->@*, '-wildcards';
+
+    for my $pattern ($exclude_patterns->@*) {
+	if ($pattern !~ m|^/|) { # non-anchored
+	    push $mksquashfs->@*, '-e', "... $pattern";
+	} else { # anchored
+	    push $mksquashfs->@*, '-e', substr($pattern, 1); # need to strip leading slash
+	}
+    }
+
+    my $cmd = [ $mksquashfs ];
+
+    push @$cmd, [ 'cstream', '-t', $bandwidth_limit * 1024 ] if $bandwidth_limit;
+
+    my $logfunc = sub {
+	my $line = shift;
+	log_info($self, "mksquashfs: $line");
+    };
+
+    PVE::Tools::run_command($cmd, logfunc => $logfunc);
+
+    return;
+};
+
+sub backup_container {
+    my ($self, $vmid, $config_filename, $id_map, $exclude_patterns, $info) = @_;
+
+    my $data = file_get_contents($config_filename);
+    my $target = "$self->{$vmid}->{'backup-dir'}/guest.conf";
+    file_set_contents($target, $data);
+
+    $self->{$vmid}->{'task-size'} += -s $target;
+
+    if (my $firewall_file = $info->{'firewall-config'}) {
+	$data = file_get_contents($firewall_file);
+	$target = "$self->{$vmid}->{'backup-dir'}/firewall.conf";
+	file_set_contents($target, $data);
+
+	$self->{$vmid}->{'task-size'} += -s $target;
+    }
+
+    my $userns_cmd = [];
+    # copied from PVE::LXC::userns_command
+    $userns_cmd = ['lxc-usernsexec', (map { ('-m', join(':', $_->@*)) } $id_map->@*), '--']
+	if scalar($id_map->@*) > 0;
+
+    my $backup_mode = $self->{'storage-plugin'}->get_lxc_backup_mode($self->{scfg});
+    if ($backup_mode eq 'tar') {
+	backup_directory_tar(
+	    $self,
+	    $vmid,
+	    $info->{directory},
+	    $userns_cmd,
+	    $exclude_patterns,
+	    $info->{sources},
+	    $info->{'bandwidth-limit'},
+	);
+    } elsif ($backup_mode eq 'squashfs') {
+	backup_directory_squashfs(
+	    $self,
+	    $vmid,
+	    $info->{directory},
+	    $exclude_patterns,
+	    $info->{'bandwidth-limit'},
+	);
+    } else {
+	die "got unexpected backup mode '$backup_mode' from storage plugin\n";
+    }
+}
+
+# Restore API
+
+sub restore_get_mechanism {
+    my ($self, $volname, $storeid) = @_;
+
+    my (undef, $relative_backup_dir) = $self->{'storage-plugin'}->parse_volname($volname);
+    my ($vmtype) = $relative_backup_dir =~ m!^\d+/([a-z]+)-!;
+
+    return ('qemu-img', $vmtype) if $vmtype eq 'qemu';
+
+    if ($vmtype eq 'lxc') {
+	my (undef, $relative_backup_dir) = $self->{'storage-plugin'}->parse_volname($volname);
+
+	if (-e "$self->{scfg}->{path}/${relative_backup_dir}/archive.tar") {
+	    $self->{'restore-mechanisms'}->{$volname} = 'tar';
+	    return ('tar', $vmtype);
+	}
+
+	if (-e "$self->{scfg}->{path}/${relative_backup_dir}/archive.sqfs") {
+	    $self->{'restore-mechanisms'}->{$volname} = 'directory';
+	    return ('directory', $vmtype)
+	}
+
+	die "unable to find archive '$volname'\n";
+    }
+
+    die "cannot restore unexpected guest type '$vmtype'\n";
+}
+
+sub restore_get_guest_config {
+    my ($self, $volname, $storeid) = @_;
+
+    my (undef, $relative_backup_dir) = $self->{'storage-plugin'}->parse_volname($volname);
+    my $filename = "$self->{scfg}->{path}/${relative_backup_dir}/guest.conf";
+
+    return file_get_contents($filename);
+}
+
+sub restore_get_firewall_config {
+    my ($self, $volname, $storeid) = @_;
+
+    my (undef, $relative_backup_dir) = $self->{'storage-plugin'}->parse_volname($volname);
+    my $filename = "$self->{scfg}->{path}/${relative_backup_dir}/firewall.conf";
+
+    return if !-e $filename;
+
+    return file_get_contents($filename);
+}
+
+sub restore_vm_init {
+    my ($self, $volname, $storeid) = @_;
+
+    my $res = {};
+
+    my (undef, $relative_backup_dir) = $self->{'storage-plugin'}->parse_volname($volname);
+    my $backup_dir = "$self->{scfg}->{path}/${relative_backup_dir}";
+
+    my @backup_files = glob("$backup_dir/*");
+    for my $backup_file (@backup_files) {
+	next if $backup_file !~ m!^(.*/(.*)\.qcow2)$!;
+	$backup_file = $1; # untaint
+	$res->{$2}->{size} = PVE::Storage::Plugin::file_size_info($backup_file);
+    }
+
+    return $res;
+}
+
+sub restore_vm_cleanup {
+    my ($self, $volname, $storeid) = @_;
+
+    return; # nothing to do
+}
+
+sub restore_vm_volume_init {
+    my ($self, $volname, $storeid, $devicename, $info) = @_;
+
+    my (undef, $relative_backup_dir) = $self->{'storage-plugin'}->parse_volname($volname);
+    return {
+	'qemu-img-path' => "$self->{scfg}->{path}/${relative_backup_dir}/${devicename}.qcow2",
+    };
+}
+
+sub restore_vm_volume_cleanup {
+    my ($self, $volname, $storeid, $devicename, $info) = @_;
+
+    return;
+}
+
+my sub restore_tar_init {
+    my ($self, $volname, $storeid) = @_;
+
+    my (undef, $relative_backup_dir) = $self->{'storage-plugin'}->parse_volname($volname);
+    return { 'tar-path' => "$self->{scfg}->{path}/${relative_backup_dir}/archive.tar" };
+}
+
+my sub restore_directory_init {
+    my ($self, $volname, $storeid) = @_;
+
+    my (undef, $relative_backup_dir, $vmid) = $self->{'storage-plugin'}->parse_volname($volname);
+    my $archive = "$self->{scfg}->{path}/${relative_backup_dir}/archive.sqfs";
+
+    my $mount_point = "/run/backup-provider-example/${vmid}.mount";
+    make_path($mount_point);
+    die "unable to create directory $mount_point\n" if !-d $mount_point;
+
+    run_command(['mount', '-o', 'ro', $archive, $mount_point]);
+
+    return { 'archive-directory' => $mount_point };
+}
+
+my sub restore_directory_cleanup {
+    my ($self, $volname, $storeid) = @_;
+
+    my (undef, undef, $vmid) = $self->{'storage-plugin'}->parse_volname($volname);
+    my $mount_point = "/run/backup-provider-example/${vmid}.mount";
+
+    run_command(['umount', $mount_point]);
+
+    return;
+}
+
+sub restore_container_init {
+    my ($self, $volname, $storeid, $info) = @_;
+
+    if ($self->{'restore-mechanisms'}->{$volname} eq 'tar') {
+	return restore_tar_init($self, $volname, $storeid);
+    } elsif ($self->{'restore-mechanisms'}->{$volname} eq 'directory') {
+	return restore_directory_init($self, $volname, $storeid);
+    } else {
+	die "no restore mechanism set for '$volname'\n";
+    }
+}
+
+sub restore_container_cleanup {
+    my ($self, $volname, $storeid, $info) = @_;
+
+    if ($self->{'restore-mechanisms'}->{$volname} eq 'tar') {
+	return; # nothing to do
+    } elsif ($self->{'restore-mechanisms'}->{$volname} eq 'directory') {
+	return restore_directory_cleanup($self, $volname, $storeid);
+    } else {
+	die "no restore mechanism set for '$volname'\n";
+    }
+}
+
+1;
diff --git a/src/PVE/BackupProvider/Plugin/Makefile b/src/PVE/BackupProvider/Plugin/Makefile
index bbd7431..bedc26e 100644
--- a/src/PVE/BackupProvider/Plugin/Makefile
+++ b/src/PVE/BackupProvider/Plugin/Makefile
@@ -1,4 +1,4 @@
-SOURCES = Base.pm
+SOURCES = Base.pm DirectoryExample.pm
 
 .PHONY: install
 install:
diff --git a/src/PVE/Storage/Custom/BackupProviderDirExamplePlugin.pm b/src/PVE/Storage/Custom/BackupProviderDirExamplePlugin.pm
new file mode 100644
index 0000000..f7cc1eb
--- /dev/null
+++ b/src/PVE/Storage/Custom/BackupProviderDirExamplePlugin.pm
@@ -0,0 +1,306 @@
+package PVE::Storage::Custom::BackupProviderDirExamplePlugin;
+
+use strict;
+use warnings;
+
+use File::Basename qw(basename);
+
+use PVE::BackupProvider::Plugin::DirectoryExample;
+use PVE::Tools;
+
+use base qw(PVE::Storage::Plugin);
+
+# Helpers
+
+sub get_vm_backup_mechanism {
+    my ($class, $scfg) = @_;
+
+    return $scfg->{'vm-backup-mechanism'} // properties()->{'vm-backup-mechanism'}->{'default'};
+}
+
+sub get_vm_backup_mode {
+    my ($class, $scfg) = @_;
+
+    return $scfg->{'vm-backup-mode'} // properties()->{'vm-backup-mode'}->{'default'};
+}
+
+sub get_lxc_backup_mode {
+    my ($class, $scfg) = @_;
+
+    return $scfg->{'lxc-backup-mode'} // properties()->{'lxc-backup-mode'}->{'default'};
+}
+
+# Configuration
+
+sub api {
+    return 11;
+}
+
+sub type {
+    return 'backup-provider-dir-example';
+}
+
+sub plugindata {
+    return {
+	content => [ { backup => 1, none => 1 }, { backup => 1 } ],
+    };
+}
+
+sub properties {
+    return {
+	'lxc-backup-mode' => {
+	    description => "How to create LXC backups. tar - create a tar archive."
+		." squashfs - create a squashfs image. Requires squashfs-tools to be installed.",
+	    type => 'string',
+	    enum => [qw(tar squashfs)],
+	    default => 'tar',
+	},
+	'vm-backup-mechanism' => {
+	    description => "Which mechanism to use for creating VM backups. nbd - access data via "
+		." NBD export. block-device - access data via regular block device.",
+	    type => 'string',
+	    enum => [qw(nbd block-device)],
+	    default => 'block-device',
+	},
+	'vm-backup-mode' => {
+	    description => "How to create VM backups. full - always create full backups."
+		." incremental - create incremental backups when possible, fallback to full when"
+		." necessary, e.g. VM disk's bitmap is invalid.",
+	    type => 'string',
+	    enum => [qw(full incremental)],
+	    default => 'full',
+	},
+    };
+}
+
+sub options {
+    return {
+	path => { fixed => 1 },
+	'lxc-backup-mode' => { optional => 1 },
+	'vm-backup-mechanism' => { optional => 1 },
+	'vm-backup-mode' => { optional => 1 },
+	disable => { optional => 1 },
+	nodes => { optional => 1 },
+	'prune-backups' => { optional => 1 },
+	'max-protected-backups' => { optional => 1 },
+    };
+}
+
+# Storage implementation
+
+# NOTE a proper backup storage should implement this
+sub prune_backups {
+    my ($class, $scfg, $storeid, $keep, $vmid, $type, $dryrun, $logfunc) = @_;
+
+    die "not implemented";
+}
+
+sub parse_volname {
+    my ($class, $volname) = @_;
+
+    if ($volname =~ m!^backup/((\d+)/[a-z]+-\d+)$!) {
+	my ($filename, $vmid) = ($1, $2);
+	return ('backup', $filename, $vmid);
+    }
+
+    die "unable to parse volume name '$volname'\n";
+}
+
+sub path {
+    my ($class, $scfg, $volname, $storeid, $snapname) = @_;
+
+    die "volume snapshot is not possible on backup-provider-dir-example volume" if $snapname;
+
+    my ($type, $filename, $vmid) = $class->parse_volname($volname);
+
+    return ("$scfg->{path}/${filename}", $vmid, $type);
+}
+
+sub create_base {
+    my ($class, $storeid, $scfg, $volname) = @_;
+
+    die "cannot create base image in backup-provider-dir-example storage\n";
+}
+
+sub clone_image {
+    my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
+
+    die "can't clone images in backup-provider-dir-example storage\n";
+}
+
+sub alloc_image {
+    my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
+
+    die "can't allocate space in backup-provider-dir-example storage\n";
+}
+
+# NOTE a proper backup storage should implement this
+sub free_image {
+    my ($class, $storeid, $scfg, $volname, $isBase) = @_;
+
+    # if it's a backing file, it would need to be merged into the upper image first.
+
+    die "not implemented";
+}
+
+sub list_images {
+    my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
+
+    my $res = [];
+
+    return $res;
+}
+
+sub list_volumes {
+    my ($class, $storeid, $scfg, $vmid, $content_types) = @_;
+
+    my $path = $scfg->{path};
+
+    my $res = [];
+    for my $type ($content_types->@*) {
+	next if $type ne 'backup';
+
+	my @guest_dirs = glob("$path/*");
+	for my $guest_dir (@guest_dirs) {
+	    next if !-d $guest_dir || $guest_dir !~ m!/(\d+)$!;
+
+	    my $backup_vmid = basename($guest_dir);
+
+	    next if defined($vmid) && $backup_vmid != $vmid;
+
+	    my @backup_dirs = glob("$guest_dir/*");
+	    for my $backup_dir (@backup_dirs) {
+		next if !-d $backup_dir || $backup_dir !~ m!/(lxc|qemu)-(\d+)$!;
+		my ($subtype, $backup_id) = ($1, $2);
+
+		my $size = 0;
+		my @backup_files = glob("$backup_dir/*");
+		$size += -s $_ for @backup_files;
+
+		push $res->@*, {
+		    volid => "$storeid:backup/${backup_vmid}/${subtype}-${backup_id}",
+		    vmid => $backup_vmid,
+		    format => "directory",
+		    ctime => $backup_id,
+		    size => $size,
+		    subtype => $subtype,
+		    content => $type,
+		    # TODO parent for incremental
+		};
+	    }
+	}
+    }
+
+    return $res;
+}
+
+sub activate_storage {
+    my ($class, $storeid, $scfg, $cache) = @_;
+
+    my $path = $scfg->{path};
+
+    my $timeout = 2;
+    if (!PVE::Tools::run_fork_with_timeout($timeout, sub {-d $path})) {
+	die "unable to activate storage '$storeid' - directory '$path' does not exist or is"
+	    ." unreachable\n";
+    }
+
+    return 1;
+}
+
+sub deactivate_storage {
+    my ($class, $storeid, $scfg, $cache) = @_;
+
+    return 1;
+}
+
+sub activate_volume {
+    my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
+
+    die "volume snapshot is not possible on backup-provider-dir-example volume" if $snapname;
+
+    return 1;
+}
+
+sub deactivate_volume {
+    my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
+
+    die "volume snapshot is not possible on backup-provider-dir-example volume" if $snapname;
+
+    return 1;
+}
+
+sub get_volume_attribute {
+    my ($class, $scfg, $storeid, $volname, $attribute) = @_;
+
+    return;
+}
+
+# NOTE a proper backup storage should implement this to support backup notes and
+# setting protected status.
+sub update_volume_attribute {
+    my ($class, $scfg, $storeid, $volname, $attribute, $value) = @_;
+
+    die "attribute '$attribute' is not supported on backup-provider-dir-example volume";
+}
+
+sub volume_size_info {
+    my ($class, $scfg, $storeid, $volname, $timeout) = @_;
+
+    my (undef, $relative_backup_dir) = $class->parse_volname($volname);
+    my ($ctime) = $relative_backup_dir =~ m/-(\d+)$/;
+    my $backup_dir = "$scfg->{path}/${relative_backup_dir}";
+
+    my $size = 0;
+    my @backup_files = glob("$backup_dir/*");
+    for my $backup_file (@backup_files) {
+	if ($backup_file =~ m!\.qcow2$!) {
+	    $size += $class->file_size_info($backup_file);
+	} else {
+	    $size += -s $backup_file;
+	}
+    }
+
+    my $parent; # TODO for incremental
+
+    return wantarray ? ($size, 'directory', $size, $parent, $ctime) : $size;
+}
+
+sub volume_resize {
+    my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
+
+    die "volume resize is not possible on backup-provider-dir-example volume";
+}
+
+sub volume_snapshot {
+    my ($class, $scfg, $storeid, $volname, $snap) = @_;
+
+    die "volume snapshot is not possible on backup-provider-dir-example volume";
+}
+
+sub volume_snapshot_rollback {
+    my ($class, $scfg, $storeid, $volname, $snap) = @_;
+
+    die "volume snapshot rollback is not possible on backup-provider-dir-example volume";
+}
+
+sub volume_snapshot_delete {
+    my ($class, $scfg, $storeid, $volname, $snap) = @_;
+
+    die "volume snapshot delete is not possible on backup-provider-dir-example volume";
+}
+
+sub volume_has_feature {
+    my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
+
+    return 0;
+}
+
+sub new_backup_provider {
+    my ($class, $scfg, $storeid, $bandwidth_limit, $log_function) = @_;
+
+    return PVE::BackupProvider::Plugin::DirectoryExample->new(
+	$class, $scfg, $storeid, $bandwidth_limit, $log_function);
+}
+
+1;
diff --git a/src/PVE/Storage/Custom/Makefile b/src/PVE/Storage/Custom/Makefile
new file mode 100644
index 0000000..c1e3eca
--- /dev/null
+++ b/src/PVE/Storage/Custom/Makefile
@@ -0,0 +1,5 @@
+SOURCES = BackupProviderDirExamplePlugin.pm
+
+.PHONY: install
+install:
+	for i in ${SOURCES}; do install -D -m 0644 $$i ${DESTDIR}${PERLDIR}/PVE/Storage/Custom/$$i; done
diff --git a/src/PVE/Storage/Makefile b/src/PVE/Storage/Makefile
index d5cc942..acd37f4 100644
--- a/src/PVE/Storage/Makefile
+++ b/src/PVE/Storage/Makefile
@@ -19,4 +19,5 @@ SOURCES= \
 .PHONY: install
 install:
 	for i in ${SOURCES}; do install -D -m 0644 $$i ${DESTDIR}${PERLDIR}/PVE/Storage/$$i; done
+	make -C Custom install
 	make -C LunCmd install
-- 
2.39.2



_______________________________________________
pve-devel mailing list
pve-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel


^ permalink raw reply	[flat|nested] 44+ messages in thread

* [pve-devel] [POC storage v2 13/25] Borg plugin
  2024-08-13 13:28 [pve-devel] [RFC qemu/storage/qemu-server/container/manager v2 00/25] backup provider API Fiona Ebner
                   ` (11 preceding siblings ...)
  2024-08-13 13:28 ` [pve-devel] [POC storage v2 12/25] add backup provider example Fiona Ebner
@ 2024-08-13 13:28 ` Fiona Ebner
  2024-08-13 13:28 ` [pve-devel] [PATCH qemu-server v2 14/25] move nbd_stop helper to QMPHelpers module Fiona Ebner
                   ` (12 subsequent siblings)
  25 siblings, 0 replies; 44+ messages in thread
From: Fiona Ebner @ 2024-08-13 13:28 UTC (permalink / raw)
  To: pve-devel

Archive names start with the guest type and ID and then the same
timestamp format as PBS.

Container archives have the following structure:
guest.config
firewall.config
filesystem/ # containing the whole filesystem structure

VM archives have the following structure
guest.config
firewall.config
volumes/ # containing a raw file for each device

A bindmount (respectively symlinks) are used to achieve this
structure, because Borg doesn't seem to support renaming on-the-fly.
(Prefix stripping via the "slashdot hack" would have helped slightly,
but is only in Borg >= 1.4
https://github.com/borgbackup/borg/actions/runs/7967940995)

NOTE: running via SSH was not yet tested. Bandwidth limit is not yet
honored and the task size is not calculated yet. Discard for VM
backups would also be nice to have, but it's not entirely clear how
(parsing progress and discarding according to that is one idea).
There is no dirty bitmap support, not sure if that is feasible to add.

Signed-off-by: Fiona Ebner <f.ebner@proxmox.com>
---

New in v2.

 src/PVE/BackupProvider/Plugin/Borg.pm  | 373 ++++++++++++++++++
 src/PVE/BackupProvider/Plugin/Makefile |   2 +-
 src/PVE/Storage.pm                     |   2 +
 src/PVE/Storage/BorgBackupPlugin.pm    | 506 +++++++++++++++++++++++++
 src/PVE/Storage/Makefile               |   1 +
 5 files changed, 883 insertions(+), 1 deletion(-)
 create mode 100644 src/PVE/BackupProvider/Plugin/Borg.pm
 create mode 100644 src/PVE/Storage/BorgBackupPlugin.pm

diff --git a/src/PVE/BackupProvider/Plugin/Borg.pm b/src/PVE/BackupProvider/Plugin/Borg.pm
new file mode 100644
index 0000000..b65321c
--- /dev/null
+++ b/src/PVE/BackupProvider/Plugin/Borg.pm
@@ -0,0 +1,373 @@
+package PVE::BackupProvider::Plugin::Borg;
+
+use strict;
+use warnings;
+
+use File::chdir;
+use File::Basename qw(basename);
+use File::Path qw(make_path remove_tree);
+use POSIX qw(strftime);
+
+use PVE::Tools;
+
+# ($vmtype, $vmid, $time_string)
+our $ARCHIVE_RE_3 = qr!^pve-(lxc|qemu)-([0-9]+)-([0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}Z)$!;
+
+sub archive_name {
+    my ($vmtype, $vmid, $backup_time) = @_;
+
+    return "pve-${vmtype}-${vmid}-" . strftime("%FT%TZ", gmtime($backup_time));
+}
+
+# remove_tree can be very verbose by default, do explicit error handling and limit to one message
+my sub _remove_tree {
+    my ($path) = @_;
+
+    remove_tree($path, { error => \my $err });
+    if ($err && @$err) { # empty array if no error
+	for my $diag (@$err) {
+	    my ($file, $message) = %$diag;
+	    die "cannot remove_tree '$path': $message\n" if $file eq '';
+	    die "cannot remove_tree '$path': unlinking $file failed - $message\n";
+	}
+    }
+}
+
+my sub prepare_run_dir {
+    my ($archive, $operation) = @_;
+
+    my $run_dir = "/run/pve-storage-borg-plugin/${archive}.${operation}";
+    _remove_tree($run_dir);
+    make_path($run_dir);
+    die "unable to create directory $run_dir\n" if !-d $run_dir;
+
+    return $run_dir;
+}
+
+my sub log_info {
+    my ($self, $message) = @_;
+
+    $self->{'log-function'}->('info', $message);
+}
+
+my sub log_warning {
+    my ($self, $message) = @_;
+
+    $self->{'log-function'}->('warn', $message);
+}
+
+my sub log_error {
+    my ($self, $message) = @_;
+
+    $self->{'log-function'}->('err', $message);
+}
+
+my sub file_contents_from_archive {
+    my ($self, $archive, $file) = @_;
+
+    my $run_dir = prepare_run_dir($archive, "file-contents");
+
+    my $raw;
+
+    eval {
+	local $CWD = $run_dir;
+
+	$self->{'storage-plugin'}->borg_cmd_extract(
+	    $self->{scfg},
+	    $self->{storeid},
+	    $archive,
+	    [$file],
+	);
+
+	$raw = PVE::Tools::file_get_contents("${run_dir}/${file}");
+    };
+    my $err = $@;
+    eval { _remove_tree($run_dir); };
+    log_warning($self, $@) if $@;
+    die $err if $err;
+
+    return $raw;
+}
+
+# Plugin implementation
+
+sub new {
+    my ($class, $storage_plugin, $scfg, $storeid, $log_function) = @_;
+
+    my $self = bless {
+	scfg => $scfg,
+	storeid => $storeid,
+	'storage-plugin' => $storage_plugin,
+	'log-function' => $log_function,
+    }, $class;
+
+    return $self;
+}
+
+sub provider_name {
+    my ($self) = @_;
+
+    return "Borg";
+}
+
+sub job_hook {
+    my ($self, $phase, $info) = @_;
+
+    if ($phase eq 'start') {
+	$self->{'job-id'} = $info->{'start-time'};
+    }
+
+    return;
+}
+
+sub backup_hook {
+    my ($self, $phase, $vmid, $vmtype, $info) = @_;
+
+    if ($phase eq 'start') {
+	$self->{$vmid}->{'task-size'} = 0;
+    }
+
+    return;
+}
+
+sub backup_get_mechanism {
+    my ($self, $vmid, $vmtype) = @_;
+
+    return ('block-device', undef) if $vmtype eq 'qemu';
+    return ('directory', undef) if $vmtype eq 'lxc';
+
+    die "unsupported VM type '$vmtype'\n";
+}
+
+sub backup_get_archive_name {
+    my ($self, $vmid, $vmtype, $backup_time) = @_;
+
+    return $self->{$vmid}->{archive} = archive_name($vmtype, $vmid, $backup_time);
+}
+
+sub backup_get_task_size {
+    my ($self, $vmid) = @_;
+
+    return $self->{$vmid}->{'task-size'};
+}
+
+sub backup_handle_log_file {
+    my ($self, $vmid, $filename) = @_;
+
+    return; # don't upload, Proxmox VE keeps the task log too
+}
+
+sub backup_vm {
+    my ($self, $vmid, $config_filename, $volumes, $info) = @_;
+
+    # TODO honor bandwith limit
+    # TODO discard?
+
+    my $archive = $self->{$vmid}->{archive};
+
+    my $run_dir = prepare_run_dir($archive, "backup-vm");
+    my $volume_dir = "${run_dir}/volumes";
+    make_path($volume_dir);
+    die "unable to create directory $volume_dir\n" if !-d $volume_dir;
+
+    PVE::Tools::file_copy($config_filename, "${run_dir}/guest.config");
+    my $paths = ['./guest.config'];
+
+    if (my $firewall_config = $info->{'firewall-config'}) {
+	PVE::Tools::file_copy($firewall_config, "${run_dir}/firewall.config");
+	push $paths->@*, './firewall.config';
+    }
+
+    for my $devicename (sort keys $volumes->%*) {
+	my $path = $volumes->{$devicename}->{path};
+	my $link_name = "${volume_dir}/${devicename}.raw";
+	symlink($path, $link_name) or die "could not create symlink $link_name -> $path\n";
+	push $paths->@*, "./volumes/" . basename($link_name, ());
+    }
+
+    # TODO --stats for size?
+
+    eval {
+	local $CWD = $run_dir;
+
+	$self->{'storage-plugin'}->borg_cmd_create(
+	    $self->{scfg},
+	    $self->{storeid},
+	    $self->{$vmid}->{archive},
+	    $paths,
+	    ['--read-special', '--progress'],
+	);
+    };
+    my $err = $@;
+    eval { _remove_tree($run_dir) };
+    log_warning($self, $@) if $@;
+    die $err if $err;
+}
+
+sub backup_container {
+    my ($self, $vmid, $config_filename, $id_map, $exclude_patterns, $info) = @_;
+
+    # TODO honor bandwith limit
+    # TODO ID map needed?
+
+    my $archive = $self->{$vmid}->{archive};
+
+    my $run_dir = prepare_run_dir($archive, "backup-container");
+
+    my $filesystem_dir = "${run_dir}/filesystem";
+    make_path($filesystem_dir);
+    die "unable to create directory $filesystem_dir\n" if !-d $filesystem_dir;
+
+    PVE::Tools::file_copy($config_filename, "${run_dir}/guest.config");
+    my $paths = ['./guest.config'];
+
+    if (my $firewall_config = $info->{'firewall-config'}) {
+	PVE::Tools::file_copy($firewall_config, "${run_dir}/firewall.config");
+	push $paths->@*, './firewall.config';
+    }
+
+    PVE::Tools::run_command(['mount', '-o', 'bind,ro', $info->{directory}, $filesystem_dir]);
+    push $paths->@*, "./filesystem";
+
+    my $opts = ['--numeric-ids', '--sparse', '--one-file-system'];
+    push $opts->@*, '--progress';
+
+    for my $pattern ($exclude_patterns->@*) {
+	if ($pattern =~ m|^/|) {
+	    push $opts->@*, '-e', "filesystem${pattern}";
+	} else {
+	    push $opts->@*, '-e', "filesystem/**${pattern}";
+	}
+    }
+
+    # TODO --stats for size?
+
+    eval {
+	local $CWD = $run_dir;
+
+	$self->{'storage-plugin'}->borg_cmd_create(
+	    $self->{scfg},
+	    $self->{storeid},
+	    $self->{$vmid}->{archive},
+	    $paths,
+	    $opts,
+	);
+    };
+    my $err = $@;
+    eval {
+	PVE::Tools::run_command(['umount', $filesystem_dir]);
+	_remove_tree($run_dir);
+    };
+    log_warning($self, "unable to clean up $run_dir - $@") if $@;
+    die $err if $err;
+}
+
+sub restore_get_mechanism {
+    my ($self, $volname, $storeid) = @_;
+
+    my (undef, $archive) = $self->{'storage-plugin'}->parse_volname($volname);
+    my ($vmtype) = $archive =~ m!^pve-([^\s-]+)!
+	or die "cannot parse guest type from archive name '$archive'\n";
+
+    return ('qemu-img', $vmtype) if $vmtype eq 'qemu';
+    return ('directory', $vmtype) if $vmtype eq 'lxc';
+
+    die "unexpected guest type '$vmtype'\n";
+}
+
+sub restore_get_guest_config {
+    my ($self, $volname, $storeid) = @_;
+
+    my (undef, $archive) = $self->{'storage-plugin'}->parse_volname($volname);
+    return file_contents_from_archive($self, $archive, 'guest.config');
+}
+
+sub restore_get_firewall_config {
+    my ($self, $volname, $storeid) = @_;
+
+    my (undef, $archive) = $self->{'storage-plugin'}->parse_volname($volname);
+    return file_contents_from_archive($self, $archive, 'firewall.config');
+}
+
+sub restore_vm_init {
+    my ($self, $volname, $storeid) = @_;
+
+    my $res = {};
+
+    my (undef, $archive, $vmid) = $self->{'storage-plugin'}->parse_volname($volname);
+    my $mount_point = prepare_run_dir($archive, "restore-vm");
+
+    $self->{'storage-plugin'}->borg_cmd_mount(
+	$self->{scfg},
+	$self->{storeid},
+	$archive,
+	$mount_point,
+    );
+
+    my @backup_files = glob("$mount_point/volumes/*");
+    for my $backup_file (@backup_files) {
+	next if $backup_file !~ m!^(.*/(.*)\.raw)$!; # untaint
+	($backup_file, my $devicename) = ($1, $2);
+	# TODO avoid dependency on base plugin?
+	$res->{$devicename}->{size} = PVE::Storage::Plugin::file_size_info($backup_file);
+    }
+
+    $self->{$volname}->{'mount-point'} = $mount_point;
+
+    return $res;
+}
+
+sub restore_vm_cleanup {
+    my ($self, $volname, $storeid) = @_;
+
+    my $mount_point = $self->{$volname}->{'mount-point'} or return;
+
+    PVE::Tools::run_command(['umount', $mount_point]);
+
+    return;
+}
+
+sub restore_vm_volume_init {
+    my ($self, $volname, $storeid, $devicename, $info) = @_;
+
+    my $mount_point = $self->{$volname}->{'mount-point'}
+	or die "expected mount point for archive not present\n";
+
+    return { 'qemu-img-path' => "${mount_point}/volumes/${devicename}.raw" };
+}
+
+sub restore_vm_volume_cleanup {
+    my ($self, $volname, $storeid, $devicename, $info) = @_;
+
+    return;
+}
+
+sub restore_container_init {
+    my ($self, $volname, $storeid, $info) = @_;
+
+    my (undef, $archive, $vmid) = $self->{'storage-plugin'}->parse_volname($volname);
+    my $mount_point = prepare_run_dir($archive, "restore-container");
+
+    $self->{'storage-plugin'}->borg_cmd_mount(
+	$self->{scfg},
+	$self->{storeid},
+	$archive,
+	$mount_point,
+    );
+
+    $self->{$volname}->{'mount-point'} = $mount_point;
+
+    return { 'archive-directory' => "${mount_point}/filesystem" };
+}
+
+sub restore_container_cleanup {
+    my ($self, $volname, $storeid, $info) = @_;
+
+    my $mount_point = $self->{$volname}->{'mount-point'} or return;
+
+    PVE::Tools::run_command(['umount', $mount_point]);
+
+    return;
+}
+
+1;
diff --git a/src/PVE/BackupProvider/Plugin/Makefile b/src/PVE/BackupProvider/Plugin/Makefile
index bedc26e..db08c2d 100644
--- a/src/PVE/BackupProvider/Plugin/Makefile
+++ b/src/PVE/BackupProvider/Plugin/Makefile
@@ -1,4 +1,4 @@
-SOURCES = Base.pm DirectoryExample.pm
+SOURCES = Base.pm Borg.pm DirectoryExample.pm
 
 .PHONY: install
 install:
diff --git a/src/PVE/Storage.pm b/src/PVE/Storage.pm
index 8993ba7..96c4e1b 100755
--- a/src/PVE/Storage.pm
+++ b/src/PVE/Storage.pm
@@ -40,6 +40,7 @@ use PVE::Storage::ZFSPlugin;
 use PVE::Storage::PBSPlugin;
 use PVE::Storage::BTRFSPlugin;
 use PVE::Storage::ESXiPlugin;
+use PVE::Storage::BorgBackupPlugin;
 
 # Storage API version. Increment it on changes in storage API interface.
 use constant APIVER => 11;
@@ -66,6 +67,7 @@ PVE::Storage::ZFSPlugin->register();
 PVE::Storage::PBSPlugin->register();
 PVE::Storage::BTRFSPlugin->register();
 PVE::Storage::ESXiPlugin->register();
+PVE::Storage::BorgBackupPlugin->register();
 
 # load third-party plugins
 if ( -d '/usr/share/perl5/PVE/Storage/Custom' ) {
diff --git a/src/PVE/Storage/BorgBackupPlugin.pm b/src/PVE/Storage/BorgBackupPlugin.pm
new file mode 100644
index 0000000..739730e
--- /dev/null
+++ b/src/PVE/Storage/BorgBackupPlugin.pm
@@ -0,0 +1,506 @@
+package PVE::Storage::BorgBackupPlugin;
+
+use strict;
+use warnings;
+
+use JSON qw(from_json);
+use Net::IP;
+use POSIX;
+
+use PVE::BackupProvider::Plugin::Borg;
+use PVE::Tools;
+
+use base qw(PVE::Storage::Plugin);
+
+my sub borg_repository_uri {
+    my ($scfg, $storeid) = @_;
+
+    my $uri = '';
+    if (my $server = $scfg->{server}) {
+	die "no username configured for $storeid\n" if !$scfg->{username};
+	my $prefix = "ssh://$scfg->{username}@";
+	$server = "[$server]" if Net::IP::ip_is_ipv6($server);
+	if (my $port = $scfg->{port}) {
+	    $uri = "${prefix}${server}:${port}";
+	} else {
+	    $uri = "${prefix}${server}";
+	}
+    }
+    $uri .= $scfg->{'repository-path'};
+
+    return $uri;
+}
+
+my sub borg_password_file_name {
+    my ($scfg, $storeid) = @_;
+
+    return "/etc/pve/priv/storage/${storeid}.pw";
+}
+
+my sub borg_set_password {
+    my ($scfg, $storeid, $password) = @_;
+
+    my $pwfile = borg_password_file_name($scfg, $storeid);
+    mkdir "/etc/pve/priv/storage";
+
+    PVE::Tools::file_set_contents($pwfile, "$password\n");
+}
+
+my sub borg_delete_password {
+    my ($scfg, $storeid) = @_;
+
+    my $pwfile = borg_password_file_name($scfg, $storeid);
+
+    unlink $pwfile;
+}
+
+my sub borg_get_password {
+    my ($scfg, $storeid) = @_;
+
+    my $pwfile = borg_password_file_name($scfg, $storeid);
+
+    return PVE::Tools::file_read_firstline($pwfile);
+}
+
+sub borg_cmd_list {
+    my ($class, $scfg, $storeid) = @_;
+
+    my $uri = borg_repository_uri($scfg, $storeid);
+
+    my $password = borg_get_password($scfg, $storeid);
+    local $ENV{BORG_PASSPHRASE} = $password if $password;
+
+    my $json = '';
+    my $cmd = ['borg', 'list', '--json', $uri];
+
+    my $errfunc = sub { warn $_[0]; };
+    my $outfunc = sub { $json .= $_[0]; };
+
+    PVE::Tools::run_command(
+	$cmd, errmsg => "command @$cmd failed", outfunc => $outfunc, errfunc => $errfunc);
+
+    my $res = eval { from_json($json) };
+    die "unable to parse 'borg list' output - $@\n" if $@;
+    return $res;
+}
+
+sub borg_cmd_create {
+    my ($class, $scfg, $storeid, $archive, $paths, $opts) = @_;
+
+    my $uri = borg_repository_uri($scfg, $storeid);
+
+    my $password = borg_get_password($scfg, $storeid);
+    local $ENV{BORG_PASSPHRASE} = $password if $password;
+
+    my $cmd = ['borg', 'create', $opts->@*, "${uri}::${archive}", $paths->@*];
+
+    PVE::Tools::run_command($cmd, errmsg => "command @$cmd failed");
+
+    return;
+}
+
+sub borg_cmd_extract {
+    my ($class, $scfg, $storeid, $archive, $paths) = @_;
+
+    my $uri = borg_repository_uri($scfg, $storeid);
+
+    my $password = borg_get_password($scfg, $storeid);
+    local $ENV{BORG_PASSPHRASE} = $password if $password;
+
+    my $cmd = ['borg', 'extract', "${uri}::${archive}", $paths->@*];
+
+    PVE::Tools::run_command($cmd, errmsg => "command @$cmd failed");
+
+    return;
+}
+
+sub borg_cmd_delete {
+    my ($class, $scfg, $storeid, $archive) = @_;
+
+    my $uri = borg_repository_uri($scfg, $storeid);
+
+    my $password = borg_get_password($scfg, $storeid);
+    local $ENV{BORG_PASSPHRASE} = $password if $password;
+
+    my $cmd = ['borg', 'delete', "${uri}::${archive}"];
+
+    PVE::Tools::run_command($cmd, errmsg => "command @$cmd failed");
+
+    return;
+}
+
+sub borg_cmd_info {
+    my ($class, $scfg, $storeid, $archive, $timeout) = @_;
+
+    my $uri = borg_repository_uri($scfg, $storeid);
+
+    my $password = borg_get_password($scfg, $storeid);
+    local $ENV{BORG_PASSPHRASE} = $password if $password;
+
+    my $json = '';
+    my $cmd = ['borg', 'info', '--json', "${uri}::${archive}"];
+
+    my $errfunc = sub { warn $_[0]; };
+    my $outfunc = sub { $json .= $_[0]; };
+
+    PVE::Tools::run_command(
+	$cmd,
+	errmsg => "command @$cmd failed",
+	timeout => $timeout,
+	outfunc => $outfunc,
+	errfunc => $errfunc,
+    );
+
+    my $res = eval { from_json($json) };
+    die "unable to parse 'borg info' output for archive '$archive' - $@\n" if $@;
+    return $res;
+}
+
+sub borg_cmd_mount {
+    my ($class, $scfg, $storeid, $archive, $mount_point) = @_;
+
+    my $uri = borg_repository_uri($scfg, $storeid);
+
+    my $password = borg_get_password($scfg, $storeid);
+    local $ENV{BORG_PASSPHRASE} = $password if $password;
+
+    my $cmd = ['borg', 'mount', "${uri}::${archive}", $mount_point];
+
+    PVE::Tools::run_command($cmd, errmsg => "command @$cmd failed");
+
+    return;
+}
+
+my sub parse_backup_time {
+    my ($time_string) = @_;
+
+    my @tm = (POSIX::strptime($time_string, "%FT%TZ"));
+    # expect sec, min, hour, mday, mon, year
+    if (grep { !defined($_) } @tm[0..5]) {
+	warn "error parsing time from string '$time_string'\n";
+	return 0;
+    } else {
+	local $ENV{TZ} = 'UTC'; # time string is UTC
+
+	# Fill in isdst to avoid undef warning. No daylight saving time for UTC.
+	$tm[8] //= 0;
+
+	if (my $since_epoch = mktime(@tm)) {
+	    return int($since_epoch);
+	} else {
+	    warn "error parsing time from string '$time_string'\n";
+	    return 0;
+	}
+    }
+}
+
+# Helpers
+
+sub type {
+    return 'borg';
+}
+
+sub plugindata {
+    return {
+	content => [ { backup => 1, none => 1 }, { backup => 1 } ],
+    };
+}
+
+sub properties {
+    return {
+	'repository-path' => {
+	    description => "Path to the backup repository",
+	    type => 'string',
+	},
+    };
+}
+
+sub options {
+    return {
+	'repository-path' => { fixed => 1 },
+	server => { optional => 1 },
+	port => { optional => 1 },
+	username => { optional => 1 },
+	# TODO ssh-password!?
+	password => { optional => 1 },
+	disable => { optional => 1 },
+	nodes => { optional => 1 },
+	'prune-backups' => { optional => 1 },
+	'max-protected-backups' => { optional => 1 },
+    };
+}
+
+# Storage implementation
+
+sub on_add_hook {
+    my ($class, $storeid, $scfg, %param) = @_;
+
+    if (defined(my $password = $param{password})) {
+	borg_set_password($scfg, $storeid, $password);
+    } else {
+	borg_delete_password($scfg, $storeid);
+    }
+
+    return;
+}
+
+sub on_update_hook {
+    my ($class, $storeid, $scfg, %param) = @_;
+
+    if (exists($param{password})) {
+	if (defined($param{password})) {
+	    borg_set_password($scfg, $storeid, $param{password});
+	} else {
+	    borg_delete_password($scfg, $storeid);
+	}
+    }
+
+    return;
+}
+
+sub on_delete_hook {
+    my ($class, $storeid, $scfg) = @_;
+
+    borg_delete_password($scfg, $storeid);
+
+    return;
+}
+
+sub prune_backups {
+    my ($class, $scfg, $storeid, $keep, $vmid, $type, $dryrun, $logfunc) = @_;
+
+    # FIXME - is 'borg prune' compatible with ours?
+    die "not implemented";
+}
+
+sub parse_volname {
+    my ($class, $volname) = @_;
+
+    if ($volname =~ m!^backup/(.*)$!) {
+	my $archive = $1;
+	if ($archive =~ $PVE::BackupProvider::Plugin::Borg::ARCHIVE_RE_3) {
+	    return ('backup', $archive, $2);
+	}
+    }
+
+    die "unable to parse Borg volume name '$volname'\n";
+}
+
+sub path {
+    my ($class, $scfg, $volname, $storeid, $snapname) = @_;
+
+    die "volume snapshot is not possible on Borg volume" if $snapname;
+
+    my $uri = borg_repository_uri($scfg, $storeid);
+    my (undef, $archive) = $class->parse_volname($volname);
+
+    return "${uri}::${archive}";
+}
+
+sub create_base {
+    my ($class, $storeid, $scfg, $volname) = @_;
+
+    die "cannot create base image in Borg storage\n";
+}
+
+sub clone_image {
+    my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
+
+    die "can't clone images in Borg storage\n";
+}
+
+sub alloc_image {
+    my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
+
+    die "can't allocate space in Borg storage\n";
+}
+
+sub free_image {
+    my ($class, $storeid, $scfg, $volname, $isBase) = @_;
+
+    my (undef, $archive) = $class->parse_volname($volname);
+
+    borg_cmd_delete($class, $scfg, $storeid, $archive);
+
+    return;
+}
+
+sub list_images {
+    my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
+
+    return []; # guest images are not supported, only backups
+}
+
+sub list_volumes {
+    my ($class, $storeid, $scfg, $vmid, $content_types) = @_;
+
+    my $res = [];
+
+    return $res if !grep { $_ eq 'backup' } $content_types->@*;
+
+    my $archives = $class->borg_cmd_list($scfg, $storeid)->{archives}
+	or die "expected 'archives' key in 'borg list' JSON output missing\n";
+
+    for my $info ($archives->@*) {
+	my $archive = $info->{archive};
+	my ($vmtype, $backup_vmid, $time_string) =
+	    $archive =~ $PVE::BackupProvider::Plugin::Borg::ARCHIVE_RE_3 or next;
+
+	next if defined($vmid) && $vmid != $backup_vmid;
+
+	push $res->@*, {
+	    volid => "${storeid}:backup/${archive}",
+	    size => 0, # FIXME how to cheaply get?
+	    content => 'backup',
+	    ctime => parse_backup_time($time_string),
+	    vmid => $backup_vmid,
+	    format => "borg-archive",
+	    subtype => $vmtype,
+	}
+    }
+
+    return $res;
+}
+
+sub status {
+    my ($class, $storeid, $scfg, $cache) = @_;
+
+    my $uri = borg_repository_uri($scfg, $storeid);
+
+    my $res;
+
+    if ($uri =~ m!^ssh://!) {
+	#FIXME ssh and df on target?
+	return;
+    } else { # $uri is a local path
+	my $timeout = 2;
+	$res = PVE::Tools::df($uri, $timeout);
+
+	return if !$res || !$res->{total};
+    }
+
+
+    return ($res->{total}, $res->{avail}, $res->{used}, 1);
+}
+
+sub activate_storage {
+    my ($class, $storeid, $scfg, $cache) = @_;
+
+    # TODO how to cheaply check? split ssh and non-ssh?
+
+    return 1;
+}
+
+sub deactivate_storage {
+    my ($class, $storeid, $scfg, $cache) = @_;
+
+    return 1;
+}
+
+sub activate_volume {
+    my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
+
+    die "volume snapshot is not possible on Borg volume" if $snapname;
+
+    return 1;
+}
+
+sub deactivate_volume {
+    my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
+
+    die "volume snapshot is not possible on Borg volume" if $snapname;
+
+    return 1;
+}
+
+sub get_volume_attribute {
+    my ($class, $scfg, $storeid, $volname, $attribute) = @_;
+
+    return;
+}
+
+sub update_volume_attribute {
+    my ($class, $scfg, $storeid, $volname, $attribute, $value) = @_;
+
+    # FIXME notes or protected possible?
+
+    die "attribute '$attribute' is not supported on Borg volume";
+}
+
+sub volume_size_info {
+    my ($class, $scfg, $storeid, $volname, $timeout) = @_;
+
+    my (undef, $archive) = $class->parse_volname($volname);
+    my (undef, undef, $time_string) =
+	$archive =~ $PVE::BackupProvider::Plugin::Borg::ARCHIVE_RE_3;
+
+    my $backup_time = 0;
+    if ($time_string) {
+	$backup_time = parse_backup_time($time_string)
+    } else {
+	warn "could not parse time from archive name '$archive'\n";
+    }
+
+    my $archives = borg_cmd_info($class, $scfg, $storeid, $archive, $timeout)->{archives}
+	or die "expected 'archives' key in 'borg info' JSON output missing\n";
+
+    my $stats = eval { $archives->[0]->{stats} }
+	or die "expected entry in 'borg info' JSON output missing\n";
+    my ($size, $used) = $stats->@{qw(original_size deduplicated_size)};
+
+    ($size) = ($size =~ /^(\d+)$/); # untaint
+    die "size '$size' not an integer\n" if !defined($size);
+    # coerce back from string
+    $size = int($size);
+    ($used) = ($used =~ /^(\d+)$/); # untaint
+    die "used '$used' not an integer\n" if !defined($used);
+    # coerce back from string
+    $used = int($used);
+
+    return wantarray ? ($size, 'borg-archive', $used, undef, $backup_time) : $size;
+}
+
+sub volume_resize {
+    my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
+
+    die "volume resize is not possible on Borg volume";
+}
+
+sub volume_snapshot {
+    my ($class, $scfg, $storeid, $volname, $snap) = @_;
+
+    die "volume snapshot is not possible on Borg volume";
+}
+
+sub volume_snapshot_rollback {
+    my ($class, $scfg, $storeid, $volname, $snap) = @_;
+
+    die "volume snapshot rollback is not possible on Borg volume";
+}
+
+sub volume_snapshot_delete {
+    my ($class, $scfg, $storeid, $volname, $snap) = @_;
+
+    die "volume snapshot delete is not possible on Borg volume";
+}
+
+sub volume_has_feature {
+    my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
+
+    return 0;
+}
+
+sub rename_volume {
+    my ($class, $scfg, $storeid, $source_volname, $target_vmid, $target_volname) = @_;
+
+    die "volume rename is not implemented in Borg storage plugin\n";
+}
+
+sub new_backup_provider {
+    my ($class, $scfg, $storeid, $bandwidth_limit, $log_function) = @_;
+
+    return PVE::BackupProvider::Plugin::Borg->new(
+	$class, $scfg, $storeid, $bandwidth_limit, $log_function);
+}
+
+1;
diff --git a/src/PVE/Storage/Makefile b/src/PVE/Storage/Makefile
index acd37f4..9fe2c66 100644
--- a/src/PVE/Storage/Makefile
+++ b/src/PVE/Storage/Makefile
@@ -14,6 +14,7 @@ SOURCES= \
 	PBSPlugin.pm \
 	BTRFSPlugin.pm \
 	LvmThinPlugin.pm \
+	BorgBackupPlugin.pm \
 	ESXiPlugin.pm
 
 .PHONY: install
-- 
2.39.2



_______________________________________________
pve-devel mailing list
pve-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel


^ permalink raw reply	[flat|nested] 44+ messages in thread

* [pve-devel] [PATCH qemu-server v2 14/25] move nbd_stop helper to QMPHelpers module
  2024-08-13 13:28 [pve-devel] [RFC qemu/storage/qemu-server/container/manager v2 00/25] backup provider API Fiona Ebner
                   ` (12 preceding siblings ...)
  2024-08-13 13:28 ` [pve-devel] [POC storage v2 13/25] Borg plugin Fiona Ebner
@ 2024-08-13 13:28 ` Fiona Ebner
  2024-08-13 13:28 ` [pve-devel] [PATCH qemu-server v2 15/25] backup: move cleanup of fleecing images to cleanup method Fiona Ebner
                   ` (11 subsequent siblings)
  25 siblings, 0 replies; 44+ messages in thread
From: Fiona Ebner @ 2024-08-13 13:28 UTC (permalink / raw)
  To: pve-devel

Like this nbd_stop() can be called from a module that cannot include
QemuServer.pm.

Signed-off-by: Fiona Ebner <f.ebner@proxmox.com>
---

No changes in v2.

 PVE/API2/Qemu.pm             | 3 ++-
 PVE/CLI/qm.pm                | 3 ++-
 PVE/QemuServer.pm            | 6 ------
 PVE/QemuServer/QMPHelpers.pm | 6 ++++++
 4 files changed, 10 insertions(+), 8 deletions(-)

diff --git a/PVE/API2/Qemu.pm b/PVE/API2/Qemu.pm
index d25a79fe..212cfc1b 100644
--- a/PVE/API2/Qemu.pm
+++ b/PVE/API2/Qemu.pm
@@ -35,6 +35,7 @@ use PVE::QemuServer::Monitor qw(mon_cmd);
 use PVE::QemuServer::Machine;
 use PVE::QemuServer::Memory qw(get_current_memory);
 use PVE::QemuServer::PCI;
+use PVE::QemuServer::QMPHelpers;
 use PVE::QemuServer::USB;
 use PVE::QemuMigrate;
 use PVE::RPCEnvironment;
@@ -5910,7 +5911,7 @@ __PACKAGE__->register_method({
 		    return;
 		},
 		'nbdstop' => sub {
-		    PVE::QemuServer::nbd_stop($state->{vmid});
+		    PVE::QemuServer::QMPHelpers::nbd_stop($state->{vmid});
 		    return;
 		},
 		'resume' => sub {
diff --git a/PVE/CLI/qm.pm b/PVE/CLI/qm.pm
index d3dbf7b4..8349997e 100755
--- a/PVE/CLI/qm.pm
+++ b/PVE/CLI/qm.pm
@@ -35,6 +35,7 @@ use PVE::QemuServer::Agent qw(agent_available);
 use PVE::QemuServer::ImportDisk;
 use PVE::QemuServer::Monitor qw(mon_cmd);
 use PVE::QemuServer::OVF;
+use PVE::QemuServer::QMPHelpers;
 use PVE::QemuServer;
 
 use PVE::CLIHandler;
@@ -385,7 +386,7 @@ __PACKAGE__->register_method ({
 
 	my $vmid = $param->{vmid};
 
-	eval { PVE::QemuServer::nbd_stop($vmid) };
+	eval { PVE::QemuServer::QMPHelpers::nbd_stop($vmid) };
 	warn $@ if $@;
 
 	return;
diff --git a/PVE/QemuServer.pm b/PVE/QemuServer.pm
index b26da505..e5ff5efb 100644
--- a/PVE/QemuServer.pm
+++ b/PVE/QemuServer.pm
@@ -8548,12 +8548,6 @@ sub generate_smbios1_uuid {
     return "uuid=".generate_uuid();
 }
 
-sub nbd_stop {
-    my ($vmid) = @_;
-
-    mon_cmd($vmid, 'nbd-server-stop', timeout => 25);
-}
-
 sub create_reboot_request {
     my ($vmid) = @_;
     open(my $fh, '>', "/run/qemu-server/$vmid.reboot")
diff --git a/PVE/QemuServer/QMPHelpers.pm b/PVE/QemuServer/QMPHelpers.pm
index 0269ea46..826938de 100644
--- a/PVE/QemuServer/QMPHelpers.pm
+++ b/PVE/QemuServer/QMPHelpers.pm
@@ -15,6 +15,12 @@ qemu_objectadd
 qemu_objectdel
 );
 
+sub nbd_stop {
+    my ($vmid) = @_;
+
+    mon_cmd($vmid, 'nbd-server-stop', timeout => 25);
+}
+
 sub qemu_deviceadd {
     my ($vmid, $devicefull) = @_;
 
-- 
2.39.2



_______________________________________________
pve-devel mailing list
pve-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel


^ permalink raw reply	[flat|nested] 44+ messages in thread

* [pve-devel] [PATCH qemu-server v2 15/25] backup: move cleanup of fleecing images to cleanup method
  2024-08-13 13:28 [pve-devel] [RFC qemu/storage/qemu-server/container/manager v2 00/25] backup provider API Fiona Ebner
                   ` (13 preceding siblings ...)
  2024-08-13 13:28 ` [pve-devel] [PATCH qemu-server v2 14/25] move nbd_stop helper to QMPHelpers module Fiona Ebner
@ 2024-08-13 13:28 ` Fiona Ebner
  2024-08-13 13:28 ` [pve-devel] [PATCH qemu-server v2 16/25] backup: cleanup: check if VM is running before issuing QMP commands Fiona Ebner
                   ` (10 subsequent siblings)
  25 siblings, 0 replies; 44+ messages in thread
From: Fiona Ebner @ 2024-08-13 13:28 UTC (permalink / raw)
  To: pve-devel

TPM drives are already detached there and it's better to group
these things together.

Signed-off-by: Fiona Ebner <f.ebner@proxmox.com>
---

No changes in v2.

 PVE/VZDump/QemuServer.pm | 25 +++++++++----------------
 1 file changed, 9 insertions(+), 16 deletions(-)

diff --git a/PVE/VZDump/QemuServer.pm b/PVE/VZDump/QemuServer.pm
index 012c9210..b2ced154 100644
--- a/PVE/VZDump/QemuServer.pm
+++ b/PVE/VZDump/QemuServer.pm
@@ -690,7 +690,6 @@ sub archive_pbs {
 
     # get list early so we die on unkown drive types before doing anything
     my $devlist = _get_task_devlist($task);
-    my $use_fleecing;
 
     $self->enforce_vm_running_for_backup($vmid);
     $self->{qmeventd_fh} = PVE::QemuServer::register_qmeventd_handle($vmid);
@@ -721,7 +720,7 @@ sub archive_pbs {
 
 	my $is_template = PVE::QemuConfig->is_template($self->{vmlist}->{$vmid});
 
-	$use_fleecing = check_and_prepare_fleecing(
+	$task->{'use-fleecing'} = check_and_prepare_fleecing(
 	    $self, $vmid, $opts->{fleecing}, $task->{disks}, $is_template, $qemu_support);
 
 	my $fs_frozen = $self->qga_fs_freeze($task, $vmid);
@@ -735,7 +734,7 @@ sub archive_pbs {
 	    devlist => $devlist,
 	    'config-file' => $conffile,
 	};
-	$params->{fleecing} = JSON::true if $use_fleecing;
+	$params->{fleecing} = JSON::true if $task->{'use-fleecing'};
 
 	if (defined(my $ns = $scfg->{namespace})) {
 	    $params->{'backup-ns'} = $ns;
@@ -784,11 +783,6 @@ sub archive_pbs {
     }
     $self->restore_vm_power_state($vmid);
 
-    if ($use_fleecing) {
-	detach_fleecing_images($task->{disks}, $vmid);
-	cleanup_fleecing_images($self, $task->{disks});
-    }
-
     die $err if $err;
 }
 
@@ -891,7 +885,6 @@ sub archive_vma {
     }
 
     my $devlist = _get_task_devlist($task);
-    my $use_fleecing;
 
     $self->enforce_vm_running_for_backup($vmid);
     $self->{qmeventd_fh} = PVE::QemuServer::register_qmeventd_handle($vmid);
@@ -911,7 +904,7 @@ sub archive_vma {
 
 	$attach_tpmstate_drive->($self, $task, $vmid);
 
-	$use_fleecing = check_and_prepare_fleecing(
+	$task->{'use-fleecing'} = check_and_prepare_fleecing(
 	    $self, $vmid, $opts->{fleecing}, $task->{disks}, $is_template, $qemu_support);
 
 	my $outfh;
@@ -942,7 +935,7 @@ sub archive_vma {
 		devlist => $devlist
 	    };
 	    $params->{'firewall-file'} = $firewall if -e $firewall;
-	    $params->{fleecing} = JSON::true if $use_fleecing;
+	    $params->{fleecing} = JSON::true if $task->{'use-fleecing'};
 	    add_backup_performance_options($params, $opts->{performance}, $qemu_support);
 
 	    $qmpclient->queue_cmd($vmid, $backup_cb, 'backup', %$params);
@@ -984,11 +977,6 @@ sub archive_vma {
 
     $self->restore_vm_power_state($vmid);
 
-    if ($use_fleecing) {
-	detach_fleecing_images($task->{disks}, $vmid);
-	cleanup_fleecing_images($self, $task->{disks});
-    }
-
     if ($err) {
 	if ($cpid) {
 	    kill(9, $cpid);
@@ -1132,6 +1120,11 @@ sub cleanup {
 
     $detach_tpmstate_drive->($task, $vmid);
 
+    if ($task->{'use-fleecing'}) {
+	detach_fleecing_images($task->{disks}, $vmid);
+	cleanup_fleecing_images($self, $task->{disks});
+    }
+
     if ($self->{qmeventd_fh}) {
 	close($self->{qmeventd_fh});
     }
-- 
2.39.2



_______________________________________________
pve-devel mailing list
pve-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel


^ permalink raw reply	[flat|nested] 44+ messages in thread

* [pve-devel] [PATCH qemu-server v2 16/25] backup: cleanup: check if VM is running before issuing QMP commands
  2024-08-13 13:28 [pve-devel] [RFC qemu/storage/qemu-server/container/manager v2 00/25] backup provider API Fiona Ebner
                   ` (14 preceding siblings ...)
  2024-08-13 13:28 ` [pve-devel] [PATCH qemu-server v2 15/25] backup: move cleanup of fleecing images to cleanup method Fiona Ebner
@ 2024-08-13 13:28 ` Fiona Ebner
  2024-08-13 13:28 ` [pve-devel] [PATCH qemu-server v2 17/25] backup: keep track of block-node size instead of volume size Fiona Ebner
                   ` (9 subsequent siblings)
  25 siblings, 0 replies; 44+ messages in thread
From: Fiona Ebner @ 2024-08-13 13:28 UTC (permalink / raw)
  To: pve-devel

When the VM is only started for backup, the VM will be stopped at that
point again. While the detach helpers do not warn about errors
currently, that might change in the future. This is also in
preparation for other cleanup QMP helpers that are more verbose about
failure.

Signed-off-by: Fiona Ebner <f.ebner@proxmox.com>
---

No changes in v2.

 PVE/VZDump/QemuServer.pm | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)

diff --git a/PVE/VZDump/QemuServer.pm b/PVE/VZDump/QemuServer.pm
index b2ced154..c46e607c 100644
--- a/PVE/VZDump/QemuServer.pm
+++ b/PVE/VZDump/QemuServer.pm
@@ -1118,13 +1118,14 @@ sub snapshot {
 sub cleanup {
     my ($self, $task, $vmid) = @_;
 
-    $detach_tpmstate_drive->($task, $vmid);
-
-    if ($task->{'use-fleecing'}) {
-	detach_fleecing_images($task->{disks}, $vmid);
-	cleanup_fleecing_images($self, $task->{disks});
+    # If VM was started only for backup, it is already stopped now.
+    if (PVE::QemuServer::Helpers::vm_running_locally($vmid)) {
+	$detach_tpmstate_drive->($task, $vmid);
+	detach_fleecing_images($task->{disks}, $vmid) if $task->{'use-fleecing'};
     }
 
+    cleanup_fleecing_images($self, $task->{disks}) if $task->{'use-fleecing'};
+
     if ($self->{qmeventd_fh}) {
 	close($self->{qmeventd_fh});
     }
-- 
2.39.2



_______________________________________________
pve-devel mailing list
pve-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel


^ permalink raw reply	[flat|nested] 44+ messages in thread

* [pve-devel] [PATCH qemu-server v2 17/25] backup: keep track of block-node size instead of volume size
  2024-08-13 13:28 [pve-devel] [RFC qemu/storage/qemu-server/container/manager v2 00/25] backup provider API Fiona Ebner
                   ` (15 preceding siblings ...)
  2024-08-13 13:28 ` [pve-devel] [PATCH qemu-server v2 16/25] backup: cleanup: check if VM is running before issuing QMP commands Fiona Ebner
@ 2024-08-13 13:28 ` Fiona Ebner
  2024-08-13 13:28 ` [pve-devel] [RFC qemu-server v2 18/25] backup: allow adding fleecing images also for EFI and TPM Fiona Ebner
                   ` (8 subsequent siblings)
  25 siblings, 0 replies; 44+ messages in thread
From: Fiona Ebner @ 2024-08-13 13:28 UTC (permalink / raw)
  To: pve-devel

For fleecing, the size needs to match exactly with what QEMU sees. In
particular, EFI disks might be attached with a 'size=' option, meaning
that size can be different from the volume's size. Commit 36377acf
("backup: disk info: also keep track of size") introduced size
tracking and it was only used for fleecing since then, so replace the
existing 'size' key in the device info hash and replace it with an
explicit 'block-node-size' for clarity.

Should also help with the following issue reported in the community
forum:
https://forum.proxmox.com/threads/152202

Fixes: 36377acf ("backup: disk info: also keep track of size")
Signed-off-by: Fiona Ebner <f.ebner@proxmox.com>
---

New in v2.

 PVE/VZDump/QemuServer.pm | 20 +++++++++++++++++---
 1 file changed, 17 insertions(+), 3 deletions(-)

diff --git a/PVE/VZDump/QemuServer.pm b/PVE/VZDump/QemuServer.pm
index c46e607c..98685127 100644
--- a/PVE/VZDump/QemuServer.pm
+++ b/PVE/VZDump/QemuServer.pm
@@ -106,6 +106,9 @@ sub prepare {
 
     PVE::Storage::activate_volumes($self->{storecfg}, $vollist);
 
+    my $block_info = mon_cmd($vmid, "query-block");
+    $block_info = { map { $_->{device} => $_ } $block_info->@* };
+
     foreach my $ds (sort keys %$drivehash) {
 	my $drive = $drivehash->{$ds};
 
@@ -133,11 +136,22 @@ sub prepare {
 	    die "cannot determine size and format of volume '$volid' - $@\n" if $@;
 	}
 
+	# The size for fleecing images needs to be exactly the same size as QEMU sees. E.g. EFI disk
+	# can be attached with a smaller size then the underyling image on the storage.
+	my $block_node_size =
+	    eval { $block_info->{"drive-$ds"}->{inserted}->{image}->{'virtual-size'}; };
+	if (!$block_node_size) {
+	    # TPM state is not attached yet and will be attached with same size, so don't warn then.
+	    $self->loginfo("could not determine block node size of drive '$ds' - using fallback")
+		if $ds !~ m/^tpmstate\d+/;
+	    $block_node_size = $size;
+	}
+
 	my $diskinfo = {
 	    path => $path,
 	    volid => $volid,
 	    storeid => $storeid,
-	    size => $size,
+	    'block-node-size' => $block_node_size,
 	    format => $format,
 	    virtdev => $ds,
 	    qmdevice => "drive-$ds",
@@ -551,7 +565,7 @@ my sub allocate_fleecing_images {
 		my $name = "vm-$vmid-fleece-$n";
 		$name .= ".$format" if $scfg->{path};
 
-		my $size = PVE::Tools::convert_size($di->{size}, 'b' => 'kb');
+		my $size = PVE::Tools::convert_size($di->{'block-node-size'}, 'b' => 'kb');
 
 		$di->{'fleece-volid'} = PVE::Storage::vdisk_alloc(
 		    $self->{storecfg}, $fleecing_storeid, $vmid, $format, $name, $size);
@@ -600,7 +614,7 @@ my sub attach_fleecing_images {
 	    my $drive = "file=$path,if=none,id=$devid,format=$format,discard=unmap";
 	    # Specify size explicitly, to make it work if storage backend rounded up size for
 	    # fleecing image when allocating.
-	    $drive .= ",size=$di->{size}" if $format eq 'raw';
+	    $drive .= ",size=$di->{'block-node-size'}" if $format eq 'raw';
 	    $drive =~ s/\\/\\\\/g;
 	    my $ret = PVE::QemuServer::Monitor::hmp_cmd($vmid, "drive_add auto \"$drive\"", 60);
 	    die "attaching fleecing image $volid failed - $ret\n" if $ret !~ m/OK/s;
-- 
2.39.2



_______________________________________________
pve-devel mailing list
pve-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel


^ permalink raw reply	[flat|nested] 44+ messages in thread

* [pve-devel] [RFC qemu-server v2 18/25] backup: allow adding fleecing images also for EFI and TPM
  2024-08-13 13:28 [pve-devel] [RFC qemu/storage/qemu-server/container/manager v2 00/25] backup provider API Fiona Ebner
                   ` (16 preceding siblings ...)
  2024-08-13 13:28 ` [pve-devel] [PATCH qemu-server v2 17/25] backup: keep track of block-node size instead of volume size Fiona Ebner
@ 2024-08-13 13:28 ` Fiona Ebner
  2024-08-13 13:28 ` [pve-devel] [RFC qemu-server v2 19/25] backup: implement backup for external providers Fiona Ebner
                   ` (7 subsequent siblings)
  25 siblings, 0 replies; 44+ messages in thread
From: Fiona Ebner @ 2024-08-13 13:28 UTC (permalink / raw)
  To: pve-devel

For the external backup API, it will be necessary to add a fleecing
image even for small disks like EFI and TPM, because there is no other
place the old data could be copied to when a new guest write comes in.

Signed-off-by: Fiona Ebner <f.ebner@proxmox.com>
---

No changes in v2.

 PVE/VZDump/QemuServer.pm | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)

diff --git a/PVE/VZDump/QemuServer.pm b/PVE/VZDump/QemuServer.pm
index 98685127..4ad4a154 100644
--- a/PVE/VZDump/QemuServer.pm
+++ b/PVE/VZDump/QemuServer.pm
@@ -548,7 +548,7 @@ my sub cleanup_fleecing_images {
 }
 
 my sub allocate_fleecing_images {
-    my ($self, $disks, $vmid, $fleecing_storeid, $format) = @_;
+    my ($self, $disks, $vmid, $fleecing_storeid, $format, $all_images) = @_;
 
     die "internal error - no fleecing storage specified\n" if !$fleecing_storeid;
 
@@ -559,7 +559,8 @@ my sub allocate_fleecing_images {
 	my $n = 0; # counter for fleecing image names
 
 	for my $di ($disks->@*) {
-	    next if $di->{virtdev} =~ m/^(?:tpmstate|efidisk)\d$/; # too small to be worth it
+	    # EFI/TPM are usually too small to be worth it, but it's required for external providers
+	    next if !$all_images && $di->{virtdev} =~ m/^(?:tpmstate|efidisk)\d$/;
 	    if ($di->{type} eq 'block' || $di->{type} eq 'file') {
 		my $scfg = PVE::Storage::storage_config($self->{storecfg}, $fleecing_storeid);
 		my $name = "vm-$vmid-fleece-$n";
@@ -623,7 +624,7 @@ my sub attach_fleecing_images {
 }
 
 my sub check_and_prepare_fleecing {
-    my ($self, $vmid, $fleecing_opts, $disks, $is_template, $qemu_support) = @_;
+    my ($self, $vmid, $fleecing_opts, $disks, $is_template, $qemu_support, $all_images) = @_;
 
     # Even if the VM was started specifically for fleecing, it's possible that the VM is resumed and
     # then starts doing IO. For VMs that are not resumed the fleecing images will just stay empty,
@@ -644,7 +645,8 @@ my sub check_and_prepare_fleecing {
 	    $self->{storecfg}, $fleecing_opts->{storage});
 	my $format = scalar(grep { $_ eq 'qcow2' } $valid_formats->@*) ? 'qcow2' : 'raw';
 
-	allocate_fleecing_images($self, $disks, $vmid, $fleecing_opts->{storage}, $format);
+	allocate_fleecing_images(
+	    $self, $disks, $vmid, $fleecing_opts->{storage}, $format, $all_images);
 	attach_fleecing_images($self, $disks, $vmid, $format);
     }
 
@@ -735,7 +737,7 @@ sub archive_pbs {
 	my $is_template = PVE::QemuConfig->is_template($self->{vmlist}->{$vmid});
 
 	$task->{'use-fleecing'} = check_and_prepare_fleecing(
-	    $self, $vmid, $opts->{fleecing}, $task->{disks}, $is_template, $qemu_support);
+	    $self, $vmid, $opts->{fleecing}, $task->{disks}, $is_template, $qemu_support, 0);
 
 	my $fs_frozen = $self->qga_fs_freeze($task, $vmid);
 
@@ -919,7 +921,7 @@ sub archive_vma {
 	$attach_tpmstate_drive->($self, $task, $vmid);
 
 	$task->{'use-fleecing'} = check_and_prepare_fleecing(
-	    $self, $vmid, $opts->{fleecing}, $task->{disks}, $is_template, $qemu_support);
+	    $self, $vmid, $opts->{fleecing}, $task->{disks}, $is_template, $qemu_support, 0);
 
 	my $outfh;
 	if ($opts->{stdout}) {
-- 
2.39.2



_______________________________________________
pve-devel mailing list
pve-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel


^ permalink raw reply	[flat|nested] 44+ messages in thread

* [pve-devel] [RFC qemu-server v2 19/25] backup: implement backup for external providers
  2024-08-13 13:28 [pve-devel] [RFC qemu/storage/qemu-server/container/manager v2 00/25] backup provider API Fiona Ebner
                   ` (17 preceding siblings ...)
  2024-08-13 13:28 ` [pve-devel] [RFC qemu-server v2 18/25] backup: allow adding fleecing images also for EFI and TPM Fiona Ebner
@ 2024-08-13 13:28 ` Fiona Ebner
  2024-08-13 13:28 ` [pve-devel] [PATCH qemu-server v2 20/25] restore: die early when there is no size for a device Fiona Ebner
                   ` (6 subsequent siblings)
  25 siblings, 0 replies; 44+ messages in thread
From: Fiona Ebner @ 2024-08-13 13:28 UTC (permalink / raw)
  To: pve-devel

The state of the VM's disk images at the time the backup is started is
preserved via a snapshot-access block node. Old data is moved to the
fleecing image when new guest writes come in. The snapshot-access
block node, as well as the associated bitmap in case of incremental
backup, will be made available to the external provider. They are
exported via NBD and for 'nbd' mechanism, the NBD socket path is
passed to the provider, while for 'block-device' mechanism, the NBD
export is made accessible as a regular block device first and the
bitmap information is made available via a $next_dirty_region->()
function. For 'block-device', the 'nbdinfo' binary is required.

The provider can indicate that it wants to do an incremental backup by
returning the bitmap ID that was used for a previous backup and it
will then be told if the bitmap was newly created (either first backup
or old bitmap was invalid) or if the bitmap can be reused.

The provider then reads the parts of the NBD or block device it needs,
either the full disk for full backup, or the dirty parts according to
the bitmap for incremental backup. The bitmap has to be respected,
reads to other parts of the image will return an error. After backing
up each part of the disk, it should be discarded in the export to
avoid unnecessary space usage in the fleecing image (requires the
storage underlying the fleecing image to support discard too).

Signed-off-by: Fiona Ebner <f.ebner@proxmox.com>
---

Changes in v2:
* Support 'block-device' mechanism, by exposing the NBD export as a
  block device via qemu-nbd.
* Adapt to API changes, i.e. pass all volumes as well as configuration
  files to the provider at once.

 PVE/VZDump/QemuServer.pm | 308 ++++++++++++++++++++++++++++++++++++++-
 1 file changed, 307 insertions(+), 1 deletion(-)

diff --git a/PVE/VZDump/QemuServer.pm b/PVE/VZDump/QemuServer.pm
index 4ad4a154..9daebbc2 100644
--- a/PVE/VZDump/QemuServer.pm
+++ b/PVE/VZDump/QemuServer.pm
@@ -20,7 +20,7 @@ use PVE::QMPClient;
 use PVE::Storage::Plugin;
 use PVE::Storage::PBSPlugin;
 use PVE::Storage;
-use PVE::Tools;
+use PVE::Tools qw(run_command);
 use PVE::VZDump;
 use PVE::Format qw(render_duration render_bytes);
 
@@ -291,6 +291,8 @@ sub archive {
 
     if ($self->{vzdump}->{opts}->{pbs}) {
 	$self->archive_pbs($task, $vmid);
+    } elsif ($self->{vzdump}->{'backup-provider'}) {
+	$self->archive_external($task, $vmid);
     } else {
 	$self->archive_vma($task, $vmid, $filename, $comp);
     }
@@ -1136,6 +1138,23 @@ sub cleanup {
 
     # If VM was started only for backup, it is already stopped now.
     if (PVE::QemuServer::Helpers::vm_running_locally($vmid)) {
+	if ($task->{cleanup}->{'nbd-stop'}) {
+	    eval { PVE::QemuServer::QMPHelpers::nbd_stop($vmid); };
+	    $self->logerr($@) if $@;
+	}
+
+	if (my $info = $task->{cleanup}->{'backup-access-teardown'}) {
+	    my $params = {
+		'target-id' => $info->{'target-id'},
+		timeout => 60,
+		success => $info->{success} ? JSON::true : JSON::false,
+	    };
+
+	    $self->loginfo("tearing down backup-access");
+	    eval { mon_cmd($vmid, "backup-access-teardown", $params->%*) };
+	    $self->logerr($@) if $@;
+	}
+
 	$detach_tpmstate_drive->($task, $vmid);
 	detach_fleecing_images($task->{disks}, $vmid) if $task->{'use-fleecing'};
     }
@@ -1147,4 +1166,291 @@ sub cleanup {
     }
 }
 
+my sub block_device_backup_cleanup {
+    my ($self, $paths, $cpids) = @_;
+
+    for my $path ($paths->@*) {
+	eval { run_command(["qemu-nbd", "-d", $path ]); };
+	$self->log('warn', "unable to disconnect NBD backup source '$path' - $@") if $@;
+    }
+
+    my $waited;
+    my $wait_limit = 5;
+    for ($waited = 0; $waited < $wait_limit && scalar(keys $cpids->%*); $waited++) {
+	while ((my $cpid = waitpid(-1, POSIX::WNOHANG)) > 0) {
+	    delete($cpids->{$cpid});
+	}
+	if ($waited == 0) {
+	    kill 15, $_ for keys $cpids->%*;
+	}
+	sleep 1;
+    }
+    if ($waited == $wait_limit && scalar(keys $cpids->%*)) {
+	kill 9, $_ for keys $cpids->%*;
+	sleep 1;
+	while ((my $cpid = waitpid(-1, POSIX::WNOHANG)) > 0) {
+	    delete($cpids->{$cpid});
+	}
+	$self->log('warn', "unable to collect nbdinfo child process '$_'") for keys $cpids->%*;
+    }
+}
+
+my sub block_device_backup_prepare {
+    my ($self, $devicename, $size, $nbd_path, $bitmap_name, $count) = @_;
+
+    my $nbd_info_uri = "nbd+unix:///${devicename}?socket=${nbd_path}";
+    my $qemu_nbd_uri = "nbd:unix:${nbd_path}:exportname=${devicename}";
+
+    my $cpid;
+    my $error_fh;
+    my $next_dirty_region;
+
+    # If there is no dirty bitmap, it can be treated as if there's a full dirty one. The output of
+    # nbdinfo is a list of tuples with offset, length, type, description. The first bit of 'type' is
+    # set when the bitmap is dirty, see QEMU's docs/interop/nbd.txt
+    my $dirty_bitmap = [];
+    if ($bitmap_name) {
+	my $input = IO::File->new();
+	my $info = IO::File->new();
+	$error_fh = IO::File->new();
+	my $nbdinfo_cmd = ["nbdinfo", $nbd_info_uri, "--map=qemu:dirty-bitmap:${bitmap_name}"];
+	$cpid = open3($input, $info, $error_fh, $nbdinfo_cmd->@*)
+	    or die "failed to spawn nbdinfo child - $!\n";
+
+	$next_dirty_region = sub {
+	    my ($offset, $length, $type);
+	    do {
+		my $line = <$info>;
+		return if !$line;
+		die "unexpected output from nbdinfo - $line\n"
+		    if $line !~ m/^\s*(\d+)\s*(\d+)\s*(\d+)/; # also untaints
+		($offset, $length, $type) = ($1, $2, $3);
+	    } while (($type & 0x1) == 0); # not dirty
+	    return ($offset, $length);
+	};
+    } else {
+	my $done = 0;
+	$next_dirty_region = sub {
+	    return if $done;
+	    $done = 1;
+	    return (0, $size);
+	};
+    }
+
+    my $blockdev = "/dev/nbd${count}";
+
+    eval {
+	run_command(["qemu-nbd", "-c", $blockdev, $qemu_nbd_uri, "--format=raw", "--discard=on"]);
+    };
+    if (my $err = $@) {
+	my $cpids = {};
+	$cpids->{$cpid} = 1 if $cpid;
+	block_device_backup_cleanup($self, [$blockdev], $cpids);
+	die $err;
+    }
+
+    return ($blockdev, $next_dirty_region, $cpid);
+}
+
+my sub backup_access_to_volume_info {
+    my ($self, $backup_access_info, $mechanism, $nbd_path) = @_;
+
+    my $child_pids = {}; # used for nbdinfo calls
+    my $count = 0; # counter for block devices, i.e. /dev/nbd${count}
+    my $volumes = {};
+
+    for my $info ($backup_access_info->@*) {
+	my $bitmap_status = 'none';
+	my $bitmap_name;
+	if (my $bitmap_action = $info->{'bitmap-action'}) {
+	    my $bitmap_action_to_status = {
+		'not-used' => 'none',
+		'not-used-removed' => 'none',
+		'new' => 'new',
+		'used' => 'reuse',
+		'invalid' => 'new',
+	    };
+
+	    $bitmap_status = $bitmap_action_to_status->{$bitmap_action}
+		or die "got unexpected bitmap action '$bitmap_action'\n";
+
+	    $bitmap_name = $info->{'bitmap-name'} or die "bitmap-name is not present\n";
+	}
+
+	my ($device, $size) = $info->@{qw(device size)};
+
+	$volumes->{$device}->{'bitmap-mode'} = $bitmap_status;
+	$volumes->{$device}->{size} = $size;
+
+	if ($mechanism eq 'block-device') {
+	    my ($blockdev, $next_dirty_region, $child_pid) = block_device_backup_prepare(
+		$self, $device, $size, $nbd_path, $bitmap_name, $count);
+	    $count++;
+	    $child_pids->{$child_pid} = 1 if $child_pid;
+	    $volumes->{$device}->{path} = $blockdev;
+	    $volumes->{$device}->{'next-dirty-region'} = $next_dirty_region;
+	} elsif ($mechanism eq 'nbd') {
+	    $volumes->{$device}->{'nbd-path'} = $nbd_path;
+	    $volumes->{$device}->{'bitmap-name'} = $bitmap_name;
+	} else {
+	    die "internal error - unkown mechanism '$mechanism'";
+	}
+    }
+
+    return ($volumes, $child_pids);
+}
+
+sub archive_external {
+    my ($self, $task, $vmid) = @_;
+
+    my $config_file = "$task->{tmpdir}/qemu-server.conf";
+    my $firewall_file = "$task->{tmpdir}/qemu-server.fw";
+
+    my $opts = $self->{vzdump}->{opts};
+
+    my $backup_provider = $self->{vzdump}->{'backup-provider'};
+
+    $self->loginfo("starting external backup via " . $backup_provider->provider_name());
+
+    my $starttime = time();
+
+    # get list early so we die on unkown drive types before doing anything
+    my $devlist = _get_task_devlist($task);
+
+    $self->enforce_vm_running_for_backup($vmid);
+    $self->{qmeventd_fh} = PVE::QemuServer::register_qmeventd_handle($vmid);
+
+    eval {
+	$SIG{INT} = $SIG{TERM} = $SIG{QUIT} = $SIG{HUP} = $SIG{PIPE} = sub {
+	    die "interrupted by signal\n";
+	};
+
+	my $qemu_support = mon_cmd($vmid, "query-proxmox-support");
+
+	$attach_tpmstate_drive->($self, $task, $vmid);
+
+	my $is_template = PVE::QemuConfig->is_template($self->{vmlist}->{$vmid});
+
+	my $fleecing = check_and_prepare_fleecing(
+	    $self, $vmid, $opts->{fleecing}, $task->{disks}, $is_template, $qemu_support, 1);
+	die "cannot setup backup access without fleecing\n" if !$fleecing;
+
+	$task->{'use-fleecing'} = 1;
+
+	my $fs_frozen = $self->qga_fs_freeze($task, $vmid);
+
+	my $target_id = $opts->{storage};
+
+	my $params = {
+	    'target-id' => $target_id,
+	    devlist => $devlist,
+	    timeout => 60,
+	};
+
+	my ($mechanism, $bitmap_name) = $backup_provider->backup_get_mechanism($vmid, 'qemu');
+	die "mechanism '$mechanism' requested by backup provider is not supported for VMs\n"
+	    if $mechanism ne 'block-device' && $mechanism ne 'nbd';
+
+	if ($mechanism eq 'block-device') {
+	    # For mechanism 'block-device' the bitmap needs to be passed to the provider. The bitmap
+	    # cannot be dumped via QMP and doing it via qemu-img is experimental, so use nbdinfo.
+	    die "need 'nbdinfo' binary from package libnbd-bin\n" if !-e "/usr/bin/nbdinfo";
+
+	    # NOTE nbds_max won't change if module is already loaded
+	    run_command(["modprobe", "nbd", "nbds_max=128"]);
+	}
+
+	if ($bitmap_name) {
+	    # prepend storage ID so different providers can never cause clashes
+	    $bitmap_name = "$opts->{storage}-" . $bitmap_name;
+	    $params->{'bitmap-name'} = $bitmap_name;
+	}
+
+	$self->loginfo("setting up snapshot-access for backup");
+
+	my $backup_access_info = eval { mon_cmd($vmid, "backup-access-setup", $params->%*) };
+	my $qmperr = $@;
+
+	$task->{cleanup}->{'backup-access-teardown'} = { 'target-id' => $target_id, success => 0 };
+
+	if ($fs_frozen) {
+	    $self->qga_fs_thaw($vmid);
+	}
+
+	die $qmperr if $qmperr;
+
+	$self->resume_vm_after_job_start($task, $vmid);
+
+	my $bitmap_info = mon_cmd($vmid, 'query-pbs-bitmap-info');
+	for my $info (sort { $a->{drive} cmp $b->{drive} } $bitmap_info->@*) {
+	    my $text = $bitmap_action_to_human->($self, $info);
+	    my $drive = $info->{drive};
+	    $drive =~ s/^drive-//; # for consistency
+	    $self->loginfo("$drive: dirty-bitmap status: $text");
+	}
+
+	$self->loginfo("starting NBD server");
+
+	my $nbd_path = "/run/qemu-server/$vmid\_nbd.backup_access";
+	mon_cmd(
+	    $vmid, "nbd-server-start", addr => { type => 'unix', data => { path => $nbd_path } } );
+	$task->{cleanup}->{'nbd-stop'} = 1;
+
+	for my $info ($backup_access_info->@*) {
+	    $self->loginfo("adding NBD export for $info->{device}");
+
+	    my $export_params = {
+		id => $info->{device},
+		'node-name' => $info->{'node-name'},
+		writable => JSON::true, # for discard
+		type => "nbd",
+		name => $info->{device}, # NBD export name
+	    };
+
+	    if ($info->{'bitmap-name'}) {
+		$export_params->{bitmaps} = [{
+		    node => $info->{'bitmap-node-name'},
+		    name => $info->{'bitmap-name'},
+		}],
+	    }
+
+	    mon_cmd($vmid, "block-export-add", $export_params->%*);
+	}
+
+	my $child_pids = {}; # used for nbdinfo calls
+	my $volumes = {};
+
+	eval {
+	    ($volumes, $child_pids) =
+		backup_access_to_volume_info($self, $backup_access_info, $mechanism, $nbd_path);
+
+	    my $param = {};
+	    $param->{'bandwidth-limit'} = $opts->{bwlimit} * 1024 if $opts->{bwlimit};
+	    $param->{'firewall-config'} = $firewall_file if -e $firewall_file;
+
+	    $backup_provider->backup_vm($vmid, $config_file, $volumes, $param);
+	};
+	my $err = $@;
+
+	if ($mechanism eq 'block-device') {
+	    my $cleanup_paths = [map { $volumes->{$_}->{path} } keys $volumes->%*];
+	    block_device_backup_cleanup($self, $cleanup_paths, $child_pids)
+	}
+
+	die $err if $err;
+    };
+    my $err = $@;
+
+    if ($err) {
+	$self->logerr($err);
+	$self->resume_vm_after_job_start($task, $vmid);
+    } else {
+	$task->{size} = $backup_provider->backup_get_task_size($vmid);
+	$task->{cleanup}->{'backup-access-teardown'}->{success} = 1;
+    }
+    $self->restore_vm_power_state($vmid);
+
+    die $err if $err;
+}
+
 1;
-- 
2.39.2



_______________________________________________
pve-devel mailing list
pve-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel


^ permalink raw reply	[flat|nested] 44+ messages in thread

* [pve-devel] [PATCH qemu-server v2 20/25] restore: die early when there is no size for a device
  2024-08-13 13:28 [pve-devel] [RFC qemu/storage/qemu-server/container/manager v2 00/25] backup provider API Fiona Ebner
                   ` (18 preceding siblings ...)
  2024-08-13 13:28 ` [pve-devel] [RFC qemu-server v2 19/25] backup: implement backup for external providers Fiona Ebner
@ 2024-08-13 13:28 ` Fiona Ebner
  2024-08-13 13:28 ` [pve-devel] [RFC qemu-server v2 21/25] backup: implement restore for external providers Fiona Ebner
                   ` (5 subsequent siblings)
  25 siblings, 0 replies; 44+ messages in thread
From: Fiona Ebner @ 2024-08-13 13:28 UTC (permalink / raw)
  To: pve-devel

Makes it a clean error for buggy (external) backup providers where the
size might not be set at all.

Signed-off-by: Fiona Ebner <f.ebner@proxmox.com>
---

No changes in v2.

 PVE/QemuServer.pm | 1 +
 1 file changed, 1 insertion(+)

diff --git a/PVE/QemuServer.pm b/PVE/QemuServer.pm
index e5ff5efb..37f56f69 100644
--- a/PVE/QemuServer.pm
+++ b/PVE/QemuServer.pm
@@ -6755,6 +6755,7 @@ my $restore_allocate_devices = sub {
     my $map = {};
     foreach my $virtdev (sort keys %$virtdev_hash) {
 	my $d = $virtdev_hash->{$virtdev};
+	die "got no size for '$virtdev'\n" if !defined($d->{size});
 	my $alloc_size = int(($d->{size} + 1024 - 1)/1024);
 	my $storeid = $d->{storeid};
 	my $scfg = PVE::Storage::storage_config($storecfg, $storeid);
-- 
2.39.2



_______________________________________________
pve-devel mailing list
pve-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel


^ permalink raw reply	[flat|nested] 44+ messages in thread

* [pve-devel] [RFC qemu-server v2 21/25] backup: implement restore for external providers
  2024-08-13 13:28 [pve-devel] [RFC qemu/storage/qemu-server/container/manager v2 00/25] backup provider API Fiona Ebner
                   ` (19 preceding siblings ...)
  2024-08-13 13:28 ` [pve-devel] [PATCH qemu-server v2 20/25] restore: die early when there is no size for a device Fiona Ebner
@ 2024-08-13 13:28 ` Fiona Ebner
  2024-09-12 12:44   ` Fabian Grünbichler
  2024-08-13 13:28 ` [pve-devel] [RFC container v2 22/25] backup: implement backup " Fiona Ebner
                   ` (4 subsequent siblings)
  25 siblings, 1 reply; 44+ messages in thread
From: Fiona Ebner @ 2024-08-13 13:28 UTC (permalink / raw)
  To: pve-devel

First, the provider is asked about what restore mechanism to use.
Currently, only 'qemu-img' is possible. Then the configuration files
are restored, the provider gives information about volumes contained
in the backup and finally the volumes are restored via
'qemu-img convert'.

The code for the restore_external_archive() function was copied and
adapted from the restore_proxmox_backup_archive() function. Together
with restore_vma_archive() it seems sensible to extract the common
parts and use a dedicated module for restore code.

The parse_restore_archive() helper was renamed, because it's not just
parsing.

Signed-off-by: Fiona Ebner <f.ebner@proxmox.com>
---

Changes in v2:
* Adapt to API changes.

 PVE/API2/Qemu.pm  |  29 +++++++++-
 PVE/QemuServer.pm | 139 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 165 insertions(+), 3 deletions(-)

diff --git a/PVE/API2/Qemu.pm b/PVE/API2/Qemu.pm
index 212cfc1b..8917905e 100644
--- a/PVE/API2/Qemu.pm
+++ b/PVE/API2/Qemu.pm
@@ -845,7 +845,7 @@ __PACKAGE__->register_method({
 	return $res;
     }});
 
-my $parse_restore_archive = sub {
+my $classify_restore_archive = sub {
     my ($storecfg, $archive) = @_;
 
     my ($archive_storeid, $archive_volname) = PVE::Storage::parse_volume_id($archive, 1);
@@ -859,6 +859,21 @@ my $parse_restore_archive = sub {
 	    $res->{type} = 'pbs';
 	    return $res;
 	}
+	my $log_function = sub {
+	    my ($log_level, $message) = @_;
+	    my $prefix = $log_level eq 'err' ? 'ERROR' : uc($log_level);
+	    print "$prefix: $message\n";
+	};
+	my $backup_provider = PVE::Storage::new_backup_provider(
+	    $storecfg,
+	    $archive_storeid,
+	    $log_function,
+	);
+	if ($backup_provider) {
+	    $res->{type} = 'external';
+	    $res->{'backup-provider'} = $backup_provider;
+	    return $res;
+	}
     }
     my $path = PVE::Storage::abs_filesystem_path($storecfg, $archive);
     $res->{type} = 'file';
@@ -1011,7 +1026,7 @@ __PACKAGE__->register_method({
 		    'backup',
 		);
 
-		$archive = $parse_restore_archive->($storecfg, $archive);
+		$archive = $classify_restore_archive->($storecfg, $archive);
 	    }
 	}
 
@@ -1069,7 +1084,15 @@ __PACKAGE__->register_method({
 			PVE::QemuServer::check_restore_permissions($rpcenv, $authuser, $merged);
 		    }
 		}
-		if ($archive->{type} eq 'file' || $archive->{type} eq 'pipe') {
+		if (my $backup_provider = $archive->{'backup-provider'}) {
+		    PVE::QemuServer::restore_external_archive(
+			$backup_provider,
+			$archive->{volid},
+			$vmid,
+			$authuser,
+			$restore_options,
+		    );
+		} elsif ($archive->{type} eq 'file' || $archive->{type} eq 'pipe') {
 		    die "live-restore is only compatible with backup images from a Proxmox Backup Server\n"
 			if $live_restore;
 		    PVE::QemuServer::restore_file_archive($archive->{path} // '-', $vmid, $authuser, $restore_options);
diff --git a/PVE/QemuServer.pm b/PVE/QemuServer.pm
index 37f56f69..6cd21b7d 100644
--- a/PVE/QemuServer.pm
+++ b/PVE/QemuServer.pm
@@ -7245,6 +7245,145 @@ sub restore_proxmox_backup_archive {
     }
 }
 
+sub restore_external_archive {
+    my ($backup_provider, $archive, $vmid, $user, $options) = @_;
+
+    die "live restore from backup provider is not implemented\n" if $options->{live};
+
+    my $storecfg = PVE::Storage::config();
+
+    my ($storeid, $volname) = PVE::Storage::parse_volume_id($archive);
+    my $scfg = PVE::Storage::storage_config($storecfg, $storeid);
+
+    my $tmpdir = "/var/tmp/vzdumptmp$$";
+    rmtree $tmpdir;
+    mkpath $tmpdir;
+
+    my $conffile = PVE::QemuConfig->config_file($vmid);
+    # disable interrupts (always do cleanups)
+    local $SIG{INT} =
+	local $SIG{TERM} =
+	local $SIG{QUIT} =
+	local $SIG{HUP} = sub { print STDERR "got interrupt - ignored\n"; };
+
+    # Note: $oldconf is undef if VM does not exists
+    my $cfs_path = PVE::QemuConfig->cfs_config_path($vmid);
+    my $oldconf = PVE::Cluster::cfs_read_file($cfs_path);
+    my $new_conf_raw = '';
+
+    my $rpcenv = PVE::RPCEnvironment::get();
+    my $devinfo = {}; # info about drives included in backup
+    my $virtdev_hash = {}; # info about allocated drives
+
+    eval {
+	# enable interrupts
+	local $SIG{INT} =
+	    local $SIG{TERM} =
+	    local $SIG{QUIT} =
+	    local $SIG{HUP} =
+	    local $SIG{PIPE} = sub { die "interrupted by signal\n"; };
+
+	my $cfgfn = "$tmpdir/qemu-server.conf";
+	my $firewall_config_fn = "$tmpdir/fw.conf";
+
+	my $cmd = "restore";
+
+	my ($mechanism, $vmtype) =
+	    $backup_provider->restore_get_mechanism($volname, $storeid);
+	die "mechanism '$mechanism' requested by backup provider is not supported for VMs\n"
+	    if $mechanism ne 'qemu-img';
+	die "cannot restore non-VM guest of type '$vmtype'\n" if $vmtype ne 'qemu';
+
+	$devinfo = $backup_provider->restore_vm_init($volname, $storeid);
+
+	my $data = $backup_provider->restore_get_guest_config($volname, $storeid)
+	    or die "backup provider failed to extract guest configuration\n";
+	PVE::Tools::file_set_contents($cfgfn, $data);
+
+	if ($data = $backup_provider->restore_get_firewall_config($volname, $storeid)) {
+	    PVE::Tools::file_set_contents($firewall_config_fn, $data);
+	    my $pve_firewall_dir = '/etc/pve/firewall';
+	    mkdir $pve_firewall_dir; # make sure the dir exists
+	    PVE::Tools::file_copy($firewall_config_fn, "${pve_firewall_dir}/$vmid.fw");
+	}
+
+	my $fh = IO::File->new($cfgfn, "r") or die "unable to read qemu-server.conf - $!\n";
+
+	$virtdev_hash = $parse_backup_hints->($rpcenv, $user, $storecfg, $fh, $devinfo, $options);
+
+	# create empty/temp config
+	PVE::Tools::file_set_contents($conffile, "memory: 128\nlock: create");
+
+	$restore_cleanup_oldconf->($storecfg, $vmid, $oldconf, $virtdev_hash) if $oldconf;
+
+	# allocate volumes
+	my $map = $restore_allocate_devices->($storecfg, $virtdev_hash, $vmid);
+
+	for my $virtdev (sort keys $virtdev_hash->%*) {
+	    my $d = $virtdev_hash->{$virtdev};
+	    next if $d->{is_cloudinit}; # no need to restore cloudinit
+
+	    my $info =
+		$backup_provider->restore_vm_volume_init($volname, $storeid, $d->{devname}, {});
+	    my $source_path = $info->{'qemu-img-path'}
+		or die "did not get source image path from backup provider\n";
+	    eval {
+		qemu_img_convert(
+		    $source_path, $d->{volid}, $d->{size}, undef, 0, $options->{bwlimit});
+	    };
+	    my $err = $@;
+	    eval {
+		$backup_provider->restore_vm_volume_cleanup($volname, $storeid, $d->{devname}, {});
+	    };
+	    if (my $cleanup_err = $@) {
+		die $cleanup_err if !$err;
+		warn $cleanup_err;
+	    }
+	    die $err if $err
+	}
+
+	$fh->seek(0, 0) || die "seek failed - $!\n";
+
+	my $cookie = { netcount => 0 };
+	while (defined(my $line = <$fh>)) {
+	    $new_conf_raw .= restore_update_config_line(
+		$cookie,
+		$map,
+		$line,
+		$options->{unique},
+	    );
+	}
+
+	$fh->close();
+    };
+    my $err = $@;
+
+    eval { $backup_provider->restore_vm_cleanup($volname, $storeid); };
+    warn "backup provider cleanup after restore failed - $@" if $@;
+
+    if ($err) {
+	$restore_deactivate_volumes->($storecfg, $virtdev_hash);
+    }
+
+    rmtree $tmpdir;
+
+    if ($err) {
+	$restore_destroy_volumes->($storecfg, $virtdev_hash);
+	die $err;
+    }
+
+    my $new_conf = restore_merge_config($conffile, $new_conf_raw, $options->{override_conf});
+    check_restore_permissions($rpcenv, $user, $new_conf);
+    PVE::QemuConfig->write_config($vmid, $new_conf);
+
+    eval { rescan($vmid, 1); };
+    warn $@ if $@;
+
+    PVE::AccessControl::add_vm_to_pool($vmid, $options->{pool}) if $options->{pool};
+
+    return;
+}
+
 sub pbs_live_restore {
     my ($vmid, $conf, $storecfg, $restored_disks, $opts) = @_;
 
-- 
2.39.2



_______________________________________________
pve-devel mailing list
pve-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel


^ permalink raw reply	[flat|nested] 44+ messages in thread

* [pve-devel] [RFC container v2 22/25] backup: implement backup for external providers
  2024-08-13 13:28 [pve-devel] [RFC qemu/storage/qemu-server/container/manager v2 00/25] backup provider API Fiona Ebner
                   ` (20 preceding siblings ...)
  2024-08-13 13:28 ` [pve-devel] [RFC qemu-server v2 21/25] backup: implement restore for external providers Fiona Ebner
@ 2024-08-13 13:28 ` Fiona Ebner
  2024-09-12 12:43   ` Fabian Grünbichler
  2024-08-13 13:28 ` [pve-devel] [RFC container v2 23/25] backup: implement restore " Fiona Ebner
                   ` (3 subsequent siblings)
  25 siblings, 1 reply; 44+ messages in thread
From: Fiona Ebner @ 2024-08-13 13:28 UTC (permalink / raw)
  To: pve-devel

The filesystem structure is made available as a directory in a
consistent manner (with details depending on the vzdump backup mode)
just like for regular backup via tar.

The backup provider needs to back up the guest and firewall
configuration and then the filesystem structure, honoring the ID maps
(for unprivileged containers) as well as file exclusions and the
bandwidth limit.

Signed-off-by: Fiona Ebner <f.ebner@proxmox.com>
---

Changes in v2:
* Adapt to API changes.

 src/PVE/VZDump/LXC.pm | 22 +++++++++++++++++++++-
 1 file changed, 21 insertions(+), 1 deletion(-)

diff --git a/src/PVE/VZDump/LXC.pm b/src/PVE/VZDump/LXC.pm
index 67d13db..0fc2a94 100644
--- a/src/PVE/VZDump/LXC.pm
+++ b/src/PVE/VZDump/LXC.pm
@@ -373,7 +373,27 @@ sub archive {
     my $userns_cmd = $task->{userns_cmd};
     my $findexcl = $self->{vzdump}->{findexcl};
 
-    if ($self->{vzdump}->{opts}->{pbs}) {
+    if (my $backup_provider = $self->{vzdump}->{'backup-provider'}) {
+	$self->loginfo("starting external backup via " . $backup_provider->provider_name());
+
+	my ($mechanism) = $backup_provider->backup_get_mechanism($vmid, 'lxc');
+	die "mechanism '$mechanism' requested by backup provider is not supported for containers\n"
+	    if $mechanism ne 'directory';
+
+	my $config_file = "$tmpdir/etc/vzdump/pct.conf";
+	my $firewall_file = "$tmpdir/etc/vzdump/pct.fw";
+
+
+	my $conf = PVE::LXC::Config->load_config($vmid);
+	my ($id_map, undef, undef) = PVE::LXC::parse_id_maps($conf);
+	my $info = {
+	    directory => $snapdir,
+	    sources => [@sources],
+	};
+	$info->{'firewall-config'} = $firewall_file if -e $firewall_file;
+	$info->{'bandwidth-limit'} = $opts->{bwlimit} * 1024 if $opts->{bwlimit};
+	$backup_provider->backup_container($vmid, $config_file, $id_map, $findexcl, $info);
+    } elsif ($self->{vzdump}->{opts}->{pbs}) {
 
 	my $param = [];
 	push @$param, "pct.conf:$tmpdir/etc/vzdump/pct.conf";
-- 
2.39.2



_______________________________________________
pve-devel mailing list
pve-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel


^ permalink raw reply	[flat|nested] 44+ messages in thread

* [pve-devel] [RFC container v2 23/25] backup: implement restore for external providers
  2024-08-13 13:28 [pve-devel] [RFC qemu/storage/qemu-server/container/manager v2 00/25] backup provider API Fiona Ebner
                   ` (21 preceding siblings ...)
  2024-08-13 13:28 ` [pve-devel] [RFC container v2 22/25] backup: implement backup " Fiona Ebner
@ 2024-08-13 13:28 ` Fiona Ebner
  2024-09-12 12:43   ` Fabian Grünbichler
  2024-08-13 13:28 ` [pve-devel] [PATCH manager v2 24/25] ui: backup: also check for backup subtype to classify archive Fiona Ebner
                   ` (2 subsequent siblings)
  25 siblings, 1 reply; 44+ messages in thread
From: Fiona Ebner @ 2024-08-13 13:28 UTC (permalink / raw)
  To: pve-devel

First, the provider is asked about what restore mechanism to use.
Currently, 'directory' and 'tar' are possible, for restoring either
from a directory containing the full filesystem structure (for which
rsync is used) or a potentially compressed tar file containing the
same.

The new functions are copied and adapted from the existing ones for
PBS or tar and it might be worth to factor out the common parts.

Signed-off-by: Fiona Ebner <f.ebner@proxmox.com>
---

Changes in v2:
* Adapt to API changes.

 src/PVE/LXC/Create.pm | 141 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 141 insertions(+)

diff --git a/src/PVE/LXC/Create.pm b/src/PVE/LXC/Create.pm
index 117103c..9d1c337 100644
--- a/src/PVE/LXC/Create.pm
+++ b/src/PVE/LXC/Create.pm
@@ -25,6 +25,24 @@ sub restore_archive {
 	if ($scfg->{type} eq 'pbs') {
 	    return restore_proxmox_backup_archive($storage_cfg, $archive, $rootdir, $conf, $no_unpack_error, $bwlimit);
 	}
+	my $log_function = sub {
+	    my ($log_level, $message) = @_;
+	    my $prefix = $log_level eq 'err' ? 'ERROR' : uc($log_level);
+	    print "$prefix: $message\n";
+	};
+	my $backup_provider =
+	    PVE::Storage::new_backup_provider($storage_cfg, $storeid, $log_function);
+	if ($backup_provider) {
+	    return restore_external_archive(
+		$backup_provider,
+		$storeid,
+		$volname,
+		$rootdir,
+		$conf,
+		$no_unpack_error,
+		$bwlimit,
+	    );
+	}
     }
 
     $archive = PVE::Storage::abs_filesystem_path($storage_cfg, $archive) if $archive ne '-';
@@ -118,6 +136,55 @@ sub restore_tar_archive {
     die $err if $err && !$no_unpack_error;
 }
 
+sub restore_external_archive {
+    my ($backup_provider, $storeid, $volname, $rootdir, $conf, $no_unpack_error, $bwlimit) = @_;
+
+    my ($mechanism, $vmtype) = $backup_provider->restore_get_mechanism($volname, $storeid);
+    die "cannot restore non-LXC guest of type '$vmtype'\n" if $vmtype ne 'lxc';
+
+    my $info = $backup_provider->restore_container_init($volname, $storeid, {});
+    eval {
+	if ($mechanism eq 'tar') {
+	    my $tar_path = $info->{'tar-path'}
+		or die "did not get path to tar file from backup provider\n";
+	    die "not a regular file '$tar_path'" if !-f $tar_path;
+	    restore_tar_archive($tar_path, $rootdir, $conf, $no_unpack_error, $bwlimit);
+	} elsif ($mechanism eq 'directory') {
+	    my $directory = $info->{'archive-directory'}
+		or die "did not get path to archive directory from backup provider\n";
+	    die "not a directory '$directory'" if !-d $directory;
+
+	    my $rsync = ['rsync', '--stats', '-h', '-X', '-A', '--numeric-ids', '-aH', '--delete',
+		'--no-whole-file', '--sparse', '--one-file-system', '--relative'];
+	    push $rsync->@*, '--bwlimit', $bwlimit if $bwlimit;
+	    push $rsync->@*, "${directory}/./", $rootdir;
+
+	    my $transferred = '';
+	    my $outfunc = sub {
+		return if $_[0] !~ /^Total transferred file size: (.+)$/;
+		$transferred = $1;
+	    };
+	    my $errfunc = sub { log_warn($_[0]); };
+
+	    my $starttime = time();
+	    PVE::Tools::run_command($rsync, outfunc => $outfunc, errfunc => $errfunc);
+	    my $delay = time () - $starttime;
+
+	    print "sync finished - transferred ${transferred} in ${delay}s\n";
+	} else {
+	    die "mechanism '$mechanism' requested by backup provider is not supported for LXCs\n";
+	}
+    };
+    my $err = $@;
+    eval { $backup_provider->restore_container_cleanup($volname, $storeid, {}); };
+    if (my $cleanup_err = $@) {
+	die $cleanup_err if !$err;
+	warn $cleanup_err;
+    }
+    die $err if $err;
+
+}
+
 sub recover_config {
     my ($storage_cfg, $volid, $vmid) = @_;
 
@@ -126,6 +193,8 @@ sub recover_config {
 	my $scfg = PVE::Storage::storage_check_enabled($storage_cfg, $storeid);
 	if ($scfg->{type} eq 'pbs') {
 	    return recover_config_from_proxmox_backup($storage_cfg, $volid, $vmid);
+	} elsif (PVE::Storage::new_backup_provider($storage_cfg, $storeid, sub {})) {
+	    return recover_config_from_external_backup($storage_cfg, $volid, $vmid);
 	}
     }
 
@@ -200,6 +269,26 @@ sub recover_config_from_tar {
     return wantarray ? ($conf, $mp_param) : $conf;
 }
 
+sub recover_config_from_external_backup {
+    my ($storage_cfg, $volid, $vmid) = @_;
+
+    $vmid //= 0;
+
+    my $raw = PVE::Storage::extract_vzdump_config($storage_cfg, $volid);
+
+    my $conf = PVE::LXC::Config::parse_pct_config("/lxc/${vmid}.conf" , $raw);
+
+    delete $conf->{snapshots};
+
+    my $mp_param = {};
+    PVE::LXC::Config->foreach_volume($conf, sub {
+	my ($ms, $mountpoint) = @_;
+	$mp_param->{$ms} = $conf->{$ms};
+    });
+
+    return wantarray ? ($conf, $mp_param) : $conf;
+}
+
 sub restore_configuration {
     my ($vmid, $storage_cfg, $archive, $rootdir, $conf, $restricted, $unique, $skip_fw) = @_;
 
@@ -209,6 +298,26 @@ sub restore_configuration {
 	if ($scfg->{type} eq 'pbs') {
 	    return restore_configuration_from_proxmox_backup($vmid, $storage_cfg, $archive, $rootdir, $conf, $restricted, $unique, $skip_fw);
 	}
+	my $log_function = sub {
+	    my ($log_level, $message) = @_;
+	    my $prefix = $log_level eq 'err' ? 'ERROR' : uc($log_level);
+	    print "$prefix: $message\n";
+	};
+	my $backup_provider =
+	    PVE::Storage::new_backup_provider($storage_cfg, $storeid, $log_function);
+	if ($backup_provider) {
+	    return restore_configuration_from_external_backup(
+		$backup_provider,
+		$vmid,
+		$storage_cfg,
+		$archive,
+		$rootdir,
+		$conf,
+		$restricted,
+		$unique,
+		$skip_fw,
+	    );
+	}
     }
     restore_configuration_from_etc_vzdump($vmid, $rootdir, $conf, $restricted, $unique, $skip_fw);
 }
@@ -249,6 +358,38 @@ sub restore_configuration_from_proxmox_backup {
     }
 }
 
+sub restore_configuration_from_external_backup {
+    my ($backup_provider, $vmid, $storage_cfg, $archive, $rootdir, $conf, $restricted, $unique, $skip_fw) = @_;
+
+    my ($storeid, $volname) = PVE::Storage::parse_volume_id($archive);
+    my $scfg = PVE::Storage::storage_config($storage_cfg, $storeid);
+
+    my ($vtype, $name, undef, undef, undef, undef, $format) =
+	PVE::Storage::parse_volname($storage_cfg, $archive);
+
+    my $oldconf = recover_config_from_external_backup($storage_cfg, $archive, $vmid);
+
+    sanitize_and_merge_config($conf, $oldconf, $restricted, $unique);
+
+    my $firewall_config =
+	$backup_provider->restore_get_firewall_config($volname, $storeid);
+
+    if ($firewall_config) {
+	my $pve_firewall_dir = '/etc/pve/firewall';
+	my $pct_fwcfg_target = "${pve_firewall_dir}/${vmid}.fw";
+	if ($skip_fw) {
+	    warn "ignoring firewall config from backup archive, lacking API permission to modify firewall.\n";
+	    warn "old firewall configuration in '$pct_fwcfg_target' left in place!\n"
+		if -e $pct_fwcfg_target;
+	} else {
+	    mkdir $pve_firewall_dir; # make sure the directory exists
+	    PVE::Tools::file_set_contents($pct_fwcfg_target, $firewall_config);
+	}
+    }
+
+    return;
+}
+
 sub sanitize_and_merge_config {
     my ($conf, $oldconf, $restricted, $unique) = @_;
 
-- 
2.39.2



_______________________________________________
pve-devel mailing list
pve-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel


^ permalink raw reply	[flat|nested] 44+ messages in thread

* [pve-devel] [PATCH manager v2 24/25] ui: backup: also check for backup subtype to classify archive
  2024-08-13 13:28 [pve-devel] [RFC qemu/storage/qemu-server/container/manager v2 00/25] backup provider API Fiona Ebner
                   ` (22 preceding siblings ...)
  2024-08-13 13:28 ` [pve-devel] [RFC container v2 23/25] backup: implement restore " Fiona Ebner
@ 2024-08-13 13:28 ` Fiona Ebner
  2024-08-13 13:28 ` [pve-devel] [RFC manager v2 25/25] backup: implement backup for external providers Fiona Ebner
  2024-09-12 12:43 ` [pve-devel] [RFC qemu/storage/qemu-server/container/manager v2 00/25] backup provider API Fabian Grünbichler
  25 siblings, 0 replies; 44+ messages in thread
From: Fiona Ebner @ 2024-08-13 13:28 UTC (permalink / raw)
  To: pve-devel

In anticipation of future storage plugins that might not have
PBS-specific formats or adhere to the vzdump naming scheme for
backups.

Signed-off-by: Fiona Ebner <f.ebner@proxmox.com>
---

No changes in v2.

 www/manager6/Utils.js              | 10 ++++++----
 www/manager6/grid/BackupView.js    |  4 ++--
 www/manager6/storage/BackupView.js |  4 ++--
 3 files changed, 10 insertions(+), 8 deletions(-)

diff --git a/www/manager6/Utils.js b/www/manager6/Utils.js
index db86fa9a..a8e4e8ee 100644
--- a/www/manager6/Utils.js
+++ b/www/manager6/Utils.js
@@ -693,12 +693,14 @@ Ext.define('PVE.Utils', {
 	'snippets': gettext('Snippets'),
     },
 
-    volume_is_qemu_backup: function(volid, format) {
-	return format === 'pbs-vm' || volid.match(':backup/vzdump-qemu-');
+    volume_is_qemu_backup: function(volume) {
+	return volume.format === 'pbs-vm' || volume.volid.match(':backup/vzdump-qemu-') ||
+	    volume.subtype === 'qemu';
     },
 
-    volume_is_lxc_backup: function(volid, format) {
-	return format === 'pbs-ct' || volid.match(':backup/vzdump-(lxc|openvz)-');
+    volume_is_lxc_backup: function(volume) {
+	return volume.format === 'pbs-ct' || volume.volid.match(':backup/vzdump-(lxc|openvz)-') ||
+	    volume.subtype === 'lxc';
     },
 
     authSchema: {
diff --git a/www/manager6/grid/BackupView.js b/www/manager6/grid/BackupView.js
index e71d1c88..ef3649c6 100644
--- a/www/manager6/grid/BackupView.js
+++ b/www/manager6/grid/BackupView.js
@@ -29,11 +29,11 @@ Ext.define('PVE.grid.BackupView', {
 	var vmtypeFilter;
 	if (vmtype === 'lxc' || vmtype === 'openvz') {
 	    vmtypeFilter = function(item) {
-		return PVE.Utils.volume_is_lxc_backup(item.data.volid, item.data.format);
+		return PVE.Utils.volume_is_lxc_backup(item.data);
 	    };
 	} else if (vmtype === 'qemu') {
 	    vmtypeFilter = function(item) {
-		return PVE.Utils.volume_is_qemu_backup(item.data.volid, item.data.format);
+		return PVE.Utils.volume_is_qemu_backup(item.data);
 	    };
 	} else {
 	    throw "unsupported VM type '" + vmtype + "'";
diff --git a/www/manager6/storage/BackupView.js b/www/manager6/storage/BackupView.js
index 878e1c8f..ad6e6a01 100644
--- a/www/manager6/storage/BackupView.js
+++ b/www/manager6/storage/BackupView.js
@@ -84,9 +84,9 @@ Ext.define('PVE.storage.BackupView', {
 		disabled: true,
 		handler: function(b, e, rec) {
 		    let vmtype;
-		    if (PVE.Utils.volume_is_qemu_backup(rec.data.volid, rec.data.format)) {
+		    if (PVE.Utils.volume_is_qemu_backup(rec.data)) {
 			vmtype = 'qemu';
-		    } else if (PVE.Utils.volume_is_lxc_backup(rec.data.volid, rec.data.format)) {
+		    } else if (PVE.Utils.volume_is_lxc_backup(rec.data)) {
 			vmtype = 'lxc';
 		    } else {
 			return;
-- 
2.39.2



_______________________________________________
pve-devel mailing list
pve-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel


^ permalink raw reply	[flat|nested] 44+ messages in thread

* [pve-devel] [RFC manager v2 25/25] backup: implement backup for external providers
  2024-08-13 13:28 [pve-devel] [RFC qemu/storage/qemu-server/container/manager v2 00/25] backup provider API Fiona Ebner
                   ` (23 preceding siblings ...)
  2024-08-13 13:28 ` [pve-devel] [PATCH manager v2 24/25] ui: backup: also check for backup subtype to classify archive Fiona Ebner
@ 2024-08-13 13:28 ` Fiona Ebner
  2024-09-12 12:43 ` [pve-devel] [RFC qemu/storage/qemu-server/container/manager v2 00/25] backup provider API Fabian Grünbichler
  25 siblings, 0 replies; 44+ messages in thread
From: Fiona Ebner @ 2024-08-13 13:28 UTC (permalink / raw)
  To: pve-devel

Hooks from the backup provider are called during start/end/abort for
both job and backup. And it is necessary to adapt some log messages
and special case some things like is already done for PBS, e.g. log
file handling.

Signed-off-by: Fiona Ebner <f.ebner@proxmox.com>
---

Changes in v2:
* Adapt to API changes.

 PVE/VZDump.pm           | 62 ++++++++++++++++++++++++++++++++++++-----
 test/vzdump_new_test.pl |  3 ++
 2 files changed, 58 insertions(+), 7 deletions(-)

diff --git a/PVE/VZDump.pm b/PVE/VZDump.pm
index f1a6b220..2b3e7b1c 100644
--- a/PVE/VZDump.pm
+++ b/PVE/VZDump.pm
@@ -206,7 +206,15 @@ sub storage_info {
     $info->{'prune-backups'} = PVE::JSONSchema::parse_property_string('prune-backups', $scfg->{'prune-backups'})
 	if defined($scfg->{'prune-backups'});
 
-    if ($type eq 'pbs') {
+    my $backup_provider = PVE::Storage::new_backup_provider(
+	$cfg,
+	$storage,
+	sub { debugmsg($_[0], $_[1]); },
+    );
+
+    if ($backup_provider) {
+	$info->{'backup-provider'} = $backup_provider
+    } elsif ($type eq 'pbs') {
 	$info->{pbs} = 1;
     } else {
 	$info->{dumpdir} = PVE::Storage::get_backup_dir($cfg, $storage);
@@ -706,6 +714,7 @@ sub new {
 	    $opts->{scfg} = $info->{scfg};
 	    $opts->{pbs} = $info->{pbs};
 	    $opts->{'prune-backups'} //= $info->{'prune-backups'};
+	    $self->{'backup-provider'} = $info->{'backup-provider'} if $info->{'backup-provider'};
 	}
     } elsif ($opts->{dumpdir}) {
 	$add_error->("dumpdir '$opts->{dumpdir}' does not exist")
@@ -990,7 +999,7 @@ sub exec_backup_task {
 	    }
 	}
 
-	if (!$self->{opts}->{pbs}) {
+	if (!$self->{opts}->{pbs} && !$self->{'backup-provider'}) {
 	    $task->{logfile} = "$opts->{dumpdir}/$basename.log";
 	}
 
@@ -1000,7 +1009,11 @@ sub exec_backup_task {
 	    $ext .= ".${comp_ext}";
 	}
 
-	if ($self->{opts}->{pbs}) {
+	if ($self->{'backup-provider'}) {
+	    die "unable to pipe backup to stdout\n" if $opts->{stdout};
+	    $task->{target} = $self->{'backup-provider'}->backup_get_archive_name(
+		$vmid, $vmtype, $task->{backup_time});
+	} elsif ($self->{opts}->{pbs}) {
 	    die "unable to pipe backup to stdout\n" if $opts->{stdout};
 	    $task->{target} = $pbs_snapshot_name;
 	} else {
@@ -1018,7 +1031,7 @@ sub exec_backup_task {
 	my $pid = $$;
 	if ($opts->{tmpdir}) {
 	    $task->{tmpdir} = "$opts->{tmpdir}/vzdumptmp${pid}_$vmid/";
-	} elsif ($self->{opts}->{pbs}) {
+	} elsif ($self->{opts}->{pbs} || $self->{'backup-provider'}) {
 	    $task->{tmpdir} = "/var/tmp/vzdumptmp${pid}_$vmid";
 	} else {
 	    # dumpdir is posix? then use it as temporary dir
@@ -1090,6 +1103,10 @@ sub exec_backup_task {
 	if ($mode eq 'stop') {
 	    $plugin->prepare ($task, $vmid, $mode);
 
+	    if ($self->{'backup-provider'}) {
+		$self->{'backup-provider'}->backup_hook(
+		    'start', $vmid, $vmtype, { 'start-time' => $task->{backup_time} });
+	    }
 	    $self->run_hook_script ('backup-start', $task, $logfd);
 
 	    if ($running) {
@@ -1104,6 +1121,10 @@ sub exec_backup_task {
 	} elsif ($mode eq 'suspend') {
 	    $plugin->prepare ($task, $vmid, $mode);
 
+	    if ($self->{'backup-provider'}) {
+		$self->{'backup-provider'}->backup_hook(
+		    'start', $vmid, $vmtype, { 'start-time' => $task->{backup_time} });
+	    }
 	    $self->run_hook_script ('backup-start', $task, $logfd);
 
 	    if ($vmtype eq 'lxc') {
@@ -1130,6 +1151,10 @@ sub exec_backup_task {
 	    }
 
 	} elsif ($mode eq 'snapshot') {
+	    if ($self->{'backup-provider'}) {
+		$self->{'backup-provider'}->backup_hook(
+		    'start', $vmid, $vmtype, { 'start-time' => $task->{backup_time} });
+	    }
 	    $self->run_hook_script ('backup-start', $task, $logfd);
 
 	    my $snapshot_count = $task->{snapshot_count} || 0;
@@ -1172,11 +1197,13 @@ sub exec_backup_task {
 	    return;
 	}
 
-	my $archive_txt = $self->{opts}->{pbs} ? 'Proxmox Backup Server' : 'vzdump';
+	my $archive_txt = 'vzdump';
+	$archive_txt = 'Proxmox Backup Server' if $self->{opts}->{pbs};
+	$archive_txt = $self->{'backup-provider'}->provider_name() if $self->{'backup-provider'};
 	debugmsg('info', "creating $archive_txt archive '$task->{target}'", $logfd);
 	$plugin->archive($task, $vmid, $task->{tmptar}, $comp);
 
-	if ($self->{opts}->{pbs}) {
+	if ($self->{'backup-provider'} || $self->{opts}->{pbs}) {
 	    # size is added to task struct in guest vzdump plugins
 	} else {
 	    rename ($task->{tmptar}, $task->{target}) ||
@@ -1190,7 +1217,8 @@ sub exec_backup_task {
 
 	# Mark as protected before pruning.
 	if (my $storeid = $opts->{storage}) {
-	    my $volname = $opts->{pbs} ? $task->{target} : basename($task->{target});
+	    my $volname = $opts->{pbs} || $self->{'backup-provider'} ? $task->{target}
+	                                                             : basename($task->{target});
 	    my $volid = "${storeid}:backup/${volname}";
 
 	    if ($opts->{'notes-template'} && $opts->{'notes-template'} ne '') {
@@ -1243,6 +1271,8 @@ sub exec_backup_task {
 	    debugmsg ('info', "pruned $pruned backup(s)${log_pruned_extra}", $logfd);
 	}
 
+	$self->{'backup-provider'}->backup_hook('end', $vmid, $vmtype, {})
+	    if $self->{'backup-provider'};
 	$self->run_hook_script ('backup-end', $task, $logfd);
     };
     my $err = $@;
@@ -1302,6 +1332,14 @@ sub exec_backup_task {
 	debugmsg ('err', "Backup of VM $vmid failed - $err", $logfd, 1);
 	debugmsg ('info', "Failed at " . strftime("%F %H:%M:%S", localtime()));
 
+	if ($self->{'backup-provider'}) {
+	    eval {
+		$self->{'backup-provider'}->backup_hook(
+		    'abort', $vmid, $task->{vmtype}, { error => $err });
+	    };
+	    debugmsg('warn', "hook 'backup-abort' for external provider failed - $@") if $@;
+	}
+
 	eval { $self->run_hook_script ('backup-abort', $task, $logfd); };
 	debugmsg('warn', $@) if $@; # message already contains command with phase name
 
@@ -1329,6 +1367,8 @@ sub exec_backup_task {
 		};
 		debugmsg('warn', "$@") if $@; # $@ contains already error prefix
 	    }
+	} elsif ($self->{'backup-provider'}) {
+	    $self->{'backup-provider'}->backup_handle_log_file($vmid, $task->{tmplog});
 	} elsif ($task->{logfile}) {
 	    system {'cp'} 'cp', $task->{tmplog}, $task->{logfile};
 	}
@@ -1387,6 +1427,8 @@ sub exec_backup {
     my $errcount = 0;
     eval {
 
+	$self->{'backup-provider'}->job_hook('start', { 'start-time' => $starttime })
+	    if $self->{'backup-provider'};
 	$self->run_hook_script ('job-start', undef, $job_start_fd);
 
 	foreach my $task (@$tasklist) {
@@ -1394,11 +1436,17 @@ sub exec_backup {
 	    $errcount += 1 if $task->{state} ne 'ok';
 	}
 
+	$self->{'backup-provider'}->job_hook('end') if $self->{'backup-provider'};
 	$self->run_hook_script ('job-end', undef, $job_end_fd);
     };
     my $err = $@;
 
     if ($err) {
+	if ($self->{'backup-provider'}) {
+	    eval { $self->{'backup-provider'}->job_hook('abort', { error => $err }); };
+	    $err .= "hook 'job-abort' for external provider failed - $@" if $@;
+	}
+
 	eval { $self->run_hook_script ('job-abort', undef, $job_end_fd); };
 	$err .= $@ if $@;
 	debugmsg ('err', "Backup job failed - $err", undef, 1);
diff --git a/test/vzdump_new_test.pl b/test/vzdump_new_test.pl
index 8cd73075..01f2a661 100755
--- a/test/vzdump_new_test.pl
+++ b/test/vzdump_new_test.pl
@@ -51,6 +51,9 @@ $pve_storage_module->mock(
     activate_storage => sub {
 	return;
     },
+    get_backup_provider => sub {
+	return;
+    },
 );
 
 my $pve_cluster_module = Test::MockModule->new('PVE::Cluster');
-- 
2.39.2



_______________________________________________
pve-devel mailing list
pve-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel


^ permalink raw reply	[flat|nested] 44+ messages in thread

* Re: [pve-devel] [RFC qemu/storage/qemu-server/container/manager v2 00/25] backup provider API
  2024-08-13 13:28 [pve-devel] [RFC qemu/storage/qemu-server/container/manager v2 00/25] backup provider API Fiona Ebner
                   ` (24 preceding siblings ...)
  2024-08-13 13:28 ` [pve-devel] [RFC manager v2 25/25] backup: implement backup for external providers Fiona Ebner
@ 2024-09-12 12:43 ` Fabian Grünbichler
  2024-09-12 15:31   ` Thomas Lamprecht
  25 siblings, 1 reply; 44+ messages in thread
From: Fabian Grünbichler @ 2024-09-12 12:43 UTC (permalink / raw)
  To: Proxmox VE development discussion

On August 13, 2024 3:28 pm, Fiona Ebner wrote:
> [snipped..]

> Container mechanism 'directory':
> 
> The backup provider gives the path to a directory with the full
> filesystem structure of the container.
> 
> Container mechanism 'directory':
> 
> The backup provider gives the path to a (potentially compressed) tar
> archive with the full filesystem structure of the container.

this seems duplicated or wrongly copy-pasted? it might make sense to
describe in more detail how the directory/tar should look like
- mapped users (guest view, not host view, like we do)
- what does "full filesystem structure" mean? (rootfs + all persistent
  mps in one hierarchy, with no top-level dirs that need to be stripped)

left some comments on the individual patches, the big picture looks good
to me.

I do wonder whether we want to support the Borg and Example plugins
though? if not, it might make sense to not ship them (but maybe just
test them?)..

there's a pretty tight coupling between storage plugin and backup
provider plugin - that might lead to some complaints (e.g., I can
imaging quite a few backup providers that just require some local file
system for temp storage, and users wondering why they can't just enable
that for an existing dir storage). it does make some things easier
though, so I am not sure we need to change that, just wanted to draw
attention to it.


_______________________________________________
pve-devel mailing list
pve-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel


^ permalink raw reply	[flat|nested] 44+ messages in thread

* Re: [pve-devel] [RFC storage v2 10/25] plugin: introduce new_backup_provider() method
  2024-08-13 13:28 ` [pve-devel] [RFC storage v2 10/25] plugin: introduce new_backup_provider() method Fiona Ebner
@ 2024-09-12 12:43   ` Fabian Grünbichler
  2024-09-12 13:21     ` Fiona Ebner
  0 siblings, 1 reply; 44+ messages in thread
From: Fabian Grünbichler @ 2024-09-12 12:43 UTC (permalink / raw)
  To: Proxmox VE development discussion

high-level comment: nicely documented! but wow is POD annoying to read
in source form ;)

On August 13, 2024 3:28 pm, Fiona Ebner wrote:
> The new_backup_provider() method can be used by storage plugins for
> external backup providers. If the method returns a provider, Proxmox
> VE will use callbacks to that provider for backups and restore instead
> of using its usual backup/restore mechanisms.
> 
> API age and version are both bumped.
> 
> The backup provider API is split into two parts, both of which again
> need different implementations for VM and LXC guests:
> 
> 1. Backup API
> 
> There are two hook callback functions, namely:
> 1. job_hook() is called during the start/end/abort phases of the
>    whole backup job.
> 2. backup_hook() is called during the start/end/abort phases of the
>    backup of an individual guest.
> 
> The backup_get_mechanism() method is used to decide on the backup
> mechanism. Currently, 'block-device' or 'nbd' for VMs, and 'directory'
> for containers is possible. The method also let's the plugin indicate
> whether to use a bitmap for incremental VM backup or not. It is enough
> to implement one mechanism for VMs and one mechanism for containers.
> 
> Next, there are methods for backing up the guest's configuration and
> data, backup_vm() for VM backup and backup_container() for container
> backup.
> 
> Finally, some helpers like getting the provider name or volume ID for
> the backup target, as well as for handling the backup log.
> 
> 1.1 Backup Mechanisms
> 
> VM:
> 
> Access to the data on the VM's disk from the time the backup started
> is made available via a so-called "snapshot access". This is either
> the full image, or in case a bitmap is used, the dirty parts of the
> image since the last time the bitmap was used for a successful backup.
> Reading outside of the dirty parts will result in an error. After
> backing up each part of the disk, it should be discarded in the export
> to avoid unnecessary space usage on the Proxmox VE side (there is an
> associated fleecing image).
> 
> VM mechanism 'block-device':
> 
> The snapshot access is exposed as a block device. If used, a bitmap is
> passed along.
> 
> VM mechanism 'nbd':
> 
> The snapshot access and, if used, bitmap are exported via NBD.
> 
> Container mechanism 'directory':
> 
> A copy or snapshot of the container's filesystem state is made
> available as a directory.
> 
> 2. Restore API
> 
> The restore_get_mechanism() method is used to decide on the restore
> mechanism. Currently, 'qemu-img' for VMs, and 'directory' or 'tar' for
> containers are possible. It is enough to implement one mechanism for
> VMs and one mechanism for containers.
> 
> Next, methods for extracting the guest and firewall configuration and
> the implementations of the restore mechanism via a pair of methods: an
> init method, for making the data available to Proxmox VE and a cleanup
> method that is called after restore.
> 
> For VMs, there also is a restore_vm_get_device_info() helper required,
> to get the disks included in the backup and their sizes.
> 
> 2.1. Restore Mechanisms
> 
> VM mechanism 'qemu-img':
> 
> The backup provider gives a path to the disk image that will be
> restored. The path needs to be something 'qemu-img' can deal with,
> e.g. can also be an NBD URI or similar.
> 
> Container mechanism 'directory':
> 
> The backup provider gives the path to a directory with the full
> filesystem structure of the container.
> 
> Container mechanism 'directory':
> 
> The backup provider gives the path to a (potentially compressed) tar
> archive with the full filesystem structure of the container.

same as in the cover letter ;) base on the code here I assume the second
one should be tar. I wonder whether just tar wouldn't be enough (easier
to not get ACLs/xattrs/ownership/.. right)?

> 
> See the PVE::BackupProvider::Plugin module for the full API
> documentation.
> 
> Signed-off-by: Fiona Ebner <f.ebner@proxmox.com>
> ---
> 

[snip..]

> diff --git a/src/PVE/Storage/Plugin.pm b/src/PVE/Storage/Plugin.pm
> index 6444390..d5b76ae 100644
> --- a/src/PVE/Storage/Plugin.pm
> +++ b/src/PVE/Storage/Plugin.pm
> @@ -1755,6 +1755,21 @@ sub rename_volume {
>      return "${storeid}:${base}${target_vmid}/${target_volname}";
>  }
>  
> +# Used by storage plugins for external backup providers. See PVE::BackupProvider::Plugin for the API
> +# the provider needs to implement.
> +#
> +# $scfg - the storage configuration
> +# $storeid - the storage ID
> +# $log_function($log_level, $message) - this log function can be used to write to the backup task
> +#   log in Proxmox VE. $log_level is 'info', 'warn' or 'err', $message is the message to be printed.
> +#
> +# Returns a blessed reference to the backup provider class.
> +sub new_backup_provider {
> +    my ($class, $scfg, $storeid, $log_function) = @_;
> +
> +    return;
> +}

would it maybe make sense to make this a "die implement me" and make the
opt-in via the storage plugin features? it would be more in line with
what we do in other parts and less subtle..

> +
>  sub config_aware_base_mkdir {
>      my ($class, $scfg, $path) = @_;
>  
> -- 
> 2.39.2
> 
> 
> 
> _______________________________________________
> pve-devel mailing list
> pve-devel@lists.proxmox.com
> https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel
> 
> 
> 


_______________________________________________
pve-devel mailing list
pve-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel


^ permalink raw reply	[flat|nested] 44+ messages in thread

* Re: [pve-devel] [RFC container v2 22/25] backup: implement backup for external providers
  2024-08-13 13:28 ` [pve-devel] [RFC container v2 22/25] backup: implement backup " Fiona Ebner
@ 2024-09-12 12:43   ` Fabian Grünbichler
  2024-09-12 13:38     ` Fiona Ebner
  0 siblings, 1 reply; 44+ messages in thread
From: Fabian Grünbichler @ 2024-09-12 12:43 UTC (permalink / raw)
  To: Proxmox VE development discussion

On August 13, 2024 3:28 pm, Fiona Ebner wrote:
> The filesystem structure is made available as a directory in a
> consistent manner (with details depending on the vzdump backup mode)
> just like for regular backup via tar.
> 
> The backup provider needs to back up the guest and firewall
> configuration and then the filesystem structure, honoring the ID maps
> (for unprivileged containers) as well as file exclusions and the
> bandwidth limit.
> 
> Signed-off-by: Fiona Ebner <f.ebner@proxmox.com>
> ---
> 
> Changes in v2:
> * Adapt to API changes.
> 
>  src/PVE/VZDump/LXC.pm | 22 +++++++++++++++++++++-
>  1 file changed, 21 insertions(+), 1 deletion(-)
> 
> diff --git a/src/PVE/VZDump/LXC.pm b/src/PVE/VZDump/LXC.pm
> index 67d13db..0fc2a94 100644
> --- a/src/PVE/VZDump/LXC.pm
> +++ b/src/PVE/VZDump/LXC.pm
> @@ -373,7 +373,27 @@ sub archive {
>      my $userns_cmd = $task->{userns_cmd};
>      my $findexcl = $self->{vzdump}->{findexcl};
>  
> -    if ($self->{vzdump}->{opts}->{pbs}) {
> +    if (my $backup_provider = $self->{vzdump}->{'backup-provider'}) {
> +	$self->loginfo("starting external backup via " . $backup_provider->provider_name());
> +
> +	my ($mechanism) = $backup_provider->backup_get_mechanism($vmid, 'lxc');
> +	die "mechanism '$mechanism' requested by backup provider is not supported for containers\n"
> +	    if $mechanism ne 'directory';
> +
> +	my $config_file = "$tmpdir/etc/vzdump/pct.conf";
> +	my $firewall_file = "$tmpdir/etc/vzdump/pct.fw";
> +
> +
> +	my $conf = PVE::LXC::Config->load_config($vmid);
> +	my ($id_map, undef, undef) = PVE::LXC::parse_id_maps($conf);
> +	my $info = {
> +	    directory => $snapdir,
> +	    sources => [@sources],
> +	};
> +	$info->{'firewall-config'} = $firewall_file if -e $firewall_file;
> +	$info->{'bandwidth-limit'} = $opts->{bwlimit} * 1024 if $opts->{bwlimit};
> +	$backup_provider->backup_container($vmid, $config_file, $id_map, $findexcl, $info);

it might be easier to hide the idmapping from the backup provider? e.g.,
hand it a idmapped bindmount or something like that?

> +    } elsif ($self->{vzdump}->{opts}->{pbs}) {
>  
>  	my $param = [];
>  	push @$param, "pct.conf:$tmpdir/etc/vzdump/pct.conf";
> -- 
> 2.39.2
> 
> 
> 
> _______________________________________________
> pve-devel mailing list
> pve-devel@lists.proxmox.com
> https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel
> 
> 
> 


_______________________________________________
pve-devel mailing list
pve-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel


^ permalink raw reply	[flat|nested] 44+ messages in thread

* Re: [pve-devel] [RFC container v2 23/25] backup: implement restore for external providers
  2024-08-13 13:28 ` [pve-devel] [RFC container v2 23/25] backup: implement restore " Fiona Ebner
@ 2024-09-12 12:43   ` Fabian Grünbichler
  2024-09-12 13:56     ` Fiona Ebner
  0 siblings, 1 reply; 44+ messages in thread
From: Fabian Grünbichler @ 2024-09-12 12:43 UTC (permalink / raw)
  To: Proxmox VE development discussion

On August 13, 2024 3:28 pm, Fiona Ebner wrote:
> First, the provider is asked about what restore mechanism to use.
> Currently, 'directory' and 'tar' are possible, for restoring either
> from a directory containing the full filesystem structure (for which
> rsync is used) or a potentially compressed tar file containing the
> same.
> 
> The new functions are copied and adapted from the existing ones for
> PBS or tar and it might be worth to factor out the common parts.
> 
> Signed-off-by: Fiona Ebner <f.ebner@proxmox.com>
> ---
> 
> Changes in v2:
> * Adapt to API changes.
> 
>  src/PVE/LXC/Create.pm | 141 ++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 141 insertions(+)
> 
> diff --git a/src/PVE/LXC/Create.pm b/src/PVE/LXC/Create.pm
> index 117103c..9d1c337 100644
> --- a/src/PVE/LXC/Create.pm
> +++ b/src/PVE/LXC/Create.pm
> @@ -25,6 +25,24 @@ sub restore_archive {
>  	if ($scfg->{type} eq 'pbs') {
>  	    return restore_proxmox_backup_archive($storage_cfg, $archive, $rootdir, $conf, $no_unpack_error, $bwlimit);
>  	}
> +	my $log_function = sub {
> +	    my ($log_level, $message) = @_;
> +	    my $prefix = $log_level eq 'err' ? 'ERROR' : uc($log_level);
> +	    print "$prefix: $message\n";
> +	};
> +	my $backup_provider =
> +	    PVE::Storage::new_backup_provider($storage_cfg, $storeid, $log_function);
> +	if ($backup_provider) {
> +	    return restore_external_archive(
> +		$backup_provider,
> +		$storeid,
> +		$volname,
> +		$rootdir,
> +		$conf,
> +		$no_unpack_error,
> +		$bwlimit,
> +	    );
> +	}
>      }
>  
>      $archive = PVE::Storage::abs_filesystem_path($storage_cfg, $archive) if $archive ne '-';
> @@ -118,6 +136,55 @@ sub restore_tar_archive {
>      die $err if $err && !$no_unpack_error;
>  }
>  
> +sub restore_external_archive {
> +    my ($backup_provider, $storeid, $volname, $rootdir, $conf, $no_unpack_error, $bwlimit) = @_;
> +
> +    my ($mechanism, $vmtype) = $backup_provider->restore_get_mechanism($volname, $storeid);
> +    die "cannot restore non-LXC guest of type '$vmtype'\n" if $vmtype ne 'lxc';
> +
> +    my $info = $backup_provider->restore_container_init($volname, $storeid, {});
> +    eval {
> +	if ($mechanism eq 'tar') {
> +	    my $tar_path = $info->{'tar-path'}
> +		or die "did not get path to tar file from backup provider\n";
> +	    die "not a regular file '$tar_path'" if !-f $tar_path;
> +	    restore_tar_archive($tar_path, $rootdir, $conf, $no_unpack_error, $bwlimit);

shouldn't this be `lxc-userns-exec`-ed?

> +	} elsif ($mechanism eq 'directory') {
> +	    my $directory = $info->{'archive-directory'}
> +		or die "did not get path to archive directory from backup provider\n";
> +	    die "not a directory '$directory'" if !-d $directory;
> +
> +	    my $rsync = ['rsync', '--stats', '-h', '-X', '-A', '--numeric-ids', '-aH', '--delete',
> +		'--no-whole-file', '--sparse', '--one-file-system', '--relative'];
> +	    push $rsync->@*, '--bwlimit', $bwlimit if $bwlimit;
> +	    push $rsync->@*, "${directory}/./", $rootdir;

and this as well?

also, for both tar and rsync we probably need to think about how to
prevent bogus input here (which might be user-creatable if they have
write access to the backup storage) from violating our assumptions..

> +
> +	    my $transferred = '';
> +	    my $outfunc = sub {
> +		return if $_[0] !~ /^Total transferred file size: (.+)$/;
> +		$transferred = $1;
> +	    };
> +	    my $errfunc = sub { log_warn($_[0]); };
> +
> +	    my $starttime = time();
> +	    PVE::Tools::run_command($rsync, outfunc => $outfunc, errfunc => $errfunc);
> +	    my $delay = time () - $starttime;
> +
> +	    print "sync finished - transferred ${transferred} in ${delay}s\n";


_______________________________________________
pve-devel mailing list
pve-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel


^ permalink raw reply	[flat|nested] 44+ messages in thread

* Re: [pve-devel] [RFC qemu-server v2 21/25] backup: implement restore for external providers
  2024-08-13 13:28 ` [pve-devel] [RFC qemu-server v2 21/25] backup: implement restore for external providers Fiona Ebner
@ 2024-09-12 12:44   ` Fabian Grünbichler
  2024-09-12 13:32     ` Fiona Ebner
  0 siblings, 1 reply; 44+ messages in thread
From: Fabian Grünbichler @ 2024-09-12 12:44 UTC (permalink / raw)
  To: Proxmox VE development discussion

On August 13, 2024 3:28 pm, Fiona Ebner wrote:
> First, the provider is asked about what restore mechanism to use.
> Currently, only 'qemu-img' is possible. Then the configuration files
> are restored, the provider gives information about volumes contained
> in the backup and finally the volumes are restored via
> 'qemu-img convert'.
> 
> The code for the restore_external_archive() function was copied and
> adapted from the restore_proxmox_backup_archive() function. Together
> with restore_vma_archive() it seems sensible to extract the common
> parts and use a dedicated module for restore code.
> 
> The parse_restore_archive() helper was renamed, because it's not just
> parsing.
> 
> Signed-off-by: Fiona Ebner <f.ebner@proxmox.com>
> ---
> 
> Changes in v2:
> * Adapt to API changes.
> 
>  PVE/API2/Qemu.pm  |  29 +++++++++-
>  PVE/QemuServer.pm | 139 ++++++++++++++++++++++++++++++++++++++++++++++
>  2 files changed, 165 insertions(+), 3 deletions(-)
> 

[snip..]

> diff --git a/PVE/QemuServer.pm b/PVE/QemuServer.pm
> index 37f56f69..6cd21b7d 100644
> --- a/PVE/QemuServer.pm
> +++ b/PVE/QemuServer.pm

[snip..]

> +
> +	# allocate volumes
> +	my $map = $restore_allocate_devices->($storecfg, $virtdev_hash, $vmid);
> +
> +	for my $virtdev (sort keys $virtdev_hash->%*) {
> +	    my $d = $virtdev_hash->{$virtdev};
> +	    next if $d->{is_cloudinit}; # no need to restore cloudinit
> +
> +	    my $info =
> +		$backup_provider->restore_vm_volume_init($volname, $storeid, $d->{devname}, {});
> +	    my $source_path = $info->{'qemu-img-path'}
> +		or die "did not get source image path from backup provider\n";
> +	    eval {
> +		qemu_img_convert(
> +		    $source_path, $d->{volid}, $d->{size}, undef, 0, $options->{bwlimit});
> +	    };

this definitely needs to get a call to file_size_info with import
hardening patches applied ;)

> +	    my $err = $@;
> +	    eval {
> +		$backup_provider->restore_vm_volume_cleanup($volname, $storeid, $d->{devname}, {});
> +	    };
> +	    if (my $cleanup_err = $@) {
> +		die $cleanup_err if !$err;
> +		warn $cleanup_err;
> +	    }
> +	    die $err if $err
> +	}
> +


_______________________________________________
pve-devel mailing list
pve-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel


^ permalink raw reply	[flat|nested] 44+ messages in thread

* Re: [pve-devel] [RFC storage v2 10/25] plugin: introduce new_backup_provider() method
  2024-09-12 12:43   ` Fabian Grünbichler
@ 2024-09-12 13:21     ` Fiona Ebner
  2024-09-13  6:13       ` Fabian Grünbichler
  0 siblings, 1 reply; 44+ messages in thread
From: Fiona Ebner @ 2024-09-12 13:21 UTC (permalink / raw)
  To: Proxmox VE development discussion, Fabian Grünbichler

Am 12.09.24 um 14:43 schrieb Fabian Grünbichler:
> On August 13, 2024 3:28 pm, Fiona Ebner wrote:
>>
>> Container mechanism 'directory':
>>
>> The backup provider gives the path to a directory with the full
>> filesystem structure of the container.
>>
>> Container mechanism 'directory':
>>
>> The backup provider gives the path to a (potentially compressed) tar
>> archive with the full filesystem structure of the container.
> 
> same as in the cover letter ;) base on the code here I assume the second
> one should be tar. I wonder whether just tar wouldn't be enough (easier
> to not get ACLs/xattrs/ownership/.. right)?
> 

Yes, should be tar, will fix!

I'd guess it is more convenient for many providers to expose (a FUSE
mount of) a directory. Using tar can mean more work. E.g. with Borg,
mounting the archive seems much cheaper than creating the tar. It's also
that the archive looks like:
guest.config
firewall.config
filesystem/
and while borg has "export-tar" where one can specify specific paths,
the tar will still contain the "filesystem/" prefix. Not sure if there
is an easy way to get rid of that.

>> diff --git a/src/PVE/Storage/Plugin.pm b/src/PVE/Storage/Plugin.pm
>> index 6444390..d5b76ae 100644
>> --- a/src/PVE/Storage/Plugin.pm
>> +++ b/src/PVE/Storage/Plugin.pm
>> @@ -1755,6 +1755,21 @@ sub rename_volume {
>>      return "${storeid}:${base}${target_vmid}/${target_volname}";
>>  }
>>  
>> +# Used by storage plugins for external backup providers. See PVE::BackupProvider::Plugin for the API
>> +# the provider needs to implement.
>> +#
>> +# $scfg - the storage configuration
>> +# $storeid - the storage ID
>> +# $log_function($log_level, $message) - this log function can be used to write to the backup task
>> +#   log in Proxmox VE. $log_level is 'info', 'warn' or 'err', $message is the message to be printed.
>> +#
>> +# Returns a blessed reference to the backup provider class.
>> +sub new_backup_provider {
>> +    my ($class, $scfg, $storeid, $log_function) = @_;
>> +
>> +    return;
>> +}
> 
> would it maybe make sense to make this a "die implement me" and make the
> opt-in via the storage plugin features? it would be more in line with
> what we do in other parts and less subtle..
> 

We don't have a method for storage plugin features yet, only
volume_has_feature() and the stand-alone storage_can_replicate(). We
could generalize (and deprecate) the latter though.


_______________________________________________
pve-devel mailing list
pve-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel

^ permalink raw reply	[flat|nested] 44+ messages in thread

* Re: [pve-devel] [RFC qemu-server v2 21/25] backup: implement restore for external providers
  2024-09-12 12:44   ` Fabian Grünbichler
@ 2024-09-12 13:32     ` Fiona Ebner
  0 siblings, 0 replies; 44+ messages in thread
From: Fiona Ebner @ 2024-09-12 13:32 UTC (permalink / raw)
  To: Proxmox VE development discussion, Fabian Grünbichler

Am 12.09.24 um 14:44 schrieb Fabian Grünbichler:
> On August 13, 2024 3:28 pm, Fiona Ebner wrote:
>> +
>> +	# allocate volumes
>> +	my $map = $restore_allocate_devices->($storecfg, $virtdev_hash, $vmid);
>> +
>> +	for my $virtdev (sort keys $virtdev_hash->%*) {
>> +	    my $d = $virtdev_hash->{$virtdev};
>> +	    next if $d->{is_cloudinit}; # no need to restore cloudinit
>> +
>> +	    my $info =
>> +		$backup_provider->restore_vm_volume_init($volname, $storeid, $d->{devname}, {});
>> +	    my $source_path = $info->{'qemu-img-path'}
>> +		or die "did not get source image path from backup provider\n";
>> +	    eval {
>> +		qemu_img_convert(
>> +		    $source_path, $d->{volid}, $d->{size}, undef, 0, $options->{bwlimit});
>> +	    };
> 
> this definitely needs to get a call to file_size_info with import
> hardening patches applied ;)
> 

Sure, added a reminder to myself for now :)


_______________________________________________
pve-devel mailing list
pve-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel

^ permalink raw reply	[flat|nested] 44+ messages in thread

* Re: [pve-devel] [RFC container v2 22/25] backup: implement backup for external providers
  2024-09-12 12:43   ` Fabian Grünbichler
@ 2024-09-12 13:38     ` Fiona Ebner
  2024-09-13  6:19       ` Fabian Grünbichler
  0 siblings, 1 reply; 44+ messages in thread
From: Fiona Ebner @ 2024-09-12 13:38 UTC (permalink / raw)
  To: Proxmox VE development discussion, Fabian Grünbichler

Am 12.09.24 um 14:43 schrieb Fabian Grünbichler:
> On August 13, 2024 3:28 pm, Fiona Ebner wrote:
>> +	$info->{'firewall-config'} = $firewall_file if -e $firewall_file;
>> +	$info->{'bandwidth-limit'} = $opts->{bwlimit} * 1024 if $opts->{bwlimit};
>> +	$backup_provider->backup_container($vmid, $config_file, $id_map, $findexcl, $info);
> 
> it might be easier to hide the idmapping from the backup provider? e.g.,
> hand it a idmapped bindmount or something like that?
>

Yes, that would be nicer. But could that potentially lead to permission
issues? A mid/long term plan is to have the backup provider code run
with lower privileges. I suppose to later implement that, the subroutine
for the provider could run within a matching user namespace too?


_______________________________________________
pve-devel mailing list
pve-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel

^ permalink raw reply	[flat|nested] 44+ messages in thread

* Re: [pve-devel] [RFC container v2 23/25] backup: implement restore for external providers
  2024-09-12 12:43   ` Fabian Grünbichler
@ 2024-09-12 13:56     ` Fiona Ebner
  2024-09-12 14:08       ` Fiona Ebner
  2024-09-13  6:34       ` Fabian Grünbichler
  0 siblings, 2 replies; 44+ messages in thread
From: Fiona Ebner @ 2024-09-12 13:56 UTC (permalink / raw)
  To: Proxmox VE development discussion, Fabian Grünbichler

Am 12.09.24 um 14:43 schrieb Fabian Grünbichler:
> On August 13, 2024 3:28 pm, Fiona Ebner wrote:
>> @@ -118,6 +136,55 @@ sub restore_tar_archive {
>>      die $err if $err && !$no_unpack_error;
>>  }
>>  
>> +sub restore_external_archive {
>> +    my ($backup_provider, $storeid, $volname, $rootdir, $conf, $no_unpack_error, $bwlimit) = @_;
>> +
>> +    my ($mechanism, $vmtype) = $backup_provider->restore_get_mechanism($volname, $storeid);
>> +    die "cannot restore non-LXC guest of type '$vmtype'\n" if $vmtype ne 'lxc';
>> +
>> +    my $info = $backup_provider->restore_container_init($volname, $storeid, {});
>> +    eval {
>> +	if ($mechanism eq 'tar') {
>> +	    my $tar_path = $info->{'tar-path'}
>> +		or die "did not get path to tar file from backup provider\n";
>> +	    die "not a regular file '$tar_path'" if !-f $tar_path;
>> +	    restore_tar_archive($tar_path, $rootdir, $conf, $no_unpack_error, $bwlimit);
> 
> shouldn't this be `lxc-userns-exec`-ed?
> 

The restore_tar_archive() function does that AFAICS.

>> +	} elsif ($mechanism eq 'directory') {
>> +	    my $directory = $info->{'archive-directory'}
>> +		or die "did not get path to archive directory from backup provider\n";
>> +	    die "not a directory '$directory'" if !-d $directory;
>> +
>> +	    my $rsync = ['rsync', '--stats', '-h', '-X', '-A', '--numeric-ids', '-aH', '--delete',
>> +		'--no-whole-file', '--sparse', '--one-file-system', '--relative'];
>> +	    push $rsync->@*, '--bwlimit', $bwlimit if $bwlimit;
>> +	    push $rsync->@*, "${directory}/./", $rootdir;
> 
> and this as well?
> 

Good catch, will fix!

> also, for both tar and rsync we probably need to think about how to
> prevent bogus input here (which might be user-creatable if they have
> write access to the backup storage) from violating our assumptions..
> 
What assumptions do you mean exactly?


_______________________________________________
pve-devel mailing list
pve-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel

^ permalink raw reply	[flat|nested] 44+ messages in thread

* Re: [pve-devel] [RFC container v2 23/25] backup: implement restore for external providers
  2024-09-12 13:56     ` Fiona Ebner
@ 2024-09-12 14:08       ` Fiona Ebner
  2024-09-13  6:35         ` Fabian Grünbichler
  2024-09-13  6:34       ` Fabian Grünbichler
  1 sibling, 1 reply; 44+ messages in thread
From: Fiona Ebner @ 2024-09-12 14:08 UTC (permalink / raw)
  To: Proxmox VE development discussion, Fabian Grünbichler

Am 12.09.24 um 15:56 schrieb Fiona Ebner:
> Am 12.09.24 um 14:43 schrieb Fabian Grünbichler:
>> On August 13, 2024 3:28 pm, Fiona Ebner wrote:
>>> +	} elsif ($mechanism eq 'directory') {
>>> +	    my $directory = $info->{'archive-directory'}
>>> +		or die "did not get path to archive directory from backup provider\n";
>>> +	    die "not a directory '$directory'" if !-d $directory;
>>> +
>>> +	    my $rsync = ['rsync', '--stats', '-h', '-X', '-A', '--numeric-ids', '-aH', '--delete',
>>> +		'--no-whole-file', '--sparse', '--one-file-system', '--relative'];
>>> +	    push $rsync->@*, '--bwlimit', $bwlimit if $bwlimit;
>>> +	    push $rsync->@*, "${directory}/./", $rootdir;
>>
>> and this as well?
>>
> 
> Good catch, will fix!
> 

Hmm, then rsync won't be able to access the source (for my Borg example)
anymore :/

WARN: rsync: [sender] change_dir
"/run/pve-storage-borg-plugin/pve-lxc-111-2024-08-13T09:34:25Z.restore-container/filesystem"
failed: Permission denied (13)

Wit restore_tar_archive we stream the contents via stdin, can't do that
here. But maybe some kind of bind mount to make it accessible?


_______________________________________________
pve-devel mailing list
pve-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel

^ permalink raw reply	[flat|nested] 44+ messages in thread

* Re: [pve-devel] [RFC qemu/storage/qemu-server/container/manager v2 00/25] backup provider API
  2024-09-12 12:43 ` [pve-devel] [RFC qemu/storage/qemu-server/container/manager v2 00/25] backup provider API Fabian Grünbichler
@ 2024-09-12 15:31   ` Thomas Lamprecht
  0 siblings, 0 replies; 44+ messages in thread
From: Thomas Lamprecht @ 2024-09-12 15:31 UTC (permalink / raw)
  To: Proxmox VE development discussion, Fabian Grünbichler

Am 12/09/2024 um 14:43 schrieb Fabian Grünbichler:
> I do wonder whether we want to support the Borg and Example plugins
> though? if not, it might make sense to not ship them (but maybe just
> test them?)..

FWIW, we could move them to a separate repository named something like
"pve-storage-plugin-examples" which could also host one or two examples
for the storage plugins (like SSHFS). We can then still decide to actually
support it as, e.g., opt-in package(s) built from that repo.

> there's a pretty tight coupling between storage plugin and backup
> provider plugin - that might lead to some complaints (e.g., I can
> imaging quite a few backup providers that just require some local file
> system for temp storage, and users wondering why they can't just enable
> that for an existing dir storage). it does make some things easier
> though, so I am not sure we need to change that, just wanted to draw
> attention to it.

FWIW, if we get feedback and can extract a base for a common use case we
should be able to do something like addinmg a new perl module implementing
most for that use case, on which such providers can then base their
implementation on.


_______________________________________________
pve-devel mailing list
pve-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel

^ permalink raw reply	[flat|nested] 44+ messages in thread

* Re: [pve-devel] [RFC storage v2 10/25] plugin: introduce new_backup_provider() method
  2024-09-12 13:21     ` Fiona Ebner
@ 2024-09-13  6:13       ` Fabian Grünbichler
  0 siblings, 0 replies; 44+ messages in thread
From: Fabian Grünbichler @ 2024-09-13  6:13 UTC (permalink / raw)
  To: Fiona Ebner, Proxmox VE development discussion


> Fiona Ebner <f.ebner@proxmox.com> hat am 12.09.2024 15:21 CEST geschrieben:
> 
>  
> Am 12.09.24 um 14:43 schrieb Fabian Grünbichler:
> > On August 13, 2024 3:28 pm, Fiona Ebner wrote:
> >>
> >> Container mechanism 'directory':
> >>
> >> The backup provider gives the path to a directory with the full
> >> filesystem structure of the container.
> >>
> >> Container mechanism 'directory':
> >>
> >> The backup provider gives the path to a (potentially compressed) tar
> >> archive with the full filesystem structure of the container.
> > 
> > same as in the cover letter ;) base on the code here I assume the second
> > one should be tar. I wonder whether just tar wouldn't be enough (easier
> > to not get ACLs/xattrs/ownership/.. right)?
> > 
> 
> Yes, should be tar, will fix!
> 
> I'd guess it is more convenient for many providers to expose (a FUSE
> mount of) a directory. Using tar can mean more work. E.g. with Borg,
> mounting the archive seems much cheaper than creating the tar. It's also
> that the archive looks like:
> guest.config
> firewall.config
> filesystem/
> and while borg has "export-tar" where one can specify specific paths,
> the tar will still contain the "filesystem/" prefix. Not sure if there
> is an easy way to get rid of that.
> 
> >> diff --git a/src/PVE/Storage/Plugin.pm b/src/PVE/Storage/Plugin.pm
> >> index 6444390..d5b76ae 100644
> >> --- a/src/PVE/Storage/Plugin.pm
> >> +++ b/src/PVE/Storage/Plugin.pm
> >> @@ -1755,6 +1755,21 @@ sub rename_volume {
> >>      return "${storeid}:${base}${target_vmid}/${target_volname}";
> >>  }
> >>  
> >> +# Used by storage plugins for external backup providers. See PVE::BackupProvider::Plugin for the API
> >> +# the provider needs to implement.
> >> +#
> >> +# $scfg - the storage configuration
> >> +# $storeid - the storage ID
> >> +# $log_function($log_level, $message) - this log function can be used to write to the backup task
> >> +#   log in Proxmox VE. $log_level is 'info', 'warn' or 'err', $message is the message to be printed.
> >> +#
> >> +# Returns a blessed reference to the backup provider class.
> >> +sub new_backup_provider {
> >> +    my ($class, $scfg, $storeid, $log_function) = @_;
> >> +
> >> +    return;
> >> +}
> > 
> > would it maybe make sense to make this a "die implement me" and make the
> > opt-in via the storage plugin features? it would be more in line with
> > what we do in other parts and less subtle..
> > 
> 
> We don't have a method for storage plugin features yet, only
> volume_has_feature() and the stand-alone storage_can_replicate(). We
> could generalize (and deprecate) the latter though.

ah yeah, I was thinking of volume_has_feature, but that is not a good fit, you are right. could also be handled via plugindata and a new helper though - it seems a bit nicer to differentiate "supports external backups" from "get instance to do external backup/restore"..


_______________________________________________
pve-devel mailing list
pve-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel

^ permalink raw reply	[flat|nested] 44+ messages in thread

* Re: [pve-devel] [RFC container v2 22/25] backup: implement backup for external providers
  2024-09-12 13:38     ` Fiona Ebner
@ 2024-09-13  6:19       ` Fabian Grünbichler
  2024-09-16 11:40         ` Fiona Ebner
  0 siblings, 1 reply; 44+ messages in thread
From: Fabian Grünbichler @ 2024-09-13  6:19 UTC (permalink / raw)
  To: Fiona Ebner, Proxmox VE development discussion

> Fiona Ebner <f.ebner@proxmox.com> hat am 12.09.2024 15:38 CEST geschrieben:
>  
> Am 12.09.24 um 14:43 schrieb Fabian Grünbichler:
> > On August 13, 2024 3:28 pm, Fiona Ebner wrote:
> >> +	$info->{'firewall-config'} = $firewall_file if -e $firewall_file;
> >> +	$info->{'bandwidth-limit'} = $opts->{bwlimit} * 1024 if $opts->{bwlimit};
> >> +	$backup_provider->backup_container($vmid, $config_file, $id_map, $findexcl, $info);
> > 
> > it might be easier to hide the idmapping from the backup provider? e.g.,
> > hand it a idmapped bindmount or something like that?
> >
> 
> Yes, that would be nicer. But could that potentially lead to permission
> issues? A mid/long term plan is to have the backup provider code run
> with lower privileges. I suppose to later implement that, the subroutine
> for the provider could run within a matching user namespace too?

yeah, I think there are a few options here
- run the provider as root-in-user-ns, give it access to the mapped FS (this is how we do regular backups, but requires some glue code/forking)
- run the provider as root-on-host, give it access to a reverse-mapped FS somehow (well, it would be nicer to run the backup code in the userns instead of as root)
- run the provider as root-on-host, give it access to the mapped FS and let it handle the (un)mapping itself (if they are not familiar with namespaces, this might go wrong)

so if we find a generic way to do the first variant, we are both closer to how we do backups, and err on the side of caution w.r.t. context of execution.


_______________________________________________
pve-devel mailing list
pve-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel

^ permalink raw reply	[flat|nested] 44+ messages in thread

* Re: [pve-devel] [RFC container v2 23/25] backup: implement restore for external providers
  2024-09-12 13:56     ` Fiona Ebner
  2024-09-12 14:08       ` Fiona Ebner
@ 2024-09-13  6:34       ` Fabian Grünbichler
  1 sibling, 0 replies; 44+ messages in thread
From: Fabian Grünbichler @ 2024-09-13  6:34 UTC (permalink / raw)
  To: Fiona Ebner, Proxmox VE development discussion


> Fiona Ebner <f.ebner@proxmox.com> hat am 12.09.2024 15:56 CEST geschrieben:
> Am 12.09.24 um 14:43 schrieb Fabian Grünbichler:
> 
> > also, for both tar and rsync we probably need to think about how to
> > prevent bogus input here (which might be user-creatable if they have
> > write access to the backup storage) from violating our assumptions..
> > 
> What assumptions do you mean exactly?

mainly things like symlinks/hardlinks in weird places, wrong looking dir layouts, containing file systems that don't belong (/dev , /proc, ..), stuff like that..

with vzdump backups, we have the reasonable assumption that backup archives are
- well-formed (created by our code)
- put there by an admin with raw storage access (can already do pretty much everything)

with external backups, we don't know who can put what onto the backup storage, there's a lot more that can theoretically be snuck in (also by less-privileged users that have access to the backup storage), so it probably warrants extra caution..


_______________________________________________
pve-devel mailing list
pve-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel

^ permalink raw reply	[flat|nested] 44+ messages in thread

* Re: [pve-devel] [RFC container v2 23/25] backup: implement restore for external providers
  2024-09-12 14:08       ` Fiona Ebner
@ 2024-09-13  6:35         ` Fabian Grünbichler
  2024-09-13 13:05           ` Fiona Ebner
  0 siblings, 1 reply; 44+ messages in thread
From: Fabian Grünbichler @ 2024-09-13  6:35 UTC (permalink / raw)
  To: Proxmox VE development discussion, Fiona Ebner


> Fiona Ebner <f.ebner@proxmox.com> hat am 12.09.2024 16:08 CEST geschrieben:
> 
>  
> Am 12.09.24 um 15:56 schrieb Fiona Ebner:
> > Am 12.09.24 um 14:43 schrieb Fabian Grünbichler:
> >> On August 13, 2024 3:28 pm, Fiona Ebner wrote:
> >>> +	} elsif ($mechanism eq 'directory') {
> >>> +	    my $directory = $info->{'archive-directory'}
> >>> +		or die "did not get path to archive directory from backup provider\n";
> >>> +	    die "not a directory '$directory'" if !-d $directory;
> >>> +
> >>> +	    my $rsync = ['rsync', '--stats', '-h', '-X', '-A', '--numeric-ids', '-aH', '--delete',
> >>> +		'--no-whole-file', '--sparse', '--one-file-system', '--relative'];
> >>> +	    push $rsync->@*, '--bwlimit', $bwlimit if $bwlimit;
> >>> +	    push $rsync->@*, "${directory}/./", $rootdir;
> >>
> >> and this as well?
> >>
> > 
> > Good catch, will fix!
> > 
> 
> Hmm, then rsync won't be able to access the source (for my Borg example)
> anymore :/
> 
> WARN: rsync: [sender] change_dir
> "/run/pve-storage-borg-plugin/pve-lxc-111-2024-08-13T09:34:25Z.restore-container/filesystem"
> failed: Permission denied (13)
> 
> Wit restore_tar_archive we stream the contents via stdin, can't do that
> here. But maybe some kind of bind mount to make it accessible?

or rsync-on-host piped to rsync-in-ns ? haven't tried though


_______________________________________________
pve-devel mailing list
pve-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel

^ permalink raw reply	[flat|nested] 44+ messages in thread

* Re: [pve-devel] [RFC container v2 23/25] backup: implement restore for external providers
  2024-09-13  6:35         ` Fabian Grünbichler
@ 2024-09-13 13:05           ` Fiona Ebner
  2024-09-19  9:44             ` Fabian Grünbichler
  0 siblings, 1 reply; 44+ messages in thread
From: Fiona Ebner @ 2024-09-13 13:05 UTC (permalink / raw)
  To: Fabian Grünbichler, Proxmox VE development discussion

Am 13.09.24 um 08:35 schrieb Fabian Grünbichler:
> 
>> Fiona Ebner <f.ebner@proxmox.com> hat am 12.09.2024 16:08 CEST geschrieben:
>>
>>  
>> Am 12.09.24 um 15:56 schrieb Fiona Ebner:
>>> Am 12.09.24 um 14:43 schrieb Fabian Grünbichler:
>>>> On August 13, 2024 3:28 pm, Fiona Ebner wrote:
>>>>> +	} elsif ($mechanism eq 'directory') {
>>>>> +	    my $directory = $info->{'archive-directory'}
>>>>> +		or die "did not get path to archive directory from backup provider\n";
>>>>> +	    die "not a directory '$directory'" if !-d $directory;
>>>>> +
>>>>> +	    my $rsync = ['rsync', '--stats', '-h', '-X', '-A', '--numeric-ids', '-aH', '--delete',
>>>>> +		'--no-whole-file', '--sparse', '--one-file-system', '--relative'];
>>>>> +	    push $rsync->@*, '--bwlimit', $bwlimit if $bwlimit;
>>>>> +	    push $rsync->@*, "${directory}/./", $rootdir;
>>>>
>>>> and this as well?
>>>>
>>>
>>> Good catch, will fix!
>>>
>>
>> Hmm, then rsync won't be able to access the source (for my Borg example)
>> anymore :/
>>
>> WARN: rsync: [sender] change_dir
>> "/run/pve-storage-borg-plugin/pve-lxc-111-2024-08-13T09:34:25Z.restore-container/filesystem"
>> failed: Permission denied (13)
>>
>> Wit restore_tar_archive we stream the contents via stdin, can't do that
>> here. But maybe some kind of bind mount to make it accessible?
> 
> or rsync-on-host piped to rsync-in-ns ? haven't tried though

Would that require setting up an rsync daemon process? Or how would you
achieve the split? The man page says that --server/sender should not be
used:

> INTERNAL OPTIONS
>        The options --server and --sender are used internally by rsync, and should never be typed by a user under normal circumstances.  Some awareness of these options may be needed in certain  sce‐
>        narios,  such  as when setting up a login that can only run an rsync command.  For instance, the support directory of the rsync distribution has an example script named rrsync (for restricted
>        rsync) that can be used with a restricted ssh login.


_______________________________________________
pve-devel mailing list
pve-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel

^ permalink raw reply	[flat|nested] 44+ messages in thread

* Re: [pve-devel] [RFC container v2 22/25] backup: implement backup for external providers
  2024-09-13  6:19       ` Fabian Grünbichler
@ 2024-09-16 11:40         ` Fiona Ebner
  0 siblings, 0 replies; 44+ messages in thread
From: Fiona Ebner @ 2024-09-16 11:40 UTC (permalink / raw)
  To: Fabian Grünbichler, Proxmox VE development discussion

Am 13.09.24 um 08:19 schrieb Fabian Grünbichler:
>> Fiona Ebner <f.ebner@proxmox.com> hat am 12.09.2024 15:38 CEST geschrieben:
>>  
>> Am 12.09.24 um 14:43 schrieb Fabian Grünbichler:
>>> On August 13, 2024 3:28 pm, Fiona Ebner wrote:
>>>> +	$info->{'firewall-config'} = $firewall_file if -e $firewall_file;
>>>> +	$info->{'bandwidth-limit'} = $opts->{bwlimit} * 1024 if $opts->{bwlimit};
>>>> +	$backup_provider->backup_container($vmid, $config_file, $id_map, $findexcl, $info);
>>>
>>> it might be easier to hide the idmapping from the backup provider? e.g.,
>>> hand it a idmapped bindmount or something like that?
>>>
>>
>> Yes, that would be nicer. But could that potentially lead to permission
>> issues? A mid/long term plan is to have the backup provider code run
>> with lower privileges. I suppose to later implement that, the subroutine
>> for the provider could run within a matching user namespace too?
> 
> yeah, I think there are a few options here
> - run the provider as root-in-user-ns, give it access to the mapped FS (this is how we do regular backups, but requires some glue code/forking)

Gave this a try. Issue is that the backup provider also needs access to
the backup target/etc. Can network access also be an issue (I guess it
is not for PBS)?

E.g. directory example plugin fails with
> ERROR: Backup of VM 112 failed - unable to open file '/mnt/pve/sparschwein/112/lxc-1726484790/guest.conf.tmp.125275' - Permission denied
and Borg plugin fails with
> ERROR: Backup of VM 112 failed - mkdir /run/pve-storage-borg-plugin: Permission denied at /usr/share/perl5/PVE/BackupProvider/Plugin/Borg.pm line 41
or after switching to /tmp with
> ERROR: Backup of VM 112 failed - file '/etc/pve/priv/storage/borg.pw' exists but open for reading failed - Permission denied

Less coupling with the associated storage plugin or a special kind of
"unprivileged" storage plugin would help. In PBS we do the
storage-plugin-related stuff first with root privileges and only run the
final pbs-client command in user namespace. Maybe we need something like
that here too, a preparatory method run as root that prepares for the
unprivileged backup operation? But that makes life more complicated for
provider implementers (and also us).

> - run the provider as root-on-host, give it access to a reverse-mapped FS somehow (well, it would be nicer to run the backup code in the userns instead of as root)

I'd try and go with this option for now if that is okay.

> - run the provider as root-on-host, give it access to the mapped FS and let it handle the (un)mapping itself (if they are not familiar with namespaces, this might go wrong)
> 
> so if we find a generic way to do the first variant, we are both closer to how we do backups, and err on the side of caution w.r.t. context of execution.


_______________________________________________
pve-devel mailing list
pve-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel

^ permalink raw reply	[flat|nested] 44+ messages in thread

* Re: [pve-devel] [RFC container v2 23/25] backup: implement restore for external providers
  2024-09-13 13:05           ` Fiona Ebner
@ 2024-09-19  9:44             ` Fabian Grünbichler
  0 siblings, 0 replies; 44+ messages in thread
From: Fabian Grünbichler @ 2024-09-19  9:44 UTC (permalink / raw)
  To: Fiona Ebner, Proxmox VE development discussion

On September 13, 2024 3:05 pm, Fiona Ebner wrote:
> Am 13.09.24 um 08:35 schrieb Fabian Grünbichler:
>> 
>>> Fiona Ebner <f.ebner@proxmox.com> hat am 12.09.2024 16:08 CEST geschrieben:
>>>
>>>  
>>> Am 12.09.24 um 15:56 schrieb Fiona Ebner:
>>>> Am 12.09.24 um 14:43 schrieb Fabian Grünbichler:
>>>>> On August 13, 2024 3:28 pm, Fiona Ebner wrote:
>>>>>> +	} elsif ($mechanism eq 'directory') {
>>>>>> +	    my $directory = $info->{'archive-directory'}
>>>>>> +		or die "did not get path to archive directory from backup provider\n";
>>>>>> +	    die "not a directory '$directory'" if !-d $directory;
>>>>>> +
>>>>>> +	    my $rsync = ['rsync', '--stats', '-h', '-X', '-A', '--numeric-ids', '-aH', '--delete',
>>>>>> +		'--no-whole-file', '--sparse', '--one-file-system', '--relative'];
>>>>>> +	    push $rsync->@*, '--bwlimit', $bwlimit if $bwlimit;
>>>>>> +	    push $rsync->@*, "${directory}/./", $rootdir;
>>>>>
>>>>> and this as well?
>>>>>
>>>>
>>>> Good catch, will fix!
>>>>
>>>
>>> Hmm, then rsync won't be able to access the source (for my Borg example)
>>> anymore :/
>>>
>>> WARN: rsync: [sender] change_dir
>>> "/run/pve-storage-borg-plugin/pve-lxc-111-2024-08-13T09:34:25Z.restore-container/filesystem"
>>> failed: Permission denied (13)
>>>
>>> Wit restore_tar_archive we stream the contents via stdin, can't do that
>>> here. But maybe some kind of bind mount to make it accessible?
>> 
>> or rsync-on-host piped to rsync-in-ns ? haven't tried though
> 
> Would that require setting up an rsync daemon process? Or how would you
> achieve the split? The man page says that --server/sender should not be
> used:
> 
>> INTERNAL OPTIONS
>>        The options --server and --sender are used internally by rsync, and should never be typed by a user under normal circumstances.  Some awareness of these options may be needed in certain  sce‐
>>        narios,  such  as when setting up a login that can only run an rsync command.  For instance, the support directory of the rsync distribution has an example script named rrsync (for restricted
>>        rsync) that can be used with a restricted ssh login.
> 

yeah, it would probably require explicitly running rsync in daemon
mode..


_______________________________________________
pve-devel mailing list
pve-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel

^ permalink raw reply	[flat|nested] 44+ messages in thread

end of thread, other threads:[~2024-09-19  9:45 UTC | newest]

Thread overview: 44+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-08-13 13:28 [pve-devel] [RFC qemu/storage/qemu-server/container/manager v2 00/25] backup provider API Fiona Ebner
2024-08-13 13:28 ` [pve-devel] [PATCH qemu v2 01/25] block/reqlist: allow adding overlapping requests Fiona Ebner
2024-08-13 13:28 ` [pve-devel] [PATCH qemu v2 02/25] PVE backup: fixup error handling for fleecing Fiona Ebner
2024-08-13 13:28 ` [pve-devel] [PATCH qemu v2 03/25] PVE backup: factor out setting up snapshot access " Fiona Ebner
2024-08-13 13:28 ` [pve-devel] [PATCH qemu v2 04/25] PVE backup: save device name in device info structure Fiona Ebner
2024-08-13 13:28 ` [pve-devel] [PATCH qemu v2 05/25] PVE backup: include device name in error when setting up snapshot access fails Fiona Ebner
2024-08-13 13:28 ` [pve-devel] [RFC qemu v2 06/25] PVE backup: add target ID in backup state Fiona Ebner
2024-08-13 13:28 ` [pve-devel] [RFC qemu v2 07/25] PVE backup: get device info: allow caller to specify filter for which devices use fleecing Fiona Ebner
2024-08-13 13:28 ` [pve-devel] [RFC qemu v2 08/25] PVE backup: implement backup access setup and teardown API for external providers Fiona Ebner
2024-08-13 13:28 ` [pve-devel] [RFC qemu v2 09/25] PVE backup: implement bitmap support for external backup access Fiona Ebner
2024-08-13 13:28 ` [pve-devel] [RFC storage v2 10/25] plugin: introduce new_backup_provider() method Fiona Ebner
2024-09-12 12:43   ` Fabian Grünbichler
2024-09-12 13:21     ` Fiona Ebner
2024-09-13  6:13       ` Fabian Grünbichler
2024-08-13 13:28 ` [pve-devel] [RFC storage v2 11/25] extract backup config: delegate to backup provider if there is one Fiona Ebner
2024-08-13 13:28 ` [pve-devel] [POC storage v2 12/25] add backup provider example Fiona Ebner
2024-08-13 13:28 ` [pve-devel] [POC storage v2 13/25] Borg plugin Fiona Ebner
2024-08-13 13:28 ` [pve-devel] [PATCH qemu-server v2 14/25] move nbd_stop helper to QMPHelpers module Fiona Ebner
2024-08-13 13:28 ` [pve-devel] [PATCH qemu-server v2 15/25] backup: move cleanup of fleecing images to cleanup method Fiona Ebner
2024-08-13 13:28 ` [pve-devel] [PATCH qemu-server v2 16/25] backup: cleanup: check if VM is running before issuing QMP commands Fiona Ebner
2024-08-13 13:28 ` [pve-devel] [PATCH qemu-server v2 17/25] backup: keep track of block-node size instead of volume size Fiona Ebner
2024-08-13 13:28 ` [pve-devel] [RFC qemu-server v2 18/25] backup: allow adding fleecing images also for EFI and TPM Fiona Ebner
2024-08-13 13:28 ` [pve-devel] [RFC qemu-server v2 19/25] backup: implement backup for external providers Fiona Ebner
2024-08-13 13:28 ` [pve-devel] [PATCH qemu-server v2 20/25] restore: die early when there is no size for a device Fiona Ebner
2024-08-13 13:28 ` [pve-devel] [RFC qemu-server v2 21/25] backup: implement restore for external providers Fiona Ebner
2024-09-12 12:44   ` Fabian Grünbichler
2024-09-12 13:32     ` Fiona Ebner
2024-08-13 13:28 ` [pve-devel] [RFC container v2 22/25] backup: implement backup " Fiona Ebner
2024-09-12 12:43   ` Fabian Grünbichler
2024-09-12 13:38     ` Fiona Ebner
2024-09-13  6:19       ` Fabian Grünbichler
2024-09-16 11:40         ` Fiona Ebner
2024-08-13 13:28 ` [pve-devel] [RFC container v2 23/25] backup: implement restore " Fiona Ebner
2024-09-12 12:43   ` Fabian Grünbichler
2024-09-12 13:56     ` Fiona Ebner
2024-09-12 14:08       ` Fiona Ebner
2024-09-13  6:35         ` Fabian Grünbichler
2024-09-13 13:05           ` Fiona Ebner
2024-09-19  9:44             ` Fabian Grünbichler
2024-09-13  6:34       ` Fabian Grünbichler
2024-08-13 13:28 ` [pve-devel] [PATCH manager v2 24/25] ui: backup: also check for backup subtype to classify archive Fiona Ebner
2024-08-13 13:28 ` [pve-devel] [RFC manager v2 25/25] backup: implement backup for external providers Fiona Ebner
2024-09-12 12:43 ` [pve-devel] [RFC qemu/storage/qemu-server/container/manager v2 00/25] backup provider API Fabian Grünbichler
2024-09-12 15:31   ` Thomas Lamprecht

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal