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>
|
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
#
|
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 '''
|
|
|
|
|
|
|
|
import logging
|
2017-07-04 03:29:54 +02:00
|
|
|
import operator
|
2016-07-12 18:44:05 +02:00
|
|
|
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()
|
|
|
|
|
2016-07-12 18:44:05 +02:00
|
|
|
class ThinPool(qubes.storage.Pool):
|
|
|
|
''' LVM Thin based pool implementation
|
|
|
|
''' # 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)
|
|
|
|
self.log = logging.getLogger('qube.storage.lvm.%s' % self._pool_id)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def config(self):
|
|
|
|
return {
|
|
|
|
'name': self.name,
|
|
|
|
'volume_group': self.volume_group,
|
|
|
|
'thin_pool': self.thin_pool,
|
|
|
|
'driver': ThinPool.driver
|
|
|
|
}
|
|
|
|
|
|
|
|
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`.
|
|
|
|
'''
|
|
|
|
|
|
|
|
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
|
2016-07-12 18:44:05 +02:00
|
|
|
return ThinVolume(**volume_config)
|
|
|
|
|
|
|
|
def setup(self):
|
2016-08-18 11:41:02 +02:00
|
|
|
pass # TODO Should we create a non existing pool?
|
2016-07-12 18:44:05 +02:00
|
|
|
|
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
|
2016-11-04 13:19:49 +01:00
|
|
|
if vid.endswith('-snap'):
|
|
|
|
# implementation detail volume
|
|
|
|
continue
|
2017-07-04 03:29:54 +02:00
|
|
|
if vid.endswith('-back'):
|
|
|
|
# old revisions
|
|
|
|
continue
|
2016-07-12 18:44:05 +02:00
|
|
|
config = {
|
2017-06-26 11:42:24 +02:00
|
|
|
'pool': self,
|
2016-07-12 18:44:05 +02:00
|
|
|
'vid': vid,
|
|
|
|
'name': vid,
|
|
|
|
'volume_group': self.volume_group,
|
2016-11-03 03:53:06 +01:00
|
|
|
'rw': vol_info['attr'][1] == 'w',
|
2016-07-12 18:44:05 +02:00
|
|
|
}
|
|
|
|
volumes += [ThinVolume(**config)]
|
|
|
|
return volumes
|
|
|
|
|
2016-08-28 23:50:04 +02:00
|
|
|
|
2017-07-29 04:42:19 +02:00
|
|
|
def init_cache(log=logging.getLogger('qubes.storage.lvm')):
|
2016-11-03 03:53:06 +01:00
|
|
|
cmd = ['lvs', '--noheadings', '-o',
|
2017-07-04 03:29:54 +02:00
|
|
|
'vg_name,pool_lv,name,lv_size,data_percent,lv_attr,origin',
|
2016-11-03 03:53:06 +01:00
|
|
|
'--units', 'b', '--separator', ',']
|
|
|
|
if os.getuid() != 0:
|
|
|
|
cmd.insert(0, 'sudo')
|
|
|
|
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
|
|
|
|
close_fds=True)
|
2016-08-28 23:50:04 +02:00
|
|
|
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)
|
|
|
|
|
|
|
|
result = {}
|
|
|
|
|
2016-09-02 19:46:11 +02:00
|
|
|
for line in out.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, \
|
|
|
|
origin = line.split(',', 6)
|
2016-11-03 03:53:06 +01:00
|
|
|
if '' in [pool_name, pool_lv, name, size, usage_percent]:
|
2016-08-28 23:50:04 +02:00
|
|
|
continue
|
|
|
|
name = pool_name + "/" + name
|
|
|
|
size = int(size[:-1])
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
size_cache = init_cache()
|
|
|
|
|
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)
|
2017-06-09 04:46:46 +02:00
|
|
|
self.log = logging.getLogger('qube.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'
|
|
|
|
|
2016-09-02 19:45:31 +02:00
|
|
|
self._size = size
|
|
|
|
|
2017-06-07 00:20:41 +02:00
|
|
|
@property
|
|
|
|
def path(self):
|
|
|
|
return '/dev/' + self.vid
|
|
|
|
|
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):]
|
|
|
|
seconds = int(revision_vid[:-len('-back')])
|
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:
|
|
|
|
return qubes.storage.lvm.size_cache[self.vid]['size']
|
|
|
|
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")
|
|
|
|
|
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-06-09 04:46:46 +02:00
|
|
|
self.log.debug('Resetting volatile ' + self.vid)
|
|
|
|
try:
|
|
|
|
cmd = ['remove', self.vid]
|
|
|
|
qubes_lvm(cmd, self.log)
|
|
|
|
except qubes.storage.StoragePoolException:
|
|
|
|
pass
|
|
|
|
# pylint: disable=protected-access
|
|
|
|
cmd = ['create', self.pool._pool_id, self.vid.split('/')[1],
|
|
|
|
str(self.size)]
|
|
|
|
qubes_lvm(cmd, self.log)
|
|
|
|
|
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(),
|
|
|
|
key=operator.itemgetter(1))
|
|
|
|
revisions = revisions[:-self.revisions_to_keep]
|
|
|
|
revisions = [rev_id for rev_id, _ in revisions]
|
|
|
|
|
|
|
|
for rev_id in revisions:
|
|
|
|
try:
|
|
|
|
cmd = ['remove', self.vid + rev_id]
|
|
|
|
qubes_lvm(cmd, self.log)
|
|
|
|
except qubes.storage.StoragePoolException:
|
|
|
|
pass
|
|
|
|
|
2017-06-09 04:46:46 +02:00
|
|
|
def _commit(self):
|
|
|
|
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
|
|
|
|
assert hasattr(self, '_vid_snap')
|
|
|
|
|
2017-07-04 03:29:54 +02:00
|
|
|
if self.revisions_to_keep > 0:
|
|
|
|
cmd = ['clone', self.vid,
|
|
|
|
'{}-{}-back'.format(self.vid, int(time.time()))]
|
2017-06-09 04:46:46 +02:00
|
|
|
qubes_lvm(cmd, self.log)
|
2017-07-04 03:29:54 +02:00
|
|
|
self._remove_revisions()
|
2017-06-09 04:46:46 +02:00
|
|
|
|
|
|
|
cmd = ['remove', self.vid]
|
|
|
|
qubes_lvm(cmd, self.log)
|
|
|
|
cmd = ['clone', self._vid_snap, self.vid]
|
|
|
|
qubes_lvm(cmd, self.log)
|
|
|
|
|
|
|
|
|
|
|
|
def create(self):
|
|
|
|
assert self.vid
|
|
|
|
assert self.size
|
|
|
|
if self.save_on_stop:
|
|
|
|
if self.source:
|
|
|
|
cmd = ['clone', str(self.source), self.vid]
|
|
|
|
else:
|
|
|
|
cmd = [
|
|
|
|
'create',
|
|
|
|
self.pool._pool_id, # pylint: disable=protected-access
|
|
|
|
self.vid.split('/', 1)[1],
|
|
|
|
str(self.size)
|
|
|
|
]
|
|
|
|
qubes_lvm(cmd, self.log)
|
|
|
|
reset_cache()
|
|
|
|
return self
|
|
|
|
|
2017-06-26 11:39:02 +02:00
|
|
|
def remove(self):
|
|
|
|
assert self.vid
|
|
|
|
if self.is_dirty():
|
|
|
|
cmd = ['remove', self._vid_snap]
|
|
|
|
qubes_lvm(cmd, self.log)
|
|
|
|
|
2017-07-04 03:29:54 +02:00
|
|
|
self._remove_revisions(self.revisions.keys())
|
2017-06-26 11:39:02 +02:00
|
|
|
if not os.path.exists(self.path):
|
|
|
|
return
|
|
|
|
cmd = ['remove', self.vid]
|
|
|
|
qubes_lvm(cmd, self.log)
|
|
|
|
reset_cache()
|
|
|
|
|
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
|
|
|
|
qubes_lvm(['activate', self.vid], self.log)
|
2017-06-09 04:46:46 +02:00
|
|
|
devpath = '/dev/' + self.vid
|
|
|
|
return devpath
|
|
|
|
|
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
|
|
|
|
|
|
|
|
# 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
|
2017-06-26 11:59:53 +02:00
|
|
|
cmd = ['remove', self.vid]
|
|
|
|
qubes_lvm(cmd, self.log)
|
2017-06-09 04:46:46 +02:00
|
|
|
cmd = ['clone', str(src_volume), str(self)]
|
|
|
|
qubes_lvm(cmd, self.log)
|
|
|
|
else:
|
2017-06-26 11:42:24 +02:00
|
|
|
src_path = src_volume.export()
|
2017-07-04 03:29:54 +02:00
|
|
|
cmd = ['dd', 'if=' + src_path, 'of=/dev/' + self.vid,
|
2017-06-09 04:46:46 +02:00
|
|
|
'conv=sparse']
|
2017-07-04 03:29:54 +02:00
|
|
|
p = yield from asyncio.create_subprocess_exec(*cmd)
|
|
|
|
yield from p.wait()
|
|
|
|
if p.returncode != 0:
|
|
|
|
raise qubes.storage.StoragePoolException(
|
|
|
|
'Failed to import volume {!r}, dd exit code: {}'.format(
|
|
|
|
src_volume, p.returncode))
|
2017-06-09 04:46:46 +02:00
|
|
|
reset_cache()
|
|
|
|
|
|
|
|
return self
|
|
|
|
|
|
|
|
def import_data(self):
|
|
|
|
''' Returns an object that can be `open()`. '''
|
|
|
|
devpath = '/dev/' + self.vid
|
|
|
|
return devpath
|
|
|
|
|
|
|
|
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'] !=
|
|
|
|
self.source.vid.split('/')[1])
|
|
|
|
|
|
|
|
|
2017-06-09 04:46:46 +02:00
|
|
|
def revert(self, revision=None):
|
2017-07-04 03:29:54 +02:00
|
|
|
if revision is None:
|
|
|
|
revision = \
|
|
|
|
max(self.revisions.items(), key=operator.itemgetter(1))[0]
|
|
|
|
old_path = self.path + '-' + 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)
|
|
|
|
|
|
|
|
cmd = ['remove', self.vid]
|
|
|
|
qubes_lvm(cmd, self.log)
|
2017-07-04 03:29:54 +02:00
|
|
|
cmd = ['clone', self.vid + '-' + revision, self.vid]
|
2017-06-09 04:46:46 +02:00
|
|
|
qubes_lvm(cmd, self.log)
|
|
|
|
reset_cache()
|
|
|
|
return self
|
|
|
|
|
|
|
|
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'
|
|
|
|
' disabled. If you really know what you'
|
|
|
|
' are doing, use `lvresize` on %s manually.' %
|
|
|
|
(self.name, self.vid))
|
|
|
|
|
|
|
|
cmd = ['extend', self.vid, str(size)]
|
|
|
|
qubes_lvm(cmd, self.log)
|
|
|
|
reset_cache()
|
|
|
|
|
|
|
|
def _snapshot(self):
|
|
|
|
try:
|
|
|
|
cmd = ['remove', self._vid_snap]
|
|
|
|
qubes_lvm(cmd, self.log)
|
|
|
|
except: # pylint: disable=bare-except
|
|
|
|
pass
|
|
|
|
|
|
|
|
if self.source is None:
|
|
|
|
cmd = ['clone', self.vid, self._vid_snap]
|
|
|
|
else:
|
|
|
|
cmd = ['clone', str(self.source), self._vid_snap]
|
|
|
|
qubes_lvm(cmd, self.log)
|
|
|
|
|
|
|
|
|
|
|
|
def start(self):
|
2017-07-04 03:29:54 +02:00
|
|
|
if self.snap_on_start or self.save_on_stop:
|
2017-06-09 04:46:46 +02:00
|
|
|
if not self.save_on_stop or not self.is_dirty():
|
|
|
|
self._snapshot()
|
2017-07-04 03:29:54 +02:00
|
|
|
else:
|
2017-06-09 04:46:46 +02:00
|
|
|
self._reset()
|
|
|
|
|
|
|
|
reset_cache()
|
|
|
|
return self
|
|
|
|
|
|
|
|
def stop(self):
|
2017-07-04 03:29:54 +02:00
|
|
|
if self.save_on_stop:
|
2017-06-09 04:46:46 +02:00
|
|
|
self._commit()
|
2017-07-04 03:29:54 +02:00
|
|
|
if self.snap_on_start or self.save_on_stop:
|
2017-06-09 04:46:46 +02:00
|
|
|
cmd = ['remove', self._vid_snap]
|
|
|
|
qubes_lvm(cmd, self.log)
|
2017-07-04 03:29:54 +02:00
|
|
|
else:
|
2017-06-09 04:46:46 +02:00
|
|
|
cmd = ['remove', self.vid]
|
|
|
|
qubes_lvm(cmd, self.log)
|
|
|
|
reset_cache()
|
|
|
|
return self
|
|
|
|
|
|
|
|
def verify(self):
|
|
|
|
''' Verifies the volume. '''
|
|
|
|
try:
|
|
|
|
vol_info = size_cache[self.vid]
|
|
|
|
return vol_info['attr'][4] == 'a'
|
|
|
|
except KeyError:
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
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:
|
|
|
|
return qubes.storage.lvm.size_cache[self.vid]['usage']
|
|
|
|
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
|
|
|
|
|
|
|
|
2017-06-09 04:46:46 +02:00
|
|
|
def qubes_lvm(cmd, log=logging.getLogger('qubes.storage.lvm')):
|
2016-11-03 03:53:06 +01:00
|
|
|
''' Call :program:`lvm` to execute an LVM operation '''
|
|
|
|
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':
|
|
|
|
size = int(cmd[2]) / (1000 * 1000)
|
|
|
|
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]]
|
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
|
|
|
|
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
|
|
|
|
close_fds=True)
|
2016-07-12 18:44:05 +02:00
|
|
|
out, err = p.communicate()
|
|
|
|
return_code = p.returncode
|
|
|
|
if out:
|
2016-08-28 23:56:06 +02:00
|
|
|
log.debug(out)
|
2016-07-12 18:44:05 +02:00
|
|
|
if return_code == 0 and err:
|
|
|
|
log.warning(err)
|
|
|
|
elif return_code != 0:
|
|
|
|
assert err, "Command exited unsuccessful, but printed nothing to stderr"
|
|
|
|
raise qubes.storage.StoragePoolException(err)
|
|
|
|
return True
|
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()
|