1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
|
#!/usr/bin/python3
"""
Copyright (C) 2023 Michael Ablassmeier <abi@grinser.de>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
import logging
from argparse import Namespace
from typing import Union
from libvirtnbdbackup import nbdcli
from libvirtnbdbackup import virt
from libvirtnbdbackup.virt.client import DomainDisk
from libvirtnbdbackup.qemu import util as qemu
from libvirtnbdbackup.objects import processInfo
from libvirtnbdbackup.nbdcli.exceptions import NbdClientException
from libvirtnbdbackup.exceptions import DiskBackupFailed
def setup(args: Namespace, disk: DomainDisk, remoteHost: str, port: int) -> processInfo:
"""Start background qemu-nbd process used during backup
if domain is offline, in case of remote backup, initiate
ssh session and start process on remote system."""
bitMap: str = ""
if args.level in ("inc", "diff"):
bitMap = args.cpt.name
socket = f"{args.socketfile}.{disk.target}"
if remoteHost != "":
logging.info(
"Offline backup, starting remote NBD server, address: [%s:%s]",
args.nbd_ip or remoteHost,
port,
)
nbdProc = qemu.util(disk.target).startRemoteBackupNbdServer(
args, disk, bitMap, port
)
logging.info("Remote NBD server started, PID: [%s].", nbdProc.pid)
return nbdProc
logging.info("Offline backup, starting local NBD server, socket: [%s]", socket)
nbdProc = qemu.util(disk.target).startBackupNbdServer(
disk.format, disk.path, socket, bitMap
)
logging.info("Local NBD Service started, PID: [%s]", nbdProc.pid)
return nbdProc
def connect( # pylint: disable=too-many-arguments
args: Namespace,
disk: DomainDisk,
metaContext: str,
remoteIP: str,
port: int,
virtClient: virt.client,
):
"""Connect to started nbd endpoint"""
socket = args.socketfile
if args.offline is True:
socket = f"{args.socketfile}.{disk.target}"
cType: Union[nbdcli.TCP, nbdcli.Unix]
if virtClient.remoteHost != "":
cType = nbdcli.TCP(disk.target, metaContext, remoteIP, args.tls, port)
else:
cType = nbdcli.Unix(disk.target, metaContext, socket)
nbdClient = nbdcli.client(cType, args.no_sparse_detection)
try:
return nbdClient.connect()
except NbdClientException as e:
raise DiskBackupFailed(
f"NBD endpoint: [{cType}]: connection failed: [{e}]"
) from e
|