2015-01-19 18:03:23 +01:00
|
|
|
#!/usr/bin/python2 -O
|
|
|
|
# vim: fileencoding=utf-8
|
|
|
|
|
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>
|
|
|
|
#
|
2015-01-19 18:03:23 +01:00
|
|
|
# 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.
|
2015-01-16 15:33:03 +01:00
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
#
|
2015-01-19 18:03:23 +01:00
|
|
|
# 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.
|
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
|
|
|
|
|
|
|
|
import qubes
|
|
|
|
import qubes.config
|
|
|
|
import qubes.storage
|
|
|
|
import qubes.vm.templatevm
|
2016-03-27 22:30:33 +02:00
|
|
|
from qubes.devices import BlockDevice
|
2015-01-16 15:33:03 +01:00
|
|
|
|
|
|
|
|
2016-03-03 01:05:23 +01:00
|
|
|
class XenStorage(qubes.storage.Storage):
|
2015-01-16 15:33:03 +01:00
|
|
|
'''Class for VM storage of Xen VMs.
|
|
|
|
'''
|
|
|
|
|
|
|
|
root_dev = 'xvda'
|
|
|
|
private_dev = 'xvdb'
|
|
|
|
volatile_dev = 'xvdc'
|
|
|
|
modules_dev = 'xvdd'
|
|
|
|
|
2016-03-03 01:05:23 +01:00
|
|
|
def __init__(self, vm, vmdir, **kwargs):
|
|
|
|
""" Instantiate the storage.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
vm: a QubesVM
|
|
|
|
vmdir: the root directory of the pool
|
|
|
|
"""
|
|
|
|
assert vm is not None
|
|
|
|
assert vmdir is not None
|
|
|
|
|
|
|
|
super(XenStorage, self).__init__(vm, **kwargs)
|
|
|
|
|
|
|
|
self.vmdir = vmdir
|
|
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
def private_img(self):
|
|
|
|
'''Path to the private image'''
|
|
|
|
return self.abspath(qubes.config.vm_files['private_img'])
|
|
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
def root_img(self):
|
|
|
|
'''Path to the root image'''
|
2016-02-11 05:44:38 +01:00
|
|
|
return self.vm.template.storage.root_img \
|
|
|
|
if hasattr(self.vm, 'template') and self.vm.template \
|
2016-03-03 01:05:23 +01:00
|
|
|
else self.abspath(qubes.config.vm_files['root_img'])
|
|
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
def rootcow_img(self):
|
|
|
|
'''Path to the root COW image'''
|
|
|
|
|
|
|
|
if isinstance(self.vm, qubes.vm.templatevm.TemplateVM):
|
|
|
|
return self.abspath(qubes.config.vm_files['rootcow_img'])
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
def volatile_img(self):
|
|
|
|
'''Path to the volatile image'''
|
|
|
|
return self.abspath(qubes.config.vm_files['volatile_img'])
|
|
|
|
|
2016-03-27 22:30:33 +02:00
|
|
|
def format_disk_dev(self, path, name, script=None, rw=True, devtype='disk',
|
|
|
|
domain=None):
|
2015-01-16 15:33:03 +01:00
|
|
|
|
2016-03-27 22:30:33 +02:00
|
|
|
return BlockDevice(path, name, script, rw, domain, devtype)
|
2015-01-16 15:33:03 +01:00
|
|
|
|
2016-03-03 01:05:23 +01:00
|
|
|
def root_dev_config(self):
|
2016-03-27 22:30:33 +02:00
|
|
|
dev_name = 'root'
|
2015-01-16 15:33:03 +01:00
|
|
|
if isinstance(self.vm, qubes.vm.templatevm.TemplateVM):
|
2016-03-03 01:05:23 +01:00
|
|
|
return self.format_disk_dev(
|
|
|
|
'{root}:{rootcow}'.format(
|
|
|
|
root=self.root_img,
|
|
|
|
rootcow=self.rootcow_img),
|
2016-03-27 22:30:33 +02:00
|
|
|
dev_name,
|
2015-01-16 15:33:03 +01:00
|
|
|
script='block-origin')
|
|
|
|
|
2016-03-03 01:05:23 +01:00
|
|
|
elif self.vm.hvm and hasattr(self.vm, 'template'):
|
|
|
|
# HVM template-based VM - only one device-mapper layer, in dom0
|
|
|
|
# (root+volatile)
|
|
|
|
# HVM detection based on 'kernel' property is massive hack,
|
|
|
|
# but taken from assumption that VM needs Qubes-specific kernel
|
|
|
|
# (actually initramfs) to assemble the second layer of device-mapper
|
|
|
|
return self.format_disk_dev(
|
|
|
|
'{root}:{volatile}'.format(
|
|
|
|
root=self.vm.template.storage.root_img,
|
|
|
|
volatile=self.volatile_img),
|
2016-03-27 22:30:33 +02:00
|
|
|
dev_name,
|
2016-03-03 01:05:23 +01:00
|
|
|
script='block-snapshot')
|
|
|
|
|
2015-01-16 15:33:03 +01:00
|
|
|
elif hasattr(self.vm, 'template'):
|
2016-03-03 01:05:23 +01:00
|
|
|
# any other template-based VM - two device-mapper layers: one
|
|
|
|
# in dom0 (here) from root+root-cow, and another one from
|
|
|
|
# this+volatile.img
|
|
|
|
return self.format_disk_dev(
|
|
|
|
'{root}:{template_rootcow}'.format(
|
|
|
|
root=self.root_img,
|
|
|
|
template_rootcow=self.vm.template.storage.rootcow_img),
|
2015-01-16 15:33:03 +01:00
|
|
|
self.root_dev,
|
2015-10-02 13:30:23 +02:00
|
|
|
script='block-snapshot',
|
|
|
|
rw=False)
|
2015-01-16 15:33:03 +01:00
|
|
|
|
|
|
|
else:
|
2016-03-03 01:05:23 +01:00
|
|
|
# standalone qube
|
2016-03-27 22:30:33 +02:00
|
|
|
return self.format_disk_dev(self.root_img, dev_name)
|
2015-10-02 16:02:51 +02:00
|
|
|
|
2015-01-16 15:33:03 +01:00
|
|
|
|
2016-03-03 01:05:23 +01:00
|
|
|
def private_dev_config(self):
|
2016-03-27 22:30:33 +02:00
|
|
|
return self.format_disk_dev(self.private_img, 'private')
|
2015-01-16 15:33:03 +01:00
|
|
|
|
2016-03-03 01:05:23 +01:00
|
|
|
def volatile_dev_config(self):
|
2016-03-27 22:30:33 +02:00
|
|
|
return self.format_disk_dev(self.volatile_img, 'volatile')
|
2015-01-16 15:33:03 +01:00
|
|
|
|
|
|
|
|
|
|
|
def create_on_disk_private_img(self, source_template=None):
|
|
|
|
if source_template is None:
|
|
|
|
f_private = open(self.private_img, 'a+b')
|
|
|
|
f_private.truncate(self.private_img_size)
|
|
|
|
f_private.close()
|
|
|
|
|
|
|
|
else:
|
|
|
|
self.vm.log.info("Copying the template's private image: {}".format(
|
2016-02-11 05:43:39 +01:00
|
|
|
source_template.storage.private_img))
|
|
|
|
self._copy_file(source_template.storage.private_img,
|
|
|
|
self.private_img)
|
2015-01-16 15:33:03 +01:00
|
|
|
|
|
|
|
|
|
|
|
def create_on_disk_root_img(self, source_template=None):
|
|
|
|
if source_template is None:
|
2015-01-19 17:06:30 +01:00
|
|
|
fd = open(self.root_img, 'a+b')
|
|
|
|
fd.truncate(self.root_img_size)
|
|
|
|
fd.close()
|
2015-01-16 15:33:03 +01:00
|
|
|
|
|
|
|
elif self.vm.updateable:
|
|
|
|
# if not updateable, just use template's disk
|
|
|
|
self.vm.log.info("--> Copying the template's root image: {}".format(
|
2016-02-11 05:43:39 +01:00
|
|
|
source_template.storage.root_img))
|
|
|
|
self._copy_file(source_template.storage.root_img, self.root_img)
|
2015-01-16 15:33:03 +01:00
|
|
|
|
|
|
|
|
|
|
|
def resize_private_img(self, size):
|
2015-01-19 17:06:30 +01:00
|
|
|
fd = open(self.private_img, 'a+b')
|
|
|
|
fd.truncate(size)
|
|
|
|
fd.close()
|
2015-01-16 15:33:03 +01:00
|
|
|
|
|
|
|
# find loop device if any
|
|
|
|
p = subprocess.Popen(
|
|
|
|
['sudo', 'losetup', '--associated', self.private_img],
|
|
|
|
stdout=subprocess.PIPE)
|
|
|
|
result = p.communicate()
|
|
|
|
|
|
|
|
m = re.match(r'^(/dev/loop\d+):\s', result[0])
|
|
|
|
if m is not None:
|
|
|
|
loop_dev = m.group(1)
|
|
|
|
|
|
|
|
# resize loop device
|
|
|
|
subprocess.check_call(
|
|
|
|
['sudo', 'losetup', '--set-capacity', loop_dev])
|
|
|
|
|
|
|
|
|
|
|
|
def commit_template_changes(self):
|
|
|
|
assert isinstance(self.vm, qubes.vm.templatevm.TemplateVM)
|
|
|
|
|
|
|
|
# TODO: move rootcow_img to this class; the same for vm.is_outdated()
|
|
|
|
if os.path.exists(self.vm.rootcow_img):
|
2015-01-19 17:06:30 +01:00
|
|
|
os.rename(self.vm.rootcow_img, self.vm.rootcow_img + '.old')
|
2015-01-16 15:33:03 +01:00
|
|
|
|
|
|
|
old_umask = os.umask(002)
|
|
|
|
f_cow = open(self.vm.rootcow_img, 'w')
|
|
|
|
f_root = open(self.root_img, 'r')
|
|
|
|
f_root.seek(0, os.SEEK_END)
|
|
|
|
# make empty sparse file of the same size as root.img
|
|
|
|
f_cow.truncate(f_root.tell())
|
|
|
|
f_cow.close()
|
|
|
|
f_root.close()
|
|
|
|
os.umask(old_umask)
|
2016-03-03 01:05:23 +01:00
|
|
|
|
|
|
|
|
2016-02-10 17:02:36 +01:00
|
|
|
def reset_volatile_storage(self):
|
|
|
|
try:
|
|
|
|
# no template set, in any way (Standalone VM, Template VM)
|
|
|
|
if self.vm.template is None:
|
|
|
|
raise AttributeError
|
2016-03-03 01:05:23 +01:00
|
|
|
|
2016-02-10 17:02:36 +01:00
|
|
|
# template-based HVM with only one device-mapper layer -
|
2016-03-03 01:05:23 +01:00
|
|
|
# volatile.img used as upper layer on root.img, no root-cow.img
|
|
|
|
# intermediate layer
|
2016-02-10 17:02:36 +01:00
|
|
|
if self.vm.hvm:
|
2016-03-03 01:05:23 +01:00
|
|
|
if os.path.exists(self.volatile_img):
|
|
|
|
if self.vm.debug:
|
2016-02-10 17:02:36 +01:00
|
|
|
if os.path.getmtime(self.vm.template.storage.root_img) \
|
2016-03-03 01:05:23 +01:00
|
|
|
> os.path.getmtime(self.volatile_img):
|
|
|
|
self.vm.log.warning(
|
|
|
|
'Template have changed, resetting root.img')
|
|
|
|
else:
|
|
|
|
self.vm.log.warning(
|
|
|
|
'Debug mode: not resetting root.img; if you'
|
|
|
|
' want to force root.img reset, either'
|
|
|
|
' update template VM, or remove volatile.img'
|
|
|
|
' file.')
|
|
|
|
return
|
|
|
|
|
|
|
|
os.remove(self.volatile_img)
|
|
|
|
|
|
|
|
# FIXME stat on f_root; with open() ...
|
|
|
|
f_volatile = open(self.volatile_img, "w")
|
2016-02-10 17:02:36 +01:00
|
|
|
f_root = open(self.vm.template.storage.root_img, "r")
|
2016-01-29 17:56:33 +01:00
|
|
|
# make empty sparse file of the same size as root.img
|
2016-03-03 01:05:23 +01:00
|
|
|
f_root.seek(0, os.SEEK_END)
|
2016-01-29 17:56:33 +01:00
|
|
|
f_volatile.truncate(f_root.tell())
|
2016-03-03 01:05:23 +01:00
|
|
|
f_volatile.close()
|
|
|
|
f_root.close()
|
|
|
|
return # XXX why is that? super() does not run
|
2016-02-10 17:02:36 +01:00
|
|
|
except AttributeError: # self.vm.template
|
|
|
|
pass
|
2016-03-03 01:05:23 +01:00
|
|
|
|
2016-02-10 17:02:36 +01:00
|
|
|
super(XenStorage, self).reset_volatile_storage()
|
2016-03-03 01:05:23 +01:00
|
|
|
|
|
|
|
|
|
|
|
def prepare_for_vm_startup(self):
|
|
|
|
super(XenStorage, self).prepare_for_vm_startup()
|
|
|
|
|
|
|
|
if self.drive is not None:
|
2016-01-29 17:56:33 +01:00
|
|
|
# pylint: disable=unused-variable
|
2016-03-03 01:05:23 +01:00
|
|
|
(drive_type, drive_domain, drive_path) = self.drive.split(":")
|
|
|
|
|
|
|
|
if drive_domain.lower() != "dom0":
|
|
|
|
# XXX "VM '{}' holding '{}' does not exists".format(
|
|
|
|
drive_vm = self.vm.app.domains[drive_domain]
|
|
|
|
|
|
|
|
if not drive_vm.is_running():
|
|
|
|
raise qubes.exc.QubesVMNotRunningError(drive_vm,
|
|
|
|
'VM {!r} holding {!r} isn\'t running'.format(
|
|
|
|
drive_domain, drive_path))
|
|
|
|
|
|
|
|
if self.rootcow_img and not os.path.exists(self.rootcow_img):
|
|
|
|
self.commit_template_changes()
|
|
|
|
|
|
|
|
class XenPool(qubes.storage.Pool):
|
|
|
|
def get_storage(self):
|
|
|
|
""" Returns an instantiated ``XenStorage``. """
|
|
|
|
return XenStorage(self.vm, vmdir=self.vmdir)
|