003QubesTemplateVm.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #!/usr/bin/python2
  2. # -*- coding: utf-8 -*-
  3. #
  4. # The Qubes OS Project, http://www.qubes-os.org
  5. #
  6. # Copyright (C) 2010 Joanna Rutkowska <joanna@invisiblethingslab.com>
  7. # Copyright (C) 2013 Marek Marczykowski <marmarek@invisiblethingslab.com>
  8. #
  9. # This program is free software; you can redistribute it and/or
  10. # modify it under the terms of the GNU General Public License
  11. # as published by the Free Software Foundation; either version 2
  12. # of the License, or (at your option) any later version.
  13. #
  14. # This program is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU General Public License
  20. # along with this program; if not, write to the Free Software
  21. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  22. #
  23. #
  24. import os
  25. import subprocess
  26. import sys
  27. from qubes.qubes import QubesVm,register_qubes_vm_class,dry_run
  28. from qubes.qubes import QubesVmCollection,QubesException,QubesVmLabels
  29. from qubes.qubes import defaults,system_path,vm_files,vmm
  30. class QubesTemplateVm(QubesVm):
  31. """
  32. A class that represents an TemplateVM. A child of QubesVm.
  33. """
  34. # In which order load this VM type from qubes.xml
  35. load_order = 50
  36. def get_attrs_config(self):
  37. attrs_config = super(QubesTemplateVm, self).get_attrs_config()
  38. attrs_config['dir_path']['func'] = \
  39. lambda value: value if value is not None else \
  40. os.path.join(system_path["qubes_templates_dir"], self.name)
  41. attrs_config['label']['default'] = defaults["template_label"]
  42. return attrs_config
  43. def __init__(self, **kwargs):
  44. super(QubesTemplateVm, self).__init__(**kwargs)
  45. self.appvms = QubesVmCollection()
  46. @property
  47. def type(self):
  48. return "TemplateVM"
  49. @property
  50. def updateable(self):
  51. return True
  52. def is_template(self):
  53. return True
  54. def get_firewall_defaults(self):
  55. return { "rules": list(), "allow": False, "allowDns": False, "allowIcmp": False, "allowYumProxy": True }
  56. @property
  57. def rootcow_img(self):
  58. return self.storage.rootcow_img
  59. def clone_disk_files(self, src_vm, verbose):
  60. if dry_run:
  61. return
  62. super(QubesTemplateVm, self).clone_disk_files(src_vm=src_vm, verbose=verbose)
  63. # Create root-cow.img
  64. self.commit_changes(verbose=verbose)
  65. def commit_changes (self, verbose = False):
  66. self.log.debug('commit_changes()')
  67. if not vmm.offline_mode:
  68. assert not self.is_running(), "Attempt to commit changes on running Template VM!"
  69. if verbose:
  70. print >> sys.stderr, "--> Commiting template updates... COW: {0}...".format (self.rootcow_img)
  71. if dry_run:
  72. return
  73. self.storage.commit_template_changes()
  74. register_qubes_vm_class(QubesTemplateVm)