2016-07-12 18:44:05 +02:00
|
|
|
#
|
|
|
|
# The Qubes OS Project, http://www.qubes-os.org
|
|
|
|
#
|
|
|
|
# Copyright (C) 2016 Bahtiar `kalkin-` Gadimov <bahtiar@gadimov.de>
|
|
|
|
#
|
2017-10-12 00:11:50 +02:00
|
|
|
# This library 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.
|
2016-07-12 18:44:05 +02:00
|
|
|
#
|
2017-10-12 00:11:50 +02:00
|
|
|
# This library is distributed in the hope that it will be useful,
|
2016-07-12 18:44:05 +02:00
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2017-10-12 00:11:50 +02:00
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
# Lesser General Public License for more details.
|
2016-07-12 18:44:05 +02:00
|
|
|
#
|
2017-10-12 00:11:50 +02:00
|
|
|
# You should have received a copy of the GNU Lesser General Public
|
|
|
|
# License along with this library; if not, see <https://www.gnu.org/licenses/>.
|
2016-07-12 18:44:05 +02:00
|
|
|
#
|
2017-01-18 22:16:46 +01:00
|
|
|
|
2016-07-12 18:44:05 +02:00
|
|
|
''' Driver for storing vm images in a LVM thin pool '''
|
2018-10-19 02:38:47 +02:00
|
|
|
import functools
|
2016-07-12 18:44:05 +02:00
|
|
|
import logging
|
|
|
|
import os
|
|
|
|
import subprocess
|
|
|
|
|
2017-07-04 03:29:54 +02:00
|
|
|
import time
|
|
|
|
|
|
|
|
import asyncio
|
|
|
|
|
2016-07-12 18:44:05 +02:00
|
|
|
import qubes
|
2017-06-09 04:46:46 +02:00
|
|
|
import qubes.storage
|
|
|
|
import qubes.utils
|
2016-07-12 18:44:05 +02:00
|
|
|
|
|
|
|
|
2016-11-25 02:04:09 +01:00
|
|
|
def check_lvm_version():
|
|
|
|
#Check if lvm is very very old, like in Travis-CI
|
|
|
|
try:
|
|
|
|
lvm_help = subprocess.check_output(['lvm', 'lvcreate', '--help'],
|
|
|
|
stderr=subprocess.DEVNULL).decode()
|
|
|
|
return '--setactivationskip' not in lvm_help
|
2017-05-26 15:06:12 +02:00
|
|
|
except (subprocess.CalledProcessError, FileNotFoundError):
|
2016-11-25 02:04:09 +01:00
|
|
|
pass
|
|
|
|
|
|
|
|
lvm_is_very_old = check_lvm_version()
|
|
|
|
|
2018-03-14 22:54:00 +01:00
|
|
|
|
2016-07-12 18:44:05 +02:00
|
|
|
class ThinPool(qubes.storage.Pool):
|
|
|
|
''' LVM Thin based pool implementation
|
2018-03-14 22:54:00 +01:00
|
|
|
|
|
|
|
Volumes are stored as LVM thin volumes, in thin pool specified by
|
|
|
|
*volume_group*/*thin_pool* arguments. LVM volume naming scheme:
|
|
|
|
|
|
|
|
vm-{vm_name}-{volume_name}[-suffix]
|
|
|
|
|
|
|
|
Where suffix can be one of:
|
|
|
|
"-snap" - snapshot for currently running VM, at VM shutdown will be
|
|
|
|
either discarded (if save_on_stop=False), or committed
|
|
|
|
(if save_on_stop=True)
|
|
|
|
"-{revision_id}" - volume revision - new revision is automatically
|
|
|
|
created at each VM shutdown, *revisions_to_keep* control how many
|
|
|
|
old revisions (in addition to the current one) should be stored
|
|
|
|
"" (no suffix) - the most recent committed volume state; also volatile
|
|
|
|
volume (snap_on_start=False, save_on_stop=False)
|
|
|
|
|
|
|
|
On VM startup, new volume is created, depending on volume type,
|
|
|
|
according to the table below:
|
|
|
|
|
|
|
|
snap_on_start, save_on_stop
|
|
|
|
False, False, - no suffix, fresh empty volume
|
|
|
|
False, True, - "-snap", snapshot of last committed revision
|
|
|
|
True , False, - "-snap", snapshot of last committed revision
|
|
|
|
of source volume (from VM's template)
|
|
|
|
True, True, - unsupported configuration
|
|
|
|
|
|
|
|
Volume's revision_id format is "{timestamp}-back", where timestamp is in
|
|
|
|
'%s' format (seconds since unix epoch)
|
2016-07-12 18:44:05 +02:00
|
|
|
''' # pylint: disable=protected-access
|
|
|
|
|
2016-08-28 23:50:04 +02:00
|
|
|
size_cache = None
|
|
|
|
|
2016-07-12 18:44:05 +02:00
|
|
|
driver = 'lvm_thin'
|
|
|
|
|
2016-07-14 15:24:11 +02:00
|
|
|
def __init__(self, volume_group, thin_pool, revisions_to_keep=1, **kwargs):
|
|
|
|
super(ThinPool, self).__init__(revisions_to_keep=revisions_to_keep,
|
|
|
|
**kwargs)
|
2016-07-12 18:44:05 +02:00
|
|
|
self.volume_group = volume_group
|
|
|
|
self.thin_pool = thin_pool
|
|
|
|
self._pool_id = "{!s}/{!s}".format(volume_group, thin_pool)
|
2018-02-16 22:47:37 +01:00
|
|
|
self.log = logging.getLogger('qubes.storage.lvm.%s' % self._pool_id)
|
2016-07-12 18:44:05 +02:00
|
|
|
|
2017-08-12 22:44:03 +02:00
|
|
|
self._volume_objects_cache = {}
|
|
|
|
|
2018-07-15 21:30:04 +02:00
|
|
|
def __repr__(self):
|
|
|
|
return '<{} at {:#x} name={!r} volume_group={!r} thin_pool={!r}>'.\
|
|
|
|
format(
|
|
|
|
type(self).__name__, id(self),
|
|
|
|
self.name, self.volume_group, self.thin_pool)
|
|
|
|
|
2016-07-12 18:44:05 +02:00
|
|
|
@property
|
|
|
|
def config(self):
|
|
|
|
return {
|
|
|
|
'name': self.name,
|
|
|
|
'volume_group': self.volume_group,
|
|
|
|
'thin_pool': self.thin_pool,
|
2018-04-13 15:56:23 +02:00
|
|
|
'driver': ThinPool.driver,
|
|
|
|
'revisions_to_keep': self.revisions_to_keep,
|
2016-07-12 18:44:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
def destroy(self):
|
|
|
|
pass # TODO Should we remove an existing pool?
|
|
|
|
|
|
|
|
def init_volume(self, vm, volume_config):
|
|
|
|
''' Initialize a :py:class:`qubes.storage.Volume` from `volume_config`.
|
|
|
|
'''
|
|
|
|
|
2018-02-16 22:47:32 +01:00
|
|
|
if 'revisions_to_keep' not in volume_config.keys():
|
|
|
|
volume_config['revisions_to_keep'] = self.revisions_to_keep
|
2016-07-12 18:44:05 +02:00
|
|
|
if 'vid' not in volume_config.keys():
|
|
|
|
if vm and hasattr(vm, 'name'):
|
|
|
|
vm_name = vm.name
|
|
|
|
else:
|
|
|
|
# for the future if we have volumes not belonging to a vm
|
|
|
|
vm_name = qubes.utils.random_string()
|
|
|
|
|
|
|
|
assert self.name
|
|
|
|
|
2017-06-07 01:21:51 +02:00
|
|
|
volume_config['vid'] = "{!s}/vm-{!s}-{!s}".format(
|
2016-07-12 18:44:05 +02:00
|
|
|
self.volume_group, vm_name, volume_config['name'])
|
|
|
|
|
|
|
|
volume_config['volume_group'] = self.volume_group
|
2017-06-09 04:46:46 +02:00
|
|
|
volume_config['pool'] = self
|
2017-08-12 22:44:03 +02:00
|
|
|
volume = ThinVolume(**volume_config)
|
|
|
|
self._volume_objects_cache[volume_config['vid']] = volume
|
|
|
|
return volume
|
2016-07-12 18:44:05 +02:00
|
|
|
|
|
|
|
def setup(self):
|
2018-01-11 03:56:30 +01:00
|
|
|
reset_cache()
|
2018-01-12 05:12:08 +01:00
|
|
|
cache_key = self.volume_group + '/' + self.thin_pool
|
|
|
|
if cache_key not in size_cache:
|
|
|
|
raise qubes.storage.StoragePoolException(
|
|
|
|
'Thin pool {} does not exist'.format(cache_key))
|
|
|
|
if size_cache[cache_key]['attr'][0] != 't':
|
|
|
|
raise qubes.storage.StoragePoolException(
|
|
|
|
'Volume {} is not a thin pool'.format(cache_key))
|
2018-01-11 03:56:30 +01:00
|
|
|
# TODO Should we create a non existing pool?
|
2016-07-12 18:44:05 +02:00
|
|
|
|
2017-08-12 22:44:03 +02:00
|
|
|
def get_volume(self, vid):
|
|
|
|
''' Return a volume with given vid'''
|
|
|
|
if vid in self._volume_objects_cache:
|
|
|
|
return self._volume_objects_cache[vid]
|
|
|
|
|
|
|
|
config = {
|
|
|
|
'pool': self,
|
|
|
|
'vid': vid,
|
|
|
|
'name': vid,
|
|
|
|
'volume_group': self.volume_group,
|
|
|
|
}
|
|
|
|
# don't cache this object, as it doesn't carry full configuration
|
|
|
|
return ThinVolume(**config)
|
|
|
|
|
2017-06-26 02:46:49 +02:00
|
|
|
def list_volumes(self):
|
2016-07-12 18:44:05 +02:00
|
|
|
''' Return a list of volumes managed by this pool '''
|
|
|
|
volumes = []
|
2016-11-03 03:53:06 +01:00
|
|
|
for vid, vol_info in size_cache.items():
|
|
|
|
if not vid.startswith(self.volume_group + '/'):
|
|
|
|
continue
|
|
|
|
if vol_info['pool_lv'] != self.thin_pool:
|
2016-07-12 18:44:05 +02:00
|
|
|
continue
|
2018-03-15 22:11:05 +01:00
|
|
|
if vid.endswith('-snap') or vid.endswith('-import'):
|
2016-11-04 13:19:49 +01:00
|
|
|
# implementation detail volume
|
|
|
|
continue
|
2017-07-04 03:29:54 +02:00
|
|
|
if vid.endswith('-back'):
|
|
|
|
# old revisions
|
|
|
|
continue
|
2018-03-14 22:54:00 +01:00
|
|
|
volume = self.get_volume(vid)
|
|
|
|
if volume in volumes:
|
|
|
|
continue
|
|
|
|
volumes.append(volume)
|
2016-07-12 18:44:05 +02:00
|
|
|
return volumes
|
|
|
|
|
2017-10-29 02:23:00 +02:00
|
|
|
@property
|
|
|
|
def size(self):
|
|
|
|
try:
|
|
|
|
return qubes.storage.lvm.size_cache[
|
|
|
|
self.volume_group + '/' + self.thin_pool]['size']
|
|
|
|
except KeyError:
|
|
|
|
return 0
|
|
|
|
|
|
|
|
@property
|
|
|
|
def usage(self):
|
|
|
|
try:
|
|
|
|
return qubes.storage.lvm.size_cache[
|
|
|
|
self.volume_group + '/' + self.thin_pool]['usage']
|
|
|
|
except KeyError:
|
|
|
|
return 0
|
|
|
|
|
2016-08-28 23:50:04 +02:00
|
|
|
|
2018-10-19 02:38:47 +02:00
|
|
|
_init_cache_cmd = ['lvs', '--noheadings', '-o',
|
|
|
|
'vg_name,pool_lv,name,lv_size,data_percent,lv_attr,origin',
|
|
|
|
'--units', 'b', '--separator', ';']
|
2016-08-28 23:50:04 +02:00
|
|
|
|
2018-10-19 02:38:47 +02:00
|
|
|
def _parse_lvm_cache(lvm_output):
|
2016-08-28 23:50:04 +02:00
|
|
|
result = {}
|
|
|
|
|
2018-10-19 02:38:47 +02:00
|
|
|
for line in lvm_output.splitlines():
|
2017-01-26 18:26:06 +01:00
|
|
|
line = line.decode().strip()
|
2017-07-04 03:29:54 +02:00
|
|
|
pool_name, pool_lv, name, size, usage_percent, attr, \
|
2017-09-13 18:50:02 +02:00
|
|
|
origin = line.split(';', 6)
|
2017-10-29 02:23:00 +02:00
|
|
|
if '' in [pool_name, name, size, usage_percent]:
|
2016-08-28 23:50:04 +02:00
|
|
|
continue
|
|
|
|
name = pool_name + "/" + name
|
2017-09-12 01:57:49 +02:00
|
|
|
size = int(size[:-1]) # Remove 'B' suffix
|
2016-08-28 23:50:04 +02:00
|
|
|
usage = int(size / 100 * float(usage_percent))
|
2016-11-03 03:53:06 +01:00
|
|
|
result[name] = {'size': size, 'usage': usage, 'pool_lv': pool_lv,
|
2017-07-04 03:29:54 +02:00
|
|
|
'attr': attr, 'origin': origin}
|
2016-08-28 23:50:04 +02:00
|
|
|
|
|
|
|
return result
|
|
|
|
|
2018-10-19 02:38:47 +02:00
|
|
|
def init_cache(log=logging.getLogger('qubes.storage.lvm')):
|
|
|
|
cmd = _init_cache_cmd
|
|
|
|
if os.getuid() != 0:
|
|
|
|
cmd.insert(0, 'sudo')
|
|
|
|
environ = os.environ.copy()
|
|
|
|
environ['LC_ALL'] = 'C.utf8'
|
|
|
|
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
|
|
|
|
close_fds=True, env=environ)
|
|
|
|
out, err = p.communicate()
|
|
|
|
return_code = p.returncode
|
|
|
|
if return_code == 0 and err:
|
|
|
|
log.warning(err)
|
|
|
|
elif return_code != 0:
|
|
|
|
raise qubes.storage.StoragePoolException(err)
|
|
|
|
|
|
|
|
return _parse_lvm_cache(out)
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def init_cache_coro(log=logging.getLogger('qubes.storage.lvm')):
|
|
|
|
cmd = _init_cache_cmd
|
|
|
|
if os.getuid() != 0:
|
|
|
|
cmd = ['sudo'] + cmd
|
|
|
|
environ = os.environ.copy()
|
|
|
|
environ['LC_ALL'] = 'C.utf8'
|
|
|
|
p = yield from asyncio.create_subprocess_exec(*cmd,
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
stderr=subprocess.PIPE,
|
|
|
|
close_fds=True, env=environ)
|
|
|
|
out, err = yield from p.communicate()
|
|
|
|
return_code = p.returncode
|
|
|
|
if return_code == 0 and err:
|
|
|
|
log.warning(err)
|
|
|
|
elif return_code != 0:
|
|
|
|
raise qubes.storage.StoragePoolException(err)
|
|
|
|
|
|
|
|
return _parse_lvm_cache(out)
|
2016-08-28 23:50:04 +02:00
|
|
|
|
|
|
|
size_cache = init_cache()
|
|
|
|
|
2018-03-14 22:54:00 +01:00
|
|
|
|
|
|
|
def _revision_sort_key(revision):
|
|
|
|
'''Sort key for revisions. Sort them by time
|
|
|
|
|
|
|
|
:returns timestamp
|
|
|
|
'''
|
|
|
|
if isinstance(revision, tuple):
|
|
|
|
revision = revision[0]
|
|
|
|
if '-' in revision:
|
|
|
|
revision = revision.split('-')[0]
|
|
|
|
return int(revision)
|
|
|
|
|
2018-10-19 02:38:47 +02:00
|
|
|
def locked(method):
|
|
|
|
'''Decorator running given Volume's coroutine under a lock.
|
|
|
|
Needs to be added after wrapping with @asyncio.coroutine, for example:
|
|
|
|
|
|
|
|
>>>@locked
|
|
|
|
>>>@asyncio.coroutine
|
|
|
|
>>>def start(self):
|
|
|
|
>>> pass
|
|
|
|
'''
|
|
|
|
@asyncio.coroutine
|
|
|
|
@functools.wraps(method)
|
|
|
|
def wrapper(self, *args, **kwargs):
|
|
|
|
with (yield from self._lock): # pylint: disable=protected-access
|
|
|
|
return (yield from method(self, *args, **kwargs))
|
|
|
|
return wrapper
|
2018-03-14 22:54:00 +01:00
|
|
|
|
2016-07-12 18:44:05 +02:00
|
|
|
class ThinVolume(qubes.storage.Volume):
|
|
|
|
''' Default LVM thin volume implementation
|
|
|
|
''' # pylint: disable=too-few-public-methods
|
|
|
|
|
2016-08-28 23:50:04 +02:00
|
|
|
|
2016-09-02 19:45:31 +02:00
|
|
|
def __init__(self, volume_group, size=0, **kwargs):
|
2016-07-12 18:44:05 +02:00
|
|
|
self.volume_group = volume_group
|
2016-09-02 19:45:31 +02:00
|
|
|
super(ThinVolume, self).__init__(size=size, **kwargs)
|
2018-02-16 22:47:37 +01:00
|
|
|
self.log = logging.getLogger('qubes.storage.lvm.%s' % str(self.pool))
|
2016-07-12 18:44:05 +02:00
|
|
|
|
2017-07-04 03:29:54 +02:00
|
|
|
if self.snap_on_start or self.save_on_stop:
|
2016-07-12 18:44:05 +02:00
|
|
|
self._vid_snap = self.vid + '-snap'
|
2018-03-15 22:11:05 +01:00
|
|
|
if self.save_on_stop:
|
|
|
|
self._vid_import = self.vid + '-import'
|
2016-07-12 18:44:05 +02:00
|
|
|
|
2016-09-02 19:45:31 +02:00
|
|
|
self._size = size
|
2018-10-19 02:38:47 +02:00
|
|
|
self._lock = asyncio.Lock()
|
2016-09-02 19:45:31 +02:00
|
|
|
|
2017-06-07 00:20:41 +02:00
|
|
|
@property
|
|
|
|
def path(self):
|
2018-03-14 22:54:00 +01:00
|
|
|
return '/dev/' + self._vid_current
|
|
|
|
|
|
|
|
@property
|
|
|
|
def _vid_current(self):
|
|
|
|
if self.vid in size_cache:
|
|
|
|
return self.vid
|
|
|
|
vol_revisions = self.revisions
|
|
|
|
if vol_revisions:
|
|
|
|
last_revision = \
|
|
|
|
max(vol_revisions.items(), key=_revision_sort_key)[0]
|
|
|
|
return self.vid + '-' + last_revision
|
|
|
|
# detached pool? return expected path
|
|
|
|
return self.vid
|
2017-06-07 00:20:41 +02:00
|
|
|
|
2016-07-12 18:44:05 +02:00
|
|
|
@property
|
|
|
|
def revisions(self):
|
2017-07-04 03:29:54 +02:00
|
|
|
name_prefix = self.vid + '-'
|
|
|
|
revisions = {}
|
|
|
|
for revision_vid in size_cache:
|
|
|
|
if not revision_vid.startswith(name_prefix):
|
|
|
|
continue
|
|
|
|
if not revision_vid.endswith('-back'):
|
|
|
|
continue
|
|
|
|
revision_vid = revision_vid[len(name_prefix):]
|
2018-03-14 22:54:00 +01:00
|
|
|
# get revision without suffix
|
|
|
|
seconds = int(revision_vid.split('-')[0])
|
2016-07-14 15:24:11 +02:00
|
|
|
iso_date = qubes.storage.isodate(seconds).split('.', 1)[0]
|
2017-07-04 03:29:54 +02:00
|
|
|
revisions[revision_vid] = iso_date
|
|
|
|
return revisions
|
2016-07-12 18:44:05 +02:00
|
|
|
|
2016-08-28 23:50:04 +02:00
|
|
|
@property
|
|
|
|
def size(self):
|
|
|
|
try:
|
2018-02-07 00:19:07 +01:00
|
|
|
if self.is_dirty():
|
|
|
|
return qubes.storage.lvm.size_cache[self._vid_snap]['size']
|
2018-03-14 22:54:00 +01:00
|
|
|
return qubes.storage.lvm.size_cache[self._vid_current]['size']
|
2016-08-28 23:50:04 +02:00
|
|
|
except KeyError:
|
|
|
|
return self._size
|
|
|
|
|
2016-09-04 23:49:42 +02:00
|
|
|
@size.setter
|
|
|
|
def size(self, _):
|
|
|
|
raise qubes.storage.StoragePoolException(
|
|
|
|
"You shouldn't use lvm size setter")
|
|
|
|
|
2018-10-19 02:38:47 +02:00
|
|
|
@asyncio.coroutine
|
2017-06-09 04:46:46 +02:00
|
|
|
def _reset(self):
|
|
|
|
''' Resets a volatile volume '''
|
2017-07-04 03:29:54 +02:00
|
|
|
assert not self.snap_on_start and not self.save_on_stop, \
|
|
|
|
"Not a volatile volume"
|
2017-12-21 18:19:10 +01:00
|
|
|
self.log.debug('Resetting volatile %s', self.vid)
|
2017-06-09 04:46:46 +02:00
|
|
|
try:
|
|
|
|
cmd = ['remove', self.vid]
|
2018-10-19 02:38:47 +02:00
|
|
|
yield from qubes_lvm_coro(cmd, self.log)
|
2017-06-09 04:46:46 +02:00
|
|
|
except qubes.storage.StoragePoolException:
|
|
|
|
pass
|
|
|
|
# pylint: disable=protected-access
|
|
|
|
cmd = ['create', self.pool._pool_id, self.vid.split('/')[1],
|
|
|
|
str(self.size)]
|
2018-10-19 02:38:47 +02:00
|
|
|
yield from qubes_lvm_coro(cmd, self.log)
|
2017-06-09 04:46:46 +02:00
|
|
|
|
2018-10-19 02:38:47 +02:00
|
|
|
@asyncio.coroutine
|
2017-07-04 03:29:54 +02:00
|
|
|
def _remove_revisions(self, revisions=None):
|
|
|
|
'''Remove old volume revisions.
|
|
|
|
|
|
|
|
If no revisions list is given, it removes old revisions according to
|
|
|
|
:py:attr:`revisions_to_keep`
|
|
|
|
|
|
|
|
:param revisions: list of revisions to remove
|
|
|
|
'''
|
|
|
|
if revisions is None:
|
|
|
|
revisions = sorted(self.revisions.items(),
|
2018-03-14 22:54:00 +01:00
|
|
|
key=_revision_sort_key)
|
2018-02-16 22:47:36 +01:00
|
|
|
# pylint: disable=invalid-unary-operand-type
|
|
|
|
revisions = revisions[:(-self.revisions_to_keep) or None]
|
2017-07-04 03:29:54 +02:00
|
|
|
revisions = [rev_id for rev_id, _ in revisions]
|
|
|
|
|
|
|
|
for rev_id in revisions:
|
2018-03-14 22:54:00 +01:00
|
|
|
# safety check
|
|
|
|
assert rev_id != self._vid_current
|
2017-07-04 03:29:54 +02:00
|
|
|
try:
|
2018-02-16 22:47:33 +01:00
|
|
|
cmd = ['remove', self.vid + '-' + rev_id]
|
2018-10-19 02:38:47 +02:00
|
|
|
yield from qubes_lvm_coro(cmd, self.log)
|
2017-07-04 03:29:54 +02:00
|
|
|
except qubes.storage.StoragePoolException:
|
|
|
|
pass
|
|
|
|
|
2018-10-19 02:38:47 +02:00
|
|
|
@asyncio.coroutine
|
2018-03-14 22:54:00 +01:00
|
|
|
def _commit(self, vid_to_commit=None, keep=False):
|
|
|
|
'''
|
|
|
|
Commit temporary volume into current one. By default
|
|
|
|
:py:attr:`_vid_snap` is used (which is created by :py:meth:`start()`),
|
|
|
|
but can be overriden by *vid_to_commit* argument.
|
|
|
|
|
|
|
|
:param vid_to_commit: LVM volume ID to commit into this one
|
|
|
|
:param keep: whether to keep or not *vid_to_commit*.
|
|
|
|
IOW use 'clone' or 'rename' methods.
|
|
|
|
:return: None
|
|
|
|
'''
|
2017-06-09 04:46:46 +02:00
|
|
|
msg = "Trying to commit {!s}, but it has save_on_stop == False"
|
|
|
|
msg = msg.format(self)
|
|
|
|
assert self.save_on_stop, msg
|
|
|
|
|
|
|
|
msg = "Trying to commit {!s}, but it has rw == False"
|
|
|
|
msg = msg.format(self)
|
|
|
|
assert self.rw, msg
|
2018-03-14 22:54:00 +01:00
|
|
|
if vid_to_commit is None:
|
|
|
|
assert hasattr(self, '_vid_snap')
|
|
|
|
vid_to_commit = self._vid_snap
|
2017-06-09 04:46:46 +02:00
|
|
|
|
2018-10-19 02:38:47 +02:00
|
|
|
assert self._lock.locked()
|
2018-03-14 22:54:00 +01:00
|
|
|
if not os.path.exists('/dev/' + vid_to_commit):
|
|
|
|
# nothing to commit
|
|
|
|
return
|
|
|
|
|
|
|
|
if self._vid_current == self.vid:
|
|
|
|
cmd = ['rename', self.vid,
|
|
|
|
'{}-{}-back'.format(self.vid, int(time.time()))]
|
2018-10-19 02:38:47 +02:00
|
|
|
yield from qubes_lvm_coro(cmd, self.log)
|
|
|
|
yield from reset_cache_coro()
|
2017-06-09 04:46:46 +02:00
|
|
|
|
2018-03-14 22:54:00 +01:00
|
|
|
cmd = ['clone' if keep else 'rename',
|
|
|
|
vid_to_commit,
|
|
|
|
self.vid]
|
2018-10-19 02:38:47 +02:00
|
|
|
yield from qubes_lvm_coro(cmd, self.log)
|
|
|
|
yield from reset_cache_coro()
|
2018-03-14 22:54:00 +01:00
|
|
|
# make sure the one we've committed right now is properly
|
|
|
|
# detected as the current one - before removing anything
|
|
|
|
assert self._vid_current == self.vid
|
2017-06-09 04:46:46 +02:00
|
|
|
|
2018-03-14 22:54:00 +01:00
|
|
|
# and remove old snapshots, if needed
|
2018-10-19 02:38:47 +02:00
|
|
|
yield from self._remove_revisions()
|
2017-06-09 04:46:46 +02:00
|
|
|
|
2018-10-19 02:38:47 +02:00
|
|
|
@locked
|
|
|
|
@asyncio.coroutine
|
2017-06-09 04:46:46 +02:00
|
|
|
def create(self):
|
|
|
|
assert self.vid
|
|
|
|
assert self.size
|
|
|
|
if self.save_on_stop:
|
|
|
|
if self.source:
|
2018-03-14 22:54:00 +01:00
|
|
|
cmd = ['clone', self.source.path, self.vid]
|
2017-06-09 04:46:46 +02:00
|
|
|
else:
|
|
|
|
cmd = [
|
|
|
|
'create',
|
|
|
|
self.pool._pool_id, # pylint: disable=protected-access
|
|
|
|
self.vid.split('/', 1)[1],
|
|
|
|
str(self.size)
|
|
|
|
]
|
2018-10-19 02:38:47 +02:00
|
|
|
yield from qubes_lvm_coro(cmd, self.log)
|
|
|
|
yield from reset_cache_coro()
|
2017-06-09 04:46:46 +02:00
|
|
|
return self
|
|
|
|
|
2018-10-19 02:38:47 +02:00
|
|
|
@locked
|
|
|
|
@asyncio.coroutine
|
2017-06-26 11:39:02 +02:00
|
|
|
def remove(self):
|
|
|
|
assert self.vid
|
2017-12-14 21:57:59 +01:00
|
|
|
try:
|
|
|
|
if os.path.exists('/dev/' + self._vid_snap):
|
|
|
|
cmd = ['remove', self._vid_snap]
|
2018-10-19 02:38:47 +02:00
|
|
|
yield from qubes_lvm_coro(cmd, self.log)
|
2017-12-14 21:57:59 +01:00
|
|
|
except AttributeError:
|
|
|
|
pass
|
2017-06-26 11:39:02 +02:00
|
|
|
|
2018-03-15 22:11:05 +01:00
|
|
|
try:
|
|
|
|
if os.path.exists('/dev/' + self._vid_import):
|
|
|
|
cmd = ['remove', self._vid_import]
|
2018-10-19 02:38:47 +02:00
|
|
|
yield from qubes_lvm_coro(cmd, self.log)
|
2018-03-15 22:11:05 +01:00
|
|
|
except AttributeError:
|
|
|
|
pass
|
|
|
|
|
2018-10-19 02:38:47 +02:00
|
|
|
yield from self._remove_revisions(self.revisions.keys())
|
2017-06-26 11:39:02 +02:00
|
|
|
if not os.path.exists(self.path):
|
|
|
|
return
|
2018-03-14 22:54:00 +01:00
|
|
|
cmd = ['remove', self.path]
|
2018-10-19 02:38:47 +02:00
|
|
|
yield from qubes_lvm_coro(cmd, self.log)
|
|
|
|
yield from reset_cache_coro()
|
2017-08-12 22:44:03 +02:00
|
|
|
# pylint: disable=protected-access
|
|
|
|
self.pool._volume_objects_cache.pop(self.vid, None)
|
2017-06-26 11:39:02 +02:00
|
|
|
|
2017-06-09 04:46:46 +02:00
|
|
|
def export(self):
|
|
|
|
''' Returns an object that can be `open()`. '''
|
2017-07-29 04:42:19 +02:00
|
|
|
# make sure the device node is available
|
2018-03-14 22:54:00 +01:00
|
|
|
qubes_lvm(['activate', self.path], self.log)
|
|
|
|
devpath = self.path
|
2017-06-09 04:46:46 +02:00
|
|
|
return devpath
|
|
|
|
|
2018-10-19 02:38:47 +02:00
|
|
|
@locked
|
2017-07-04 03:29:54 +02:00
|
|
|
@asyncio.coroutine
|
2017-06-09 04:46:46 +02:00
|
|
|
def import_volume(self, src_volume):
|
|
|
|
if not src_volume.save_on_stop:
|
|
|
|
return self
|
|
|
|
|
2018-03-14 22:54:00 +01:00
|
|
|
if self.is_dirty():
|
|
|
|
raise qubes.storage.StoragePoolException(
|
|
|
|
'Cannot import to dirty volume {} -'
|
|
|
|
' start and stop a qube to cleanup'.format(self.vid))
|
2018-03-15 22:11:05 +01:00
|
|
|
self.abort_if_import_in_progress()
|
2017-06-09 04:46:46 +02:00
|
|
|
# HACK: neat trick to speed up testing if you have same physical thin
|
|
|
|
# pool assigned to two qubes-pools i.e: qubes_dom0 and test-lvm
|
|
|
|
# pylint: disable=line-too-long
|
|
|
|
if isinstance(src_volume.pool, ThinPool) and \
|
|
|
|
src_volume.pool.thin_pool == self.pool.thin_pool: # NOQA
|
2018-10-19 02:38:47 +02:00
|
|
|
yield from self._commit(src_volume.path[len('/dev/'):], keep=True)
|
2017-06-09 04:46:46 +02:00
|
|
|
else:
|
2018-03-15 22:11:05 +01:00
|
|
|
cmd = ['create',
|
|
|
|
self.pool._pool_id, # pylint: disable=protected-access
|
|
|
|
self._vid_import.split('/')[1],
|
2018-03-14 22:54:00 +01:00
|
|
|
str(src_volume.size)]
|
2018-10-19 02:38:47 +02:00
|
|
|
yield from qubes_lvm_coro(cmd, self.log)
|
2017-06-26 11:42:24 +02:00
|
|
|
src_path = src_volume.export()
|
2018-03-15 22:11:05 +01:00
|
|
|
cmd = ['dd', 'if=' + src_path, 'of=/dev/' + self._vid_import,
|
|
|
|
'conv=sparse', 'status=none']
|
|
|
|
if not os.access('/dev/' + self._vid_import, os.W_OK) or \
|
|
|
|
not os.access(src_path, os.R_OK):
|
|
|
|
cmd.insert(0, 'sudo')
|
|
|
|
|
2017-07-04 03:29:54 +02:00
|
|
|
p = yield from asyncio.create_subprocess_exec(*cmd)
|
|
|
|
yield from p.wait()
|
|
|
|
if p.returncode != 0:
|
2018-03-15 22:11:05 +01:00
|
|
|
cmd = ['remove', self._vid_import]
|
2018-10-19 02:38:47 +02:00
|
|
|
yield from qubes_lvm_coro(cmd, self.log)
|
2017-07-04 03:29:54 +02:00
|
|
|
raise qubes.storage.StoragePoolException(
|
|
|
|
'Failed to import volume {!r}, dd exit code: {}'.format(
|
|
|
|
src_volume, p.returncode))
|
2018-10-19 02:38:47 +02:00
|
|
|
yield from self._commit(self._vid_import)
|
2017-06-09 04:46:46 +02:00
|
|
|
|
|
|
|
return self
|
|
|
|
|
2018-10-19 02:38:47 +02:00
|
|
|
@locked
|
|
|
|
@asyncio.coroutine
|
2017-06-09 04:46:46 +02:00
|
|
|
def import_data(self):
|
|
|
|
''' Returns an object that can be `open()`. '''
|
2018-03-15 22:11:05 +01:00
|
|
|
if self.is_dirty():
|
|
|
|
raise qubes.storage.StoragePoolException(
|
|
|
|
'Cannot import data to dirty volume {}, stop the qube first'.
|
|
|
|
format(self.vid))
|
|
|
|
self.abort_if_import_in_progress()
|
|
|
|
# pylint: disable=protected-access
|
|
|
|
cmd = ['create', self.pool._pool_id, self._vid_import.split('/')[1],
|
|
|
|
str(self.size)]
|
2018-10-19 02:38:47 +02:00
|
|
|
yield from qubes_lvm_coro(cmd, self.log)
|
|
|
|
yield from reset_cache_coro()
|
2018-03-15 22:11:05 +01:00
|
|
|
devpath = '/dev/' + self._vid_import
|
2017-06-09 04:46:46 +02:00
|
|
|
return devpath
|
|
|
|
|
2018-10-19 02:38:47 +02:00
|
|
|
@locked
|
|
|
|
@asyncio.coroutine
|
2018-03-15 22:11:05 +01:00
|
|
|
def import_data_end(self, success):
|
|
|
|
'''Either commit imported data, or discard temporary volume'''
|
|
|
|
if not os.path.exists('/dev/' + self._vid_import):
|
|
|
|
raise qubes.storage.StoragePoolException(
|
|
|
|
'No import operation in progress on {}'.format(self.vid))
|
|
|
|
if success:
|
2018-10-19 02:38:47 +02:00
|
|
|
yield from self._commit(self._vid_import)
|
2018-03-15 22:11:05 +01:00
|
|
|
else:
|
|
|
|
cmd = ['remove', self._vid_import]
|
2018-10-19 02:38:47 +02:00
|
|
|
yield from qubes_lvm_coro(cmd, self.log)
|
2018-03-15 22:11:05 +01:00
|
|
|
|
|
|
|
def abort_if_import_in_progress(self):
|
|
|
|
try:
|
|
|
|
devpath = '/dev/' + self._vid_import
|
|
|
|
if os.path.exists(devpath):
|
|
|
|
raise qubes.storage.StoragePoolException(
|
|
|
|
'Import operation in progress on {}'.format(self.vid))
|
|
|
|
except AttributeError: # self._vid_import
|
|
|
|
# no vid_import - import definitely not in progress
|
|
|
|
pass
|
|
|
|
|
2017-06-09 04:46:46 +02:00
|
|
|
def is_dirty(self):
|
|
|
|
if self.save_on_stop:
|
2017-07-04 03:29:54 +02:00
|
|
|
return os.path.exists('/dev/' + self._vid_snap)
|
2017-06-09 04:46:46 +02:00
|
|
|
return False
|
|
|
|
|
2017-07-04 03:29:54 +02:00
|
|
|
def is_outdated(self):
|
|
|
|
if not self.snap_on_start:
|
|
|
|
return False
|
|
|
|
if self._vid_snap not in size_cache:
|
|
|
|
return False
|
|
|
|
return (size_cache[self._vid_snap]['origin'] !=
|
2018-03-14 22:54:00 +01:00
|
|
|
self.source.path.split('/')[-1])
|
2017-07-04 03:29:54 +02:00
|
|
|
|
2018-10-19 02:38:47 +02:00
|
|
|
@locked
|
|
|
|
@asyncio.coroutine
|
2017-06-09 04:46:46 +02:00
|
|
|
def revert(self, revision=None):
|
2018-03-14 22:54:00 +01:00
|
|
|
if self.is_dirty():
|
|
|
|
raise qubes.storage.StoragePoolException(
|
|
|
|
'Cannot revert dirty volume {}, stop the qube first'.format(
|
|
|
|
self.vid))
|
2018-03-15 22:11:05 +01:00
|
|
|
self.abort_if_import_in_progress()
|
2017-07-04 03:29:54 +02:00
|
|
|
if revision is None:
|
|
|
|
revision = \
|
2018-03-14 22:54:00 +01:00
|
|
|
max(self.revisions.items(), key=_revision_sort_key)[0]
|
|
|
|
old_path = '/dev/' + self.vid + '-' + revision
|
2017-06-09 04:46:46 +02:00
|
|
|
if not os.path.exists(old_path):
|
|
|
|
msg = "Volume {!s} has no {!s}".format(self, old_path)
|
|
|
|
raise qubes.storage.StoragePoolException(msg)
|
|
|
|
|
2018-03-14 22:54:00 +01:00
|
|
|
if self.vid in size_cache:
|
|
|
|
cmd = ['remove', self.vid]
|
2018-10-19 02:38:47 +02:00
|
|
|
yield from qubes_lvm_coro(cmd, self.log)
|
2017-07-04 03:29:54 +02:00
|
|
|
cmd = ['clone', self.vid + '-' + revision, self.vid]
|
2018-10-19 02:38:47 +02:00
|
|
|
yield from qubes_lvm_coro(cmd, self.log)
|
|
|
|
yield from reset_cache_coro()
|
2017-06-09 04:46:46 +02:00
|
|
|
return self
|
|
|
|
|
2018-10-19 02:38:47 +02:00
|
|
|
@locked
|
|
|
|
@asyncio.coroutine
|
2017-06-09 04:46:46 +02:00
|
|
|
def resize(self, size):
|
|
|
|
''' Expands volume, throws
|
|
|
|
:py:class:`qubst.storage.qubes.storage.StoragePoolException` if
|
|
|
|
given size is less than current_size
|
|
|
|
'''
|
|
|
|
if not self.rw:
|
|
|
|
msg = 'Can not resize reađonly volume {!s}'.format(self)
|
|
|
|
raise qubes.storage.StoragePoolException(msg)
|
|
|
|
|
2017-07-17 23:28:29 +02:00
|
|
|
if size < self.size:
|
2017-06-09 04:46:46 +02:00
|
|
|
raise qubes.storage.StoragePoolException(
|
|
|
|
'For your own safety, shrinking of %s is'
|
2018-02-07 01:45:53 +01:00
|
|
|
' disabled (%d < %d). If you really know what you'
|
2017-06-09 04:46:46 +02:00
|
|
|
' are doing, use `lvresize` on %s manually.' %
|
2018-02-07 01:45:53 +01:00
|
|
|
(self.name, size, self.size, self.vid))
|
2017-06-09 04:46:46 +02:00
|
|
|
|
2017-10-08 18:37:25 +02:00
|
|
|
if size == self.size:
|
|
|
|
return
|
|
|
|
|
2017-08-06 12:47:58 +02:00
|
|
|
if self.is_dirty():
|
|
|
|
cmd = ['extend', self._vid_snap, str(size)]
|
2018-10-19 02:38:47 +02:00
|
|
|
yield from qubes_lvm_coro(cmd, self.log)
|
2018-03-15 22:11:05 +01:00
|
|
|
elif hasattr(self, '_vid_import') and \
|
|
|
|
os.path.exists('/dev/' + self._vid_import):
|
|
|
|
cmd = ['extend', self._vid_import, str(size)]
|
2018-10-19 02:38:47 +02:00
|
|
|
yield from qubes_lvm_coro(cmd, self.log)
|
2018-02-07 00:19:07 +01:00
|
|
|
elif self.save_on_stop or not self.snap_on_start:
|
2018-03-14 22:54:00 +01:00
|
|
|
cmd = ['extend', self._vid_current, str(size)]
|
2018-10-19 02:38:47 +02:00
|
|
|
yield from qubes_lvm_coro(cmd, self.log)
|
|
|
|
yield from reset_cache_coro()
|
2017-06-09 04:46:46 +02:00
|
|
|
|
2018-10-19 02:38:47 +02:00
|
|
|
@asyncio.coroutine
|
2017-06-09 04:46:46 +02:00
|
|
|
def _snapshot(self):
|
|
|
|
try:
|
|
|
|
cmd = ['remove', self._vid_snap]
|
2018-10-19 02:38:47 +02:00
|
|
|
yield from qubes_lvm_coro(cmd, self.log)
|
2017-06-09 04:46:46 +02:00
|
|
|
except: # pylint: disable=bare-except
|
|
|
|
pass
|
|
|
|
|
|
|
|
if self.source is None:
|
2018-03-14 22:54:00 +01:00
|
|
|
cmd = ['clone', self._vid_current, self._vid_snap]
|
2017-06-09 04:46:46 +02:00
|
|
|
else:
|
2018-03-14 22:54:00 +01:00
|
|
|
cmd = ['clone', self.source.path, self._vid_snap]
|
2018-10-19 02:38:47 +02:00
|
|
|
yield from qubes_lvm_coro(cmd, self.log)
|
2017-06-09 04:46:46 +02:00
|
|
|
|
2018-10-19 02:38:47 +02:00
|
|
|
@locked
|
|
|
|
@asyncio.coroutine
|
2017-06-09 04:46:46 +02:00
|
|
|
def start(self):
|
2018-03-15 22:11:05 +01:00
|
|
|
self.abort_if_import_in_progress()
|
2017-10-16 00:44:52 +02:00
|
|
|
try:
|
|
|
|
if self.snap_on_start or self.save_on_stop:
|
|
|
|
if not self.save_on_stop or not self.is_dirty():
|
2018-10-19 02:38:47 +02:00
|
|
|
yield from self._snapshot()
|
2017-10-16 00:44:52 +02:00
|
|
|
else:
|
2018-10-19 02:38:47 +02:00
|
|
|
yield from self._reset()
|
2017-10-16 00:44:52 +02:00
|
|
|
finally:
|
2018-10-19 02:38:47 +02:00
|
|
|
yield from reset_cache_coro()
|
2017-06-09 04:46:46 +02:00
|
|
|
return self
|
|
|
|
|
2018-10-19 02:38:47 +02:00
|
|
|
@locked
|
|
|
|
@asyncio.coroutine
|
2017-06-09 04:46:46 +02:00
|
|
|
def stop(self):
|
2017-10-16 00:44:52 +02:00
|
|
|
try:
|
|
|
|
if self.save_on_stop:
|
2018-10-19 02:38:47 +02:00
|
|
|
yield from self._commit()
|
2018-03-14 22:54:00 +01:00
|
|
|
if self.snap_on_start and not self.save_on_stop:
|
2017-10-16 00:44:52 +02:00
|
|
|
cmd = ['remove', self._vid_snap]
|
2018-10-19 02:38:47 +02:00
|
|
|
yield from qubes_lvm_coro(cmd, self.log)
|
2018-03-14 22:54:00 +01:00
|
|
|
elif not self.snap_on_start and not self.save_on_stop:
|
2017-10-16 00:44:52 +02:00
|
|
|
cmd = ['remove', self.vid]
|
2018-10-19 02:38:47 +02:00
|
|
|
yield from qubes_lvm_coro(cmd, self.log)
|
2017-10-16 00:44:52 +02:00
|
|
|
finally:
|
2018-10-19 02:38:47 +02:00
|
|
|
yield from reset_cache_coro()
|
2017-06-09 04:46:46 +02:00
|
|
|
return self
|
|
|
|
|
|
|
|
def verify(self):
|
|
|
|
''' Verifies the volume. '''
|
2017-10-16 00:43:10 +02:00
|
|
|
if not self.save_on_stop and not self.snap_on_start:
|
|
|
|
# volatile volumes don't need any files
|
|
|
|
return True
|
|
|
|
if self.source is not None:
|
2018-03-14 22:54:00 +01:00
|
|
|
vid = self.source.path[len('/dev/'):]
|
2017-10-16 00:43:10 +02:00
|
|
|
else:
|
2018-03-14 22:54:00 +01:00
|
|
|
vid = self._vid_current
|
2017-06-09 04:46:46 +02:00
|
|
|
try:
|
2017-10-16 00:43:10 +02:00
|
|
|
vol_info = size_cache[vid]
|
|
|
|
if vol_info['attr'][4] != 'a':
|
|
|
|
raise qubes.storage.StoragePoolException(
|
|
|
|
'volume {} not active'.format(vid))
|
2017-06-09 04:46:46 +02:00
|
|
|
except KeyError:
|
2017-10-16 00:43:10 +02:00
|
|
|
raise qubes.storage.StoragePoolException(
|
|
|
|
'volume {} missing'.format(vid))
|
2017-12-21 18:19:10 +01:00
|
|
|
return True
|
2017-06-09 04:46:46 +02:00
|
|
|
|
|
|
|
|
2016-11-03 21:41:48 +01:00
|
|
|
def block_device(self):
|
2017-04-15 16:29:50 +02:00
|
|
|
''' Return :py:class:`qubes.storage.BlockDevice` for serialization in
|
2016-11-03 21:41:48 +01:00
|
|
|
the libvirt XML template as <disk>.
|
|
|
|
'''
|
2017-07-04 03:29:54 +02:00
|
|
|
if self.snap_on_start or self.save_on_stop:
|
2017-04-15 16:29:50 +02:00
|
|
|
return qubes.storage.BlockDevice(
|
2016-11-03 21:41:48 +01:00
|
|
|
'/dev/' + self._vid_snap, self.name, self.script,
|
|
|
|
self.rw, self.domain, self.devtype)
|
2017-04-15 20:04:38 +02:00
|
|
|
|
|
|
|
return super(ThinVolume, self).block_device()
|
2016-09-04 23:49:42 +02:00
|
|
|
|
2016-08-28 23:50:04 +02:00
|
|
|
@property
|
|
|
|
def usage(self): # lvm thin usage always returns at least the same usage as
|
|
|
|
# the parent
|
|
|
|
try:
|
2018-03-14 22:54:00 +01:00
|
|
|
return qubes.storage.lvm.size_cache[self._vid_current]['usage']
|
2016-08-28 23:50:04 +02:00
|
|
|
except KeyError:
|
|
|
|
return 0
|
|
|
|
|
|
|
|
|
2016-07-12 18:44:05 +02:00
|
|
|
def pool_exists(pool_id):
|
|
|
|
''' Return true if pool exists '''
|
2016-11-03 03:53:06 +01:00
|
|
|
try:
|
|
|
|
vol_info = size_cache[pool_id]
|
|
|
|
return vol_info['attr'][0] == 't'
|
|
|
|
except KeyError:
|
|
|
|
return False
|
2016-07-12 18:44:05 +02:00
|
|
|
|
2018-10-19 02:38:47 +02:00
|
|
|
def _get_lvm_cmdline(cmd):
|
|
|
|
''' Build command line for :program:`lvm` call.
|
|
|
|
The purpose of this function is to keep all the detailed lvm options in
|
|
|
|
one place.
|
2016-07-12 18:44:05 +02:00
|
|
|
|
2018-10-19 02:38:47 +02:00
|
|
|
:param cmd: array of str, where cmd[0] is action and the rest are arguments
|
|
|
|
:return array of str appropriate for subprocess.Popen
|
|
|
|
'''
|
2016-11-03 03:53:06 +01:00
|
|
|
action = cmd[0]
|
|
|
|
if action == 'remove':
|
|
|
|
lvm_cmd = ['lvremove', '-f', cmd[1]]
|
|
|
|
elif action == 'clone':
|
|
|
|
lvm_cmd = ['lvcreate', '-kn', '-ay', '-s', cmd[1], '-n', cmd[2]]
|
|
|
|
elif action == 'create':
|
|
|
|
lvm_cmd = ['lvcreate', '-T', cmd[1], '-kn', '-ay', '-n', cmd[2], '-V',
|
|
|
|
str(cmd[3]) + 'B']
|
|
|
|
elif action == 'extend':
|
2017-10-02 18:23:41 +02:00
|
|
|
size = int(cmd[2]) / (1024 * 1024)
|
2016-11-03 03:53:06 +01:00
|
|
|
lvm_cmd = ["lvextend", "-L%s" % size, cmd[1]]
|
2017-07-29 04:42:19 +02:00
|
|
|
elif action == 'activate':
|
|
|
|
lvm_cmd = ['lvchange', '-ay', cmd[1]]
|
2017-10-13 01:11:55 +02:00
|
|
|
elif action == 'rename':
|
|
|
|
lvm_cmd = ['lvrename', cmd[1], cmd[2]]
|
2016-11-03 03:53:06 +01:00
|
|
|
else:
|
|
|
|
raise NotImplementedError('unsupported action: ' + action)
|
2016-11-25 02:04:09 +01:00
|
|
|
if lvm_is_very_old:
|
|
|
|
# old lvm in trusty image used there does not support -k option
|
|
|
|
lvm_cmd = [x for x in lvm_cmd if x != '-kn']
|
2016-11-03 03:53:06 +01:00
|
|
|
if os.getuid() != 0:
|
|
|
|
cmd = ['sudo', 'lvm'] + lvm_cmd
|
|
|
|
else:
|
|
|
|
cmd = ['lvm'] + lvm_cmd
|
2018-10-19 02:38:47 +02:00
|
|
|
|
|
|
|
return cmd
|
|
|
|
|
|
|
|
def _process_lvm_output(returncode, stdout, stderr, log):
|
|
|
|
'''Process output of LVM, determine if the call was successful and
|
|
|
|
possibly log warnings.'''
|
2018-04-14 21:36:03 +02:00
|
|
|
# Filter out warning about intended over-provisioning.
|
|
|
|
# Upstream discussion about missing option to silence it:
|
|
|
|
# https://bugzilla.redhat.com/1347008
|
2018-10-19 02:38:47 +02:00
|
|
|
err = '\n'.join(line for line in stderr.decode().splitlines()
|
2018-04-14 21:36:03 +02:00
|
|
|
if 'exceeds the size of thin pool' not in line)
|
2018-10-19 02:38:47 +02:00
|
|
|
if stdout:
|
|
|
|
log.debug(stdout)
|
|
|
|
if returncode == 0 and err:
|
2016-07-12 18:44:05 +02:00
|
|
|
log.warning(err)
|
2018-10-19 02:38:47 +02:00
|
|
|
elif returncode != 0:
|
2016-07-12 18:44:05 +02:00
|
|
|
assert err, "Command exited unsuccessful, but printed nothing to stderr"
|
2018-04-13 15:57:09 +02:00
|
|
|
err = err.replace('%', '%%')
|
2016-07-12 18:44:05 +02:00
|
|
|
raise qubes.storage.StoragePoolException(err)
|
|
|
|
return True
|
2016-08-28 23:50:04 +02:00
|
|
|
|
2018-10-19 02:38:47 +02:00
|
|
|
def qubes_lvm(cmd, log=logging.getLogger('qubes.storage.lvm')):
|
|
|
|
''' Call :program:`lvm` to execute an LVM operation '''
|
|
|
|
# the only caller for this non-coroutine version is ThinVolume.export()
|
|
|
|
cmd = _get_lvm_cmdline(cmd)
|
|
|
|
environ = os.environ.copy()
|
|
|
|
environ['LC_ALL'] = 'C.utf8'
|
|
|
|
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
|
|
|
|
close_fds=True, env=environ)
|
|
|
|
out, err = p.communicate()
|
|
|
|
return _process_lvm_output(p.returncode, out, err, log)
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def qubes_lvm_coro(cmd, log=logging.getLogger('qubes.storage.lvm')):
|
|
|
|
''' Call :program:`lvm` to execute an LVM operation
|
|
|
|
|
|
|
|
Coroutine version of :py:func:`qubes_lvm`'''
|
|
|
|
cmd = _get_lvm_cmdline(cmd)
|
|
|
|
environ = os.environ.copy()
|
|
|
|
environ['LC_ALL'] = 'C.utf8'
|
|
|
|
p = yield from asyncio.create_subprocess_exec(*cmd,
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
stderr=subprocess.PIPE,
|
|
|
|
close_fds=True, env=environ)
|
|
|
|
out, err = yield from p.communicate()
|
|
|
|
return _process_lvm_output(p.returncode, out, err, log)
|
|
|
|
|
2016-08-28 23:50:04 +02:00
|
|
|
|
2016-09-02 19:46:11 +02:00
|
|
|
def reset_cache():
|
2016-09-04 23:25:39 +02:00
|
|
|
qubes.storage.lvm.size_cache = init_cache()
|
2018-10-19 02:38:47 +02:00
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def reset_cache_coro():
|
|
|
|
qubes.storage.lvm.size_cache = yield from init_cache_coro()
|