2015-01-16 15:33:03 +01:00
|
|
|
#
|
2015-01-19 18:03:23 +01:00
|
|
|
# The Qubes OS Project, https://www.qubes-os.org/
|
2015-01-16 15:33:03 +01:00
|
|
|
#
|
2015-01-19 18:03:23 +01:00
|
|
|
# Copyright (C) 2015 Joanna Rutkowska <joanna@invisiblethingslab.com>
|
|
|
|
# Copyright (C) 2013-2015 Marek Marczykowski-Górecki
|
|
|
|
# <marmarek@invisiblethingslab.com>
|
2015-01-16 15:33:03 +01:00
|
|
|
# Copyright (C) 2015 Wojtek Porczyk <woju@invisiblethingslab.com>
|
|
|
|
#
|
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.
|
2015-01-16 15:33:03 +01:00
|
|
|
#
|
2017-10-12 00:11:50 +02:00
|
|
|
# This library is distributed in the hope that it will be useful,
|
2015-01-16 15:33:03 +01: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.
|
2015-01-16 15:33:03 +01: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/>.
|
2015-01-16 15:33:03 +01:00
|
|
|
#
|
2017-01-18 22:16:46 +01:00
|
|
|
|
2016-06-16 13:32:54 +02:00
|
|
|
''' This module contains pool implementations backed by file images'''
|
|
|
|
|
2015-01-16 15:33:03 +01:00
|
|
|
from __future__ import absolute_import
|
|
|
|
|
|
|
|
import os
|
|
|
|
import os.path
|
2015-01-19 18:14:15 +01:00
|
|
|
import re
|
2015-01-16 15:33:03 +01:00
|
|
|
import subprocess
|
|
|
|
|
2016-07-12 17:53:31 +02:00
|
|
|
import qubes.storage
|
2015-01-16 15:33:03 +01:00
|
|
|
|
2016-04-15 20:40:53 +02:00
|
|
|
BLKSIZE = 512
|
2015-01-16 15:33:03 +01:00
|
|
|
|
|
|
|
|
2016-07-12 17:53:31 +02:00
|
|
|
class FilePool(qubes.storage.Pool):
|
|
|
|
''' File based 'original' disk implementation
|
2017-07-01 22:45:22 +02:00
|
|
|
|
|
|
|
Volumes are stored in sparse files. Additionally device-mapper is used for
|
|
|
|
applying copy-on-write layer.
|
|
|
|
|
|
|
|
Quick reference on device-mapper layers:
|
|
|
|
|
|
|
|
snap_on_start save_on_stop layout
|
|
|
|
yes yes not supported
|
|
|
|
no yes snapshot-origin(volume.img, volume-cow.img)
|
|
|
|
yes no snapshot(
|
|
|
|
snapshot(source.img, source-cow.img),
|
|
|
|
volume-cow.img)
|
|
|
|
no no volume.img directly
|
|
|
|
|
2016-07-12 17:53:31 +02:00
|
|
|
''' # pylint: disable=protected-access
|
2016-04-30 20:42:46 +02:00
|
|
|
driver = 'file'
|
2015-01-16 15:33:03 +01:00
|
|
|
|
2016-07-12 17:53:31 +02:00
|
|
|
def __init__(self, revisions_to_keep=1, dir_path=None, **kwargs):
|
2017-11-06 20:52:12 +01:00
|
|
|
self._revisions_to_keep = 0
|
2016-07-12 17:53:31 +02:00
|
|
|
super(FilePool, self).__init__(revisions_to_keep=revisions_to_keep,
|
|
|
|
**kwargs)
|
2016-04-15 20:15:18 +02:00
|
|
|
assert dir_path, "No pool dir_path specified"
|
|
|
|
self.dir_path = os.path.normpath(dir_path)
|
2016-06-06 18:01:46 +02:00
|
|
|
self._volumes = []
|
2016-03-03 01:05:23 +01:00
|
|
|
|
2016-04-15 16:01:27 +02:00
|
|
|
@property
|
|
|
|
def config(self):
|
|
|
|
return {
|
|
|
|
'name': self.name,
|
|
|
|
'dir_path': self.dir_path,
|
2016-04-30 20:42:46 +02:00
|
|
|
'driver': FilePool.driver,
|
2016-07-12 17:53:31 +02:00
|
|
|
'revisions_to_keep': self.revisions_to_keep
|
2016-04-15 16:01:27 +02:00
|
|
|
}
|
|
|
|
|
2016-07-12 17:53:31 +02:00
|
|
|
def init_volume(self, vm, volume_config):
|
2017-07-01 22:45:22 +02:00
|
|
|
if volume_config.get('snap_on_start', False) and \
|
|
|
|
volume_config.get('save_on_stop', False):
|
|
|
|
raise NotImplementedError(
|
|
|
|
'snap_on_start + save_on_stop not supported by file driver')
|
2016-07-12 17:53:31 +02:00
|
|
|
volume_config['dir_path'] = self.dir_path
|
|
|
|
|
|
|
|
if 'vid' not in volume_config:
|
|
|
|
volume_config['vid'] = os.path.join(
|
|
|
|
self._vid_prefix(vm), volume_config['name'])
|
|
|
|
|
|
|
|
try:
|
2017-07-01 22:45:22 +02:00
|
|
|
if not volume_config.get('save_on_stop', False):
|
2016-07-12 17:53:31 +02:00
|
|
|
volume_config['revisions_to_keep'] = 0
|
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
|
2017-11-06 20:52:12 +01:00
|
|
|
if 'revisions_to_keep' not in volume_config:
|
|
|
|
volume_config['revisions_to_keep'] = self.revisions_to_keep
|
2017-07-01 22:45:22 +02:00
|
|
|
|
2017-06-09 04:46:46 +02:00
|
|
|
volume_config['pool'] = self
|
2016-07-12 17:53:31 +02:00
|
|
|
volume = FileVolume(**volume_config)
|
|
|
|
self._volumes += [volume]
|
|
|
|
return volume
|
|
|
|
|
2017-11-06 20:52:12 +01:00
|
|
|
@property
|
|
|
|
def revisions_to_keep(self):
|
|
|
|
return self._revisions_to_keep
|
|
|
|
|
|
|
|
@revisions_to_keep.setter
|
|
|
|
def revisions_to_keep(self, value):
|
|
|
|
value = int(value)
|
|
|
|
if value > 1:
|
|
|
|
raise NotImplementedError(
|
|
|
|
'FilePool supports maximum 1 volume revision to keep')
|
|
|
|
self._revisions_to_keep = value
|
|
|
|
|
2016-04-15 20:33:04 +02:00
|
|
|
def destroy(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def setup(self):
|
|
|
|
create_dir_if_not_exists(self.dir_path)
|
|
|
|
appvms_path = os.path.join(self.dir_path, 'appvms')
|
|
|
|
create_dir_if_not_exists(appvms_path)
|
|
|
|
vm_templates_path = os.path.join(self.dir_path, 'vm-templates')
|
|
|
|
create_dir_if_not_exists(vm_templates_path)
|
|
|
|
|
2016-07-12 17:53:31 +02:00
|
|
|
@staticmethod
|
|
|
|
def _vid_prefix(vm):
|
|
|
|
''' Helper to create a prefix for the vid for volume
|
|
|
|
''' # FIX Remove this if we drop the file backend
|
|
|
|
import qubes.vm.templatevm # pylint: disable=redefined-outer-name
|
|
|
|
import qubes.vm.dispvm # pylint: disable=redefined-outer-name
|
|
|
|
if isinstance(vm, qubes.vm.templatevm.TemplateVM):
|
|
|
|
subdir = 'vm-templates'
|
|
|
|
else:
|
|
|
|
subdir = 'appvms'
|
|
|
|
|
|
|
|
return os.path.join(subdir, vm.name)
|
2016-04-15 20:40:53 +02:00
|
|
|
|
|
|
|
def target_dir(self, vm):
|
2016-04-15 20:15:18 +02:00
|
|
|
""" Returns the path to vmdir depending on the type of the VM.
|
|
|
|
|
|
|
|
The default QubesOS file storage saves the vm images in three
|
|
|
|
different directories depending on the ``QubesVM`` type:
|
|
|
|
|
|
|
|
* ``appvms`` for ``QubesAppVm`` or ``QubesHvm``
|
|
|
|
* ``vm-templates`` for ``QubesTemplateVm`` or ``QubesTemplateHvm``
|
|
|
|
|
|
|
|
Args:
|
|
|
|
vm: a QubesVM
|
|
|
|
pool_dir: the root directory of the pool
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
string (str) absolute path to the directory where the vm files
|
|
|
|
are stored
|
|
|
|
"""
|
2016-04-15 20:40:48 +02:00
|
|
|
|
2016-07-12 17:53:31 +02:00
|
|
|
return os.path.join(self.dir_path, self._vid_prefix(vm))
|
2016-04-14 19:00:52 +02:00
|
|
|
|
2017-06-26 02:46:49 +02:00
|
|
|
def list_volumes(self):
|
2016-06-06 18:01:46 +02:00
|
|
|
return self._volumes
|
2016-04-14 19:00:52 +02:00
|
|
|
|
2017-10-29 02:23:00 +02:00
|
|
|
@property
|
|
|
|
def size(self):
|
|
|
|
statvfs = os.statvfs(self.dir_path)
|
|
|
|
return statvfs.f_frsize * statvfs.f_blocks
|
|
|
|
|
|
|
|
@property
|
|
|
|
def usage(self):
|
|
|
|
statvfs = os.statvfs(self.dir_path)
|
|
|
|
return statvfs.f_frsize * (statvfs.f_blocks - statvfs.f_bfree)
|
|
|
|
|
2018-03-19 22:36:45 +01:00
|
|
|
def included_in(self, app):
|
|
|
|
''' Check if there is pool containing this one - either as a
|
|
|
|
filesystem or its LVM volume'''
|
|
|
|
return qubes.storage.search_pool_containing_dir(
|
|
|
|
[pool for pool in app.pools.values() if pool is not self],
|
|
|
|
self.dir_path)
|
2016-04-15 19:59:40 +02:00
|
|
|
|
2016-07-12 17:53:31 +02:00
|
|
|
class FileVolume(qubes.storage.Volume):
|
2016-04-15 15:16:25 +02:00
|
|
|
''' Parent class for the xen volumes implementation which expects a
|
2016-07-12 17:53:31 +02:00
|
|
|
`target_dir` param on initialization. '''
|
2016-04-15 20:40:53 +02:00
|
|
|
|
2017-07-01 22:45:22 +02:00
|
|
|
def __init__(self, dir_path, **kwargs):
|
2016-07-12 17:53:31 +02:00
|
|
|
self.dir_path = dir_path
|
|
|
|
assert self.dir_path, "dir_path not specified"
|
2017-11-06 20:52:12 +01:00
|
|
|
self._revisions_to_keep = 0
|
2016-04-30 20:42:46 +02:00
|
|
|
super(FileVolume, self).__init__(**kwargs)
|
2016-04-15 20:40:53 +02:00
|
|
|
|
2017-07-01 22:45:22 +02:00
|
|
|
if self.snap_on_start:
|
2017-06-09 04:46:46 +02:00
|
|
|
img_name = self.source.vid + '-cow.img'
|
2016-12-01 00:10:37 +01:00
|
|
|
self.path_source_cow = os.path.join(self.dir_path, img_name)
|
2016-07-12 17:53:31 +02:00
|
|
|
|
2017-11-06 20:52:12 +01:00
|
|
|
@property
|
|
|
|
def revisions_to_keep(self):
|
|
|
|
return self._revisions_to_keep
|
|
|
|
|
|
|
|
@revisions_to_keep.setter
|
|
|
|
def revisions_to_keep(self, value):
|
|
|
|
if int(value) > 1:
|
|
|
|
raise NotImplementedError(
|
|
|
|
'FileVolume supports maximum 1 volume revision to keep')
|
|
|
|
self._revisions_to_keep = int(value)
|
|
|
|
|
2017-06-09 04:46:46 +02:00
|
|
|
def create(self):
|
|
|
|
assert isinstance(self.size, int) and self.size > 0, \
|
2017-07-01 22:45:22 +02:00
|
|
|
'Volume size must be > 0'
|
|
|
|
if not self.snap_on_start:
|
2017-06-09 04:46:46 +02:00
|
|
|
create_sparse_file(self.path, self.size)
|
|
|
|
|
2017-06-26 11:39:02 +02:00
|
|
|
def remove(self):
|
2017-07-01 22:45:22 +02:00
|
|
|
if not self.snap_on_start:
|
2017-06-26 11:39:02 +02:00
|
|
|
_remove_if_exists(self.path)
|
2017-07-01 22:45:22 +02:00
|
|
|
if self.snap_on_start or self.save_on_stop:
|
|
|
|
_remove_if_exists(self.path_cow)
|
2017-06-26 11:39:02 +02:00
|
|
|
|
2017-06-09 04:46:46 +02:00
|
|
|
def is_dirty(self):
|
2017-07-01 22:45:22 +02:00
|
|
|
if not self.save_on_stop:
|
|
|
|
return False
|
|
|
|
if os.path.exists(self.path_cow):
|
|
|
|
stat = os.stat(self.path_cow)
|
|
|
|
return stat.st_blocks > 0
|
|
|
|
return False
|
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
|
|
|
|
''' # pylint: disable=no-self-use
|
|
|
|
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 `truncate` on %s manually.' %
|
|
|
|
(self.name, self.vid))
|
|
|
|
|
|
|
|
with open(self.path, 'a+b') as fd:
|
|
|
|
fd.truncate(size)
|
|
|
|
|
2017-07-01 22:45:22 +02:00
|
|
|
p = subprocess.Popen(['losetup', '--associated', self.path],
|
2017-06-09 04:46:46 +02:00
|
|
|
stdout=subprocess.PIPE)
|
|
|
|
result = p.communicate()
|
|
|
|
|
|
|
|
m = re.match(r'^(/dev/loop\d+):\s', result[0].decode())
|
|
|
|
if m is not None:
|
|
|
|
loop_dev = m.group(1)
|
|
|
|
|
|
|
|
# resize loop device
|
2017-07-01 22:45:22 +02:00
|
|
|
subprocess.check_call(['losetup', '--set-capacity',
|
2017-06-09 04:46:46 +02:00
|
|
|
loop_dev])
|
|
|
|
self.size = size
|
|
|
|
|
|
|
|
def commit(self):
|
|
|
|
msg = 'Tried to commit a non commitable volume {!r}'.format(self)
|
2017-07-01 22:45:22 +02:00
|
|
|
assert self.save_on_stop and self.rw, msg
|
2017-06-09 04:46:46 +02:00
|
|
|
|
|
|
|
if os.path.exists(self.path_cow):
|
2017-07-01 22:45:22 +02:00
|
|
|
if self.revisions_to_keep:
|
|
|
|
old_path = self.path_cow + '.old'
|
|
|
|
os.rename(self.path_cow, old_path)
|
|
|
|
else:
|
|
|
|
os.unlink(self.path_cow)
|
2017-06-09 04:46:46 +02:00
|
|
|
|
2017-07-01 22:45:22 +02:00
|
|
|
create_sparse_file(self.path_cow, self.size)
|
2017-06-09 04:46:46 +02:00
|
|
|
return self
|
|
|
|
|
|
|
|
def export(self):
|
2017-07-01 22:45:22 +02:00
|
|
|
# FIXME: this should rather return snapshot(self.path, self.path_cow)
|
|
|
|
# if domain is running
|
2017-06-09 04:46:46 +02:00
|
|
|
return self.path
|
|
|
|
|
|
|
|
def import_volume(self, src_volume):
|
2018-03-21 01:48:20 +01:00
|
|
|
if src_volume.snap_on_start:
|
|
|
|
raise qubes.storage.StoragePoolException(
|
|
|
|
"Can not import snapshot volume {!s} in to pool {!s} ".format(
|
|
|
|
src_volume, self))
|
2017-06-09 04:46:46 +02:00
|
|
|
if self.save_on_stop:
|
2017-06-26 11:59:53 +02:00
|
|
|
_remove_if_exists(self.path)
|
2017-06-09 04:46:46 +02:00
|
|
|
copy_file(src_volume.export(), self.path)
|
|
|
|
return self
|
|
|
|
|
|
|
|
def import_data(self):
|
2018-03-19 03:29:26 +01:00
|
|
|
if not self.save_on_stop:
|
|
|
|
raise qubes.storage.StoragePoolException(
|
|
|
|
"Can not import into save_on_stop=False volume {!s}".format(
|
|
|
|
self))
|
|
|
|
create_sparse_file(self.path_import, self.size)
|
|
|
|
return self.path_import
|
|
|
|
|
|
|
|
def import_data_end(self, success):
|
|
|
|
if success:
|
|
|
|
os.rename(self.path_import, self.path)
|
|
|
|
else:
|
|
|
|
os.unlink(self.path_import)
|
|
|
|
return self
|
2017-06-09 04:46:46 +02:00
|
|
|
|
|
|
|
def reset(self):
|
|
|
|
''' Remove and recreate a volatile volume '''
|
2017-07-01 22:45:22 +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
|
|
|
assert isinstance(self.size, int) and self.size > 0, \
|
|
|
|
'Volatile volume size must be > 0'
|
|
|
|
|
|
|
|
_remove_if_exists(self.path)
|
2017-07-01 22:45:22 +02:00
|
|
|
create_sparse_file(self.path, self.size)
|
2017-06-09 04:46:46 +02:00
|
|
|
return self
|
|
|
|
|
|
|
|
def start(self):
|
2017-07-01 22:45:22 +02:00
|
|
|
if not self.save_on_stop and not self.snap_on_start:
|
2017-06-09 04:46:46 +02:00
|
|
|
self.reset()
|
|
|
|
else:
|
2017-07-01 22:45:22 +02:00
|
|
|
if not self.save_on_stop:
|
|
|
|
# make sure previous snapshot is removed - even if VM
|
|
|
|
# shutdown routine wasn't called (power interrupt or so)
|
|
|
|
_remove_if_exists(self.path_cow)
|
|
|
|
if not os.path.exists(self.path_cow):
|
|
|
|
create_sparse_file(self.path_cow, self.size)
|
|
|
|
if not self.snap_on_start:
|
|
|
|
_check_path(self.path)
|
|
|
|
if hasattr(self, 'path_source_cow'):
|
|
|
|
if not os.path.exists(self.path_source_cow):
|
|
|
|
create_sparse_file(self.path_source_cow, self.size)
|
2017-06-09 04:46:46 +02:00
|
|
|
return self
|
|
|
|
|
|
|
|
def stop(self):
|
|
|
|
if self.save_on_stop:
|
|
|
|
self.commit()
|
|
|
|
elif self.snap_on_start:
|
|
|
|
_remove_if_exists(self.path_cow)
|
|
|
|
else:
|
|
|
|
_remove_if_exists(self.path)
|
|
|
|
return self
|
|
|
|
|
2017-06-07 00:20:41 +02:00
|
|
|
@property
|
|
|
|
def path(self):
|
2017-07-01 22:45:22 +02:00
|
|
|
if self.snap_on_start:
|
2017-06-09 04:46:46 +02:00
|
|
|
return os.path.join(self.dir_path, self.source.vid + '.img')
|
2017-06-07 00:20:41 +02:00
|
|
|
return os.path.join(self.dir_path, self.vid + '.img')
|
|
|
|
|
|
|
|
@property
|
|
|
|
def path_cow(self):
|
|
|
|
img_name = self.vid + '-cow.img'
|
|
|
|
return os.path.join(self.dir_path, img_name)
|
|
|
|
|
2018-03-19 03:29:26 +01:00
|
|
|
@property
|
|
|
|
def path_import(self):
|
|
|
|
img_name = self.vid + '-import.img'
|
|
|
|
return os.path.join(self.dir_path, img_name)
|
|
|
|
|
2016-07-12 17:53:31 +02:00
|
|
|
def verify(self):
|
|
|
|
''' Verifies the volume. '''
|
2017-07-01 22:45:22 +02:00
|
|
|
if not os.path.exists(self.path) and \
|
|
|
|
(self.snap_on_start or self.save_on_stop):
|
2016-07-12 17:53:31 +02:00
|
|
|
msg = 'Missing image file: {!s}.'.format(self.path)
|
|
|
|
raise qubes.storage.StoragePoolException(msg)
|
|
|
|
return True
|
|
|
|
|
|
|
|
@property
|
|
|
|
def script(self):
|
2017-07-01 22:45:22 +02:00
|
|
|
if not self.snap_on_start and not self.save_on_stop:
|
2016-07-12 17:53:31 +02:00
|
|
|
return None
|
2017-07-01 22:45:22 +02:00
|
|
|
elif not self.snap_on_start and self.save_on_stop:
|
2016-07-12 17:53:31 +02:00
|
|
|
return 'block-origin'
|
2017-07-01 22:45:22 +02:00
|
|
|
elif self.snap_on_start:
|
2016-07-12 17:53:31 +02:00
|
|
|
return 'block-snapshot'
|
2017-12-21 18:19:10 +01:00
|
|
|
return None
|
2016-07-12 17:53:31 +02:00
|
|
|
|
|
|
|
def block_device(self):
|
2017-04-15 16:29:50 +02:00
|
|
|
''' Return :py:class:`qubes.storage.BlockDevice` for serialization in
|
2016-07-12 17:53:31 +02:00
|
|
|
the libvirt XML template as <disk>.
|
2016-06-16 13:32:54 +02:00
|
|
|
'''
|
2016-07-12 17:53:31 +02:00
|
|
|
path = self.path
|
2017-07-01 22:45:22 +02:00
|
|
|
if self.snap_on_start:
|
2016-12-01 00:10:37 +01:00
|
|
|
path += ":" + self.path_source_cow
|
2017-07-01 22:45:22 +02:00
|
|
|
if self.snap_on_start or self.save_on_stop:
|
2016-07-12 17:53:31 +02:00
|
|
|
path += ":" + self.path_cow
|
2017-04-15 16:29:50 +02:00
|
|
|
return qubes.storage.BlockDevice(path, self.name, self.script, self.rw,
|
2016-07-12 17:53:31 +02:00
|
|
|
self.domain, self.devtype)
|
2016-06-16 13:32:54 +02:00
|
|
|
|
2016-07-12 17:53:31 +02:00
|
|
|
@property
|
|
|
|
def revisions(self):
|
|
|
|
if not hasattr(self, 'path_cow'):
|
|
|
|
return {}
|
2016-04-15 19:59:40 +02:00
|
|
|
|
2016-07-12 17:53:31 +02:00
|
|
|
old_revision = self.path_cow + '.old' # pylint: disable=no-member
|
2016-04-15 15:16:25 +02:00
|
|
|
|
2016-07-12 17:53:31 +02:00
|
|
|
if not os.path.exists(old_revision):
|
|
|
|
return {}
|
2017-04-15 20:04:38 +02:00
|
|
|
|
|
|
|
seconds = os.path.getctime(old_revision)
|
|
|
|
iso_date = qubes.storage.isodate(seconds).split('.', 1)[0]
|
2017-07-01 22:45:22 +02:00
|
|
|
return {'old': iso_date}
|
2016-04-15 19:59:40 +02:00
|
|
|
|
|
|
|
@property
|
2016-04-15 20:40:53 +02:00
|
|
|
def usage(self):
|
|
|
|
''' Returns the actualy used space '''
|
2017-07-30 18:36:23 +02:00
|
|
|
usage = 0
|
|
|
|
if self.save_on_stop or self.snap_on_start:
|
|
|
|
usage = get_disk_usage(self.path_cow)
|
|
|
|
if self.save_on_stop or not self.snap_on_start:
|
|
|
|
usage += get_disk_usage(self.path)
|
|
|
|
return usage
|
|
|
|
|
2016-04-15 20:40:53 +02:00
|
|
|
|
2016-06-21 14:41:14 +02:00
|
|
|
|
2016-04-15 20:40:53 +02:00
|
|
|
def create_sparse_file(path, size):
|
|
|
|
''' Create an empty sparse file '''
|
2016-04-15 19:59:40 +02:00
|
|
|
if os.path.exists(path):
|
2017-12-21 18:19:10 +01:00
|
|
|
raise IOError("Volume %s already exists" % path)
|
2016-04-15 20:40:53 +02:00
|
|
|
parent_dir = os.path.dirname(path)
|
|
|
|
if not os.path.exists(parent_dir):
|
|
|
|
os.makedirs(parent_dir)
|
2016-04-15 19:59:40 +02:00
|
|
|
with open(path, 'a+b') as fh:
|
|
|
|
fh.truncate(size)
|
2016-04-15 20:40:53 +02:00
|
|
|
|
|
|
|
|
|
|
|
def get_disk_usage_one(st):
|
|
|
|
'''Extract disk usage of one inode from its stat_result struct.
|
|
|
|
|
|
|
|
If known, get real disk usage, as written to device by filesystem, not
|
|
|
|
logical file size. Those values may be different for sparse files.
|
|
|
|
|
|
|
|
:param os.stat_result st: stat result
|
|
|
|
:returns: disk usage
|
|
|
|
'''
|
|
|
|
try:
|
|
|
|
return st.st_blocks * BLKSIZE
|
|
|
|
except AttributeError:
|
|
|
|
return st.st_size
|
|
|
|
|
|
|
|
|
|
|
|
def get_disk_usage(path):
|
|
|
|
'''Get real disk usage of given path (file or directory).
|
|
|
|
|
|
|
|
When *path* points to directory, then it is evaluated recursively.
|
|
|
|
|
2017-07-20 04:03:35 +02:00
|
|
|
This function tries estimate real disk usage. See documentation of
|
2016-04-15 20:40:53 +02:00
|
|
|
:py:func:`get_disk_usage_one`.
|
|
|
|
|
|
|
|
:param str path: path to evaluate
|
|
|
|
:returns: disk usage
|
|
|
|
'''
|
|
|
|
try:
|
|
|
|
st = os.lstat(path)
|
|
|
|
except OSError:
|
|
|
|
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:
|
|
|
|
ret += get_disk_usage_one(os.lstat(os.path.join(dirpath, name)))
|
|
|
|
|
|
|
|
return ret
|
|
|
|
|
|
|
|
|
|
|
|
def create_dir_if_not_exists(path):
|
|
|
|
""" Check if a directory exists in if not create it.
|
|
|
|
|
|
|
|
This method does not create any parent directories.
|
|
|
|
"""
|
|
|
|
if not os.path.exists(path):
|
|
|
|
os.mkdir(path)
|
|
|
|
|
|
|
|
|
|
|
|
def copy_file(source, destination):
|
2016-06-16 13:37:17 +02:00
|
|
|
'''Effective file copy, preserving sparse files etc.'''
|
2016-04-15 20:40:53 +02:00
|
|
|
# We prefer to use Linux's cp, because it nicely handles sparse files
|
|
|
|
assert os.path.exists(source), \
|
|
|
|
"Missing the source %s to copy from" % source
|
|
|
|
assert not os.path.exists(destination), \
|
|
|
|
"Destination %s already exists" % destination
|
|
|
|
|
|
|
|
parent_dir = os.path.dirname(destination)
|
|
|
|
if not os.path.exists(parent_dir):
|
|
|
|
os.makedirs(parent_dir)
|
|
|
|
|
|
|
|
try:
|
2017-10-29 17:35:59 +01:00
|
|
|
cmd = ['cp', '--sparse=always',
|
2016-07-12 17:53:31 +02:00
|
|
|
'--reflink=auto', source, destination]
|
|
|
|
subprocess.check_call(cmd)
|
2016-04-15 20:40:53 +02:00
|
|
|
except subprocess.CalledProcessError:
|
|
|
|
raise IOError('Error while copying {!r} to {!r}'.format(source,
|
|
|
|
destination))
|
|
|
|
|
|
|
|
|
2016-05-21 03:38:27 +02:00
|
|
|
def _remove_if_exists(path):
|
2016-06-21 12:47:47 +02:00
|
|
|
''' Removes a file if it exist, silently succeeds if file does not exist '''
|
2016-05-21 03:38:27 +02:00
|
|
|
if os.path.exists(path):
|
|
|
|
os.remove(path)
|
2016-04-15 20:40:53 +02:00
|
|
|
|
|
|
|
|
|
|
|
def _check_path(path):
|
|
|
|
''' Raise an StoragePoolException if ``path`` does not exist'''
|
|
|
|
if not os.path.exists(path):
|
2016-07-12 17:53:31 +02:00
|
|
|
msg = 'Missing image file: %s' % path
|
|
|
|
raise qubes.storage.StoragePoolException(msg)
|