003QubesTemplateVm.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. def rootcow_img(self):
  57. return self.storage.rootcow_img
  58. def clone_disk_files(self, src_vm, verbose):
  59. if dry_run:
  60. return
  61. super(QubesTemplateVm, self).clone_disk_files(src_vm=src_vm, verbose=verbose)
  62. # Create root-cow.img
  63. self.commit_changes(verbose=verbose)
  64. def commit_changes (self, verbose = False):
  65. if not vmm.offline_mode:
  66. assert not self.is_running(), "Attempt to commit changes on running Template VM!"
  67. if verbose:
  68. print >> sys.stderr, "--> Commiting template updates... COW: {0}...".format (self.rootcow_img)
  69. if dry_run:
  70. return
  71. self.storage.commit_template_changes()
  72. register_qubes_vm_class(QubesTemplateVm)