Skip to content

Commit

Permalink
improved error handling, show vm name if set
Browse files Browse the repository at this point in the history
  • Loading branch information
abbbi committed Jan 30, 2024
1 parent 35a54b4 commit 8c62727
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 19 deletions.
29 changes: 29 additions & 0 deletions libqmpbackup/qmpcommon.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,35 @@ def __init__(self, qmp):
self.qmp = qmp
self.log = logging.getLogger(__name__)

async def show_vm_state(self):
"""Show and check if virtual machine is in required
state"""
status = await self.qmp.execute("query-status")
if status["running"] is False and not status["status"] in (
"prelaunch",
"paused",
):
raise RuntimeError(f"VM not ready for backup, state: [{status}]")
self.log.info("VM is in state: [%s]", status["status"])

async def show_name(self):
"""Show qemu version"""
name = await self.qmp.execute("query-name")
if name:
self.log.info("VM Name: [%s]", name["name"])

def show_version(self):
"""Show name of VM; if setn"""
hv_version = self.qmp._greeting._raw["QMP"] # pylint: disable=W0212
qemu = hv_version["version"]["qemu"]
self.log.info(
"Qemu version: [%s.%s.%s] [%s]",
qemu["major"],
qemu["micro"],
qemu["minor"],
hv_version["version"]["package"],
)

@staticmethod
def transaction_action(action, **kwargs):
"""Return transaction action object"""
Expand Down
2 changes: 1 addition & 1 deletion libqmpbackup/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@
This work is licensed under the terms of the GNU GPL, version 3. See
the LICENSE file in the top-level directory.
"""
VERSION = "0.28"
VERSION = "0.29"
31 changes: 14 additions & 17 deletions qmpbackup
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import argparse
from datetime import datetime
from dataclasses import asdict
from qemu.qmp import protocol, QMPClient
from qemu.qmp.error import QMPError

from libqmpbackup.qmpcommon import QmpCommon
from libqmpbackup import lib
Expand Down Expand Up @@ -200,24 +201,17 @@ async def main():
log.fatal("Can't connect QMP socket [%s]: %s", argv.socket, errmsg)
sys.exit(1)

status = await qmp.execute("query-status")
if status["running"] is False and not status["status"] in ("prelaunch", "paused"):
log.fatal("VM not ready for backup, state: [%s]", status)
sys.exit(1)
qemu_client = QmpCommon(qmp)

log.info("VM is in state: [%s]", status["status"])
try:
await qemu_client.show_vm_state()
except RuntimeError as errmsg:
log.fatal(errmsg)
sys.exit(1)

qemu_client = QmpCommon(qmp)

hv_version = qmp._greeting._raw["QMP"]
qemu = hv_version["version"]["qemu"]
log.info(
"Qemu version: [%s.%s.%s] [%s]",
qemu["major"],
qemu["micro"],
qemu["minor"],
hv_version["version"]["package"],
)
qemu_client.show_version()
await qemu_client.show_name()

excluded_disks = None
included_disks = None
Expand Down Expand Up @@ -360,11 +354,14 @@ async def main():
try:
await qemu_client.prepare_target_devices(blockdev, target_files)
await qemu_client.backup(argv, blockdev, qga)
except Exception as errmsg:
except QMPError as errmsg:
log.fatal("Error executing backup: %s", errmsg)
sys.exit(1)
finally:
await qemu_client.remove_target_devices(blockdev)
try:
await qemu_client.remove_target_devices(blockdev)
except QMPError as errmsg:
log.warning("Unable to cleanup: %s", errmsg)
if qga is not False:
fs.thaw(qga)

Expand Down
2 changes: 1 addition & 1 deletion t/runtest
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ fi
echo "Starting qemu process"
KVMOPT=""
[ -e /dev/kvm ] && KVMOPT="--enable-kvm" && echo "with kvm"
qemu-system-x86_64 $KVMOPT -smp "$(nproc)" -daemonize -display none -m 1024 \
qemu-system-x86_64 -name "testvm" $KVMOPT -smp "$(nproc)" -daemonize -display none -m 1024 \
-hda /tmp/disk1.qcow2 \
-hdb /tmp/disk2.qcow2 \
-hdc /tmp/disk3.raw \
Expand Down

0 comments on commit 8c62727

Please sign in to comment.