2013-01-23 15:55:14 +01:00
|
|
|
#!/usr/bin/python2
|
2012-02-22 06:09:25 +01:00
|
|
|
#
|
|
|
|
# The Qubes OS Project, http://www.qubes-os.org
|
|
|
|
#
|
|
|
|
# Copyright (C) 2012 Agnieszka Kostrzewa <agnieszka.kostrzewa@gmail.com>
|
|
|
|
#
|
|
|
|
# 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, write to the Free Software
|
|
|
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
#
|
|
|
|
#
|
|
|
|
|
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
from PyQt4.QtCore import *
|
|
|
|
from PyQt4.QtGui import *
|
|
|
|
|
|
|
|
import subprocess
|
|
|
|
import time
|
2014-01-13 05:19:06 +01:00
|
|
|
from qubes.qubes import QubesException
|
2012-02-22 06:09:25 +01:00
|
|
|
|
|
|
|
from thread_monitor import *
|
|
|
|
|
|
|
|
from datetime import datetime
|
|
|
|
from string import replace
|
|
|
|
|
2012-03-19 14:22:51 +01:00
|
|
|
mount_for_backup_path = '/usr/libexec/qubes-manager/mount_for_backup.sh'
|
2012-02-22 06:09:25 +01:00
|
|
|
|
|
|
|
def check_if_mounted(dev_path):
|
|
|
|
mounts_file = open("/proc/mounts")
|
|
|
|
for m in list(mounts_file):
|
|
|
|
if m.startswith(dev_path):
|
|
|
|
return m.split(" ")[1]
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
def mount_device(dev_path):
|
|
|
|
try:
|
|
|
|
mount_dir_name = "backup" + replace(str(datetime.now()),' ', '-').split(".")[0]
|
2012-03-19 14:22:51 +01:00
|
|
|
pmount_cmd = [mount_for_backup_path, dev_path, mount_dir_name]
|
2012-02-22 06:09:25 +01:00
|
|
|
res = subprocess.check_call(pmount_cmd)
|
|
|
|
except Exception as ex:
|
2012-04-21 21:03:26 +02:00
|
|
|
QMessageBox.warning (None, "Error mounting selected device!", "<b>Could not mount {0}.</b><br><br>ERROR: {1}".format(dev_path, ex))
|
2012-02-22 06:09:25 +01:00
|
|
|
return None
|
|
|
|
if res == 0:
|
|
|
|
dev_mount_path = "/media/"+mount_dir_name
|
|
|
|
return dev_mount_path
|
|
|
|
return None
|
|
|
|
|
|
|
|
def umount_device(dev_mount_path):
|
2012-04-22 17:13:42 +02:00
|
|
|
while True:
|
|
|
|
try:
|
2012-10-16 00:42:02 +02:00
|
|
|
pumount_cmd = ["sudo", "pumount", "--luks-force", dev_mount_path]
|
2012-04-22 17:13:42 +02:00
|
|
|
res = subprocess.check_call(pumount_cmd)
|
|
|
|
if res == 0:
|
|
|
|
dev_mount_path = None
|
|
|
|
return dev_mount_path
|
|
|
|
except Exception as ex:
|
|
|
|
title = "Error unmounting backup device!"
|
|
|
|
text = "<b>Could not unmount {0}.</b><br>\
|
|
|
|
<b>Please retry or unmount it manually using</b><br> pumount {0}.<br><br>\
|
|
|
|
ERROR: {1}".format(dev_mount_path, ex)
|
|
|
|
button = QMessageBox.warning (None, title, text, QMessageBox.Ok | QMessageBox.Retry, QMessageBox.Retry)
|
|
|
|
if button == QMessageBox.Ok:
|
|
|
|
return dev_mount_path
|
2012-02-22 06:09:25 +01:00
|
|
|
|
2014-01-12 05:42:19 +01:00
|
|
|
def detach_device(dialog, dev_name):
|
|
|
|
""" Detach device from dom0, if device was attached from some VM"""
|
|
|
|
if not dev_name.startswith(dialog.vm.name+":"):
|
|
|
|
with dialog.blk_manager.blk_lock:
|
|
|
|
dialog.blk_manager.detach_device(dialog.vm, dev_name)
|
|
|
|
dialog.blk_manager.update()
|
2014-01-13 05:09:07 +01:00
|
|
|
else:
|
|
|
|
# umount/LUKS remove do not trigger udev event on underlying device,
|
|
|
|
# so trigger it manually - to publish back as available device
|
|
|
|
subprocess.call(["sudo", "udevadm", "trigger", "--action=change",
|
|
|
|
"--subsystem-match=block",
|
|
|
|
"--sysname-match=%s" % dev_name.split(":")[1]])
|
|
|
|
with dialog.blk_manager.blk_lock:
|
|
|
|
dialog.blk_manager.update()
|
|
|
|
|
2014-01-12 05:42:19 +01:00
|
|
|
|
2013-09-27 09:19:38 +02:00
|
|
|
def fill_appvms_list(dialog):
|
|
|
|
dialog.appvm_combobox.clear()
|
2013-11-28 03:52:02 +01:00
|
|
|
dialog.appvm_combobox.addItem("dom0")
|
2013-09-27 09:19:38 +02:00
|
|
|
|
|
|
|
dialog.appvm_combobox.setCurrentIndex(0) #current selected is null ""
|
2013-11-28 03:45:22 +01:00
|
|
|
|
2013-09-27 09:19:38 +02:00
|
|
|
for vm in dialog.qvm_collection.values():
|
|
|
|
if vm.is_appvm() and vm.internal:
|
|
|
|
continue
|
|
|
|
if vm.is_template() and vm.installed_by_rpm:
|
|
|
|
continue
|
|
|
|
|
|
|
|
if vm.is_running() and vm.qid != 0:
|
|
|
|
dialog.appvm_combobox.addItem(vm.name)
|
2012-02-22 06:09:25 +01:00
|
|
|
|
|
|
|
def fill_devs_list(dialog):
|
|
|
|
dialog.dev_combobox.clear()
|
|
|
|
dialog.dev_combobox.addItem("None")
|
2013-11-28 03:45:22 +01:00
|
|
|
|
2012-04-13 01:08:36 +02:00
|
|
|
dialog.blk_manager.blk_lock.acquire()
|
2012-02-22 06:09:25 +01:00
|
|
|
for a in dialog.blk_manager.attached_devs:
|
|
|
|
if dialog.blk_manager.attached_devs[a]['attached_to']['vm'] == dialog.vm.name :
|
|
|
|
att = a + " " + unicode(dialog.blk_manager.attached_devs[a]['size']) + " " + dialog.blk_manager.attached_devs[a]['desc']
|
|
|
|
dialog.dev_combobox.addItem(att, QVariant(a))
|
|
|
|
for a in dialog.blk_manager.free_devs:
|
|
|
|
att = a + " " + unicode(dialog.blk_manager.free_devs[a]['size']) + " " + dialog.blk_manager.free_devs[a]['desc']
|
|
|
|
dialog.dev_combobox.addItem(att, QVariant(a))
|
2012-04-13 01:08:36 +02:00
|
|
|
dialog.blk_manager.blk_lock.release()
|
|
|
|
|
2012-02-22 06:09:25 +01:00
|
|
|
dialog.dev_combobox.setCurrentIndex(0) #current selected is null ""
|
|
|
|
dialog.prev_dev_idx = 0
|
|
|
|
dialog.dir_line_edit.clear()
|
2012-03-02 02:50:12 +01:00
|
|
|
enable_dir_line_edit(dialog, True)
|
2012-02-22 06:09:25 +01:00
|
|
|
|
|
|
|
|
|
|
|
def enable_dir_line_edit(dialog, boolean):
|
|
|
|
dialog.dir_line_edit.setEnabled(boolean)
|
2013-11-28 03:45:22 +01:00
|
|
|
dialog.select_path_button.setEnabled(boolean)
|
2012-02-22 06:09:25 +01:00
|
|
|
|
2014-01-12 05:20:48 +01:00
|
|
|
def update_device_appvm_enabled(dialog, idx):
|
|
|
|
# Only one of those can be used
|
|
|
|
dialog.dev_combobox.setEnabled(dialog.appvm_combobox.currentIndex() == 0)
|
|
|
|
dialog.appvm_combobox.setEnabled(dialog.dev_combobox.currentIndex() == 0)
|
2012-02-22 06:09:25 +01:00
|
|
|
|
|
|
|
def dev_combobox_activated(dialog, idx):
|
|
|
|
if idx == dialog.prev_dev_idx: #nothing has changed
|
|
|
|
return
|
|
|
|
#there was a change
|
|
|
|
|
|
|
|
dialog.dir_line_edit.setText("")
|
|
|
|
|
2014-01-12 05:42:19 +01:00
|
|
|
# umount old device if any
|
2012-02-22 06:09:25 +01:00
|
|
|
if dialog.dev_mount_path != None:
|
|
|
|
dialog.dev_mount_path = umount_device(dialog.dev_mount_path)
|
2012-03-02 02:50:12 +01:00
|
|
|
if dialog.dev_mount_path != None:
|
2012-02-22 06:09:25 +01:00
|
|
|
dialog.dev_combobox.setCurrentIndex(dialog.prev_dev_idx)
|
|
|
|
return
|
2014-01-12 05:42:19 +01:00
|
|
|
else:
|
|
|
|
detach_device(dialog,
|
|
|
|
str(dialog.dev_combobox.itemData(dialog.prev_dev_idx).toString()))
|
2012-02-22 06:09:25 +01:00
|
|
|
|
2014-01-12 05:42:19 +01:00
|
|
|
# then attach new one
|
|
|
|
if dialog.dev_combobox.currentIndex() != 0: #An existing device chosen
|
2012-02-22 06:09:25 +01:00
|
|
|
dev_name = str(dialog.dev_combobox.itemData(idx).toString())
|
|
|
|
|
2014-01-13 05:19:43 +01:00
|
|
|
if dev_name.startswith(dialog.vm.name+":"):
|
|
|
|
# originally attached to dom0
|
|
|
|
dev_path = "/dev/"+dev_name.split(":")[1]
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
with dialog.blk_manager.blk_lock:
|
|
|
|
if dev_name in dialog.blk_manager.free_devs:
|
2014-01-12 05:42:19 +01:00
|
|
|
#attach it to dom0, then treat it as an attached device
|
|
|
|
dialog.blk_manager.attach_device(dialog.vm, dev_name)
|
|
|
|
dialog.blk_manager.update()
|
|
|
|
|
2014-01-13 05:19:43 +01:00
|
|
|
if dev_name in dialog.blk_manager.attached_devs: #is attached to dom0
|
|
|
|
assert dialog.blk_manager.attached_devs[dev_name]['attached_to']['vm'] == dialog.vm.name
|
|
|
|
dev_path = "/dev/" + dialog.blk_manager.attached_devs[dev_name]['attached_to']['frontend']
|
|
|
|
else:
|
|
|
|
raise QubesException("device not attached?!")
|
|
|
|
except QubesException as ex:
|
|
|
|
QMessageBox.warning (None, "Error attaching selected device!",
|
|
|
|
"<b>Could not attach {0}.</b><br><br>ERROR: {1}".format(dev_name, ex))
|
|
|
|
dialog.dev_combobox.setCurrentIndex(0) #if couldn't mount - set current device to "None"
|
|
|
|
dialog.prev_dev_idx = 0
|
|
|
|
return
|
2012-02-22 06:09:25 +01:00
|
|
|
|
|
|
|
#check if device mounted
|
|
|
|
dialog.dev_mount_path = check_if_mounted(dev_path)
|
2012-03-02 02:50:12 +01:00
|
|
|
if dialog.dev_mount_path == None:
|
2012-02-22 06:09:25 +01:00
|
|
|
dialog.dev_mount_path = mount_device(dev_path)
|
2012-03-02 02:50:12 +01:00
|
|
|
if dialog.dev_mount_path == None:
|
2012-02-22 06:09:25 +01:00
|
|
|
dialog.dev_combobox.setCurrentIndex(0) #if couldn't mount - set current device to "None"
|
2012-03-02 02:50:12 +01:00
|
|
|
dialog.prev_dev_idx = 0
|
2014-01-12 05:42:19 +01:00
|
|
|
detach_device(dialog,
|
|
|
|
str(dialog.dev_combobox.itemData(idx).toString()))
|
2012-03-02 02:50:12 +01:00
|
|
|
return
|
2013-11-28 03:45:22 +01:00
|
|
|
|
2012-02-22 06:09:25 +01:00
|
|
|
dialog.prev_dev_idx = idx
|
2012-10-16 00:42:02 +02:00
|
|
|
|
2014-01-12 05:43:15 +01:00
|
|
|
if hasattr(dialog, 'selected_vms'):
|
|
|
|
# backup window
|
|
|
|
if dialog.dev_mount_path != None:
|
|
|
|
# Initialize path with root of mounted device
|
|
|
|
dialog.dir_line_edit.setText(dialog.dev_mount_path)
|
|
|
|
dialog.select_dir_page.emit(SIGNAL("completeChanged()"))
|
2013-11-28 03:50:17 +01:00
|
|
|
|
|
|
|
def select_path_button_clicked(dialog, select_file = False):
|
2014-01-11 22:57:34 +01:00
|
|
|
backup_location = str(dialog.dir_line_edit.text())
|
2012-02-22 06:09:25 +01:00
|
|
|
file_dialog = QFileDialog()
|
|
|
|
file_dialog.setReadOnly(True)
|
2012-03-02 02:50:12 +01:00
|
|
|
|
2013-11-28 03:50:17 +01:00
|
|
|
if select_file:
|
|
|
|
file_dialog_function = file_dialog.getOpenFileName
|
|
|
|
else:
|
|
|
|
file_dialog_function = file_dialog.getExistingDirectory
|
|
|
|
|
2013-09-27 09:19:38 +02:00
|
|
|
new_appvm = None
|
|
|
|
new_path = None
|
2013-11-28 03:50:17 +01:00
|
|
|
if dialog.appvm_combobox.currentIndex() != 0: #An existing appvm chosen
|
2013-09-27 09:19:38 +02:00
|
|
|
new_appvm = str(dialog.appvm_combobox.currentText())
|
|
|
|
elif dialog.dev_mount_path != None:
|
2013-11-28 03:50:17 +01:00
|
|
|
new_path = file_dialog_function(dialog, "Select backup location.", dialog.dev_mount_path)
|
2012-03-02 02:50:12 +01:00
|
|
|
else:
|
2014-01-11 22:57:34 +01:00
|
|
|
new_path = file_dialog_function(dialog, "Select backup location.", backup_location)
|
2013-11-28 03:50:17 +01:00
|
|
|
|
2013-09-27 09:19:38 +02:00
|
|
|
if new_path != None:
|
2014-01-10 03:37:28 +01:00
|
|
|
new_path = str(new_path)
|
2013-12-29 04:31:20 +01:00
|
|
|
if os.path.basename(new_path) == 'qubes.xml':
|
2014-01-11 22:57:34 +01:00
|
|
|
backup_location = os.path.dirname(new_path)
|
2013-12-29 04:31:20 +01:00
|
|
|
else:
|
2014-01-11 22:57:34 +01:00
|
|
|
backup_location = new_path
|
|
|
|
dialog.dir_line_edit.setText(backup_location)
|
2012-02-22 06:09:25 +01:00
|
|
|
|
2014-01-11 22:57:34 +01:00
|
|
|
if (new_path or new_appvm) and len(backup_location) > 0:
|
2013-09-27 09:19:38 +02:00
|
|
|
dialog.select_dir_page.emit(SIGNAL("completeChanged()"))
|
2012-02-22 06:09:25 +01:00
|
|
|
|
|
|
|
def simulate_long_lasting_proces(period, progress_callback):
|
|
|
|
for i in range(period):
|
|
|
|
progress_callback((i*100)/period)
|
|
|
|
time.sleep(1)
|
|
|
|
|
|
|
|
progress_callback(100)
|
|
|
|
return 0
|
|
|
|
|