qvm-add-appvm 3.3 KB

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