qvm-remove 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #!/usr/bin/python2.6
  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 optparse import OptionParser;
  24. def main():
  25. usage = "usage: %prog [options] <vm-name>"
  26. parser = OptionParser (usage)
  27. parser.add_option ("-q", "--quiet", action="store_false", dest="verbose", default=True)
  28. parser.add_option ("--just-db", action="store_true", dest="remove_from_db_only", default=False,
  29. help="Remove only from the Qubes Xen DB, do not remove any files")
  30. (options, args) = parser.parse_args ()
  31. if (len (args) != 1):
  32. parser.error ("You must specify VM name!")
  33. vmname = args[0]
  34. qvm_collection = QubesVmCollection()
  35. qvm_collection.lock_db_for_writing()
  36. qvm_collection.load()
  37. vm = qvm_collection.get_vm_by_name(vmname)
  38. if vm is None or vm.qid not in qvm_collection:
  39. print "A VM with the name '{0}' does not exist in the system.".format(vmname)
  40. exit(1)
  41. if vm.is_templete():
  42. dependent_vms = qvm_collection.get_vms_based_on(vm.qid)
  43. if len(dependent_vms) > 0:
  44. print "The following AppVMs use '{0}' as a template:".format(vmname)
  45. for vm in dependent_vms:
  46. print "{name:<12} (qid={qid})".format(qid=vm.qid, name=vm.name)
  47. print "Please remove those VMs first, or use the --force option."
  48. exit (1)
  49. if qvm_collection.default_template_qid == vm.qid:
  50. qvm_collection.default_template_qid = None
  51. if vm.is_netvm():
  52. if qvm_collection.default_netvm_qid == vm.qid:
  53. qvm_collection.default_netvm_qid = None
  54. if vm.is_running():
  55. print "Cannot remove a running VM, stop it first"
  56. exit (1)
  57. if vm.installed_by_rpm and not options.remove_from_db_only:
  58. if options.verbose:
  59. print "This VM has been installed by RPM, use rpm -e <pkg name> to remove it!"
  60. exit (1)
  61. try:
  62. if options.verbose:
  63. print "--> Removing from Xen Storage..."
  64. vm.remove_from_xen_storage()
  65. except (IOError, OSError) as err:
  66. print "Warning: {0}".format(err)
  67. # Do not exit, perhaps the VM was not in the Xen store
  68. # so just remove it from Qubes DB
  69. try:
  70. if vm.installed_by_rpm:
  71. if options.verbose:
  72. print "--> VM installed by RPM, leaving all the files on disk"
  73. else:
  74. if options.verbose:
  75. print "--> Removing all the files on disk..."
  76. #TODO: ask for confirmation, perhaps?
  77. vm.remove_from_disk()
  78. except (IOError, OSError) as err:
  79. print "Warning: {0}".format(err)
  80. # Do not exit, perhaps the VM files were somehow removed
  81. # so just remove it from Qubes DB
  82. qvm_collection.pop(vm.qid)
  83. qvm_collection.save()
  84. qvm_collection.unlock_db()
  85. main()