2017-03-01 16:39:12 +01:00
|
|
|
# -*- encoding: utf8 -*-
|
|
|
|
#
|
|
|
|
# The Qubes OS Project, http://www.qubes-os.org
|
|
|
|
#
|
|
|
|
# Copyright (C) 2017 Marek Marczykowski-Górecki
|
|
|
|
# <marmarek@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, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
'''Storage subsystem.'''
|
|
|
|
|
2017-05-25 12:40:22 +02:00
|
|
|
|
2017-03-01 16:39:12 +01:00
|
|
|
class Volume(object):
|
|
|
|
'''Storage volume.'''
|
|
|
|
def __init__(self, app, pool=None, vid=None, vm=None, vm_name=None):
|
|
|
|
'''Construct a Volume object.
|
|
|
|
|
|
|
|
Volume may be identified using pool+vid, or vm+vm_name. Either of
|
|
|
|
those argument pairs must be given.
|
|
|
|
|
|
|
|
:param Qubes app: application instance
|
|
|
|
:param str pool: pool name
|
|
|
|
:param str vid: volume id (within pool)
|
|
|
|
:param str vm: owner VM name
|
|
|
|
:param str vm_name: name within owning VM (like 'private', 'root' etc)
|
|
|
|
'''
|
|
|
|
self.app = app
|
|
|
|
if pool is None and vm is None:
|
|
|
|
raise ValueError('Either pool or vm must be given')
|
|
|
|
if pool is not None and vid is None:
|
|
|
|
raise ValueError('If pool is given, vid must be too.')
|
|
|
|
if vm is not None and vm_name is None:
|
|
|
|
raise ValueError('If vm is given, vm_name must be too.')
|
|
|
|
self._pool = pool
|
|
|
|
self._vid = vid
|
|
|
|
self._vm = vm
|
|
|
|
self._vm_name = vm_name
|
|
|
|
self._info = None
|
|
|
|
|
2017-05-25 12:40:22 +02:00
|
|
|
def _qubesd_call(self, func_name, payload=None, payload_stream=None):
|
2017-03-01 16:39:12 +01:00
|
|
|
'''Make a call to qubesd regarding this volume
|
|
|
|
|
|
|
|
:param str func_name: API function name, like `Info` or `Resize`
|
|
|
|
:param bytes payload: Payload to send.
|
2017-05-25 12:40:22 +02:00
|
|
|
:param file payload_stream: Stream to pipe payload from. Only one of
|
|
|
|
`payload` and `payload_stream` can be used.
|
2017-03-01 16:39:12 +01:00
|
|
|
'''
|
|
|
|
if self._vm is not None:
|
2017-05-12 19:36:03 +02:00
|
|
|
method = 'admin.vm.volume.' + func_name
|
2017-03-01 16:39:12 +01:00
|
|
|
dest = self._vm
|
|
|
|
arg = self._vm_name
|
|
|
|
else:
|
2017-05-25 12:40:22 +02:00
|
|
|
if payload_stream:
|
|
|
|
raise NotImplementedError(
|
|
|
|
'payload_stream not implemented for '
|
|
|
|
'admin.pool.volume.* calls')
|
2017-05-12 19:36:03 +02:00
|
|
|
method = 'admin.pool.volume.' + func_name
|
2017-03-01 16:39:12 +01:00
|
|
|
dest = 'dom0'
|
2017-03-12 03:01:54 +01:00
|
|
|
arg = self._pool
|
|
|
|
if payload is not None:
|
|
|
|
payload = self._vid.encode('ascii') + b' ' + payload
|
|
|
|
else:
|
|
|
|
payload = self._vid.encode('ascii')
|
2017-05-25 12:40:22 +02:00
|
|
|
return self.app.qubesd_call(dest, method, arg, payload=payload,
|
|
|
|
payload_stream=payload_stream)
|
2017-03-01 16:39:12 +01:00
|
|
|
|
|
|
|
def _fetch_info(self, force=True):
|
|
|
|
'''Fetch volume properties
|
|
|
|
|
|
|
|
Populate self._info dict
|
|
|
|
|
|
|
|
:param bool force: refresh self._info, even if already populated.
|
|
|
|
'''
|
|
|
|
if not force and self._info is not None:
|
|
|
|
return
|
|
|
|
info = self._qubesd_call('Info')
|
|
|
|
info = info.decode('ascii')
|
|
|
|
self._info = dict([line.split('=', 1) for line in info.splitlines()])
|
|
|
|
|
2017-03-12 21:21:16 +01:00
|
|
|
def __eq__(self, other):
|
|
|
|
if isinstance(other, Volume):
|
|
|
|
return self.pool == other.pool and self.vid == other.vid
|
|
|
|
return NotImplemented
|
|
|
|
|
2017-06-20 00:00:35 +02:00
|
|
|
def __lt__(self, other):
|
|
|
|
# pylint: disable=protected-access
|
|
|
|
if isinstance(other, Volume):
|
|
|
|
if self._vm and other._vm:
|
|
|
|
return (self._vm, self._vm_name) < (other._vm, other._vm_name)
|
|
|
|
elif self._vid and other._vid:
|
|
|
|
return (self._pool, self._vid) < (other._pool, other._vid)
|
|
|
|
return NotImplemented
|
|
|
|
|
|
|
|
|
2017-06-19 03:02:43 +02:00
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
'''per-VM volume name, if available'''
|
|
|
|
return self._vm_name
|
|
|
|
|
2017-03-01 16:39:12 +01:00
|
|
|
@property
|
|
|
|
def pool(self):
|
|
|
|
'''Storage volume pool name.'''
|
|
|
|
if self._pool is not None:
|
|
|
|
return self._pool
|
2017-04-21 02:47:23 +02:00
|
|
|
self._fetch_info()
|
|
|
|
return str(self._info['pool'])
|
2017-03-01 16:39:12 +01:00
|
|
|
|
|
|
|
@property
|
|
|
|
def vid(self):
|
|
|
|
'''Storage volume id, unique within given pool.'''
|
|
|
|
if self._vid is not None:
|
|
|
|
return self._vid
|
2017-04-21 02:47:23 +02:00
|
|
|
self._fetch_info()
|
|
|
|
return str(self._info['vid'])
|
2017-03-01 16:39:12 +01:00
|
|
|
|
|
|
|
@property
|
|
|
|
def size(self):
|
|
|
|
'''Size of volume, in bytes.'''
|
|
|
|
self._fetch_info(True)
|
|
|
|
return int(self._info['size'])
|
|
|
|
|
|
|
|
@property
|
|
|
|
def usage(self):
|
|
|
|
'''Used volume space, in bytes.'''
|
|
|
|
self._fetch_info(True)
|
|
|
|
return int(self._info['usage'])
|
|
|
|
|
|
|
|
@property
|
|
|
|
def rw(self):
|
|
|
|
'''True if volume is read-write.'''
|
|
|
|
self._fetch_info()
|
|
|
|
return self._info['rw'] == 'True'
|
|
|
|
|
2018-03-18 17:00:39 +01:00
|
|
|
@rw.setter
|
|
|
|
def rw(self, value):
|
|
|
|
'''Set rw property'''
|
|
|
|
self._qubesd_call('Set.rw', str(value).encode('ascii'))
|
|
|
|
self._info = None
|
|
|
|
|
2017-03-01 16:39:12 +01:00
|
|
|
@property
|
|
|
|
def snap_on_start(self):
|
|
|
|
'''Create a snapshot from source on VM start.'''
|
|
|
|
self._fetch_info()
|
|
|
|
return self._info['snap_on_start'] == 'True'
|
|
|
|
|
|
|
|
@property
|
|
|
|
def save_on_stop(self):
|
|
|
|
'''Commit changes to original volume on VM stop.'''
|
|
|
|
self._fetch_info()
|
|
|
|
return self._info['save_on_stop'] == 'True'
|
|
|
|
|
|
|
|
@property
|
|
|
|
def source(self):
|
|
|
|
'''Volume ID of source volume (for :py:attr:`snap_on_start`).
|
|
|
|
|
|
|
|
If None, this volume itself will be used.
|
|
|
|
'''
|
|
|
|
self._fetch_info()
|
|
|
|
if self._info['source']:
|
|
|
|
return self._info['source']
|
2017-04-21 02:47:23 +02:00
|
|
|
return None
|
2017-03-01 16:39:12 +01:00
|
|
|
|
|
|
|
@property
|
|
|
|
def revisions_to_keep(self):
|
|
|
|
'''Number of revisions to keep around'''
|
|
|
|
self._fetch_info()
|
|
|
|
return int(self._info['revisions_to_keep'])
|
|
|
|
|
2018-03-18 17:00:39 +01:00
|
|
|
@revisions_to_keep.setter
|
|
|
|
def revisions_to_keep(self, value):
|
|
|
|
'''Set revisions_to_keep property'''
|
|
|
|
self._qubesd_call('Set.revisions_to_keep', str(value).encode('ascii'))
|
|
|
|
self._info = None
|
|
|
|
|
2017-12-22 16:52:30 +01:00
|
|
|
def is_outdated(self):
|
|
|
|
''' Returns `True` if this snapshot of a source volume (for
|
|
|
|
`snap_on_start`=True) is outdated.
|
|
|
|
'''
|
|
|
|
self._fetch_info(True)
|
|
|
|
return self._info.get('is_outdated', False) == 'True'
|
|
|
|
|
2017-03-01 16:39:12 +01:00
|
|
|
def resize(self, size):
|
|
|
|
'''Resize volume.
|
|
|
|
|
|
|
|
Currently only extending is supported.
|
|
|
|
|
|
|
|
:param int size: new size in bytes.
|
|
|
|
'''
|
|
|
|
self._qubesd_call('Resize', str(size).encode('ascii'))
|
|
|
|
|
|
|
|
@property
|
|
|
|
def revisions(self):
|
|
|
|
''' Returns iterable containing revision identifiers'''
|
|
|
|
revisions = self._qubesd_call('ListSnapshots')
|
|
|
|
return revisions.decode('ascii').splitlines()
|
|
|
|
|
|
|
|
def revert(self, revision):
|
|
|
|
''' Revert volume to previous revision
|
|
|
|
|
|
|
|
:param str revision: Revision identifier to revert to
|
|
|
|
'''
|
|
|
|
if not isinstance(revision, str):
|
|
|
|
raise TypeError('revision must be a str')
|
|
|
|
self._qubesd_call('Revert', revision.encode('ascii'))
|
2017-03-12 21:21:16 +01:00
|
|
|
|
2017-05-25 12:40:22 +02:00
|
|
|
def import_data(self, stream):
|
|
|
|
''' Import volume data from a given file-like object.
|
|
|
|
|
|
|
|
This function override existing volume content
|
|
|
|
|
|
|
|
:param stream: file-like object, must support fileno()
|
|
|
|
'''
|
|
|
|
self._qubesd_call('Import', payload_stream=stream)
|
|
|
|
|
2017-06-19 03:02:43 +02:00
|
|
|
def clone(self, source):
|
|
|
|
''' Clone data from sane volume of another VM.
|
|
|
|
|
|
|
|
This function override existing volume content.
|
|
|
|
This operation is implemented for VM volumes - those in vm.volumes
|
|
|
|
collection (not pool.volumes).
|
|
|
|
|
|
|
|
:param source: source volume object
|
|
|
|
'''
|
|
|
|
|
|
|
|
# pylint: disable=protected-access
|
2017-06-25 21:32:39 +02:00
|
|
|
|
|
|
|
# get a token from source volume
|
|
|
|
token = source._qubesd_call('CloneFrom')
|
|
|
|
# and use it to actually clone volume data
|
|
|
|
self._qubesd_call('CloneTo', payload=token)
|
2017-06-19 03:02:43 +02:00
|
|
|
|
2017-03-12 21:21:16 +01:00
|
|
|
|
|
|
|
class Pool(object):
|
|
|
|
''' A Pool is used to manage different kind of volumes (File
|
|
|
|
based/LVM/Btrfs/...).
|
|
|
|
'''
|
|
|
|
def __init__(self, app, name=None):
|
|
|
|
''' Initialize storage pool wrapper
|
|
|
|
|
|
|
|
:param app: Qubes() object
|
|
|
|
:param name: name of the pool
|
|
|
|
'''
|
|
|
|
self.app = app
|
|
|
|
self.name = name
|
|
|
|
self._config = None
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
|
|
|
|
|
|
|
def __eq__(self, other):
|
|
|
|
if isinstance(other, Pool):
|
|
|
|
return self.name == other.name
|
2017-03-13 04:28:13 +01:00
|
|
|
elif isinstance(other, str):
|
|
|
|
return self.name == other
|
2017-03-12 21:21:16 +01:00
|
|
|
return NotImplemented
|
|
|
|
|
|
|
|
def __lt__(self, other):
|
|
|
|
if isinstance(other, Pool):
|
|
|
|
return self.name < other.name
|
|
|
|
return NotImplemented
|
|
|
|
|
|
|
|
@property
|
|
|
|
def config(self):
|
|
|
|
''' Storage pool config '''
|
|
|
|
if self._config is None:
|
|
|
|
pool_info_data = self.app.qubesd_call(
|
2017-05-12 19:36:03 +02:00
|
|
|
'dom0', 'admin.pool.Info', self.name, None)
|
2017-03-12 21:21:16 +01:00
|
|
|
pool_info_data = pool_info_data.decode('utf-8')
|
|
|
|
assert pool_info_data.endswith('\n')
|
|
|
|
pool_info_data = pool_info_data[:-1]
|
|
|
|
self._config = dict(
|
|
|
|
l.split('=', 1) for l in pool_info_data.splitlines())
|
|
|
|
return self._config
|
|
|
|
|
2018-01-17 16:21:11 +01:00
|
|
|
@property
|
|
|
|
def size(self):
|
|
|
|
''' Storage pool size, in bytes'''
|
|
|
|
return self.config['size']
|
|
|
|
|
|
|
|
@property
|
|
|
|
def usage(self):
|
|
|
|
''' Space used in the pool, in bytes '''
|
|
|
|
return self.config['usage']
|
|
|
|
|
2017-03-12 21:21:16 +01:00
|
|
|
@property
|
|
|
|
def driver(self):
|
|
|
|
''' Storage pool driver '''
|
|
|
|
return self.config['driver']
|
|
|
|
|
2018-03-18 17:00:39 +01:00
|
|
|
@property
|
|
|
|
def revisions_to_keep(self):
|
|
|
|
'''Number of revisions to keep around'''
|
|
|
|
return int(self.config['revisions_to_keep'])
|
|
|
|
|
|
|
|
@revisions_to_keep.setter
|
|
|
|
def revisions_to_keep(self, value):
|
|
|
|
'''Set revisions_to_keep property'''
|
|
|
|
self.app.qubesd_call('dom0',
|
|
|
|
'admin.pool.Set.revisions_to_keep',
|
|
|
|
self.name,
|
|
|
|
str(value).encode('ascii'))
|
|
|
|
self._config = None
|
|
|
|
|
2017-03-12 21:21:16 +01:00
|
|
|
@property
|
|
|
|
def volumes(self):
|
|
|
|
''' Volumes managed by this pool '''
|
|
|
|
volumes_data = self.app.qubesd_call(
|
2017-05-12 19:36:03 +02:00
|
|
|
'dom0', 'admin.pool.volume.List', self.name, None)
|
2017-03-12 21:21:16 +01:00
|
|
|
assert volumes_data.endswith(b'\n')
|
|
|
|
volumes_data = volumes_data[:-1].decode('ascii')
|
|
|
|
for vid in volumes_data.splitlines():
|
|
|
|
yield Volume(self.app, self.name, vid)
|