qvm-add-template 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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,vmm
  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 hasattr(os, "geteuid") and 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. vmm.offline_mode = True
  52. qvm_collection = QubesVmCollection()
  53. qvm_collection.lock_db_for_writing()
  54. qvm_collection.load()
  55. if qvm_collection.get_vm_by_name(vmname) is not None:
  56. print >> sys.stderr, "ERROR: A VM with the name '{0}' already exists in the system.".format(vmname)
  57. exit(1)
  58. vm = qvm_collection.add_new_vm("QubesTemplateVm", name=vmname,
  59. conf_file=options.conf_file,
  60. dir_path=options.dir_path,
  61. installed_by_rpm=options.installed_by_rpm)
  62. try:
  63. vm.verify_files()
  64. except QubesException as err:
  65. print >> sys.stderr, "ERROR: {0}".format(err)
  66. qvm_collection.pop(vm.qid)
  67. exit (1)
  68. qvm_collection.save()
  69. qvm_collection.unlock_db()
  70. main()