2013-01-20 21:44:13 +01:00
|
|
|
#!/usr/bin/python2
|
2014-05-18 21:01:21 +02:00
|
|
|
# -*- encoding: utf8 -*-
|
2010-04-05 20:58:57 +02:00
|
|
|
#
|
|
|
|
# The Qubes OS Project, http://www.qubes-os.org
|
|
|
|
#
|
|
|
|
# Copyright (C) 2010 Joanna Rutkowska <joanna@invisiblethingslab.com>
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU General Public License
|
|
|
|
# as published by the Free Software Foundation; either version 2
|
|
|
|
# of the License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
#
|
|
|
|
#
|
|
|
|
|
|
|
|
from qubes.qubes import QubesVmCollection
|
|
|
|
from optparse import OptionParser;
|
2011-04-06 23:55:16 +02:00
|
|
|
import os
|
2011-10-07 21:56:58 +02:00
|
|
|
import sys
|
2010-04-05 20:58:57 +02:00
|
|
|
|
|
|
|
def main():
|
|
|
|
usage = "usage: %prog [options] <vm-name>"
|
|
|
|
parser = OptionParser (usage)
|
|
|
|
parser.add_option ("-q", "--quiet", action="store_false", dest="verbose", default=True)
|
|
|
|
parser.add_option ("--just-db", action="store_true", dest="remove_from_db_only", default=False,
|
|
|
|
help="Remove only from the Qubes Xen DB, do not remove any files")
|
2011-04-06 23:52:39 +02:00
|
|
|
parser.add_option ("--force-root", action="store_true", dest="force_root", default=False,
|
|
|
|
help="Force to run, even with root privileges")
|
2010-04-05 20:58:57 +02:00
|
|
|
(options, args) = parser.parse_args ()
|
|
|
|
if (len (args) != 1):
|
|
|
|
parser.error ("You must specify VM name!")
|
|
|
|
vmname = args[0]
|
|
|
|
|
|
|
|
qvm_collection = QubesVmCollection()
|
|
|
|
qvm_collection.lock_db_for_writing()
|
|
|
|
qvm_collection.load()
|
|
|
|
vm = qvm_collection.get_vm_by_name(vmname)
|
|
|
|
if vm is None or vm.qid not in qvm_collection:
|
2011-10-07 21:56:58 +02:00
|
|
|
print >> sys.stderr, "A VM with the name '{0}' does not exist in the system.".format(vmname)
|
2010-04-05 20:58:57 +02:00
|
|
|
exit(1)
|
|
|
|
|
2015-05-23 04:41:49 +02:00
|
|
|
if hasattr(os, "geteuid") and os.geteuid() == 0 and not options.force_root:
|
2011-10-07 21:56:58 +02:00
|
|
|
print >> sys.stderr, "*** Running this tool as root is strongly discouraged, this will lead you in permissions problems."
|
2015-05-23 04:41:49 +02:00
|
|
|
print >> sys.stderr, "Retry as unprivileged user."
|
|
|
|
print >> sys.stderr, "... or use --force-root to continue anyway."
|
|
|
|
exit(1)
|
2011-04-06 23:52:39 +02:00
|
|
|
|
2011-03-05 15:13:31 +01:00
|
|
|
if vm.is_template():
|
2010-04-05 20:58:57 +02:00
|
|
|
dependent_vms = qvm_collection.get_vms_based_on(vm.qid)
|
|
|
|
if len(dependent_vms) > 0:
|
2011-10-07 21:56:58 +02:00
|
|
|
print >> sys.stderr, "The following AppVMs use '{0}' as a template:".format(vmname)
|
2010-04-05 20:58:57 +02:00
|
|
|
for vm in dependent_vms:
|
2011-10-07 21:56:58 +02:00
|
|
|
print >> sys.stderr, "{name:<12} (qid={qid})".format(qid=vm.qid, name=vm.name)
|
|
|
|
print >> sys.stderr, "Please remove those VMs first."
|
2010-04-05 20:58:57 +02:00
|
|
|
exit (1)
|
|
|
|
if qvm_collection.default_template_qid == vm.qid:
|
|
|
|
qvm_collection.default_template_qid = None
|
|
|
|
|
|
|
|
if vm.is_netvm():
|
|
|
|
if qvm_collection.default_netvm_qid == vm.qid:
|
|
|
|
qvm_collection.default_netvm_qid = None
|
|
|
|
|
|
|
|
|
|
|
|
if vm.is_running():
|
2011-10-07 21:56:58 +02:00
|
|
|
print >> sys.stderr, "Cannot remove a running VM, stop it first"
|
2010-04-05 20:58:57 +02:00
|
|
|
exit (1)
|
|
|
|
|
|
|
|
if vm.installed_by_rpm and not options.remove_from_db_only:
|
|
|
|
if options.verbose:
|
2011-10-07 21:56:58 +02:00
|
|
|
print >> sys.stderr, "This VM has been installed by RPM, use rpm -e <pkg name> to remove it!"
|
2010-04-05 20:58:57 +02:00
|
|
|
exit (1)
|
|
|
|
|
|
|
|
try:
|
|
|
|
if vm.installed_by_rpm:
|
|
|
|
if options.verbose:
|
2011-10-07 21:56:58 +02:00
|
|
|
print >> sys.stderr, "--> VM installed by RPM, leaving all the files on disk"
|
2012-03-06 14:46:36 +01:00
|
|
|
elif not options.remove_from_db_only:
|
2010-04-05 20:58:57 +02:00
|
|
|
if options.verbose:
|
|
|
|
print "--> Removing all the files on disk..."
|
|
|
|
#TODO: ask for confirmation, perhaps?
|
|
|
|
vm.remove_from_disk()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
except (IOError, OSError) as err:
|
2011-10-07 21:56:58 +02:00
|
|
|
print >> sys.stderr, "Warning: {0}".format(err)
|
2010-04-05 20:58:57 +02:00
|
|
|
# Do not exit, perhaps the VM files were somehow removed
|
|
|
|
# so just remove it from Qubes DB
|
|
|
|
|
|
|
|
|
|
|
|
qvm_collection.pop(vm.qid)
|
|
|
|
qvm_collection.save()
|
|
|
|
qvm_collection.unlock_db()
|
|
|
|
|
|
|
|
main()
|