qvm-remove 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 hasattr(os, "geteuid") and os.geteuid() == 0 and 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. if vm.is_template():
  52. dependent_vms = qvm_collection.get_vms_based_on(vm.qid)
  53. if len(dependent_vms) > 0:
  54. print >> sys.stderr, "The following AppVMs use '{0}' as a template:".format(vmname)
  55. for vm in dependent_vms:
  56. print >> sys.stderr, "{name:<12} (qid={qid})".format(qid=vm.qid, name=vm.name)
  57. print >> sys.stderr, "Please remove those VMs first."
  58. exit (1)
  59. if qvm_collection.default_template_qid == vm.qid:
  60. qvm_collection.default_template_qid = None
  61. if vm.is_netvm():
  62. if qvm_collection.default_netvm_qid == vm.qid:
  63. qvm_collection.default_netvm_qid = None
  64. if vm.is_running():
  65. print >> sys.stderr, "Cannot remove a running VM, stop it first"
  66. exit (1)
  67. if vm.installed_by_rpm and not options.remove_from_db_only:
  68. if options.verbose:
  69. print >> sys.stderr, "This VM has been installed by RPM, use yum remove <pkg name> to remove it!"
  70. exit (1)
  71. try:
  72. if vm.installed_by_rpm:
  73. if options.verbose:
  74. print >> sys.stderr, "--> VM installed by RPM, leaving all the files on disk"
  75. elif not options.remove_from_db_only:
  76. if options.verbose:
  77. print "--> Removing all the files on disk..."
  78. #TODO: ask for confirmation, perhaps?
  79. vm.remove_from_disk()
  80. except (IOError, OSError) as err:
  81. print >> sys.stderr, "Warning: {0}".format(err)
  82. # Do not exit, perhaps the VM files were somehow removed
  83. # so just remove it from Qubes DB
  84. qvm_collection.pop(vm.qid)
  85. qvm_collection.save()
  86. qvm_collection.unlock_db()
  87. main()