2017-02-28 00:01:06 +01:00
|
|
|
# encoding=utf-8
|
|
|
|
#
|
|
|
|
# The Qubes OS Project, https://www.qubes-os.org/
|
|
|
|
#
|
|
|
|
# Copyright (C) 2010-2015 Joanna Rutkowska <joanna@invisiblethingslab.com>
|
|
|
|
# Copyright (C) 2013-2015 Marek Marczykowski-Górecki
|
|
|
|
# <marmarek@invisiblethingslab.com>
|
|
|
|
# Copyright (C) 2014-2015 Wojtek Porczyk <woju@invisiblethingslab.com>
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Lesser 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.
|
|
|
|
#
|
|
|
|
|
|
|
|
'''Various utility functions.'''
|
2017-06-25 19:33:14 +02:00
|
|
|
import os
|
2017-02-28 00:01:06 +01:00
|
|
|
|
2017-05-11 23:21:04 +02:00
|
|
|
import qubesadmin.exc
|
2017-02-28 00:01:06 +01:00
|
|
|
|
|
|
|
|
|
|
|
def parse_size(size):
|
|
|
|
'''Parse human readable size into bytes.'''
|
|
|
|
units = [
|
|
|
|
('K', 1000), ('KB', 1000),
|
|
|
|
('M', 1000 * 1000), ('MB', 1000 * 1000),
|
|
|
|
('G', 1000 * 1000 * 1000), ('GB', 1000 * 1000 * 1000),
|
|
|
|
('Ki', 1024), ('KiB', 1024),
|
|
|
|
('Mi', 1024 * 1024), ('MiB', 1024 * 1024),
|
|
|
|
('Gi', 1024 * 1024 * 1024), ('GiB', 1024 * 1024 * 1024),
|
|
|
|
]
|
|
|
|
|
|
|
|
size = size.strip().upper()
|
|
|
|
if size.isdigit():
|
|
|
|
return int(size)
|
|
|
|
|
|
|
|
for unit, multiplier in units:
|
2017-03-13 04:31:38 +01:00
|
|
|
if size.endswith(unit.upper()):
|
2017-02-28 00:01:06 +01:00
|
|
|
size = size[:-len(unit)].strip()
|
|
|
|
return int(size) * multiplier
|
|
|
|
|
2017-05-11 23:21:04 +02:00
|
|
|
raise qubesadmin.exc.QubesException("Invalid size: {0}.".format(size))
|
2017-02-28 00:01:06 +01:00
|
|
|
|
|
|
|
|
|
|
|
def mbytes_to_kmg(size):
|
|
|
|
'''Convert mbytes to human readable format.'''
|
|
|
|
if size > 1024:
|
|
|
|
return "%d GiB" % (size / 1024)
|
2017-04-21 02:47:23 +02:00
|
|
|
return "%d MiB" % size
|
2017-02-28 00:01:06 +01:00
|
|
|
|
|
|
|
|
|
|
|
def kbytes_to_kmg(size):
|
|
|
|
'''Convert kbytes to human readable format.'''
|
|
|
|
if size > 1024:
|
|
|
|
return mbytes_to_kmg(size / 1024)
|
2017-04-21 02:47:23 +02:00
|
|
|
return "%d KiB" % size
|
2017-02-28 00:01:06 +01:00
|
|
|
|
|
|
|
|
|
|
|
def bytes_to_kmg(size):
|
|
|
|
'''Convert bytes to human readable format.'''
|
|
|
|
if size > 1024:
|
|
|
|
return kbytes_to_kmg(size / 1024)
|
2017-04-21 02:47:23 +02:00
|
|
|
return "%d B" % size
|
2017-02-28 00:01:06 +01:00
|
|
|
|
|
|
|
|
|
|
|
def size_to_human(size):
|
|
|
|
"""Humane readable size, with 1/10 precision"""
|
|
|
|
if size < 1024:
|
|
|
|
return str(size)
|
2018-07-16 02:25:25 +02:00
|
|
|
if size < 1024 * 1024:
|
2017-02-28 00:01:06 +01:00
|
|
|
return str(round(size / 1024.0, 1)) + ' KiB'
|
2018-07-16 02:25:25 +02:00
|
|
|
if size < 1024 * 1024 * 1024:
|
2017-02-28 00:01:06 +01:00
|
|
|
return str(round(size / (1024.0 * 1024), 1)) + ' MiB'
|
2017-04-21 02:47:23 +02:00
|
|
|
return str(round(size / (1024.0 * 1024 * 1024), 1)) + ' GiB'
|
2017-02-28 00:01:06 +01:00
|
|
|
|
|
|
|
|
|
|
|
def get_entry_point_one(group, name):
|
|
|
|
'''Get a single entry point of given type,
|
|
|
|
raise TypeError when there are multiple.
|
|
|
|
'''
|
2017-11-09 17:25:12 +01:00
|
|
|
import pkg_resources
|
2017-02-28 00:01:06 +01:00
|
|
|
epoints = tuple(pkg_resources.iter_entry_points(group, name))
|
|
|
|
if not epoints:
|
|
|
|
raise KeyError(name)
|
2019-03-07 03:07:42 +01:00
|
|
|
if len(epoints) > 1:
|
2017-02-28 00:01:06 +01:00
|
|
|
raise TypeError(
|
|
|
|
'more than 1 implementation of {!r} found: {}'.format(name,
|
|
|
|
', '.join('{}.{}'.format(ep.module_name, '.'.join(ep.attrs))
|
|
|
|
for ep in epoints)))
|
|
|
|
return epoints[0].load()
|
2017-06-25 19:33:14 +02:00
|
|
|
|
|
|
|
UPDATES_DEFAULT_VM_DISABLE_FLAG = \
|
|
|
|
'/var/lib/qubes/updates/vm-default-disable-updates'
|
|
|
|
|
|
|
|
def updates_vms_status(qvm_collection):
|
|
|
|
'''Check whether all VMs have the same check-updates value;
|
|
|
|
if yes, return it; otherwise, return None
|
|
|
|
'''
|
|
|
|
# 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.domains:
|
|
|
|
if vm.qid == 0:
|
|
|
|
continue
|
|
|
|
if vm.features.get('check-updates', True) != status:
|
|
|
|
# "mixed"
|
|
|
|
return None
|
|
|
|
return status
|
2018-07-18 23:50:54 +02:00
|
|
|
|
|
|
|
|
2018-07-19 20:06:00 +02:00
|
|
|
def vm_dependencies(app, reference_vm):
|
|
|
|
'''Helper function that returns a list of all the places a given VM is used
|
|
|
|
in. Output is a list of tuples (property_holder, property_name), with None
|
|
|
|
as property_holder for global properties
|
|
|
|
'''
|
2018-07-18 23:50:54 +02:00
|
|
|
|
|
|
|
result = []
|
|
|
|
|
|
|
|
global_properties = ['default_dispvm', 'default_netvm',
|
2019-02-18 23:09:31 +01:00
|
|
|
'default_template', 'clockvm', 'updatevm',
|
|
|
|
'management_dispvm']
|
2018-07-18 23:50:54 +02:00
|
|
|
|
|
|
|
for prop in global_properties:
|
|
|
|
if reference_vm == getattr(app, prop, None):
|
2018-07-19 19:52:22 +02:00
|
|
|
result.append((None, prop))
|
2018-07-18 23:50:54 +02:00
|
|
|
|
2019-02-18 23:09:31 +01:00
|
|
|
vm_properties = ['template', 'netvm', 'default_dispvm', 'management_dispvm']
|
2018-07-18 23:50:54 +02:00
|
|
|
|
|
|
|
for vm in app.domains:
|
|
|
|
for prop in vm_properties:
|
|
|
|
if reference_vm == getattr(vm, prop, None) and \
|
|
|
|
not vm.property_is_default(prop):
|
2018-07-19 19:52:22 +02:00
|
|
|
result.append((vm, prop))
|
2018-07-18 23:50:54 +02:00
|
|
|
|
|
|
|
return result
|