2013-07-29 03:51:39 +02:00
|
|
|
#!/usr/bin/python2
|
|
|
|
#
|
|
|
|
# The Qubes OS Project, http://www.qubes-os.org
|
|
|
|
#
|
|
|
|
# Copyright (C) 2013 Marek Marczykowski <marmarek@invisiblethingslab.com>
|
|
|
|
#
|
|
|
|
# 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
|
2015-11-07 18:58:49 +01:00
|
|
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
|
|
|
|
# USA.
|
2013-07-29 03:51:39 +02:00
|
|
|
#
|
|
|
|
|
|
|
|
from __future__ import absolute_import
|
|
|
|
|
|
|
|
import os
|
|
|
|
import os.path
|
2015-11-07 18:58:49 +01:00
|
|
|
import re
|
2013-07-29 03:51:39 +02:00
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
|
2015-11-21 13:22:27 +01:00
|
|
|
from qubes.qubes import QubesException, vm_files
|
2015-11-07 18:58:49 +01:00
|
|
|
from qubes.storage import Pool, QubesVmStorage
|
2015-02-09 06:02:20 +01:00
|
|
|
|
2013-07-29 03:51:39 +02:00
|
|
|
|
2015-11-07 19:04:51 +01:00
|
|
|
class XenStorage(QubesVmStorage):
|
2013-07-29 03:51:39 +02:00
|
|
|
"""
|
|
|
|
Class for VM storage of Xen VMs.
|
|
|
|
"""
|
|
|
|
|
2015-11-07 22:31:50 +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
|
|
|
|
|
2015-11-07 19:04:51 +01:00
|
|
|
super(XenStorage, self).__init__(vm, **kwargs)
|
2013-07-29 03:51:39 +02:00
|
|
|
|
2015-11-07 22:31:50 +01:00
|
|
|
self.vmdir = vmdir
|
|
|
|
|
2015-02-09 06:02:20 +01:00
|
|
|
if self.vm.is_template():
|
2015-11-07 22:31:50 +01:00
|
|
|
self.rootcow_img = os.path.join(self.vmdir,
|
|
|
|
vm_files["rootcow_img"])
|
2015-02-09 06:02:20 +01:00
|
|
|
else:
|
|
|
|
self.rootcow_img = None
|
|
|
|
|
2015-11-08 19:29:41 +01:00
|
|
|
self.private_img = os.path.join(vmdir, 'private.img')
|
|
|
|
if self.vm.template:
|
|
|
|
self.root_img = self.vm.template.root_img
|
|
|
|
else:
|
|
|
|
self.root_img = os.path.join(vmdir, 'root.img')
|
|
|
|
self.volatile_img = os.path.join(vmdir, 'volatile.img')
|
|
|
|
|
2015-11-22 18:51:55 +01:00
|
|
|
def root_dev_config(self):
|
2016-01-06 03:27:50 +01:00
|
|
|
if self.vm.is_template() and self.rootcow_img:
|
2015-11-22 17:18:42 +01:00
|
|
|
return self.format_disk_dev(
|
2016-01-06 03:27:50 +01:00
|
|
|
"{root}:{rootcow}".format(
|
|
|
|
root=self.root_img, rootcow=self.rootcow_img),
|
2013-07-29 03:51:39 +02:00
|
|
|
"block-origin", self.root_dev, True)
|
2016-01-06 03:43:32 +01:00
|
|
|
elif self.vm.template and not hasattr(self.vm, 'kernel'):
|
|
|
|
# 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
|
2015-11-22 17:18:42 +01:00
|
|
|
return self.format_disk_dev(
|
2016-01-06 03:27:50 +01:00
|
|
|
"{root}:{volatile}".format(
|
|
|
|
root=self.vm.template.storage.root_img,
|
|
|
|
volatile=self.volatile_img),
|
2015-07-01 04:35:09 +02:00
|
|
|
"block-snapshot", self.root_dev, True)
|
2013-07-29 03:51:39 +02:00
|
|
|
elif self.vm.template:
|
2015-07-01 04:35:09 +02: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
|
2015-11-22 17:18:42 +01:00
|
|
|
return self.format_disk_dev(
|
2016-01-06 03:27:50 +01:00
|
|
|
"{root}:{rootcow}".format(
|
|
|
|
root=self.root_img,
|
|
|
|
rootcow=self.vm.template.storage.rootcow_img),
|
2013-07-29 03:51:39 +02:00
|
|
|
"block-snapshot", self.root_dev, False)
|
|
|
|
else:
|
2015-11-22 17:18:42 +01:00
|
|
|
return self.format_disk_dev(
|
2016-01-06 03:27:50 +01:00
|
|
|
"{root}".format(root=self.root_img),
|
2013-07-29 03:51:39 +02:00
|
|
|
None, self.root_dev, True)
|
|
|
|
|
2015-11-22 18:51:55 +01:00
|
|
|
def private_dev_config(self):
|
|
|
|
return self.format_disk_dev(self.private_img, None,
|
|
|
|
self.private_dev, True)
|
2013-07-29 03:51:39 +02:00
|
|
|
|
2015-11-22 18:51:55 +01:00
|
|
|
def volatile_dev_config(self):
|
|
|
|
return self.format_disk_dev(self.volatile_img, None,
|
|
|
|
self.volatile_dev, True)
|
2013-07-29 03:51:39 +02:00
|
|
|
|
|
|
|
def create_on_disk_private_img(self, verbose, source_template = None):
|
|
|
|
if source_template:
|
|
|
|
template_priv = source_template.private_img
|
|
|
|
if verbose:
|
|
|
|
print >> sys.stderr, "--> Copying the template's private image: {0}".\
|
|
|
|
format(template_priv)
|
|
|
|
self._copy_file(template_priv, self.private_img)
|
|
|
|
else:
|
|
|
|
f_private = open (self.private_img, "a+b")
|
|
|
|
f_private.truncate (self.private_img_size)
|
|
|
|
f_private.close ()
|
|
|
|
|
|
|
|
def create_on_disk_root_img(self, verbose, source_template = None):
|
|
|
|
if source_template:
|
|
|
|
if not self.vm.updateable:
|
|
|
|
# just use template's disk
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
template_root = source_template.root_img
|
|
|
|
if verbose:
|
|
|
|
print >> sys.stderr, "--> Copying the template's root image: {0}".\
|
|
|
|
format(template_root)
|
|
|
|
|
|
|
|
self._copy_file(template_root, self.root_img)
|
|
|
|
else:
|
|
|
|
f_root = open (self.root_img, "a+b")
|
|
|
|
f_root.truncate (self.root_img_size)
|
|
|
|
f_root.close ()
|
2015-02-09 06:02:20 +01:00
|
|
|
if self.vm.is_template():
|
|
|
|
self.commit_template_changes()
|
|
|
|
|
|
|
|
def rename(self, old_name, new_name):
|
2015-11-07 19:04:51 +01:00
|
|
|
super(XenStorage, self).rename(old_name, new_name)
|
2015-02-09 06:02:20 +01:00
|
|
|
|
|
|
|
old_dirpath = os.path.join(os.path.dirname(self.vmdir), old_name)
|
|
|
|
if self.rootcow_img:
|
|
|
|
self.rootcow_img = self.rootcow_img.replace(old_dirpath,
|
|
|
|
self.vmdir)
|
2013-07-29 03:51:39 +02:00
|
|
|
|
|
|
|
def resize_private_img(self, size):
|
|
|
|
f_private = open (self.private_img, "a+b")
|
|
|
|
f_private.truncate (size)
|
|
|
|
f_private.close ()
|
|
|
|
|
|
|
|
# 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 self.vm.is_template()
|
2015-07-01 04:35:09 +02:00
|
|
|
if not self.rootcow_img:
|
|
|
|
return
|
2015-02-09 06:02:20 +01:00
|
|
|
if os.path.exists (self.rootcow_img):
|
|
|
|
os.rename (self.rootcow_img, self.rootcow_img + '.old')
|
2013-07-29 03:51:39 +02:00
|
|
|
|
|
|
|
old_umask = os.umask(002)
|
2015-02-09 06:02:20 +01:00
|
|
|
f_cow = open (self.rootcow_img, "w")
|
2013-07-29 03:51:39 +02:00
|
|
|
f_root = open (self.root_img, "r")
|
|
|
|
f_root.seek(0, os.SEEK_END)
|
|
|
|
f_cow.truncate (f_root.tell()) # make empty sparse file of the same size as root.img
|
|
|
|
f_cow.close ()
|
|
|
|
f_root.close()
|
|
|
|
os.umask(old_umask)
|
|
|
|
|
2015-07-01 04:35:09 +02:00
|
|
|
def reset_volatile_storage(self, verbose = False, source_template = None):
|
|
|
|
if source_template is None:
|
|
|
|
source_template = self.vm.template
|
|
|
|
|
|
|
|
if source_template is not None:
|
|
|
|
# template-based VM with only one device-mapper layer -
|
|
|
|
# volatile.img used as upper layer on root.img, no root-cow.img
|
|
|
|
# intermediate layer
|
|
|
|
if not source_template.storage.rootcow_img:
|
|
|
|
if os.path.exists(self.volatile_img):
|
|
|
|
if self.vm.debug:
|
|
|
|
if os.path.getmtime(source_template.storage.root_img)\
|
|
|
|
> os.path.getmtime(self.volatile_img):
|
|
|
|
if verbose:
|
|
|
|
print >>sys.stderr, "--> WARNING: template have changed, resetting root.img"
|
|
|
|
else:
|
|
|
|
if verbose:
|
|
|
|
print >>sys.stderr, "--> Debug mode: not resetting root.img"
|
|
|
|
print >>sys.stderr, "--> Debug mode: if you want to force root.img reset, either update template VM, or remove volatile.img file"
|
|
|
|
return
|
|
|
|
os.remove(self.volatile_img)
|
|
|
|
|
|
|
|
f_volatile = open(self.volatile_img, "w")
|
|
|
|
f_root = open(source_template.storage.root_img, "r")
|
|
|
|
f_root.seek(0, os.SEEK_END)
|
|
|
|
f_volatile.truncate(f_root.tell()) # make empty sparse file of the same size as root.img
|
|
|
|
f_volatile.close()
|
|
|
|
f_root.close()
|
|
|
|
return
|
2015-11-07 19:04:51 +01:00
|
|
|
super(XenStorage, self).reset_volatile_storage(
|
2015-07-01 04:35:09 +02:00
|
|
|
verbose=verbose, source_template=source_template)
|
2015-07-25 04:17:59 +02:00
|
|
|
|
|
|
|
def prepare_for_vm_startup(self, verbose):
|
2015-11-07 19:04:51 +01:00
|
|
|
super(XenStorage, self).prepare_for_vm_startup(verbose=verbose)
|
2015-07-25 04:17:59 +02:00
|
|
|
|
|
|
|
if self.drive is not None:
|
|
|
|
(drive_type, drive_domain, drive_path) = self.drive.split(":")
|
|
|
|
if drive_domain.lower() != "dom0":
|
|
|
|
try:
|
|
|
|
# FIXME: find a better way to access QubesVmCollection
|
|
|
|
drive_vm = self.vm._collection.get_vm_by_name(drive_domain)
|
|
|
|
# prepare for improved QubesVmCollection
|
|
|
|
if drive_vm is None:
|
|
|
|
raise KeyError
|
|
|
|
if not drive_vm.is_running():
|
|
|
|
raise QubesException(
|
|
|
|
"VM '{}' holding '{}' isn't running".format(
|
|
|
|
drive_domain, drive_path))
|
|
|
|
except KeyError:
|
|
|
|
raise QubesException(
|
|
|
|
"VM '{}' holding '{}' does not exists".format(
|
|
|
|
drive_domain, drive_path))
|
2015-11-07 18:58:49 +01:00
|
|
|
|
|
|
|
|
|
|
|
class XenPool(Pool):
|
2015-11-07 22:31:50 +01:00
|
|
|
|
2015-11-21 14:46:23 +01:00
|
|
|
def __init__(self, vm, dir_path):
|
2015-11-21 13:22:27 +01:00
|
|
|
super(XenPool, self).__init__(vm, dir_path)
|
2015-11-07 19:32:38 +01:00
|
|
|
|
|
|
|
def getStorage(self):
|
2015-11-07 22:31:50 +01:00
|
|
|
""" Returns an instantiated ``XenStorage``. """
|
2015-11-21 13:22:27 +01:00
|
|
|
return XenStorage(self.vm, vmdir=self.vmdir)
|