Re: [Qemu-devel] [PATCH 2/2] backup: QEMU Backup Tool

2017-08-16 Thread Stefan Hajnoczi
On Fri, Aug 11, 2017 at 06:34:00PM +0530, Ishani Chugh wrote:
> qemu-backup will be a command-line tool for performing full and
> incremental disk backups on running VMs. It is intended as a
> reference implementation for management stack and backup developers
> to see QEMU's backup features in action. The tool writes details of
> guest in a configuration file and the data is retrieved from the file
> while creating a backup. The location of config file can be set as an
> environment variable QEMU_BACKUP_CONFIG. The usage is as follows:
> 
> Add a guest
> python qemu-backup.py guest add --guest  --qmp 
> 
> Add a drive for backup in a specified guest
> python qemu-backup.py drive add --guest  --id  
> [--target ]
> 
> Create backup of the added drives:
> python qemu-backup.py backup --guest 
> 
> List all guest configs in configuration file:
> python qemu-backup.py guest list
> 
> Restore operation
> python qemu-backup.py restore --guest 
> 
> Remove a guest
> python qemu-backup.py guest remove --guest 
> 
> Signed-off-by: Ishani Chugh 
> ---
>  contrib/backup/qemu-backup.py | 309 
> ++
>  1 file changed, 309 insertions(+)
>  create mode 100644 contrib/backup/qemu-backup.py

Please see my previous review comments.  They have not been addressed
yet:

https://lists.gnu.org/archive/html/qemu-devel/2017-08/msg02021.html


signature.asc
Description: PGP signature


[Qemu-devel] [PATCH 2/2] backup: QEMU Backup Tool

2017-08-11 Thread Ishani Chugh
qemu-backup will be a command-line tool for performing full and
incremental disk backups on running VMs. It is intended as a
reference implementation for management stack and backup developers
to see QEMU's backup features in action. The tool writes details of
guest in a configuration file and the data is retrieved from the file
while creating a backup. The location of config file can be set as an
environment variable QEMU_BACKUP_CONFIG. The usage is as follows:

Add a guest
python qemu-backup.py guest add --guest  --qmp 

Add a drive for backup in a specified guest
python qemu-backup.py drive add --guest  --id  [--target 
]

Create backup of the added drives:
python qemu-backup.py backup --guest 

List all guest configs in configuration file:
python qemu-backup.py guest list

Restore operation
python qemu-backup.py restore --guest 

Remove a guest
python qemu-backup.py guest remove --guest 

Signed-off-by: Ishani Chugh 
---
 contrib/backup/qemu-backup.py | 309 ++
 1 file changed, 309 insertions(+)
 create mode 100644 contrib/backup/qemu-backup.py

diff --git a/contrib/backup/qemu-backup.py b/contrib/backup/qemu-backup.py
new file mode 100644
index 000..9bbbdb7
--- /dev/null
+++ b/contrib/backup/qemu-backup.py
@@ -0,0 +1,309 @@
+#!/usr/bin/python
+# -*- coding: utf-8 -*-
+#
+# Copyright (C) 2013 Red Hat, Inc.
+#
+# 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 2 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 .
+#
+
+"""
+This file is an implementation of backup tool
+"""
+from __future__ import print_function
+from argparse import ArgumentParser
+import os
+import errno
+from socket import error as socket_error
+try:
+import configparser
+except ImportError:
+import ConfigParser as configparser
+import sys
+sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..',
+ 'scripts', 'qmp'))
+from qmp import QEMUMonitorProtocol
+
+
+class BackupTool(object):
+"""BackupTool Class"""
+def __init__(self,
+ config_file=os.path.expanduser('~')+'/.qemu/backup/config'):
+if "QEMU_BACKUP_CONFIG" in os.environ:
+self.config_file = os.environ["QEMU_BACKUP_CONFIG"]
+else:
+self.config_file = config_file
+try:
+if not os.path.isdir(os.path.expanduser('~')+'/.qemu/backup'):
+os.makedirs(os.path.expanduser('~')+'/.qemu/backup')
+except:
+print("Cannot find the config file", file=sys.stderr)
+exit(1)
+self.config = configparser.ConfigParser()
+self.config.read(self.config_file)
+
+def write_config(self):
+"""
+Writes configuration to ini file.
+"""
+config_file = open(self.config_file+".tmp", 'w')
+self.config.write(config_file)
+config_file.flush()
+os.fsync(config_file.fileno())
+config_file.close()
+os.rename(self.config_file+".tmp", self.config_file)
+
+def get_socket_address(self, socket_address):
+"""
+Return Socket address in form of string or tuple
+"""
+if socket_address.startswith('tcp'):
+return (socket_address.split(':')[1],
+int(socket_address.split(':')[2]))
+return socket_address.split(':',2)[1]
+
+def _full_backup(self, guest_name):
+"""
+Performs full backup of guest
+"""
+if guest_name not in self.config.sections():
+print ("Cannot find specified guest", file=sys.stderr)
+exit(1)
+
+self.verify_guest_running(guest_name)
+connection = QEMUMonitorProtocol(
+ self.get_socket_address(
+ self.config[guest_name]['qmp']))
+connection.connect()
+cmd = {"execute": "transaction", "arguments": {"actions": []}}
+for key in self.config[guest_name]:
+if key.startswith("drive_"):
+drive = key[len('drive_'):]
+target = self.config[guest_name][key]
+sub_cmd = {"type": "drive-backup", "data": {"device": drive,
+"target": target,
+"sync": "full"}}
+

[Qemu-devel] [PATCH 2/2] backup: QEMU Backup Tool

2017-08-10 Thread Ishani Chugh
qemu-backup will be a command-line tool for performing full and
incremental disk backups on running VMs. It is intended as a
reference implementation for management stack and backup developers
to see QEMU's backup features in action. The tool writes details of
guest in a configuration file and the data is retrieved from the file
while creating a backup. The location of config file can be set as an
environment variable QEMU_BACKUP_CONFIG. The usage is as follows:

Add a guest
python qemu-backup.py guest add --guest  --qmp 

Add a drive for backup in a specified guest
python qemu-backup.py drive add --guest  --id  [--target 
]

Create backup of the added drives:
python qemu-backup.py backup --guest 

List all guest configs in configuration file:
python qemu-backup.py guest list

Restore operation
python qemu-backup.py restore --guest 

Remove a guest
python qemu-backup.py guest remove --guest 

Signed-off-by: Ishani Chugh 
---
 contrib/backup/qemu-backup.py | 309 ++
 1 file changed, 309 insertions(+)
 create mode 100644 contrib/backup/qemu-backup.py

diff --git a/contrib/backup/qemu-backup.py b/contrib/backup/qemu-backup.py
new file mode 100644
index 000..9bbbdb7
--- /dev/null
+++ b/contrib/backup/qemu-backup.py
@@ -0,0 +1,309 @@
+#!/usr/bin/python
+# -*- coding: utf-8 -*-
+#
+# Copyright (C) 2013 Red Hat, Inc.
+#
+# 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 2 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 .
+#
+
+"""
+This file is an implementation of backup tool
+"""
+from __future__ import print_function
+from argparse import ArgumentParser
+import os
+import errno
+from socket import error as socket_error
+try:
+import configparser
+except ImportError:
+import ConfigParser as configparser
+import sys
+sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..',
+ 'scripts', 'qmp'))
+from qmp import QEMUMonitorProtocol
+
+
+class BackupTool(object):
+"""BackupTool Class"""
+def __init__(self,
+ config_file=os.path.expanduser('~')+'/.qemu/backup/config'):
+if "QEMU_BACKUP_CONFIG" in os.environ:
+self.config_file = os.environ["QEMU_BACKUP_CONFIG"]
+else:
+self.config_file = config_file
+try:
+if not os.path.isdir(os.path.expanduser('~')+'/.qemu/backup'):
+os.makedirs(os.path.expanduser('~')+'/.qemu/backup')
+except:
+print("Cannot find the config file", file=sys.stderr)
+exit(1)
+self.config = configparser.ConfigParser()
+self.config.read(self.config_file)
+
+def write_config(self):
+"""
+Writes configuration to ini file.
+"""
+config_file = open(self.config_file+".tmp", 'w')
+self.config.write(config_file)
+config_file.flush()
+os.fsync(config_file.fileno())
+config_file.close()
+os.rename(self.config_file+".tmp", self.config_file)
+
+def get_socket_address(self, socket_address):
+"""
+Return Socket address in form of string or tuple
+"""
+if socket_address.startswith('tcp'):
+return (socket_address.split(':')[1],
+int(socket_address.split(':')[2]))
+return socket_address.split(':',2)[1]
+
+def _full_backup(self, guest_name):
+"""
+Performs full backup of guest
+"""
+if guest_name not in self.config.sections():
+print ("Cannot find specified guest", file=sys.stderr)
+exit(1)
+
+self.verify_guest_running(guest_name)
+connection = QEMUMonitorProtocol(
+ self.get_socket_address(
+ self.config[guest_name]['qmp']))
+connection.connect()
+cmd = {"execute": "transaction", "arguments": {"actions": []}}
+for key in self.config[guest_name]:
+if key.startswith("drive_"):
+drive = key[len('drive_'):]
+target = self.config[guest_name][key]
+sub_cmd = {"type": "drive-backup", "data": {"device": drive,
+"target": target,
+"sync": "full"}}
+