2012-10-09 01:18:49 +02:00
|
|
|
#!/usr/bin/python
|
2014-05-18 21:01:21 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
2011-09-29 13:52:26 +02:00
|
|
|
#
|
|
|
|
# The Qubes OS Project, http://www.qubes-os.org
|
|
|
|
#
|
|
|
|
# Copyright (C) 2011 Marek Marczykowski <marmarek@invisiblethingslab.com>
|
2014-04-24 21:50:12 +02:00
|
|
|
# Copyright (C) 2014 Wojciech Porczyk <wojciech@porczyk.eu>
|
2011-09-29 13:52:26 +02:00
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
#
|
|
|
|
#
|
2014-12-26 02:56:38 +01:00
|
|
|
|
2015-03-29 17:33:02 +02:00
|
|
|
from __future__ import absolute_import
|
|
|
|
|
2014-12-12 03:58:33 +01:00
|
|
|
import string
|
|
|
|
from lxml import etree
|
|
|
|
from lxml.etree import ElementTree, SubElement, Element
|
2011-09-29 13:52:26 +02:00
|
|
|
|
2015-03-29 17:33:02 +02:00
|
|
|
from qubes.qubes import QubesException
|
|
|
|
from qubes.qubes import vmm
|
|
|
|
from qubes.qubes import system_path,vm_files
|
2011-09-29 13:52:26 +02:00
|
|
|
import sys
|
2011-10-31 13:31:17 +01:00
|
|
|
import os
|
2011-09-29 13:52:26 +02:00
|
|
|
import subprocess
|
|
|
|
import re
|
2012-02-07 16:09:35 +01:00
|
|
|
import time
|
2014-03-05 00:45:46 +01:00
|
|
|
import stat
|
2014-12-26 02:56:38 +01:00
|
|
|
import libvirt
|
2015-03-29 17:33:02 +02:00
|
|
|
from qubes.qdb import QubesDB,Error,DisconnectedError
|
2011-09-29 13:52:26 +02:00
|
|
|
|
|
|
|
import xen.lowlevel.xc
|
|
|
|
import xen.lowlevel.xs
|
|
|
|
|
2014-04-30 13:30:02 +02:00
|
|
|
BLKSIZE = 512
|
|
|
|
|
2014-12-12 03:58:33 +01:00
|
|
|
# all frontends, prefer xvdi
|
|
|
|
# TODO: get this from libvirt driver?
|
|
|
|
AVAILABLE_FRONTENDS = ['xvd'+c for c in
|
|
|
|
string.lowercase[8:]+string.lowercase[:8]]
|
|
|
|
|
2011-09-29 13:52:26 +02:00
|
|
|
def mbytes_to_kmg(size):
|
|
|
|
if size > 1024:
|
2012-02-07 16:22:04 +01:00
|
|
|
return "%d GiB" % (size/1024)
|
2011-09-29 13:52:26 +02:00
|
|
|
else:
|
2012-02-07 16:22:04 +01:00
|
|
|
return "%d MiB" % size
|
2011-09-29 13:52:26 +02:00
|
|
|
|
|
|
|
def kbytes_to_kmg(size):
|
|
|
|
if size > 1024:
|
|
|
|
return mbytes_to_kmg(size/1024)
|
|
|
|
else:
|
2012-02-07 16:22:04 +01:00
|
|
|
return "%d KiB" % size
|
2011-09-29 13:52:26 +02:00
|
|
|
|
|
|
|
def bytes_to_kmg(size):
|
|
|
|
if size > 1024:
|
|
|
|
return kbytes_to_kmg(size/1024)
|
|
|
|
else:
|
|
|
|
return "%d B" % size
|
|
|
|
|
2012-02-07 16:09:35 +01:00
|
|
|
def size_to_human (size):
|
|
|
|
"""Humane readable size, with 1/10 precission"""
|
|
|
|
if size < 1024:
|
|
|
|
return str (size);
|
|
|
|
elif size < 1024*1024:
|
|
|
|
return str(round(size/1024.0,1)) + ' KiB'
|
|
|
|
elif size < 1024*1024*1024:
|
|
|
|
return str(round(size/(1024.0*1024),1)) + ' MiB'
|
|
|
|
else:
|
|
|
|
return str(round(size/(1024.0*1024*1024),1)) + ' GiB'
|
|
|
|
|
2012-02-10 20:46:35 +01:00
|
|
|
def parse_size(size):
|
|
|
|
units = [ ('K', 1024), ('KB', 1024),
|
|
|
|
('M', 1024*1024), ('MB', 1024*1024),
|
|
|
|
('G', 1024*1024*1024), ('GB', 1024*1024*1024),
|
|
|
|
]
|
|
|
|
|
|
|
|
size = size.strip().upper()
|
|
|
|
if size.isdigit():
|
2012-03-28 00:20:13 +02:00
|
|
|
return int(size)
|
2012-02-10 20:46:35 +01:00
|
|
|
|
|
|
|
for unit, multiplier in units:
|
|
|
|
if size.endswith(unit):
|
|
|
|
size = size[:-len(unit)].strip()
|
|
|
|
return int(size)*multiplier
|
|
|
|
|
|
|
|
raise QubesException("Invalid size: {0}.".format(size))
|
|
|
|
|
2014-04-24 21:50:12 +02:00
|
|
|
def get_disk_usage_one(st):
|
|
|
|
try:
|
|
|
|
return st.st_blocks * BLKSIZE
|
2014-04-30 13:30:02 +02:00
|
|
|
except AttributeError:
|
2014-04-24 21:50:12 +02:00
|
|
|
return st.st_size
|
|
|
|
|
|
|
|
def get_disk_usage(path):
|
|
|
|
try:
|
2014-05-02 16:59:17 +02:00
|
|
|
st = os.lstat(path)
|
|
|
|
except OSError:
|
2014-04-24 21:50:12 +02:00
|
|
|
return 0
|
|
|
|
|
|
|
|
ret = get_disk_usage_one(st)
|
|
|
|
|
|
|
|
# if path is not a directory, this is skipped
|
|
|
|
for dirpath, dirnames, filenames in os.walk(path):
|
|
|
|
for name in dirnames + filenames:
|
2014-05-02 16:59:17 +02:00
|
|
|
ret += get_disk_usage_one(os.lstat(os.path.join(dirpath, name)))
|
2014-04-24 21:50:12 +02:00
|
|
|
|
|
|
|
return ret
|
|
|
|
|
2012-02-10 10:54:18 +01:00
|
|
|
def print_stdout(text):
|
|
|
|
print (text)
|
|
|
|
|
|
|
|
def print_stderr(text):
|
|
|
|
print >> sys.stderr, (text)
|
2011-09-29 13:52:26 +02:00
|
|
|
|
2012-04-11 01:35:51 +02:00
|
|
|
###### Block devices ########
|
|
|
|
|
2011-09-29 13:52:26 +02:00
|
|
|
def block_devid_to_name(devid):
|
|
|
|
major = devid / 256
|
|
|
|
minor = devid % 256
|
|
|
|
|
|
|
|
dev_class = ""
|
|
|
|
if major == 202:
|
|
|
|
dev_class = "xvd"
|
|
|
|
elif major == 8:
|
|
|
|
dev_class = "sd"
|
|
|
|
else:
|
|
|
|
raise QubesException("Unknown device class %d" % major)
|
|
|
|
|
|
|
|
if minor % 16 == 0:
|
|
|
|
return "%s%c" % (dev_class, ord('a')+minor/16)
|
|
|
|
else:
|
|
|
|
return "%s%c%d" % (dev_class, ord('a')+minor/16, minor%16)
|
|
|
|
|
|
|
|
def block_name_to_majorminor(name):
|
|
|
|
# check if it is already devid
|
|
|
|
if isinstance(name, int):
|
|
|
|
return (name / 256, name % 256)
|
|
|
|
if name.isdigit():
|
|
|
|
return (int(name) / 256, int(name) % 256)
|
|
|
|
|
2014-03-05 00:45:46 +01:00
|
|
|
if os.path.exists('/dev/%s' % name):
|
|
|
|
blk_info = os.stat(os.path.realpath('/dev/%s' % name))
|
|
|
|
if stat.S_ISBLK(blk_info.st_mode):
|
|
|
|
return (blk_info.st_rdev / 256, blk_info.st_rdev % 256)
|
|
|
|
|
2011-09-29 13:52:26 +02:00
|
|
|
major = 0
|
|
|
|
minor = 0
|
2012-03-15 10:58:57 +01:00
|
|
|
dXpY_style = False
|
2011-09-29 13:52:26 +02:00
|
|
|
disk = True
|
2012-08-26 14:41:35 +02:00
|
|
|
|
|
|
|
if name.startswith("xvd"):
|
2011-09-29 13:52:26 +02:00
|
|
|
major = 202
|
2012-08-26 14:41:35 +02:00
|
|
|
elif name.startswith("sd"):
|
2011-09-29 13:52:26 +02:00
|
|
|
major = 8
|
2012-08-26 14:41:35 +02:00
|
|
|
elif name.startswith("mmcblk"):
|
|
|
|
dXpY_style = True
|
2012-03-15 10:58:57 +01:00
|
|
|
major = 179
|
2011-09-29 13:52:26 +02:00
|
|
|
elif name.startswith("scd"):
|
|
|
|
disk = False
|
|
|
|
major = 11
|
|
|
|
elif name.startswith("sr"):
|
|
|
|
disk = False
|
|
|
|
major = 11
|
2012-03-26 20:29:49 +02:00
|
|
|
elif name.startswith("loop"):
|
2014-09-19 00:35:13 +02:00
|
|
|
dXpY_style = True
|
2012-03-26 20:29:49 +02:00
|
|
|
disk = False
|
|
|
|
major = 7
|
2012-03-09 00:04:36 +01:00
|
|
|
elif name.startswith("md"):
|
2014-01-08 06:42:29 +01:00
|
|
|
dXpY_style = True
|
2012-03-09 00:04:36 +01:00
|
|
|
major = 9
|
2013-11-16 01:29:50 +01:00
|
|
|
elif name.startswith("dm-"):
|
|
|
|
disk = False
|
|
|
|
major = 253
|
2011-09-29 13:52:26 +02:00
|
|
|
else:
|
2012-03-09 00:00:20 +01:00
|
|
|
# Unknown device
|
|
|
|
return (0, 0)
|
2011-09-29 13:52:26 +02:00
|
|
|
|
2012-08-26 14:41:35 +02:00
|
|
|
if not dXpY_style:
|
2014-06-07 04:54:15 +02:00
|
|
|
name_match = re.match(r"^([a-z]+)([a-z-])([0-9]*)$", name)
|
2012-08-26 14:41:35 +02:00
|
|
|
else:
|
|
|
|
name_match = re.match(r"^([a-z]+)([0-9]*)(?:p([0-9]+))?$", name)
|
|
|
|
if not name_match:
|
|
|
|
raise QubesException("Invalid device name: %s" % name)
|
|
|
|
|
2011-09-29 13:52:26 +02:00
|
|
|
if disk:
|
2012-03-15 10:58:57 +01:00
|
|
|
if dXpY_style:
|
|
|
|
minor = int(name_match.group(2))*8
|
|
|
|
else:
|
|
|
|
minor = (ord(name_match.group(2))-ord('a')) * 16
|
2011-09-29 13:52:26 +02:00
|
|
|
else:
|
|
|
|
minor = 0
|
|
|
|
if name_match.group(3):
|
|
|
|
minor += int(name_match.group(3))
|
|
|
|
|
|
|
|
return (major, minor)
|
|
|
|
|
|
|
|
|
|
|
|
def block_name_to_devid(name):
|
|
|
|
# check if it is already devid
|
|
|
|
if isinstance(name, int):
|
|
|
|
return name
|
|
|
|
if name.isdigit():
|
|
|
|
return int(name)
|
|
|
|
|
|
|
|
(major, minor) = block_name_to_majorminor(name)
|
|
|
|
return major << 8 | minor
|
|
|
|
|
2012-02-14 20:57:43 +01:00
|
|
|
def block_find_unused_frontend(vm = None):
|
|
|
|
assert vm is not None
|
|
|
|
assert vm.is_running()
|
|
|
|
|
2014-12-12 03:58:33 +01:00
|
|
|
xml = vm.libvirt_domain.XMLDesc()
|
|
|
|
parsed_xml = etree.fromstring(xml)
|
|
|
|
used = [target.get('dev', None) for target in
|
|
|
|
parsed_xml.xpath("//domain/devices/disk/target")]
|
|
|
|
for dev in AVAILABLE_FRONTENDS:
|
|
|
|
if dev not in used:
|
|
|
|
return dev
|
2012-02-14 20:57:43 +01:00
|
|
|
return None
|
|
|
|
|
2014-12-12 03:58:33 +01:00
|
|
|
def block_list_vm(vm, system_disks = False):
|
|
|
|
name_re = re.compile(r"^[a-z0-9-]{1,12}$")
|
|
|
|
device_re = re.compile(r"^[a-z0-9/-]{1,64}$")
|
2011-09-29 13:52:26 +02:00
|
|
|
# FIXME: any better idea of desc_re?
|
|
|
|
desc_re = re.compile(r"^.{1,255}$")
|
|
|
|
mode_re = re.compile(r"^[rw]$")
|
|
|
|
|
2014-12-12 03:58:33 +01:00
|
|
|
assert vm is not None
|
2012-10-08 00:48:20 +02:00
|
|
|
|
2014-12-12 03:58:33 +01:00
|
|
|
if not vm.is_running():
|
|
|
|
return []
|
2011-09-29 13:52:26 +02:00
|
|
|
|
|
|
|
devices_list = {}
|
|
|
|
|
2015-03-26 22:10:49 +01:00
|
|
|
try:
|
|
|
|
untrusted_devices = vm.qdb.multiread('/qubes-block-devices/')
|
2015-03-29 23:47:39 +02:00
|
|
|
except Error:
|
2015-03-29 17:22:15 +02:00
|
|
|
vm.refresh()
|
2015-03-26 22:10:49 +01:00
|
|
|
return {}
|
2014-12-12 03:58:33 +01:00
|
|
|
|
|
|
|
def get_dev_item(dev, item):
|
|
|
|
return untrusted_devices.get(
|
|
|
|
'/qubes-block-devices/%s/%s' % (dev, item),
|
|
|
|
None)
|
|
|
|
|
|
|
|
untrusted_devices_names = list(set(map(lambda x: x.split("/")[2],
|
|
|
|
untrusted_devices.keys())))
|
|
|
|
for untrusted_dev_name in untrusted_devices_names:
|
|
|
|
if name_re.match(untrusted_dev_name):
|
|
|
|
dev_name = untrusted_dev_name
|
|
|
|
untrusted_device_size = get_dev_item(dev_name, 'size')
|
|
|
|
untrusted_device_desc = get_dev_item(dev_name, 'desc')
|
|
|
|
untrusted_device_mode = get_dev_item(dev_name, 'mode')
|
|
|
|
untrusted_device_device = get_dev_item(dev_name, 'device')
|
|
|
|
if untrusted_device_desc is None or untrusted_device_mode is None\
|
|
|
|
or untrusted_device_size is None:
|
|
|
|
print >>sys.stderr, "Missing field in %s device parameters" %\
|
|
|
|
dev_name
|
2012-03-30 00:19:15 +02:00
|
|
|
continue
|
2014-12-12 03:58:33 +01:00
|
|
|
if untrusted_device_device is None:
|
|
|
|
untrusted_device_device = '/dev/' + dev_name
|
|
|
|
if not device_re.match(untrusted_device_device):
|
|
|
|
print >> sys.stderr, "Invalid %s device path in VM '%s'" % (
|
|
|
|
dev_name, vm.name)
|
2011-09-29 13:52:26 +02:00
|
|
|
continue
|
2014-12-12 03:58:33 +01:00
|
|
|
device_device = untrusted_device_device
|
|
|
|
if not untrusted_device_size.isdigit():
|
|
|
|
print >> sys.stderr, "Invalid %s device size in VM '%s'" % (
|
|
|
|
dev_name, vm.name)
|
2011-09-29 13:52:26 +02:00
|
|
|
continue
|
2014-12-12 03:58:33 +01:00
|
|
|
device_size = int(untrusted_device_size)
|
|
|
|
if not desc_re.match(untrusted_device_desc):
|
|
|
|
print >> sys.stderr, "Invalid %s device desc in VM '%s'" % (
|
|
|
|
dev_name, vm.name)
|
2011-09-29 13:52:26 +02:00
|
|
|
continue
|
2014-12-12 03:58:33 +01:00
|
|
|
device_desc = untrusted_device_desc
|
|
|
|
if not mode_re.match(untrusted_device_mode):
|
|
|
|
print >> sys.stderr, "Invalid %s device mode in VM '%s'" % (
|
|
|
|
dev_name, vm.name)
|
2012-03-09 00:00:20 +01:00
|
|
|
continue
|
2014-12-12 03:58:33 +01:00
|
|
|
device_mode = untrusted_device_mode
|
2012-03-27 12:59:47 +02:00
|
|
|
|
|
|
|
if not system_disks:
|
2014-12-12 03:58:33 +01:00
|
|
|
if vm.qid == 0 and device_desc.startswith(system_path[
|
|
|
|
"qubes_base_dir"]):
|
2012-03-27 12:59:47 +02:00
|
|
|
continue
|
|
|
|
|
2014-12-12 03:58:33 +01:00
|
|
|
visible_name = "%s:%s" % (vm.name, dev_name)
|
|
|
|
devices_list[visible_name] = {
|
|
|
|
"name": visible_name,
|
|
|
|
"vm": vm.name,
|
|
|
|
"device": device_device,
|
|
|
|
"size": device_size,
|
|
|
|
"desc": device_desc,
|
|
|
|
"mode": device_mode
|
|
|
|
}
|
2011-09-29 13:52:26 +02:00
|
|
|
|
|
|
|
return devices_list
|
|
|
|
|
2014-12-12 03:58:33 +01:00
|
|
|
def block_list(qvmc = None, vm = None, system_disks = False):
|
|
|
|
if vm is not None:
|
|
|
|
if not vm.is_running():
|
|
|
|
return []
|
|
|
|
else:
|
|
|
|
vm_list = [ vm ]
|
|
|
|
else:
|
|
|
|
if qvmc is None:
|
|
|
|
raise QubesException("You must pass either qvm or vm argument")
|
|
|
|
vm_list = qvmc.values()
|
|
|
|
|
|
|
|
devices_list = {}
|
|
|
|
for vm in vm_list:
|
|
|
|
devices_list.update(block_list_vm(vm, system_disks))
|
|
|
|
return devices_list
|
|
|
|
|
|
|
|
def block_check_attached(qvmc, device):
|
|
|
|
"""
|
|
|
|
|
|
|
|
@type qvmc: QubesVmCollection
|
|
|
|
"""
|
|
|
|
if qvmc is None:
|
|
|
|
# TODO: ValueError
|
|
|
|
raise QubesException("You need to pass qvmc argument")
|
|
|
|
|
|
|
|
for vm in qvmc.values():
|
2015-04-14 23:07:54 +02:00
|
|
|
if vm.qid == 0:
|
|
|
|
# Connecting devices to dom0 not supported
|
|
|
|
continue
|
2015-07-01 04:42:44 +02:00
|
|
|
if not vm.is_running():
|
|
|
|
continue
|
2015-03-29 23:38:36 +02:00
|
|
|
try:
|
|
|
|
libvirt_domain = vm.libvirt_domain
|
|
|
|
if libvirt_domain:
|
|
|
|
xml = libvirt_domain.XMLDesc()
|
|
|
|
else:
|
|
|
|
xml = None
|
|
|
|
except libvirt.libvirtError:
|
|
|
|
if vmm.libvirt_conn.virConnGetLastError()[0] == libvirt.VIR_ERR_NO_DOMAIN:
|
2015-04-14 23:07:54 +02:00
|
|
|
xml = None
|
2015-03-29 23:38:36 +02:00
|
|
|
else:
|
|
|
|
raise
|
|
|
|
if xml:
|
2014-12-12 03:58:33 +01:00
|
|
|
parsed_xml = etree.fromstring(xml)
|
|
|
|
disks = parsed_xml.xpath("//domain/devices/disk")
|
|
|
|
for disk in disks:
|
|
|
|
backend_name = 'dom0'
|
2015-07-13 00:58:11 +02:00
|
|
|
if disk.find('backenddomain') is not None:
|
|
|
|
backend_name = disk.find('backenddomain').get('name')
|
2014-12-12 03:58:33 +01:00
|
|
|
source = disk.find('source')
|
|
|
|
if disk.get('type') == 'file':
|
|
|
|
path = source.get('file')
|
|
|
|
elif disk.get('type') == 'block':
|
|
|
|
path = source.get('dev')
|
2012-02-14 20:55:51 +01:00
|
|
|
else:
|
2014-12-12 03:58:33 +01:00
|
|
|
# TODO: logger
|
|
|
|
print >>sys.stderr, "Unknown disk type '%s' attached to " \
|
|
|
|
"VM '%s'" % (source.get('type'),
|
|
|
|
vm.name)
|
|
|
|
continue
|
|
|
|
if backend_name == device['vm'] and path == device['device']:
|
|
|
|
return {
|
|
|
|
"frontend": disk.find('target').get('dev'),
|
|
|
|
"vm": vm}
|
2011-09-29 13:52:26 +02:00
|
|
|
return None
|
|
|
|
|
2014-12-12 03:58:33 +01:00
|
|
|
def device_attach_check(vm, backend_vm, device, frontend, mode):
|
2012-10-09 23:15:47 +02:00
|
|
|
""" Checks all the parameters, dies on errors """
|
2011-09-29 13:52:26 +02:00
|
|
|
if not vm.is_running():
|
|
|
|
raise QubesException("VM %s not running" % vm.name)
|
|
|
|
|
|
|
|
if not backend_vm.is_running():
|
|
|
|
raise QubesException("VM %s not running" % backend_vm.name)
|
|
|
|
|
2014-12-12 03:58:33 +01:00
|
|
|
if device['mode'] == 'r' and mode == 'w':
|
|
|
|
raise QubesException("Cannot attach read-only device in read-write "
|
|
|
|
"mode")
|
|
|
|
|
|
|
|
def block_attach(qvmc, vm, device, frontend=None, mode="w", auto_detach=False, wait=True):
|
|
|
|
backend_vm = qvmc.get_vm_by_name(device['vm'])
|
|
|
|
device_attach_check(vm, backend_vm, device, frontend, mode)
|
2012-02-14 20:57:43 +01:00
|
|
|
if frontend is None:
|
|
|
|
frontend = block_find_unused_frontend(vm)
|
|
|
|
if frontend is None:
|
|
|
|
raise QubesException("No unused frontend found")
|
|
|
|
else:
|
|
|
|
# Check if any device attached at this frontend
|
2014-12-12 03:58:33 +01:00
|
|
|
xml = vm.libvirt_domain.XMLDesc()
|
|
|
|
parsed_xml = etree.fromstring(xml)
|
|
|
|
disks = parsed_xml.xpath("//domain/devices/disk/target[@dev='%s']" %
|
|
|
|
frontend)
|
|
|
|
if len(disks):
|
2012-02-14 20:57:43 +01:00
|
|
|
raise QubesException("Frontend %s busy in VM %s, detach it first" % (frontend, vm.name))
|
2011-09-29 13:52:26 +02:00
|
|
|
|
|
|
|
# Check if this device is attached to some domain
|
2014-12-12 03:58:33 +01:00
|
|
|
attached_vm = block_check_attached(qvmc, device)
|
2011-09-29 13:52:26 +02:00
|
|
|
if attached_vm:
|
|
|
|
if auto_detach:
|
2014-12-12 03:58:33 +01:00
|
|
|
block_detach(attached_vm['vm'], attached_vm['frontend'])
|
2011-09-29 13:52:26 +02:00
|
|
|
else:
|
2014-12-12 03:58:33 +01:00
|
|
|
raise QubesException("Device %s from %s already connected to VM "
|
|
|
|
"%s as %s" % (device['device'],
|
|
|
|
backend_vm.name, attached_vm['vm'], attached_vm['frontend']))
|
|
|
|
|
|
|
|
disk = Element("disk")
|
|
|
|
disk.set('type', 'block')
|
|
|
|
disk.set('device', 'disk')
|
|
|
|
SubElement(disk, 'driver').set('name', 'phy')
|
|
|
|
SubElement(disk, 'source').set('dev', device['device'])
|
|
|
|
SubElement(disk, 'target').set('dev', frontend)
|
|
|
|
if backend_vm.qid != 0:
|
2015-04-04 16:18:10 +02:00
|
|
|
SubElement(disk, 'backenddomain').set('name', device['vm'])
|
2014-12-12 03:58:33 +01:00
|
|
|
vm.libvirt_domain.attachDevice(etree.tostring(disk, encoding='utf-8'))
|
2015-04-14 23:08:52 +02:00
|
|
|
try:
|
|
|
|
# trigger watches to update device status
|
|
|
|
# FIXME: this should be removed once libvirt will report such
|
|
|
|
# events itself
|
|
|
|
vm.qdb.write('/qubes-block-devices', '')
|
|
|
|
except Error:
|
|
|
|
pass
|
2014-12-12 03:58:33 +01:00
|
|
|
|
|
|
|
def block_detach(vm, frontend = "xvdi"):
|
|
|
|
|
|
|
|
xml = vm.libvirt_domain.XMLDesc()
|
|
|
|
parsed_xml = etree.fromstring(xml)
|
|
|
|
attached = parsed_xml.xpath("//domain/devices/disk")
|
|
|
|
for disk in attached:
|
|
|
|
if frontend is not None and disk.find('target').get('dev') != frontend:
|
|
|
|
# Not the device we are looking for
|
|
|
|
continue
|
|
|
|
if frontend is None:
|
|
|
|
# ignore system disks
|
|
|
|
if disk.find('domain') == None and \
|
|
|
|
disk.find('source').get('dev').startswith(system_path[
|
|
|
|
"qubes_base_dir"]):
|
|
|
|
continue
|
|
|
|
vm.libvirt_domain.detachDevice(etree.tostring(disk, encoding='utf-8'))
|
2015-04-14 23:08:52 +02:00
|
|
|
try:
|
|
|
|
# trigger watches to update device status
|
|
|
|
# FIXME: this should be removed once libvirt will report such
|
|
|
|
# events itself
|
|
|
|
vm.qdb.write('/qubes-block-devices', '')
|
|
|
|
except Error:
|
|
|
|
pass
|
2011-09-29 13:52:26 +02:00
|
|
|
|
2014-12-12 03:58:33 +01:00
|
|
|
def block_detach_all(vm):
|
2012-04-13 00:29:13 +02:00
|
|
|
""" Detach all non-system devices"""
|
2014-12-12 03:58:33 +01:00
|
|
|
|
|
|
|
block_detach(vm, None)
|
2012-04-13 00:29:13 +02:00
|
|
|
|
2012-10-08 00:44:40 +02:00
|
|
|
####### USB devices ######
|
|
|
|
|
2012-10-26 23:30:55 +02:00
|
|
|
usb_ver_re = re.compile(r"^(1|2)$")
|
2012-11-07 01:33:19 +01:00
|
|
|
usb_device_re = re.compile(r"^[0-9]+-[0-9]+(_[0-9]+)?$")
|
2012-11-07 17:43:41 +01:00
|
|
|
usb_port_re = re.compile(r"^$|^[0-9]+-[0-9]+(\.[0-9]+)?$")
|
2012-10-26 23:30:55 +02:00
|
|
|
|
2012-10-24 22:30:24 +02:00
|
|
|
def usb_setup(backend_vm_xid, vm_xid, devid, usb_ver):
|
2012-10-10 22:01:06 +02:00
|
|
|
"""
|
|
|
|
Attach frontend to the backend.
|
2012-10-12 21:28:22 +02:00
|
|
|
backend_vm_xid - id of the backend domain
|
|
|
|
vm_xid - id of the frontend domain
|
|
|
|
devid - id of the pvusb controller
|
2012-10-10 22:01:06 +02:00
|
|
|
"""
|
2012-10-12 22:06:42 +02:00
|
|
|
num_ports = 8
|
2013-05-17 04:06:29 +02:00
|
|
|
trans = vmm.xs.transaction_start()
|
2012-10-10 22:01:06 +02:00
|
|
|
|
2012-10-12 22:06:42 +02:00
|
|
|
be_path = "/local/domain/%d/backend/vusb/%d/%d" % (backend_vm_xid, vm_xid, devid)
|
2012-10-12 21:28:22 +02:00
|
|
|
fe_path = "/local/domain/%d/device/vusb/%d" % (vm_xid, devid)
|
2012-10-10 22:01:06 +02:00
|
|
|
|
2012-10-12 21:28:22 +02:00
|
|
|
be_perm = [{'dom': backend_vm_xid}, {'dom': vm_xid, 'read': True} ]
|
|
|
|
fe_perm = [{'dom': vm_xid}, {'dom': backend_vm_xid, 'read': True} ]
|
2012-10-10 22:01:06 +02:00
|
|
|
|
|
|
|
# Create directories and set permissions
|
2013-05-17 04:06:29 +02:00
|
|
|
vmm.xs.write(trans, be_path, "")
|
|
|
|
vmm.xs.set_permissions(trans, be_path, be_perm)
|
2012-10-10 22:01:06 +02:00
|
|
|
|
2013-05-17 04:06:29 +02:00
|
|
|
vmm.xs.write(trans, fe_path, "")
|
|
|
|
vmm.xs.set_permissions(trans, fe_path, fe_perm)
|
2012-10-10 22:01:06 +02:00
|
|
|
|
2012-10-12 21:28:22 +02:00
|
|
|
# Write backend information into the location that frontend looks for
|
2013-05-17 04:06:29 +02:00
|
|
|
vmm.xs.write(trans, "%s/backend-id" % fe_path, str(backend_vm_xid))
|
|
|
|
vmm.xs.write(trans, "%s/backend" % fe_path, be_path)
|
2012-10-10 22:01:06 +02:00
|
|
|
|
2012-10-12 21:28:22 +02:00
|
|
|
# Write frontend information into the location that backend looks for
|
2013-05-17 04:06:29 +02:00
|
|
|
vmm.xs.write(trans, "%s/frontend-id" % be_path, str(vm_xid))
|
|
|
|
vmm.xs.write(trans, "%s/frontend" % be_path, fe_path)
|
2012-10-10 22:01:06 +02:00
|
|
|
|
|
|
|
# Write USB Spec version field.
|
2013-05-17 04:06:29 +02:00
|
|
|
vmm.xs.write(trans, "%s/usb-ver" % be_path, usb_ver)
|
2012-10-10 22:01:06 +02:00
|
|
|
|
|
|
|
# Write virtual root hub field.
|
2013-05-17 04:06:29 +02:00
|
|
|
vmm.xs.write(trans, "%s/num-ports" % be_path, str(num_ports))
|
2012-10-10 22:01:06 +02:00
|
|
|
for port in range(1, num_ports+1):
|
|
|
|
# Set all port to disconnected state
|
2013-05-17 04:06:29 +02:00
|
|
|
vmm.xs.write(trans, "%s/port/%d" % (be_path, port), "")
|
2012-10-10 22:01:06 +02:00
|
|
|
|
|
|
|
# Set state to XenbusStateInitialising
|
2013-05-17 04:06:29 +02:00
|
|
|
vmm.xs.write(trans, "%s/state" % fe_path, "1")
|
|
|
|
vmm.xs.write(trans, "%s/state" % be_path, "1")
|
|
|
|
vmm.xs.write(trans, "%s/online" % be_path, "1")
|
2012-10-10 22:01:06 +02:00
|
|
|
|
2013-05-17 04:06:29 +02:00
|
|
|
vmm.xs.transaction_end(trans)
|
2012-10-10 22:01:06 +02:00
|
|
|
|
2012-11-07 01:14:12 +01:00
|
|
|
def usb_decode_device_from_xs(xs_encoded_device):
|
|
|
|
""" recover actual device name (xenstore doesn't allow dot in key names, so it was translated to underscore) """
|
|
|
|
return xs_encoded_device.replace('_', '.')
|
|
|
|
|
|
|
|
def usb_encode_device_for_xs(device):
|
|
|
|
""" encode actual device name (xenstore doesn't allow dot in key names, so translated it into underscore) """
|
|
|
|
return device.replace('.', '_')
|
|
|
|
|
2012-10-08 00:44:40 +02:00
|
|
|
def usb_list():
|
2012-10-10 16:54:23 +02:00
|
|
|
"""
|
|
|
|
Returns a dictionary of USB devices (for PVUSB backends running in all VM).
|
|
|
|
The dictionary is keyed by 'name' (see below), each element is a dictionary itself:
|
|
|
|
vm = name of the backend domain
|
|
|
|
xid = xid of the backend domain
|
|
|
|
device = <frontend device number>-<frontend port number>
|
|
|
|
name = <name of backend domain>:<frontend device number>-<frontend port number>
|
|
|
|
desc = description
|
|
|
|
"""
|
2012-10-08 00:44:40 +02:00
|
|
|
# FIXME: any better idea of desc_re?
|
|
|
|
desc_re = re.compile(r"^.{1,255}$")
|
|
|
|
|
|
|
|
devices_list = {}
|
|
|
|
|
2013-05-17 04:06:29 +02:00
|
|
|
xs_trans = vmm.xs.transaction_start()
|
|
|
|
vm_list = vmm.xs.ls(xs_trans, '/local/domain')
|
2012-10-08 00:44:40 +02:00
|
|
|
|
|
|
|
for xid in vm_list:
|
2013-05-17 04:06:29 +02:00
|
|
|
vm_name = vmm.xs.read(xs_trans, '/local/domain/%s/name' % xid)
|
|
|
|
vm_devices = vmm.xs.ls(xs_trans, '/local/domain/%s/qubes-usb-devices' % xid)
|
2012-10-08 00:44:40 +02:00
|
|
|
if vm_devices is None:
|
|
|
|
continue
|
2012-11-07 01:14:12 +01:00
|
|
|
# when listing devices in xenstore we get encoded names
|
|
|
|
for xs_encoded_device in vm_devices:
|
2012-10-08 00:44:40 +02:00
|
|
|
# Sanitize device id
|
2012-11-07 01:33:19 +01:00
|
|
|
if not usb_device_re.match(xs_encoded_device):
|
2012-11-08 02:59:55 +01:00
|
|
|
print >> sys.stderr, "Invalid device id in backend VM '%s'" % vm_name
|
2012-10-08 00:44:40 +02:00
|
|
|
continue
|
2012-11-07 01:14:12 +01:00
|
|
|
device = usb_decode_device_from_xs(xs_encoded_device)
|
2013-05-17 04:06:29 +02:00
|
|
|
device_desc = vmm.xs.read(xs_trans, '/local/domain/%s/qubes-usb-devices/%s/desc' % (xid, xs_encoded_device))
|
2012-10-08 00:44:40 +02:00
|
|
|
if not desc_re.match(device_desc):
|
|
|
|
print >> sys.stderr, "Invalid %s device desc in VM '%s'" % (device, vm_name)
|
|
|
|
continue
|
|
|
|
visible_name = "%s:%s" % (vm_name, device)
|
2012-10-26 23:09:36 +02:00
|
|
|
# grab version
|
2013-05-17 04:06:29 +02:00
|
|
|
usb_ver = vmm.xs.read(xs_trans, '/local/domain/%s/qubes-usb-devices/%s/usb-ver' % (xid, xs_encoded_device))
|
2012-10-26 23:09:36 +02:00
|
|
|
if usb_ver is None or not usb_ver_re.match(usb_ver):
|
|
|
|
print >> sys.stderr, "Invalid %s device USB version in VM '%s'" % (device, vm_name)
|
|
|
|
continue
|
2012-10-08 00:44:40 +02:00
|
|
|
devices_list[visible_name] = {"name": visible_name, "xid":int(xid),
|
|
|
|
"vm": vm_name, "device":device,
|
2012-10-26 23:09:36 +02:00
|
|
|
"desc":device_desc,
|
|
|
|
"usb_ver":usb_ver}
|
2012-10-08 00:44:40 +02:00
|
|
|
|
2013-05-17 04:06:29 +02:00
|
|
|
vmm.xs.transaction_end(xs_trans)
|
2012-10-08 00:44:40 +02:00
|
|
|
return devices_list
|
|
|
|
|
2012-10-12 23:34:18 +02:00
|
|
|
def usb_check_attached(xs_trans, backend_vm, device):
|
2012-10-10 16:54:23 +02:00
|
|
|
"""
|
|
|
|
Checks if the given device in the given backend attached to any frontend.
|
|
|
|
Parameters:
|
|
|
|
backend_vm - xid of the backend domain
|
|
|
|
device - device name in the backend domain
|
|
|
|
Returns None or a dictionary:
|
|
|
|
vm - the name of the frontend domain
|
|
|
|
xid - xid of the frontend domain
|
2012-10-12 23:34:18 +02:00
|
|
|
frontend - frontend device number FIXME
|
|
|
|
devid - frontend port number FIXME
|
2012-10-10 16:54:23 +02:00
|
|
|
"""
|
2012-10-09 23:33:17 +02:00
|
|
|
# sample xs content: /local/domain/0/backend/vusb/4/0/port/1 = "7-5"
|
2012-10-10 16:07:34 +02:00
|
|
|
attached_dev = None
|
2013-05-17 04:06:29 +02:00
|
|
|
vms = vmm.xs.ls(xs_trans, '/local/domain/%d/backend/vusb' % backend_vm)
|
2012-10-09 23:15:47 +02:00
|
|
|
if vms is None:
|
|
|
|
return None
|
|
|
|
for vm in vms:
|
2012-10-10 17:08:24 +02:00
|
|
|
if not vm.isdigit():
|
|
|
|
print >> sys.stderr, "Invalid VM id"
|
|
|
|
continue
|
2013-05-17 04:06:29 +02:00
|
|
|
frontend_devs = vmm.xs.ls(xs_trans, '/local/domain/%d/backend/vusb/%s' % (backend_vm, vm))
|
2012-10-09 23:15:47 +02:00
|
|
|
if frontend_devs is None:
|
|
|
|
continue
|
|
|
|
for frontend_dev in frontend_devs:
|
2012-10-10 17:08:24 +02:00
|
|
|
if not frontend_dev.isdigit():
|
|
|
|
print >> sys.stderr, "Invalid frontend in VM %s" % vm
|
|
|
|
continue
|
2013-05-17 04:06:29 +02:00
|
|
|
ports = vmm.xs.ls(xs_trans, '/local/domain/%d/backend/vusb/%s/%s/port' % (backend_vm, vm, frontend_dev))
|
2012-10-09 23:15:47 +02:00
|
|
|
if ports is None:
|
|
|
|
continue
|
|
|
|
for port in ports:
|
2012-11-07 01:33:19 +01:00
|
|
|
# FIXME: refactor, see similar loop in usb_find_unused_frontend(), use usb_list() instead?
|
2012-10-10 17:08:24 +02:00
|
|
|
if not port.isdigit():
|
|
|
|
print >> sys.stderr, "Invalid port in VM %s frontend %s" % (vm, frontend)
|
|
|
|
continue
|
2013-05-17 04:06:29 +02:00
|
|
|
dev = vmm.xs.read(xs_trans, '/local/domain/%d/backend/vusb/%s/%s/port/%s' % (backend_vm, vm, frontend_dev, port))
|
2012-11-07 17:43:41 +01:00
|
|
|
if dev == "":
|
2014-03-05 00:45:46 +01:00
|
|
|
continue
|
2012-11-07 01:33:19 +01:00
|
|
|
# Sanitize device id
|
2012-11-07 17:43:41 +01:00
|
|
|
if not usb_port_re.match(dev):
|
2012-11-08 02:59:55 +01:00
|
|
|
print >> sys.stderr, "Invalid device id in backend VM %d @ %s/%s/port/%s" % \
|
|
|
|
(backend_vm, vm, frontend_dev, port)
|
2012-11-07 01:33:19 +01:00
|
|
|
continue
|
2012-11-07 17:43:41 +01:00
|
|
|
if dev == device:
|
2012-10-09 23:15:47 +02:00
|
|
|
frontend = "%s-%s" % (frontend_dev, port)
|
2013-05-04 04:45:55 +02:00
|
|
|
#TODO
|
2012-10-10 16:58:20 +02:00
|
|
|
vm_name = xl_ctx.domid_to_name(int(vm))
|
2012-10-11 01:08:49 +02:00
|
|
|
if vm_name is None:
|
|
|
|
# FIXME: should we wipe references to frontends running on nonexistent VMs?
|
|
|
|
continue
|
2012-10-10 16:58:20 +02:00
|
|
|
attached_dev = {"xid":int(vm), "frontend": frontend, "devid": device, "vm": vm_name}
|
2012-10-10 16:07:34 +02:00
|
|
|
break
|
|
|
|
return attached_dev
|
2012-10-08 23:08:54 +02:00
|
|
|
|
2012-10-12 22:12:40 +02:00
|
|
|
#def usb_check_frontend_busy(vm, front_dev, port):
|
|
|
|
# devport = frontend.split("-")
|
|
|
|
# if len(devport) != 2:
|
|
|
|
# raise QubesException("Malformed frontend syntax, must be in device-port format")
|
|
|
|
# # FIXME:
|
2013-05-17 04:06:29 +02:00
|
|
|
# # return vmm.xs.read('', '/local/domain/%d/device/vusb/%d/state' % (vm.xid, frontend)) == '4'
|
2012-10-12 22:12:40 +02:00
|
|
|
# return False
|
2012-10-09 23:15:47 +02:00
|
|
|
|
2012-10-24 22:30:24 +02:00
|
|
|
def usb_find_unused_frontend(xs_trans, backend_vm_xid, vm_xid, usb_ver):
|
2012-10-12 21:28:22 +02:00
|
|
|
"""
|
|
|
|
Find an unused frontend/port to link the given backend with the given frontend.
|
2012-10-12 23:34:18 +02:00
|
|
|
Creates new frontend if needed.
|
|
|
|
Returns frontend specification in <device>-<port> format.
|
2012-10-12 21:28:22 +02:00
|
|
|
"""
|
|
|
|
|
2012-11-07 00:41:13 +01:00
|
|
|
# This variable holds an index of last frontend scanned by the loop below.
|
|
|
|
# If nothing found, this value will be used to derive the index of a new frontend.
|
2012-10-12 21:28:22 +02:00
|
|
|
last_frontend_dev = -1
|
2012-11-07 00:41:13 +01:00
|
|
|
|
2013-05-17 04:06:29 +02:00
|
|
|
frontend_devs = vmm.xs.ls(xs_trans, "/local/domain/%d/device/vusb" % vm_xid)
|
2012-10-12 21:28:22 +02:00
|
|
|
if frontend_devs is not None:
|
|
|
|
for frontend_dev in frontend_devs:
|
|
|
|
if not frontend_dev.isdigit():
|
|
|
|
print >> sys.stderr, "Invalid frontend_dev in VM %d" % vm_xid
|
|
|
|
continue
|
|
|
|
frontend_dev = int(frontend_dev)
|
|
|
|
fe_path = "/local/domain/%d/device/vusb/%d" % (vm_xid, frontend_dev)
|
2013-05-17 04:06:29 +02:00
|
|
|
if vmm.xs.read(xs_trans, "%s/backend-id" % fe_path) == str(backend_vm_xid):
|
|
|
|
if vmm.xs.read(xs_trans, '/local/domain/%d/backend/vusb/%d/%d/usb-ver' % (backend_vm_xid, vm_xid, frontend_dev)) != usb_ver:
|
2012-10-26 23:30:55 +02:00
|
|
|
last_frontend_dev = frontend_dev
|
2012-10-24 22:30:24 +02:00
|
|
|
continue
|
2012-11-07 00:24:05 +01:00
|
|
|
# here: found an existing frontend already connected to right backend using an appropriate USB version
|
2013-05-17 04:06:29 +02:00
|
|
|
ports = vmm.xs.ls(xs_trans, '/local/domain/%d/backend/vusb/%d/%d/port' % (backend_vm_xid, vm_xid, frontend_dev))
|
2012-10-12 21:28:22 +02:00
|
|
|
if ports is None:
|
2012-11-07 00:37:50 +01:00
|
|
|
print >> sys.stderr, "No ports in VM %d frontend_dev %d?" % (vm_xid, frontend_dev)
|
2012-10-26 23:30:55 +02:00
|
|
|
last_frontend_dev = frontend_dev
|
2012-10-12 21:28:22 +02:00
|
|
|
continue
|
|
|
|
for port in ports:
|
2012-11-07 01:33:19 +01:00
|
|
|
# FIXME: refactor, see similar loop in usb_check_attached(), use usb_list() instead?
|
2012-10-12 21:28:22 +02:00
|
|
|
if not port.isdigit():
|
|
|
|
print >> sys.stderr, "Invalid port in VM %d frontend_dev %d" % (vm_xid, frontend_dev)
|
|
|
|
continue
|
|
|
|
port = int(port)
|
2013-05-17 04:06:29 +02:00
|
|
|
dev = vmm.xs.read(xs_trans, '/local/domain/%d/backend/vusb/%d/%s/port/%s' % (backend_vm_xid, vm_xid, frontend_dev, port))
|
2012-11-07 01:33:19 +01:00
|
|
|
# Sanitize device id
|
2012-11-07 17:43:41 +01:00
|
|
|
if not usb_port_re.match(dev):
|
2012-11-08 02:42:09 +01:00
|
|
|
print >> sys.stderr, "Invalid device id in backend VM %d @ %d/%d/port/%d" % \
|
|
|
|
(backend_vm_xid, vm_xid, frontend_dev, port)
|
2012-11-07 01:33:19 +01:00
|
|
|
continue
|
2012-11-07 17:43:41 +01:00
|
|
|
if dev == "":
|
2012-10-12 21:28:22 +02:00
|
|
|
return '%d-%d' % (frontend_dev, port)
|
|
|
|
last_frontend_dev = frontend_dev
|
|
|
|
|
2012-10-12 22:06:42 +02:00
|
|
|
# create a new frontend_dev and link it to the backend
|
|
|
|
frontend_dev = last_frontend_dev + 1
|
2012-10-24 22:30:24 +02:00
|
|
|
usb_setup(backend_vm_xid, vm_xid, frontend_dev, usb_ver)
|
2012-10-12 22:06:42 +02:00
|
|
|
return '%d-%d' % (frontend_dev, 1)
|
2014-03-05 00:45:46 +01:00
|
|
|
|
2012-10-08 23:08:54 +02:00
|
|
|
def usb_attach(vm, backend_vm, device, frontend=None, auto_detach=False, wait=True):
|
2012-10-09 23:15:47 +02:00
|
|
|
device_attach_check(vm, backend_vm, device, frontend)
|
|
|
|
|
2013-05-17 04:06:29 +02:00
|
|
|
xs_trans = vmm.xs.transaction_start()
|
2012-10-26 23:30:55 +02:00
|
|
|
|
2012-11-07 01:14:12 +01:00
|
|
|
xs_encoded_device = usb_encode_device_for_xs(device)
|
2013-05-17 04:06:29 +02:00
|
|
|
usb_ver = vmm.xs.read(xs_trans, '/local/domain/%s/qubes-usb-devices/%s/usb-ver' % (backend_vm.xid, xs_encoded_device))
|
2012-10-26 23:30:55 +02:00
|
|
|
if usb_ver is None or not usb_ver_re.match(usb_ver):
|
2013-05-17 04:06:29 +02:00
|
|
|
vmm.xs.transaction_end(xs_trans)
|
2012-10-26 23:30:55 +02:00
|
|
|
raise QubesException("Invalid %s device USB version in VM '%s'" % (device, backend_vm.name))
|
|
|
|
|
2012-10-09 23:15:47 +02:00
|
|
|
if frontend is None:
|
2012-10-24 22:30:24 +02:00
|
|
|
frontend = usb_find_unused_frontend(xs_trans, backend_vm.xid, vm.xid, usb_ver)
|
2012-10-09 23:15:47 +02:00
|
|
|
else:
|
|
|
|
# Check if any device attached at this frontend
|
2012-10-12 22:12:40 +02:00
|
|
|
#if usb_check_frontend_busy(vm, frontend):
|
|
|
|
# raise QubesException("Frontend %s busy in VM %s, detach it first" % (frontend, vm.name))
|
2013-05-17 04:06:29 +02:00
|
|
|
vmm.xs.transaction_end(xs_trans)
|
2012-10-12 22:12:40 +02:00
|
|
|
raise NotImplementedError("Explicit USB frontend specification is not implemented yet")
|
2012-10-09 23:15:47 +02:00
|
|
|
|
|
|
|
# Check if this device is attached to some domain
|
2012-10-12 23:34:18 +02:00
|
|
|
attached_vm = usb_check_attached(xs_trans, backend_vm.xid, device)
|
2013-05-17 04:06:29 +02:00
|
|
|
vmm.xs.transaction_end(xs_trans)
|
2012-10-12 23:34:18 +02:00
|
|
|
|
2012-10-09 23:15:47 +02:00
|
|
|
if attached_vm:
|
|
|
|
if auto_detach:
|
2012-10-12 23:34:18 +02:00
|
|
|
usb_detach(backend_vm, attached_vm)
|
2012-10-09 23:15:47 +02:00
|
|
|
else:
|
|
|
|
raise QubesException("Device %s from %s already connected to VM %s as %s" % (device, backend_vm.name, attached_vm['vm'], attached_vm['frontend']))
|
|
|
|
|
2012-10-11 02:10:47 +02:00
|
|
|
# Run helper script
|
|
|
|
xl_cmd = [ '/usr/lib/qubes/xl-qvm-usb-attach.py', str(vm.xid), device, frontend, str(backend_vm.xid) ]
|
2012-10-09 23:15:47 +02:00
|
|
|
subprocess.check_call(xl_cmd)
|
2012-10-08 23:08:54 +02:00
|
|
|
|
2012-10-12 23:34:18 +02:00
|
|
|
def usb_detach(backend_vm, attachment):
|
|
|
|
xl_cmd = [ '/usr/lib/qubes/xl-qvm-usb-detach.py', str(attachment['xid']), attachment['devid'], attachment['frontend'], str(backend_vm.xid) ]
|
|
|
|
subprocess.check_call(xl_cmd)
|
2012-10-08 23:08:54 +02:00
|
|
|
|
2012-10-12 23:34:18 +02:00
|
|
|
def usb_detach_all(vm):
|
|
|
|
raise NotImplementedError("Detaching all devices from a given VM is not implemented yet")
|
2012-10-08 23:08:54 +02:00
|
|
|
|
2012-04-11 01:35:51 +02:00
|
|
|
####### QubesWatch ######
|
|
|
|
|
2012-04-11 01:34:17 +02:00
|
|
|
def only_in_first_list(l1, l2):
|
|
|
|
ret=[]
|
|
|
|
for i in l1:
|
|
|
|
if not i in l2:
|
|
|
|
ret.append(i)
|
|
|
|
return ret
|
|
|
|
|
|
|
|
class QubesWatch(object):
|
|
|
|
def __init__(self):
|
2014-12-26 02:56:38 +01:00
|
|
|
self._qdb = {}
|
|
|
|
self._qdb_events = {}
|
2012-04-11 01:34:17 +02:00
|
|
|
self.block_callback = None
|
2014-03-31 03:42:05 +02:00
|
|
|
self.meminfo_callback = None
|
2012-04-11 01:34:17 +02:00
|
|
|
self.domain_callback = None
|
2014-12-26 02:56:38 +01:00
|
|
|
vmm.libvirt_conn.domainEventRegisterAny(
|
|
|
|
None,
|
|
|
|
libvirt.VIR_DOMAIN_EVENT_ID_LIFECYCLE,
|
|
|
|
self._domain_list_changed, None)
|
|
|
|
vmm.libvirt_conn.domainEventRegisterAny(
|
|
|
|
None,
|
|
|
|
libvirt.VIR_DOMAIN_EVENT_ID_DEVICE_REMOVED,
|
|
|
|
self._device_removed, None)
|
|
|
|
# TODO: device attach libvirt event
|
|
|
|
for vm in vmm.libvirt_conn.listAllDomains():
|
2015-03-29 23:38:36 +02:00
|
|
|
try:
|
|
|
|
if vm.isActive():
|
|
|
|
self._register_watches(vm)
|
|
|
|
except libvirt.libvirtError:
|
|
|
|
# this will happen if we loose a race with another tool,
|
|
|
|
# which can just remove the domain
|
|
|
|
if vmm.libvirt_conn.virConnGetLastError()[0] == libvirt.VIR_ERR_NO_DOMAIN:
|
|
|
|
pass
|
|
|
|
raise
|
2014-12-26 02:56:38 +01:00
|
|
|
# and for dom0
|
|
|
|
self._register_watches(None)
|
|
|
|
|
|
|
|
def _qdb_handler(self, watch, fd, events, domain_name):
|
|
|
|
try:
|
|
|
|
path = self._qdb[domain_name].read_watch()
|
|
|
|
except DisconnectedError:
|
|
|
|
libvirt.virEventRemoveHandle(watch)
|
|
|
|
del(self._qdb_events[domain_name])
|
|
|
|
self._qdb[domain_name].close()
|
|
|
|
del(self._qdb[domain_name])
|
|
|
|
return
|
|
|
|
if path.startswith('/qubes-block-devices'):
|
|
|
|
if self.block_callback is not None:
|
|
|
|
self.block_callback(domain_name)
|
|
|
|
|
2012-04-11 01:34:17 +02:00
|
|
|
|
|
|
|
def setup_block_watch(self, callback):
|
|
|
|
self.block_callback = callback
|
|
|
|
|
2014-03-31 03:42:05 +02:00
|
|
|
def setup_meminfo_watch(self, callback):
|
2015-02-07 01:14:22 +01:00
|
|
|
raise NotImplementedError
|
2014-03-31 03:42:05 +02:00
|
|
|
|
2012-04-11 01:34:17 +02:00
|
|
|
def setup_domain_watch(self, callback):
|
|
|
|
self.domain_callback = callback
|
|
|
|
|
2014-03-31 03:42:05 +02:00
|
|
|
def get_meminfo_key(self, xid):
|
|
|
|
return '/local/domain/%s/memory/meminfo' % xid
|
2012-04-11 01:34:17 +02:00
|
|
|
|
2014-12-26 02:56:38 +01:00
|
|
|
def _register_watches(self, libvirt_domain):
|
|
|
|
if libvirt_domain:
|
|
|
|
name = libvirt_domain.name()
|
|
|
|
if name in self._qdb:
|
|
|
|
return
|
|
|
|
# open separate connection to Qubes DB:
|
|
|
|
# 1. to not confuse pull() with responses to real commands sent from
|
|
|
|
# other threads (like read, write etc) with watch events
|
|
|
|
# 2. to not think whether QubesDB is thread-safe (it isn't)
|
|
|
|
while libvirt_domain.isActive() and name not in self._qdb:
|
|
|
|
try:
|
|
|
|
self._qdb[name] = QubesDB(name)
|
|
|
|
except Error as e:
|
|
|
|
if e.args[0] != 2:
|
|
|
|
raise
|
|
|
|
time.sleep(0.5)
|
2015-04-14 23:07:54 +02:00
|
|
|
if name not in self._qdb:
|
|
|
|
# domain no longer active
|
|
|
|
return
|
2014-12-26 02:56:38 +01:00
|
|
|
else:
|
|
|
|
name = "dom0"
|
|
|
|
self._qdb[name] = QubesDB(name)
|
|
|
|
self._qdb[name].watch('/qubes-block-devices')
|
|
|
|
self._qdb_events[name] = libvirt.virEventAddHandle(
|
|
|
|
self._qdb[name].watch_fd(),
|
|
|
|
libvirt.VIR_EVENT_HANDLE_READABLE,
|
|
|
|
self._qdb_handler, name)
|
|
|
|
|
|
|
|
def _unregister_watches(self, libvirt_domain):
|
|
|
|
name = libvirt_domain.name()
|
|
|
|
if name in self._qdb_events:
|
|
|
|
libvirt.virEventRemoveHandle(self._qdb_events[name])
|
|
|
|
del(self._qdb_events[name])
|
|
|
|
if name in self._qdb:
|
|
|
|
self._qdb[name].close()
|
|
|
|
del(self._qdb[name])
|
|
|
|
|
|
|
|
def _domain_list_changed(self, conn, domain, event, reason, param):
|
|
|
|
if event == libvirt.VIR_DOMAIN_EVENT_STARTED:
|
|
|
|
self._register_watches(domain)
|
|
|
|
elif event == libvirt.VIR_DOMAIN_EVENT_STOPPED:
|
|
|
|
self._unregister_watches(domain)
|
|
|
|
else:
|
|
|
|
# ignore other events for now
|
|
|
|
return None
|
2012-04-11 01:34:17 +02:00
|
|
|
if self.domain_callback:
|
2015-03-30 00:08:00 +02:00
|
|
|
self.domain_callback(name=domain.name(), uuid=domain.UUID())
|
2012-04-11 01:34:17 +02:00
|
|
|
|
2014-12-26 02:56:38 +01:00
|
|
|
def _device_removed(self, conn, domain, device, param):
|
|
|
|
if self.block_callback is not None:
|
|
|
|
self.block_callback(domain.name())
|
2012-04-11 01:34:17 +02:00
|
|
|
|
|
|
|
def watch_loop(self):
|
|
|
|
while True:
|
2014-12-26 02:56:38 +01:00
|
|
|
libvirt.virEventRunDefaultImpl()
|
2012-04-11 01:34:17 +02:00
|
|
|
|
2014-04-11 07:06:12 +02:00
|
|
|
##### updates check #####
|
|
|
|
|
|
|
|
UPDATES_DOM0_DISABLE_FLAG='/var/lib/qubes/updates/disable-updates'
|
2015-08-03 22:43:07 +02:00
|
|
|
UPDATES_DEFAULT_VM_DISABLE_FLAG=\
|
|
|
|
'/var/lib/qubes/updates/vm-default-disable-updates'
|
2014-04-11 07:06:12 +02:00
|
|
|
|
|
|
|
def updates_vms_toggle(qvm_collection, value):
|
2015-08-03 22:43:07 +02:00
|
|
|
# Flag for new VMs
|
|
|
|
if value:
|
|
|
|
if os.path.exists(UPDATES_DEFAULT_VM_DISABLE_FLAG):
|
|
|
|
os.unlink(UPDATES_DEFAULT_VM_DISABLE_FLAG)
|
|
|
|
else:
|
|
|
|
open(UPDATES_DEFAULT_VM_DISABLE_FLAG, "w").close()
|
|
|
|
|
|
|
|
# Change for existing VMs
|
2014-04-11 07:06:12 +02:00
|
|
|
for vm in qvm_collection.values():
|
|
|
|
if vm.qid == 0:
|
|
|
|
continue
|
|
|
|
if value:
|
|
|
|
vm.services.pop('qubes-update-check', None)
|
|
|
|
if vm.is_running():
|
|
|
|
try:
|
|
|
|
vm.run("systemctl start qubes-update-check.timer",
|
|
|
|
user="root")
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
vm.services['qubes-update-check'] = False
|
|
|
|
if vm.is_running():
|
|
|
|
try:
|
|
|
|
vm.run("systemctl stop qubes-update-check.timer",
|
|
|
|
user="root")
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
def updates_dom0_toggle(qvm_collection, value):
|
|
|
|
if value:
|
|
|
|
if os.path.exists(UPDATES_DOM0_DISABLE_FLAG):
|
|
|
|
os.unlink(UPDATES_DOM0_DISABLE_FLAG)
|
|
|
|
else:
|
|
|
|
open(UPDATES_DOM0_DISABLE_FLAG, "w").close()
|
|
|
|
|
|
|
|
def updates_dom0_status(qvm_collection):
|
|
|
|
return not os.path.exists(UPDATES_DOM0_DISABLE_FLAG)
|
|
|
|
|
2015-08-03 22:43:07 +02:00
|
|
|
def updates_vms_status(qvm_collection):
|
|
|
|
# default value:
|
|
|
|
status = not os.path.exists(UPDATES_DEFAULT_VM_DISABLE_FLAG)
|
|
|
|
# check if all the VMs uses the default value
|
|
|
|
for vm in qvm_collection.values():
|
|
|
|
if vm.qid == 0:
|
|
|
|
continue
|
|
|
|
if vm.services.get('qubes-update-check', True) != status:
|
|
|
|
# "mixed"
|
|
|
|
return None
|
|
|
|
return status
|
2014-04-11 07:06:12 +02:00
|
|
|
|
2011-09-29 13:52:26 +02:00
|
|
|
# vim:sw=4:et:
|