qvm-add-template 3.1 KB

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