<commit_before>#!/usr/bin/python2
#
# The Qubes OS Project, http://www.qubes-os.org
#
# Copyright (C) 2013  <NAME> <<EMAIL>>
#
# 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.
#
#

from __future__ import absolute_import

import os
import os.path
import subprocess
import sys
import re

from qubes.storage import QubesVmStorage
from qubes.qubes import QubesException, vm_files


class QubesXenVmStorage(QubesVmStorage):
    """
    Class for VM storage of Xen VMs.
    """

    def __init__(self, vm, **kwargs):
        super(QubesXenVmStorage, self).__init__(vm, **kwargs)

        self.root_dev = "xvda"
        self.private_dev = "xvdb"
        self.volatile_dev = "xvdc"
        self.modules_dev = "xvdd"

        if self.vm.is_template():
            self.rootcow_img = os.path.join(self.vmdir, vm_files["rootcow_img"])
        else:
            self.rootcow_img = None

    def _format_disk_dev(self, path, script, vdev, rw=True, type="disk", domain=None):
        if path is None:
            return ''
        template = "    <disk type='block' device='{type}'>\n" \
                   "      <driver name='phy'/>\n" \
                   "      <source dev='{path}'/>\n" \
                   "      <target dev='{vdev}' bus='xen'/>\n" \
                   "{params}" \
                   "    </disk>\n"
        params = ""
        if not rw:
            params += "      <readonly/>\n"
        if domain:
            params += "      <backenddomain name='%s'/>\n" % domain
        if script:
            params += "      <script path='%s'/>\n" % script
        return template.format(path=path, vdev=vdev, type=type,
            params=params)

    def _get_rootdev(self):
        if self.vm.is_template():
            return self._format_disk_dev(
                    "{dir}/root.img:{dir}/root-cow.img".format(
                        dir=self.vmdir),
                    "block-origin", self.root_dev, True)
        elif self.vm.template:
            return self._format_disk_dev(
                    "{dir}/root.img:{dir}/root-cow.img".format(
                        dir=self.vm.template.dir_path),
                    "block-snapshot", self.root_dev, False)
        else:
            return self._format_disk_dev(
                    "{dir}/root.img".format(dir=self.vmdir),
                    None, self.root_dev, True)

    def get_config_params(self):
        args = {}
        args['rootdev'] = self._get_rootdev()
        args['privatedev'] = \
                self._format_disk_dev(self.private_img,
                        None, self.private_dev, True)
        args['volatiledev'] = \
                self._format_disk_dev(self.volatile_img,
                        None, self.volatile_dev, True)
        if self.modules_img is not None:
            args['otherdevs'] = \
                    self._format_disk_dev(self.modules_img,<commit_msg>core/storage: fix disk handling for HVM template<commit_after>#!/usr/bin/python2
#
# The Qubes OS Project, http://www.qubes-os.org
#
# Copyright (C) 2013  <NAME> <<EMAIL>>
#
# 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.
#
#

from __future__ import absolute_import

import os
import os.path
import subprocess
import sys
import re

from qubes.storage import QubesVmStorage
from qubes.qubes import QubesException, vm_files


class QubesXenVmStorage(QubesVmStorage):
    """
    Class for VM storage of Xen VMs.
    """

    def __init__(self, vm, **kwargs):
        super(QubesXenVmStorage, self).__init__(vm, **kwargs)

        self.root_dev = "xvda"
        self.private_dev = "xvdb"
        self.volatile_dev = "xvdc"
        self.modules_dev = "xvdd"

        if self.vm.is_template():
            self.rootcow_img = os.path.join(self.vmdir, vm_files["rootcow_img"])
        else:
            self.rootcow_img = None

    def _format_disk_dev(self, path, script, vdev, rw=True, type="disk", domain=None):
        if path is None:
            return ''
        template = "    <disk type='block' device='{type}'>\n" \
                   "      <driver name='phy'/>\n" \
                   "      <source dev='{path}'/>\n" \
                   "      <target dev='{vdev}' bus='xen'/>\n" \
                   "{params}" \
                   "    </disk>\n"
        params = ""
        if not rw:
            params += "      <readonly/>\n"
        if domain:
            params += "      <backenddomain name='%s'/>\n" % domain
        if script:
            params += "      <script path='%s'/>\n" % script
        return template.format(path=path, vdev=vdev, type=type,
            params=params)

    def _get_rootdev(self):
        if self.vm.is_template() and \
                os.path.exists(os.path.join(self.vmdir, "root-cow.img")):
            return self._format_disk_dev(
                    "{dir}/root.img:{dir}/root-cow.img".format(
                        dir=self.vmdir),
                    "block-origin", self.root_dev, True)
        elif self.vm.template:
            return self._format_disk_dev(
                    "{dir}/root.img:{dir}/root-cow.img".format(
                        dir=self.vm.template.dir_path),
                    "block-snapshot", self.root_dev, False)
        else:
            return self._format_disk_dev(
                    "{dir}/root.img".format(dir=self.vmdir),
                    None, self.root_dev, True)

    def get_config_params(self):
        args = {}
        args['rootdev'] = self._get_rootdev()
        args['privatedev'] = \
                self._format_disk_dev(self.private_img,
                        None, self.private_dev, True)
        args['volatiledev'] = \
                self._format_disk_dev(self.volatile_img,
                        None, self.volatile_dev, True)
        if self.modules_img is not None:
            args['otherdevs'] = \
                    self._format_disk_dev(self.modules_img,
{'fraction_non_alphanumeric': 0.11441743503772003, 'fraction_numerical': 0.004749930148086058, 'mean_word_length': 1.9854045037531276, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1957374', 'n_tokens_mistral': 2194, 'n_tokens_neox': 2113, 'n_words': 562}