qvm-remove 4.0 KB

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