qvm-add-template 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #!/usr/bin/python2
  2. # -*- encoding: utf8 -*-
  3. #
  4. # The Qubes OS Project, http://www.qubes-os.org
  5. #
  6. # Copyright (C) 2010 Joanna Rutkowska <joanna@invisiblethingslab.com>
  7. #
  8. # This program is free software; you can redistribute it and/or
  9. # modify it under the terms of the GNU General Public License
  10. # as published by the Free Software Foundation; either version 2
  11. # of the License, or (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License
  19. # along with this program; if not, write to the Free Software
  20. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  21. #
  22. #
  23. from qubes.qubes import QubesVmCollection
  24. from qubes.qubes import QubesException
  25. from optparse import OptionParser;
  26. import sys
  27. import os
  28. def main():
  29. usage = "usage: %prog [options] <vm-template-name>\n"\
  30. "Adds an already installed template to the Qubes DB"
  31. parser = OptionParser (usage)
  32. parser.add_option ("-p", "--path", dest="dir_path",
  33. help="Specify path to the template directory")
  34. parser.add_option ("-c", "--conf", dest="conf_file",
  35. help="Specify the Xen VM .conf file to use\
  36. (relative to the template dir path)")
  37. parser.add_option ("--rpm", action="store_true", dest="installed_by_rpm",
  38. help="Template files have been installed by RPM", default=False)
  39. parser.add_option ("--force-root", action="store_true", dest="force_root", default=False,
  40. help="Force to run, even with root privileges")
  41. (options, args) = parser.parse_args ()
  42. if (len (args) != 1):
  43. parser.error ("You must specify at least the TemplateVM name!")
  44. vmname = args[0]
  45. if os.geteuid() == 0:
  46. if not options.force_root and not options.installed_by_rpm:
  47. print >> sys.stderr, "*** Running this tool as root is strongly discouraged, this will lead you in permissions problems."
  48. print >> sys.stderr, "Retry as unprivileged user."
  49. print >> sys.stderr, "... or use --force-root to continue anyway."
  50. exit(1)
  51. qvm_collection = QubesVmCollection()
  52. qvm_collection.lock_db_for_writing()
  53. qvm_collection.load()
  54. if qvm_collection.get_vm_by_name(vmname) is not None:
  55. print >> sys.stderr, "ERROR: A VM with the name '{0}' already exists in the system.".format(vmname)
  56. exit(1)
  57. vm = qvm_collection.add_new_vm("QubesTemplateVm", name=vmname,
  58. conf_file=options.conf_file,
  59. dir_path=options.dir_path,
  60. installed_by_rpm=options.installed_by_rpm)
  61. try:
  62. vm.verify_files()
  63. except QubesException as err:
  64. print >> sys.stderr, "ERROR: {0}".format(err)
  65. qvm_collection.pop(vm.qid)
  66. exit (1)
  67. qvm_collection.save()
  68. qvm_collection.unlock_db()
  69. main()