qvm-remove 4.0 KB

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