qvm-add-appvm 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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] <appvm-name> <vm-template-name>\n\n"\
  29. "Adds an already installed appvm to the Qubes DB\n"\
  30. "WARNING: Noramlly you would not need this command,\n"\
  31. "and you would use qvm-create instead!"
  32. parser = OptionParser (usage)
  33. parser.add_option ("-p", "--path", dest="dir_path",
  34. help="Specify path to the template directory")
  35. parser.add_option ("-c", "--conf", dest="conf_file",
  36. help="Specify the Xen VM .conf file to use\
  37. (relative to the template dir path)")
  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) != 2):
  42. parser.error ("You must specify at least the AppVM and TemplateVM names!")
  43. vmname = args[0]
  44. templatename = args[1]
  45. if os.geteuid() == 0:
  46. if not options.force_root:
  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. template = qvm_collection.get_vm_by_name(templatename)
  58. if template is None:
  59. print >> sys.stderr, "ERROR: A Template VM with the name '{0}' does not exist in the system.".format(templatename)
  60. exit(1)
  61. vm = qvm_collection.add_new_vm("QubesAppVm", name=vmname, template=template,
  62. conf_file=options.conf_file,
  63. dir_path=options.dir_path)
  64. try:
  65. vm.verify_files()
  66. except QubesException as err:
  67. print >> sys.stderr, "ERROR: {0}".format(err)
  68. qvm_collection.pop(vm.qid)
  69. exit (1)
  70. qvm_collection.save()
  71. qvm_collection.unlock_db()
  72. main()