[27-June-2018: Refer to: https://kashyapc.fedorapeople.org/virt/qemu/NBD-export-of-a-PIT-snapshot-of-a-disk-with-QEMU-2.8.txt] Use-case: Provide an NBD export that serves you a (read-only?) point-in-time (PIT) snapshot of a disk. [Also sometimes referred to as "Image Fleecing"] Procedure: ----------------------------------------------------------------------- (1) Create the target image ('drive1', with 'drive0' as its backing file) using 'blockdev-add' $ qemu-img create -f qcow2 target.qcow2 1G $ ./qmp-shell -v -p /export/qemu-backup-tests/qmp-sock (QEMU) blockdev-add options={"id":"drive-ide1-0-0","driver":"qcow2","file":{"driver":"file","filename":"/export/target.qcow2"},"backing":"drive-ide0-0-0"} (2) Start the built-in NBD server over UNIX socket 'nbd-server-start' (QEMU) nbd-server-start addr={"type":"unix","data":{"path":"./nbd-sock"}} (3) Take the backup of device 'drive0' into 'drive1' using 'blockdev-backup' (QEMU) blockdev-backup device=drive-ide0-0-0 target=drive-ide1-0-0 sync=none (4) Then, export 'drive1' over NBD with 'nbd-server-add' (QEMU) nbd-server-add device=drive-ide1-0-0 (5) Discard the NBD export at anytime you wish (with 'nbd-server-stop'): (QEMU) nbd-server-stop ----------------------------------------------------------------------- * * * === Same as above, but with QMP JSON output === (1) Add block device with a backing file ----------------- $ qemu-img create -f qcow2 target.qcow2 1G ----------------- (QEMU) blockdev-add options={"id":"drive-ide1-0-0","driver":"qcow2","file":{"driver":"file","filename":"/export/target.qcow2"},"backing":"drive-ide0-0-0"} { "execute": "blockdev-add", "arguments": { "options": { "backing": "drive-ide0-0-0", "driver": "qcow2", "id": "drive-ide1-0-0", "file": { "driver": "file", "filename": "/export/target.qcow2" } } } } { "return": {} } (QEMU) ----------------- (qemu) info block drive-ide0-0-0 (#block123): ./cirros-0.3.3.qcow2 (qcow2) Cache mode: writeback drive-ide1-0-0 (#block360): /export/target.qcow2 (qcow2) Removable device: not locked, tray closed Cache mode: writeback Backing file: ./cirros-0.3.3.qcow2 (chain depth: 1) (qemu) ----------------- (2) NBD server start (QEMU) nbd-server-start addr={"type":"unix","data":{"path":"./nbd-sock"}} { "execute": "nbd-server-start", "arguments": { "addr": { "data": { "path": "./nbd-sock" }, "type": "unix" } } } { "return": {} } (QEMU) (3) Take the backup of the backing file to the target drive: (QEMU) blockdev-backup device=drive-ide0-0-0 target=drive-ide1-0-0 sync=none { "execute": "blockdev-backup", "arguments": { "device": "drive-ide0-0-0", "target": "drive-ide1-0-0", "sync": "none" } } { "error": { "class": "GenericError", "desc": "Node 'drive-ide0-0-0' is busy: node is used as backing hd of '#block360'" } } (QEMU) [PATCH v24 01/12] unblock backup operations in backing file -- https://lists.gnu.org/archive/html/qemu-devel/2016-07/msg06096.html ----------------------------------------------------------------------- (QEMU) blockdev-backup device=drive-ide0-0-0 target=drive-ide1-0-0 sync=none { "execute": "blockdev-backup", "arguments": { "device": "drive-ide0-0-0", "target": "drive-ide1-0-0", "sync": "none" } } { "return": {} } ----------------------------------------------------------------------- (QEMU) query-block-jobs { "execute": "query-block-jobs", "arguments": {} } { "return": [ { "busy": false, "type": "backup", "len": 41126400, "paused": false, "ready": false, "io-status": "ok", "offset": 0, "device": "drive-ide0-0-0", "speed": 0 } ] } (QEMU) ----------------------------------------------------------------------- (QEMU) nbd-server-start addr={"type":"unix","data":{"path":"./nbd-sock"}} { "execute": "nbd-server-start", "arguments": { "addr": { "data": { "path": "./nbd-sock" }, "type": "unix" } } } { "return": {} } ----------------------------------------------------------------------- (QEMU) nbd-server-add device=drive-ide1-0-0 { "execute": "nbd-server-add", "arguments": { "device": "drive-ide1-0-0" } } { "return": {} } (QEMU) -----------------------------------------------------------------------